<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Boris Nagaev</title>
 <link href="http://starius.ru/atom.xml" rel="self"/>
 <link href="http://starius.ru/"/>
 <updated>2018-06-18T22:45:54+00:00</updated>
 <id>http://starius.ru</id>
 <author>
   <name>Boris Nagaev</name>
   <email>bnagaev@gmail.com</email>
 </author>

 
 <entry>
   <title>NPG-explorer 0.5.1 and 0.5.2 released</title>
   <link href="http://starius.ru/2015/11/08/npge-0.5.1-and-0.5.2-released/"/>
   <updated>2015-11-08T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/11/08/npge-0.5.1-and-0.5.2-released</id>
   <content type="html">&lt;p&gt;NPG-explorer is a new tool for nucleotide pangenome
construction and analysis of closely related prokaryotic
genomes.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/npge/npge/releases/tag/0.5.1&quot;&gt;Version 0.5.1&lt;/a&gt; was released on Oct 30, 2015.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/npge/npge/releases/tag/0.5.2&quot;&gt;Version 0.5.2&lt;/a&gt; was released on Nov 08, 2015.&lt;/p&gt;

&lt;p&gt;Version 0.5.1 has a focus on bug fixes and a few new developments.
Version 0.5.2 fixes bugs of the Windows version.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Changes (0.5.1):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Pangenome: join collinear blocks even if bad.&lt;/li&gt;
  &lt;li&gt;Fix intermediate fragments joining. This bug was
introduced in 2c76b512a3eaffc613c
(2015-05-16), version 0.4.0 (2015-06-12).&lt;/li&gt;
  &lt;li&gt;Update requirements of a good pangenome in README.md
according to what is implemented now.&lt;/li&gt;
  &lt;li&gt;Genes-vs-blocks partition: print only &lt;code class=&quot;highlighter-rouge&quot;&gt;locus_tag&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;GUI: jump to fragment end if there is no gene end.&lt;/li&gt;
  &lt;li&gt;Info: print % of NPG for total blocks length.&lt;/li&gt;
  &lt;li&gt;Recognize lua-npge format of Fragment id encoding.&lt;/li&gt;
  &lt;li&gt;Download compressed files from the database.&lt;/li&gt;
  &lt;li&gt;Fix errors in GetData and AddGenes.&lt;/li&gt;
  &lt;li&gt;AddGenes: skip repeated &lt;code class=&quot;highlighter-rouge&quot;&gt;locus_tag&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Fix some build issues on various setups.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;meta_test&lt;/code&gt;: ignore directories without script.npge.&lt;/li&gt;
  &lt;li&gt;Setup Travis for cross-compiling to Windows (MXE).&lt;/li&gt;
  &lt;li&gt;Setup Travis for uploading build artifacts to GitHub.&lt;/li&gt;
  &lt;li&gt;Update BLAST in the distribution to 2.2.31.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changes (0.5.2):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Fix Windows build&lt;/li&gt;
  &lt;li&gt;Fix typos and errors in README and Info’s output&lt;/li&gt;
  &lt;li&gt;config: do not fail with lua-npge npge.conf&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://mouse.belozersky.msu.ru/tools/npge.html&quot;&gt;homepage&lt;/a&gt;,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge&quot;&gt;source&lt;/a&gt; is hosted on GitHub,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/releases&quot;&gt;downloads&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/issues/new&quot;&gt;report a bug&lt;/a&gt;
 (GitHub sign-in is needed).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This work was presented at ECCB’14 conference:
&lt;a href=&quot;https://github.com/npge/npge/blob/master/doc/eccb14.md&quot;&gt;abstract&lt;/a&gt; (&lt;a href=&quot;https://github.com/npge/npge/blob/master/doc/eccb14_ru.md&quot;&gt;ru&lt;/a&gt;) and
&lt;a href=&quot;http://f1000.com/posters/browse/summary/1096831&quot;&gt;poster&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Speeding up byte substitution</title>
   <link href="http://starius.ru/2015/09/05/speed-up-substitution/"/>
   <updated>2015-09-05T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/09/05/speed-up-substitution</id>
   <content type="html">&lt;p&gt;Let us speed up the following program:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;substituion&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;input string&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This code solves many practical problems:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Substitution_cipher&quot;&gt;substitution cipher&lt;/a&gt;,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Complementary_DNA&quot;&gt;complementary DNA calculation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To speed up this code approximately twofold, replace
&lt;code class=&quot;highlighter-rouge&quot;&gt;char translate[256]&lt;/code&gt; table with
 &lt;code class=&quot;highlighter-rouge&quot;&gt;short short_translate[256*256]&lt;/code&gt;.
This table has size &lt;code class=&quot;highlighter-rouge&quot;&gt;256 * 256 * 2 = 131072&lt;/code&gt; bytes and
is generated once per a substitution table.&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in_xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out_xy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;short_translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_xy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out_xy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And use &lt;code class=&quot;highlighter-rouge&quot;&gt;short&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;char&lt;/code&gt; in the code:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;len&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;short_output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;short_translate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;short_input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s all.&lt;/p&gt;

&lt;p&gt;It works two times faster because it needs two times less
table lookups.&lt;/p&gt;

&lt;p&gt;Note: we can’t use &lt;code class=&quot;highlighter-rouge&quot;&gt;int&lt;/code&gt; instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;short&lt;/code&gt; because
a substitution table for &lt;code class=&quot;highlighter-rouge&quot;&gt;int&lt;/code&gt; would occupy
&lt;code class=&quot;highlighter-rouge&quot;&gt;256 ^ 4 * 4 = 16G&lt;/code&gt;. A table of that size is too large to
fit in CPU cache.&lt;/p&gt;

&lt;p&gt;See &lt;a href=&quot;https://github.com/starius/dna-complement&quot;&gt;the code and the benchmark&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>NPG-explorer 0.5.0 released</title>
   <link href="http://starius.ru/2015/07/16/npge-0.5.0-released/"/>
   <updated>2015-07-16T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/07/16/npge-0.5.0-released</id>
   <content type="html">&lt;p&gt;NPG-explorer is a new tool for nucleotide pangenome
construction and analysis of closely related prokaryotic
genomes.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/npge/npge/releases/tag/0.5.0&quot;&gt;Version 0.5.0&lt;/a&gt; was released on Jul 16, 2015.&lt;/p&gt;

