<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayrat Badykov on Medium]]></title>
        <description><![CDATA[Stories by Ayrat Badykov on Medium]]></description>
        <link>https://medium.com/@ayratbadykov?source=rss-ea3452eb4a27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ClY1kaFvgy_5FUYSSfduIQ.jpeg</url>
            <title>Stories by Ayrat Badykov on Medium</title>
            <link>https://medium.com/@ayratbadykov?source=rss-ea3452eb4a27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 10:50:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayratbadykov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[zkSNARK in Ethereum]]></title>
            <link>https://medium.com/@ayratbadykov/zksnark-in-ethereum-62ead3258164?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/62ead3258164</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[ethereum-blockchain]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sat, 15 Sep 2018 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-17T09:26:04.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Uyi-K59hgceHoRGu.jpg" /></figure><h3>Background</h3><p>This week at the <a href="https://github.com/poanetwork/mana">Mana Project</a>, we finished implementing the EIPs (Ethereum Improvement Proposals) for the Byzantium hard fork. The hardest EIPs to implement were those related to zero-knowledge proofs, or zkSNARKs:</p><ul><li><a href="https://eips.ethereum.org/EIPS/eip-196">EIP-196</a> <a href="https://github.com/poanetwork/mana/pull/397">Mana implementation</a></li><li><a href="https://eips.ethereum.org/EIPS/eip-197">EIP-197</a> <a href="https://github.com/poanetwork/mana/pull/406">Mana implementation</a></li></ul><p>To mark this milestone, I’ll give a short description of what a zero knowledge proof is and describe it’s usefulness. I won’t go into too much detail because it uses advanced elliptic curve cryptography based on elliptic curves over finite fields. But if you’re interested in the implementation details please visit <a href="https://github.com/poanetwork/bn">BN</a> — our implementation of BN128 elliptic curve operations.</p><h3>Zero-knowledge proof</h3><p>Zero-Knowledge Succinct Non-interactive ARgument of Knowledge, or zkSNARKs, is a form of zero-knowledge proof. A zero-knowledge proof is a type of cryptography that allows one party (the prover) to prove to another party (the verifier) that it possesses some information without revealing any details about information itself.</p><p>A zero-knowledge proof must have three properties:</p><ul><li><strong>Completeness:</strong> If the statement is true, the verifier will be convinced that the prover is honest.</li><li><strong>Soundness:</strong> If the statement is false, nothing can convince the verifier that the statement is true.</li><li><strong>Zero-knowledge:</strong> If the statement is true, the verifier can not learn any additional information from the statement.</li></ul><h4>Example</h4><p>The following example is inspired by the work of Professor Oded Goldeich. In this scenario, you are the prover and your friend is the verifier.</p><p>Imagine your friend is colorblind (and you are not). He has a red ball and a green ball, one in each hand. Your task is to prove to him the 2 balls are different colors without giving him any additional information. He takes the balls behind his back and either switches them between hands or keeps them in the same hand. He brings them back out and you inform him whether or not he switched.</p><p>Because you can see the colors, you can tell him each time with certainty whether or not he switched. After a number of rounds, your friend will become convinced the balls are different colors because you accurately tell him whether or not he switched each time. Your statements are always true, and your friend gains no knowledge about which ball is red and which is green</p><p>This example satisfies all three zero-knowledge proof properties.</p><h4>Use cases</h4><ul><li>Authentication without revealing your identity</li><li>Proving that you have some private information (for example, a private key) without revealing any information about the key</li><li>All other NP-class problems (for example mathematical operations).</li></ul><h3>Zero-knowledge proof in blockchains</h3><p>We all know that blockchain technology is based on the idea of a decentralized ledger that records transactions and so forth. But the problem in the naive “ledger” approach is that it’s transparent. Everybody can see all running transactions, payment history, balances etc. This is where zero-knowledge proofs come into play.</p><h4>Zcash</h4><p>Zcash is the first cryptocurrency that based on zero-knowledge proofs. Zcash provides privacy options backed by zkSNARKs. Transactions can be configured to hide the sender, recipient, and value on the blockchain.</p><h4>Ethereum</h4><p>In the Byzantium hard fork, a zkSNARK functionality was added to Ethereum in the form of precompiled contracts.</p><p>It is still early but there are already several DApps for Ethereum that use this technology. One of them is <a href="https://github.com/stonecoldpat/anonymousvoting">Open Vote Network</a>, a 2-round decentralized voting protocol.</p><h3>Conclusion</h3><p>Zero-knowledge proofs allow for cryptographically secure blockchain transactions. The Ethereum Byzantium hard fork incorporated the ability to use zkSNARKs for zero-knowledge proofs. While zkSNARks is a nice addition to the Ethereum toolbox, it’s too early to see if it will be widely adopted by the Ethereum community.</p><h3>See also</h3><ul><li><a href="https://github.com/poanetwork/mana">Mana Project</a></li><li><a href="https://github.com/poanetwork/bn">BN</a></li><li><a href="https://medium.com/@VitalikButerin/zk-snarks-under-the-hood-b33151a013f6">Zk-SNARKs: Under the Hood</a> by Vitalik Buterin</li><li><a href="https://z.cash/technology/zksnarks.html">What are zk-SNARKs?</a></li></ul><p><em>Originally published at </em><a href="https://www.badykov.com/ethereum/2018/09/15/zkSNARK-in-Ethereum/"><em>www.badykov.com</em></a><em> on September 15, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62ead3258164" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cronenberg]]></title>
            <link>https://medium.com/@ayratbadykov/cronenberg-303d5e615a0a?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/303d5e615a0a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[cron]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Tue, 27 Feb 2018 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T20:19:30.742Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1MrydGhEHfd_xQfv.jpg" /></figure><p>In <a href="http://www.badykov.com/rust/2018/01/28/in-rust-i-trust/">my previous post</a> I wrote about the plan of how I am going to learn the Rust language. One of the the plans’s points was to write a couple of simple libraries.</p><p>So here comes the first library that I wrote in Rust — Cronenberg.</p><h3>Background</h3><p>A couple of weeks ago I finally found some time to dive into emacs’s <a href="https://orgmode.org/">org-mode</a> and I found it very useful. For those who don’t know what org-mode is, it is a mode for keeping notes, maintaining TODO lists, and doing project planning with a fast and effective plain-text system.</p><p>I personally use it for planning my day, keeping track of my work tasks and gym programs. I synchronize org-mode files with my Android phone using open source <a href="http://www.orgzly.com/">Orgzly</a> app (by the way, there are a couple of apps for ios too).</p><p>Org-mode has an amazing deadlines and scheduling feature. I got an idea to write a simple Rust app that will notify me about scheduled events in my org-mode files. <a href="https://github.com/ayrat555/doomsday">The project</a> has a working title of ‘Doomsday’. I use macOS at work and a linux distro at home so I decided to use cron for scheduling system notifications.</p><p>I encountered the first problem when I was writing interaction with crontab files: rust ecosystem has a library for parsing cron command entries but it uses nightly version of Rust and an older version of <a href="https://github.com/Geal/nom">nom</a> (parser combinator framework). I decided to write a simple cron parser myself.</p><h3>Parsing cron command entries</h3><p>cronenberg provides two core components</p><ul><li>TimeItem: An enum that represents cron command time or date field</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25840b1d9a6d09c071a0640d4113e3a5/href">https://medium.com/media/25840b1d9a6d09c071a0640d4113e3a5/href</a></iframe><ul><li>CronItem: A struct that represents cron command entry, for example, * * 5-7 1,2,5 8 sudo rm -rf /</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dfe449d4aa402865a428ce6c9f6d089/href">https://medium.com/media/3dfe449d4aa402865a428ce6c9f6d089/href</a></iframe><h4>Usage example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a4d40a7e82b16ac6aada2ae121902e4/href">https://medium.com/media/5a4d40a7e82b16ac6aada2ae121902e4/href</a></iframe><p>I used <a href="https://github.com/Geal/nom">nom</a> library for parsing. Although I heard good things about this crate, I’m not particularly fond of it. It extensively uses macros and learning this library was like learning a new programming language.</p><p>Here’s example of parsing cron time item:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a4d40a7e82b16ac6aada2ae121902e4/href">https://medium.com/media/5a4d40a7e82b16ac6aada2ae121902e4/href</a></iframe><p>I used <a href="https://github.com/Geal/nom">nom</a> library for parsing. Although I heard good things about this crate, I’m not particularly fond of it. It extensively uses macros and learning this library was like learning a new programming language.</p><p>Here’s example of parsing cron time item:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5597c7849c24e2b008cda548972060d1/href">https://medium.com/media/5597c7849c24e2b008cda548972060d1/href</a></iframe><h3>Roadmap</h3><p>I’m planning to add more features to cronenberg in the future:</p><ul><li>words for days of the week: Sun,...,Mon</li><li>words for months: Jan,..,Dec</li><li>steps: */5</li></ul><p>The library has a GitHub repository — <a href="https://github.com/ayrat555/cronenberg">cronenberg</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*vVWDOSdvd9yLGLKI.jpg" /></figure><p><em>Originally published at </em><a href="https://www.badykov.com/rust/2018/02/27/cronenberg/"><em>www.badykov.com</em></a><em> on February 27, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=303d5e615a0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[In Rust I trust]]></title>
            <link>https://medium.com/@ayratbadykov/in-rust-i-trust-a9a7336479bb?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9a7336479bb</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[rustlang]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sun, 28 Jan 2018 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T20:06:31.860Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G2EP5sVdZ4geTZLy.jpg" /></figure><p>So it’s time to write my first post in 2018. I’ve been busy learning the Rust language. In this post I’ll try to explain why I decided to learn it, how I’m learning it and awesome things this language can be used for.</p><h3>My motivation to learn Rust</h3><p>It first occurred to me to learn Rust in July of 2017. At that time we were trying to implement <a href="https://github.com/exthereum">the Ethereum protocol in Elixir</a>.</p><p>In Ethereum’s <a href="https://github.com/ethereum/wiki/wiki/Patricia-Tree">Merkle Patricia Tree</a> SHA3 hashes are used for node keys. The only implementation of SHA3 I found in Erlang/Elixir ecosystem was <a href="https://github.com/potatosalad/erlang-keccakf1600">erlang-keccakf1600</a> NIF written in C. But I’m not a fan of either Erlang or C. I had heard that there is a new player in system programming language market that can compete with C/C++. I decided to write Elixir NIF in the Rust language because this whole ‘Ethereum in Elixir’ thing was a side project, I didn’t have any deadlines and I wanted it to be perfect from the ground up. Also it was a good opportunity to learn new things. My adventures with Rust began.</p><p>My plans to learn Rust were crushed when I was assigned a new project at my work in Bookmate: I had to write a microservice in Kotlin, the language that I didn’t know at the time. Thankfully Kotlin is an easy to learn language and I had written a couple of microservices in Java before. Kotlin positions itself as a pragmatic and easy-to-use alternative to Java. But unfortunately I had to suspend my playing with Rust to direct all my attention to this project. After finishing the Kotlin project my motivation to finish the Ethereum project has died so I didn’t have reasons to learn Rust (by the way the other guys from the Exthereum github organization are still working on the project).</p><p>My second attempt to learn the language began in December of 2017. I got an idea for a simple game. And do you know what language did I choose to implement it? That’s right, I chose Rust. The journey continues to this date.</p><h3>My plan to learn Rust</h3><blockquote><em>…I want to stress that Rust isn’t the kind of language you can learn in a couple days and just deal with the hard/technical/good-practice stuff later. You will be forced to learn strict safety immediately and it will probably feel uncomfortable at first. However in my own experience, this has led me towards feeling like compiling my code actually means something to me again.</em></blockquote><blockquote><em>Mitchell Nordine</em></blockquote><p>I created a plan to learn Rust as effectively as possible:</p><ol><li>Read O’Reilly’s <a href="http://shop.oreilly.com/product/0636920040385.do">Programming Rust book</a>.</li><li>Finish all of 78 exercises from <a href="http://exercism.io/languages/rust/about">Exercism</a>. You can start doing exercises while reading the book.</li><li>Write a couple of simple libraries.</li></ol><p>I hope after finishing step 3 I can say that I have a basic knowledge of Rust. At the time of writing this post I have finished 47 of 78 exercises (finished exercises are available in the github repository — <a href="https://github.com/ayrat-playground/exercism_rust">exercism_rust</a>).</p><p>I experienced firsthand the steep learning curve of this language. Concepts of ownership, references and lifetime were the hardest to grasp.</p><h3>Usage</h3><p>Rust is a new systems programming language. Like C and C++, Rust gives developers fine control over the use of memory, so it can be used for every task that C/C++ are used:</p><ul><li>Game development. There is a great site called <a href="http://arewegameyet.com/">Are we game yet?</a> that lists all game development resources in Rust ecosystem.</li><li>Operating systems. <a href="https://www.redox-os.org/">Redox</a> is a Unix-like operating system written in Rust <a href="https://os.phil-opp.com/">https://os.phil-opp.com/</a> — This blog series creates a small operating system.</li><li>Elixir Nifs. <a href="https://github.com/hansihe/rustler">Rustler</a> — Safe Rust bridge for creating Erlang NIF functions.</li><li>Ruby extensions. <a href="https://github.com/tildeio/helix">Helix</a></li><li>Etc</li></ul><p><em>Originally published at </em><a href="https://www.badykov.com/rust/2018/01/28/in-rust-i-trust/"><em>www.badykov.com</em></a><em> on January 28, 2018.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9a7336479bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Telegram bot in Elixir]]></title>
            <link>https://medium.com/@ayratbadykov/simple-telegram-bot-in-elixir-de8da5e797bf?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/de8da5e797bf</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[telegram]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sun, 26 Nov 2017 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T20:03:04.539Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xfuTgf9C4xkmmlEA.jpg" /></figure><h3>Background</h3><p>Recently I got an opportunity to write a simple Telegram bot. The purpose of this bot is very simple: it has to send random quotes of <a href="https://en.wikipedia.org/wiki/Victor_Pelevin">Victor Pelevin</a> for any user interaction. I decided to write this bot in Elixir. So in this post I will try describe the bot’s implementation.</p><p>Telegram API provides two mutually exclusive ways of getting bot updates:</p><ul><li><a href="https://core.telegram.org/bots/api#setwebhook">webhooks</a></li><li>polling via <a href="https://core.telegram.org/bots/api#getupdates">getUpdates method</a></li></ul><p>I think, the first way is more scalable because webhook requests can be loadbalanced between unlimited instances of your bot apps. But the second way is much easier to implement, because you do not even need a web server.</p><p>This bot is dedicated to Pelevin’s birthday which is the 22th of November. I started to develop a day before his birthday so I didn’t have much time to be messing around with webhooks so I chose the second variant of getting updates from Telegram.</p><h3>The main design</h3><p>The bot consists of two parts:</p><ul><li>Polling process</li><li>Replying processes</li></ul><p>Every 100 ms polling process gets bot updates and then sends them via replying processes.</p><h3>Polling process</h3><p>I divided polling process into two modules:</p><ul><li>QuotesBot.Polling.Logic - module with polling logic</li><li>QuotesBot.Polling.Server - module with GenServer callbacks</li></ul><p>QuotesBot.Polling.Logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3aaa3937799a5684e4f753353b82a41/href">https://medium.com/media/e3aaa3937799a5684e4f753353b82a41/href</a></iframe><p>To be able to mock telegram_api in tests with the awesome <a href="https://github.com/plataformatec/mox">mox library</a> I’m getting telegram_api from config file. poll/1 method gets updates from Telegram and replies to every update with replying processes (QuotesBot.Bot.Server), it returns id of the last update that was processed.</p><p>Here’s QuotesBot.Polling.Server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/941df7b7a5a809a3217b8322eb8acc47/href">https://medium.com/media/941df7b7a5a809a3217b8322eb8acc47/href</a></iframe><p>Its implementation is straightforward. In init callback it schedules update polling. And in handle_info callback it polls updates and schedules the next update polling.</p><h3>Replying processes</h3><p>I also divided replying process into two modules:</p><ul><li>QuotesBot.Bot.Server - module with GenServer callbacks</li><li>QuotesBot.Bot.Logic - module with replying logic</li></ul><p>QuotesBot.Bot.Server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6303d7957d8729336a0168a4aa373fbe/href">https://medium.com/media/6303d7957d8729336a0168a4aa373fbe/href</a></iframe><p>Here I’m using <a href="https://github.com/devinus/poolboy">poolboy</a> library. reply/1 method sends a telegram update to process from process pool. :poolboy.transaction/1 gets process from process pool.</p><p>poolboy is configured on the bot startup:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/189ac24cb4c84ed3f4fe1ceaff7f3d2b/href">https://medium.com/media/189ac24cb4c84ed3f4fe1ceaff7f3d2b/href</a></iframe><p>I don’t provide the source code of QuotesBot.Bot.Logic, because it is the domain logic of getting quotes that is not necessary to understand the bot implementation.</p><h3>See also</h3><ul><li>the bot is available at <a href="http://telegram.me/pelevin_quotes_bot">http://telegram.me/pelevin_quotes_bot</a>. Notice: the bot is written for russian speaking users so it replies only with russian quotes.</li><li><a href="https://core.telegram.org/">https://core.telegram.org/</a></li><li><a href="https://elixirschool.com/en/lessons/libraries/poolboy/">https://elixirschool.com/en/lessons/libraries/poolboy/</a></li></ul><p><em>Originally published at </em><a href="https://www.badykov.com/elixir/2017/11/26/simple-telegram-bot/"><em>www.badykov.com</em></a><em> on November 26, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de8da5e797bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Y combinator]]></title>
            <link>https://medium.com/@ayratbadykov/y-combinator-cc4205789170?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc4205789170</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[lisp]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sun, 12 Nov 2017 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T06:06:55.004Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*-2du4POtWHAl2Zn8_gS4kw.png" /></figure><p>In the <a href="http://www.badykov.com/general/2017/11/04/lisp-interpreter-in-lisp/">previous post</a> I wrote about Metacircular Evaluator — list interpeter that is written in lisp. Now I will give a short description of the Curry’s Paradoxical Combinator. It will help to comprehend the idea that a thing can be described in terms of itself.</p><p><a href="https://en.wikipedia.org/wiki/Haskell_Curry">Haskell Brooks Curry</a> was an American mathematician. He was so cool that there are three programming languages named after him, Haskell, Brook and Curry. By the way, I can’t find time to learn Haskell. I heard that when you know Haskell, your life is complete and you can die peacefully.</p><p>Another awesome thing that he created was Curry’s Paradoxical Combinator, aka Y combinator, aka WTF combinator (by the people who try to understand it).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b951692db08e1a40d55ecf52d60b0887/href">https://medium.com/media/b951692db08e1a40d55ecf52d60b0887/href</a></iframe><p>Y is higher order function which, when applied to some function, produces the object which is the fixed point of that function.</p><p>A fixed point of a function is an element of the function’s domain that is mapped to itself by the function.</p><blockquote><em>So Lisp is a fixed point of the of the Metacircular Evaluator.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*WWkQ-0fWuo4IGxOz.gif" /></figure><p><em>Originally published at </em><a href="https://www.badykov.com/general/2017/11/12/ycombinator/"><em>www.badykov.com</em></a><em> on November 12, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc4205789170" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lisp interpreter in Lisp]]></title>
            <link>https://medium.com/@ayratbadykov/lisp-interpreter-in-lisp-bcec492ee34e?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcec492ee34e</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[lisp]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sat, 04 Nov 2017 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T06:00:23.343Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/476/0*vkA2xwXHCOxk01f-.jpg" /></figure><h3>Introduction</h3><p>Since the end of August, every saturday my colleagues at Bookmate and I are watching “Structure and Interpretation of Computer Programs” <a href="https://www.youtube.com/playlist?list=PL4kmJpCNr9UJtH-YZkSqkUhgRku55_Pf6">course lectures</a>. Here’s excerpt from the course culmination — The Metacircular Evaluator.</p><h3>Eval</h3><p>Eval takes as arguments an expression and an environment. It classifies the expression and directs its evaluation. Eval is structured as a case analysis of the syntactic type of the expression to be evaluated. In order to keep the procedure general, we express the determination of the type of an expression abstractly, making no commitment to any particular representation for the various types of expressions. Each type of expression has a predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures.</p><p>Primitive expressions</p><ul><li>For self-evaluating expressions, such as numbers, eval returns the expression itself.</li><li>Eval must look up variables in the environment to find their values.</li></ul><p>Special forms</p><ul><li>For quoted expressions, eval returns the expression that was quoted.</li><li>An assignment to (or a definition of) a variable must recursively call eval to compute the new value to be associated with the variable. The environment must be modified to change (or create) the binding of the variable.</li><li>A lambda expression must be transformed into an applicable procedure by packaging together the parameters and body specified by the lambda expression with the environment of the evaluation.</li><li>A case analysis (cond) is transformed into a nest of if expressions and then evaluated.</li></ul><p>Combinations</p><ul><li>For a procedure application, eval must recursively evaluate the operator part and the operands of the combination. The resulting procedure and arguments are passed to apply, which handles the actual procedure application.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e86e634671f608f4b589462d7f435e3/href">https://medium.com/media/3e86e634671f608f4b589462d7f435e3/href</a></iframe><h3>Apply</h3><p>Apply takes two arguments, a procedure and a list of arguments to which the procedure should be applied. Apply classifies procedures into two kinds:</p><ul><li>it calls apply-primop to apply primitives</li><li>it applies compound procedures by sequentially evaluating the expressions that make up the body of the procedure. The environment for the evaluation of the body of a compound procedure is constructed by extending the base environment carried by the procedure to include a frame that binds the parameters of the procedure to the arguments to which the procedure is to be applied</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbfa50fe22a9b008a1ad3e9552ad3de3/href">https://medium.com/media/bbfa50fe22a9b008a1ad3e9552ad3de3/href</a></iframe><h3>Misc</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4fb94db6f52c5940f7b42ac75bc3963/href">https://medium.com/media/e4fb94db6f52c5940f7b42ac75bc3963/href</a></iframe><h3>Wizard</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/0*hmGkPgOGTlOL1DQS.jpg" /></figure><p><em>Originally published at </em><a href="https://www.badykov.com/general/2017/11/04/lisp-interpreter-in-lisp/"><em>www.badykov.com</em></a><em> on November 4, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcec492ee34e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bloom filters in Ethereum]]></title>
            <link>https://medium.com/@ayratbadykov/bloom-filters-in-ethereum-4b884c464b0b?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b884c464b0b</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Sun, 29 Oct 2017 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-22T05:53:53.067Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/736/0*nUm0TQeZLM9Vk3pw.jpg" /></figure><h3>Background</h3><p>Bloom filters are compact data structures for probabilistic representation of a set in order to support membership queries (i.e. queries that ask: “Is element X in set Y?”).</p><p>An empty Bloom filter is a bit array of m bits, all set to 0. There must also be k different hash functions defined, each of which maps or hashes some set element to one of the m array positions, generating a uniform random distribution. Typically, k is a constant, much smaller than m, which is proportional to the number of elements to be added; the precise choice of k and the constant of proportionality of m are determined by the intended false positive rate of the filter.</p><p>To add an element, feed it to each of the k hash functions to get k array positions. Set the bits at all these positions to 1.</p><p>To query for an element (test whether it is in the set), feed it to each of the k hash functions to get k array positions. If any of the bits at these positions is 0, the element is definitely not in the set — if it were, then all the bits would have been set to 1 when it was inserted. If all are 1, then either the element is in the set, or the bits have by chance been set to 1 during the insertion of other elements, resulting in a false positive. In a simple Bloom filter, there is no way to distinguish between the two cases, but more advanced techniques can address this problem</p><p>Pros:</p><ul><li>space efficient</li></ul><p>Cons:</p><ul><li>can’t store an associated object</li><li>deletions are not allowed</li><li>small false positive probability</li></ul><p>Applications:</p><ul><li>weak password dictionary</li><li>cache sharing</li><li>query filtering and routing</li><li>blockchain (logs)</li></ul><h3>Ethereum bloom filter function</h3><p>Here’s excerpt from the <a href="https://ethereum.github.io/yellowpaper/paper.pdf">yellow paper</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2y3IKNd-fCHRLTkm.jpg" /></figure><p>If you didn’t understand anything, no worries, I will try to describe its implementation in the next section.</p><h3>Elixir implementation</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b72a4453e4b04eb7fa8030c839503504/href">https://medium.com/media/b72a4453e4b04eb7fa8030c839503504/href</a></iframe><p>The bloom/2 method accepts 2 aguments:</p><ul><li>data — binary. a new object to add to the bloom filter</li><li>number — integer. the bloom filter. default value is zero (for new filter)</li></ul><p>Bloom filter generation can be divided into three parts:</p><ul><li>calculate sha3 hash of an input data (1)</li><li>get three bit indices from this hash (2)</li><li>set these indices to one in the filter (3)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5ddcd0e11b883651e00421827a53fff/href">https://medium.com/media/d5ddcd0e11b883651e00421827a53fff/href</a></iframe><p>The bit_numbers/1 method gets indices from the low order 11-bits of the first three double-bytes of the SHA3 hash.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee86694f8968a269f33f6fd5e1e311d7/href">https://medium.com/media/ee86694f8968a269f33f6fd5e1e311d7/href</a></iframe><p>The last thing left to do is to define method to check if filter has an object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f05afcec2777826da5ff8ddd3c1e868/href">https://medium.com/media/1f05afcec2777826da5ff8ddd3c1e868/href</a></iframe><p>Now we can use our Bloom filter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39df24a6086ee6ff6eaf8b5cc8ae2144/href">https://medium.com/media/39df24a6086ee6ff6eaf8b5cc8ae2144/href</a></iframe><p>Described code has a GitHub repository — <a href="https://github.com/ayrat555/eth_bloom">https://github.com/ayrat555/eth_bloom</a></p><h3>See also</h3><ul><li><a href="https://ethereum.github.io/yellowpaper/paper.pdf">https://ethereum.github.io/yellowpaper/paper.pdf</a></li><li><a href="https://en.m.wikipedia.org/wiki/Bloom_filter">https://en.m.wikipedia.org/wiki/Bloom_filter</a></li><li><a href="https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/riKfa/bloom-filters-the-basics">https://www.coursera.org/learn/algorithms-graphs-data-structures/lecture/riKfa/bloom-filters-the-basics</a></li></ul><p><em>Originally published at </em><a href="https://www.badykov.com/ethereum/2017/10/29/ethereum-bloom-filter/"><em>www.badykov.com</em></a><em> on October 29, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b884c464b0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elixir macros are evil (sometimes)]]></title>
            <link>https://medium.com/@ayratbadykov/elixir-macros-are-evil-sometimes-6cfeeb326bfe?source=rss-ea3452eb4a27------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cfeeb326bfe</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[Ayrat Badykov]]></dc:creator>
            <pubDate>Wed, 18 Oct 2017 00:00:00 GMT</pubDate>
            <atom:updated>2018-09-17T09:05:59.240Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*psqGcr0dwpWVkQRM.jpg" /></figure><h3>Introduction</h3><p>First, a bit of background. A couple of months ago I got interested in Ethereum. I decided to write a simple Elixir wrapper for its <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC">JSON RPC api </a>methods. JSON RPC is light-weight remote procedure call protocol.</p><p>JSON RPC message must consist of three properties:</p><ul><li>method — the name of the method</li><li>params — object or array of values (In Ethereum’s case, an array)</li><li>id — the id of the request</li></ul><p>Let’s consider Ethereum’s web3_sha3 method that calculates a sha3 hash of the input data. It has one parameter — the string to be converted into a sha3 hash. To call this method wrap the parameter info in an array and call it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b6ee7869bcbbf420158376c8cbf9f9a/href">https://medium.com/media/0b6ee7869bcbbf420158376c8cbf9f9a/href</a></iframe><p>Ethereum JSON RPC consists of more than 50 methods. Note that parameters can have types other than string (binary). For example, eth_getBlockByHash accepts boolean value as one of its input parameters.</p><h3>The first approach</h3><p>“That’s easy.”, I thought at the time. “If method requests differ only in their names and input parameters I should write macro that would generate all of these methods for me”</p><p>And I wrote this macro. Here’s a simplified version:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed70ee73df4d132de90751b7666e6877/href">https://medium.com/media/ed70ee73df4d132de90751b7666e6877/href</a></iframe><p>I was satisfied with this solution because it was concise, without any boilerplate code. I felt like Hercules when he killed the lernean hydra cutting its heads and searing the headless tendons of the neck but instead of the power of fire I had the power of elixir macros.</p><p>But unfortunately, the solution had a couple of problems.</p><h3>Problems with the first approach</h3><p>Soon an issue with the request for implementation of smart contract compilation and interaction was opened in the repository. The requested feature is more high level compared to the original API methods and it used different combinations of them.</p><p>I decided to test this feature with the mox library created by our lord and savior Jose Valim. As described in its readme, the mox library follows the principles outlined in <a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/">“Mocks and explicit contracts”</a>, summarized below:</p><ul><li>No ad-hoc mocks. You can only create mocks based on behaviours</li><li>No dynamic generation of modules during tests. Mocks are preferably defined in your test_helper.exs or in a setup_all block and not per test</li><li>Concurrency support. Tests using the same mock can still use async: true</li><li>Rely on pattern matching and function clauses for asserting on the input instead of complex expectation rules</li></ul><p>As you can see I couldn’t mock with mox because all methods are dynamically generated and don’t abide any behaviour. So I created an issue in the mox library asking if I should define behaviour with all api methods or if there is some other way.</p><p>One minute later I got an answer from Jose:</p><blockquote><em>One of the guidelines in Elixir is that every time you you generate a function in the user code it should be to abide to some behaviour, so yes, you need behaviours.</em></blockquote><blockquote><em>Jose Valim</em></blockquote><p>Another problem with the current implementation was the method signatures. It’s not apparent what parameters every method accepts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3fb188072f737838e7e604262c8b392/href">https://medium.com/media/e3fb188072f737838e7e604262c8b392/href</a></iframe><p>To summarize, there were two problems:</p><ul><li>Methods were hard to mock</li><li>Ugly method signature</li></ul><p>So I began refactoring the library to get rid of this problems.</p><h3>The second approach</h3><p>In the second approach I defined behaviour with all JSON API methods</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48da65b9427d36a2223e768a506d0c86/href">https://medium.com/media/48da65b9427d36a2223e768a506d0c86/href</a></iframe><p>And then I implemented this behavior in the macro:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73aaab7142ae1b5c3bb20ddf35c44989/href">https://medium.com/media/73aaab7142ae1b5c3bb20ddf35c44989/href</a></iframe><p>So now I can easily mock any method and all method signatures have explicit parameter types.</p><p>I implemented the behaviour in the macro because Ethereum JSON RPC has two types of clients:</p><ul><li>ipc clients using unix sockets</li><li>http clients</li></ul><p>So to define this client I only needed to include the macro in client modules and override the request/2 method like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8031830beeb4b0da21b3c866742173f7/href">https://medium.com/media/8031830beeb4b0da21b3c866742173f7/href</a></iframe><h3>Conclusion</h3><p>So Elixir macros as all powerful tools should be used responsibly.</p><p>If you have any remarks or thoughts on the matter please leave comments.</p><p>The library has a GitHub repository — <a href="https://github.com/exthereum/ethereumex">https://github.com/exthereum/ethereumex</a></p><p><em>Originally published at </em><a href="https://www.badykov.com/elixir/2017/10/18/macros-are-evil/"><em>www.badykov.com</em></a><em> on October 18, 2017.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cfeeb326bfe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>