&lt;p&gt;This release has a focus on bug fixes and a few
new developments.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Changes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Change alignment requirements.
    &lt;ul&gt;
      &lt;li&gt;Change meaning and default value of &lt;code class=&quot;highlighter-rouge&quot;&gt;MIN_END&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Add option &lt;code class=&quot;highlighter-rouge&quot;&gt;FRAME_LENGTH&lt;/code&gt;.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Add processor CountSMS&lt;/li&gt;
  &lt;li&gt;Filter: increase size of gap score table to 1000&lt;/li&gt;
  &lt;li&gt;Tools.
    &lt;ul&gt;
      &lt;li&gt;BLAST version: 2.2.29 -&amp;gt; 2.2.30&lt;/li&gt;
      &lt;li&gt;Option npge -v [–version]&lt;/li&gt;
      &lt;li&gt;Clarify option -g&lt;/li&gt;
      &lt;li&gt;Hide option &lt;code class=&quot;highlighter-rouge&quot;&gt;DEV_NULL&lt;/code&gt; from npge.conf&lt;/li&gt;
      &lt;li&gt;GUI: show % of length occupied by low similarity&lt;/li&gt;
      &lt;li&gt;Examine: do not recommend nan identity&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Documentation.
    &lt;ul&gt;
      &lt;li&gt;ECCB’14: add abstract in English (original)&lt;/li&gt;
      &lt;li&gt;README: meaning of columns of *.bi files&lt;/li&gt;
      &lt;li&gt;README: add blocks types algorithm (image)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Fix bugs.
    &lt;ul&gt;
      &lt;li&gt;FreeBSD build fixes. Thanks to Dmitry Marakasov!&lt;/li&gt;
      &lt;li&gt;Fix segfault in PrintPartition.&lt;/li&gt;
      &lt;li&gt;Fix the bug in BlastFinder (crash on empty input).&lt;/li&gt;
      &lt;li&gt;Fix windows x64 build.&lt;/li&gt;
      &lt;li&gt;Do not apply UPX to 64-bit Windows executables.&lt;/li&gt;
      &lt;li&gt;Lua code: compatibility with Lua &amp;gt;= 5.2.&lt;/li&gt;
      &lt;li&gt;Find more locus_tag’s in GeneBank files.&lt;/li&gt;
      &lt;li&gt;FindLowSimilar: fix division by zero.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://mouse.belozersky.msu.ru/tools/npge.html&quot;&gt;homepage&lt;/a&gt;,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge&quot;&gt;source&lt;/a&gt; is hosted on GitHub,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/releases&quot;&gt;downloads&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/issues/new&quot;&gt;report a bug&lt;/a&gt;
 (GitHub sign-in is needed).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This work was presented at ECCB’14 conference:
&lt;a href=&quot;https://github.com/npge/npge/blob/master/doc/eccb14.md&quot;&gt;abstract&lt;/a&gt; (&lt;a href=&quot;https://github.com/npge/npge/blob/master/doc/eccb14_ru.md&quot;&gt;ru&lt;/a&gt;) and
&lt;a href=&quot;http://f1000.com/posters/browse/summary/1096831&quot;&gt;poster&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>NPG-explorer 0.4.0 released</title>
   <link href="http://starius.ru/2015/06/12/npge-0.4.0-released/"/>
   <updated>2015-06-12T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/06/12/npge-0.4.0-released</id>
   <content type="html">&lt;p&gt;NPG-explorer is a new tool for nucleotide pangenome
construction and analysis of closely related prokaryotic
genomes.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/npge/npge/releases/tag/0.4.0&quot;&gt;Version 0.4.0&lt;/a&gt; was released on Jun 12, 2015.&lt;/p&gt;

&lt;p&gt;This release has a focus on new developments and a few
bug fixes.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Changes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Change alignment requirements. Require high similarity in
  every slice of length &lt;code class=&quot;highlighter-rouge&quot;&gt;MIN_LENGTH&lt;/code&gt;. Use logarithmic
  gap penalty. Introduce option &lt;code class=&quot;highlighter-rouge&quot;&gt;MIN_END&lt;/code&gt;. Do not require
  length of fragment &amp;gt;= &lt;code class=&quot;highlighter-rouge&quot;&gt;MIN_LENGTH&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Short blocks of one fragment are now called minor blocks.
  All minor blocks are shorter than &lt;code class=&quot;highlighter-rouge&quot;&gt;MIN_LENGTH&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;GUI/BSA: show block’s length for global blocks.&lt;/li&gt;
  &lt;li&gt;Use Travis CI: https://travis-ci.org/npge/npge&lt;/li&gt;
  &lt;li&gt;BlockSet alignment: g-blocks and i-blocks. G-blocks
  are formed from joined consequent s-blocks. S-blocks
  are always aligned in g-blocks. Other blocks form
  i-blocks.&lt;/li&gt;
  &lt;li&gt;Speed up SplitRepeats. Improve time complexity by
  building the tree from diagnostic positions. Critical
  for blocks of &amp;gt; 500 fragments.&lt;/li&gt;
  &lt;li&gt;PostProcessing produces new files:
    &lt;ul&gt;
      &lt;li&gt;table of global blocks’ properties (&lt;code class=&quot;highlighter-rouge&quot;&gt;*.gbi&lt;/code&gt;)&lt;/li&gt;
      &lt;li&gt;blockset alignments with block names (&lt;code class=&quot;highlighter-rouge&quot;&gt;*.blocks&lt;/code&gt;)&lt;/li&gt;
      &lt;li&gt;pangenome-small.bi&lt;/li&gt;
      &lt;li&gt;nj-global-tree-full.tre&lt;/li&gt;
      &lt;li&gt;genes/partition.tsv&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Mutations file parser written in Python (example)&lt;/li&gt;
  &lt;li&gt;Print local time with timezone offset to logs&lt;/li&gt;
  &lt;li&gt;Add descriptions of some algorithms (&lt;code class=&quot;highlighter-rouge&quot;&gt;*.md files&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Translate ECCB’14 abstract into Russian&lt;/li&gt;
  &lt;li&gt;Fix bugs:
    &lt;ul&gt;
      &lt;li&gt;Fix bugs in GUI&lt;/li&gt;
      &lt;li&gt;Fix bugs in annotations parser (GetData)&lt;/li&gt;
      &lt;li&gt;Fix bugs in local blockset alignment builder. It
  used to consider linear sequences to be circular.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://mouse.belozersky.msu.ru/tools/npge.html&quot;&gt;homepage&lt;/a&gt;,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge&quot;&gt;source&lt;/a&gt; is hosted on GitHub,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/releases&quot;&gt;downloads&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/npge/npge/issues/new&quot;&gt;report a bug&lt;/a&gt;
 (GitHub sign-in is needed).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This work was presented at ECCB’14 conference:
&lt;a href=&quot;http://www.eccb14.org/95&quot;&gt;abstract&lt;/a&gt; (&lt;a href=&quot;https://github.com/npge/npge/blob/master/doc/eccb14_ru.md&quot;&gt;ru&lt;/a&gt;) and
&lt;a href=&quot;http://f1000.com/posters/browse/summary/1096831&quot;&gt;poster&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Brainfuck to C translator written in Lua</title>
   <link href="http://starius.ru/2015/05/16/brainfuck-to-c-in-lua/"/>
   <updated>2015-05-16T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/05/16/brainfuck-to-c-in-lua</id>
   <content type="html">&lt;p&gt;Lua code translating &lt;a href=&quot;https://en.wikipedia.org/wiki/Brainfuck&quot;&gt;Brainfuck&lt;/a&gt; to C:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brainfuckToC_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'++ptr;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;lt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--ptr;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'+'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'++*ptr;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--*ptr;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'.'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'putchar(*ptr);'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;','&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*ptr=getchar();'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'['&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'while (*ptr) {'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;']'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;brainfuckToC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bf_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_code&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bf_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gsub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'.'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brainfuckToC_map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_code&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PROLOGUE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;[[
#include &amp;lt;stdio.h&amp;gt;
int main() {
    #define TAPE_SIZE 30000
    char array[TAPE_SIZE] = {0};
    char *ptr=array;
]]&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EPILOGUE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;[[
    return 0;
}
]]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROLOGUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;io.lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brainfuckToC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EPILOGUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This script reads Brainfuck code from stdin and writes
corresponding C code to stdout.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us compare the performance of code generated by the script
with Brainfuck interpreter &lt;a href=&quot;http://kiyuko.org/software/beef&quot;&gt;beef&lt;/a&gt; and Brainfsck JIT
compiler from &lt;a href=&quot;https://corsix.github.io/dynasm-doc/tutorial.html&quot;&gt;DynASM tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Download &lt;a href=&quot;https://raw.githubusercontent.com/corsix/dynasm-doc/gh-pages/mandelbrot.bf&quot;&gt;a mandelbrot set fractal viewer in
Brainfuck&lt;/a&gt; written by Erik Bosman.&lt;/p&gt;

&lt;h2 id=&quot;brainfuck-to-c-translator&quot;&gt;Brainfuck to C translator&lt;/h2&gt;

&lt;p&gt;Translate Brainfuck code to C using the translator:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;lua brainfuck-to-c.lua &amp;lt; mandelbrot.bf &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; mandelbrot.c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Compile generated C code:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc mandelbrot.c &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; mandelbrot.exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run the program:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; ./mandelbrot.exe
&amp;lt;mandelbrot &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;fractal here&amp;gt;
real    0m23.183s
user    0m23.141s
sys     0m0.004s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add option &lt;code class=&quot;highlighter-rouge&quot;&gt;-O3&lt;/code&gt; to enable more optimizations:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time &lt;/span&gt;gcc &lt;span class=&quot;nt&quot;&gt;-O3&lt;/span&gt; mandelbrot.c &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; mandelbrot.exe

real    0m4.796s
user    0m4.728s
sys     0m0.048s

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; ./mandelbrot.exe
&amp;lt;mandelbrot &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;fractal here&amp;gt;
real    0m1.599s
user    0m1.596s
sys     0m0.000s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;brainfuck-interpreter-beef&quot;&gt;Brainfuck interpreter beef&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time &lt;/span&gt;beef mandelbrot.bf
&amp;lt;mandelbrot &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;fractal here&amp;gt;
real    0m28.981s
user    0m28.886s
sys     0m0.044s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;brainfuck-jit-compiler&quot;&gt;Brainfuck JIT compiler&lt;/h2&gt;

&lt;p&gt;The Brainfuck JIT compiler can be found in the &lt;a href=&quot;https://corsix.github.io/dynasm-doc/tutorial.html&quot;&gt;DynASM
tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Build the Brainfuck JIT compiler:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/corsix/dynasm-doc.git
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;dynasm-doc
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git submodule update &lt;span class=&quot;nt&quot;&gt;--init&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; minilua luajit-2.0/src/host/minilua.c
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./minilua luajit-2.0/dynasm/dynasm.lua &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bf_dynasm_c.c &lt;span class=&quot;nt&quot;&gt;-D&lt;/span&gt; X64 bf_dynasm.c
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc bf_dynasm_c.c &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; bf_dynasm_c.exe
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run the program:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;time&lt;/span&gt; ./bf_dynasm_c.exe mandelbrot.bf
&amp;lt;mandelbrot &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;fractal here&amp;gt;
real    0m2.750s
user    0m2.740s
sys     0m0.004s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;

&lt;p&gt;The JIT compiler’s performance of Brainfuck code execution
is comparable to the performance of C code generated from
Brainfuck code with &lt;code class=&quot;highlighter-rouge&quot;&gt;-O3&lt;/code&gt; optimizations enabled. The time of
execution of &lt;code class=&quot;highlighter-rouge&quot;&gt;gcc -O3&lt;/code&gt; (4.7 seconds) added to the time of
execution of compiled C code (1.6 seconds) exceeds the time of
execution of JIT compiler (2.7 seconds).&lt;/p&gt;

&lt;p&gt;Obvious advantage of JIT compiler is that it doesn’t require C
compiler to execute Brainfuck code. Same JIT compiler binary
can execute any Brainfuck code while C compiler is needed each
time to compile a C code translated from a Brainfuck code using
the translator.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://luajit.org/dynasm.html&quot;&gt;DynASM project&lt;/a&gt; is great!&lt;/p&gt;

&lt;p&gt;Time of execution was measured on
Intel(R) Core(TM) i5-3317U CPU @ 1.70GHz.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Phpass password hashing for Lua</title>
   <link href="http://starius.ru/2015/05/13/lua-phpass/"/>
   <updated>2015-05-13T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/05/13/lua-phpass</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://www.openwall.com/phpass/&quot;&gt;phpass&lt;/a&gt; (pronounced “pH pass”) is a portable public
domain password hashing framework for use in PHP applications.
phpass has been integrated into &lt;a href=&quot;http://ryan.boren.me/2007/12/17/secure-cookies-and-passwords/&quot;&gt;WordPress 2.5+&lt;/a&gt;,
&lt;a href=&quot;https://bbpress.org/&quot;&gt;bbPress&lt;/a&gt;, &lt;a href=&quot;http://vanillaforums.org/&quot;&gt;Vanilla&lt;/a&gt;, &lt;a href=&quot;http://forum.pivotx.net/viewtopic.php?p=7836#p7836&quot;&gt;PivotX 2.1.0+&lt;/a&gt;,
&lt;a href=&quot;http://chyrp.net/&quot;&gt;Chyrp&lt;/a&gt;, &lt;a href=&quot;http://textpattern.com/&quot;&gt;Textpattern 4.4.0+&lt;/a&gt;, and
&lt;a href=&quot;http://www.concrete5.org/developers/bugs/5-6-2-1/more-secure-password-hashing/&quot;&gt;concrete5 5.6.3+&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lua module &lt;a href=&quot;https://github.com/starius/lua-phpass&quot;&gt;lua-phpass&lt;/a&gt; implements a subset of
phpass (iterated MD5). It’s sufficient to create and check a
password hash compatible with portable phpass hash, e.g. a
password from wordpress database. Blowfish-based bcrypt and
BSDI-style extended DES-based hashes are not supported.&lt;/p&gt;

&lt;p&gt;This module is distributed under terms of the &lt;a href=&quot;https://github.com/starius/lua-phpass/blob/master/LICENSE&quot;&gt;MIT
License&lt;/a&gt;.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks install phpass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;dependencies&quot;&gt;Dependencies&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mkottman/luacrypto&quot;&gt;LuaCrypto&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code was tested against Lua 5.1, 5.2, 5.3 and LuaJIT 2.0,
2.1.&lt;/p&gt;

&lt;p&gt;LuaCrypto for Lua 5.3 requires &lt;a href=&quot;http://lua.2524044.n2.nabble.com/ANN-phpass-password-hashing-for-Lua-tp7667347p7667348.html&quot;&gt;the following
patch&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-diff highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;diff --git a/src/lcrypto.c b/src/lcrypto.c
index 48364d1..e5a62c4 100644
&lt;/span&gt;&lt;span class=&quot;gd&quot;&gt;--- a/src/lcrypto.c
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+++ b/src/lcrypto.c
&lt;/span&gt;&lt;span class=&quot;gu&quot;&gt;@@ -968,7 +968,7 @@ static int verify_fverify(lua_State *L)
&lt;/span&gt; 
 static int rand_do_bytes(lua_State *L, int (*bytes)(unsigned char *, int))
 {
&lt;span class=&quot;gd&quot;&gt;-    size_t count = (size_t)luaL_checkint(L, 1);
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+    size_t count = (size_t)luaL_checkinteger(L, 1);
&lt;/span&gt;     unsigned char tmp[256], *buf = tmp;
     if (count &amp;gt; sizeof tmp)
         buf = (unsigned char *)malloc(count);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I have applied this patch to &lt;a href=&quot;https://github.com/starius/luacrypto&quot;&gt;my fork of LuaCrypto&lt;/a&gt;.
There is also &lt;a href=&quot;https://gist.githubusercontent.com/starius/b20d3e63929ae678c857/raw/4b4499f442337b6f577422364358590bd00c9d48/luacrypto-0.3.2-2.rockspec&quot;&gt;the modified version&lt;/a&gt; of rockspec
for version 0.3.2, which installs modified LuaCrypto.&lt;/p&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;phpass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'phpass'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'test12345'&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phpass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashPassword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;--&amp;gt; &quot;$P$EYyDnrNHtS2MG5vTVkvXD6wMnd0C/N/&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;phpass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkPassword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;--&amp;gt; true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;phpass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;checkPassword&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'other password'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;--&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/exavolt/python-phpass&quot;&gt;Python-phpass&lt;/a&gt;, python implementation of phpass
was used as a reference.&lt;/p&gt;

&lt;p&gt;The algorithm used in &lt;code class=&quot;highlighter-rouge&quot;&gt;phpass.hashPassword&lt;/code&gt; generates random
salt, so this function returns different hashes for a password.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;phpass.hashPassword&lt;/code&gt; has second argument, &lt;code class=&quot;highlighter-rouge&quot;&gt;count_log2&lt;/code&gt;,
which is &lt;code class=&quot;highlighter-rouge&quot;&gt;log2&lt;/code&gt; of number of iterations. The algorithm of
hashing is as follows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;count = 2 ^ count_log2
salt = ...
hash = md5(salt .. password)
for i = 1, count do
    hash = md5(hash .. password)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>Lua MO files parser translates Lapis site</title>
   <link href="http://starius.ru/2015/05/11/mo/"/>
   <updated>2015-05-11T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/05/11/mo</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;https://github.com/starius/mo.lua&quot;&gt;The parser for MO files&lt;/a&gt; was packaged for LuaRocks
and uploaded to LuaRocks site as &lt;a href=&quot;https://luarocks.org/modules/starius/mo&quot;&gt;rock “mo”&lt;/a&gt;.
It is available for installation under terms of &lt;a href=&quot;https://github.com/starius/mo.lua/blob/master/LICENSE&quot;&gt;MIT
license&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks install mo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MO file is binary format of &lt;a href=&quot;http://www.gnu.org/software/hello/manual/gettext/MO-Files.html&quot;&gt;GNU gettext&lt;/a&gt; used to
store translations. It is created from PO file using tool
&lt;code class=&quot;highlighter-rouge&quot;&gt;msgfmt&lt;/code&gt;. It can be used to translate an application or a site
from English to other language.&lt;/p&gt;

&lt;p&gt;J.Jørgen von Bargen sent &lt;a href=&quot;http://lua-users.org/lists/lua-l/2010-04/msg00005.html&quot;&gt;the code of parser&lt;/a&gt; to
&lt;a href=&quot;http://www.lua.org/lua-l.html&quot;&gt;lua-l&lt;/a&gt; mailing list on Apr, 01, 2010. I have changed
behavior of the function: now it returns translating function
in case of success and &lt;code class=&quot;highlighter-rouge&quot;&gt;function(x) return x end&lt;/code&gt; in case of
errors. The code was covered with unit tests &lt;a href=&quot;https://coveralls.io/r/starius/mo.lua&quot;&gt;with coverage
100%&lt;/a&gt;. Unit tests are written using unit testing
framework &lt;a href=&quot;http://olivinelabs.com/busted/&quot;&gt;Busted&lt;/a&gt;.&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;where-molua-is-used&quot;&gt;Where mo.lua is used&lt;/h2&gt;

&lt;p&gt;I use this package in code of my site &lt;a href=&quot;http://kodomoquiz.tk&quot;&gt;kodomoquiz&lt;/a&gt;,
based on &lt;a href=&quot;http://leafo.net/lapis/&quot;&gt;Lapis&lt;/a&gt;. “mo” is used in many files of
kodomoquiz, e.g. in &lt;a href=&quot;https://github.com/starius/kodomoquiz/blob/master/app.lua&quot;&gt;file app.lua&lt;/a&gt; (as variable &lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Let us translate some Lua project into Russian.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;xgettext &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; ru &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--language&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Lua &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.lua &lt;span class=&quot;nt&quot;&gt;--from-code&lt;/span&gt; utf-8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This command creates (or adds to) file &lt;code class=&quot;highlighter-rouge&quot;&gt;ru/messages.po&lt;/code&gt;. It
finds texts like &lt;code class=&quot;highlighter-rouge&quot;&gt;_(&quot;text&quot;)&lt;/code&gt; in source files and adds them to
&lt;code class=&quot;highlighter-rouge&quot;&gt;.po&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;There are a lot of programs to translate .po file. One of them
is &lt;a href=&quot;http://poedit.net/&quot;&gt;poedit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To compile &lt;code class=&quot;highlighter-rouge&quot;&gt;.po&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;.mo&lt;/code&gt;, run the following command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;msgfmt ru/messages.po &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; ru/messages.mo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Module mo.lua is used to parse &lt;code class=&quot;highlighter-rouge&quot;&gt;.mo&lt;/code&gt; file from Lua:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;-- load mo.lua&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'mo'&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- load .mo file with translations&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- returns translating function on success&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- returns nil, 'error message' on failure&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'russian.mo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- translate from English into Russian&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- prints &quot;привет&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- the function returns its argument if it can't translate it&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;unknown phrase&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- prints &quot;unknown phrase&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;see-also&quot;&gt;See also&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.gnu.org/software/hello/manual/gettext/MO-Files.html&quot;&gt;GNU gettext&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://lua.2524044.n2.nabble.com/ANN-using-parser-of-MO-files-to-translate-Lapis-site-td7667288.html&quot;&gt;Thread in lua-l about mo.lua&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Introducing lua-rote, Lua binding to ROTE, Terminal Emulation library</title>
   <link href="http://starius.ru/2015/03/28/lua-rote/"/>
   <updated>2015-03-28T00:00:00+00:00</updated>
   <id>http://starius.ru/2015/03/28/lua-rote</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://rote.sourceforge.net/&quot;&gt;ROTE&lt;/a&gt; is a simple C library for VT102 terminal emulation.
It allows the programmer to set up virtual ‘screens’ and send
them data. The virtual screens will emulate the behavior of a
VT102 terminal, interpreting escape sequences, control
characters and such. The library supports ncurses as well so
that you may render the virtual screen to the real screen
when you need to.&lt;/p&gt;

&lt;p&gt;There are several programs that do terminal emulation, such
as xterm, rxvt, screen and even the Linux console driver
itself. However, it is not easy to isolate their terminal
emulation logic and put it in a module that can be easily
reused in other programs. That’s where the ROTE library
comes in.&lt;/p&gt;

&lt;p&gt;The goal of the &lt;a href=&quot;http://starius.github.io/lua-rote&quot;&gt;lua-rote&lt;/a&gt; library is to provide terminal
emulation support for Lua applications, making it
possible to write programs that display terminals in
embedded windows within them, or even monitor the display
produced by other programs. The lua-rote library depend
only on Lua, ROTE itself, ncurses and luaposix.&lt;/p&gt;

&lt;p&gt;The ROTE library is able to render the
virtual screens to the physical screen (actually any
ncurses window) and can also translate ncurses key codes to
the escape sequences the Linux console would have produced
(and feed them into the terminal). Using ncurses is not
mandatory however, and ROTE will work fine without it, but
in that case the application must take care of drawing the
terminal to the screen in whichever way it sees fit.&lt;/p&gt;

&lt;p&gt;ROTE also encapsulates the functionality needed to execute
a child process using the virtual screen as the controlling
terminal. It will handle the creation of the
pseudo-terminal and the child process. All the application
has to do is tell it the command to run in the terminal and
call an update function at regular intervals to allow the
terminal to update itself.&lt;/p&gt;

&lt;p&gt;ROTE is extremely useful to programmatically interact
with curses applications (e.g., for unit testing).&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Lua 5.1, 5.2, 5.3 or LuaJIT&lt;/li&gt;
  &lt;li&gt;curses (binary + headers)&lt;/li&gt;
  &lt;li&gt;luaposix (install after installing curses headers!)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://rote.sourceforge.net/&quot;&gt;ROTE&lt;/a&gt; (install after installing curses headers!)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Curses and luaposix are needed for drawing state of ROTE
terminal on curses’ WINDOW object
(method &lt;a href=&quot;#draw&quot;&gt;RoteTerm:draw()&lt;/a&gt;).
If you do not need this feature and want to exclude these
two dependencies, then remove CURSES and luaposix from
file &lt;code class=&quot;highlighter-rouge&quot;&gt;lua-rote-*.rockspec&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;See &lt;a href=&quot;https://github.com/starius/lua-rote/blob/master/.travis/install_rote.sh&quot;&gt;shell script&lt;/a&gt; with installation
commands for Debian Wheezy.&lt;/p&gt;

&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;p&gt;This library is built using &lt;a href=&quot;http://luarocks.org&quot;&gt;LuaRocks&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;option-1-install-from-luarocks-server&quot;&gt;Option 1: install from LuaRocks server&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks install lua-rote
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you have installed ROTE to prefix other than “/usr”,
you have to provide this path to LuaRocks.
For example, if you have installed ROTE to “/usr/local”,
use the following command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks install lua-rote &lt;span class=&quot;nv&quot;&gt;ROTE_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;option-2-install-from-local-source-tree&quot;&gt;Option 2: install from local source tree&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/starius/lua-rote.git
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;lua-rote
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks make
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;running-unit-tests&quot;&gt;Running unit tests&lt;/h2&gt;

&lt;p&gt;Unit tests are written using unit testing framework
&lt;a href=&quot;http://olivinelabs.com/busted/&quot;&gt;busted&lt;/a&gt;.
Unit tests can serve as reference documentation and
code examples.&lt;/p&gt;

&lt;p&gt;To run unit tests, install busted from LuaRocks:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;luarocks install busted
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Go to the source folder of lua-rote and run command &lt;code class=&quot;highlighter-rouge&quot;&gt;busted&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;busted
++++++++++++++++++++++++++++++++
32 successes / 0 failures / 0 errors / 0 pending : 1.5 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;running-the-demo&quot;&gt;Running the demo&lt;/h2&gt;

&lt;p&gt;Program &lt;a href=&quot;https://github.com/starius/lua-rote/blob/master/demo/boxshell.lua&quot;&gt;boxshell.lua&lt;/a&gt; is a clone of
ROTE’s example program “boxshell.c” (file “demo/boxshell.c”
in ROTE’s source tree).
Both programs include the following steps:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;start curses,&lt;/li&gt;
  &lt;li&gt;fill the screen with blue,&lt;/li&gt;
  &lt;li&gt;create curses window in the middle of the screen,&lt;/li&gt;
  &lt;li&gt;start ROTE terminal, fork bash inside,&lt;/li&gt;
  &lt;li&gt;do in a loop until child process dies:
    &lt;ul&gt;
      &lt;li&gt;redraw curses window accorsing to ROTE terminal,&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;getch()&lt;/code&gt;, results of which are passed to ROTE terminal.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run &lt;code class=&quot;highlighter-rouge&quot;&gt;lua demo/boxshell.lua&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;ls&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;busted&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://i.imgur.com/F5K9gJt.png&quot; alt=&quot;boxshell.lua&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Currently lua-rote does not support unicode characters,
that is why busted was changed to produce “+” instead of “●”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;There are some differences between boxshell.c and
boxshell.lua. Program boxshell.lua can fork other commands
as well as bash. boxshell.c uses &lt;code class=&quot;highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; mode
repeating draw-getch cycle without a delay,
while boxshell.lua uses &lt;code class=&quot;highlighter-rouge&quot;&gt;halfdelay&lt;/code&gt; mode repeating
draw-getch cycle 10 times a second.
That is why boxshell.c constantly consumes 100% CPU,
while boxshell.lua consumes almost no CPU when inactive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;h3 id=&quot;module-rote&quot;&gt;Module rote&lt;/h3&gt;

&lt;p&gt;Library lua-rote is loaded from module “rote”:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'rote'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All code of the library “lives” inside this module.&lt;/p&gt;

&lt;h3 id=&quot;class-roteterm&quot;&gt;Class RoteTerm&lt;/h3&gt;

&lt;p&gt;The main part of the library is class RoteTerm.
It wraps C structure RoteTerm, declared in library ROTE.
RoteTerm represents terminal emulator.&lt;/p&gt;

&lt;p&gt;Create a new virtual terminal with the given dimensions.
(Height is 24 rows, width is 80 columns.)&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RoteTerm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Instance of RoteTerm is destroyed automatically
when the corresponding Lua object is collected.&lt;/p&gt;

&lt;h3 id=&quot;start-child-process&quot;&gt;Start child process&lt;/h3&gt;

&lt;p&gt;Start a forked process in the terminal:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forkPty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'less /some/file'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The command will be interpreted by ‘/bin/sh -c’.&lt;/p&gt;

&lt;p&gt;Returns PID of the child process.
On error returns &lt;code class=&quot;highlighter-rouge&quot;&gt;-1&lt;/code&gt;.
Notice that passing an invalid command will not cause
an error at this level: the shell will try to execute
the command and will exit with status 127. You can catch
that by installing a &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGCHLD&lt;/code&gt; handler if you want.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;If you want to be notified when child processes exits,
you should handle the &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGCHLD&lt;/code&gt; signal.
If, on the other hand, you want to ignore exitting
child processes, you should set the &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGCHLD&lt;/code&gt; handler to
&lt;code class=&quot;highlighter-rouge&quot;&gt;SIG_IGN&lt;/code&gt; to prevent child processes from hanging
around the system as ‘zombie processes’.&lt;/p&gt;

  &lt;p&gt;You can use luaposix to manage child processes as described
above. See file &lt;a href=&quot;https://github.com/starius/lua-rote/blob/master/demo/boxshell.lua&quot;&gt;demo/boxshell.lua&lt;/a&gt;.&lt;/p&gt;

  &lt;p&gt;Continuing to write to a RoteTerm whose child process
has died does not accomplish a lot, but is not an error
and should not cause your program to crash or block
indefinitely or anything of that sort :-)&lt;/p&gt;

  &lt;p&gt;If, however, you want to be tidy and inform the RoteTerm
that its child has died, call method &lt;code class=&quot;highlighter-rouge&quot;&gt;forsakeChild&lt;/code&gt;
when appropriate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can get the PID later by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;rt:childPid()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Disconnect the RoteTerm from its forked child process:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forsakeChild&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;getting-contents-of-the-terminal&quot;&gt;Getting contents of the terminal&lt;/h3&gt;

&lt;p&gt;You can get number of rows and columns of the terminal:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Get cursor coordinates:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Before getting any output from the child process, call method
&lt;code class=&quot;highlighter-rouge&quot;&gt;rt:update()&lt;/code&gt; to update internal state of RoteTerm.&lt;/p&gt;

&lt;p&gt;You can get value of character and attribute of any cell:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cellChar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- string of length 1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cellAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;lua-rote provides &lt;a href=&quot;#handling-attributes&quot;&gt;several functions&lt;/a&gt;
to handle attribute values.&lt;/p&gt;

&lt;p&gt;Get current attribute, that is the attribute that will be
used for newly characters:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Get a row as a string (not terminated with &lt;code class=&quot;highlighter-rouge&quot;&gt;\n&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rowText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Get whole terminal as a string (rows are terminated with &lt;code class=&quot;highlighter-rouge&quot;&gt;\n&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;termText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;a name=&quot;draw&quot; id=&quot;draw&quot;&gt;&lt;/a&gt;
Draw contents of ROTE terminal on curses WINDOW:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;curses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'posix.curses'&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- setup curses, see demo/boxshell.lua&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;start_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;start_col&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start_col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;changing-the-terminal-state&quot;&gt;Changing the terminal state&lt;/h3&gt;

&lt;p&gt;You can directly change internal state of RoteTerm by
calling the following methods:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setCellChar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;character&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- character at (row, col)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setCellAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- attribute at (row, col)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- current attribute&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can pass data to the child process or to the terminal:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;-- Puts data ':wq\n' into the terminal.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- If there is a forked process, the data will be sent to it.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- If there is no forked process, the data will simply&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- be injected into the terminal (as in inject()).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;':wq\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Inject data directly into the terminal.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;':wq\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;-- Indicates to the terminal that the key has been pressed.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- Appropriate escape sequence is passed to method write().&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keycode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;string.byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keyPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keycode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can get values of keycodes from &lt;a href=&quot;https://luaposix.github.io/luaposix/modules/posix.curses.html&quot;&gt;posix.curses&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;snapshots&quot;&gt;Snapshots&lt;/h3&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;-- take a snapshot of the current contents of the terminal&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;snapshot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;takeSnapshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- ... do something ...&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- restore a snapshot previously taken&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restoreSnapshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;snapshot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Snapshot object is deleted automatically when the
corresponding Lua object is collected.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;handling-attributes&quot; id=&quot;handling-attributes&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;handling-attributes&quot;&gt;Handling attributes&lt;/h3&gt;

&lt;p&gt;An ‘attribute’ as used in this library means an 8-bit value
that conveys a foreground color code, a background color code,
and the bold and blink bits. Each cell in the virtual terminal
screen is associated with an attribute that specifies
its appearance.&lt;/p&gt;

&lt;p&gt;The bits of an attribute, from most significant to
least significant, are&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; bit:      7 6 5 4 3 2 1 0
 content:  S F F F H B B B
           | `-,-' | `-,-'
           |   |   |   |
           |   |   |   `----- 3-bit background color (0 - 7)
           |   |   `--------- blink bit
           |   `------------- 3-bit foreground color (0 - 7)
           `----------------- bold bit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Color codes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;0 = black,&lt;/li&gt;
  &lt;li&gt;1 = red,&lt;/li&gt;
  &lt;li&gt;2 = green,&lt;/li&gt;
  &lt;li&gt;3 = yellow,&lt;/li&gt;
  &lt;li&gt;4 = blue,&lt;/li&gt;
  &lt;li&gt;5 = magenta,&lt;/li&gt;
  &lt;li&gt;6 = cyan,&lt;/li&gt;
  &lt;li&gt;7 = white.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are functions provided to “pack” and “unpack”
attribute bits:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;foreground&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blink&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foreground&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- foreground and background are integers (0 - 7)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;-- bold and blink are booleans&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The library provides tables converting color codes to and from
human readable names:&lt;/p&gt;

&lt;div class=&quot;language-lua highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color2name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- prints &quot;green&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name2color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;green&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- prints &quot;2&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;bugs&quot;&gt;Bugs&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Unicode characters are printed and read with errors.&lt;/li&gt;
  &lt;li&gt;Method &lt;code class=&quot;highlighter-rouge&quot;&gt;RoteTerm:draw()&lt;/code&gt; is &lt;a href=&quot;https://travis-ci.org/starius/lua-rote/jobs/54479120#L1160&quot;&gt;unreliable&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;ROTE can’t read cell 0x0 in 1x2 window when
 reads second time. It seems to be related to
 low number of columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/starius/lua-rote/issues/new&quot;&gt;Report a bug&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;author&quot;&gt;Author&lt;/h2&gt;

&lt;p&gt;Corresponding author: Boris Nagaev, email: bnagaev@gmail.com&lt;/p&gt;

&lt;p&gt;Copyright (C) 2015 Boris Nagaev&lt;/p&gt;

&lt;p&gt;See the &lt;a href=&quot;https://github.com/starius/lua-rote/blob/master/LICENSE&quot;&gt;LICENSE&lt;/a&gt; file for terms of use.&lt;/p&gt;

&lt;p&gt;ROTE was written by Bruno T. C. de Oliveira,
see &lt;a href=&quot;http://rote.sourceforge.net/&quot;&gt;rote.sourceforge.net&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://starius.github.io/lua-rote&quot;&gt;Home page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://rote.sourceforge.net/&quot;&gt;ROTE&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/starius/lua-rote/issues/new&quot;&gt;Report a bug&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/lua/comments/30ast4/ann_luarote_lua_binding_to_rote_terminal/&quot;&gt;Reddit&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://habrahabr.ru/post/254089/&quot;&gt;Хабрахабр&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://lua-users.org/lists/lua-l/2015-03/msg00325.html&quot;&gt;lua-l&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://olivinelabs.com/busted/&quot;&gt;Busted&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/moteus/lua-travis-example&quot;&gt;lua-travis-example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Bitcoin deposits for deals (Howto)</title>
   <link href="http://starius.ru/2014/12/18/bitcoin-escrow/"/>
   <updated>2014-12-18T00:00:00+00:00</updated>
   <id>http://starius.ru/2014/12/18/bitcoin-escrow</id>
   <content type="html">&lt;p&gt;Bitcoin is a very convenient and reliable as a storage of money.
But how can you carry out deals with it?
Traditional methods introduce a trusted third party.
Bitcoin turned out to be powerful enough to carry out
deals safely without involment of third parties.
Below is a detailed instruction of how to carry out a deal with
anybody you do not trust without involment of third parties.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://i.imgur.com/5l657XE.png&quot; alt=&quot;esrow&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let us review existing methods at first.&lt;/p&gt;

&lt;h3 id=&quot;using-a-transaction-with-traditional-money&quot;&gt;Using a transaction with traditional money&lt;/h3&gt;

&lt;p&gt;When using a transaction with traditional money, you rely on
existing laws protecting both parties from each other.
This requires the inclusion of a third party. Furthermore,
there is no anonymity and exchange fees are added.&lt;/p&gt;

&lt;h3 id=&quot;carrying-out-transactions-using-an-escrow-account&quot;&gt;Carrying out transactions using an escrow account&lt;/h3&gt;

&lt;p&gt;An escrow is a third party trusted by both parties to the transaction.
the buyer gives money to an escrow, and the seller gives the goods
to the escrow. Then the escrow gives money to the seller and the goods to buyer.
This method has a drawback. The escrow has to accumulate a good reputation.
The better the reputation, the more deals are consequently
mediated by the escrow. Escrow is a system with positive feedback, therefore
the overall number of escrows decreases, which results in centralization.
Centralization is not ideal, because it results in a monopoly
and creates less competition.&lt;/p&gt;

&lt;h3 id=&quot;bitcoin-deals-without-third-parties&quot;&gt;Bitcoin deals without third parties&lt;/h3&gt;

&lt;p&gt;Third parties are not needed for a bitcoin deal.
A buyer, a seller and the bitcoin itself are sufficient
for a secure deal. Banks, money exchanges, lawyers, and
escrows are parasitizing on deals.
The protocol of bitcoin already has all that is needed to
carry out a secure deal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The algorithm is very simple: both parties deposit some money.
If the deal succeeds, the money is returned, otherwise it is lost.&lt;/strong&gt;&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;&lt;a name=&quot;cut&quot; id=&quot;cut&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;a href=&quot;https://en.bitcoin.it/wiki/Address#Multi-signature_addresses&quot;&gt;multisig address&lt;/a&gt; is used.
Debiting the multisig address needs several signatures.
Popular multisig address types are 2/2 and 2/3.
Debiting a 2/2 multisig address needs 2 signatures of 2.
Debiting a 2/3 multisig address needs 2 signatures of 3.&lt;/p&gt;

&lt;p&gt;The buyer and the seller create a multisig 2/2 address
and move their money to it.
None of them can get his money back without approval
by the other party.
After this the deal itself is carried out
(let it be a purchase of goods or a provision of services
for bitcoin).
If the deal succeeds (the buyer gets the goods and the seller
gets the money), the parties of the deal take the money
back from the deposit.
If a conflict occurs or one party just disappears,
they both lose the money deposited.&lt;/p&gt;

&lt;p&gt;Each party depends on the other. The risk of losing money
is a greater deterrent to dishonesty, than is general law.
Some people accept penalities for violation of the law,
but no one wants to lose money. The loss of money is the
most powerful economic argument.
It makes no sense to cheat if it obviously leads to losses.&lt;/p&gt;

&lt;p&gt;In order for this method to work, the deposited money must
exceed the price of the goods.
Otherwise the party getting the goods or the money first,
can pull the plug on the deposit and disappear.
&lt;strong&gt;It is best if each party deposits double the price
of the goods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With bitcoin, money is deposited from both parties by one bitcoin
transaction. This means that either both parties
deposit the money or none of them deposits.
This prevents dishonesty or trickery by one of the parties.&lt;/p&gt;

&lt;p&gt;The final diagram:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://i.imgur.com/5l657XE.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Price of the goods is 1 BTC. Each party deposites 2 BTC.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Alice and Bob create common multisig 2/2 address to be used
as deposit storage.&lt;/li&gt;
  &lt;li&gt;Alice and Bob sign the transaction, moving
2 BTC from Alice’s wallet to the multisig 2/2 address and
2 BTC from Bob’s wallet to the multisig 2/2 address.&lt;/li&gt;
  &lt;li&gt;The deal itself:
    &lt;ul&gt;
      &lt;li&gt;Alice provides services to Bob.&lt;/li&gt;
      &lt;li&gt;Bob pays 1 BTC to Alice.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Alice and Bob take the money back from the deposit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This algorithm is a wrapper for any deal.
The overhead is only fees of 2 bitcoin transactions
(to and from deposit).
If transactions refilling personal wallets are counted as well
(see below “Preparation”), then the final fee is four transaction fees
(0.0004 BTC).&lt;/p&gt;

&lt;p&gt;If the deal includes a private meeting, then moving the money
to and from a deposit can happen before and after the private meeting.
Moreover, participants of a private meeting feel better
if they are bound by the deposit instead of being
just strangers.
The method described works well for distant deals as well
(e.g., a person from one country sells an intellectual product
to a person from other country).
Several consequent deals may happen “inside”
one deposit.&lt;/p&gt;

&lt;h3 id=&quot;implementation&quot;&gt;Implementation&lt;/h3&gt;

&lt;p&gt;Whereas the graphical tools Bitcoin-qt and Electrum do not provide a
convenient interface to carry out two transactions needed,
the Web client &lt;a href=&quot;https://coinb.in/&quot;&gt;Coinbin&lt;/a&gt;
based on &lt;a href=&quot;http://bitcoinjs.org/&quot;&gt;BitcoinJS&lt;/a&gt;
does provide it.
The client doesn’t depend on the developer’s server
and doesn’t store cryptographic keys remotely,
so it doesn’t require to trust third parties.
Furthermore, Coinbin can be
&lt;a href=&quot;https://github.com/OutCast3k/coinbin/archive/master.zip&quot;&gt;downloaded&lt;/a&gt;
and used locally or uploaded to one’s own server.
E-mail and passwords are used as seeds for key generation.
To posses multiple addresses, use several pairs (E-mail, password).
Backup your E-mail and password to prevent loss of access to the wallet.
&lt;a href=&quot;https://www.keepassx.org/&quot;&gt;KeePassX&lt;/a&gt; is a good place for this.&lt;/p&gt;

&lt;p&gt;Step-by-step instructions for securing a deal:&lt;/p&gt;

&lt;h4 id=&quot;preparation&quot;&gt;Preparation&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;Alice and Bob create Coinbin wallets
(&lt;a href=&quot;http://i.imgur.com/xWV35gc.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;See keys, backup private keys, tell each other their
public keys and addresses
(&lt;a href=&quot;http://i.imgur.com/Bph3789.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;Send the money to be deposited to Coinbin addresses
(0.01 BTC from each of them)
(&lt;a href=&quot;http://i.imgur.com/K8gooL1.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;Alice creates multisig 2/2 address
    &lt;ul&gt;
      &lt;li&gt;Finds MultiSig Address in Coinbin menu
(&lt;a href=&quot;http://i.imgur.com/OivaZnW.png&quot;&gt;pic&lt;/a&gt;).
The &lt;a href=&quot;http://i.imgur.com/Bdasncz.png&quot;&gt;form&lt;/a&gt; is shown.&lt;/li&gt;
      &lt;li&gt;Fill the form with public keys of her’s and Bob’s
(&lt;a href=&quot;http://i.imgur.com/gAF538K.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Backs up resulting address and Redeem Script
and sends them to Bob
(&lt;a href=&quot;http://i.imgur.com/2Tv7AAX.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Bob verifies Redeem Script
    &lt;ul&gt;
      &lt;li&gt;Selects Verify in Coinbin menu
(&lt;a href=&quot;http://i.imgur.com/2NpTwOj.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Enters Redeem Script and compares public keys,
address and that Required Signatures = 2
(&lt;a href=&quot;https://i.imgur.com/NtzBRO8.png&quot;&gt;pic&lt;/a&gt;).
&lt;strong&gt;Clarification&lt;/strong&gt;: Bob knows his public key and Alice’s
public key sent to him previously.
He gets the address of multisig 2/2 along with
Redeem Script.
Required Signatures is the number of signatures
required for debiting multisig 2/2 address,
so it is 2.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;depositing-money&quot;&gt;Depositing money&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;Alice creates a transaction moving the money to multisig 2/2 address
(that is depositing the money)
    &lt;ul&gt;
      &lt;li&gt;Selects Transaction in Coinbin menu
(&lt;a href=&quot;https://i.imgur.com/WxP9g4i.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Goes to Advanced Options, unchecks Clear existing inputs…
(&lt;a href=&quot;http://i.imgur.com/Rc54mb0.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Enters first paying address (Alice’s) as an Input
(&lt;a href=&quot;http://i.imgur.com/qI3TJCH.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Enters second paying address (Bob’s) as an Input
(&lt;a href=&quot;http://i.imgur.com/sscFtfY.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Enters multisig 2/2 address as an Output.
Amount of money is sum of inputs minus
bitcoin transaction fee
(&lt;a href=&quot;http://i.imgur.com/0YFlfYd.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Gets Transaction Script and sends it to Bob for verification
(&lt;a href=&quot;http://i.imgur.com/2TZuyXr.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Bob verifies Transaction Script
    &lt;ul&gt;
      &lt;li&gt;Selects Verify in Coinbin menu
(&lt;a href=&quot;http://i.imgur.com/ATPIxC7.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Enters Transaction Script and makes sure
multisig address and true amount of money are in Outputs
(&lt;a href=&quot;http://i.imgur.com/InxmBla.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Alice and Bob sign the Transaction Script one after another
    &lt;ul&gt;
      &lt;li&gt;Alice selects Sign in Coinbin menu
(&lt;a href=&quot;http://i.imgur.com/k0Od2Xr.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Alice enters Transaction Script and her private key
(&lt;a href=&quot;http://i.imgur.com/k0Od2Xr.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Alice gets partly signed transaction and sends it to Bob
(&lt;a href=&quot;http://i.imgur.com/MAPOGDv.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Bob verifies the transaction one more time and makes sure
output address (multisig) and amount of money
have not changed.&lt;/li&gt;
      &lt;li&gt;Bob completes signing the transaction
(same actions like for Alice part of signing,
but uses partly signed Transaction Script instead of
original one)
(&lt;a href=&quot;http://i.imgur.com/qmk1BeT.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Bob verifies the transaction one more time and makes sure
column Signed is marked for each row
(&lt;a href=&quot;https://i.imgur.com/LPxNLvZ.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Bob selects Broadcast in Coinbin menu
(&lt;a href=&quot;http://i.imgur.com/LjWRrSK.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
      &lt;li&gt;Bob enters Transaction Script and presses Submit
(&lt;a href=&quot;http://i.imgur.com/MtnFVak.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The money has been deposited and
they can switch to the deal itself.&lt;/p&gt;

&lt;h4 id=&quot;getting-deposited-money-back&quot;&gt;Getting deposited money back&lt;/h4&gt;

&lt;p&gt;If the deal fails, both parties lose deposited money.
(This is the whole point, making another party
conscientiously fulfill the conditions of the deal
under pain of losing money.)
If the deal succeeds, parties move the money back from
the deposit to their personal wallets.
This is possible only if both parties agree.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Alice creates a transaction moving money from the multisig 2/2 address
to personal wallets of Alice and Bob.
    &lt;ul&gt;
      &lt;li&gt;Alice selects Transaction in Coinbin menu
(&lt;a href=&quot;https://i.imgur.com/WxP9g4i.png&quot;&gt;pic&lt;/a&gt;),&lt;/li&gt;
      &lt;li&gt;clears all inputs and outputs left over from
previous transaction
(&lt;a href=&quot;http://i.imgur.com/avIRd4e.png&quot;&gt;pic&lt;/a&gt;),&lt;/li&gt;
      &lt;li&gt;enters Redeem Script (see Preparation)
(&lt;a href=&quot;https://i.imgur.com/jHHdz2P.png&quot;&gt;pic&lt;/a&gt;),&lt;/li&gt;
      &lt;li&gt;enters personal addresses of Alice and Bob as Outputs
(&lt;a href=&quot;http://i.imgur.com/rf5DaHK.png&quot;&gt;pic&lt;/a&gt;),&lt;/li&gt;
      &lt;li&gt;gets Transaction Script and sends it to Bob for verification
(&lt;a href=&quot;http://i.imgur.com/DUUzjoO.png&quot;&gt;pic&lt;/a&gt;).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Bob verifies Transaction Script
(finds his personal address and checks amount of money)&lt;/li&gt;
  &lt;li&gt;Alice signs Transaction Script and sends it to Bob&lt;/li&gt;
  &lt;li&gt;Bob verifies Transaction Script one moretime
(finds his personal address and checks amount of money)&lt;/li&gt;
  &lt;li&gt;Bob signs Transaction Script and broadcasts the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The money has been moved from the deposit back to personal
addresses of Alice and Bob.
The deal is over.&lt;/p&gt;

&lt;h3 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;The algorithm described acts as fraud protection for any deal.
It doesn’t introduce third parties as traditional methods do,
and overhead is almost nothing.
The algorithm is not complicated even if it looks so.
I mastered the technique after two “deals” with myself.&lt;/p&gt;

&lt;p&gt;Improvements of the algorithm and software
implementation are welcome.&lt;/p&gt;

&lt;h3 id=&quot;links&quot;&gt;Links&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/OutCast3k/coinbin&quot;&gt;GitHub&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Bitcoin address of Coinbin developer: 1CWHWkTWaq1K5hevimJia3cyinQsrgXUvg&lt;/li&gt;
  &lt;li&gt;Bitcoin address of the instruction author: 1CsfihQGy5kg3nnTJLzfmNDiPGqiQgdeMz&lt;/li&gt;
&lt;/ol&gt;

</content>
 </entry>
 

</feed>
