NetNewsWire/Modules/Parser/Tests/ParserTests/Resources/russcox.atom
2024-07-06 21:07:05 -07:00

182 lines
434 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<feed xmlns="http://www.w3.org/2005/Atom">
<title>research!rsc</title>
<id>tag:research.swtch.com,2012:research.swtch.com</id>
<link rel="self" href="http://research.swtch.com/feed.atom"></link>
<updated>2019-03-01T11:01:00-05:00</updated>
<author>
<name>Russ Cox</name>
<uri>https://swtch.com/~rsc</uri>
<email>rsc@swtch.com</email>
</author>
<entry>
<title>Transparent Logs for Skeptical Clients</title>
<id>tag:research.swtch.com,2012:research.swtch.com/tlog</id>
<link rel="alternate" href="http://research.swtch.com/tlog"></link>
<published>2019-03-01T11:00:00-05:00</published>
<updated>2019-03-01T11:02:00-05:00</updated>
<summary type="text">How an untrusted server can publish a verifiably append-only log.</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;Suppose we want to maintain and publish a public, append-only log of data.&#xA;Suppose also that clients are skeptical about our correct implementation&#xA;and operation of the log:&#xA;it might be to our advantage to leave things out of the log,&#xA;or to enter something in the log today and then remove it tomorrow.&#xA;How can we convince the client we are behaving?&#xA;&#xA;&lt;p&gt;&#xA;This post is about an elegant data structure we can use to publish&#xA;a log of &lt;i&gt;N&lt;/i&gt; records with these three properties:&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;For any specific record &lt;i&gt;R&lt;/i&gt; in a log of length &lt;i&gt;N&lt;/i&gt;,&#xA;we can construct a proof of length&#xA;&lt;i&gt;O&lt;/i&gt;(lg &lt;i&gt;N&lt;/i&gt;) allowing the client to verify that &lt;i&gt;R&lt;/i&gt; is in the log.&#xA;&lt;li&gt;&#xA;For any earlier log observed and remembered by the client,&#xA;we can construct a proof of length&#xA;&lt;i&gt;O&lt;/i&gt;(lg &lt;i&gt;N&lt;/i&gt;) allowing the client to verify that the earlier log&#xA;is a prefix of the current log.&#xA;&lt;li&gt;&#xA;An auditor can efficiently iterate over the records in the log.&lt;/ol&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;(In this post, “lg &lt;i&gt;N&lt;/i&gt;” denotes the base-2 logarithm of &lt;i&gt;N&lt;/i&gt;,&#xA;reserving the word “log” to mean only “a sequence of records.”)&#xA;&#xA;&lt;p&gt;&#xA;The&#xA;&lt;a href=&#34;https://www.certificate-transparency.org/&#34;&gt;Certificate Transparency&lt;/a&gt;&#xA;project publishes TLS certificates in this kind of log.&#xA;Google Chrome uses property (1) to verify that&#xA;an &lt;a href=&#34;https://en.wikipedia.org/wiki/Extended_Validation_Certificate&#34;&gt;enhanced validation certificate&lt;/a&gt;&#xA;is recorded in a known log before accepting the certificate.&#xA;Property (2) ensures that an accepted certificate cannot later disappear from the log undetected.&#xA;Property (3) allows an auditor to scan the entire certificate log&#xA;at any later time to detect misissued or stolen certificates.&#xA;All this happens without blindly trusting that&#xA;the log itself is operating correctly.&#xA;Instead, the clients of the log—Chrome and any auditors—verify&#xA;correct operation of the log as part of accessing it.&#xA;&#xA;&lt;p&gt;&#xA;This post explains the design and implementation&#xA;of this verifiably tamper-evident log,&#xA;also called a &lt;i&gt;transparent log&lt;/i&gt;.&#xA;To start, we need some cryptographic building blocks.&#xA;&lt;a class=anchor href=&#34;#cryptographic_hashes_authentication_and_commitments&#34;&gt;&lt;h2 id=&#34;cryptographic_hashes_authentication_and_commitments&#34;&gt;Cryptographic Hashes, Authentication, and Commitments&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A &lt;i&gt;cryptographic hash function&lt;/i&gt; is a deterministic&#xA;function H that maps an arbitrary-size message &lt;i&gt;M&lt;/i&gt;&#xA;to a small fixed-size output H(&lt;i&gt;M&lt;/i&gt;),&#xA;with the property that it is infeasible in practice to produce&#xA;any pair of distinct messages &lt;i&gt;M&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; ≠ &lt;i&gt;M&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; with&#xA;identical hashes H(&lt;i&gt;M&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;) = H(&lt;i&gt;M&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;).&#xA;Of course, what is feasible in practice changes.&#xA;In 1995, SHA-1 was a reasonable cryptographic hash function.&#xA;In 2017, SHA-1 became a &lt;i&gt;broken&lt;/i&gt; cryptographic hash function,&#xA;when researchers identified and demonstrated&#xA;a &lt;a href=&#34;https://shattered.io/&#34;&gt;practical way to generate colliding messages&lt;/a&gt;.&#xA;Today, SHA-256 is believed to be a reasonable cryptographic hash function.&#xA;Eventually it too will be broken.&#xA;&#xA;&lt;p&gt;&#xA;A (non-broken) cryptographic hash function provides&#xA;a way to bootstrap a small amount of trusted data into&#xA;a much larger amount of data.&#xA;Suppose I want to share a very large file with you,&#xA;but I am concerned that the data may not arrive intact,&#xA;whether due to random corruption or a&#xA;&lt;a href=&#34;TODO&#34;&gt;man-in-the-middle attack&lt;/a&gt;.&#xA;I can meet you in person and hand you,&#xA;written on a piece of paper,&#xA;the SHA-256 hash of the file.&#xA;Then, no matter what unreliable path the bits take,&#xA;you can check whether you got the right ones by&#xA;recomputing the SHA-256 hash of the download.&#xA;If it matches, then you can be certain,&#xA;assuming SHA-256 has not been broken,&#xA;that you downloaded the exact bits I intended.&#xA;The SHA-256 hash &lt;i&gt;authenticates&lt;/i&gt;—that is,&#xA;it proves the authenticity of—the downloaded bits,&#xA;even though it is only 256 bits and the download&#xA;is far larger.&#xA;&#xA;&lt;p&gt;&#xA;We can also turn the scenario around,&#xA;so that, instead of distrusting the network,&#xA;you distrust me.&#xA;If I tell you the SHA-256 of a file I promise to send,&#xA;the SHA-256 serves as a verifiable &lt;i&gt;commitment&lt;/i&gt; to a particular sequence of bits.&#xA;I cannot later send a different bit sequence and convince you&#xA;it is the file I promised.&#xA;&#xA;&lt;p&gt;&#xA;A single hash can be an authentication or commitment&#xA;of an arbitrarily large amount of data,&#xA;but verification then requires hashing the entire data set.&#xA;To allow selective verification of subsets of the data,&#xA;we can use not just a single hash&#xA;but instead a balanced binary tree of hashes,&#xA;known as a Merkle tree.&#xA;&lt;a class=anchor href=&#34;#merkle_trees&#34;&gt;&lt;h2 id=&#34;merkle_trees&#34;&gt;Merkle Trees&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A Merkle tree is constructed from &lt;i&gt;N&lt;/i&gt; records,&#xA;where &lt;i&gt;N&lt;/i&gt; is a power of two.&#xA;First, each record is hashed independently, producing &lt;i&gt;N&lt;/i&gt; hashes.&#xA;Then pairs of hashes are themselves hashed,&#xA;producing &lt;i&gt;N&lt;/i&gt;/2 new hashes.&#xA;Then pairs of those hashes are hashed,&#xA;to produce &lt;i&gt;N&lt;/i&gt;/4 hashes,&#xA;and so on, until a single hash remains.&#xA;This diagram shows the Merkle tree of size &lt;i&gt;N&lt;/i&gt; = 16:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-16&#34; class=&#34;center pad&#34; width=518 height=193 src=&#34;tlog-16.png&#34; srcset=&#34;tlog-16.png 1x, tlog-16@1.5x.png 1.5x, tlog-16@2x.png 2x, tlog-16@3x.png 3x, tlog-16@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The boxes across the bottom represent the 16 records.&#xA;Each number in the tree denotes a single hash,&#xA;with inputs connected by downward lines.&#xA;We can refer to any hash by its coordinates:&#xA;level &lt;i&gt;L&lt;/i&gt; hash number &lt;i&gt;K&lt;/i&gt;, which we will abbreviate h(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;).&#xA;At level 0, each hashs input is a single record;&#xA;at higher levels, each hashs input is a pair of hashes from the level below.&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;h(0,&lt;i&gt;K&lt;/i&gt;)&#x9;= H(record &lt;i&gt;K&lt;/i&gt;)&lt;br&gt;&#xA;h(&lt;i&gt;L&lt;/i&gt;+1, &lt;i&gt;K&lt;/i&gt;)&#x9;= H(h(&lt;i&gt;L&lt;/i&gt;, 2&lt;i&gt;K&lt;/i&gt;), h(&lt;i&gt;L&lt;/i&gt;, 2&lt;i&gt;K&lt;/i&gt;+1))&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;To prove that a particular record is contained in the tree&#xA;represented by a given top-level hash&#xA;(that is, to allow the client to authenticate a record, or verify a prior&#xA;commitment, or both),&#xA;it suffices to provide the hashes needed to recompute&#xA;the overall top-level hash from the records hash.&#xA;For example, suppose we want to prove that a certain bit string &lt;i&gt;B&lt;/i&gt;&#xA;is in fact record 9 in a tree of 16 records with top-level hash &lt;i&gt;T&lt;/i&gt;.&#xA;We can provide those bits along with the other hash inputs&#xA;needed to reconstruct the overall tree hash using those bits.&#xA;Specifically, the client can derive as well as we can that:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;T&#x9;=&#x9;h(4,0)&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), h(3,1))&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), H(h(2,2),&#x9;&#x9;h(2,3)))&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), H(H(h(1,4),&#x9;h(1,5)),&#x9;h(2,3)))&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), H(H(H(h(0,8), h(0,9)),&#x9;h(1,5)),&#x9;h(2,3)))&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), H(H(H(h(0,8), H(record 9)),&#x9;h(1,5)),&#x9;h(2,3)))&lt;br&gt;&#xA;&#x9;=&#x9;H(h(3,0), H(H(H(h(0,8), H(&lt;i&gt;B&lt;/i&gt;)),&#x9;h(1,5)),&#x9;h(2,3)))&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;If we give the client the values [h(3,0), h(0,8), h(1,5), h(2,3)],&#xA;the client can calculate H(&lt;i&gt;B&lt;/i&gt;) and then combine all those hashes&#xA;using the formula and check whether the result matches &lt;i&gt;T&lt;/i&gt;.&#xA;If so, the client can be cryptographically certain&#xA;that &lt;i&gt;B&lt;/i&gt; really is record 9 in the tree with top-level hash &lt;i&gt;T&lt;/i&gt;.&#xA;In effect, proving that &lt;i&gt;B&lt;/i&gt; is a record in the Merkle tree with hash &lt;i&gt;T&lt;/i&gt;&#xA;is done by giving a verifiable computation of &lt;i&gt;T&lt;/i&gt; with H(&lt;i&gt;B&lt;/i&gt;) as an input.&#xA;&#xA;&lt;p&gt;&#xA;Graphically, the proof consists of the sibling hashes (circled in blue)&#xA;of nodes along the path (highlighted in yellow)&#xA;from the record being proved up to the tree root.&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-r9-16&#34; class=&#34;center pad&#34; width=518 height=202 src=&#34;tlog-r9-16.png&#34; srcset=&#34;tlog-r9-16.png 1x, tlog-r9-16@1.5x.png 1.5x, tlog-r9-16@2x.png 2x, tlog-r9-16@3x.png 3x, tlog-r9-16@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In general, the proof that a given record is contained&#xA;in the tree requires lg &lt;i&gt;N&lt;/i&gt; hashes, one for each level&#xA;below the root.&#xA;&#xA;&lt;p&gt;&#xA;Building our log as a sequence of records&#xA;hashed in a Merkle tree would give us a way to write&#xA;an efficient (lg &lt;i&gt;N&lt;/i&gt;-length) proof that a particular record&#xA;is in the log.&#xA;But there are two related problems to solve:&#xA;our log needs to be defined for any length &lt;i&gt;N&lt;/i&gt;,&#xA;not just powers of two,&#xA;and we need to be able to write&#xA;an efficient proof that one log is a prefix of another.&#xA;&lt;a class=anchor href=&#34;#merkle_tree-structured_log&#34;&gt;&lt;h2 id=&#34;merkle_tree-structured_log&#34;&gt;A Merkle Tree-Structured Log&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;To generalize the Merkle tree&#xA;to non-power-of-two sizes, we can&#xA;write &lt;i&gt;N&lt;/i&gt; as a sum of decreasing powers of two,&#xA;then build complete Merkle trees of those sizes&#xA;for successive sections of the input,&#xA;and finally hash the at-most-lg &lt;i&gt;N&lt;/i&gt; complete trees together&#xA;to produce a single top-level hash.&#xA;For example, 13 = 8 + 4 + 1:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-13&#34; class=&#34;center pad&#34; width=434 height=193 src=&#34;tlog-13.png&#34; srcset=&#34;tlog-13.png 1x, tlog-13@1.5x.png 1.5x, tlog-13@2x.png 2x, tlog-13@3x.png 3x, tlog-13@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The new hashes marked “x” combine the complete trees,&#xA;building up from right to left, to produce the overall tree hash.&#xA;Note that these hashes necessarily combine trees of&#xA;different sizes and therefore hashes&#xA;from different levels;&#xA;for example, h(3,x) = H(h(2,2), h(0,12)).&#xA;&#xA;&lt;p&gt;&#xA;The proof strategy for complete Merkle trees applies&#xA;equally well to these incomplete trees.&#xA;For example, the proof that&#xA;record 9 is in the tree of size 13&#xA;is [h(3,0), h(0,8), h(1,5), h(0,12)]:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-r9-13&#34; class=&#34;center pad&#34; width=437 height=202 src=&#34;tlog-r9-13.png&#34; srcset=&#34;tlog-r9-13.png 1x, tlog-r9-13@1.5x.png 1.5x, tlog-r9-13@2x.png 2x, tlog-r9-13@3x.png 3x, tlog-r9-13@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Note that h(0,12) is included in the proof because&#xA;it is the sibling of h(2,2) in the computation of h(3,x).&#xA;&#xA;&lt;p&gt;&#xA;We still need to be able to write an efficient proof&#xA;that the log of size &lt;i&gt;N&lt;/i&gt; with tree hash &lt;i&gt;T&lt;/i&gt;&#xA;is a prefix of the log of size &lt;i&gt;N&lt;/i&gt; (&amp;gt; &lt;i&gt;N&lt;/i&gt;) with tree hash &lt;i&gt;T&lt;/i&gt;.&#xA;Earlier, proving that &lt;i&gt;B&lt;/i&gt; is a record in the Merkle tree with hash &lt;i&gt;T&lt;/i&gt;&#xA;was done by giving a verifiable computation of &lt;i&gt;T&lt;/i&gt; using H(&lt;i&gt;B&lt;/i&gt;) as an input.&#xA;To prove that the log with tree hash &lt;i&gt;T&lt;/i&gt;&#xA;is included in the log with tree hash &lt;i&gt;T&lt;/i&gt;,&#xA;we can follow the same idea:&#xA;give verifiable computations of &lt;i&gt;T&lt;/i&gt; and &lt;i&gt;T&lt;/i&gt;,&#xA;in which all the inputs to the computation of &lt;i&gt;T&lt;/i&gt;&#xA;are also inputs to the computation of &lt;i&gt;T&lt;/i&gt;.&#xA;For example, consider the trees of size 7 and 13:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-o7-13&#34; class=&#34;center pad&#34; width=437 height=193 src=&#34;tlog-o7-13.png&#34; srcset=&#34;tlog-o7-13.png 1x, tlog-o7-13@1.5x.png 1.5x, tlog-o7-13@2x.png 2x, tlog-o7-13@3x.png 3x, tlog-o7-13@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In the diagram, the “x” nodes complete the tree of size 13 with hash &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;,&#xA;while the “y” nodes complete the tree of size 7 with hash &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt;.&#xA;To prove that &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt;s leaves are included in &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;,&#xA;we first give the computation of &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt; in terms of complete subtrees&#xA;(circled in blue):&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt;&#x9;=&#x9;H(h(2,0), H(h(1,2), h(0,6)))&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Then we give the computation of &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;,&#xA;expanding hashes as needed to expose&#xA;the same subtrees.&#xA;Doing so exposes sibling subtrees (circled in red):&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;&#x9;=&#x9;H(h(3,0),&#x9;H(h(2,2), h(0,12)))&lt;br&gt;&#xA;&#x9;=&#x9;H(H(h(2,0), h(2,1)),&#x9;H(h(2,2), h(0,12)))&lt;br&gt;&#xA;&#x9;=&#x9;H(H(h(2,0), H(h(1,2), h(1,3))),&#x9;H(h(2,2), h(0,12)))&lt;br&gt;&#xA;&#x9;=&#x9;H(H(h(2,0), H(h(1,2), H(h(0,6), h(0,7)))),&#x9;H(h(2,2), h(0,12)))&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Assuming the client knows the trees have sizes 7 and 13,&#xA;it can derive the required decomposition itself.&#xA;We need only supply the hashes [h(2,0), h(1,2), h(0,6), h(0,7), h(2,2), h(0,12)].&#xA;The client recalculates the &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt; and &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;&#xA;implied by the hashes and checks that they match the originals.&#xA;&#xA;&lt;p&gt;&#xA;Note that these proofs only use hashes for completed subtrees—that is, numbered hashes,&#xA;never the “x” or “y” hashes that combine differently-sized subtrees.&#xA;The numbered hashes are &lt;i&gt;permanent&lt;/i&gt;,&#xA;in the sense that once such a hash appears in a&#xA;tree of a given size, that same hash will appear in all&#xA;trees of larger sizes.&#xA;In contrast, the “x” and “y” hashes are &lt;i&gt;ephemeral&lt;/i&gt;—computed&#xA;for a single tree and never seen again.&#xA;The hashes common to the decomposition of two different-sized trees&#xA;therefore must always be permanent hashes.&#xA;The decomposition of the larger tree could make use of ephemeral&#xA;hashes for the exposed siblings,&#xA;but we can easily use only&#xA;permanent hashes instead.&#xA;In the example above,&#xA;the reconstruction of &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;&#xA;from the parts of &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt;&#xA;uses h(2,2) and h(0,12)&#xA;instead of assuming access to &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;s h(3,x).&#xA;Avoiding the ephemeral hashes extends the maximum&#xA;record proof size from lg &lt;i&gt;N&lt;/i&gt; hashes to 2 lg &lt;i&gt;N&lt;/i&gt; hashes&#xA;and the maximum tree proof size from 2 lg &lt;i&gt;N&lt;/i&gt; hashes&#xA;to 3 lg &lt;i&gt;N&lt;/i&gt; hashes.&#xA;Note that most top-level hashes,&#xA;including &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;7&lt;/sub&gt; and &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;sub&gt;3&lt;/sub&gt;,&#xA;are themselves ephemeral hashes,&#xA;requiring up to lg &lt;i&gt;N&lt;/i&gt; permanent hashes to compute.&#xA;The exceptions are the power-of-two-sized trees&#xA;&lt;i&gt;T&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt;, &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;, &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;4&lt;/sub&gt;, &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;8&lt;/sub&gt;, and so on.&#xA;&lt;a class=anchor href=&#34;#storing_a_log&#34;&gt;&lt;h2 id=&#34;storing_a_log&#34;&gt;Storing a Log&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Storing the log requires only a few append-only files.&#xA;The first file holds the log record data, concatenated.&#xA;The second file is an index of the first,&#xA;holding a sequence of int64 values giving the start offset&#xA;of each record in the first file.&#xA;This index allows efficient random access to any record&#xA;by its record number.&#xA;While we could recompute any hash tree from the record data alone,&#xA;doing so would require &lt;i&gt;N&lt;/i&gt;1 hash operations&#xA;for a tree of size &lt;i&gt;N&lt;/i&gt;.&#xA;Efficient generation of proofs therefore requires&#xA;precomputing and storing the hash trees&#xA;in some more accessible form.&#xA;&#xA;&lt;p&gt;&#xA;As we noted in the previous section,&#xA;there is significant commonality between trees.&#xA;In particular, the latest hash tree includes all the&#xA;permanent hashes from all earlier hash trees,&#xA;so it is enough to store “only” the latest hash tree.&#xA;A straightforward way to do this is to&#xA;maintain lg &lt;i&gt;N&lt;/i&gt; append-only files, each holding&#xA;the sequence of hashes at one level of the tree.&#xA;Because hashes are fixed size,&#xA;any particular hash can be read efficiently&#xA;by reading from the file at the appropriate offset.&#xA;&#xA;&lt;p&gt;&#xA;To write a new log record, we must&#xA;append the record data to the data file,&#xA;append the offset of that data to the index file,&#xA;and&#xA;append the hash of the data to the level-0 hash file.&#xA;Then, if we completed a pair of hashes in the level-0 hash file,&#xA;we append the hash of the pair to the level-1 hash file;&#xA;if that completed a pair of hashes in the level-1 hash file,&#xA;we append the hash of that pair to the level-2 hash file;&#xA;and so on up the tree.&#xA;Each log record write will append a hash to at least one and&#xA;at most lg &lt;i&gt;N&lt;/i&gt; hash files,&#xA;with an average of just under two new hashes per write.&#xA;(A binary tree with &lt;i&gt;N&lt;/i&gt; leaves has &lt;i&gt;N&lt;/i&gt;1 interior nodes.)&#xA;&#xA;&lt;p&gt;&#xA;It is also possible to interlace lg &lt;i&gt;N&lt;/i&gt; append-only hash files&#xA;into a single append-only file,&#xA;so that the log can be stored in only three files:&#xA;record data, record index, and hashes.&#xA;See Appendix A for details.&#xA;Another possibility is to store the log in a pair of database tables,&#xA;one for record data and one for hashes&#xA;(the database can provide the record index itself).&#xA;&#xA;&lt;p&gt;&#xA;Whether in files or in database tables, the stored form&#xA;of the log is append-only, so cached data never goes stale,&#xA;making it trivial to have parallel, read-only replicas of a log.&#xA;In contrast, writing to the log is inherently centralized,&#xA;requiring a dense sequence numbering of all records&#xA;(and in many cases also duplicate suppression).&#xA;An implementation using the two-table database representation&#xA;can delegate both replication and coordination of writes&#xA;to the underlying database,&#xA;especially if the underlying database is globally-replicated and consistent,&#xA;like&#xA;&lt;a href=&#34;https://ai.google/research/pubs/pub39966&#34;&gt;Google Cloud Spanner&lt;/a&gt;&#xA;or &lt;a href=&#34;https://www.cockroachlabs.com/docs/stable/architecture/overview.html&#34;&gt;CockroachDB&lt;/a&gt;.&#xA;&#xA;&lt;p&gt;&#xA;It is of course not enough just to store the log.&#xA;We must also make it available to clients.&#xA;&lt;a class=anchor href=&#34;#serving_a_log&#34;&gt;&lt;h2 id=&#34;serving_a_log&#34;&gt;Serving a Log&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Remember that each client consuming the log is skeptical about the logs&#xA;correct operation.&#xA;The log server must make it easy for the client to&#xA;verify two things: first, that any particular record is in the log,&#xA;and second, that the current log is an append-only&#xA;extension of a previously-observed earlier log.&#xA;&#xA;&lt;p&gt;&#xA;To be useful, the log server must also make it easy to find a record&#xA;given some kind of lookup key,&#xA;and it must allow an auditor to iterate&#xA;over the entire log looking for entries that dont belong.&#xA;&#xA;&lt;p&gt;&#xA;To do all this, the log server must answer five queries:&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Latest&lt;/i&gt;() returns the current log size and top-level hash, cryptographically signed by the server for non-repudiation.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;RecordProof&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;) returns the proof that record &lt;i&gt;R&lt;/i&gt; is contained in the tree of size &lt;i&gt;N&lt;/i&gt;.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;TreeProof&lt;/i&gt;(&lt;i&gt;N&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;) returns the proof that the tree of size &lt;i&gt;N&lt;/i&gt; is a prefix of the tree of size &lt;i&gt;N&lt;/i&gt;.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Lookup&lt;/i&gt;(&lt;i&gt;K&lt;/i&gt;) returns the record index &lt;i&gt;R&lt;/i&gt; matching lookup key &lt;i&gt;K&lt;/i&gt;, if any.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Data&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;) returns the data associated with record &lt;i&gt;R&lt;/i&gt;.&lt;/ol&gt;&#xA;&lt;a class=anchor href=&#34;#verifying_a_log&#34;&gt;&lt;h2 id=&#34;verifying_a_log&#34;&gt;Verifying a Log&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The client uses the first three queries&#xA;to maintain a cached copy of the most recent log it has observed&#xA;and make sure that the server never removes anything&#xA;from an observed log.&#xA;To do this, the client caches the most recently observed&#xA;log size &lt;i&gt;N&lt;/i&gt; and top-level hash &lt;i&gt;T&lt;/i&gt;.&#xA;Then, before accepting data bits &lt;i&gt;B&lt;/i&gt; as record number &lt;i&gt;R&lt;/i&gt;,&#xA;the client verifies that &lt;i&gt;R&lt;/i&gt; is included in that log.&#xA;If necessary (that is, if &lt;i&gt;R&lt;/i&gt; ≥ its cached &lt;i&gt;N&lt;/i&gt;),&#xA;the client updates its cached &lt;i&gt;N&lt;/i&gt;, &lt;i&gt;T&lt;/i&gt;&#xA;to those of the latest log, but only after verifying&#xA;that the latest log includes everything from the current cached log.&#xA;In pseudocode:&#xA;&lt;pre&gt;validate(bits B as record R):&#xA; if R ≥ cached.N:&#xA; N, T = server.Latest()&#xA; if server.TreeProof(cached.N, N) cannot be verified:&#xA; fail loudly&#xA; cached.N, cached.T = N, T&#xA; if server.RecordProof(R, cached.N) cannot be verified using B:&#xA; fail loudly&#xA; accept B as record R&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The clients proof verification ensures that the log server is behaving&#xA;correctly, at least as observed by the client.&#xA;If a devious server can distinguish individual clients,&#xA;it can still serve different logs to different clients,&#xA;so that a victim client sees invalid entries&#xA;never exposed to other clients or auditors.&#xA;But if the server does lie to a victim,&#xA;the fact that the victim requires any later log to&#xA;include what it has seen before&#xA;means the server must keep up the lie,&#xA;forever serving an alternate log containing the lie.&#xA;This makes eventual detection more likely.&#xA;For example, if the victim ever arrived through a proxy&#xA;or compared its cached log against another client,&#xA;or if the server ever made a mistake about&#xA;which clients to lie to,&#xA;the inconsistency would be readily exposed.&#xA;Requiring the server to sign the &lt;i&gt;Latest&lt;/i&gt;() response&#xA;makes it impossible for the server to disavow the inconsistency,&#xA;except by claiming to have been compromised entirely.&#xA;&#xA;&lt;p&gt;&#xA;The client-side checks are a little bit like how&#xA;a Git client maintains its own&#xA;cached copy of a remote repository and then,&#xA;before accepting an update during &lt;code&gt;git&lt;/code&gt; &lt;code&gt;pull&lt;/code&gt;,&#xA;verifies that the remote repository includes all local commits.&#xA;But the transparent log client only needs to&#xA;download lg &lt;i&gt;N&lt;/i&gt; hashes for the verification,&#xA;while Git downloads all &lt;i&gt;cached&lt;/i&gt;.&lt;i&gt;N&lt;/i&gt; &lt;i&gt;N&lt;/i&gt; new data records,&#xA;and more generally, the transparent log client&#xA;can selectively read and authenticate individual&#xA;entries from the log,&#xA;without being required to download and store&#xA;a full copy of the entire log.&#xA;&lt;a class=anchor href=&#34;#tiling_a_log&#34;&gt;&lt;h2 id=&#34;tiling_a_log&#34;&gt;Tiling a Log&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;As described above,&#xA;storing the log requires simple, append-only storage&#xA;linear in the total log size,&#xA;and serving or accessing the log requires&#xA;network traffic only logarithmic in the total log size.&#xA;This would be a completely reasonable place to stop&#xA;(and is where Certificate Transparency as defined in&#xA;&lt;a href=&#34;https://tools.ietf.org/html/rfc6962&#34;&gt;RFC 6962&lt;/a&gt; stops).&#xA;However, one useful optimization can both cut the hash storage in half&#xA;and make the network traffic more cache-friendly,&#xA;with only a minor increase in implementation complexity.&#xA;That optimization is based on splitting the hash tree into tiles,&#xA;like&#xA;&lt;a href=&#34;https://medium.com/google-design/google-maps-cb0326d165f5#ccfa&#34;&gt;Google Maps splits the globe into tiles&lt;/a&gt;.&#xA;&#xA;&lt;p&gt;&#xA;A binary tree can be split into tiles of fixed height &lt;i&gt;H&lt;/i&gt;&#xA;and width 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt;.&#xA;For example, here is the permanent hash tree for the log with 27 records,&#xA;split into tiles of height 2:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-tile-27&#34; class=&#34;center pad&#34; width=847 height=236 src=&#34;tlog-tile-27.png&#34; srcset=&#34;tlog-tile-27.png 1x, tlog-tile-27@1.5x.png 1.5x, tlog-tile-27@2x.png 2x, tlog-tile-27@3x.png 3x, tlog-tile-27@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;We can assign each tile a two-dimensional coordinate, analogous to the hash coordinates weve been using:&#xA;tile(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) denotes the tile at tile level &lt;i&gt;L&lt;/i&gt;&#xA;(hash levels &lt;i&gt;H&lt;/i&gt;·&lt;i&gt;L&lt;/i&gt; up to &lt;i&gt;H&lt;/i&gt;·(&lt;i&gt;L&lt;/i&gt;+1)), &lt;i&gt;K&lt;/i&gt;th from the left.&#xA;For any given log size, the rightmost tile at each level&#xA;may not yet be complete:&#xA;the bottom row of hashes may contain only &lt;i&gt;W&lt;/i&gt; &amp;lt; 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; hashes.&#xA;In that case we will write tile(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;)/&lt;i&gt;W&lt;/i&gt;.&#xA;(When the tile is complete, the “/&lt;i&gt;W&lt;/i&gt;” is omitted, understood to be 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt;.)&#xA;&lt;a class=anchor href=&#34;#storing_tiles&#34;&gt;&lt;h2 id=&#34;storing_tiles&#34;&gt;Storing Tiles&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Only the bottom row of each tile needs to be stored:&#xA;the upper rows can be recomputed by hashing lower ones.&#xA;In our example, a tile of height two stores 4 hashes instead of 6,&#xA;a 33% storage reduction.&#xA;For tiles of greater heights,&#xA;the storage reduction asymptotically approaches 50%.&#xA;The cost is that reading a hash that has been optimized&#xA;away may require reading as much as half a tile,&#xA;increasing I/O requirements.&#xA;For a real system, height four seems like a reasonable balance between storage costs and increased I/O overhead.&#xA;It stores 16 hashes instead of 30—a 47% storage reduction—and&#xA;(assuming SHA-256) a single 16-hash tile is only 512 bytes&#xA;(a single disk sector!).&#xA;&#xA;&lt;p&gt;&#xA;The file storage described earlier maintained lg &lt;i&gt;N&lt;/i&gt; hash files,&#xA;one for each level.&#xA;Using tiled storage,&#xA;we only write the hash files&#xA;for levels that are a multiple of the tile height.&#xA;For tiles of height 4, wed only write the hash files&#xA;for levels 0, 4, 8, 12, 16, and so on.&#xA;When we need a hash at another level,&#xA;we can read its tile and recompute the hash.&#xA;&lt;a class=anchor href=&#34;#serving_tiles&#34;&gt;&lt;h2 id=&#34;serving_tiles&#34;&gt;Serving Tiles&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The proof-serving requests&#xA;&lt;i&gt;RecordProof&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;) and&#xA;&lt;i&gt;TreeProof&lt;/i&gt;(&lt;i&gt;N&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;)&#xA;are not particularly cache-friendly.&#xA;For example, although &lt;i&gt;RecordProof&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;)&#xA;often shares many hashes&#xA;with both &lt;i&gt;RecordProof&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;+1, &lt;i&gt;N&lt;/i&gt;)&#xA;and &lt;i&gt;RecordProof&lt;/i&gt;(&lt;i&gt;R&lt;/i&gt;, &lt;i&gt;N&lt;/i&gt;+1),&#xA;the three are distinct requests that must be cached independently.&#xA;&#xA;&lt;p&gt;&#xA;A more cache-friendly approach would be&#xA;to replace &lt;i&gt;RecordProof&lt;/i&gt; and &lt;i&gt;TreeProof&lt;/i&gt; by a general&#xA;request &lt;i&gt;Hash&lt;/i&gt;(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;),&#xA;serving a single permanent hash.&#xA;The client can easily compute which specific hashes it needs,&#xA;and there are many fewer individual hashes&#xA;than whole proofs (2&lt;i&gt;N&lt;/i&gt; vs &lt;i&gt;N&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;/2),&#xA;which will help the cache hit rate.&#xA;Unfortunately, switching to &lt;i&gt;Hash&lt;/i&gt; requests is inefficient:&#xA;obtaining a record proof used to take one request&#xA;and now takes up to 2 lg &lt;i&gt;N&lt;/i&gt; requests, while&#xA;tree proofs take up to 3 lg &lt;i&gt;N&lt;/i&gt; requests.&#xA;Also, each request delivers only a single hash (32 bytes):&#xA;the request overhead is likely significantly larger&#xA;than the payload.&#xA;&#xA;&lt;p&gt;&#xA;We can stay cache-friendly&#xA;while reducing the number of requests&#xA;and the relative request overhead,&#xA;at a small cost in bandwidth,&#xA;by adding a request &lt;i&gt;Tile&lt;/i&gt;(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;)&#xA;that returns the requested tile.&#xA;The client can request the tiles it needs for a given proof,&#xA;and it can cache tiles, especially those higher in the tree,&#xA;for use in future proofs.&#xA;&#xA;&lt;p&gt;&#xA;For a real system using SHA-256, a tile of height 8 would be 8 kB.&#xA;A typical proof in a large log of, say, 100 million records would&#xA;require only three complete tiles, or 24 kB downloaded,&#xA;plus one incomplete tile (192 bytes) for the top of the tree.&#xA;And tiles of height 8 can be served directly from&#xA;stored tiles of height 4 (the size suggested in the previous section).&#xA;Another reasonable choice would be to both store and serve tiles of height 6 (2 kB each) or 7 (4 kB each).&#xA;&#xA;&lt;p&gt;&#xA;If there are caches in front of the server,&#xA;each differently-sized partial tile must be given&#xA;a different name,&#xA;so that a client that needs a larger partial tile&#xA;is not given a stale smaller one.&#xA;Even though the tile height is conceptually constant for a given system,&#xA;it is probably helpful to be explicit about the tile height&#xA;in the request, so that a system can transition from one&#xA;fixed tile height to another without ambiguity.&#xA;For example, in a simple GET-based HTTP API,&#xA;we could use &lt;code&gt;/tile/H/L/K&lt;/code&gt; to name a complete tile&#xA;and &lt;code&gt;/tile/H/L/K.W&lt;/code&gt; to name a partial tile with only&#xA;&lt;i&gt;W&lt;/i&gt; hashes.&#xA;&lt;a class=anchor href=&#34;#authenticating_tiles&#34;&gt;&lt;h2 id=&#34;authenticating_tiles&#34;&gt;Authenticating Tiles&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;One potential problem with downloading and caching tiles&#xA;is not being sure that they are correct.&#xA;An attacker might be able to modify downloaded tiles&#xA;and cause proofs to fail unexpectedly.&#xA;We can avoid this problem by authenticating the tiles&#xA;against the signed top-level tree hash after downloading them.&#xA;Specifically, if we have a signed top-level tree hash &lt;i&gt;T&lt;/i&gt;,&#xA;we first download the at most (lg &lt;i&gt;N&lt;/i&gt;)/&lt;i&gt;H&lt;/i&gt; tiles storing&#xA;the hashes for the complete subtrees that make up &lt;i&gt;T&lt;/i&gt;.&#xA;In the diagram of &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;sub&gt;7&lt;/sub&gt; earlier, that would be tile(2,0)/1,&#xA;tile(1,1)/2, and tile(0,6)/3.&#xA;Computing &lt;i&gt;T&lt;/i&gt; will use every hash in these tiles;&#xA;if we get the right &lt;i&gt;T&lt;/i&gt;, the hashes are all correct.&#xA;These tiles make up the top and right sides&#xA;of the tile tree for the given hash tree, and now we know they are correct.&#xA;To authenticate any other tile,&#xA;we first authenticate its parent tile&#xA;(the topmost parents are all authenticated already)&#xA;and then check that the result of hashing all the hashes&#xA;in the tile produces the corresponding entry in the parent tile.&#xA;Using the &lt;i&gt;T&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt;&lt;sub&gt;7&lt;/sub&gt; example again,&#xA;given a downloaded tile purporting to be tile(0,1), we can compute&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;h(2,1) = H(H(h(0,4), h(0,5)), H(h(0,6), h(0,7)))&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;and check whether that value matches the h(2,1)&#xA;recorded directly in an already-authenticated tile(1,0).&#xA;If so, that authenticates the downloaded tile.&#xA;&lt;a class=anchor href=&#34;#summary&#34;&gt;&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Putting this all together, weve seen how to publish a&#xA;transparent (tamper-evident, immutable, append-only) log&#xA;with the following properties:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;A client can verify any particular record using &lt;i&gt;O&lt;/i&gt;(lg &lt;i&gt;N&lt;/i&gt;) downloaded bytes.&#xA;&lt;li&gt;&#xA;A client can verify any new log contains an older log using &lt;i&gt;O&lt;/i&gt;(lg &lt;i&gt;N&lt;/i&gt;) downloaded bytes.&#xA;&lt;li&gt;&#xA;For even a large log, these verifications can be done in 3 RPCs of about 8 kB each.&#xA;&lt;li&gt;&#xA;The RPCs used for verification can be made to proxy and cache well, whether for network efficiency or possibly for privacy.&#xA;&lt;li&gt;&#xA;Auditors can iterate over the entire log looking for bad entries.&#xA;&lt;li&gt;&#xA;Writing &lt;i&gt;N&lt;/i&gt; records defines a sequence of &lt;i&gt;N&lt;/i&gt; hash trees, in which the &lt;i&gt;n&lt;/i&gt;th tree contains 2&lt;i&gt;n&lt;/i&gt;1 hashes, a total of &lt;i&gt;N&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; hashes. But instead of needing to store &lt;i&gt;N&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt; hashes, the entire sequence can be compacted into at most 2&lt;i&gt;N&lt;/i&gt; hashes, with at most lg &lt;i&gt;N&lt;/i&gt; reads required to reconstruct a specific hash from a specific tree.&#xA;&lt;li&gt;&#xA;Those 2&lt;i&gt;N&lt;/i&gt; hashes can themselves be compacted down to 1.06&lt;i&gt;N&lt;/i&gt; hashes, at a cost of potentially reading 8 adjacent hashes to reconstruct any one hash from the 2&lt;i&gt;N&lt;/i&gt;.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Overall, this structure makes the log server itself essentially untrusted.&#xA;It cant remove an observed record without detection.&#xA;It cant lie to one client without keeping the client on an alternate timeline forever,&#xA;making detection easy by comparing against another client.&#xA;The log itself is also easily proxied and cached,&#xA;so that even if the main server disappeared,&#xA;replicas could keep serving the cached log.&#xA;Finally, auditors can check the log for entries that should not be there,&#xA;so that the actual content of the log can be verified asynchronously&#xA;from its use.&#xA;&lt;a class=anchor href=&#34;#further_reading&#34;&gt;&lt;h2 id=&#34;further_reading&#34;&gt;Further Reading&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The original sources needed to understand this data structure are&#xA;all quite readable and repay careful study.&#xA;Ralph Merkle introduced Merkle trees in his&#xA;Ph.D. thesis,&#xA;“&lt;a href=&#34;http://www.merkle.com/papers/Thesis1979.pdf&#34;&gt;Secrecy, authentication, and public-key systems&lt;/a&gt;” (1979),&#xA;using them to convert a digital signature scheme&#xA;with single-use public keys into one with multiple-use keys.&#xA;The multiple-use key was the top-level hash of a tree of 2&lt;sup&gt;&lt;i&gt;L&lt;/i&gt;&lt;/sup&gt; pseudorandomly&#xA;generated single-use keys.&#xA;Each signature began with a specific single-use key,&#xA;its index &lt;i&gt;K&lt;/i&gt; in the tree,&#xA;and a proof (consisting of &lt;i&gt;L&lt;/i&gt; hashes)&#xA;authenticating the key as record &lt;i&gt;K&lt;/i&gt; in the tree.&#xA;Adam Langleys blog post&#xA;“&lt;a href=&#34;https://www.imperialviolet.org/2013/07/18/hashsig.html&#34;&gt;Hash based signatures&lt;/a&gt;” (2013)&#xA;gives a short introduction to&#xA;the single-use signature scheme and&#xA;how Merkles tree helped.&#xA;&#xA;&lt;p&gt;&#xA;Scott Crosby and Dan Wallach&#xA;introduced the idea of using a Merkle tree to store a verifiably append-only log&#xA;in their paper,&#xA;“&lt;a href=&#34;http://static.usenix.org/event/sec09/tech/full_papers/crosby.pdf&#34;&gt;Efficient Data Structures for Tamper-Evident Logging&lt;/a&gt;” (2009).&#xA;The key advance was the efficient proof&#xA;that one trees log is contained as a prefix of a larger trees log.&#xA;&#xA;&lt;p&gt;&#xA;Ben Laurie, Adam Langley, and Emilia Kasper&#xA;adopted this verifiable, transparent log&#xA;in the design for&#xA;&lt;a href=&#34;https://www.certificate-transparency.org/&#34;&gt;Certificate Transparency (CT) system&lt;/a&gt; (2012),&#xA;detailed in&#xA;&lt;a href=&#34;https://tools.ietf.org/html/rfc6962&#34;&gt;RFC 6962&lt;/a&gt; (2013).&#xA;CTs computation of the top-level hashes&#xA;for non-power-of-two-sized logs differs&#xA;in minor ways from Crosby and Wallachs paper;&#xA;this post used the CT definitions.&#xA;Ben Laurie&#39;s ACM Queue article, “&lt;a href=&#34;https://queue.acm.org/detail.cfm?id=2668154&#34;&gt;Certificate Transparency: Public, verifiable, append-only logs&lt;/a&gt;” (2014),&#xA;presents a high-level overview and additional motivation and context.&#xA;&#xA;&lt;p&gt;&#xA;Adam Eijdenberg, Ben Laurie, and Al Cutters paper&#xA;“&lt;a href=&#34;https://github.com/google/trillian/blob/master/docs/papers/VerifiableDataStructures.pdf&#34;&gt;Verifiable Data Structures&lt;/a&gt;” (2015),&#xA;presents Certificate Transparencys log&#xA;as a general building block—a transparent log—for use in a variety of systems.&#xA;It also introduces an analogous transparent map&#xA;from arbitrary keys to arbitrary values,&#xA;perhaps a topic for a future post.&#xA;&#xA;&lt;p&gt;&#xA;Googles “General Transparency” server, &lt;a href=&#34;https://github.com/google/trillian/blob/master/README.md&#34;&gt;Trillian&lt;/a&gt;,&#xA;is a production-quality storage implementation&#xA;for both transparent logs and transparent maps.&#xA;The RPC service serves proofs, not hashes or tiles,&#xA;but the server &lt;a href=&#34;https://github.com/google/trillian/blob/master/docs/storage/storage.md&#34;&gt;uses tiles in its internal storage&lt;/a&gt;.&#xA;&#xA;&lt;p&gt;&#xA;To authenticate modules (software packages)&#xA;in the Go language ecosystem, we are&#xA;&lt;a href=&#34;https://blog.golang.org/modules2019&#34;&gt;planning to use a transparent log&lt;/a&gt;&#xA;to store the expected cryptographic hashes of specific module versions,&#xA;so that a client can be cryptographically certain&#xA;that it will download the same software tomorrow&#xA;that it downloaded today.&#xA;For that systems network service,&#xA;we plan to serve tiles directly, not proofs.&#xA;This post effectively serves as an extended explanation&#xA;of the transparent log, for reference from&#xA;&lt;a href=&#34;https://golang.org/design/25530-notary&#34;&gt;the Go-specific design&lt;/a&gt;.&#xA;&lt;a class=anchor href=&#34;#appendix_a&#34;&gt;&lt;h2 id=&#34;appendix_a&#34;&gt;Appendix A: Postorder Storage Layout&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The file-based storage described earlier held the permanent hash tree&#xA;in lg &lt;i&gt;N&lt;/i&gt; append-only files, one for each level of the tree.&#xA;The hash h(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) would be stored in the &lt;i&gt;L&lt;/i&gt;th hash file&#xA;at offset &lt;i&gt;K&lt;/i&gt; · &lt;i&gt;HashSize&lt;/i&gt;&#xA;&#xA;&lt;p&gt;&#xA;Crosby and Wallach pointed out that it is easy to merge the lg &lt;i&gt;N&lt;/i&gt; hash tree levels&#xA;into a single, append-only hash file by using the postorder numbering of&#xA;the binary tree, in which a parent hash is stored immediately after its&#xA;rightmost child.&#xA;For example, the permanent hash tree after writing &lt;i&gt;N&lt;/i&gt; = 13 records is laid out like:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-post-13&#34; class=&#34;center pad&#34; width=560 height=157 src=&#34;tlog-post-13.png&#34; srcset=&#34;tlog-post-13.png 1x, tlog-post-13@1.5x.png 1.5x, tlog-post-13@2x.png 2x, tlog-post-13@3x.png 3x, tlog-post-13@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In the diagram, each hash is numbered&#xA;and aligned horizontally according to its location in the interlaced file.&#xA;&#xA;&lt;p&gt;&#xA;The postorder numbering makes the hash file append-only:&#xA;each new record completes between 1 and lg &lt;i&gt;N&lt;/i&gt; new hashes&#xA;(on average 2),&#xA;which are simply appended to the file,&#xA;lower levels first.&#xA;&#xA;&lt;p&gt;&#xA;Reading a specific hash from the file can still be done with a single read&#xA;at a computable offset, but the calculation is no longer completely trivial.&#xA;Hashes at level 0 are placed by adding in gaps for&#xA;completed higher-level hashes,&#xA;and a hash at any higher level immediately&#xA;follows its right child hash:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(0, &lt;i&gt;K&lt;/i&gt;) = &lt;i&gt;K&lt;/i&gt; + &lt;i&gt;K&lt;/i&gt;/2 + &lt;i&gt;K&lt;/i&gt;/4 + &lt;i&gt;K&lt;/i&gt;/8 + ... &lt;br&gt;&#xA;seq(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) = seq(&lt;i&gt;L&lt;/i&gt;1, 2&lt;i&gt;K&lt;/i&gt; + 1) + 1 = seq(0, 2&lt;sup&gt;&lt;i&gt;L&lt;/i&gt;&lt;/sup&gt;(&lt;i&gt;K&lt;/i&gt;+1) 1) + &lt;i&gt;L&lt;/i&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;The interlaced layout also improves locality of access.&#xA;Reading a proof typically means reading one hash&#xA;from each level,&#xA;all clustered around a particular leaf in the tree.&#xA;If each tree level is stored separately,&#xA;each hash is in a different file and there is no possibility of I/O overlap.&#xA;But when the tree is stored in interlaced form,&#xA;the accesses at the bottom levels will all be near each other,&#xA;making it possible to fetch many of the needed hashes&#xA;with a single disk read.&#xA;&lt;a class=anchor href=&#34;#appendix_b&#34;&gt;&lt;h2 id=&#34;appendix_b&#34;&gt;Appendix B: Inorder Storage Layout&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A different way to interlace the lg &lt;i&gt;N&lt;/i&gt; hash files&#xA;would be to use an inorder tree numbering,&#xA;in which each parent hash is stored between its left&#xA;and right subtrees:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-in-13&#34; class=&#34;center pad&#34; width=602 height=157 src=&#34;tlog-in-13.png&#34; srcset=&#34;tlog-in-13.png 1x, tlog-in-13@1.5x.png 1.5x, tlog-in-13@2x.png 2x, tlog-in-13@3x.png 3x, tlog-in-13@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;This storage order does not correspond to append-only writes to the file,&#xA;but each hash entry is still write-once.&#xA;For example, with 13 records written, as in the diagram,&#xA;hashes have been stored at indexes 014, 1622 and 24,&#xA;but not yet at indexes 15 and 23,&#xA;which will eventually hold&#xA;h(4,0) and h(3,1).&#xA;In effect, the space for a parent hash is reserved&#xA;when its left subtree has been completed,&#xA;but it can only be filled in later, once its right subtree has also been completed.&#xA;&#xA;&lt;p&gt;&#xA;Although the file is no longer append-only, the inorder numbering&#xA;has other useful properties.&#xA;First, the offset math is simpler:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(0, &lt;i&gt;K&lt;/i&gt;) = 2&lt;i&gt;K&lt;/i&gt; &lt;br&gt;&#xA;seq(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) = 2&lt;sup&gt;&lt;i&gt;L&lt;/i&gt;+1&lt;/sup&gt;&lt;i&gt;K&lt;/i&gt; + 2&lt;sup&gt;&lt;i&gt;L&lt;/i&gt;&lt;/sup&gt; 1&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Second, locality is improved.&#xA;Now each parent hash sits exactly in the middle of its&#xA;child subtrees,&#xA;instead of on the far right side.&#xA;&lt;a class=anchor href=&#34;#appendix_c&#34;&gt;&lt;h2 id=&#34;appendix_c&#34;&gt;Appendix C: Tile Storage Layout&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Storing the hash tree in lg &lt;i&gt;N&lt;/i&gt; separate levels made&#xA;converting to tile storage very simple: just dont write (&lt;i&gt;H&lt;/i&gt;1)/&lt;i&gt;H&lt;/i&gt; of the files.&#xA;The simplest tile implementation is probably to use separate files,&#xA;but it is worth examining what it would take to convert an&#xA;interlaced hash storage file to tile storage.&#xA;Its not as straightforward as omitting a few files.&#xA;Its not enough to just omit the hashes at certain levels:&#xA;we also want each tile to appear contiguously in the file.&#xA;For example, for tiles of height 2,&#xA;the first tile at tile level 1 stores hashes h(2,0)h(2,3),&#xA;but neither the postorder nor inorder interlacing&#xA;would place those four hashes next to each other.&#xA;&#xA;&lt;p&gt;&#xA;Instead, we must simply define that tiles are stored contiguously&#xA;and then decide a linear tile layout order.&#xA;For tiles of height 2, the tiles form a 4-ary tree,&#xA;and in general, the tiles form a 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt;-ary tree.&#xA;We could use a postorder layout, as in Appendix A:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(0, &lt;i&gt;K&lt;/i&gt;) = &lt;i&gt;K&lt;/i&gt; + &lt;i&gt;K&lt;/i&gt;/2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; + &lt;i&gt;K&lt;/i&gt;/2&lt;sup&gt;2&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; + &lt;i&gt;K&lt;/i&gt;/2&lt;sup&gt;3&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; + ... &lt;br&gt;&#xA;seq(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) = seq(&lt;i&gt;L&lt;/i&gt;1, 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt;&lt;i&gt;K&lt;/i&gt; + 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; 1) + 1 = seq(0, 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;·&lt;i&gt;L&lt;/i&gt;&lt;/sup&gt;(&lt;i&gt;K&lt;/i&gt;+1) 1) + &lt;i&gt;L&lt;/i&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;The postorder tile sequence places a parent tile&#xA;immediately after its rightmost child tile,&#xA;but the parent tile begins to be written&#xA;after the leftmost child tile is completed.&#xA;This means writing increasingly far ahead of the&#xA;filled part of the hash file.&#xA;For example, with tiles of height 2,&#xA;the first hash of&#xA;tile(2,0) (postorder index 20) is written after filling tile(1,0) (postorder index 4):&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-tile-post-16&#34; class=&#34;center pad&#34; width=498 height=126 src=&#34;tlog-tile-post-16.png&#34; srcset=&#34;tlog-tile-post-16.png 1x, tlog-tile-post-16@1.5x.png 1.5x, tlog-tile-post-16@2x.png 2x, tlog-tile-post-16@3x.png 3x, tlog-tile-post-16@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The hash file catches up—there are no tiles written after index 20 until the hash file fills in entirely behind it—but&#xA;then jumps ahead again—finishing tile 20 triggers writing the first hash into tile 84.&#xA;In general only the first 1/2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt; or so of the hash file is guaranteed to be densely packed.&#xA;Most file systems efficiently support files with large holes,&#xA;but not all do:&#xA;we may want to use a different tile layout to avoid arbitrarily large holes.&#xA;&#xA;&lt;p&gt;&#xA;Placing a parent tile immediately after its leftmost childs completed subtree&#xA;would eliminate all holes (other than incomplete tiles) and would seem to correspond to&#xA;the inorder layout of Appendix B:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-tile-in1-16&#34; class=&#34;center pad&#34; width=498 height=126 src=&#34;tlog-tile-in1-16.png&#34; srcset=&#34;tlog-tile-in1-16.png 1x, tlog-tile-in1-16@1.5x.png 1.5x, tlog-tile-in1-16@2x.png 2x, tlog-tile-in1-16@3x.png 3x, tlog-tile-in1-16@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;But while the tree structure is regular,&#xA;the numbering is not.&#xA;Instead, the offset math is more like the postorder traversal.&#xA;A simpler but far less obvious alternative is to vary the exact&#xA;placement of the parent tiles relative to the subtrees:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;tlog-tile-code-16&#34; class=&#34;center pad&#34; width=498 height=126 src=&#34;tlog-tile-code-16.png&#34; srcset=&#34;tlog-tile-code-16.png 1x, tlog-tile-code-16@1.5x.png 1.5x, tlog-tile-code-16@2x.png 2x, tlog-tile-code-16@3x.png 3x, tlog-tile-code-16@4x.png 4x&#34;&gt;&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(&lt;i&gt;L&lt;/i&gt;, &lt;i&gt;K&lt;/i&gt;) = ((&lt;i&gt;K&lt;/i&gt; + &lt;i&gt;B&lt;/i&gt; 2)/(&lt;i&gt;B&lt;/i&gt; 1))&lt;sub&gt;&lt;i&gt;B&lt;/i&gt;&lt;/sub&gt; || (1)&lt;sub&gt;&lt;i&gt;B&lt;/i&gt;&lt;/sub&gt;&lt;sup&gt;L&lt;/sup&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Here, (&lt;i&gt;X&lt;/i&gt;)&lt;sub&gt;&lt;i&gt;B&lt;/i&gt;&lt;/sub&gt; means &lt;i&gt;X&lt;/i&gt; written as a base-&lt;i&gt;B&lt;/i&gt; number,&#xA;|| denotes concatenation of base-&lt;i&gt;B&lt;/i&gt; numbers,&#xA;(1)&lt;sub&gt;&lt;i&gt;B&lt;/i&gt;&lt;/sub&gt;&lt;sup&gt;L&lt;/sup&gt;&#xA;means the base-&lt;i&gt;B&lt;/i&gt; digit 1 repeated &lt;i&gt;L&lt;/i&gt; times,&#xA;and the base is &lt;i&gt;B&lt;/i&gt; = 2&lt;sup&gt;&lt;i&gt;H&lt;/i&gt;&lt;/sup&gt;.&#xA;&#xA;&lt;p&gt;&#xA;This encoding generalizes the inorder binary-tree traversal (&lt;i&gt;H&lt;/i&gt; = 1, &lt;i&gt;B&lt;/i&gt; = 2),&#xA;preserving its regular offset math&#xA;at the cost of losing its regular tree structure.&#xA;Since we only care about doing the math,&#xA;not exactly what the tree looks like,&#xA;this is probably a reasonable tradeoff.&#xA;For more about this surprising ordering,&#xA;see my blog post,&#xA;“&lt;a href=&#34;https://research.swtch.com/treenum&#34;&gt;An Encoded Tree Traversal&lt;/a&gt;.”&#xA;</content>
</entry>
<entry>
<title>An Encoded Tree Traversal</title>
<id>tag:research.swtch.com,2012:research.swtch.com/treenum</id>
<link rel="alternate" href="http://research.swtch.com/treenum"></link>
<published>2019-02-25T12:00:00-05:00</published>
<updated>2019-02-25T12:02:00-05:00</updated>
<summary type="text">An unexpected tree traversal ordering.</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;Every basic data structures course identifies three ways to traverse a binary tree.&#xA;Its not entirely clear how to generalize them to &lt;i&gt;k&lt;/i&gt;-ary trees,&#xA;and I recently noticed an unexpected ordering that Id like to know more about.&#xA;If you know of references to this ordering, please leave a comment&#xA;or email me (&lt;i&gt;rsc@swtch.com&lt;/i&gt;).&#xA;&lt;a class=anchor href=&#34;#binary_tree_orderings&#34;&gt;&lt;h2 id=&#34;binary_tree_orderings&#34;&gt;Binary Tree Orderings&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;First a refresher about binary-tree orderings&#xA;to set up an analogy to &lt;i&gt;k&lt;/i&gt;-ary trees.&#xA;&#xA;&lt;p&gt;&#xA;Preorder visits a node before its left and right subtrees:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b2-pre&#34; class=&#34;center pad&#34; width=625 height=138 src=&#34;treenum-b2-pre.png&#34; srcset=&#34;treenum-b2-pre.png 1x, treenum-b2-pre@1.5x.png 1.5x, treenum-b2-pre@2x.png 2x, treenum-b2-pre@3x.png 3x, treenum-b2-pre@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Inorder visits a node between its left and right subtrees:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b2-in&#34; class=&#34;center pad&#34; width=625 height=138 src=&#34;treenum-b2-in.png&#34; srcset=&#34;treenum-b2-in.png 1x, treenum-b2-in@1.5x.png 1.5x, treenum-b2-in@2x.png 2x, treenum-b2-in@3x.png 3x, treenum-b2-in@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Postorder visits a node after its left and right subtrees:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b2-post&#34; class=&#34;center pad&#34; width=625 height=138 src=&#34;treenum-b2-post.png&#34; srcset=&#34;treenum-b2-post.png 1x, treenum-b2-post@1.5x.png 1.5x, treenum-b2-post@2x.png 2x, treenum-b2-post@3x.png 3x, treenum-b2-post@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Each picture shows the same 16-leaf, 31-node binary tree, with the nodes&#xA;numbered and also placed horizontally using the order&#xA;visited in the given traversal.&#xA;&#xA;&lt;p&gt;&#xA;It was observed long ago that one way to represent a tree&#xA;in linear storage is to record the nodes in a fixed order&#xA;(such as one of these), along with a separate array giving&#xA;the number of children of each node.&#xA;In the pictures, the trees are complete, balanced trees, so the&#xA;number of children of each node can be derived from&#xA;the number of total leaves.&#xA;(For more, see Knuth Volume 1 §2.3.1;&#xA;for references, see §2.3.1.6, and §2.6.)&#xA;&#xA;&lt;p&gt;&#xA;It is convenient to refer to nodes in a tree by&#xA;a two-dimensional coordinate (&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;), consisting of the level of&#xA;the node (with 0 being the leaves) and its sequence number at that level.&#xA;For example, the root of the 16-node tree has coordinate (4, 0),&#xA;while the leaves are (0, 0) through (0, 15).&#xA;&#xA;&lt;p&gt;&#xA;When storing a tree using a linearized ordering such as these,&#xA;it is often necessary to be able to convert a two-dimensional&#xA;coordinate to its index in the linear ordering.&#xA;For example,&#xA;the right child of the root—node (3, 1)—has&#xA;number 16, 23, and 29&#xA;in the three different orderings.&#xA;&#xA;&lt;p&gt;&#xA;The linearized pre-ordering of (&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) is given by:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(&lt;i&gt;L&lt;/i&gt;, 0) = 0 (&lt;i&gt;L&lt;/i&gt; is height of tree)&lt;br&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = seq(&lt;i&gt;l&lt;/i&gt;+1, &lt;i&gt;n&lt;/i&gt;/2) + 1 (&lt;i&gt;n&lt;/i&gt; even)&lt;br&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = seq(&lt;i&gt;l&lt;/i&gt;+1, &lt;i&gt;n&lt;/i&gt;/2) + 2&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;+1&lt;/sup&gt; (&lt;i&gt;n&lt;/i&gt; odd)&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;This ordering is awkward because it changes depending on the height of the tree.&#xA;&#xA;&lt;p&gt;&#xA;The linearized post-ordering of (&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) is given by:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(0, &lt;i&gt;n&lt;/i&gt;) = &lt;i&gt;n&lt;/i&gt; + &lt;i&gt;n&lt;/i&gt;/2 + &lt;i&gt;n&lt;/i&gt;/4 + &lt;i&gt;n&lt;/i&gt;/8 + ...&lt;br&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = seq(&lt;i&gt;l&lt;/i&gt;1, 2 &lt;i&gt;n&lt;/i&gt; + 1) + 1 = seq(0, 2&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt; &lt;i&gt;n&lt;/i&gt; + 2&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt; 1) + &lt;i&gt;l&lt;/i&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;This ordering is independent of the height of the tree,&#xA;but the leaf numbering is still a little complex.&#xA;&#xA;&lt;p&gt;&#xA;The linearized in-ordering is much more regular.&#xA;Its clear just looking at it that seq(0, &lt;i&gt;n&lt;/i&gt;) = 2 &lt;i&gt;n&lt;/i&gt;,&#xA;and in fact a single equation applies to all levels:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = 2&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;+1&lt;/sup&gt; &lt;i&gt;n&lt;/i&gt; + 2&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt; 1&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;If you need to linearize a complete binary tree,&#xA;using the in-order traversal has the simplest math.&#xA;&lt;a class=anchor href=&#34;#k-ary_tree_orderings&#34;&gt;&lt;h2 id=&#34;k-ary_tree_orderings&#34;&gt;&lt;i&gt;k&lt;/i&gt;-ary Tree Orderings&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The three binary orderings&#xA;correspond to visiting the node after 0, 1, or 2 of&#xA;its child subtrees.&#xA;To generalize to &lt;i&gt;k&lt;/i&gt;-ary trees,&#xA;we can visit the node after any number&#xA;of subtrees from 0 to &lt;i&gt;k&lt;/i&gt;,&#xA;producing &lt;i&gt;k&lt;/i&gt;+1 orderings.&#xA;For example, for &lt;i&gt;k&lt;/i&gt; = 3,&#xA;here are the four generalized orderings&#xA;of a 27-leaf, 39-node 3-ary tree:&#xA;&#xA;&lt;p&gt;&#xA;Preorder (inorder-0):&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-pre&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-pre.png&#34; srcset=&#34;treenum-b3-pre.png 1x, treenum-b3-pre@1.5x.png 1.5x, treenum-b3-pre@2x.png 2x, treenum-b3-pre@3x.png 3x, treenum-b3-pre@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Inorder-1:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-in1&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-in1.png&#34; srcset=&#34;treenum-b3-in1.png 1x, treenum-b3-in1@1.5x.png 1.5x, treenum-b3-in1@2x.png 2x, treenum-b3-in1@3x.png 3x, treenum-b3-in1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Inorder-2:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-in2&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-in2.png&#34; srcset=&#34;treenum-b3-in2.png 1x, treenum-b3-in2@1.5x.png 1.5x, treenum-b3-in2@2x.png 2x, treenum-b3-in2@3x.png 3x, treenum-b3-in2@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Postorder (inorder-3):&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-post&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-post.png&#34; srcset=&#34;treenum-b3-post.png 1x, treenum-b3-post@1.5x.png 1.5x, treenum-b3-post@2x.png 2x, treenum-b3-post@3x.png 3x, treenum-b3-post@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Just looking at the leaves of each, &lt;i&gt;none&lt;/i&gt; of them&#xA;has a nice simple form with regular gaps&#xA;like the seq(0, &lt;i&gt;n&lt;/i&gt;) = 2 &lt;i&gt;n&lt;/i&gt; of in-order traversal&#xA;for binary trees.&#xA;Instead, both the possible “in-order” traversals&#xA;end up with equations more like the post-order traversal.&#xA;What happened?&#xA;Where did the nice, regular pattern go?&#xA;&lt;a class=anchor href=&#34;#unexpected_ordering&#34;&gt;&lt;h2 id=&#34;unexpected_ordering&#34;&gt;An Unexpected Ordering&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;In a binary tree, the in-order numbering has the property that&#xA;after the first leaf, one new parent (non-leaf) node is introduced&#xA;before each additional one leaf.&#xA;This works out nicely because the number of parent nodes in a binary&#xA;tree of &lt;i&gt;N&lt;/i&gt; leaves is &lt;i&gt;N&lt;/i&gt;1.&#xA;The number of parents nodes in a &lt;i&gt;k&lt;/i&gt;-ary tree of &lt;i&gt;N&lt;/i&gt; leaves is&#xA;(&lt;i&gt;N&lt;/i&gt;1)/(&lt;i&gt;k&lt;/i&gt;1),&#xA;so we could try to build a nicer numbering&#xA;by,&#xA;after the first leaf,&#xA;introducing one new parent node&#xA;before each additional &lt;i&gt;k&lt;/i&gt;1 leaf nodes.&#xA;That is, the leaves would be numbered by&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(0, &lt;i&gt;n&lt;/i&gt;) = &lt;i&gt;n&lt;/i&gt; + (&lt;i&gt;n&lt;/i&gt;+&lt;i&gt;k&lt;/i&gt;2)/(&lt;i&gt;k&lt;/i&gt;1),&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;which gives this leaf structure:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-code-chop&#34; class=&#34;center pad&#34; width=667 height=14 src=&#34;treenum-b3-code-chop.png&#34; srcset=&#34;treenum-b3-code-chop.png 1x, treenum-b3-code-chop@1.5x.png 1.5x, treenum-b3-code-chop@2x.png 2x, treenum-b3-code-chop@3x.png 3x, treenum-b3-code-chop@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;But how do we fill in the gaps?&#xA;The first three triples—0, 2, 3 and 5, 6, 8 and 9, 11, 12—clearly&#xA;get nodes 1, 4, 7, and 10 as their three parents and one grandparent,&#xA;but which is the grandparent?&#xA;The binary in-order traversal was very self-similar,&#xA;so lets try the same thing here:&#xA;after the first node,&#xA;reserve one node for higher levels&#xA;before each &lt;i&gt;k&lt;/i&gt;1 nodes at this level.&#xA;That is, the parents are 1, 7, 10,&#xA;and the grandparent is 4.&#xA;&#xA;&lt;p&gt;&#xA;Applying this process throughout the tree,&#xA;we end up with this traversal order (inorder-G, for gap-induced):&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-code&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-code.png&#34; srcset=&#34;treenum-b3-code.png 1x, treenum-b3-code@1.5x.png 1.5x, treenum-b3-code@2x.png 2x, treenum-b3-code@3x.png 3x, treenum-b3-code@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;For contrast, here is the ordering from the previous section&#xA;that visited each node after its first subtree (inorder-1):&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-in1&#34; class=&#34;center pad&#34; width=667 height=128 src=&#34;treenum-b3-in1.png&#34; srcset=&#34;treenum-b3-in1.png 1x, treenum-b3-in1@1.5x.png 1.5x, treenum-b3-in1@2x.png 2x, treenum-b3-in1@3x.png 3x, treenum-b3-in1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The inorder-1 traversal has a regular tree structure but irregular numbering.&#xA;In contrast, the inorder-G traversal has an irregular tree structure but very&#xA;regular numbering that generalizes the binary inorder numbering:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = &lt;i&gt;k&lt;/i&gt;&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt; (&lt;i&gt;n&lt;/i&gt;+&lt;i&gt;k&lt;/i&gt;2)/(&lt;i&gt;k&lt;/i&gt;1) + &lt;i&gt;k&lt;/i&gt;&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;-1&lt;/sup&gt; + &lt;i&gt;k&lt;/i&gt;&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;-2&lt;/sup&gt; + ... + &lt;i&gt;k&lt;/i&gt;&lt;sup&gt;0&lt;/sup&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;For a binary tree, inorder-1 and inorder-G are the same:&#xA;the traversal has both a regular tree structure and a regular numbering.&#xA;But for &lt;i&gt;k&lt;/i&gt;-ary trees, it seems you can pick only one.&#xA;&#xA;&lt;p&gt;&#xA;The regularity of the numbering is easiest to see in base &lt;i&gt;k&lt;/i&gt;.&#xA;For example, here is the binary inorder traversal with binary numbering:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b2-d2-in&#34; class=&#34;center pad&#34; width=1003 height=138 src=&#34;treenum-b2-d2-in.png&#34; srcset=&#34;treenum-b2-d2-in.png 1x, treenum-b2-d2-in@1.5x.png 1.5x, treenum-b2-d2-in@2x.png 2x, treenum-b2-d2-in@3x.png 3x, treenum-b2-d2-in@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The bottom row uses numbers ending in 0;&#xA;the next row up uses numbers ending in 01;&#xA;then 011; and so on.&#xA;&#xA;&lt;p&gt;&#xA;For the 3-ary tree, it is the inorder-G traversal (not inorder-1 or inorder-2) that produces an equivalent pattern:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;treenum-b3-d3-in&#34; class=&#34;center pad&#34; width=1065 height=128 src=&#34;treenum-b3-d3-in.png&#34; srcset=&#34;treenum-b3-d3-in.png 1x, treenum-b3-d3-in@1.5x.png 1.5x, treenum-b3-d3-in@2x.png 2x, treenum-b3-d3-in@3x.png 3x, treenum-b3-d3-in@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The bottom row uses numbers ending in 0 or 2;&#xA;the next row up uses numbers ending in 01 or 21;&#xA;then 011 or 211; and so on.&#xA;The general rule is that&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;seq(&lt;i&gt;l&lt;/i&gt;, &lt;i&gt;n&lt;/i&gt;) = ((&lt;i&gt;n&lt;/i&gt;+&lt;i&gt;k&lt;/i&gt;2)/(&lt;i&gt;k&lt;/i&gt;1))&lt;sub&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sub&gt; || (1)&lt;sub&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sub&gt;&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;where (&lt;i&gt;x&lt;/i&gt;)&lt;sub&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sub&gt; means &lt;i&gt;x&lt;/i&gt; written as a base-&lt;i&gt;k&lt;/i&gt; number,&#xA;|| denotes concatenation of base-&lt;i&gt;k&lt;/i&gt; numbers,&#xA;and&#xA;(1)&lt;sub&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sub&gt;&lt;sup&gt;&lt;i&gt;l&lt;/i&gt;&lt;/sup&gt;&#xA;means the base-&lt;i&gt;k&lt;/i&gt; digit 1 repeated &lt;i&gt;l&lt;/i&gt; times.&#xA;&#xA;&lt;p&gt;&#xA;Through a roundabout way, then, weve ended up with a&#xA;tree traversal thats really just a nice base-&lt;i&gt;k&lt;/i&gt; encoding.&#xA;There must be existing uses of this encoding,&#xA;but Ive been unable to find any or even determine what its name is.&#xA;&lt;a class=anchor href=&#34;#further_reading&#34;&gt;&lt;h2 id=&#34;further_reading&#34;&gt;Further Reading?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Does anyone know of any places this has appeared before? Id love to read about them. Thanks.&#xA;</content>
</entry>
<entry>
<title>Our Software Dependency Problem</title>
<id>tag:research.swtch.com,2012:research.swtch.com/deps</id>
<link rel="alternate" href="http://research.swtch.com/deps"></link>
<published>2019-01-23T11:00:00-05:00</published>
<updated>2019-01-23T11:02:00-05:00</updated>
<summary type="text">Download and run code from strangers on the internet. What could go wrong?</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;For decades, discussion of software reuse was far more common than actual software reuse.&#xA;Today, the situation is reversed: developers reuse software written by others every day,&#xA;in the form of software dependencies,&#xA;and the situation goes mostly unexamined.&#xA;&#xA;&lt;p&gt;&#xA;My own background includes a decade of working with&#xA;Googles internal source code system,&#xA;which treats software dependencies as a first-class concept,&lt;a class=footnote id=body1 href=&#34;#note1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&#xA;and also developing support for&#xA;dependencies in the Go programming language.&lt;a class=footnote id=body2 href=&#34;#note2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&#xA;&#xA;&lt;p&gt;&#xA;Software dependencies carry with them&#xA;serious risks that are too often overlooked.&#xA;The shift to easy, fine-grained software reuse has happened so quickly&#xA;that we do not yet understand the best practices for choosing&#xA;and using dependencies effectively,&#xA;or even for deciding when they are appropriate and when not.&#xA;My purpose in writing this article is to raise awareness of the risks&#xA;and encourage more investigation of solutions.&#xA;&lt;a class=anchor href=&#34;#what_is_a_dependency&#34;&gt;&lt;h2 id=&#34;what_is_a_dependency&#34;&gt;What is a dependency?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;In todays software development world,&#xA;a &lt;i&gt;dependency&lt;/i&gt; is additional code that you want to call from your program.&#xA;Adding a dependency avoids repeating work already done:&#xA;designing, writing, testing, debugging, and maintaining a specific&#xA;unit of code.&#xA;In this article well call that unit of code a &lt;i&gt;package&lt;/i&gt;;&#xA;some systems use terms like library or module instead of package.&#xA;&#xA;&lt;p&gt;&#xA;Taking on externally-written dependencies is an old practice:&#xA;most programmers have at one point in their careers&#xA;had to go through the steps of manually downloading and installing&#xA;a required library, like Cs PCRE or zlib, or C++s Boost or Qt,&#xA;or Javas JodaTime or JUnit.&#xA;These packages contain high-quality, debugged code&#xA;that required significant expertise to develop.&#xA;For a program that needs the functionality provided by one of these packages,&#xA;the tedious work of manually downloading, installing, and updating&#xA;the package&#xA;is easier than the work of redeveloping that functionality from scratch.&#xA;But the high fixed costs of reuse&#xA;mean that manually-reused packages tend to be big:&#xA;a tiny package would be easier to reimplement.&#xA;&#xA;&lt;p&gt;&#xA;A &lt;i&gt;dependency manager&lt;/i&gt;&#xA;(sometimes called a package manager)&#xA;automates the downloading and installation of dependency packages.&#xA;As dependency managers&#xA;make individual packages easier to download and install,&#xA;the lower fixed costs make&#xA;smaller packages economical to publish and reuse.&#xA;&#xA;&lt;p&gt;&#xA;For example, the Node.js dependency manager NPM provides&#xA;access to over 750,000 packages.&#xA;One of them, &lt;code&gt;escape-string-regexp&lt;/code&gt;,&#xA;provides a single function that escapes regular expression&#xA;operators in its input.&#xA;The entire implementation is:&#xA;&lt;pre&gt;var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;&#xA;&#xA;module.exports = function (str) {&#xA;&#x9;if (typeof str !== &#39;string&#39;) {&#xA;&#x9;&#x9;throw new TypeError(&#39;Expected a string&#39;);&#xA;&#x9;}&#xA;&#x9;return str.replace(matchOperatorsRe, &#39;\\$&amp;amp;&#39;);&#xA;};&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Before dependency managers, publishing an eight-line code library&#xA;would have been unthinkable: too much overhead for too little benefit.&#xA;But NPM has driven the overhead approximately to zero,&#xA;with the result that nearly-trivial functionality&#xA;can be packaged and reused.&#xA;In late January 2019, the &lt;code&gt;escape-string-regexp&lt;/code&gt; package&#xA;is explicitly depended upon by almost a thousand&#xA;other NPM packages,&#xA;not to mention all the packages developers write for their own use&#xA;and dont share.&#xA;&#xA;&lt;p&gt;&#xA;Dependency managers now exist for essentially every programming language.&#xA;Maven Central (Java),&#xA;Nuget (.NET),&#xA;Packagist (PHP),&#xA;PyPI (Python),&#xA;and RubyGems (Ruby)&#xA;each host over 100,000 packages.&#xA;The arrival of this kind of fine-grained, widespread software reuse&#xA;is one of the most consequential shifts in software development&#xA;over the past two decades.&#xA;And if were not more careful, it will lead to serious problems.&#xA;&lt;a class=anchor href=&#34;#what_could_go_wrong&#34;&gt;&lt;h2 id=&#34;what_could_go_wrong&#34;&gt;What could go wrong?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A package, for this discussion, is code you download from the internet.&#xA;Adding a package as a dependency outsources the work of developing that&#xA;code—designing, writing, testing, debugging, and maintaining—to&#xA;someone else on the internet,&#xA;someone you often dont know.&#xA;By using that code, you are exposing your own program&#xA;to all the failures and flaws in the dependency.&#xA;Your programs execution now literally &lt;i&gt;depends&lt;/i&gt;&#xA;on code downloaded from this stranger on the internet.&#xA;Presented this way, it sounds incredibly unsafe.&#xA;Why would anyone do this?&#xA;&#xA;&lt;p&gt;&#xA;We do this because its easy,&#xA;because it seems to work,&#xA;because everyone else is doing it too,&#xA;and, most importantly, because&#xA;it seems like a natural continuation of&#xA;age-old established practice.&#xA;But there are important differences were ignoring.&#xA;&#xA;&lt;p&gt;&#xA;Decades ago, most developers already&#xA;trusted others to write software they depended on,&#xA;such as operating systems and compilers.&#xA;That software was bought from known sources,&#xA;often with some kind of support agreement.&#xA;There was still a potential for bugs or outright mischief,&lt;a class=footnote id=body3 href=&#34;#note3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&#xA;but at least we knew who we were dealing with and usually&#xA;had commercial or legal recourses available.&#xA;&#xA;&lt;p&gt;&#xA;The phenomenon of open-source software,&#xA;distributed at no cost over the internet,&#xA;has displaced many of those earlier software purchases.&#xA;When reuse was difficult, there were fewer projects publishing reusable code packages.&#xA;Even though their licenses typically disclaimed, among other things,&#xA;any “implied warranties of merchantability and fitness for&#xA;a particular purpose,”&#xA;the projects built up well-known reputations&#xA;that often factored heavily into peoples decisions about which to use.&#xA;The commercial and legal support for trusting our software sources&#xA;was replaced by reputational support.&#xA;Many common early packages still enjoy good reputations:&#xA;consider BLAS (published 1979), Netlib (1987), libjpeg (1991),&#xA;LAPACK (1992), HP STL (1994), and zlib (1995).&#xA;&#xA;&lt;p&gt;&#xA;Dependency managers have scaled this open-source code reuse model down:&#xA;now, developers can share code at the granularity of&#xA;individual functions of tens of lines.&#xA;This is a major technical accomplishment.&#xA;There are myriad available packages,&#xA;and writing code can involve such a large number of them,&#xA;but the commercial, legal, and reputational support mechanisms&#xA;for trusting the code have not carried over.&#xA;We are trusting more code with less justification for doing so.&#xA;&#xA;&lt;p&gt;&#xA;The cost of adopting a bad dependency can be viewed&#xA;as the sum, over all possible bad outcomes,&#xA;of the cost of each bad outcome&#xA;multiplied by its probability of happening (risk).&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;deps-cost&#34; class=&#34;center pad&#34; width=383 height=95 src=&#34;deps-cost.png&#34; srcset=&#34;deps-cost.png 1x, deps-cost@1.5x.png 1.5x, deps-cost@2x.png 2x, deps-cost@3x.png 3x, deps-cost@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The context where a dependency will be used&#xA;determines the cost of a bad outcome.&#xA;At one end of the spectrum is a personal hobby project,&#xA;where the cost of most bad outcomes&#xA;is near zero:&#xA;youre just having fun, bugs have no real impact other than&#xA;wasting some time, and even debugging them can be fun.&#xA;So the risk probability almost doesnt matter: its being multiplied by zero.&#xA;At the other end of the spectrum is production software&#xA;that must be maintained for years.&#xA;Here, the cost of a bug in&#xA;a dependency can be very high:&#xA;servers may go down,&#xA;sensitive data may be divulged,&#xA;customers may be harmed,&#xA;companies may fail.&#xA;High failure costs make it much more important&#xA;to estimate and then reduce any risk of a serious failure.&#xA;&#xA;&lt;p&gt;&#xA;No matter what the expected cost,&#xA;experiences with larger dependencies&#xA;suggest some approaches for&#xA;estimating and reducing the risks of adding a software dependency.&#xA;It is likely that better tooling is needed to help reduce&#xA;the costs of these approaches,&#xA;much as dependency managers have focused to date on&#xA;reducing the costs of download and installation.&#xA;&lt;a class=anchor href=&#34;#inspect_the_dependency&#34;&gt;&lt;h2 id=&#34;inspect_the_dependency&#34;&gt;Inspect the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;You would not hire a software developer youve never heard of&#xA;and know nothing about.&#xA;You would learn more about them first:&#xA;check references, conduct a job interview,&#xA;run background checks, and so on.&#xA;Before you depend on a package you found on the internet,&#xA;it is similarly prudent&#xA;to learn a bit about it first.&#xA;&#xA;&lt;p&gt;&#xA;A basic inspection can give you a sense&#xA;of how likely you are to run into problems trying to use this code.&#xA;If the inspection reveals likely minor problems,&#xA;you can take steps to prepare for or maybe avoid them.&#xA;If the inspection reveals major problems,&#xA;it may be best not to use the package:&#xA;maybe youll find a more suitable one,&#xA;or maybe you need to develop one yourself.&#xA;Remember that open-source packages are published&#xA;by their authors in the hope that they will be useful&#xA;but with no guarantee of usability or support.&#xA;In the middle of a production outage, youll be the one debugging it.&#xA;As the original GNU General Public License warned,&#xA;“The entire risk as to the quality and performance of the&#xA;program is with you.&#xA;Should the program prove defective, you assume the cost of all&#xA;necessary servicing, repair or correction.”&lt;a class=footnote id=body4 href=&#34;#note4&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&#xA;&#xA;&lt;p&gt;&#xA;The rest of this section outlines some considerations when inspecting a package&#xA;and deciding whether to depend on it.&#xA;&lt;a class=anchor href=&#34;#design&#34;&gt;&lt;h3 id=&#34;design&#34;&gt;Design&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Is packages documentation clear? Does the API have a clear design?&#xA;If the authors can explain the packages API and its design well to you, the user,&#xA;in the documentation,&#xA;that increases the likelihood they have explained the implementation well to the computer, in the source code.&#xA;Writing code for a clear, well-designed API is also easier, faster, and hopefully less error-prone.&#xA;Have the authors documented what they expect from client code&#xA;in order to make future upgrades compatible?&#xA;(Examples include the C++&lt;a class=footnote id=body5 href=&#34;#note5&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt; and Go&lt;a class=footnote id=body6 href=&#34;#note6&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt; compatibility documents.)&#xA;&lt;a class=anchor href=&#34;#code_quality&#34;&gt;&lt;h3 id=&#34;code_quality&#34;&gt;Code Quality&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Is the code well-written?&#xA;Read some of it.&#xA;Does it look like the authors have been careful, conscientious, and consistent?&#xA;Does it look like code youd want to debug? You may need to.&#xA;&#xA;&lt;p&gt;&#xA;Develop your own systematic ways to check code quality.&#xA;For example, something as simple as compiling a C or C++ program with&#xA;important compiler warnings enabled (for example, &lt;code&gt;-Wall&lt;/code&gt;)&#xA;can give you a sense of how seriously the developers work to avoid&#xA;various undefined behaviors.&#xA;Recent languages like Go, Rust, and Swift use an &lt;code&gt;unsafe&lt;/code&gt; keyword to mark&#xA;code that violates the type system; look to see how much unsafe code there is.&#xA;More advanced semantic tools like Infer&lt;a class=footnote id=body7 href=&#34;#note7&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt; or SpotBugs&lt;a class=footnote id=body8 href=&#34;#note8&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt; are helpful too.&#xA;Linters are less helpful: you should ignore rote suggestions&#xA;about topics like brace style and focus instead on semantic problems.&#xA;&#xA;&lt;p&gt;&#xA;Keep an open mind to development practices you may not be familiar with.&#xA;For example, the SQLite library ships as a single 200,000-line C source file&#xA;and a single 11,000-line header, the “amalgamation.”&#xA;The sheer size of these files should raise an initial red flag,&#xA;but closer investigation would turn up the&#xA;actual development source code, a traditional file tree with&#xA;over a hundred C source files, tests, and support scripts.&#xA;It turns out that the single-file distribution is built automatically from the original sources&#xA;and is easier for end users, especially those without dependency managers.&#xA;(The compiled code also runs faster, because the compiler can see more optimization opportunities.)&#xA;&lt;a class=anchor href=&#34;#testing&#34;&gt;&lt;h3 id=&#34;testing&#34;&gt;Testing&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Does the code have tests?&#xA;Can you run them?&#xA;Do they pass?&#xA;Tests establish that the codes basic functionality is correct,&#xA;and they signal that the developer is serious about keeping it correct.&#xA;For example, the SQLite development tree has an incredibly thorough test suite&#xA;with over 30,000 individual test cases&#xA;as well as developer documentation explaining the testing strategy.&lt;a class=footnote id=body9 href=&#34;#note9&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;&#xA;On the other hand,&#xA;if there are few tests or no tests, or if the tests fail, thats a serious red flag:&#xA;future changes to the package&#xA;are likely to introduce regressions that could easily have been caught.&#xA;If you insist on tests in code you write yourself (you do, right?),&#xA;you should insist on tests in code you outsource to others.&#xA;&#xA;&lt;p&gt;&#xA;Assuming the tests exist, run, and pass, you can gather more&#xA;information by running them with run-time instrumentation&#xA;like code coverage analysis, race detection,&lt;a class=footnote id=body10 href=&#34;#note10&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;&#xA;memory allocation checking,&#xA;and memory leak detection.&#xA;&lt;a class=anchor href=&#34;#debugging&#34;&gt;&lt;h3 id=&#34;debugging&#34;&gt;Debugging&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Find the packages issue tracker.&#xA;Are there many open bug reports? How long have they been open?&#xA;Are there many fixed bugs? Have any bugs been fixed recently?&#xA;If you see lots of open issues about what look like real bugs,&#xA;especially if they have been open for a long time,&#xA;thats not a good sign.&#xA;On the other hand, if the closed issues show that bugs are&#xA;rarely found and promptly fixed,&#xA;thats great.&#xA;&lt;a class=anchor href=&#34;#maintenance&#34;&gt;&lt;h3 id=&#34;maintenance&#34;&gt;Maintenance&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Look at the packages commit history.&#xA;How long has the code been actively maintained?&#xA;Is it actively maintained now?&#xA;Packages that have been actively maintained for an extended&#xA;amount of time are more likely to continue to be maintained.&#xA;How many people work on the package?&#xA;Many packages are personal projects that developers&#xA;create and share for fun in their spare time.&#xA;Others are the result of thousands of hours of work&#xA;by a group of paid developers.&#xA;In general, the latter kind of package is more likely to have&#xA;prompt bug fixes, steady improvements, and general upkeep.&#xA;&#xA;&lt;p&gt;&#xA;On the other hand, some code really is “done.”&#xA;For example, NPMs &lt;code&gt;escape-string-regexp&lt;/code&gt;,&#xA;shown earlier, may never need to be modified again.&#xA;&lt;a class=anchor href=&#34;#usage&#34;&gt;&lt;h3 id=&#34;usage&#34;&gt;Usage&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Do many other packages depend on this code?&#xA;Dependency managers can often provide statistics about usage,&#xA;or you can use a web search to estimate how often&#xA;others write about using the package.&#xA;More users should at least mean more people for whom&#xA;the code works well enough,&#xA;along with faster detection of new bugs.&#xA;Widespread usage is also a hedge against the question of continued maintenance:&#xA;if a widely-used package loses its maintainer,&#xA;an interested user is likely to step forward.&#xA;&#xA;&lt;p&gt;&#xA;For example, libraries like PCRE or Boost or JUnit&#xA;are incredibly widely used.&#xA;That makes it more likely—although certainly not guaranteed—that&#xA;bugs you might otherwise run into have already been fixed,&#xA;because others ran into them first.&#xA;&lt;a class=anchor href=&#34;#security&#34;&gt;&lt;h3 id=&#34;security&#34;&gt;Security&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Will you be processing untrusted inputs with the package?&#xA;If so, does it seem to be robust against malicious inputs?&#xA;Does it have a history of security problems&#xA;listed in the National Vulnerability Database (NVD)?&lt;a class=footnote id=body11 href=&#34;#note11&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt;&#xA;&#xA;&lt;p&gt;&#xA;For example, when Jeff Dean and I started work on&#xA;Google Code Search&lt;a class=footnote id=body12 href=&#34;#note12&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;—&lt;code&gt;grep&lt;/code&gt; over public source code—in 2006,&#xA;the popular PCRE regular expression library seemed like an obvious choice.&#xA;In an early discussion with Googles security team, however,&#xA;we learned that PCRE had a history of problems like buffer overflows,&#xA;especially in its parser.&#xA;We could have learned the same by searching for PCRE in the NVD.&#xA;That discovery didnt immediately cause us to abandon PCRE,&#xA;but it did make us think more carefully about testing and isolation.&#xA;&lt;a class=anchor href=&#34;#licensing&#34;&gt;&lt;h3 id=&#34;licensing&#34;&gt;Licensing&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Is the code properly licensed?&#xA;Does it have a license at all?&#xA;Is the license acceptable for your project or company?&#xA;A surprising fraction of projects on GitHub have no clear license.&#xA;Your project or company may impose further restrictions on the&#xA;allowed licenses of dependencies.&#xA;For example, Google disallows the use of code licensed under&#xA;AGPL-like licenses (too onerous) as well as WTFPL-like licenses (too vague).&lt;a class=footnote id=body13 href=&#34;#note13&#34;&gt;&lt;sup&gt;13&lt;/sup&gt;&lt;/a&gt;&#xA;&lt;a class=anchor href=&#34;#dependencies&#34;&gt;&lt;h3 id=&#34;dependencies&#34;&gt;Dependencies&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Does the code have dependencies of its own?&#xA;Flaws in indirect dependencies are just as bad for your program&#xA;as flaws in direct dependencies.&#xA;Dependency managers can list all the transitive dependencies&#xA;of a given package, and each of them should ideally be inspected as&#xA;described in this section.&#xA;A package with many dependencies incurs additional inspection work,&#xA;because those same dependencies incur additional risk&#xA;that needs to be evaluated.&#xA;&#xA;&lt;p&gt;&#xA;Many developers have never looked at the full list of transitive&#xA;dependencies of their code and dont know what they depend on.&#xA;For example, in March 2016 the NPM user community discovered&#xA;that many popular projects—including Babel, Ember, and React—all depended&#xA;indirectly on a tiny package called &lt;code&gt;left-pad&lt;/code&gt;,&#xA;consisting of a single 8-line function body.&#xA;They discovered this when&#xA;the author of &lt;code&gt;left-pad&lt;/code&gt; deleted that package from NPM,&#xA;inadvertently breaking most Node.js users builds.&lt;a class=footnote id=body14 href=&#34;#note14&#34;&gt;&lt;sup&gt;14&lt;/sup&gt;&lt;/a&gt;&#xA;And &lt;code&gt;left-pad&lt;/code&gt; is hardly exceptional in this regard.&#xA;For example, 30% of the&#xA;750,000 packages published on NPM&#xA;depend—at least indirectly—on &lt;code&gt;escape-string-regexp&lt;/code&gt;.&#xA;Adapting Leslie Lamports observation about distributed systems,&#xA;a dependency manager can easily&#xA;create a situation in which the failure of a package you didnt&#xA;even know existed can render your own code unusable.&#xA;&lt;a class=anchor href=&#34;#test_the_dependency&#34;&gt;&lt;h2 id=&#34;test_the_dependency&#34;&gt;Test the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The inspection process should include running a packages own tests.&#xA;If the package passes the inspection and you decide to make your&#xA;project depend on it,&#xA;the next step should be to write new tests focused on the functionality&#xA;needed by your application.&#xA;These tests often start out as short standalone programs&#xA;written to make sure you can understand the packages API&#xA;and that it does what you think it does.&#xA;(If you cant or it doesnt, turn back now!)&#xA;It is worth then taking the extra effort to turn those programs&#xA;into automated tests that can be run against newer versions of the package.&#xA;If you find a bug and have a potential fix,&#xA;youll want to be able to rerun these project-specific tests&#xA;easily, to make sure that the fix did not break anything else.&#xA;&#xA;&lt;p&gt;&#xA;It is especially worth exercising the likely problem areas&#xA;identified by the&#xA;basic inspection.&#xA;For Code Search, we knew from past experience&#xA;that PCRE sometimes took&#xA;a long time to execute certain regular expression searches.&#xA;Our initial plan was to have separate thread pools for&#xA;“simple” and “complicated” regular expression searches.&#xA;One of the first tests we ran was a benchmark,&#xA;comparing &lt;code&gt;pcregrep&lt;/code&gt; with a few other &lt;code&gt;grep&lt;/code&gt; implementations.&#xA;When we found that, for one basic test case,&#xA;&lt;code&gt;pcregrep&lt;/code&gt; was 70X slower than the&#xA;fastest &lt;code&gt;grep&lt;/code&gt; available,&#xA;we started to rethink our plan to use PCRE.&#xA;Even though we eventually dropped PCRE entirely,&#xA;that benchmark remains in our code base today.&#xA;&lt;a class=anchor href=&#34;#abstract_the_dependency&#34;&gt;&lt;h2 id=&#34;abstract_the_dependency&#34;&gt;Abstract the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Depending on a package is a decision that you are likely to&#xA;revisit later.&#xA;Perhaps updates will take the package in a new direction.&#xA;Perhaps serious security problems will be found.&#xA;Perhaps a better option will come along.&#xA;For all these reasons, it is worth the effort&#xA;to make it easy to migrate your project to a new dependency.&#xA;&#xA;&lt;p&gt;&#xA;If the package will be used from many places in your projects source code,&#xA;migrating to a new dependency would require making&#xA;changes to all those different source locations.&#xA;Worse, if the package will be exposed in your own projects API,&#xA;migrating to a new dependency would require making&#xA;changes in all the code calling your API,&#xA;which you might not control.&#xA;To avoid these costs, it makes sense to&#xA;define an interface of your own,&#xA;along with a thin wrapper implementing that&#xA;interface using the dependency.&#xA;Note that the wrapper should include only&#xA;what your project needs from the dependency,&#xA;not everything the dependency offers.&#xA;Ideally, that allows you to&#xA;substitute a different, equally appropriate dependency later,&#xA;by changing only the wrapper.&#xA;Migrating your per-project tests to use the new interface&#xA;tests the interface and wrapper implementation&#xA;and also makes it easy to test any potential replacements&#xA;for the dependency.&#xA;&#xA;&lt;p&gt;&#xA;For Code Search, we developed an abstract &lt;code&gt;Regexp&lt;/code&gt; class&#xA;that defined the interface Code Search needed from any&#xA;regular expression engine.&#xA;Then we wrote a thin wrapper around PCRE&#xA;implementing that interface.&#xA;The indirection made it easy to test alternate libraries,&#xA;and it kept us from accidentally introducing knowledge&#xA;of PCRE internals into the rest of the source tree.&#xA;That in turn ensured that it would be easy to switch&#xA;to a different dependency if needed.&#xA;&lt;a class=anchor href=&#34;#isolate_the_dependency&#34;&gt;&lt;h2 id=&#34;isolate_the_dependency&#34;&gt;Isolate the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;It may also be appropriate to isolate a dependency&#xA;at run-time, to limit the possible damage caused by bugs in it.&#xA;For example, Google Chrome allows users to add dependencies—extension code—to the browser.&#xA;When Chrome launched in 2008, it introduced&#xA;the critical feature (now standard in all browsers)&#xA;of isolating each extension in a sandbox running in a separate&#xA;operating-system process.&lt;a class=footnote id=body15 href=&#34;#note15&#34;&gt;&lt;sup&gt;15&lt;/sup&gt;&lt;/a&gt;&#xA;An exploitable bug in an badly-written extension&#xA;therefore did not automatically have access to the entire memory&#xA;of the browser itself&#xA;and could be stopped from making inappropriate system calls.&lt;a class=footnote id=body16 href=&#34;#note16&#34;&gt;&lt;sup&gt;16&lt;/sup&gt;&lt;/a&gt;&#xA;For Code Search, until we dropped PCRE entirely,&#xA;our plan was to isolate at least the PCRE parser&#xA;in a similar sandbox.&#xA;Today,&#xA;another option would be a lightweight hypervisor-based sandbox&#xA;like gVisor.&lt;a class=footnote id=body17 href=&#34;#note17&#34;&gt;&lt;sup&gt;17&lt;/sup&gt;&lt;/a&gt;&#xA;Isolating dependencies&#xA;reduces the associated risks of running that code.&#xA;&#xA;&lt;p&gt;&#xA;Even with these examples and other off-the-shelf options,&#xA;run-time isolation of suspect code is still too difficult and rarely done.&#xA;True isolation would require a completely memory-safe language,&#xA;with no escape hatch into untyped code.&#xA;Thats challenging not just in entirely unsafe languages like C and C++&#xA;but also in languages that provide restricted unsafe operations,&#xA;like Java when including JNI, or like Go, Rust, and Swift&#xA;when including their “unsafe” features.&#xA;Even in a memory-safe language like JavaScript,&#xA;code often has access to far more than it needs.&#xA;In November 2018, the latest version of the NPM package &lt;code&gt;event-stream&lt;/code&gt;,&#xA;which provided a functional streaming API for JavaScript events,&#xA;was discovered to contain obfuscated malicious code that had been&#xA;added two and a half months earlier.&#xA;The code, which harvested large Bitcoin wallets from users of the Copay mobile app,&#xA;was accessing system resources entirely unrelated to processing&#xA;event streams.&lt;a class=footnote id=body18 href=&#34;#note18&#34;&gt;&lt;sup&gt;18&lt;/sup&gt;&lt;/a&gt;&#xA;One of many possible defenses to this kind of problem&#xA;would be to better restrict what dependencies can access.&#xA;&lt;a class=anchor href=&#34;#avoid_the_dependency&#34;&gt;&lt;h2 id=&#34;avoid_the_dependency&#34;&gt;Avoid the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If a dependency seems too risky and you cant find&#xA;a way to isolate it, the best answer may be to avoid it entirely,&#xA;or at least to avoid the parts youve identified as most problematic.&#xA;&#xA;&lt;p&gt;&#xA;For example, as we better understood the risks and costs associated&#xA;with PCRE, our plan for Google Code Search evolved&#xA;from “use PCRE directly,” to “use PCRE but sandbox the parser,”&#xA;to “write a new regular expression parser but keep the PCRE execution engine,”&#xA;to “write a new parser and connect it to a different, more efficient open-source execution engine.”&#xA;Later we rewrote the execution engine as well,&#xA;so that no dependencies were left,&#xA;and we open-sourced the result: RE2.&lt;a class=footnote id=body19 href=&#34;#note19&#34;&gt;&lt;sup&gt;19&lt;/sup&gt;&lt;/a&gt;&#xA;&#xA;&lt;p&gt;&#xA;If you only need a&#xA;tiny fraction of a dependency,&#xA;it may be simplest to make a copy of what you need&#xA;(preserving appropriate copyright and other legal notices, of course).&#xA;You are taking on responsibility for fixing bugs, maintenance, and so on,&#xA;but youre also completely isolated from the larger risks.&#xA;The Go developer community has a proverb about this:&#xA;“A little copying is better than a little dependency.”&lt;a class=footnote id=body20 href=&#34;#note20&#34;&gt;&lt;sup&gt;20&lt;/sup&gt;&lt;/a&gt;&#xA;&lt;a class=anchor href=&#34;#upgrade_the_dependency&#34;&gt;&lt;h2 id=&#34;upgrade_the_dependency&#34;&gt;Upgrade the dependency&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;For a long time, the conventional wisdom about software was “if it aint broke, dont fix it.”&#xA;Upgrading carries a chance of introducing new bugs;&#xA;without a corresponding reward—like a new feature you need—why take the risk?&#xA;This analysis ignores two costs.&#xA;The first is the cost of the eventual upgrade.&#xA;In software, the difficulty of making code changes does not scale linearly:&#xA;making ten small changes is less work and easier to get right&#xA;than making one equivalent large change.&#xA;The second is the cost of discovering already-fixed bugs the hard way.&#xA;Especially in a security context, where known bugs are actively exploited,&#xA;every day you wait is another day that attackers can break in.&#xA;&#xA;&lt;p&gt;&#xA;For example, consider the year 2017 at Equifax, as recounted by executives&#xA;in detailed congressional testimony.&lt;a class=footnote id=body21 href=&#34;#note21&#34;&gt;&lt;sup&gt;21&lt;/sup&gt;&lt;/a&gt;&#xA;On March 7, a new vulnerability in Apache Struts was disclosed, and a patched version was released.&#xA;On March 8, Equifax received a notice from US-CERT about the need to update&#xA;any uses of Apache Struts.&#xA;Equifax ran source code and network scans on March 9 and March 15, respectively;&#xA;neither scan turned up a particular group of public-facing web servers.&#xA;On May 13, attackers found the servers that Equifaxs security teams could not.&#xA;They used the Apache Struts vulnerability to breach Equifaxs network&#xA;and then steal detailed personal and financial information&#xA;about 148 million people&#xA;over the next two months.&#xA;Equifax finally noticed the breach on July 29&#xA;and publicly disclosed it on September 4.&#xA;By the end of September, Equifaxs CEO, CIO, and CSO had all resigned,&#xA;and a congressional investigation was underway.&#xA;&#xA;&lt;p&gt;&#xA;Equifaxs experience drives home the point that&#xA;although dependency managers know the versions they are using at build time,&#xA;you need other arrangements to track that information&#xA;through your production deployment process.&#xA;For the Go language, we are experimenting with automatically&#xA;including a version manifest in every binary, so that deployment&#xA;processes can scan binaries for dependencies that need upgrading.&#xA;Go also makes that information available at run-time, so that&#xA;servers can consult databases of known bugs and self-report to&#xA;monitoring software when they are in need of upgrades.&#xA;&#xA;&lt;p&gt;&#xA;Upgrading promptly is important, but upgrading means&#xA;adding new code to your project,&#xA;which should mean updating your evaluation of the risks&#xA;of using the dependency based on the new version.&#xA;As minimum, youd want to skim the diffs showing the&#xA;changes being made from the current version to the&#xA;upgraded versions,&#xA;or at least read the release notes,&#xA;to identify the most likely areas of concern in the upgraded code.&#xA;If a lot of code is changing, so that the diffs are difficult to digest,&#xA;that is also information you can incorporate into your&#xA;risk assessment update.&#xA;&#xA;&lt;p&gt;&#xA;Youll also want to re-run the tests youve written&#xA;that are specific to your project,&#xA;to make sure the upgraded package is at least as suitable&#xA;for the project as the earlier version.&#xA;It also makes sense to re-run the packages own tests.&#xA;If the package has its own dependencies,&#xA;it is entirely possible that your projects configuration&#xA;uses different versions of those dependencies&#xA;(either older or newer ones) than the packages authors use.&#xA;Running the packages own tests can quickly identify problems&#xA;specific to your configuration.&#xA;&#xA;&lt;p&gt;&#xA;Again, upgrades should not be completely automatic.&#xA;You need to verify that the upgraded versions are appropriate for&#xA;your environment before deploying them.&lt;a class=footnote id=body22 href=&#34;#note22&#34;&gt;&lt;sup&gt;22&lt;/sup&gt;&lt;/a&gt;&#xA;&#xA;&lt;p&gt;&#xA;If your upgrade process includes re-running the&#xA;integration and qualification tests youve already written for the dependency,&#xA;so that you are likely to identify new problems before they reach production,&#xA;then, in most cases, delaying an upgrade is riskier than upgrading quickly.&#xA;&#xA;&lt;p&gt;&#xA;The window for security-critical upgrades is especially short.&#xA;In the aftermath of the Equifax breach, forensic security teams found&#xA;evidence that attackers (perhaps different ones)&#xA;had successfully exploited the Apache Struts&#xA;vulnerability on the affected servers on March 10, only three days&#xA;after it was publicly disclosed, but theyd only run a single &lt;code&gt;whoami&lt;/code&gt; command.&#xA;&lt;a class=anchor href=&#34;#watch_your_dependencies&#34;&gt;&lt;h2 id=&#34;watch_your_dependencies&#34;&gt;Watch your dependencies&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Even after all that work, youre not done tending your dependencies.&#xA;Its important to continue to monitor them and perhaps even&#xA;re-evaluate your decision to use them.&#xA;&#xA;&lt;p&gt;&#xA;First, make sure that you keep using the&#xA;specific package versions you think you are.&#xA;Most dependency managers now make it easy or even automatic&#xA;to record the cryptographic hash of the expected source code&#xA;for a given package version&#xA;and then to check that hash when re-downloading the package&#xA;on another computer or in a test environment.&#xA;This ensures that your build use&#xA;the same dependency source code you inspected and tested.&#xA;These kinds of checks&#xA;prevented the &lt;code&gt;event-stream&lt;/code&gt; attacker,&#xA;described earlier, from silently inserting&#xA;malicious code in the already-released version 3.3.5.&#xA;Instead, the attacker had to create a new version, 3.3.6,&#xA;and wait for people to upgrade (without looking closely at the changes).&#xA;&#xA;&lt;p&gt;&#xA;It is also important to watch for new indirect dependencies creeping in:&#xA;upgrades can easily introduce new packages&#xA;upon which the success of your project now depends.&#xA;They deserve your attention as well.&#xA;In the case of &lt;code&gt;event-stream&lt;/code&gt;, the malicious code was&#xA;hidden in a different package, &lt;code&gt;flatmap-stream&lt;/code&gt;,&#xA;which the new &lt;code&gt;event-stream&lt;/code&gt; release added as a&#xA;new dependency.&#xA;&#xA;&lt;p&gt;&#xA;Creeping dependencies can also affect the size of your project.&#xA;During the development of Googles Sawzall&lt;a class=footnote id=body23 href=&#34;#note23&#34;&gt;&lt;sup&gt;23&lt;/sup&gt;&lt;/a&gt;—a JITed&#xA;logs processing language—the authors discovered at various times that&#xA;the main interpreter binary contained not just Sawzalls JIT&#xA;but also (unused) PostScript, Python, and JavaScript interpreters.&#xA;Each time, the culprit turned out to be unused dependencies&#xA;declared by some library Sawzall did depend on,&#xA;combined with the fact that Googles build system&#xA;eliminated any manual effort needed to start using a new dependency..&#xA;This kind of error is the reason that the Go language&#xA;makes importing an unused package a compile-time error.&#xA;&#xA;&lt;p&gt;&#xA;Upgrading is a natural time to revisit the decision to use a dependency thats changing.&#xA;Its also important to periodically revisit any dependency that &lt;i&gt;isnt&lt;/i&gt; changing.&#xA;Does it seem plausible that there are no security problems or other bugs to fix?&#xA;Has the project been abandoned?&#xA;Maybe its time to start planning to replace that dependency.&#xA;&#xA;&lt;p&gt;&#xA;Its also important to recheck the security history of each dependency.&#xA;For example, Apache Struts disclosed different major remote code execution&#xA;vulnerabilities in 2016, 2017, and 2018.&#xA;Even if you have a list of all the servers that run it and&#xA;update them promptly, that track record might make you rethink using it at all.&#xA;&lt;a class=anchor href=&#34;#conclusion&#34;&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Software reuse is finally here,&#xA;and I dont mean to understate its benefits:&#xA;it has brought an enormously positive transformation&#xA;for software developers.&#xA;Even so, weve accepted this transformation without&#xA;completely thinking through the potential consequences.&#xA;The old reasons for trusting dependencies are becoming less valid&#xA;at exactly the same time we have more dependencies than ever.&#xA;&#xA;&lt;p&gt;&#xA;The kind of critical examination of specific dependencies that&#xA;I outlined in this article is a significant amount of work&#xA;and remains the exception rather than the rule.&#xA;But I doubt there are any developers who actually&#xA;make the effort to do this for every possible new dependency.&#xA;I have only done a subset of them for a subset of my own dependencies.&#xA;Most of the time the entirety of the decision is “lets see what happens.”&#xA;Too often, anything more than that seems like too much effort.&#xA;&#xA;&lt;p&gt;&#xA;But the Copay and Equifax attacks are clear warnings of&#xA;real problems in the way we consume software dependencies today.&#xA;We should not ignore the warnings.&#xA;I offer three broad recommendations.&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Recognize the problem.&lt;/i&gt;&#xA;If nothing else, I hope this article has convinced&#xA;you that there is a problem here worth addressing.&#xA;We need many people to focus significant effort on solving it.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Establish best practices for today.&lt;/i&gt;&#xA;We need to establish best practices for managing dependencies&#xA;using whats available today.&#xA;This means working out processes that evaluate, reduce, and track risk,&#xA;from the original adoption decision through to production use.&#xA;In fact, just as some engineers specialize in testing,&#xA;it may be that we need engineers who specialize in managing dependencies.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Develop better dependency technology for tomorrow.&lt;/i&gt;&#xA;Dependency managers have essentially eliminated the cost of&#xA;downloading and installing a dependency.&#xA;Future development effort should focus on reducing the cost of&#xA;the kind of evaluation and maintenance necessary to use&#xA;a dependency.&#xA;For example, package discovery sites might work to find&#xA;more ways to allow developers to share their findings.&#xA;Build tools should, at the least, make it easy to run a packages own tests.&#xA;More aggressively,&#xA;build tools and package management systems could also work together&#xA;to allow package authors to test new changes against all public clients&#xA;of their APIs.&#xA;Languages should also provide easy ways to isolate a suspect package.&lt;/ol&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Theres a lot of good software out there.&#xA;Lets work together to find out how to reuse it safely.&#xA;&#xA;&lt;p&gt;&#xA;&lt;a class=anchor href=&#34;#references&#34;&gt;&lt;h2 id=&#34;references&#34;&gt;References&lt;/h2&gt;&lt;/a&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;a name=note1&gt;&lt;/a&gt;&#xA;Rachel Potvin and Josh Levenberg, “Why Google Stores Billions of Lines of Code in a Single Repository,” &lt;i&gt;Communications of the ACM&lt;/i&gt; 59(7) (July 2016), pp. 78-87. &lt;a href=&#34;https://doi.org/10.1145/2854146&#34;&gt;https://doi.org/10.1145/2854146&lt;/a&gt; &lt;a class=back href=&#34;#body1&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note2&gt;&lt;/a&gt;&#xA;Russ Cox, “Go &amp;amp; Versioning,” February 2018. &lt;a href=&#34;https://research.swtch.com/vgo&#34;&gt;https://research.swtch.com/vgo&lt;/a&gt; &lt;a class=back href=&#34;#body2&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note3&gt;&lt;/a&gt;&#xA;Ken Thompson, “Reflections on Trusting Trust,” &lt;i&gt;Communications of the ACM&lt;/i&gt; 27(8) (August 1984), pp. 761763. &lt;a href=&#34;https://doi.org/10.1145/358198.358210&#34;&gt;https://doi.org/10.1145/358198.358210&lt;/a&gt; &lt;a class=back href=&#34;#body3&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note4&gt;&lt;/a&gt;&#xA;GNU Project, “GNU General Public License, version 1,” February 1989. &lt;a href=&#34;https://www.gnu.org/licenses/old-licenses/gpl-1.0.html&#34;&gt;https://www.gnu.org/licenses/old-licenses/gpl-1.0.html&lt;/a&gt; &lt;a class=back href=&#34;#body4&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note5&gt;&lt;/a&gt;&#xA;Titus Winters, “SD-8: Standard Library Compatibility,” C++ Standing Document, August 2018. &lt;a href=&#34;https://isocpp.org/std/standing-documents/sd-8-standard-library-compatibility&#34;&gt;https://isocpp.org/std/standing-documents/sd-8-standard-library-compatibility&lt;/a&gt; &lt;a class=back href=&#34;#body5&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note6&gt;&lt;/a&gt;&#xA;Go Project, “Go 1 and the Future of Go Programs,” September 2013. &lt;a href=&#34;https://golang.org/doc/go1compat&#34;&gt;https://golang.org/doc/go1compat&lt;/a&gt; &lt;a class=back href=&#34;#body6&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note7&gt;&lt;/a&gt;&#xA;Facebook, “Infer: A tool to detect bugs in Java and C/C++/Objective-C code before it ships.” &lt;a href=&#34;https://fbinfer.com/&#34;&gt;https://fbinfer.com/&lt;/a&gt; &lt;a class=back href=&#34;#body7&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note8&gt;&lt;/a&gt;&#xA;“SpotBugs: Find bugs in Java Programs.” &lt;a href=&#34;https://spotbugs.github.io/&#34;&gt;https://spotbugs.github.io/&lt;/a&gt; &lt;a class=back href=&#34;#body8&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note9&gt;&lt;/a&gt;&#xA;D. Richard Hipp, “How SQLite is Tested.” &lt;a href=&#34;https://www.sqlite.org/testing.html&#34;&gt;https://www.sqlite.org/testing.html&lt;/a&gt; &lt;a class=back href=&#34;#body9&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note10&gt;&lt;/a&gt;&#xA;Alexander Potapenko, “Testing Chromium: ThreadSanitizer v2, a next-gen data race detector,” April 2014. &lt;a href=&#34;https://blog.chromium.org/2014/04/testing-chromium-threadsanitizer-v2.html&#34;&gt;https://blog.chromium.org/2014/04/testing-chromium-threadsanitizer-v2.html&lt;/a&gt; &lt;a class=back href=&#34;#body10&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note11&gt;&lt;/a&gt;&#xA;NIST, “National Vulnerability Database Search and Statistics.” &lt;a href=&#34;https://nvd.nist.gov/vuln/search&#34;&gt;https://nvd.nist.gov/vuln/search&lt;/a&gt; &lt;a class=back href=&#34;#body11&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note12&gt;&lt;/a&gt;&#xA;Russ Cox, “Regular Expression Matching with a Trigram Index, or How Google Code Search Worked,” January 2012. &lt;a href=&#34;https://swtch.com/~rsc/regexp/regexp4.html&#34;&gt;https://swtch.com/~rsc/regexp/regexp4.html&lt;/a&gt; &lt;a class=back href=&#34;#body12&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note13&gt;&lt;/a&gt;&#xA;Google, “Google Open Source: Using Third-Party Licenses.” &lt;a href=&#34;https://opensource.google.com/docs/thirdparty/licenses/#banned&#34;&gt;https://opensource.google.com/docs/thirdparty/licenses/#banned&lt;/a&gt; &lt;a class=back href=&#34;#body13&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note14&gt;&lt;/a&gt;&#xA;Nathan Willis, “A single Node of failure,” LWN, March 2016. &lt;a href=&#34;https://lwn.net/Articles/681410/&#34;&gt;https://lwn.net/Articles/681410/&lt;/a&gt; &lt;a class=back href=&#34;#body14&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note15&gt;&lt;/a&gt;&#xA;Charlie Reis, “Multi-process Architecture,” September 2008. &lt;a href=&#34;https://blog.chromium.org/2008/09/multi-process-architecture.html&#34;&gt;https://blog.chromium.org/2008/09/multi-process-architecture.html&lt;/a&gt; &lt;a class=back href=&#34;#body15&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note16&gt;&lt;/a&gt;&#xA;Adam Langley, “Chromiums seccomp Sandbox,” August 2009. &lt;a href=&#34;https://www.imperialviolet.org/2009/08/26/seccomp.html&#34;&gt;https://www.imperialviolet.org/2009/08/26/seccomp.html&lt;/a&gt; &lt;a class=back href=&#34;#body16&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note17&gt;&lt;/a&gt;&#xA;Nicolas Lacasse, “Open-sourcing gVisor, a sandboxed container runtime,” May 2018. &lt;a href=&#34;https://cloud.google.com/blog/products/gcp/open-sourcing-gvisor-a-sandboxed-container-runtime&#34;&gt;https://cloud.google.com/blog/products/gcp/open-sourcing-gvisor-a-sandboxed-container-runtime&lt;/a&gt; &lt;a class=back href=&#34;#body17&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note18&gt;&lt;/a&gt;&#xA;Adam Baldwin, “Details about the event-stream incident,” November 2018. &lt;a href=&#34;https://blog.npmjs.org/post/180565383195/details-about-the-event-stream-incident&#34;&gt;https://blog.npmjs.org/post/180565383195/details-about-the-event-stream-incident&lt;/a&gt; &lt;a class=back href=&#34;#body18&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note19&gt;&lt;/a&gt;&#xA;Russ Cox, “RE2: a principled approach to regular expression matching,” March 2010. &lt;a href=&#34;https://opensource.googleblog.com/2010/03/re2-principled-approach-to-regular.html&#34;&gt;https://opensource.googleblog.com/2010/03/re2-principled-approach-to-regular.html&lt;/a&gt; &lt;a class=back href=&#34;#body19&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note20&gt;&lt;/a&gt;&#xA;Rob Pike, “Go Proverbs,” November 2015. &lt;a href=&#34;https://go-proverbs.github.io/&#34;&gt;https://go-proverbs.github.io/&lt;/a&gt; &lt;a class=back href=&#34;#body20&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note21&gt;&lt;/a&gt;&#xA;U.S. House of Representatives Committee on Oversight and Government Reform, “The Equifax Data Breach,” Majority Staff Report, 115th Congress, December 2018. &lt;a href=&#34;https://oversight.house.gov/report/committee-releases-report-revealing-new-information-on-equifax-data-breach/&#34;&gt;https://oversight.house.gov/report/committee-releases-report-revealing-new-information-on-equifax-data-breach/&lt;/a&gt; &lt;a class=back href=&#34;#body21&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note22&gt;&lt;/a&gt;&#xA;Russ Cox, “The Principles of Versioning in Go,” GopherCon Singapore, May 2018. &lt;a href=&#34;https://www.youtube.com/watch?v=F8nrpe0XWRg&#34;&gt;https://www.youtube.com/watch?v=F8nrpe0XWRg&lt;/a&gt; &lt;a class=back href=&#34;#body22&#34;&gt;(⇡)&lt;/a&gt;&#xA;&lt;li&gt;&lt;a name=note23&gt;&lt;/a&gt;&#xA;Rob Pike, Sean Dorward, Robert Griesemer, and Sean Quinlan, “Interpreting the Data: Parallel Analysis with Sawzall,” &lt;i&gt;Scientific Programming Journal&lt;/i&gt;, vol. 13 (2005). &lt;a href=&#34;https://doi.org/10.1155/2005/962135&#34;&gt;https://doi.org/10.1155/2005/962135&lt;/a&gt; &lt;a class=back href=&#34;#body23&#34;&gt;(⇡)&lt;/a&gt;&lt;/ol&gt;&#xA;&lt;a class=anchor href=&#34;#coda&#34;&gt;&lt;h2 id=&#34;coda&#34;&gt;Coda&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This post is a draft of my current thinking on this topic.&#xA;I hope that sharing it will provoke productive discussion,&#xA;attract more attention to the general problem,&#xA;and help me refine my own thoughts.&#xA;I also intend to publish a revised copy of this as an article elsewhere.&#xA;For both these reasons, unlike most of my blog posts,&#xA;&lt;i&gt;this post is not Creative Commons-licensed&lt;/i&gt;.&#xA;Please link people to this post instead of making a copy.&#xA;When a more final version is published, I will link to it here.&#xA;&#xA;&lt;p class=copyright&gt;&#xA;© 2019 Russ Cox. All Rights Reserved.&#xA;</content>
</entry>
<entry>
<title>Why Add Versions To Go?</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-why-versions</id>
<link rel="alternate" href="http://research.swtch.com/vgo-why-versions"></link>
<published>2018-06-07T10:20:00-04:00</published>
<updated>2018-06-07T10:22:00-04:00</updated>
<summary type="text">Why should Go understand package versions at all? (Go &amp; Versioning, Part 10)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;People sometimes ask me why we should add package versions to Go at all.&#xA;Isn&#39;t Go doing well enough without versions?&#xA;Usually these people have had a bad experience with versions&#xA;in another language, and they associate versions with breaking changes.&#xA;In this post, I want to talk a little about why we do need to add support for package versions to Go.&#xA;Later posts will address why we won&#39;t encourage breaking changes.&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; command has two failure modes caused by ignorance of versions:&#xA;it can use code that is too old, and it can use code that is too new.&#xA;For example, suppose we want to use a package D, so we run &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;D&lt;/code&gt;&#xA;with no packages installed yet.&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; command will download the latest copy of D&#xA;(whatever &lt;code&gt;git&lt;/code&gt; &lt;code&gt;clone&lt;/code&gt; brings down),&#xA;which builds successfully.&#xA;To make our discussion easier, let&#39;s call that D version 1.0&#xA;and keep D&#39;s dependency requirements in mind&#xA;(and in our diagrams).&#xA;But remember that while we understand the idea of versions&#xA;and dependency requirements, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; does not.&#xA;&lt;pre&gt;$ go get D&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;vgo-why-1&#34; class=&#34;center pad&#34; width=200 height=39 src=&#34;vgo-why-1.png&#34; srcset=&#34;vgo-why-1.png 1x, vgo-why-1@1.5x.png 1.5x, vgo-why-1@2x.png 2x, vgo-why-1@3x.png 3x, vgo-why-1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Now suppose that a month later, we want to use C, which happens to import D.&#xA;We run &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;C&lt;/code&gt;.&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; command downloads the latest copy of C,&#xA;which happens to be C 1.8 and imports D.&#xA;Since &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; already has a downloaded copy of D, it uses that one&#xA;instead of incurring the cost of a fresh download.&#xA;Unfortunately, the build of C fails:&#xA;C is using a new feature from D introduced in D 1.4,&#xA;and &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; is reusing D 1.0.&#xA;The code is too old.&#xA;&lt;pre&gt;$ go get C&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;vgo-why-2&#34; class=&#34;center pad&#34; width=201 height=96 src=&#34;vgo-why-2.png&#34; srcset=&#34;vgo-why-2.png 1x, vgo-why-2@1.5x.png 1.5x, vgo-why-2@2x.png 2x, vgo-why-2@3x.png 3x, vgo-why-2@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Next we try running &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt;, which downloads the latest&#xA;copy of all the code involved, including code already downloaded.&#xA;&lt;pre&gt;$ go get -u C&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;vgo-why-3&#34; class=&#34;center pad&#34; width=201 height=104 src=&#34;vgo-why-3.png&#34; srcset=&#34;vgo-why-3.png 1x, vgo-why-3@1.5x.png 1.5x, vgo-why-3@2x.png 2x, vgo-why-3@3x.png 3x, vgo-why-3@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Unfortunately, D 1.6 was released an hour ago and&#xA;contains a bug that breaks C.&#xA;Now the code is too new.&#xA;Watching this play out from above, we know what &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;&#xA;needs to do: use D ≥ 1.4 but not D 1.6, so maybe D 1.4 or D 1.5.&#xA;It&#39;s very difficult to tell &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; that today,&#xA;since it doesn&#39;t understand the concept of a package version.&#xA;&#xA;&lt;p&gt;&#xA;Getting back to the original question in the post, &lt;i&gt;why add versions to Go?&lt;/i&gt;&#xA;&#xA;&lt;p&gt;&#xA;Because agreeing on a versioning system—a syntax for version identifiers,&#xA;along with rules for how to order and interpret them—establishes&#xA;a way for us to communicate more precisely with our tools, and with each other,&#xA;about which copy of a package we mean.&#xA;Versioning matters for correct builds, as we just saw,&#xA;but it enables other interesting tools too.&#xA;&#xA;&lt;p&gt;&#xA;For example, the obvious next step is to be able to list&#xA;which versions of a package are being used in a given&#xA;build and whether any of them have updates available.&#xA;Generalizing that, it would be useful to have a tool that&#xA;examines a list of builds, perhaps all the targets built at&#xA;a given company, and assembles the same list.&#xA;Such a list of versions can then feed into compliance checks,&#xA;queries into bug databases, and so on.&#xA;Embedding the version list in a built binary would even&#xA;allow a program to make these checks on its own behalf&#xA;while it runs.&#xA;These all exist for other systems already, of course: I&#39;m not claiming the ideas are novel.&#xA;The point is that establishing agreement on a versioning system&#xA;enables all these tools,&#xA;which can even be built outside the language toolchain.&#xA;&#xA;&lt;p&gt;&#xA;We can also move from query tools, which tell you about&#xA;your code, to development tools, which update it for you.&#xA;For example, an obvious next step is a tool to update&#xA;a package&#39;s dependencies to their latest versions&#xA;automatically whenever the package&#39;s tests&#xA;and those of its dependencies continue to pass.&#xA;Being able to describe versions might also enable&#xA;tools that apply code cleanups.&#xA;For example, having versions would let us write instructions&#xA;“when using D version ≥ 1.4, replace&#xA;the common client code idiom &lt;code&gt;x.Foo(1).Bar(2)&lt;/code&gt; with &lt;code&gt;x.FooBar()&lt;/code&gt;”&#xA;that a tool like &lt;code&gt;go&lt;/code&gt; &lt;code&gt;fix&lt;/code&gt; could execute.&#xA;&#xA;&lt;p&gt;&#xA;The goal of our work adding versions to the core Go toolchain—or,&#xA;more generally, adding them to the shared working vocabulary of&#xA;both Go developers and our tools—is to establish a foundation&#xA;that helps with core issues like building working programs&#xA;but also enables interesting external tools like these,&#xA;and certainly others we haven&#39;t imagined yet.&#xA;&#xA;&lt;p&gt;&#xA;If we&#39;re building a foundation for other tools, we should aim&#xA;to make that foundation as versatile, strong, and robust as possible,&#xA;to enable as many other tools as possible, with as little hindrance&#xA;as possible to those tools.&#xA;We&#39;re not just writing a single tool.&#xA;We&#39;re defining the way all these tools will work together.&#xA;This foundation is an API in the broad sense of something&#xA;that programs must be written against.&#xA;Like in any API, we want to choose a design that is&#xA;powerful enough to enable many uses&#xA;but at the same time simple, reliable, consistent, coherent,&#xA;and predictable.&#xA;Future posts will explore how vgo&#39;s design decisions&#xA;aim for those properties.&#xA;</content>
</entry>
<entry>
<title>What is Software Engineering?</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-eng</id>
<link rel="alternate" href="http://research.swtch.com/vgo-eng"></link>
<published>2018-05-30T10:00:00-04:00</published>
<updated>2018-05-30T10:02:00-04:00</updated>
<summary type="text">What is software engineering and what does Go mean by it? (Go &amp; Versioning, Part 9)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;Nearly all of Gos distinctive design decisions&#xA;were aimed at making software engineering simpler and easier.&#xA;We&#39;ve said this often.&#xA;The canonical reference is Rob Pike&#39;s 2012 article,&#xA;“&lt;a href=&#34;https://talks.golang.org/2012/splash.article&#34;&gt;Go at Google: Language Design in the Service of Software Engineering&lt;/a&gt;.”&#xA;But what is software engineering?&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;Software engineering is what happens to programming&#xA;&lt;br&gt;when you add time and other programmers.&lt;/i&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Programming means getting a program working.&#xA;You have a problem to solve, you write some Go code,&#xA;you run it, you get your answer, youre done.&#xA;Thats programming,&#xA;and that&#39;s difficult enough by itself.&#xA;But what if that code has to keep working, day after day?&#xA;What if five other programmers need to work on the code too?&#xA;Then you start to think about version control systems,&#xA;to track how the code changes over time&#xA;and to coordinate with the other programmers.&#xA;You add unit tests,&#xA;to make sure bugs you fix are not reintroduced over time,&#xA;not by you six months from now,&#xA;and not by that new team member whos unfamiliar with the code.&#xA;You think about modularity and design patterns,&#xA;to divide the program into parts that team members&#xA;can work on mostly independently.&#xA;You use tools to help you find bugs earlier.&#xA;You look for ways to make programs as clear as possible,&#xA;so that bugs are less likely.&#xA;You make sure that small changes can be tested quickly,&#xA;even in large programs.&#xA;You&#39;re doing all of this because your programming&#xA;has turned into software engineering.&#xA;&#xA;&lt;p&gt;&#xA;(This definition and explanation of software engineering&#xA;is my riff on an original theme by my Google colleague Titus Winters,&#xA;whose preferred phrasing is “software engineering is programming integrated over time.”&#xA;It&#39;s worth seven minutes of your time to see&#xA;&lt;a href=&#34;https://www.youtube.com/watch?v=tISy7EJQPzI&amp;t=8m17s&#34;&gt;his presentation of this idea at CppCon 2017&lt;/a&gt;,&#xA;from 8:17 to 15:00 in the video.)&#xA;&#xA;&lt;p&gt;&#xA;As I said earlier,&#xA;nearly all of Gos distinctive design decisions&#xA;have been motivated by concerns about software engineering,&#xA;by trying to accommodate time and other programmers&#xA;into the daily practice of programming.&#xA;&#xA;&lt;p&gt;&#xA;For example, most people think that we format Go code with &lt;code&gt;gofmt&lt;/code&gt;&#xA;to make code look nicer or to end debates among&#xA;team members about program layout.&#xA;But the &lt;a href=&#34;https://groups.google.com/forum/#!msg/golang-nuts/HC2sDhrZW5Y/7iuKxdbLExkJ&#34;&gt;most important reason for &lt;code&gt;gofmt&lt;/code&gt;&lt;/a&gt;&#xA;is that if an algorithm defines how Go source code is formatted,&#xA;then programs, like &lt;code&gt;goimports&lt;/code&gt; or &lt;code&gt;gorename&lt;/code&gt; or &lt;code&gt;go&lt;/code&gt; &lt;code&gt;fix&lt;/code&gt;,&#xA;can edit the source code more easily,&#xA;without introducing spurious formatting changes when writing the code back.&#xA;This helps you maintain code over time.&#xA;&#xA;&lt;p&gt;&#xA;As another example, Go import paths are URLs.&#xA;If code said &lt;code&gt;import&lt;/code&gt; &lt;code&gt;&#34;uuid&#34;&lt;/code&gt;,&#xA;youd have to ask which &lt;code&gt;uuid&lt;/code&gt; package.&#xA;Searching for &lt;code&gt;uuid&lt;/code&gt; on &lt;a href=&#34;https://godoc.org&#34;&gt;godoc.org&lt;/a&gt; turns up dozens of packages.&#xA;If instead the code says &lt;code&gt;import&lt;/code&gt; &lt;code&gt;&#34;github.com/pborman/uuid&#34;&lt;/code&gt;,&#xA;now its clear which package we mean.&#xA;Using URLs avoids ambiguity&#xA;and also reuses an existing mechanism for giving out names,&#xA;making it simpler and easier to coordinate with other programmers.&#xA;&#xA;&lt;p&gt;&#xA;Continuing the example,&#xA;Go import paths are written in Go source files,&#xA;not in a separate build configuration file.&#xA;This makes Go source files self-contained,&#xA;which makes it easier to understand, modify, and copy them.&#xA;These decisions, and more, were all made with the goal of&#xA;simplifying software engineering.&#xA;&#xA;&lt;p&gt;&#xA;In later posts I will talk specifically about why&#xA;versions are important for software engineering&#xA;and how software engineering concerns motivate&#xA;the design changes from dep to vgo.&#xA;</content>
</entry>
<entry>
<title>The vgo proposal is accepted. Now what?</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-accepted</id>
<link rel="alternate" href="http://research.swtch.com/vgo-accepted"></link>
<published>2018-05-29T16:45:00-04:00</published>
<updated>2018-05-29T16:47:00-04:00</updated>
<summary type="text">What is the state of vgo? (Go &amp; Versioning, Part 8)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;Last week, the proposal review committee accepted the “vgo approach” elaborated&#xA;on this blog in February and then summarized as &lt;a href=&#34;https://golang.org/issue/24301&#34;&gt;proposal #24301&lt;/a&gt;.&#xA;There has been some confusion about exactly what that means&#xA;and what happens next.&#xA;&#xA;&lt;p&gt;&#xA;In general, &lt;a href=&#34;https://golang.org/s/proposal&#34;&gt;a Go proposal&lt;/a&gt; is a discussion about whether to adopt a particular&#xA;approach and move on to writing, reviewing, and releasing a production implementation. Accepting a proposal&#xA;does not mean the implementation is complete. (In some cases there is no&#xA;implementation yet at all!) Accepting a proposal only means that we believe&#xA;the design is appropriate and that the production implementation can proceed&#xA;and be committed and released.&#xA;Inevitably we find details that need adjustment during that process.&#xA;&#xA;&lt;p&gt;&#xA;Vgo as it exists today is not the final implementation.&#xA;It is a prototype to make the ideas concrete&#xA;and to make it possible to experiment with the approach.&#xA;Bugs and design flaws will necessarily be found and fixed as we move toward making it the official&#xA;approach in the go command.&#xA;For example, the original vgo prototype downloaded code from sites&#xA;like GitHub using their APIs, for better efficiency and to avoid requiring&#xA;users to have every possible version control system installed.&#xA;Unfortunately, the GitHub API is far more restrictively rate-limited than&#xA;plain &lt;code&gt;git&lt;/code&gt; access, so the current vgo implementation has gone back&#xA;to invoking &lt;code&gt;git&lt;/code&gt;.&#xA;Although we&#39;d still &lt;a href=&#34;https://blogs.msdn.microsoft.com/devops/2018/05/29/announcing-the-may-2018-git-security-vulnerability/&#34;&gt;like to move away&lt;/a&gt;&#xA;from version control as the default&#xA;mechanism for obtaining open source code, we won&#39;t do that until we have a viable&#xA;replacement ready, to make any transition&#xA;as smooth as possible.&#xA;&#xA;&lt;p&gt;&#xA;More generally, the key reason for the vgo proposal is to add a common vocabulary&#xA;and semantics around versions of Go code, so that&#xA;developers and all kinds of tools can be precise&#xA;when talking to each other about exactly which program should be built, run, or analyzed.&#xA;Accepting the proposal is the beginning, not the end.&#xA;&#xA;&lt;p&gt;&#xA;One thing I&#39;ve heard from many people is that they want to start&#xA;using vgo in their company or project but are held back by not having&#xA;support for it in the toolchains their developers are using.&#xA;The fact that vgo is integrated deeply into the go command,&#xA;instead of being a separate vendor directory-writer,&#xA;introduces a chicken-and-egg problem.&#xA;To address that problem and make it as easy as possible&#xA;for developers to try the vgo approach,&#xA;we plan to include vgo functionality as an experimental opt-in feature in Go 1.11,&#xA;with the hope of incorporating feedback and finalizing the feature for Go 1.12.&#xA;(This rollout is analogous to how we included vendor directory functionality&#xA;as an experimental opt-in feature in Go 1.5 and turned it on by default in Go 1.6.)&#xA;We also plan to make &lt;a href=&#34;https://golang.org/issue/25069&#34;&gt;minimal changes to legacy &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;&lt;/a&gt; so that&#xA;it can obtain and understand code written using vgo conventions.&#xA;Those changes will be included in the next point release for Go 1.9 and Go 1.10.&#xA;&#xA;&lt;p&gt;&#xA;One thing I&#39;ve heard from zero people is that&#xA;&lt;a href=&#34;https://research.swtch.com/vgo&#34;&gt;they wish my blog posts were longer&lt;/a&gt;.&#xA;The original posts are quite dense and a number of important points&#xA;are more buried than they should be.&#xA;This post is the first of a series of much shorter posts to try to make&#xA;focused points about specific details of the vgo design, approach, and process.&#xA;</content>
</entry>
<entry>
<title>Versioned Go Commands</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-cmd</id>
<link rel="alternate" href="http://research.swtch.com/vgo-cmd"></link>
<published>2018-02-23T10:09:00-05:00</published>
<updated>2018-02-23T10:11:00-05:00</updated>
<summary type="text">What does it mean to add versioning to the go command? (Go &amp; Versioning, Part 7)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;What does it mean to add versioning to the &lt;code&gt;go&lt;/code&gt; command?&#xA;The &lt;a href=&#34;vgo-intro&#34;&gt;overview post&lt;/a&gt; gave a preview,&#xA;but the followup posts focused mainly on underlying&#xA;details: &lt;a href=&#34;vgo-import&#34;&gt;the import compatibility rule&lt;/a&gt;,&#xA;&lt;a href=&#34;vgo-mvs&#34;&gt;minimal version selection&lt;/a&gt;,&#xA;and &lt;a href=&#34;vgo-module&#34;&gt;defining go modules&lt;/a&gt;.&#xA;With those better understood, this post examines the&#xA;details of how versioning affects the &lt;code&gt;go&lt;/code&gt; command line&#xA;and the reasons for those changes.&#xA;&#xA;&lt;p&gt;&#xA;The major changes are:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;All commands (&lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt;, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;run&lt;/code&gt;, and so on)&#xA;will download imported source code automatically,&#xA;if the necessary version is not already present&#xA;in the download cache on the local system.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; command will serve mainly to change&#xA;which version of a package should be used in future&#xA;build commands.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; command will add access to module&#xA;information.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A new &lt;code&gt;go&lt;/code&gt; &lt;code&gt;release&lt;/code&gt; command will automate some of the&#xA;work a module author should do when tagging a new release,&#xA;such as checking API compatibility.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;all&lt;/code&gt; pattern is redefined to make sense in the&#xA;world of modules.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Developers can and will be encouraged to work in&#xA;directories outside the GOPATH tree.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;All these changes are implemented in the &lt;code&gt;vgo&lt;/code&gt; prototype.&#xA;&#xA;&lt;p&gt;&#xA;Deciding exactly how a build system should work is hard.&#xA;The introduction of new build caching in Go 1.10 prompted some&#xA;important, difficult decisions about the meaning of &lt;code&gt;go&lt;/code&gt; commands,&#xA;and the introduction of versioning does too.&#xA;Before I explain some of the decisions, I want to start by&#xA;explaining a guiding principle that I&#39;ve found helpful recently,&#xA;which I call the isolation rule:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;The result of a build command should depend only on the&#xA;source files that are its logical inputs, never on&#xA;hidden state left behind by previous build commands.)&lt;/i&gt;&#xA;&#xA;&lt;p&gt;&#xA;&lt;i&gt;That is, what a command does in isolation—on a&#xA;clean system loaded with only the relevant input&#xA;source files—is what it should do all the time,&#xA;no matter what else has happened on the system recently.&lt;/i&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;To see the wisdom of this rule, let me retell an old build story&#xA;and show how the isolation rule explains what happened.&#xA;&lt;a class=anchor href=&#34;#old_build_story&#34;&gt;&lt;h2 id=&#34;old_build_story&#34;&gt;An Old Build Story&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Long ago, when compilers and computers were very slow,&#xA;developers had scripts to build their whole programs from scratch,&#xA;but if they were just modifying one source file,&#xA;they might save time by manually recompiling just that file&#xA;and then relinking the overall program,&#xA;avoiding the cost of recompiling all the source files that&#xA;hadn&#39;t changed.&#xA;These manual incremental builds were fast but error-prone:&#xA;if you forgot to recompile a source file that you&#39;d modified,&#xA;the link of the final executable would use an out-of-date object file,&#xA;the executable would demonstrate buggy behavior,&#xA;and you might spend a long time staring at the (correct!) source code&#xA;looking for a bug that you&#39;d already fixed.&#xA;&#xA;&lt;p&gt;&#xA;&lt;a href=&#34;https://www.princeton.edu/~hos/mike/transcripts/feldman.htm&#34;&gt;Stu Feldman once explained&lt;/a&gt; what it was like in the early 1970s&#xA;when he spent a few months working on a few-thousand-line Ratfor program:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;I would go home for dinner at six or so, recompile the whole&#xA;world in the background, shut up, and then drive home.&#xA;It would take through the drive home and through dinner for&#xA;anything to happen.&#xA;This is because I kept making the classic error of&#xA;debugging a correct program, because you&#39;d forget to&#xA;compile the change.&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Transliterated to modern C tools (instead of Ratfor),&#xA;Feldman would work on a large program&#xA;by first compiling it from scratch:&#xA;&lt;pre&gt;$ rm -f *.o &amp;amp;&amp;amp; cc *.c &amp;amp;&amp;amp; ld *.o&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This build follows the isolation rule: starting from the same&#xA;source files, it produces the same result,&#xA;no matter what else has been run in that directory.&#xA;&#xA;&lt;p&gt;&#xA;But then Feldman would make changes to specific source files&#xA;and recompile only the modified ones, to save time:&#xA;&lt;pre&gt;$ cc r2.c r3.c r5.c &amp;amp;&amp;amp; ld *.o&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This incremental build does not follow the isolation rule.&#xA;The correctness of the command depends on Feldman&#xA;remembering which files they modified, and it&#39;s easy to&#xA;forget one. But it was so much faster, everyone did it anyway,&#xA;resorting to routines like Feldman&#39;s daily “build during dinner”&#xA;to correct any mistakes.&#xA;&#xA;&lt;p&gt;&#xA;Feldman continued:&lt;blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;Then one day, Steve Johnson came storming into my office in his usual way,&#xA;saying basically, “Goddamn it, I just spent the whole morning&#xA;debugging a correct program, again.&#xA;Why doesn&#39;t anybody do something like this? ...”&lt;/blockquote&gt;&#xA;&#xA;&lt;p&gt;&#xA;And that&#39;s the story of how Stu Feldman invented &lt;code&gt;make&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;&lt;code&gt;Make&lt;/code&gt; was a major advance because it provided&#xA;fast, incremental builds that followed the isolation rule.&#xA;Isolation is important because it means the build&#xA;is properly abstracted: only the source code matters.&#xA;As a developer, you can make changes to source code and not even&#xA;think about details like stale object files.&#xA;&#xA;&lt;p&gt;&#xA;However, the isolation rule is never an absolute.&#xA;There is always some area where it applies,&#xA;which I call the abstraction zone.&#xA;When you step out of the abstraction zone,&#xA;you are back to needing to keep state in your head.&#xA;For &lt;code&gt;make&lt;/code&gt;, the abstraction zone is a single directory.&#xA;If you are working on a program made up of libraries&#xA;in multiple directories, traditional &lt;code&gt;make&lt;/code&gt; is no help.&#xA;Most Unix programs in the 1970s fit in a single&#xA;directory, so it just wasn&#39;t important for &lt;code&gt;make&lt;/code&gt;&#xA;to provide isolation semantics in multi-directory builds.&#xA;&lt;a class=anchor href=&#34;#go_builds_and_the_isolation_rule&#34;&gt;&lt;h2 id=&#34;go_builds_and_the_isolation_rule&#34;&gt;Go Builds and the Isolation Rule&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;One way to view the history of design bug fixes in the &lt;code&gt;go&lt;/code&gt; command&#xA;is a sequence of steps extending its abstraction zone&#xA;to better match developer expectations.&#xA;&#xA;&lt;p&gt;&#xA;One of the advances of the &lt;code&gt;go&lt;/code&gt; command&#xA;was correct handling of source code spread across multiple&#xA;directories, extending the abstraction zone beyond what&#xA;&lt;code&gt;make&lt;/code&gt; provided.&#xA;Go programs are almost always spread across&#xA;multiple directories, and when we used &lt;code&gt;make&lt;/code&gt; it was&#xA;very common to forget to install a package in one directory&#xA;before trying to use it in another directory.&#xA;We were all too familiar with “the classic error of debugging a correct program.”&#xA;But even after fixing that,&#xA;there were still many ways to step out of the &lt;code&gt;go&lt;/code&gt; command&#39;s&#xA;abstraction zone, with unfortunate consequences.&#xA;&#xA;&lt;p&gt;&#xA;To take one example, if you had multiple directory trees&#xA;listed in GOPATH, builds in one tree blindly assumed that&#xA;installed packages in the others were up-to-date if present,&#xA;but it would rebuild them if missing.&#xA;This violation of the isolation rule caused&#xA;no end of mysterious problems&#xA;for projects using &lt;code&gt;godep&lt;/code&gt;, which used a second GOPATH entry&#xA;to simulate vendor directories.&#xA;We fixed this in Go 1.5.&#xA;&#xA;&lt;p&gt;&#xA;As another example, until very recently&#xA;command-line flags were not part of the abstraction zone.&#xA;If you start with a standard Go 1.9 distribution and run&#xA;&lt;pre&gt;$ go build hello.go&#xA;$ go install -a -gcflags=-N std&#xA;$ go build hello.go&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;the second &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; command produces a different&#xA;executable than the first.&#xA;The first &lt;code&gt;hello&lt;/code&gt; is linked against an optimized build of the Go&#xA;and standard library,&#xA;while the second &lt;code&gt;hello&lt;/code&gt; is linked against an unoptimized standard library.&#xA;This violation of the isolation rule led to widespread use&#xA;of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; &lt;code&gt;-a&lt;/code&gt; (always rebuild everything),&#xA;to reestablish isolation semantics.&#xA;We fixed this in Go 1.10.&#xA;&#xA;&lt;p&gt;&#xA;In both cases, the &lt;code&gt;go&lt;/code&gt; command was “working as designed.”&#xA;These were the kinds of details that we always kept mental track of&#xA;when using other build systems,&#xA;so it seemed reasonable to us not to abstract them away.&#xA;In fact, when I designed the behavior, I thought it was feature that&#xA;&lt;pre&gt;$ go install -a -gcflags=-N std&#xA;$ go build hello.go&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;let you build an optimized &lt;code&gt;hello&lt;/code&gt;&#xA;against an unoptimized standard library,&#xA;and I sometimes took advantage of that.&#xA;But, on the whole, Go developers disagreed.&#xA;They did not expect to, nor want to, keep mental track of that state.&#xA;For me, the isolation rule is useful because it gives a&#xA;simple test that helps me&#xA;cut through any mental contamination left by years of using&#xA;less capable build systems:&#xA;every command should have only one meaning, no matter what&#xA;other commands have preceded it.&#xA;&#xA;&lt;p&gt;&#xA;The isolation rule implies that some commands may need&#xA;to be made more complex, so one command can serve where&#xA;two commands did before.&#xA;For example, if you follow the isolation rule,&#xA;how &lt;i&gt;do&lt;/i&gt; you build an optimized &lt;code&gt;hello&lt;/code&gt;&#xA;against an unoptimized standard library?&#xA;We answered this in Go 1.10 by extending the &lt;code&gt;-gcflags&lt;/code&gt;&#xA;argument to start with an optional pattern&#xA;that controls which packages the flags affect.&#xA;To build an optimized hello against an unoptimized standard library,&#xA;&lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; &lt;code&gt;-gcflags=std=-N&lt;/code&gt; &lt;code&gt;hello.go&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;The isolation rule also implies that previously&#xA;context-dependent commands need to settle on one&#xA;context-independent meaning.&#xA;A good general rule seems to be to use the&#xA;one meaning that developers are most familiar with.&#xA;For example, a different variation of the flag problem&#xA;is:&#xA;&lt;pre&gt;$ go build -gcflags=-N hello.go&#xA;$ rm -rf $GOROOT/pkg&#xA;$ go build -gcflags=-N hello.go&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;In Go 1.9, the first &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; command builds an unoptimized hello&#xA;against the preinstalled, optimized standard library.&#xA;The second &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; command finds no preinstalled&#xA;standard library, so it rebuilds the standard library,&#xA;and the &lt;code&gt;-gcflags&lt;/code&gt; applies to all packages built during&#xA;the command, so the result is an unoptimized hello&#xA;built against an unoptimized standard library.&#xA;For Go 1.10, we had to choose which meaning is the one true meaning.&#xA;&#xA;&lt;p&gt;&#xA;Our original thought was that in the absence of a restricting pattern&#xA;like &lt;code&gt;std=&lt;/code&gt;, the &lt;code&gt;-gcflags=-N&lt;/code&gt; should apply to all packages&#xA;in the build, so that this command would always build&#xA;an unoptimized hello against an unoptimized standard library.&#xA;But most developers expect this command to apply the &lt;code&gt;-gcflags=-N&lt;/code&gt;&#xA;only to the argument of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt;, namely &lt;code&gt;hello.go&lt;/code&gt;,&#xA;because that&#39;s how it works in the common case,&#xA;when you have &lt;i&gt;not&lt;/i&gt; just deleted &lt;code&gt;$GOROOT/pkg&lt;/code&gt;.&#xA;We decided to preserve this expectation, defining that&#xA;when no pattern is given, the flags apply only to the&#xA;packages or files named on the build comamnd line.&#xA;In Go 1.10, building &lt;code&gt;hello.go&lt;/code&gt; with &lt;code&gt;-gcflags=-N&lt;/code&gt;&#xA;always builds an unoptimized hello against an optimized&#xA;standard library, even if &lt;code&gt;$GOROOT/pkg&lt;/code&gt;&#xA;has been deleted and the standard library must be rebuilt&#xA;on the spot.&#xA;If you do want a completely unoptimized build, that&#39;s &lt;code&gt;-gcflags=all=-N&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;The isolation rule is also helpful for thinking through&#xA;the design questions that arise in a versioned &lt;code&gt;go&lt;/code&gt; command.&#xA;Like in the flag decisions, some commands need to be&#xA;made more capable.&#xA;Others have multiple meanings now and must be&#xA;reduced to a single meaning.&#xA;&lt;a class=anchor href=&#34;#automatic_downloads&#34;&gt;&lt;h2 id=&#34;automatic_downloads&#34;&gt;Automatic Downloads&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The most significant implication of the isolation rule&#xA;is that commands like &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt;, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;install&lt;/code&gt;,&#xA;and &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; should download versioned dependencies&#xA;as needed (that is, if not already downloaded and cached).&#xA;&#xA;&lt;p&gt;&#xA;Suppose I have a brand new Go 1.10 installation&#xA;and I write this program to &lt;code&gt;hello.go&lt;/code&gt;:&#xA;&lt;pre&gt;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;rsc.io/quote&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;fmt.Println(quote.Hello())&#xA;}&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This fails:&#xA;&lt;pre&gt;$ go run hello.go&#xA;hello.go:5: import &#34;rsc.io/quote&#34;: import not found&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;But this succeeds:&#xA;&lt;pre&gt;$ go get rsc.io/quote&#xA;$ go run hello.go&#xA;Hello, world.&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;I can explain this.&#xA;After eight years of conditioning by use of &lt;code&gt;goinstall&lt;/code&gt;&#xA;and &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;, it seemed obvious to me that this behavior&#xA;was correct:&#xA;&lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; downloads &lt;code&gt;rsc.io/quote&lt;/code&gt; for us and&#xA;stashes it away for use by future commands,&#xA;so &lt;i&gt;of course&lt;/i&gt; that must happen before &lt;code&gt;go&lt;/code&gt; &lt;code&gt;run&lt;/code&gt;.&#xA;But I can explain the behavior of the optimization flag examples&#xA;in the previous section too,&#xA;and until a few months ago they also seemed obviously correct.&#xA;After more thought, I now believe&#xA;that any &lt;code&gt;go&lt;/code&gt; command should be able to download&#xA;versioned dependencies as needed.&#xA;I changed my mind for a few reasons.&#xA;&#xA;&lt;p&gt;&#xA;The first reason is the isolation rule.&#xA;The fact that every other design mistake I&#39;ve made&#xA;in the &lt;code&gt;go&lt;/code&gt; command violated the isolation rule&#xA;strongly suggests that requiring a prepatory&#xA;&lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; is a mistake too.&#xA;&#xA;&lt;p&gt;&#xA;The second reason is that I&#39;ve found it helpful&#xA;to think of the downloaded versioned source code as living&#xA;in a local cache that developers shouldn&#39;t need to&#xA;think about at all. If it&#39;s really a cache, cache misses&#xA;can&#39;t be failures.&#xA;&#xA;&lt;p&gt;&#xA;The third reason is the mental bookkeeping required.&#xA;Today&#39;s &lt;code&gt;go&lt;/code&gt; command expects developers&#xA;to keep track of which packages are and are not downloaded,&#xA;just as earlier &lt;code&gt;go&lt;/code&gt; commands expected developers&#xA;to keep track of which compiler flags had been&#xA;used during the most recent package installs.&#xA;As programs grow and as we add more precision about&#xA;versioning, the mental burden will grow,&#xA;even though the &lt;code&gt;go&lt;/code&gt; command is already tracking the same information.&#xA;For example, I think this hypothetical session&#xA;is a suboptimal developer experience:&#xA;&lt;pre&gt;$ git clone https://github.com/rsc/hello&#xA;$ cd hello&#xA;$ go build&#xA;go: rsc.io/sampler(v1.3.1) not installed&#xA;$ go get&#xA;go: installing rsc.io/sampler(v1.3.1)&#xA;$ go build&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If the command knows exactly what it needs, why make&#xA;the user do it?&#xA;&#xA;&lt;p&gt;&#xA;The fourth reason is that build systems in other languages&#xA;already do this.&#xA;When you check out a Rust repo and build it,&#xA;&lt;code&gt;cargo&lt;/code&gt; &lt;code&gt;build&lt;/code&gt; automatically fetches the dependencies&#xA;as part of the build, no questions asked.&#xA;&#xA;&lt;p&gt;&#xA;The fifth reason is that downloading on demand&#xA;allows downloading lazily, which in large programs&#xA;may mean not downloading many dependencies at all.&#xA;For example, the popular logging package&#xA;&lt;code&gt;github.com/sirupsen/logrus&lt;/code&gt; depends on&#xA;&lt;code&gt;golang.org/x/sys&lt;/code&gt;, but only when building on Solaris.&#xA;The eventual &lt;code&gt;go.mod&lt;/code&gt; file in &lt;code&gt;logrus&lt;/code&gt; would&#xA;list a specific version of &lt;code&gt;x/sys&lt;/code&gt; as a dependency.&#xA;When &lt;code&gt;vgo&lt;/code&gt; sees &lt;code&gt;logrus&lt;/code&gt; in a project, it will&#xA;consult the &lt;code&gt;go.mod&lt;/code&gt; file and determine which&#xA;version satisfies an &lt;code&gt;x/sys&lt;/code&gt; import.&#xA;But all the users not building for Solaris&#xA;will never see an &lt;code&gt;x/sys&lt;/code&gt; import, so they can avoid&#xA;the download of &lt;code&gt;x/sys&lt;/code&gt; entirely.&#xA;This optimization will become more important&#xA;as the dependency graph grows.&#xA;&#xA;&lt;p&gt;&#xA;I do expect resistance from developers who aren&#39;t yet ready&#xA;to think about builds that download code on demand.&#xA;We may need to make it possible to disable that with an&#xA;environment variable, but downloads should be enabled by default.&#xA;&lt;a class=anchor href=&#34;#go_get&#34;&gt;&lt;h2 id=&#34;go_get&#34;&gt;Changing Versions (&lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;)&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Plain &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;, without &lt;code&gt;-u&lt;/code&gt;, violates the command isolation rule&#xA;and must be fixed.&#xA;Today:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;If GOPATH is empty, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;rsc.io/quote&lt;/code&gt; downloads and builds the latest version of &lt;code&gt;rsc.io/quote&lt;/code&gt;&#xA;and its dependencies (for example, &lt;code&gt;rsc.io/sampler&lt;/code&gt;).&#xA;&lt;li&gt;&#xA;If there is already a &lt;code&gt;rsc.io/quote&lt;/code&gt; in GOPATH, from a &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; last year, then&#xA;the new &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; builds the old version.&#xA;&lt;li&gt;&#xA;If &lt;code&gt;rsc.io/sampler&lt;/code&gt; is already in GOPATH but &lt;code&gt;rsc.io/quote&lt;/code&gt; is not,&#xA;then &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; downloads the latest &lt;code&gt;rsc.io/quote&lt;/code&gt; and builds it&#xA;against the old copy of &lt;code&gt;rsc.io/sampler&lt;/code&gt;.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Overall, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; depends on the state of GOPATH, which&#xA;breaks the command isolation rule.&#xA;We need to fix that.&#xA;Since &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; has at least three meanings today,&#xA;we have some latitude in defining new behavior.&#xA;Today, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; fetches the latest version of the named modules&#xA;but then the exact versions of any dependencies requested by those modules,&#xA;subject to &lt;a href=&#34;vgo-mvs&#34;&gt;minimal version selection&lt;/a&gt;.&#xA;For example, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;rsc.io/quote&lt;/code&gt; always fetches the latest version of &lt;code&gt;rsc.io/quote&lt;/code&gt;&#xA;and then builds it with the exact version of &lt;code&gt;rsc.io/sampler&lt;/code&gt; that &lt;code&gt;rsc.io/quote&lt;/code&gt; has requested.&#xA;&#xA;&lt;p&gt;&#xA;&lt;code&gt;Vgo&lt;/code&gt; also allows module versions to be specified on the command line:&#xA;&lt;pre&gt;$ vgo get rsc.io/quote@latest # default&#xA;$ vgo get rsc.io/quote@v1.3.0&#xA;$ vgo get rsc.io/quote@&#39;&amp;lt;v1.6&#39; # finds v1.5.2&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;All of these also download (if not already cached)&#xA;the specific version of &lt;code&gt;rsc.io/sampler&lt;/code&gt; named in &lt;code&gt;rsc.io/quote&lt;/code&gt;&#39;s &lt;code&gt;go.mod&lt;/code&gt; file.&#xA;These commands modify the current module&#39;s &lt;code&gt;go.mod&lt;/code&gt; file,&#xA;and in that sense they do influence the operation of future commands.&#xA;But that influence is through an explicit file that users are expected&#xA;to know about and edit, not through hidden cache state.&#xA;Note that if the version requested on the command line is&#xA;earlier than the one already in &lt;code&gt;go.mod&lt;/code&gt;, then &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;&#xA;does a downgrade, which will also downgrade other packages&#xA;if needed, again following &lt;a href=&#34;vgo-mvs&#34;&gt;minimal version selection&lt;/a&gt;.&#xA;&#xA;&lt;p&gt;&#xA;In contrast to plain &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;, the &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; command&#xA;behaves the same&#xA;no matter what the state of the GOPATH source cache:&#xA;it downloads the latest copy of the named packages&#xA;and the latest copy of all their dependencies.&#xA;Since it follows the command isolation rule,&#xA;we should keep the same behavior:&#xA;&lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; upgrades the named modules to their latest versions&#xA;and also upgrades all of their dependencies.&#xA;&#xA;&lt;p&gt;&#xA;One idea that has come up in past few days is to introduce&#xA;a mode halfway between &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; (download the exact dependencies&#xA;of the thing I asked for) and &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; (download the latest dependencies).&#xA;If we believe that authors are conscientious about being very careful&#xA;with patch releases and only using them for critical, safe fixes,&#xA;then it might make sense to have a &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-p&lt;/code&gt;&#xA;that is like &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; but then applies only patch-level upgrades.&#xA;For example, if &lt;code&gt;rsc.io/quote&lt;/code&gt; requires &lt;code&gt;rsc.io/sampler&lt;/code&gt; v1.3.0&#xA;but v1.3.1 and v1.4.0 are also available,&#xA;then &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-p&lt;/code&gt; &lt;code&gt;rsc.io/quote&lt;/code&gt; would upgrade &lt;code&gt;rsc.io/sampler&lt;/code&gt;&#xA;to v1.3.1, not v1.4.0.&#xA;If you think this would be useful, please let us know.&#xA;&#xA;&lt;p&gt;&#xA;Of course, all the &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; variants record the effect of their&#xA;additions and upgrades in the &lt;code&gt;go.mod&lt;/code&gt; file.&#xA;In a sense, we&#39;ve made these commands follow the isolation rule&#xA;by introducing &lt;code&gt;go.mod&lt;/code&gt; as an explicit, visible input&#xA;replaces a previously implicit, hidden input: the state of the entire GOPATH.&#xA;&lt;a class=anchor href=&#34;#module_information&#34;&gt;&lt;h2 id=&#34;module_information&#34;&gt;Module Information (&lt;code&gt;go&lt;/code&gt; &lt;code&gt;list&lt;/code&gt;)&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;In addition to changing the versions being used,&#xA;we need to provide some way to inspect the current ones.&#xA;The &lt;code&gt;go&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; command is already in charge of reporting&#xA;useful information:&#xA;&lt;pre&gt;$ go list -f {{.Dir}} rsc.io/quote&#xA;/Users/rsc/src/rsc.io/quote&#xA;$ go list -f {{context.ReleaseTags}}&#xA;[go1.1 go1.2 go1.3 go1.4 go1.5 go1.6 go1.7 go1.8 go1.9 go1.10]&#xA;$&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;It probably makes sense to make module information available to&#xA;the format template, and we should also provide shorthands for&#xA;common operations like listing all the current module&#39;s dependencies.&#xA;The &lt;code&gt;vgo&lt;/code&gt; prototype already provides correct information for packages&#xA;in dependency modules.&#xA;For example:&#xA;&lt;pre&gt;$ vgo list -f {{.Dir}} rsc.io/quote&#xA;/Users/rsc/src/v/rsc.io/quote@v1.5.2&#xA;$&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;It also has a few shorthands. First, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-t&lt;/code&gt; lists all available tagged versions of a module:&#xA;&lt;pre&gt;$ vgo list -t rsc.io/quote&#xA;rsc.io/quote&#xA;&#x9;v1.0.0&#xA;&#x9;v1.1.0&#xA;&#x9;v1.2.0&#xA;&#x9;v1.2.1&#xA;&#x9;v1.3.0&#xA;&#x9;v1.4.0&#xA;&#x9;v1.5.0&#xA;&#x9;v1.5.1&#xA;&#x9;v1.5.2&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Second, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; lists the current module&#xA;followed by its dependencies:&#xA;&lt;pre&gt;$ vgo list -m&#xA;MODULE VERSION&#xA;github.com/you/hello -&#xA;golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c&#xA;rsc.io/quote v1.5.2&#xA;rsc.io/sampler v1.3.0&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Finally, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; adds a column showing the latest version of each module:&#xA;&lt;pre&gt;$ vgo list -m -u&#xA;MODULE VERSION LATEST&#xA;github.com/you/hello - -&#xA;golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c v0.0.0-20180208041248-4e4a3210bb54&#xA;rsc.io/quote v1.5.2 (2018-02-14 10:44) -&#xA;rsc.io/sampler v1.3.0 (2018-02-13 14:05) v1.99.99 (2018-02-13 17:20)&#xA;$&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;In the long term, these should be shorthands for more general support&#xA;in the format template, so that other programs can obtain the&#xA;information in other forms. Today they are just special cases.&#xA;&lt;a class=anchor href=&#34;#preparing_new_versions&#34;&gt;&lt;h2 id=&#34;preparing_new_versions&#34;&gt;Preparing New Versions (&lt;code&gt;go&lt;/code&gt; &lt;code&gt;release&lt;/code&gt;)&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to encourage authors to issue tagged releases&#xA;of their modules, so we need to make that as easy as possible.&#xA;We intend to add a &lt;code&gt;go&lt;/code&gt; &lt;code&gt;release&lt;/code&gt; command that can take care&#xA;of as much of the bookkeeping as needed.&#xA;For example, it might:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Check for backwards-incompatible type changes,&#xA;compared to the previous release.&#xA;We run a check like this when working on&#xA;the Go standard library, and it is very helpful.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Suggest whether this release should be a new point release&#xA;or a new minor release (because there&#39;s new API or&#xA;because many lines of code have changed).&#xA;Or perhaps always suggest a new minor release&#xA;unless the author asks for a point release,&#xA;to keep a potential &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-p&lt;/code&gt; useful.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Scan all source files in the module, even ones that&#xA;aren&#39;t normally built, to make sure that all imports&#xA;can be satisfied by the requirements listed in &lt;code&gt;go.mod&lt;/code&gt;.&#xA;Referring back to the example in the download section,&#xA;this check would make sure that &lt;code&gt;logrus&lt;/code&gt;&#39;s&#xA;&lt;code&gt;go.mod&lt;/code&gt; lists &lt;code&gt;x/sys&lt;/code&gt;.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;As new best practices for releases arise, we can add them to&#xA;&lt;code&gt;go&lt;/code&gt; &lt;code&gt;release&lt;/code&gt; so that authors always only have one step&#xA;to check whether their module is ready for a new release.&#xA;&lt;a class=anchor href=&#34;#pattern_matching&#34;&gt;&lt;h2 id=&#34;pattern_matching&#34;&gt;Pattern matching&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Most &lt;code&gt;go&lt;/code&gt; commands take a list of packages as arguments,&#xA;and that list can include patterns, like &lt;code&gt;rsc.io/...&lt;/code&gt;&#xA;(all packages with import paths beginning with &lt;code&gt;rsc.io/&lt;/code&gt;),&#xA;or &lt;code&gt;./...&lt;/code&gt; (all packages in the current directory or&#xA;subdirectories), or &lt;code&gt;all&lt;/code&gt; (all packages).&#xA;We need to check that these make sense in the new world of modules.&#xA;&#xA;&lt;p&gt;&#xA;Originally, patterns did not treat vendor directories specially,&#xA;so that if &lt;code&gt;github.com/you/hello/vendor/rsc.io/quote&lt;/code&gt; existed,&#xA;then &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;github.com/you/hello/...&lt;/code&gt; matched and tested it,&#xA;as did &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;./...&lt;/code&gt; when working in the &lt;code&gt;hello&lt;/code&gt; source directory.&#xA;The argument in favor of matching vendored code was that&#xA;doing so avoided a special case and that it was actually useful&#xA;to test your dependencies, as configured in your project,&#xA;along with the rest of your project.&#xA;The argument against matching vendored code was that&#xA;many developers wanted an easy way to test just the&#xA;code in their projects, assuming that dependencies&#xA;have already been tested separately and are not changing.&#xA;In Go 1.9, respecting that argument, we changed the &lt;code&gt;...&lt;/code&gt;&#xA;pattern not to walk into &lt;code&gt;vendor&lt;/code&gt; directories,&#xA;so that &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;github.com/you/hello/...&lt;/code&gt; does not&#xA;test vendored dependencies.&#xA;This sets up nicely for &lt;code&gt;vgo&lt;/code&gt;, which naturally would not&#xA;match dependencies either, since they no longer live in&#xA;a subdirectory of the main project.&#xA;That is, there is no change in the behavior of &lt;code&gt;...&lt;/code&gt; patterns&#xA;when moving from &lt;code&gt;go&lt;/code&gt; to &lt;code&gt;vgo&lt;/code&gt;, because that change&#xA;happened from Go 1.8 to Go 1.9 instead.&#xA;&#xA;&lt;p&gt;&#xA;That leaves the pattern &lt;code&gt;all&lt;/code&gt;.&#xA;When we first wrote the &lt;code&gt;go&lt;/code&gt; command,&#xA;before &lt;code&gt;goinstall&lt;/code&gt; and &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;,&#xA;it made sense to talk about building or testing “all packages.”&#xA;Today, it makes much less sense:&#xA;most developers work in a GOPATH that has a mix of many&#xA;different things, including many packages downloaded&#xA;and forgotten about.&#xA;I expect that almost no one runs commands&#xA;like &lt;code&gt;go&lt;/code&gt; &lt;code&gt;install&lt;/code&gt; &lt;code&gt;all&lt;/code&gt; or &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;all&lt;/code&gt; anymore:&#xA;it catches too many things that don&#39;t matter.&#xA;The real problem is that &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;all&lt;/code&gt; violates the isolation rule:&#xA;its meaning depends on the implicit state of GOPATH&#xA;set up by previous commands,&#xA;so no one depends on its meaning anymore.&#xA;In the &lt;code&gt;vgo&lt;/code&gt; prototype, we have redefined &lt;code&gt;all&lt;/code&gt;&#xA;to have a single, consistent meaning:&#xA;all the packages in the current module,&#xA;plus all the packages they depend on through one&#xA;a sequence of one or more imports.&#xA;&#xA;&lt;p&gt;&#xA;The new &lt;code&gt;all&lt;/code&gt; is exactly the packages a developer would need&#xA;to test in order to sanity check that a particular&#xA;combination of dependency versions work together,&#xA;but it leaves out nearby packages that don&#39;t matter in the current&#xA;module.&#xA;For example, in the &lt;a href=&#34;vgo1&#34;&gt;overview post&lt;/a&gt;,&#xA;our &lt;code&gt;hello&lt;/code&gt; module imported &lt;code&gt;rsc.io/quote&lt;/code&gt;&#xA;but not any other packages,&#xA;and in particular not the buggy package &lt;code&gt;rsc.io/quote/buggy&lt;/code&gt;.&#xA;Running &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;all&lt;/code&gt; in the &lt;code&gt;hello&lt;/code&gt; module&#xA;tests all packages in that module and then also&#xA;&lt;code&gt;rsc.io/quote&lt;/code&gt;.&#xA;It omits &lt;code&gt;rsc.io/quote/buggy&lt;/code&gt;, because&#xA;that one is not needed, even indirectly,&#xA;by the &lt;code&gt;hello&lt;/code&gt; module, so it&#39;s irrelevant to test.&#xA;This definition of &lt;code&gt;all&lt;/code&gt; restores repeatability,&#xA;and combined with Go 1.10&#39;s test caching,&#xA;it should make &lt;code&gt;go&lt;/code&gt; &lt;code&gt;test&lt;/code&gt; &lt;code&gt;all&lt;/code&gt; more useful than it&#xA;ever has been.&#xA;&lt;a class=anchor href=&#34;#working_outside_gopath&#34;&gt;&lt;h2 id=&#34;working_outside_gopath&#34;&gt;Working outside GOPATH&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If there can be multiple versions of a package&#xA;with a given import path, then it no longer makes sense&#xA;to require the active development version of that&#xA;package to reside in a specific directory.&#xA;What if I need to work on bug fixes for both v1.3 and v1.4&#xA;at the same time?&#xA;Clearly it must be possible to check out modules&#xA;in different locations.&#xA;In fact, at that point there&#39;s no need to work in GOPATH at all.&#xA;&#xA;&lt;p&gt;&#xA;GOPATH was doing three things: it defined the&#xA;versions of dependencies (now in &lt;code&gt;go.mod&lt;/code&gt;),&#xA;it held the source code for those dependencies&#xA;(now in a separate cache), and it provided a way&#xA;to infer the import path for code in a particular&#xA;directory (remove the leading &lt;code&gt;$GOPATH/src&lt;/code&gt;).&#xA;As long as we have some mechanism to decide the import path&#xA;for the code in the current directory, we can stop&#xA;requiring that developers work in GOPATH.&#xA;That mechanism is the &lt;code&gt;go.mod&lt;/code&gt; file&#39;s &lt;code&gt;module&lt;/code&gt; directive.&#xA;If I&#39;m a directory named &lt;code&gt;buggy&lt;/code&gt; and &lt;code&gt;../go.mod&lt;/code&gt; says:&#xA;&lt;pre&gt;module &#34;rsc.io/quote&#34;&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;then my directory&#39;s import path must be &lt;code&gt;rsc.io/quote/buggy&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;vgo&lt;/code&gt; prototype enables work outside GOPATH today,&#xA;as the examples&#xA;in the &lt;a href=&#34;vgo-intro&#34;&gt;overview post&lt;/a&gt; showed.&#xA;In fact, when inferring a &lt;code&gt;go.mod&lt;/code&gt; from other dependency&#xA;information, &lt;code&gt;vgo&lt;/code&gt; will look for import comments&#xA;in the current directory or subdirectories to try to&#xA;get its bearings.&#xA;For example, this worked even before Upspin&#xA;had introduced a &lt;code&gt;go.mod&lt;/code&gt; file:&#xA;&lt;pre&gt;$ cd $HOME&#xA;$ git clone https://github.com/upspin/upspin&#xA;$ cd upspin&#xA;$ vgo test -short ./...&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;vgo&lt;/code&gt; command inferred from import comments that the module&#xA;is named &lt;code&gt;upspin.io&lt;/code&gt;, and it inferred a list of&#xA;dependency version requirements from &lt;code&gt;Gopkg.lock&lt;/code&gt;.&#xA;&lt;a class=anchor href=&#34;#whats_next&#34;&gt;&lt;h2 id=&#34;whats_next&#34;&gt;What&#39;s Next?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This is the last of my initial posts about&#xA;the &lt;code&gt;vgo&lt;/code&gt; design and prototype.&#xA;There is more to work out, but inflicting 67 pages&#xA;of posts on everyone seems like enough for one week.&#xA;&#xA;&lt;p&gt;&#xA;I had planned to post a FAQ today and submit a Go proposal Monday,&#xA;but I will be away next week after Monday.&#xA;Rather than disappear for the first four days of official proposal&#xA;discussion, I think I will post the proposal when I return.&#xA;Please continue to ask questions on the mailing list threads&#xA;or on these posts&#xA;and to try the &lt;code&gt;vgo&lt;/code&gt; prototype.&#xA;&#xA;&lt;p&gt;&#xA;Thanks very much for all your interest and feedback so far.&#xA;It&#39;s very important to me that we all work together&#xA;to produce something that works well for Go developers&#xA;and that is easy for us all to switch to.&#xA;&#xA;&lt;p&gt;&#xA;&lt;b&gt;Update&lt;/b&gt;, March 20, 2018: The official Go proposal is at &lt;a href=&#34;https://golang.org/issue/24301&#34;&gt;https://golang.org/issue/24301&lt;/a&gt;,&#xA;and the second comment on the issue will be the FAQ.&#xA;</content>
</entry>
<entry>
<title>Defining Go Modules</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-module</id>
<link rel="alternate" href="http://research.swtch.com/vgo-module"></link>
<published>2018-02-22T17:00:00-05:00</published>
<updated>2018-02-22T17:02:00-05:00</updated>
<summary type="text">How to specify what&#39;s in a module. (Go &amp; Versioning, Part 6)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;As introduced in the &lt;a href=&#34;vgo-intro&#34;&gt;overview post&lt;/a&gt;, a Go &lt;i&gt;module&lt;/i&gt;&#xA;is a collection of packages versioned as a unit,&#xA;along with a &lt;code&gt;go.mod&lt;/code&gt; file listing other required modules.&#xA;The move to modules is an opportunity for us to revisit and fix&#xA;many details of how the &lt;code&gt;go&lt;/code&gt; command manages source code.&#xA;The current &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; model will be about ten years old when we&#xA;retire it in favor of modules.&#xA;We need to make sure that the module design will serve us&#xA;well for the next decade. In particular:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to encourage more developers to tag releases of their&#xA;packages, instead of expecting that&#xA;users will just pick a commit hash that looks good to them.&#xA;Tagging explicit releases makes clear what is expected to be&#xA;useful to others and what is still under development.&#xA;At the same time, it must still be possible—although maybe not convenient—to request specific&#xA;commits.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to move away from invoking version control&#xA;tools such as &lt;code&gt;bzr&lt;/code&gt;, &lt;code&gt;fossil&lt;/code&gt;, &lt;code&gt;git&lt;/code&gt;, &lt;code&gt;hg&lt;/code&gt;, and &lt;code&gt;svn&lt;/code&gt; to download source code.&#xA;These fragment the ecosystem: packages developed using Bazaar or&#xA;Fossil, for example, are effectively unavailable to users who cannot&#xA;or choose not to install these tools.&#xA;The version control tools have also been a source of &lt;a href=&#34;https://golang.org/issue/22131&#34;&gt;exciting&lt;/a&gt; &lt;a href=&#34;https://www.mercurial-scm.org/wiki/WhatsNew/Archive#Mercurial_3.2.3_.282014-12-18.29&#34;&gt;security&lt;/a&gt; &lt;a href=&#34;https://git-blame.blogspot.com/2014/12/git-1856-195-205-214-and-221-and.html&#34;&gt;problems&lt;/a&gt;.&#xA;It would be good to move them outside the security perimeter.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to allow multiple modules to be developed in a single&#xA;source code repository but versioned independently.&#xA;While most developers will likely keep working with one module per repo,&#xA;larger projects might benefit from having multiple modules in a single repo.&#xA;For example, we&#39;d like to keep &lt;code&gt;golang.org/x/text&lt;/code&gt; a single repository&#xA;but be able to version experimental new packages separately&#xA;from established packages.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to make it easy for individuals and companies to&#xA;put caching proxies in front of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; downloads, whether for availability&#xA;(use a local copy to ensure the download works tomorrow)&#xA;or security&#xA;(vet packages before they can be used inside a company).&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to make it possible, at some future point, to introduce&#xA;a shared proxy for use by the Go community, similar in spirit&#xA;to those used by Rust, Node, and other languages.&#xA;At the same time, the design must work well without assuming&#xA;such a proxy or registry.&#xA;&lt;li&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We want to eliminate vendor directories. They were introduced&#xA;for reproducibility and availability, but we now have better&#xA;mechanisms.&#xA;Reproducibility is handled by proper versioning, and availability&#xA;is handled by caching proxies.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This post presents the parts of the &lt;code&gt;vgo&lt;/code&gt; design that address&#xA;these issues.&#xA;Everything here is preliminary: we will change the design&#xA;if we find that it is not right.&#xA;&lt;a class=anchor href=&#34;#versioned_releases&#34;&gt;&lt;h2 id=&#34;versioned_releases&#34;&gt;Versioned Releases&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Abstraction boundaries let projects scale.&#xA;Originally, all Go packages could be imported by all other Go packages.&#xA;We introduced the &lt;a href=&#34;https://golang.org/s/go14internal&#34;&gt;&lt;code&gt;internal&lt;/code&gt; directory convention&lt;/a&gt;&#xA;in Go 1.4 to eliminate the problem that&#xA;developers who chose to structure a program as multiple packages&#xA;had to worry about other users importing and depending on details of&#xA;helper packages never meant for public use.&#xA;&#xA;&lt;p&gt;&#xA;The Go community has a similar visibility problem now with&#xA;repository commits.&#xA;Today, it&#39;s very common for users to identify package versions&#xA;by commit identifiers (usually Git hashes),&#xA;with the result that developers who structure work as a sequence&#xA;of commits need to worry, at least in the back of their mind,&#xA;about users pinning to any of those commits,&#xA;which again were never meant for public use.&#xA;We need to change the expectations in the Go open source community,&#xA;to establish a norm that authors tag releases and users prefer those.&#xA;&#xA;&lt;p&gt;&#xA;I don&#39;t think this point, that users should be choosing from versions&#xA;issued by authors instead of picking out individual commits&#xA;from the Git history, is particularly controversial.&#xA;The difficult part is shifting the norm.&#xA;We need to make it easy for authors to tag commits&#xA;and easy for users to use those tags.&#xA;&#xA;&lt;p&gt;&#xA;The most common way authors share code today is on code hosting sites,&#xA;especially GitHub.&#xA;For code on GitHub, all authors will need to do is tag a commit&#xA;and push the tag.&#xA;We also plan to provide a tool, maybe called &lt;code&gt;go&lt;/code&gt; &lt;code&gt;release&lt;/code&gt;,&#xA;to compare different versions of a module&#xA;for API compatibility at the type level, to catch inadvertent&#xA;breaking changes that are visible in the type system,&#xA;and also to help authors decide between issuing&#xA;should be a minor release (because it adds new API or changes many lines of code)&#xA;or only a patch release.&#xA;&#xA;&lt;p&gt;&#xA;For users, &lt;code&gt;vgo&lt;/code&gt; itself operates entirely in terms of tagged versions.&#xA;However, we know that at least during the transition from old practices to new,&#xA;and perhaps indefinitely as a way to bootstrap new projects,&#xA;an escape hatch will be necessary, to allow specifying a commit.&#xA;This is possible in &lt;code&gt;vgo&lt;/code&gt;, but it has been designed so as to&#xA;make users prefer explicitly tagged versions.&#xA;&#xA;&lt;p&gt;&#xA;Specifically, &lt;code&gt;vgo&lt;/code&gt; understands the special pseudo-version&#xA;&lt;code&gt;v0.0.0-&lt;/code&gt;&lt;i&gt;yyyymmddhhmmss&lt;/i&gt;&lt;code&gt;-&lt;/code&gt;&lt;i&gt;commit&lt;/i&gt;&#xA;as referring to the given commit identifier,&#xA;which is typically a shortened Git hash&#xA;and which must have a commit time matching the (UTC) timestamp.&#xA;This form is a valid semantic version string&#xA;for a prerelease of v0.0.0.&#xA;For example, this pair of &lt;code&gt;Gopkg.toml&lt;/code&gt; stanzas:&#xA;&lt;pre&gt;[[projects]]&#xA; name = &#34;google.golang.org/appengine&#34;&#xA; packages = [&#xA; &#34;internal&#34;,&#xA; &#34;internal/base&#34;,&#xA; &#34;internal/datastore&#34;,&#xA; &#34;internal/log&#34;,&#xA; &#34;internal/remote_api&#34;,&#xA; &#34;internal/urlfetch&#34;,&#xA; &#34;urlfetch&#34;&#xA; ]&#xA; revision = &#34;150dc57a1b433e64154302bdc40b6bb8aefa313a&#34;&#xA; version = &#34;v1.0.0&#34;&#xA;&#xA;[[projects]]&#xA; branch = &#34;master&#34;&#xA; name = &#34;github.com/google/go-github&#34;&#xA; packages = [&#34;github&#34;]&#xA; revision = &#34;922ceac0585d40f97d283d921f872fc50480e06e&#34;&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;correspond to these &lt;code&gt;go.mod&lt;/code&gt; lines:&#xA;&lt;pre&gt;require (&#xA;&#x9;&#34;google.golang.org/appengine&#34; v1.0.0&#xA;&#x9;&#34;github.com/google/go-github&#34; v0.0.0-20180116225909-922ceac0585d&#xA;)&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The pseudo-version form is chosen so that the standard&#xA;semver precedence rules compare two pseudo-versions by commit time,&#xA;because the timestamp encoding makes string comparison match time comparison.&#xA;The form also ensures that &lt;code&gt;vgo&lt;/code&gt; will always prefer a tagged semantic version&#xA;over an untagged pseudo-version,&#xA;beacuse even if v0.0.1 is very old, it has a greater semver precedence than any v0.0.0 prerelease.&#xA;(Note also that this matches the choice made by &lt;code&gt;dep&lt;/code&gt; when adding a new&#xA;dependency to a project.)&#xA;And of course pseudo-version strings are unwieldy:&#xA;they stand out in &lt;code&gt;go.mod&lt;/code&gt; files and &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; output.&#xA;All these inconveniences help encourage authors and users&#xA;to prefer explicitly tagged versions,&#xA;a bit like the extra step of having to write &lt;code&gt;import&lt;/code&gt; &lt;code&gt;&#34;unsafe&#34;&lt;/code&gt;&#xA;encourages developers to prefer writing safe code.&#xA;&lt;a class=anchor href=&#34;#go.mod_file&#34;&gt;&lt;h2 id=&#34;go.mod_file&#34;&gt;The &lt;code&gt;go.mod&lt;/code&gt; File&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A module version is defined by a tree of source files.&#xA;The &lt;code&gt;go.mod&lt;/code&gt; file describes the module and also indicates the root directory.&#xA;When &lt;code&gt;vgo&lt;/code&gt; is run in a directory, it looks in the current&#xA;directory and then successive parents to find the &lt;code&gt;go.mod&lt;/code&gt;&#xA;marking the root.&#xA;&#xA;&lt;p&gt;&#xA;The file format is line-oriented, with &lt;code&gt;//&lt;/code&gt; comments only.&#xA;Each line holds a single directive, which is a single verb&#xA;(&lt;code&gt;module&lt;/code&gt;, &lt;code&gt;require&lt;/code&gt;, &lt;code&gt;exclude&lt;/code&gt;, or &lt;code&gt;replace&lt;/code&gt;, as defined by&#xA;&lt;a href=&#34;vgo-mvs&#34;&gt;minimum version selection&lt;/a&gt;),&#xA;followed by arguments:&#xA;&lt;pre&gt;module &#34;my/thing&#34;&#xA;require &#34;other/thing&#34; v1.0.2&#xA;require &#34;new/thing&#34; v2.3.4&#xA;exclude &#34;old/thing&#34; v1.2.3&#xA;replace &#34;bad/thing&#34; v1.4.5 =&amp;gt; &#34;good/thing&#34; v1.4.5&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The leading verb can be factored out of adjacent lines,&#xA;leading to a block, like in Go imports:&#xA;&lt;pre&gt;require (&#xA;&#x9;&#34;new/thing&#34; v2.3.4&#xA;&#x9;&#34;old/thing&#34; v1.2.3&#xA;)&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;My goals for the file format were that it be (1) clear and simple,&#xA;(2) easy for people to read, edit, manipulate, and diff,&#xA;(3) easy for programs like &lt;code&gt;vgo&lt;/code&gt; to read, modify, and write back,&#xA;preserving comments and general structure,&#xA;and&#xA;(4) have room for limited future growth.&#xA;I looked at JSON, TOML, XML, and YAML but none of them&#xA;seemed to have those four properties all at once.&#xA;For example, the approach used in &lt;code&gt;Gopkg.toml&lt;/code&gt; above&#xA;leads to three lines for each requirement,&#xA;making them harder to skim, sort, and diff.&#xA;Instead I designed a minimal format reminiscent&#xA;of the top of a Go program, but hopefully not close enough to be&#xA;confusing.&#xA;I adapted an existing comment-friendly parser.&#xA;&#xA;&lt;p&gt;&#xA;The eventual &lt;code&gt;go&lt;/code&gt; command integration may change the&#xA;file format, perhaps even adopting a more standard framing,&#xA;but for compatibility we will keep the ability to read today&#39;s&#xA;&lt;code&gt;go.mod&lt;/code&gt; files, just as &lt;code&gt;vgo&lt;/code&gt; can also read requirement information from&#xA;&lt;code&gt;GLOCKFILE&lt;/code&gt;, &lt;code&gt;Godeps/Godeps.json&lt;/code&gt;, &lt;code&gt;Gopkg.lock&lt;/code&gt;, &lt;code&gt;dependencies.tsv&lt;/code&gt;,&#xA;&lt;code&gt;glide.lock&lt;/code&gt;, &lt;code&gt;vendor.conf&lt;/code&gt;, &lt;code&gt;vendor.yml&lt;/code&gt;, &lt;code&gt;vendor/manifest&lt;/code&gt;,&#xA;and &lt;code&gt;vendor/vendor.json&lt;/code&gt; files.&#xA;&lt;a class=anchor href=&#34;#from_repository_to_modules&#34;&gt;&lt;h2 id=&#34;from_repository_to_modules&#34;&gt;From Repository to Modules&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Developers work in version control systems,&#xA;and clearly &lt;code&gt;vgo&lt;/code&gt; must make that as easy as possible.&#xA;It is not reasonable to expect developers to prepare&#xA;module archives themselves, for example.&#xA;Instead, &lt;code&gt;vgo&lt;/code&gt; makes it easy to export modules&#xA;directly from any version control repository&#xA;following some basic, unobtrusive conventions.&#xA;&#xA;&lt;p&gt;&#xA;To start, it suffices to create a repository&#xA;and tag a commit, using a semver-formatted tag like &lt;code&gt;v0.1.0&lt;/code&gt;.&#xA;The leading &lt;code&gt;v&lt;/code&gt; is required, and having three numbers is also required.&#xA;Although &lt;code&gt;vgo&lt;/code&gt; itself accepts shorthands like &lt;code&gt;v0.1&lt;/code&gt; on the command&#xA;line, the canonical form &lt;code&gt;v0.1.0&lt;/code&gt; must be used&#xA;in repository tags, to avoid ambiguity.&#xA;Only the tag is required.&#xA;In order to use commits made without use of &lt;code&gt;vgo&lt;/code&gt;,&#xA;a &lt;code&gt;go.mod&lt;/code&gt; file is not strictly required at this point.&#xA;Creating new tagged commits creates new module versions.&#xA;Easy.&#xA;&#xA;&lt;p&gt;&#xA;When developers reach v2, semantic import versioning&#xA;means that a &lt;code&gt;/v2/&lt;/code&gt; is added to the import path&#xA;at the end of the module root prefix: &lt;code&gt;my/thing/v2/sub/pkg&lt;/code&gt;.&#xA;There are good reasons for this convention, as described in the &lt;a href=&#34;vgo-import&#34;&gt;earlier post&lt;/a&gt;,&#xA;but it is still a departure from existing tools.&#xA;Realizing this, &lt;code&gt;vgo&lt;/code&gt; will not use any v2 or later tag&#xA;in a source code repository without first checking that it&#xA;has a &lt;code&gt;go.mod&lt;/code&gt; with a module path declaration ending in that&#xA;major version&#xA;(for example, &lt;code&gt;module&lt;/code&gt; &lt;code&gt;&#34;my/thing/v2&#34;&lt;/code&gt;).&#xA;&lt;code&gt;Vgo&lt;/code&gt; uses that declaration as evidence that the author&#xA;is using semantic import versioning&#xA;to name packages within that module.&#xA;This is especially important for multi-package modules,&#xA;since the import paths within the module must&#xA;contain the &lt;code&gt;/v2/&lt;/code&gt; element to avoid referring back to the v1 module.&#xA;&#xA;&lt;p&gt;&#xA;We expect that most developers will prefer to follow&#xA;the usual “major branch” convention,&#xA;in which different major versions live in different branches.&#xA;In this case,&#xA;the root directory in a v2 branch would&#xA;have a &lt;code&gt;go.mod&lt;/code&gt; indicating v2, like this:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;gitmod-1&#34; class=&#34;center pad&#34; width=591 height=416 src=&#34;gitmod-1.png&#34; srcset=&#34;gitmod-1.png 1x, gitmod-1@1.5x.png 1.5x, gitmod-1@2x.png 2x, gitmod-1@3x.png 3x, gitmod-1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;This is roughly how most developers already work.&#xA;In the picture, the v1.0.0 tag points to a commit that predates &lt;code&gt;vgo&lt;/code&gt;.&#xA;It has no &lt;code&gt;go.mod&lt;/code&gt; file at all, and that works fine.&#xA;In the commit tagged v1.0.1,&#xA;the author has added a &lt;code&gt;go.mod&lt;/code&gt; file&#xA;that says &lt;code&gt;module&lt;/code&gt; &lt;code&gt;&#34;my/thing&#34;&lt;/code&gt;.&#xA;After that commit, however, the author forks&#xA;a new v2 development branch.&#xA;In addition to whatever code changes prompted v2&#xA;(including the replacement of &lt;code&gt;bar&lt;/code&gt; with &lt;code&gt;quux&lt;/code&gt;),&#xA;the &lt;code&gt;go.mod&lt;/code&gt; in that new branch is updated&#xA;to say &lt;code&gt;module&lt;/code&gt; &lt;code&gt;&#34;my/thing/v2&#34;&lt;/code&gt;.&#xA;The branches can then proceed independently.&#xA;In truth, &lt;code&gt;vgo&lt;/code&gt; really has no idea about branches.&#xA;It just resolves the tag to a commit and then looks&#xA;at the &lt;code&gt;go.mod&lt;/code&gt; file in the commit.&#xA;Again, the &lt;code&gt;go.mod&lt;/code&gt; file is required for v2 and later&#xA;so that &lt;code&gt;vgo&lt;/code&gt; can use the &lt;code&gt;module&lt;/code&gt; line&#xA;as a sign that the code has been written&#xA;with semantic import versioning in mind,&#xA;so the imports in &lt;code&gt;foo&lt;/code&gt; say &lt;code&gt;my/thing/v2/foo/quux&lt;/code&gt;,&#xA;not &lt;code&gt;my/thing/foo/quux&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;As an alternative, &lt;code&gt;vgo&lt;/code&gt; also supports a&#xA;“major subdirectory” convention,&#xA;in which major versions above v1&#xA;are developed in subdirectories:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;gitmod-2&#34; class=&#34;center pad&#34; width=376 height=768 src=&#34;gitmod-2.png&#34; srcset=&#34;gitmod-2.png 1x, gitmod-2@1.5x.png 1.5x, gitmod-2@2x.png 2x, gitmod-2@3x.png 3x, gitmod-2@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In this case, v2.0.0 is created not by forking the whole tree&#xA;into a separate branch but by copying it into a subdirectory.&#xA;Again the &lt;code&gt;go.mod&lt;/code&gt; must be updated to say&#xA;&lt;code&gt;&#34;my/thing/v2&#34;&lt;/code&gt;.&#xA;Afterward, v1.x.x tags pointing at commits address the files&#xA;in the root directory, excluding &lt;code&gt;v2/&lt;/code&gt;,&#xA;while v2.x.x tags pointing at commits address the files&#xA;in the &lt;code&gt;v2/&lt;/code&gt; subdirectory only.&#xA;The &lt;code&gt;go.mod&lt;/code&gt; file lets &lt;code&gt;vgo&lt;/code&gt; distinguishes the two cases.&#xA;It would also be meaningful to have a v1.x.x and a v2.x.x tag&#xA;pointing at the same commit: they would&#xA;address different subtrees of the commit.&#xA;&#xA;&lt;p&gt;&#xA;We expect that developers may feel strongly about&#xA;choosing one convention or the other.&#xA;Instead of taking sides, &lt;code&gt;vgo&lt;/code&gt; supports both.&#xA;Note that for major versions above v2,&#xA;the major subdirectory approach may&#xA;provide a more graceful transition for users of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;.&#xA;On the other hand, users of &lt;code&gt;dep&lt;/code&gt; or vendoring tools&#xA;should be able to consume repositories using either convention.&#xA;Certainly we will make sure &lt;code&gt;dep&lt;/code&gt; can.&#xA;&lt;a class=anchor href=&#34;#multiple-module_repositories&#34;&gt;&lt;h3 id=&#34;multiple-module_repositories&#34;&gt;Multiple-Module Repositories&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Developers may also find it useful to maintain a collection of&#xA;modules in a single source code repository.&#xA;We want &lt;code&gt;vgo&lt;/code&gt; to support this possibility.&#xA;In general, there is already wide variation in how different&#xA;developers, teams, projects, and companies apply&#xA;source control, and we do not believe it is productive to&#xA;impose a single mapping like “one repository equals one module”&#xA;onto all developers.&#xA;Having some flexibility here should also help &lt;code&gt;vgo&lt;/code&gt; adapt&#xA;as best practices around souce control continue to change.&#xA;&#xA;&lt;p&gt;&#xA;In the major subdirectory convention,&#xA;&lt;code&gt;v2/&lt;/code&gt; contains the module &lt;code&gt;&#34;my/thing/v2&#34;&lt;/code&gt;.&#xA;A natural extension is to allow subdirectories&#xA;not named for major versions.&#xA;For example, we could add a &lt;code&gt;blue/&lt;/code&gt; subdirectory&#xA;that contains the module &lt;code&gt;&#34;my/thing/blue&#34;&lt;/code&gt;,&#xA;confirmed by a &lt;code&gt;blue/go.mod&lt;/code&gt; file with that module path.&#xA;In this case, the source control commit tags addressing that module&#xA;would take the form &lt;code&gt;blue/v1.x.x&lt;/code&gt;.&#xA;Similarly, the tag &lt;code&gt;blue/v2.x.x&lt;/code&gt; would address the &lt;code&gt;blue/v2/&lt;/code&gt; subdirectory.&#xA;The existence of the &lt;code&gt;blue/go.mod&lt;/code&gt; file excludes the &lt;code&gt;blue/&lt;/code&gt; tree&#xA;from the outer &lt;code&gt;my/thing&lt;/code&gt; module.&#xA;&#xA;&lt;p&gt;&#xA;In the Go project, we intend to explore using this convention to allow&#xA;repositories like &lt;code&gt;golang.org/x/text&lt;/code&gt; to define multiple,&#xA;independent modules.&#xA;This lets us retain the convenience of coarse-grained source control&#xA;but still promote different subtrees to v1 at different times.&#xA;&lt;a class=anchor href=&#34;#deprecated_versions&#34;&gt;&lt;h3 id=&#34;deprecated_versions&#34;&gt;Deprecated Versions&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Authors also need to be able to deprecate a version,&#xA;to indicate that it should not be used anymore.&#xA;This is not yet implemented in the &lt;code&gt;vgo&lt;/code&gt; prototype,&#xA;but one way it could work would be to define that&#xA;on code hosting sites, the existence of a tag v1.0.0+deprecated&#xA;(ideally pointing at the same commit as v1.0.0)&#xA;would indicate that the commit is deprecated.&#xA;It is of course important not to remove the tag entirely,&#xA;because that will break builds.&#xA;Deprecated modules would be highlighted in some way&#xA;in &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; output (“show me my modules and information&#xA;about updates”),&#xA;so that users would know to update.&#xA;&#xA;&lt;p&gt;&#xA;Also, because programs will have access to their own module&#xA;lists and versions at runtime, a program could also be configured&#xA;to check its own module versions against some chosen authority&#xA;and self-report in some way when it is running deprecated versions.&#xA;Again, the details here are not worked out,&#xA;but it&#39;s a good example of something that&#39;s possible&#xA;once developers and tools share a vocabulary for&#xA;describing versions.&#xA;&lt;a class=anchor href=&#34;#publishing&#34;&gt;&lt;h3 id=&#34;publishing&#34;&gt;Publishing&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Given a source control repository,&#xA;developers need to be able to&#xA;publish it in a form that &lt;code&gt;vgo&lt;/code&gt; can consume.&#xA;In the general case, we will provide a command that authors run to&#xA;turn their source control repositories into file trees that can be&#xA;served to &lt;code&gt;vgo&lt;/code&gt; by any web server capable of serving static files.&#xA;Similar to current &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;, &lt;code&gt;vgo&lt;/code&gt; expects a page with a &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tag&#xA;to help translate from a module name to the tree of files&#xA;for that module.&#xA;For example, to look up &lt;code&gt;swtch.com/testmod&lt;/code&gt;, the &lt;code&gt;vgo&lt;/code&gt; command&#xA;fetches the usual page:&#xA;&lt;pre&gt;$ curl -sSL &#39;https://swtch.com/testmod?go-get=1&#39;&#xA;&amp;lt;!DOCTYPE html&amp;gt;&#xA;&amp;lt;meta name=&#34;go-import&#34; content=&#34;swtch.com/testmod mod https://storage.googleapis.com/gomodules/rsc&#34;&amp;gt;&#xA;Nothing to see here.&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;mod&lt;/code&gt; server type indicates that modules are served&#xA;in a file tree at that base URL.&#xA;The relevant files at &lt;i&gt;storage.googleapis.com/gomodules/rsc&lt;/i&gt; in this simple case are:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;a href=&#34;https://storage.googleapis.com/gomodules/rsc/swtch.com/testmod/@v/list&#34;&gt;&lt;code&gt;.../swtch.com/testmod/@v/list&lt;/code&gt;&lt;/a&gt;&#xA;&lt;li&gt;&#xA;&lt;a href=&#34;https://storage.googleapis.com/gomodules/rsc/swtch.com/testmod/@v/v1.0.0.info&#34;&gt;&lt;code&gt;.../swtch.com/testmod/@v/v1.0.0.info&lt;/code&gt;&lt;/a&gt;&#xA;&lt;li&gt;&#xA;&lt;a href=&#34;https://storage.googleapis.com/gomodules/rsc/swtch.com/testmod/@v/v1.0.0.mod&#34;&gt;&lt;code&gt;.../swtch.com/testmod/@v/v1.0.0.mod&lt;/code&gt;&lt;/a&gt;&#xA;&lt;li&gt;&#xA;&lt;a href=&#34;https://storage.googleapis.com/gomodules/rsc/swtch.com/testmod/@v/v1.0.0.zip&#34;&gt;&lt;code&gt;.../swtch.com/testmod/@v/v1.0.0.zip&lt;/code&gt;&lt;/a&gt;&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The exact meaning of these URLs is discussed in the “Download Protocol” section later in the post.&#xA;&lt;a class=anchor href=&#34;#code_hosting_sites&#34;&gt;&lt;h3 id=&#34;code_hosting_sites&#34;&gt;Code Hosting Sites&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A huge amount of development happens on code hosting sites,&#xA;and we want that work to integrate into &lt;code&gt;vgo&lt;/code&gt; as smoothly as possible.&#xA;Instead of expecting developers to publish modules elsewhere,&#xA;&lt;code&gt;vgo&lt;/code&gt; will have support for reading the information it needs&#xA;from those sites directly, using their HTTP-based APIs.&#xA;In general, archive downloads can be significantly faster than&#xA;the existing version control checkouts.&#xA;For example, working on a laptop with a gigabit internet connection,&#xA;it takes 10 seconds to download the&#xA;&lt;a href=&#34;https://github.com/cockroachdb/cockroach&#34;&gt;CockroachDB source tree&lt;/a&gt;&#xA;as a zip file from GitHub&#xA;but almost four minutes to &lt;code&gt;git&lt;/code&gt; &lt;code&gt;clone&lt;/code&gt; it.&#xA;Sites need only provide an archive of any form that can be fetched&#xA;with a simple HTTP GET.&#xA;Gerrit servers, for example, only support downloading gzipped tar files.&#xA;&lt;code&gt;Vgo&lt;/code&gt; translates downloaded archives into the standard form.&#xA;&#xA;&lt;p&gt;&#xA;The initial prototype only includes support for GitHub and the Go project&#39;s Gerrit server,&#xA;but we will add support for Bitbucket and other major hosting sites too,&#xA;before shipping anything in the main Go toolchain.&#xA;&#xA;&lt;p&gt;&#xA;With the combination of the lightweight repository conventions,&#xA;which mostly match what developers are already doing,&#xA;and the support for known code hosting sites,&#xA;we expect that most open source activity will be unaffected by&#xA;the move to modules,&#xA;other than simply adding a &lt;code&gt;go.mod&lt;/code&gt; to each repository.&#xA;&#xA;&lt;p&gt;&#xA;Companies taking advantage of old &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;&#39;s direct use of&#xA;&lt;code&gt;git&lt;/code&gt; and other source control tools will need to adjust.&#xA;Perhaps it would make sense to write a proxy that serves&#xA;the &lt;code&gt;vgo&lt;/code&gt; expectations but using version control tools.&#xA;Companies could then run one of those to produce&#xA;an experience much like using the open source hosting sites.&#xA;&lt;a class=anchor href=&#34;#module_archives&#34;&gt;&lt;h2 id=&#34;module_archives&#34;&gt;Module Archives&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The mapping from repositories to modules is a bit complex,&#xA;because the way developers use source control varies.&#xA;The end goal is to map all that complexity down&#xA;into a common, single format for Go modules&#xA;for use by proxies or other code consumers&#xA;(for example, &lt;i&gt;godoc.org&lt;/i&gt; or any code checking tools).&#xA;&#xA;&lt;p&gt;&#xA;The standard format in the &lt;code&gt;vgo&lt;/code&gt; prototype is zip archives&#xA;in which all paths begin&#xA;with the module path and version.&#xA;For example, after running &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; of&#xA;&lt;code&gt;rsc.io/quote&lt;/code&gt; v1.5.2,&#xA;you can find the zip file in &lt;code&gt;vgo&lt;/code&gt;&#39;s download cache:&#xA;&lt;pre&gt;$ unzip -l $GOPATH/src/v/cache/rsc.io/quote/@v/v1.5.2.zip&#xA; 1479 00-00-1980 00:00 rsc.io/quote@v1.5.2/LICENSE&#xA; 131 00-00-1980 00:00 rsc.io/quote@v1.5.2/README.md&#xA; 240 00-00-1980 00:00 rsc.io/quote@v1.5.2/buggy/buggy_test.go&#xA; 55 00-00-1980 00:00 rsc.io/quote@v1.5.2/go.mod&#xA; 793 00-00-1980 00:00 rsc.io/quote@v1.5.2/quote.go&#xA; 917 00-00-1980 00:00 rsc.io/quote@v1.5.2/quote_test.go&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;I used zip because it is well-specified, widely supported, and&#xA;cleanly extensible if needed, and it allows random access to individual files.&#xA;(In contrast, tar files, the other obvious choice, are none of these things and don&#39;t.)&#xA;&lt;a class=anchor href=&#34;#download_protocol&#34;&gt;&lt;h2 id=&#34;download_protocol&#34;&gt;Download Protocol&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;To download information about modules, as well as the modules themselves,&#xA;the &lt;code&gt;vgo&lt;/code&gt; prototype issues only simple HTTP GET requests.&#xA;A key design goal was to make it possible to serve modules from&#xA;static hosting sites, so the requests have no URL query parameters.&#xA;&#xA;&lt;p&gt;&#xA;As we saw earlier, custom domains can specify that a module&#xA;is hosted at a particular base URL.&#xA;As implemented in &lt;code&gt;vgo&lt;/code&gt; today (but, like all of &lt;code&gt;vgo&lt;/code&gt;, subject to change),&#xA;that module-hosting server must serve four request forms:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@v/list&lt;/code&gt; fetches a list of all known versions, one per line.&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@v/&lt;/code&gt;&lt;i&gt;version&lt;/i&gt;&lt;code&gt;.info&lt;/code&gt; fetches JSON-formatted metadata about that version.&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@v/&lt;/code&gt;&lt;i&gt;version&lt;/i&gt;&lt;code&gt;.mod&lt;/code&gt; fetches the &lt;code&gt;go.mod&lt;/code&gt; file for that version.&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@v/&lt;/code&gt;&lt;i&gt;version&lt;/i&gt;&lt;code&gt;.zip&lt;/code&gt; fetches the zip file for that version.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The JSON information served in the &lt;i&gt;version&lt;/i&gt;&lt;code&gt;.info&lt;/code&gt; form will likely evolve,&#xA;but today it corresponds to this struct:&#xA;&lt;pre&gt;type RevInfo struct {&#xA;&#x9;Version string // version string&#xA;&#x9;Name string // complete ID in underlying repository&#xA;&#x9;Short string // shortened ID, for use in pseudo-version&#xA;&#x9;Time time.Time // commit time&#xA;}&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;list&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; command shows the commit time of each available update&#xA;by using the &lt;code&gt;Time&lt;/code&gt; field.&#xA;&#xA;&lt;p&gt;&#xA;A general module-hosting server may optionally respond to &lt;i&gt;version&lt;/i&gt;&lt;code&gt;.info&lt;/code&gt; requests for non-semver versions as well.&#xA;A &lt;code&gt;vgo&lt;/code&gt; command like&#xA;&lt;pre&gt;vgo get my/thing/v2@1459def&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;will fetch &lt;code&gt;1459def.info&lt;/code&gt; and then derive a pseudo-version using the &lt;code&gt;Time&lt;/code&gt; and &lt;code&gt;Short&lt;/code&gt; fields.&#xA;&#xA;&lt;p&gt;&#xA;There are two more optional request forms:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@t/&lt;/code&gt;&lt;i&gt;yyyymmddhhmmss&lt;/i&gt; returns the &lt;code&gt;.info&lt;/code&gt; JSON for the latest version&#xA;at or before the given timestamp.&#xA;&lt;li&gt;&#xA;&lt;code&gt;GET&lt;/code&gt; &lt;i&gt;baseURL&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;module&lt;/i&gt;&lt;code&gt;/@t/&lt;/code&gt;&lt;i&gt;yyyymmddhhmmss&lt;/i&gt;&lt;code&gt;/&lt;/code&gt;&lt;i&gt;branch&lt;/i&gt; does the same, but limiting&#xA;the search to commits on a given branch.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;These support the use of untagged commits in &lt;code&gt;vgo&lt;/code&gt;.&#xA;If &lt;code&gt;vgo&lt;/code&gt; is adding a module and finds no tagged commits at all,&#xA;it uses the first form to find the latest commit as of now.&#xA;It does the same when looking for available updates,&#xA;assuming there are still no tagged commits.&#xA;The branch-limited form is used for the internal simulation of &lt;i&gt;gopkg.in&lt;/i&gt;.&#xA;These forms also support the command line syntaxes:&#xA;&lt;pre&gt;vgo get my/thing/v2@2018-02-01T15:34:45&#xA;vgo get my/thing/v2@2018-02-01T15:34:45@branch&#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;These might be a mistake, but they&#39;re in the prototype today, so I&#39;m mentioning them.&#xA;&lt;a class=anchor href=&#34;#proxy_servers&#34;&gt;&lt;h2 id=&#34;proxy_servers&#34;&gt;Proxy Servers&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Both individuals and companies may prefer to download Go modules from&#xA;proxy servers, whether for efficiency, availability, security, license compliance,&#xA;or any other reason.&#xA;Having a standard Go module format&#xA;and a standard download protocol,&#xA;as described in the last two sections,&#xA;makes it trivial to introduce support for proxies.&#xA;If the &lt;code&gt;$GOPROXY&lt;/code&gt; environment variable is set,&#xA;&lt;code&gt;vgo&lt;/code&gt; fetches all modules from the server at the&#xA;given base URL,&#xA;not from their usual locations.&#xA;For easy debugging, &lt;code&gt;$GOPROXY&lt;/code&gt; can even be a &lt;code&gt;file:///&lt;/code&gt; URL pointing at a local tree.&#xA;&#xA;&lt;p&gt;&#xA;We intend to write a basic proxy server that serves from&#xA;&lt;code&gt;vgo&lt;/code&gt;&#39;s own local cache, downloading new modules as needed.&#xA;Sharing such a proxy among a set of computers would help reduce&#xA;redundant downloads from the proxys users but more importantly&#xA;would ensure future availability, even if the original copies disappear.&#xA;The proxy will also have an option not to allow downloads of new modules.&#xA;In this mode, the proxy would limit the available modules to exactly&#xA;those whitelisted by the proxy administrator.&#xA;Both proxy modes are frequently requested features in corporate environments.&#xA;&#xA;&lt;p&gt;&#xA;Perhaps some day it would make sense to establish a distributed collection&#xA;of proxy servers used by default in &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;, to ensure module availability&#xA;and fast downloads for Go developers worldwide. But not yet.&#xA;Today, we are focused on making sure that &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; works as well as it&#xA;can without assuming any kind of central proxy servers.&#xA;&lt;a class=anchor href=&#34;#end_of_vendoring&#34;&gt;&lt;h2 id=&#34;end_of_vendoring&#34;&gt;The End of Vendoring&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Vendor directories serve two purposes.&#xA;First, they specify by their contents the exact version of the&#xA;dependencies to use during &lt;code&gt;go&lt;/code&gt; &lt;code&gt;build&lt;/code&gt;.&#xA;Second, they ensure the availability of those dependencies,&#xA;even if the original copies disappear.&#xA;On the other hand, vendor directories are also difficult to manage&#xA;and bloat the repositories in which they appear.&#xA;With the &lt;code&gt;go.mod&lt;/code&gt; file specifying the exact version&#xA;of dependencies to use during &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;build&lt;/code&gt;,&#xA;and with proxy servers for ensuring availability,&#xA;vendor directories are now almost entirely redundant.&#xA;They can, however, serve one final purpose:&#xA;to enable a smooth transition to the new versioned world.&#xA;&#xA;&lt;p&gt;&#xA;When building a module, &lt;code&gt;vgo&lt;/code&gt; (and later &lt;code&gt;go&lt;/code&gt;)&#xA;will completely ignore vendored dependencies;&#xA;those dependencies will also not be included in&#xA;the module&#39;s zip file.&#xA;To make it possible for authors to move to &lt;code&gt;vgo&lt;/code&gt; and &lt;code&gt;go.mod&lt;/code&gt;&#xA;while still supporting users who haven&#39;t converted,&#xA;the new &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;vendor&lt;/code&gt; command populates a module&#39;s&#xA;vendor directory with the packages users need&#xA;to reproduce the &lt;code&gt;vgo&lt;/code&gt;-based build.&#xA;&lt;a class=anchor href=&#34;#whats_next&#34;&gt;&lt;h2 id=&#34;whats_next&#34;&gt;What&#39;s Next?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The details here may be revised, but today&#39;s &lt;code&gt;go.mod&lt;/code&gt; files&#xA;will be understood by any future tooling.&#xA;Please start tagging your packages with release tags;&#xA;add &lt;code&gt;go.mod&lt;/code&gt; files if that makes sense for your project.&#xA;&#xA;&lt;p&gt;&#xA;The next post in the series will cover changes to the&#xA;&lt;code&gt;go&lt;/code&gt; tool command line experience.&#xA;</content>
</entry>
<entry>
<title>Reproducible, Verifiable, Verified Builds</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-repro</id>
<link rel="alternate" href="http://research.swtch.com/vgo-repro"></link>
<published>2018-02-21T21:28:00-05:00</published>
<updated>2018-02-21T21:30:00-05:00</updated>
<summary type="text">Consistent builds in versioned Go. (Go &amp; Versioning, Part 5)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;Once both Go developers and tools share a vocabulary around package versions,&#xA;it&#39;s relatively straightforward to add support in the toolchain for&#xA;reproducible, verifiable, and verified builds.&#xA;In fact, the basics are already in the &lt;code&gt;vgo&lt;/code&gt; prototype.&#xA;&#xA;&lt;p&gt;&#xA;Since people sometimes disagree about the exact definitions&#xA;of these terms, let&#39;s establish some basic terminology.&#xA;For this post:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;A &lt;i&gt;reproducible build&lt;/i&gt; is one that,&#xA;when repeated, produces the same result.&#xA;&lt;li&gt;&#xA;A &lt;i&gt;verifiable build&lt;/i&gt; is one that records enough&#xA;information to be precise about exactly how to repeat it.&#xA;&lt;li&gt;&#xA;A &lt;i&gt;verified build&lt;/i&gt; is one that checks that it is using&#xA;the expected source code.&lt;/ul&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;&lt;code&gt;Vgo&lt;/code&gt; delivers reproducible builds by default.&#xA;The resulting binaries are verifiable, in that&#xA;they record versions of the exact source code that went into the build.&#xA;And it is possible to configure your repository so that&#xA;users rebuilding your software verify that their builds&#xA;match yours, using cryptographic hashes,&#xA;no matter how they obtain the dependencies.&#xA;&lt;a class=anchor href=&#34;#reproducible_builds&#34;&gt;&lt;h2 id=&#34;reproducible_builds&#34;&gt;Reproducible Builds&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;At the very least, we want to make sure that when you build my program,&#xA;the build system decides to use the same versions of the code.&#xA;&lt;a href=&#34;vgo-mvs&#34;&gt;Minimal version selection&lt;/a&gt; delivers this property by default.&#xA;The &lt;code&gt;go.mod&lt;/code&gt; file alone is enough to uniquely determine which&#xA;module versions should be used for the build&#xA;(assuming dependencies are available),&#xA;and that decision is stable even as new versions of a module&#xA;are introduced into the ecosystem.&#xA;This differs from most other systems, which adopt new versions&#xA;automatically and need to be constrained to yield&#xA;reproducible builds.&#xA;I covered this in the minimal version selection post,&#xA;but it&#39;s an important, subtle detail, so I&#39;ll try to give a short reprise here.&#xA;&#xA;&lt;p&gt;&#xA;To make this concrete, let&#39;s look at a few real packages from Cargo,&#xA;Rust&#39;s package manager.&#xA;To be clear, I am not picking on Cargo.&#xA;I think Cargo is an example of the current&#xA;state of the art in package managers,&#xA;and there&#39;s much to learn from it.&#xA;If we can make Go package management as smooth as Cargo&#39;s, I&#39;ll be happy.&#xA;But I also think that it is worth exploring whether we would&#xA;benefit from choosing a different default when it comes to&#xA;version selection.&#xA;&#xA;&lt;p&gt;&#xA;Cargo prefers maximum versions in the following sense.&#xA;Over at crates.io, the latest &lt;a href=&#34;https://crates.io/crates/toml&#34;&gt;&lt;code&gt;toml&lt;/code&gt;&lt;/a&gt; is 0.4.5&#xA;as I write this post.&#xA;It lists a dependency on &lt;a href=&#34;https://crates.io/crates/serde&#34;&gt;&lt;code&gt;serde&lt;/code&gt;&lt;/a&gt; 1.0 or later;&#xA;the latest &lt;code&gt;serde&lt;/code&gt; is 1.0.27.&#xA;If you start a new project and add a dependency on&#xA;&lt;code&gt;toml&lt;/code&gt; 0.4.1 or later, Cargo has a choice to make.&#xA;According to the constraints, any of 0.4.1, 0.4.2, 0.4.3, 0.4.4, or 0.4.5 would be acceptable.&#xA;All other things being equal, Cargo prefers the &lt;a href=&#34;cargo-newest.html&#34;&gt;newest acceptable version&lt;/a&gt;, 0.4.5.&#xA;Similarly, any of &lt;code&gt;serde&lt;/code&gt; 1.0.0 through 1.0.27 are acceptable,&#xA;and Cargo chooses 1.0.27.&#xA;These choices change as new versions are introduced.&#xA;If &lt;code&gt;serde&lt;/code&gt; 1.0.28 is released tonight and I add toml 0.4.5&#xA;to a project tomorrow, I&#39;ll get 1.0.28 instead of 1.0.27.&#xA;As described so far, Cargo&#39;s builds are not repeatable.&#xA;Cargo&#39;s (entirely reasonable) answer to this problem is to&#xA;have not just a constraint file (the manifest, &lt;code&gt;Cargo.toml&lt;/code&gt;)&#xA;but also a list of the exact artifacts to use in the build&#xA;(the lock file, &lt;code&gt;Cargo.lock&lt;/code&gt;).&#xA;The lock file stops future upgrades; once it is written,&#xA;your build stays on &lt;code&gt;serde&lt;/code&gt; 1.0.27 even when 1.0.28 is released.&#xA;&#xA;&lt;p&gt;&#xA;In contrast, minimal version selection prefers the minimum allowed version,&#xA;which is the exact version requested by some &lt;code&gt;go.mod&lt;/code&gt; in the project.&#xA;That answer does not change as new versions are added.&#xA;Given the same choices in the Cargo example,&#xA;&lt;code&gt;vgo&lt;/code&gt; would select &lt;code&gt;toml&lt;/code&gt; 0.4.1 (what you requested)&#xA;and then &lt;code&gt;serde&lt;/code&gt; 1.0 (what &lt;code&gt;toml&lt;/code&gt; requested).&#xA;Those choices are stable, without a lock file.&#xA;This is what I mean when I say that &lt;code&gt;vgo&lt;/code&gt;&#39;s builds are reproducible by default.&#xA;&lt;a class=anchor href=&#34;#verifiable_builds&#34;&gt;&lt;h2 id=&#34;verifiable_builds&#34;&gt;Verifiable Builds&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Go binaries have long included a string indicating the version of Go they were built with.&#xA;Last year I wrote a tool &lt;code&gt;rsc.io/goversion&lt;/code&gt; that fetches that&#xA;information from a given executable&#xA;or tree of executables.&#xA;For example, on my Ubuntu Linux laptop,&#xA;I can look to see which system utilities are&#xA;implemented in Go:&#xA;&lt;pre&gt;$ go get -u rsc.io/goversion&#xA;$ goversion /usr/bin&#xA;/usr/bin/containerd go1.8.3&#xA;/usr/bin/containerd-shim go1.8.3&#xA;/usr/bin/ctr go1.8.3&#xA;/usr/bin/go go1.8.3&#xA;/usr/bin/gofmt go1.8.3&#xA;/usr/bin/kbfsfuse go1.8.3&#xA;/usr/bin/kbnm go1.8.3&#xA;/usr/bin/keybase go1.8.3&#xA;/usr/bin/snap go1.8.3&#xA;/usr/bin/snapctl go1.8.3&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Now that the &lt;code&gt;vgo&lt;/code&gt; prototype understands module versions, it includes&#xA;that information in the final binary too,&#xA;and the new &lt;code&gt;goversion&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; flag prints it back out.&#xA;Using our “hello, world” program from the &lt;a href=&#34;vgo-tour&#34;&gt;tour&lt;/a&gt;:&#xA;&lt;pre&gt;$ go get -u rsc.io/goversion&#xA;$ goversion ./hello&#xA;./hello go1.10&#xA;$ goversion -m hello&#xA;./hello go1.10&#xA;&#x9;path github.com/you/hello&#xA;&#x9;mod github.com/you/hello (devel)&#xA;&#x9;dep golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c&#xA;&#x9;dep rsc.io/quote v1.5.2&#xA;&#x9;dep rsc.io/sampler v1.3.0&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The main module, supposedly &lt;code&gt;github.com/you/hello&lt;/code&gt;,&#xA;has no version information, because it&#39;s the local development copy,&#xA;not a specific version we downloaded.&#xA;But if instead we build a command directly from a versioned module,&#xA;then the listing does report versions for all modules:&#xA;&lt;pre&gt;$ vgo build -o hello2 rsc.io/hello&#xA;vgo: resolving import &#34;rsc.io/hello&#34;&#xA;vgo: finding rsc.io/hello (latest)&#xA;vgo: adding rsc.io/hello v1.0.0&#xA;vgo: finding rsc.io/hello v1.0.0&#xA;vgo: finding rsc.io/quote v1.5.1&#xA;vgo: downloading rsc.io/hello v1.0.0&#xA;$ goversion -m ./hello2&#xA;./hello2 go1.10&#xA;&#x9;path rsc.io/hello&#xA;&#x9;mod rsc.io/hello v1.0.0&#xA;&#x9;dep golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c&#xA;&#x9;dep rsc.io/quote v1.5.2&#xA;&#x9;dep rsc.io/sampler v1.3.0&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;When we do integrate versions into the main Go toolchain,&#xA;we will add APIs to access this information from inside a&#xA;running binary, just like &lt;a href=&#34;https://golang.org/pkg/runtime/#Version&#34;&gt;&lt;code&gt;runtime.Version&lt;/code&gt;&lt;/a&gt;&#xA;provides access to the more limited Go version information.&#xA;&#xA;&lt;p&gt;&#xA;For the purpose of attempting to reconstruct the binary,&#xA;the information listed by &lt;code&gt;goversion&lt;/code&gt; &lt;code&gt;-m&lt;/code&gt; suffices:&#xA;put the versions into a &lt;code&gt;go.mod&lt;/code&gt; file and build the target&#xA;named on the &lt;code&gt;path&lt;/code&gt; line.&#xA;But if the result is not the same binary,&#xA;you might wonder about ways to narrow down&#xA;what&#39;s different.&#xA;What changed?&#xA;&#xA;&lt;p&gt;&#xA;When &lt;code&gt;vgo&lt;/code&gt; downloads each module, it computes a hash&#xA;of the file tree corresponding to that module.&#xA;That hash is also included in the binary, alongside the version&#xA;information,&#xA;and &lt;code&gt;goversion&lt;/code&gt; &lt;code&gt;-mh&lt;/code&gt; prints it:&#xA;&lt;pre&gt;$ goversion -mh ./hello&#xA;hello go1.10&#xA;&#x9;path github.com/you/hello&#xA;&#x9;mod github.com/you/hello (devel)&#xA;&#x9;dep golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=&#xA;&#x9;dep rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;&#x9;dep rsc.io/sampler v1.3.1 h1:F0c3J2nQCdk9ODsNhU3sElnvPIxM/xV1c/qZuAeZmac=&#xA;$ goversion -mh ./hello2&#xA;hello go1.10&#xA;&#x9;path rsc.io/hello&#xA;&#x9;mod rsc.io/hello v1.0.0 h1:CDmhdOARcor1WuRUvmE46PK91ahrSoEJqiCbf7FA56U=&#xA;&#x9;dep golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=&#xA;&#x9;dep rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;&#x9;dep rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;h1:&lt;/code&gt; prefix indicates which hash is being reported.&#xA;Today, there is only “hash 1,” a SHA-256 hash of a list of&#xA;files and the SHA-256 hashes of their contents.&#xA;If we need to update to a new hash later, the prefix will help us&#xA;tell old from new hashes.&#xA;&#xA;&lt;p&gt;&#xA;I must stress that these hashes are self-reported by the build system.&#xA;If someone gives you a binary with certain hashes in its build&#xA;information, there&#39;s no guarantee they are accurate.&#xA;They are very useful information supporting a later verification,&#xA;not a signature that can be trusted by themselves.&#xA;&lt;a class=anchor href=&#34;#verified_builds&#34;&gt;&lt;h2 id=&#34;verified_builds&#34;&gt;Verified Builds&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;An author distributing a program in source form&#xA;might want to let users verify that they are building it&#xA;with exactly the expected dependencies.&#xA;We know &lt;code&gt;vgo&lt;/code&gt; will make the same decisions about which&#xA;versions of dependencies to use, but there is still&#xA;the problem of mapping a version like v1.5.2 to&#xA;an actual source tree.&#xA;What if the author of v1.5.2 changes the tag to point&#xA;at a different file tree?&#xA;What if a malicious middlebox intercepts the download&#xA;request and delivers a different zip file?&#xA;What if the user has accidentally edited the source files&#xA;in the local copy of v1.5.2?&#xA;The &lt;code&gt;vgo&lt;/code&gt; prototype supports this kind of verification too.&#xA;&#xA;&lt;p&gt;&#xA;The final form may be somewhat different,&#xA;but if you create a file named &lt;code&gt;go.modverify&lt;/code&gt;&#xA;next to &lt;code&gt;go.mod&lt;/code&gt;, then builds will keep that file up-to-date with&#xA;known hashes for specific versions of modules:&#xA;&lt;pre&gt;$ echo &amp;gt;go.modverify&#xA;$ vgo build&#xA;$ tcat go.modverify # go get rsc.io/tcat, or use cat&#xA;golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=&#xA;rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;go.modverify&lt;/code&gt; file is a log of the hash of all versions ever encountered:&#xA;lines are only added, never removed.&#xA;If we update &lt;code&gt;rsc.io/sampler&lt;/code&gt; to v1.3.1, then the log will now contain hashes for both versions:&#xA;&lt;pre&gt;$ vgo get rsc.io/sampler@v1.3.1&#xA;$ tcat go.modverify&#xA;golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=&#xA;rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=&#xA;rsc.io/sampler v1.3.1 h1:F0c3J2nQCdk9ODsNhU3sElnvPIxM/xV1c/qZuAeZmac=&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;When &lt;code&gt;go.modverify&lt;/code&gt; exists, &lt;code&gt;vgo&lt;/code&gt; checks that all downloaded modules&#xA;used in a given build are consistent with entries already in the file.&#xA;For example, if we change the first digit of the &lt;code&gt;rsc.io/quote&lt;/code&gt; hash from &lt;code&gt;w&lt;/code&gt; to &lt;code&gt;v&lt;/code&gt;:&#xA;&lt;pre&gt;$ vgo build&#xA;vgo: verifying rsc.io/quote v1.5.2: module hash mismatch&#xA;&#x9;downloaded: h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;&#x9;go.modverify: h1:v5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Or suppose we fix that one but then modify the v1.3.0 hash. Now our build succeeds,&#xA;because v1.3.0 is not being used by the build, so its line is (correctly) ignored.&#xA;But if we try to downgrade to v1.3.0, then the build verification will correctly begin failing:&#xA;&lt;pre&gt;$ vgo build&#xA;$ vgo get rsc.io/sampler@v1.3.0&#xA;vgo: verifying rsc.io/sampler v1.3.0: module hash mismatch&#xA;&#x9;downloaded: h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=&#xA;&#x9;go.modverify: h1:8uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Developers who want to ensure that others rebuild their program with exactly&#xA;the same sources they did can store a &lt;code&gt;go.modverify&lt;/code&gt; in their repository.&#xA;Then others building using the same repo will automatically get verified builds.&#xA;For now, only the &lt;code&gt;go.modverify&lt;/code&gt; in the top-level module of the build applies.&#xA;But note that &lt;code&gt;go.modverify&lt;/code&gt; lists all dependencies,&#xA;including indirect dependencies,&#xA;so the whole build is verified.&#xA;&#xA;&lt;p&gt;&#xA;The &lt;code&gt;go.modverify&lt;/code&gt; feature helps detect unexpected mismatches between&#xA;downloaded dependencies on different machines.&#xA;It compares the hashes in &lt;code&gt;go.modverify&lt;/code&gt; against hashes&#xA;computed and saved at module download time.&#xA;It is also useful to check that downloaded modules&#xA;have not changed on the local machine since it was downloaded.&#xA;This is less about security from attacks and more about&#xA;avoiding mistakes.&#xA;For example, because source file paths appear in stack traces,&#xA;it&#39;s common to open those files when debugging.&#xA;If you accidentally (or, I suppose, intentionally)&#xA;modify the file during the debugging&#xA;session, it would be nice to be able to detect that later.&#xA;The &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;verify&lt;/code&gt; command does this:&#xA;&lt;pre&gt;$ go get -u golang.org/x/vgo # fixed a bug, sorry! :-)&#xA;$ vgo verify&#xA;all modules verified&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If a source file changes, &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;verify&lt;/code&gt; notices:&#xA;&lt;pre&gt;$ echo &amp;gt;&amp;gt;$GOPATH/src/v/rsc.io/quote@v1.5.2/quote.go&#xA;$ vgo verify&#xA;rsc.io/quote v1.5.2: dir has been modified (/Users/rsc/src/v/rsc.io/quote@v1.5.2)&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If we restore the file, all is well:&#xA;&lt;pre&gt;$ gofmt -w $GOPATH/src/v/rsc.io/quote@v1.5.2/quote.go&#xA;$ vgo verify&#xA;all modules verified&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;If cached zip files are modified after download,&#xA;&lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;verify&lt;/code&gt; notices that too,&#xA;although I can&#39;t plausibly explain how that might happen:&#xA;&lt;pre&gt;$ zip $GOPATH/src/v/cache/rsc.io/quote/@v/v1.5.2.zip /etc/resolv.conf&#xA; adding: etc/resolv.conf (deflated 36%)&#xA;$ vgo verify&#xA;rsc.io/quote v1.5.2: zip has been modified (/Users/rsc/src/v/cache/rsc.io/quote/@v/v1.5.2.zip)&#xA;$ &#xA;&lt;/pre&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Because &lt;code&gt;vgo&lt;/code&gt; keeps the original zip file after unpacking it,&#xA;if &lt;code&gt;vgo&lt;/code&gt; &lt;code&gt;verify&lt;/code&gt; decides that only one of the zip file and the&#xA;directory tree have been modified, it could even print a diff of the two.&#xA;&lt;a class=anchor href=&#34;#whats_next&#34;&gt;&lt;h2 id=&#34;whats_next&#34;&gt;What&#39;s Next?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;This is implemented already in &lt;code&gt;vgo&lt;/code&gt;.&#xA;You can try it out and use it.&#xA;As with the rest of &lt;code&gt;vgo&lt;/code&gt;, feedback about what doesn&#39;t work right (or works great) is appreciated.&#xA;&#xA;&lt;p&gt;&#xA;The functionality presented here is more the start of something&#xA;than a finished feature.&#xA;A cryptographic hash of the file tree is a building block.&#xA;The &lt;code&gt;go.modverify&lt;/code&gt; built on top of it&#xA;checks that developers all build a particular module&#xA;with precisely the same dependencies,&#xA;but there&#39;s no verification when downloading a new version of a module&#xA;(unless someone else already added it to &lt;code&gt;go.modverify&lt;/code&gt;),&#xA;nor is there any sharing of expected hashes&#xA;between modules.&#xA;&#xA;&lt;p&gt;&#xA;The exact details of how to fix those two shortcomings are not obvious.&#xA;It may make sense to allow some kind of cryptographic signatures&#xA;of the file tree, and to verify that an upgrade finds a version&#xA;signed with the same key as the previous version.&#xA;Or it may make sense to adopt an approach along the lines of&#xA;&lt;a href=&#34;https://theupdateframework.github.io/&#34;&gt;The Update Framework (TUF)&lt;/a&gt;,&#xA;although using their network protocols directly is not practical.&#xA;Or, instead of using per-repo &lt;code&gt;go.modverify&lt;/code&gt; logs,&#xA;it might make sense to establish some kind of shared global log,&#xA;a bit like &lt;a href=&#34;https://www.certificate-transparency.org/&#34;&gt;Certificate Transparency&lt;/a&gt;,&#xA;or to use a public identity server like &lt;a href=&#34;https://upspin.io/&#34;&gt;Upspin&lt;/a&gt;.&#xA;There are many avenues we might explore,&#xA;but all this is getting a little ahead of ourselves.&#xA;For now we are focused on successfully integrating versioning&#xA;into the &lt;code&gt;go&lt;/code&gt; command.&#xA;</content>
</entry>
<entry>
<title>Minimal Version Selection</title>
<id>tag:research.swtch.com,2012:research.swtch.com/vgo-mvs</id>
<link rel="alternate" href="http://research.swtch.com/vgo-mvs"></link>
<published>2018-02-21T16:41:00-05:00</published>
<updated>2018-02-21T16:43:00-05:00</updated>
<summary type="text">How do builds select which versions to use? (Go &amp; Versioning, Part 4)</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;A &lt;a href=&#34;vgo-intro&#34;&gt;versioned Go command&lt;/a&gt; must decide which module versions to use in each build.&#xA;I call this list of modules and versions for use in a given build the &lt;i&gt;build list&lt;/i&gt;.&#xA;For stable development, today&#39;s build list must also be tomorrow&#39;s build list.&#xA;But then developers must also be allowed to change the build list: to upgrade all modules, to upgrade one module, or to downgrade one module.&#xA;&#xA;&lt;p&gt;&#xA;The &lt;i&gt;version selection&lt;/i&gt; problem therefore is to define the meaning of, and to give algorithms implementing, these four operations on build lists:&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;Construct the current build list.&#xA;&lt;li&gt;&#xA;Upgrade all modules to their latest versions.&#xA;&lt;li&gt;&#xA;Upgrade one module to a specific newer version.&#xA;&lt;li&gt;&#xA;Downgrade one module to a specific older version.&lt;/ol&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The last two operations specify one module to upgrade or downgrade, but doing so may require upgrading, downgrading, adding, or removing other modules, ideally as few as possible, to satisfy dependencies.&#xA;&#xA;&lt;p&gt;&#xA;This post presents &lt;i&gt;minimal version selection&lt;/i&gt;, a new, simple approach to the version selection problem.&#xA;Minimal version selection is easy to understand and predict,&#xA;which should make it easy to work with.&#xA;It also produces &lt;i&gt;high-fidelity builds&lt;/i&gt;, in which the dependencies a user builds are as close as possible to the ones the author developed against.&#xA;It is also efficient to implement, using nothing more complex than recursive graph traversals,&#xA;so that a complete minimal version selection implementation in Go is only a few hundred lines of code.&#xA;&#xA;&lt;p&gt;&#xA;Minimal version selection assumes that each module declares its own dependency requirements: a list of minimum versions of other modules. Modules are assumed to follow the &lt;a href=&#34;vgo-import&#34;&gt;import compatibility rule&lt;/a&gt;—packages in any newer version should work as well as older ones—so a dependency requirement gives only a minimum version, never a maximum version or a list of incompatible later versions.&#xA;&#xA;&lt;p&gt;&#xA;Then the definitions of the four operations are:&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&#xA;To construct the build list for a given target: start the list with the target itself, and then append each requirement&#39;s own build list. If a module appears in the list multiple times, keep only the newest version.&#xA;&lt;li&gt;&#xA;To upgrade all modules to their latest versions: construct the build list, but read each requirement as if it requested the latest module version.&#xA;&lt;li&gt;&#xA;To upgrade one module to a specific newer version: construct the non-upgraded build list and then append the new module&#39;s build list. If a module appears in the list multiple times, keep only the newest version.&#xA;&lt;li&gt;&#xA;To downgrade one module to a specific older version: rewind the required version of each top-level requirement until that requirement&#39;s build list no longer refers to newer versions of the downgraded module.&lt;/ol&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;These operations are simple, efficient, and easy to implement.&#xA;&lt;a class=anchor href=&#34;#example&#34;&gt;&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Before we examine minimal version selection in more detail, let&#39;s look at why a new approach is necessary. We&#39;ll use the following set of modules as a running example throughout the post:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-1&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-1.png&#34; srcset=&#34;version-select-1.png 1x, version-select-1@1.5x.png 1.5x, version-select-1@2x.png 2x, version-select-1@3x.png 3x, version-select-1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The diagram shows the module requirement graph for seven modules (dotted boxes) with one or more versions.&#xA;Following semantic versioning, all versions of a given module share a major version number. We are developing module A 1, and we will run commands to update its dependency requirements. The diagram shows both A 1&#39;s current requirements and the requirements declared by various versions of released modules B 1 through F 1.&#xA;&#xA;&lt;p&gt;&#xA;Because the major version is part of the module&#39;s identifier, we must know that we are working on A 1 as opposed to A 2, but otherwise the exact version of A is unspecified—our work is unreleased. Similarly, different major versions are just different modules: for the purposes of these algorithms, B 1 is no more related to B 2 than to C 1. We could replace B 1 through F 1 in the diagram with A 2 through A 7 at a significant loss in clarity but without any change in how the algorithms handle the example.&#xA;Because all the modules in the example do have major version 1,&#xA;from now on we will omit the major version when possible,&#xA;shortening A 1 to A.&#xA;Our current development copy of A requires B 1.2 and C 1.2.&#xA;B 1.2 in turn requires D 1.3. An earlier version, B 1.1, required D 1.1. And so on. Note that F 1.1 requires G 1.1, but G 1.1 also requires F 1.1. Declaring this kind of cycle can be important when singleton functionality moves from one module to another. Our algorithms must not assume the module requirement graph is acyclic.&#xA;&lt;a class=anchor href=&#34;#low-fidelity_builds&#34;&gt;&lt;h2 id=&#34;low-fidelity_builds&#34;&gt;Low-Fidelity Builds&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Go&#39;s current version selection algorithm is simplistic, providing two different version selection algorithms, neither of which is right.&#xA;&#xA;&lt;p&gt;&#xA;The first algorithm is the default behavior of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;: if you have a local version, use that one, or else download and use the latest version.&#xA;This mode can use versions that are too old: if you have B 1.1 installed and run &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; to download A, &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; would not update to B 1.2, causing a failed or buggy build.&#xA;&#xA;&lt;p&gt;&#xA;The second algorithm is the behavior of &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt;: download and use the latest version of everything.&#xA;This mode fails by using versions that are too new: if you run &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; to download A, it will correctly update to B 1.2, but it will also update to C 1.3 and E 1.3, which aren&#39;t what A asks for, may not have been tested, and may not work.&#xA;&#xA;&lt;p&gt;&#xA;I call both these outcomes low-fidelity builds:&#xA;viewed as attempts to reproduce the build that A&#39;s author used,&#xA;these builds differ for no good reason.&#xA;After we&#39;ve seen the details of the minimal version selection algorithms, we&#39;ll look at why they produce high-fidelity builds instead.&#xA;&lt;a class=anchor href=&#34;#algorithms&#34;&gt;&lt;h2 id=&#34;algorithms&#34;&gt;Algorithms&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Now let&#39;s look at the algorithms in more detail.&#xA;&lt;a class=anchor href=&#34;#algorithm_1&#34;&gt;&lt;h3 id=&#34;algorithm_1&#34;&gt;Algorithm 1: Construct Build List&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;There are two useful (and equivalent) ways to define build list construction: as a recursive process and as a graph traversal.&#xA;&#xA;&lt;p&gt;&#xA;The recursive definition of build list construction is as follows.&#xA;Construct the rough build list for M by starting an empty list, adding M, and then appending the build list for each of M&#39;s requirements. Simplify the rough build list to produce the final build list, by keeping only the newest version of any listed module.&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-list&#34; class=&#34;center pad&#34; width=467 height=278 src=&#34;version-select-list.png&#34; srcset=&#34;version-select-list.png 1x, version-select-list@1.5x.png 1.5x, version-select-list@2x.png 2x, version-select-list@3x.png 3x, version-select-list@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;The recursive construction of build lists is useful mainly as a mental model. A literal implementation of that definition would be too inefficient, potentially requiring time exponential in the size of an acyclic module requirement graph and running forever on a cyclic graph.&#xA;&#xA;&lt;p&gt;&#xA;An equivalent, more efficient construction is based on graph reachability. The rough build list for M is also just the list of all modules reachable in the requirement graph starting at M and following arrows. This can be computed by a trivial recursive traversal of the graph, taking care not to visit a node that has already been visited. For example, A&#39;s rough build list is the highlighted module versions found by starting at A and following the highlighted arrows:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-2&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-2.png&#34; srcset=&#34;version-select-2.png 1x, version-select-2@1.5x.png 1.5x, version-select-2@2x.png 2x, version-select-2@3x.png 3x, version-select-2@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;(The simplification from rough build list to final build list remains the same.)&#xA;&#xA;&lt;p&gt;&#xA;Note that this algorithm only visits each module in the rough build list once, and only those modules, so the execution time is proportional to the rough build list size |&lt;i&gt;B&lt;/i&gt;| plus the number of arrows that must be traversed (at most |&lt;i&gt;B&lt;/i&gt;|&lt;sup&gt;2&lt;/sup&gt;).&#xA;The algorithm completely ignores versions left off the rough build list:&#xA;for example, it loads information about D 1.3, D 1.4, and E 1.2, but it does not load information about D 1.2, E 1.1 or E 1.3.&#xA;In a dependency management setting, where loading information about each module version may mean a separate network round trip, avoiding unnecessary module versions is an important optimization.&#xA;&lt;a class=anchor href=&#34;#algorithm_2&#34;&gt;&lt;h3 id=&#34;algorithm_2&#34;&gt;Algorithm 2. Upgrade All Modules&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Upgrading all modules is perhaps the most common modification made to build lists. It is what &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt; does today.&#xA;&#xA;&lt;p&gt;&#xA;We compute an upgraded build list by upgrading the module requirement graph and then applying the previous algorithm.&#xA;An upgraded module requirement graph is one in which every arrow pointing at any version of a module has been replaced by one pointing at the latest version of that module.&#xA;(It is then also possible to discard all older versions from the graph, but the build list construction won&#39;t look at them anyway, so there&#39;s no need to clean up the graph.)&#xA;&#xA;&lt;p&gt;&#xA;For example, here is the upgraded module requirement graph,&#xA;with the original build list still marked in yellow&#xA;and the upgraded build list now marked in red:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-3&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-3.png&#34; srcset=&#34;version-select-3.png 1x, version-select-3@1.5x.png 1.5x, version-select-3@2x.png 2x, version-select-3@3x.png 3x, version-select-3@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Although this tells us the upgraded build list, it does not yet tell us how to cause future builds to use that build list instead of the old build list (still marked in yellow).&#xA;To upgrade the graph we changed the requirements for all modules,&#xA;but an upgrade during development of module A must somehow be recorded only in A&#39;s requirement list&#xA;(in A&#39;s &lt;code&gt;go.mod&lt;/code&gt; file) in a way that causes&#xA;Algorithm 1 to produce the build list we want, to pick the red modules instead of the yellow ones.&#xA;To decide what to add to A&#39;s requirement list to cause that effect, we introduce a helper, Algorithm R.&#xA;&lt;a class=anchor href=&#34;#algorithm_r&#34;&gt;&lt;h3 id=&#34;algorithm_r&#34;&gt;Algorithm R. Compute a Minimal Requirement List&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Given a build list compatible with the module requirement graph below the target, we want to compute a requirement list for the target that will induce that build list.&#xA;It is always sufficient to list every module in the build list other than the target itself. For example, the upgrade we considered above could add C 1.3 (replacing C 1.2), D 1.4, E 1.3, F 1.1, and G 1.1 to A&#39;s requirement list. But in general not all of these additions are necessary, and we want to list as few additional modules as possible. For example, F 1.1 implies G 1.1 (and vice versa), so we need not list both.&#xA;At first glance it seems natural to start by adding the module versions marked in red but not yellow (on the new list but missing from the old list).&#xA;That heuristic would incorrectly drop D 1.4, which is implied by the old requirement C 1.2&#xA;but not by the new requirement C 1.3.&#xA;&#xA;&lt;p&gt;&#xA;Instead, it is correct to visit the modules in reverse postorder—that is, to visit a module only after considering all modules that point into it—and only keep a module if it is not implied by modules already visited.&#xA;For an acyclic graph, the result is a unique, minimal set of additions.&#xA;For a cyclic graph, the reverse-postorder traversal must break cycles, and then the set of additions is unique and minimal for the modules not involved in cycles.&#xA;As long as the result is correct and stable, we&#39;ll accept non-minimal answers in the case of cycles.&#xA;In this example, the upgrade needs to add C 1.3 (replacing C 1.2), D 1.4, and E 1.3. It can drop F 1.1 (implied by C 1.3) and G 1.1 (also implied by C 1.3).&#xA;&lt;a class=anchor href=&#34;#algorithm_3&#34;&gt;&lt;h3 id=&#34;algorithm_3&#34;&gt;Algorithm 3. Upgrade One Module&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Instead of upgrading all modules, cautious developers typically want to upgrade only one module, with as few other changes to the build list as possible. For example, we may want to upgrade to C 1.3, and we do not want that operation to make unnecessary changes like upgrading to E 1.3.&#xA;Like in Algorithm 2, we can upgrade one module by upgrading the requirement graph, constructing a build list from it (Algorithm 1), and then reducing that list back to a set of requirements for the top-level module (Algorithm R).&#xA;To upgrade the requirement graph, we add one new arrow from the top-level module to the upgraded module version.&#xA;&#xA;&lt;p&gt;&#xA;For example, if we want to change A&#39;s build to upgrade to C 1.3, here is the upgraded requirement graph:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-4&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-4.png&#34; srcset=&#34;version-select-4.png 1x, version-select-4@1.5x.png 1.5x, version-select-4@2x.png 2x, version-select-4@3x.png 3x, version-select-4@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;Like before, the new build list&#39;s modules are marked in red, and the old build list&#39;s are in yellow.&#xA;&#xA;&lt;p&gt;&#xA;The upgrade&#39;s effect on the build list is the unique minimal way to make the upgrade, adding the new module version and any implied requirements but nothing else.&#xA;Note that when constructing the upgraded graph, we must only add new arrows, not replace or remove old ones.&#xA;For example, if the new arrow from A to C 1.3 replaced the old arrow from A to C 1.2, the upgraded build list would omit D 1.4. That is, the upgrade of C would downgrade D, an unexpected, unwanted, and non-minimal change.&#xA;Once we&#39;ve computed the build list for the upgrade, we can run Algorithm R (above) to decide how to update the requirements list. In this case we&#39;d end up replacing C 1.2 with C 1.3 but then also adding a new requirement on D 1.4, to avoid the accidental downgrade of D.&#xA;Note that this selective upgrade only updates other modules to C&#39;s minimum requirements: the upgrade of C does not simply fetch the latest of each of C&#39;s dependencies.&#xA;&lt;a class=anchor href=&#34;#algorithm_4&#34;&gt;&lt;h3 id=&#34;algorithm_4&#34;&gt;Algorithm 4. Downgrade One Module&lt;/h3&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;We may also discover, perhaps after upgrading all modules, that the latest module version is buggy and must be avoided. In that situation, we need to be able to downgrade to an earlier version of the module. Downgrading one module may require downgrading other modules, but we want to downgrade as few other modules as possible.&#xA;Like upgrades, downgrades must make their changes to the build list by modifying a target&#39;s requirements list. Unlike upgrades, downgrades must work by removing requirements, not adding them.&#xA;This observation leads to a very simple downgrade algorithm that considers each of the target&#39;s requirements individually.&#xA;If a requirement is incompatible with the proposed downgrade—that is, if the requirement&#39;s build list includes a now-disallowed module version—then try successively older versions until finding one that is compatible with the downgrade.&#xA;&#xA;&lt;p&gt;&#xA;For example, starting with the original build graph, suppose we discover that there is a problem with D 1.4, actually introduced in D 1.3, and so we decide to downgrade to D 1.2. Our target module A depends on B 1.2 and C 1.2. To downgrade from D 1.4 to D 1.2, we must find earlier versions of B and C that do not require (directly or indirectly) versions of D later than D 1.2.&#xA;&#xA;&lt;p&gt;&#xA;Although we can consider each requirement separately, it is more efficient to consider the module requirement graph as a whole. In our example, the downgrade rule amounts to crossing out the unavailable versions of D and then following arrows backwards from unavailable modules to find and cross out other unavailable modules. At the end, the latest versions of A&#39;s requirements that remain can be recorded as the new requirements.&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-5&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-5.png&#34; srcset=&#34;version-select-5.png 1x, version-select-5@1.5x.png 1.5x, version-select-5@2x.png 2x, version-select-5@3x.png 3x, version-select-5@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In this case, downgrading to D 1.2 implies downgrading to B 1.1 and C 1.1. To avoid an unnecessary downgrade to E 1.1, we must also add a new requirement on E 1.2. We can apply Algorithm R to find the minimal set of new requirements to write to &lt;code&gt;go.mod&lt;/code&gt;.&#xA;&#xA;&lt;p&gt;&#xA;Note that if we&#39;d first upgraded to C 1.3, then the downgrade to D 1.2 would have continued to use C 1.3, which doesn&#39;t use any version of D at all. But downgrades are constrained to only downgrade packages, not also upgrade them; if an upgrade before downgrade is needed, the user must ask for it explicitly.&#xA;&lt;a class=anchor href=&#34;#theory&#34;&gt;&lt;h2 id=&#34;theory&#34;&gt;Theory&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Minimal version selection is &lt;i&gt;very&lt;/i&gt; simple. It achieves simplicity by eliminating all flexibility about what the answer must be: the build list is exactly the versions specified in the requirements.&#xA;A real system needs more flexibility,&#xA;for example the ability to exclude certain module versions or replace others.&#xA;Before we add those, it is worth examining the theoretical basis for the current system&#39;s simplicity, so we understand which kinds of extensions preserve that simplicity and which do not.&#xA;&#xA;&lt;p&gt;&#xA;If you are familiar with the way most other systems approach version selection, or if you remember my &lt;a href=&#34;version-sat&#34;&gt;Version SAT&lt;/a&gt; post from a year ago, probably the most striking feature of Minimal version selection is that it does not solve general Boolean satisfiability, or SAT.&#xA;As I explained in my earlier post,&#xA;it takes very little for a version search to fall into solving SAT; version searches in these systems are inherently intricate, complex problems for which we know no general efficient solutions.&#xA;If we want to avoid this fate, we need to know where the boundaries are, where not to step as we explore the design space.&#xA;Conveniently,&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/Schaefer%27s_dichotomy_theorem&#34;&gt;Schaefer&#39;s Dichotomy Theorem&lt;/a&gt; describes those boundaries precisely. It identifies six restricted classes of Boolean formulas for which satisfiability can be decided in polynomial time and then proves that for any class of formulas beyond those, satisfiability is NP-complete. To avoid NP-completeness, we need to limit the version selection problem to stay within one of Schaefer&#39;s restricted classes.&#xA;&#xA;&lt;p&gt;&#xA;It turns out that minimal version selection lies in the intersection of three of the six tractable SAT subproblems: 2-SAT, Horn-SAT, and Dual-Horn-SAT.&#xA;The formula corresponding to a build in minimal version selection is the AND of a set of clauses, each of which is either&#xA;a single positive literal (this version must be installed, such as during an upgrade),&#xA;a single negative literal (this version is not available, such as during a downgrade),&#xA;or the OR of one negative and one positive literal (an implication: if this version is installed, this other version must also be installed).&#xA;The formula is a 2-CNF formula, because each clause has at most two variables.&#xA;The formula is also a Horn formula, because each clause has at most one positive literal.&#xA;The formula is also a dual-Horn formula, because each clause has at most one negative literal.&#xA;That is, every satisfiability problem posed by minimal version selection can be solved by your choice of three different efficient algorithms.&#xA;It is even simpler and more efficient to specialize further,&#xA;as we did above, taking advantage of the very limited structure of these problems.&#xA;&#xA;&lt;p&gt;&#xA;Although 2-SAT is the most well-known example of a SAT subproblem with an efficient solution, the fact that these problems are both Horn and dual-Horn formulas is more interesting.&#xA;Every Horn formula has a unique satisfying assignment with the fewest variables set to true.&#xA;This proves that there is a unique minimal answer for constructing a build list,&#xA;as well for each upgrade.&#xA;The unique minimal upgrade does not use a newer version of a given module unless absolutely necessary.&#xA;Conversely, every dual-Horn formula also has a unique satisfying assignment with the fewest variables set to &lt;i&gt;false&lt;/i&gt;.&#xA;This proves that there is a unique minimal answer for each downgrade.&#xA;The unique minimal downgrade does not use an older version of a given module unless absolutely necessary.&#xA;If we want to extend minimal version selection, for example with the ability to exclude certain modules, we can only keep the uniqueness and mimimality properties by continuing to use constraints expressible as both Horn and dual-Horn formulas.&#xA;&#xA;&lt;p&gt;&#xA;(Digression: The problem minimal version selection solves is &lt;a href=&#34;https://en.wikipedia.org/wiki/NL-complete&#34;&gt;NL-complete&lt;/a&gt;: it&#39;s in NL because it&#39;s a subset of 2-SAT, and it&#39;s NL-hard because st-connectivity can be trivially transformed into a minimal version selection build list construction problem. It&#39;s delightful that we&#39;ve replaced an NP-complete problem with an NL-complete problem, but there&#39;s little practical value to knowing that: being in NL only guarantees a polynomial-time solution, and we already have a linear-time one.)&#xA;&lt;a class=anchor href=&#34;#excluding_modules&#34;&gt;&lt;h2 id=&#34;excluding_modules&#34;&gt;Excluding Modules&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Minimal version selection always selects the minimal (oldest) module version that satisfies the overall requirements of a build. If that version is buggy in some way, an upgrade or downgrade operation can modify the top-level target&#39;s requirements list to force selection of a different version.&#xA;&#xA;&lt;p&gt;&#xA;It can also be useful to record explicitly that the version is buggy, to avoid reintroducing it in any future upgrade or downgrade operations.&#xA;But we want to do that in a way that keeps the uniqueness and minimality properties of the previous section,&#xA;so we must use constraints that are both Horn and dual-Horn formulas.&#xA;That means build constraints can only be&#xA;unconditional positive assertions (X: X must be installed), unconditional negative assertions (¬Y: Y must not be installed), and&#xA;positive implications (X → Z, equivalently ¬X Z: if X is installed, then Z must be installed).&#xA;Negative implications (X → ¬Y, equivalently ¬X ¬Y: if X is installed, then Y must &lt;i&gt;not&lt;/i&gt; be installed)&#xA;cannot be added as constraints without breaking the form.&#xA;Module exclusions must therefore be unconditional:&#xA;they must be decided independent of selections&#xA;made during build list construction.&#xA;&#xA;&lt;p&gt;&#xA;What we &lt;i&gt;can&lt;/i&gt; do is allow a module to declare its own &lt;i&gt;local&lt;/i&gt; list of excluded module versions.&#xA;By local, I mean that the list is consulted only for builds within that module;&#xA;a larger build using the module only as a dependency would ignore the exclusion list.&#xA;In our example, if A&#39;s build consulted D 1.3&#39;s list,&#xA;then the exact set of exclusions would depend on whether the build selected, say, D 1.3 or D 1.4,&#xA;making the exclusions conditional and leading to an NP-complete search problem.&#xA;Only the top-level module is guaranteed to be in the build,&#xA;so only the top-level module&#39;s exclusion list is used.&#xA;Note that it would be fine to consult exclusion lists from other sources, such as a global exclusion list loaded over the network, as long as the decision to use the list is made before the build begins and the list content does not depend on which modules are selected during the build.&#xA;&#xA;&lt;p&gt;&#xA;Despite all the focus on making exclusions unconditional,&#xA;it might seem like we already have conditional exclusions:&#xA;C 1.2 requires D 1.4 and so implicitly excludes D 1.3.&#xA;But our algorithms do not treat this as an exclusion.&#xA;When Algorithm 1 runs,&#xA;it adds both D 1.3 (for B) and D 1.4 (for C) to the rough build list,&#xA;along with their minimum requirements.&#xA;The final simplification pass removes D 1.3 only because D 1.4 is present.&#xA;The difference here between declaring an incompatibility&#xA;and declaring a minimum requirement is critical.&#xA;Declaring that C 1.2 must not be built with D 1.3&#xA;only describes how to fail.&#xA;Declaring that C 1.2 must be built with D 1.4&#xA;instead describes how to succeed.&#xA;&#xA;&lt;p&gt;&#xA;Exclusions then must be unconditional.&#xA;Knowing that fact is important, but it does not tell us exactly how to implement exclusions.&#xA;A simple answer is to add exclusions as the build constraints,&#xA;with clauses like “D 1.3 must not be installed.”&#xA;Unfortunately, adding that clause alone would make&#xA;modules that require D 1.3, like B 1.2, uninstallable.&#xA;We need to express somehow that B 1.2 can choose D 1.4.&#xA;The simple way to do that is to revise the build&#xA;constraint, changing “B 1.2 → D 1.3” to “B 1.2 → D 1.3 D 1.4” and in general allowing all future versions of D.&#xA;But that clause (equivalently, ¬B 1.2 D 1.3 D 1.4)&#xA;has two positive literals,&#xA;making the overall build formula not a Horn formula anymore.&#xA;It is still a dual-Horn formula, so we can still define&#xA;a linear-time build list construction,&#xA;but that construction—and therefore the question&#xA;of how to perform an upgrade—would no longer be&#xA;guaranteed to have a unique, minimal answer.&#xA;&#xA;&lt;p&gt;&#xA;Instead of implementing exclusions as new build constraints,&#xA;we can implement them by changing existing ones.&#xA;That is, we can modify the requirements graph,&#xA;just as we did for upgrades and downgrades.&#xA;If a specific module is excluded, then we can remove&#xA;it from the module requirement graph but also change any&#xA;existing requirements on that module to require&#xA;the next newer version instead.&#xA;For example, if we excluded D 1.3,&#xA;then we&#39;d also update B 1.2 to require D 1.4:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-6&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-6.png&#34; srcset=&#34;version-select-6.png 1x, version-select-6@1.5x.png 1.5x, version-select-6@2x.png 2x, version-select-6@3x.png 3x, version-select-6@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;If the latest version of a module is removed,&#xA;then any modules requiring that version also need&#xA;to be removed, as in the downgrade algorithm.&#xA;For example, if G 1.1 were removed,&#xA;then C 1.3 would need to be removed as well.&#xA;&#xA;&lt;p&gt;&#xA;Once the exclusions have been applied to the module requirement graph, the algorithms proceed as before.&#xA;&lt;a class=anchor href=&#34;#replacing_modules&#34;&gt;&lt;h2 id=&#34;replacing_modules&#34;&gt;Replacing Modules&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;During development of A, suppose we find a bug in D 1.4, and we want to test a potential fix.&#xA;We need some way to replace D 1.4 in our build with an unreleased copy U.&#xA;We can allow a module to declare this as a replacement:&#xA;“proceed as if D 1.4&#39;s source code and requirements have been replaced by U&#39;s.”&#xA;&#xA;&lt;p&gt;&#xA;Like exclusions, replacements can be implemented by modifying the module requirement graph in a preprocessing step,&#xA;not by adding complexity to the algorithms that process the graph.&#xA;Also like exclusions, the replacement list is local to one module.&#xA;The build of A consults the replacement list from A but not from B 1.2, C 1.2, or any of the other modules in the build.&#xA;This avoids making replacements conditional, which would be difficult to implement, and it also avoids the possibility of conflicting replacements: what if B 1.2 and C 1.2 specify different replacements for E 1.2?&#xA;More generally, keeping exclusions and replacements local to one module limits the control that module exerts on other builds.&#xA;&lt;a class=anchor href=&#34;#who_controls_your_build&#34;&gt;&lt;h2 id=&#34;who_controls_your_build&#34;&gt;Who Controls Your Build?&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;The dependencies of a top-level module must be given some control over the top-level build.&#xA;B 1.2 needs to be able to make sure it is built with D 1.3 or later, not with D 1.2.&#xA;Otherwise we end up with the current &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt;&#39;s stale dependency failure mode.&#xA;&#xA;&lt;p&gt;&#xA;At the same time, for builds to remain predictable and understandable, we cannot give dependencies arbitrary, fine-grained control over the top-level build.&#xA;That leads to conflicts and surprises.&#xA;For example, suppose B declares that it requires an even version of D, while C declares that it requires a prime version of D.&#xA;D is frequently updated and is up to D 1.99.&#xA;Using B or C in isolation, it&#39;s always possible to use a relatively recent version of D (D 1.98 or D 1.97, respectively).&#xA;But when A uses both B and C,&#xA;the build silently selects the much older (and buggier) D 1.2 instead.&#xA;That&#39;s an extreme example, but it raises the question:&#xA;why should the authors of B and C be given such extreme control&#xA;over A&#39;s build?&#xA;As I write this post, there is an &lt;a href=&#34;https://github.com/kubernetes/client-go/issues/325&#34;&gt;open bug report&lt;/a&gt; that the Kubernetes Go client declares a requirement on a specific, two-year-old version of &lt;code&gt;gopkg.in/yaml.v2&lt;/code&gt;.&#xA;When a developer tried to use&#xA;a new feature of that YAML library in a program that already&#xA;used the Kubernetes Go client,&#xA;even after attempting to upgrade to the latest possible version,&#xA;code using the new feature failed to compile,&#xA;because “latest” had been constrained by the Kubernetes requirement.&#xA;In this case, the use of a two-year-old YAML library version may be entirely reasonable within the context of the Kubernetes code base,&#xA;and clearly the Kubernetes authors should have complete&#xA;control over their own builds,&#xA;but that level of control does not make sense to extend to other developers&#39; builds.&#xA;&#xA;&lt;p&gt;&#xA;In the design of module requirements, exclusions,&#xA;and replacements, I&#39;ve tried to balance the competing concerns&#xA;of allowing dependencies enough control to ensure a succesful&#xA;build without allowing them so much control that they harm the build.&#xA;Minimum requirements combine without conflict,&#xA;so it is feasible (even easy) to gather them from all dependencies.&#xA;But exclusions and replacements can and do conflict,&#xA;so we allow them to be specified only by the top-level module.&#xA;&#xA;&lt;p&gt;&#xA;A module author is therefore in complete control of&#xA;that module&#39;s build when it is the main program being built,&#xA;but not in complete control of other users&#39; builds that depend on the module.&#xA;I believe this distinction will make minimal version selection&#xA;scale to much larger, more distributed code bases than&#xA;existing systems.&#xA;&lt;a class=anchor href=&#34;#high-fidelity_builds&#34;&gt;&lt;h2 id=&#34;high-fidelity_builds&#34;&gt;High-Fidelity Builds&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Let&#39;s return now to the question of high-fidelity builds.&#xA;&#xA;&lt;p&gt;&#xA;At the start of the post we saw that, using &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; to build A, it was possible to use dependencies different than the ones A&#39;s author had used, without a good reason.&#xA;I called this as a low-fidelity build, because it is a poor reproduction of the original build of A.&#xA;Using minimal version selection, builds are instead high-fidelity. The module requirements, which are included with the module&#39;s source code, uniquely determine how to build it directly.&#xA;The user&#39;s build of A will match the author&#39;s build exactly:&#xA;a reproducible build.&#xA;But high-fidelity means more.&#xA;&#xA;&lt;p&gt;&#xA;Having a reproducible build is generally understood to be a binary property, for a whole-program build: a user&#39;s build is exactly the same the author&#39;s, or it isn&#39;t.&#xA;What about when building a library module as part of a larger program?&#xA;It would be helpful for a user&#39;s build of a library to match the author&#39;s whenever possible.&#xA;Then the user runs the same code (including dependencies) that the author developed and tested with.&#xA;In a larger project, of course, it may be impossible for a user&#39;s build of a library to match the author&#39;s build exactly.&#xA;Another part of that build may force the use of a newer dependency, making the user&#39;s build of the library deviate from the author&#39;s build.&#xA;Let&#39;s refer to a build as high-fidelity when it deviates from the author&#39;s own build only to satisfy a requirement elsewhere in the build.&#xA;&#xA;&lt;p&gt;&#xA;Consider again our original example:&#xA;&#xA;&lt;p&gt;&#xA;&lt;img name=&#34;version-select-1&#34; class=&#34;center pad&#34; width=463 height=272 src=&#34;version-select-1.png&#34; srcset=&#34;version-select-1.png 1x, version-select-1@1.5x.png 1.5x, version-select-1@2x.png 2x, version-select-1@3x.png 3x, version-select-1@4x.png 4x&#34;&gt;&#xA;&#xA;&lt;p&gt;&#xA;In this example, the build of A combines B 1.2 and D 1.4,&#xA;even though B&#39;s author was using D 1.3.&#xA;That change is necessary because A also uses C 1.2,&#xA;which requires D 1.4.&#xA;The build of A is still a high-fidelity build of B 1.2:&#xA;it deviates by using D 1.4, but only because it must.&#xA;In contrast, if the build used E 1.3,&#xA;as &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt;, Dep, and Cargo typically do,&#xA;that build would be low-fidelity: it deviates unnecessarily.&#xA;&#xA;&lt;p&gt;&#xA;Minimal version selection provides high-fidelity builds&#xA;by using the oldest version available that meets the requirements.&#xA;The release of a new version has no effect on the build.&#xA;In contrast, most other systems, including Cargo and Dep,&#xA;use the &lt;a href=&#34;cargo-newest.html&#34;&gt;newest version available&lt;/a&gt; that meets requirements listed&#xA;in a “manifest file.”&#xA;The release of a new version changes their build decisions.&#xA;To get reproducible builds, these systems add a second mechanism,&#xA;the “lock file,”&#xA;which lists the specific versions a build should use.&#xA;The lock file ensures reproducible builds for whole programs,&#xA;but it is ignored for library modules; the &lt;a href=&#34;http://doc.crates.io/faq.html#why-do-binaries-have-cargolock-in-version-control-but-not-libraries&#34;&gt;Cargo FAQ explains&lt;/a&gt; that this is “precisely because a library should &lt;b&gt;not&lt;/b&gt; be deterministically recompiled for all users of the library.”&#xA;It&#39;s true that a perfect reproduction is not always possible,&#xA;but by giving up entirely, the Cargo approach admits unnecessary deviation from the library author&#39;s builds.&#xA;That is, it delivers low-fidelity builds.&#xA;In our example, when A first adds B 1.2 or C 1.2 to its build, Cargo will see that they require E 1.2 or later and will choose E 1.3.&#xA;Until directed otherwise, however, it seems better to continue to build with&#xA;E 1.2, as the authors of B and C did.&#xA;Using the oldest allowed version also eliminates the redundancy of having&#xA;two different files (manifest and lock) that both specify which modules versions to use.&#xA;&#xA;&lt;p&gt;&#xA;Automatically using newer versions&#xA;also makes it easy for minimum requirements to be wrong.&#xA;Suppose we start working on A using B 1.1, the latest version at the time, and we record that A requires only B 1.1.&#xA;But then B 1.2 comes out and we start using it in our own builds and lock file, without updating the manifest.&#xA;At this point there is no longer any development or testing of A with B 1.1.&#xA;We may start using features or depending on bug fixes from B 1.2,&#xA;but now A incorrectly lists its minimum requirement as B 1.1.&#xA;If users always also choose newer versions than the minimum requirement, then there is not much harm done: they&#39;ll use B 1.2 as well.&#xA;But when the system does try to use the declared minimum, it will break.&#xA;For example, when a user attempts a limited update of A, the system cannot see that updating to B 1.2 is also required.&#xA;More generally, whenever the minimum versions (in the manifest) and the built versions (in the lock file) differ,&#xA;why should we believe that building with the minimum versions&#xA;will produce a working library?&#xA;To try to detect this problem, &lt;a href=&#34;https://github.com/rust-lang/cargo/issues/4100&#34;&gt;Cargo developers have proposed&lt;/a&gt; that&#xA;&lt;code&gt;cargo&lt;/code&gt; &lt;code&gt;publish&lt;/code&gt; try a build with the minimum versions of all dependencies before publishing.&#xA;That will detect when A starts using a new feature in B 1.2—building with B 1.1 will fail—but it will not&#xA;detect when A starts depending on a new bug fix.&#xA;&#xA;&lt;p&gt;&#xA;The fundamental problem is that preferring the newest allowed version of a module during version selection produces a low-fidelity build. Lock files are a partial solution, targeting whole-program builds; additional build checks like in &lt;code&gt;cargo&lt;/code&gt; &lt;code&gt;publish&lt;/code&gt; are also a partial solution. A more complete solution is to use the version of the module the author did.&#xA;That makes a user&#39;s build as close as possible to the author&#39;s build: a high-fidelity build.&#xA;&lt;a class=anchor href=&#34;#upgrade_speed&#34;&gt;&lt;h2 id=&#34;upgrade_speed&#34;&gt;Upgrade Speed&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;Given that minimal version selection takes the minimum allowed version&#xA;of each dependency, it&#39;s easy to think that this would lead to use of very&#xA;old copies of packages, which in turn might lead to unnecessary bugs or&#xA;security problems.&#xA;In practice, however, I think the opposite will happen, because&#xA;the minimum allowed version is the &lt;i&gt;maximum&lt;/i&gt; of all the constraints,&#xA;so the one lever of control made available to all modules in a build is the ability&#xA;to force the use of a newer version of a dependency than would otherwise be used.&#xA;I expect that users of minimal version selection will end up with&#xA;programs that are almost as up-to-date as their friends using more aggressive&#xA;systems like Cargo.&#xA;&#xA;&lt;p&gt;&#xA;For example, suppose you are writing a program that depends on a handful of other modules,&#xA;all of which depend on some very common module, like &lt;code&gt;gopkg.in/yaml.v2&lt;/code&gt;.&#xA;Your program&#39;s build will use the &lt;i&gt;newest&lt;/i&gt; YAML version among the ones requested&#xA;by your module and that handful of dependencies.&#xA;Even just one conscientious dependency can force your build to update&#xA;many other dependencies.&#xA;This is the opposite of the Kubernetes Go client problem I mentioned earlier.&#xA;&#xA;&lt;p&gt;&#xA;If anything, minimal version selection would instead suffer the opposite problem,&#xA;that this “max of the minimums” answer serves as a ratchet that&#xA;forces dependencies forward too quickly.&#xA;But I think in practice dependencies will move forward at just the right speed,&#xA;which ends up being just the right amount slower than Cargo and friends.&#xA;&lt;a class=anchor href=&#34;#upgrade_timing&#34;&gt;&lt;h2 id=&#34;upgrade_timing&#34;&gt;Upgrade Timing&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;A key feature of minimal version selection is that upgrade do not&#xA;happen until a developer asks for them to happen.&#xA;You don&#39;t get an untested version of a module unless&#xA;you asked for that module to be upgraded.&#xA;&#xA;&lt;p&gt;&#xA;For example, in Cargo, if package B depends on package C 2.9&#xA;and you add B to your build, you don&#39;t get C 2.9.&#xA;You get the &lt;a href=&#34;cargo-newest.html&#34;&gt;latest allowed version&lt;/a&gt; at that moment, maybe C 2.15.&#xA;Maybe C 2.15 was released just a few minutes ago and the author&#xA;hasn&#39;t yet been told about an important bug.&#xA;That&#39;s too bad for you and your build.&#xA;On the other hand, in minimal version selection, module B&#39;s &lt;code&gt;go.mod&lt;/code&gt; file&#xA;will list the exact version of C that B&#39;s author developed and tested with.&#xA;You&#39;ll get that version.&#xA;Or maybe some other module in your program developed and tested&#xA;with a newer version of C.&#xA;Then you&#39;ll get that version.&#xA;But you will never get a version of C&#xA;that some module in the program&#xA;did not explicitly request in its &lt;code&gt;go.mod&lt;/code&gt; file.&#xA;This should mean you only ever get a version of C that&#xA;worked for someone else, not the very latest version that maybe&#xA;hasn&#39;t worked for anyone.&#xA;&#xA;&lt;p&gt;&#xA;To be clear, my purpose here is not to pick on Cargo,&#xA;which I think is a very well-designed system.&#xA;I&#39;m using Cargo here as an example of a model that many developers are&#xA;familiar with, to try to convey what would be different in&#xA;minimal version selection.&#xA;&lt;a class=anchor href=&#34;#minimality&#34;&gt;&lt;h2 id=&#34;minimality&#34;&gt;Minimality&lt;/h2&gt;&lt;/a&gt;&#xA;&#xA;&#xA;&lt;p&gt;&#xA;I call this system minimal version selection because the system as a whole appears to be minimal:&#xA;I don&#39;t see how to remove anything more without breaking it.&#xA;Some people will undoubtedly say that too much has been removed already, but so far it seems perfectly able to handle the real-world&#xA;cases I&#39;ve examined.&#xA;We&#39;ll find out more by experimenting with the &lt;code&gt;vgo&lt;/code&gt; prototype.&#xA;&#xA;&lt;p&gt;&#xA;The key to minimal version selection is its preference for the minimum allowed version of a module.&#xA;When I compared &lt;code&gt;go&lt;/code&gt; &lt;code&gt;get&lt;/code&gt; &lt;code&gt;-u&lt;/code&gt;&#39;s “upgrade everything to latest” approach&#xA;to Cargo&#39;s “manifest and lock” approach&#xA;in the context of a system that can rely on the &lt;a href=&#34;vgo-import&#34;&gt;import compatibility rule&lt;/a&gt;,&#xA;I realized that both manifest and lock exist for the same purpose:&#xA;to work around the “upgrade everything to latest” default behavior.&#xA;The manifest describes which newer versions are unneeded,&#xA;and the lock describes which newer versions are unwanted.&#xA;Instead, why not change the default?&#xA;Use the minimum version allowed,&#xA;typically the exact version the author used,&#xA;and leave timing of upgrades completely to user control.&#xA;This approach leads to reproducible builds without lock files,&#xA;and more generally to high-fidelity builds&#xA;that deviate from the author&#39;s&#xA;own build only when required.&#xA;&#xA;&lt;p&gt;&#xA;More than anything else, I wanted to find a version selection algorithm that was understandable. Predictable. Boring.&#xA;Where other systems instead seem to optimize for displays of raw flexibility and power, minimal version selection aims to be invisible.&#xA;I hope it succeeds.&#xA;</content>
</entry>
<entry>
<title>Go and Dogma</title>
<id>tag:research.swtch.com,2012:research.swtch.com/dogma</id>
<link rel="alternate" href="http://research.swtch.com/dogma"></link>
<published>2017-01-09T09:00:00-05:00</published>
<updated>2017-01-09T09:02:00-05:00</updated>
<summary type="text">Programming language dogmatics.</summary>
<content type="html">&#xA;&#xA;&lt;p&gt;&#xA;[&lt;i&gt;Cross-posting from last years &lt;a href=&#34;https://www.reddit.com/r/golang/comments/46bd5h/ama_we_are_the_go_contributors_ask_us_anything/d05yyde/?context=3&amp;st=ixq5hjko&amp;sh=7affd469&#34;&gt;Go contributors AMA&lt;/a&gt; on Reddit, because its still important to remember.&lt;/i&gt;]&#xA;&#xA;&lt;p&gt;&#xA;One of the perks of working on Go these past years has been the chance to have many great discussions with other language designers and implementers, for example about how well various design decisions worked out or the common problems of implementing what look like very different languages (for example both Go and Haskell need some kind of “green threads”, so there are more shared runtime challenges than you might expect). In one such conversation, when I was talking to a group of early Lisp hackers, one of them pointed out that these discussions are basically never dogmatic. Designers and implementers remember working through the good arguments on both sides of a particular decision, and theyre often eager to hear about someone elses experience with what happens when you make that decision differently. Contrast that kind of discussion with the heated arguments or overly zealous statements you sometimes see from users of the same languages. Theres a real disconnect, possibly because the users dont have the experience of weighing the arguments on both sides and dont realize how easily a particular decision might have gone the other way.&#xA;&#xA;&lt;p&gt;&#xA;Language design and implementation is engineering. We make decisions using evaluations of costs and benefits or, if we must, using predictions of those based on past experience. I think we have an important responsibility to explain both sides of a particular decision, to make clear that the arguments for an alternate decision are actually good ones that we weighed and balanced, and to avoid the suggestion that particular design decisions approach dogma. I hope &lt;a href=&#34;https://www.reddit.com/r/golang/comments/46bd5h/ama_we_are_the_go_contributors_ask_us_anything/d05yyde/?context=3&amp;st=ixq5hjko&amp;sh=7affd469&#34;&gt;the Reddit AMA&lt;/a&gt; as well as discussion on &lt;a href=&#34;https://groups.google.com/group/golang-nuts&#34;&gt;golang-nuts&lt;/a&gt; or &lt;a href=&#34;http://stackoverflow.com/questions/tagged/go&#34;&gt;StackOverflow&lt;/a&gt; or the &lt;a href=&#34;https://forum.golangbridge.org/&#34;&gt;Go Forum&lt;/a&gt; or at &lt;a href=&#34;https://golang.org/wiki/Conferences&#34;&gt;conferences&lt;/a&gt; help with that.&#xA;&#xA;&lt;p&gt;&#xA;But we need help from everyone. Remember that none of the decisions in Go are infallible; theyre just our best attempts at the time we made them, not wisdom received on stone tablets. If someone asks why Go does X instead of Y, please try to present the engineering reasons fairly, including for Y, and avoid argument solely by appeal to authority. Its too easy to fall into the “well thats just not how its done here” trap. And now that I know about and watch for that trap, I see it in nearly every technical community, although some more than others.&#xA;</content>
</entry>
<entry>
<title>A Tour of Acme</title>
<id>tag:research.swtch.com,2012:research.swtch.com/acme</id>
<link rel="alternate" href="http://research.swtch.com/acme"></link>
<published>2012-09-17T11:00:00-04:00</published>
<updated>2012-09-17T11:00:00-04:00</updated>
<summary type="text">A video introduction to Acme, the Plan 9 text editor</summary>
<content type="html">&#xA;&lt;p class=&#34;lp&#34;&gt;&#xA;People I work with recognize my computer easily:&#xA;it&#39;s the one with nothing but yellow windows and blue bars on the screen.&#xA;That&#39;s the text editor acme, written by Rob Pike for Plan 9 in the early 1990s.&#xA;Acme focuses entirely on the idea of text as user interface.&#xA;It&#39;s difficult to explain acme without seeing it, though, so I&#39;ve put together&#xA;a screencast explaining the basics of acme and showing a brief programming session.&#xA;Remember as you watch the video that the 854x480 screen is quite cramped.&#xA;Usually you&#39;d run acme on a larger screen: even my MacBook Air has almost four times&#xA;as much screen real estate.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;div style=&#34;border: 1px solid black; width: 853px; height: 480px;&#34;&gt;&lt;iframe width=&#34;853&#34; height=&#34;480&#34; src=&#34;https://www.youtube.com/embed/dP1xVpMPn8M?rel=0&#34; frameborder=&#34;0&#34; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The video doesn&#39;t show everything acme can do, nor does it show all the ways you can use it.&#xA;Even small idioms like where you type text to be loaded or executed vary from user to user.&#xA;To learn more about acme, read Rob Pike&#39;s paper &amp;ldquo;&lt;a href=&#34;/acme.pdf&#34;&gt;Acme: A User Interface for Programmers&lt;/a&gt;&amp;rdquo; and then try it.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Acme runs on most operating systems.&#xA;If you use &lt;a href=&#34;http://plan9.bell-labs.com/plan9/&#34;&gt;Plan 9 from Bell Labs&lt;/a&gt;, you already have it.&#xA;If you use FreeBSD, Linux, OS X, or most other Unix clones, you can get it as part of &lt;a href=&#34;http://swtch.com/plan9port/&#34;&gt;Plan 9 from User Space&lt;/a&gt;.&#xA;If you use Windows, I suggest trying acme as packaged in &lt;a href=&#34;http://code.google.com/p/acme-sac/&#34;&gt;acme stand alone complex&lt;/a&gt;, which is based on the Inferno programming environment.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=lp&gt;&lt;b&gt;Mini-FAQ&lt;/b&gt;:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;i&gt;Q. Can I use scalable fonts?&lt;/i&gt; A. On the Mac, yes. If you run &lt;code&gt;acme -f /mnt/font/Monaco/16a/font&lt;/code&gt; you get 16-point anti-aliased Monaco as your font, served via &lt;a href=&#34;http://swtch.com/plan9port/man/man4/fontsrv.html&#34;&gt;fontsrv&lt;/a&gt;. If you&#39;d like to add X11 support to fontsrv, I&#39;d be happy to apply the patch.&#xA;&lt;li&gt;&lt;i&gt;Q. Do I need X11 to build on the Mac?&lt;/i&gt; A. No. The build will complain that it cannot build &amp;lsquo;snarfer&amp;rsquo; but it should complete otherwise. You probably don&#39;t need snarfer.&#xA;&lt;/ul&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;If you&#39;re interested in history, the predecessor to acme was called help. Rob Pike&#39;s paper &amp;ldquo;&lt;a href=&#34;/help.pdf&#34;&gt;A Minimalist Global User Interface&lt;/a&gt;&amp;rdquo; describes it. See also &amp;ldquo;&lt;a href=&#34;/sam.pdf&#34;&gt;The Text Editor sam&lt;/a&gt;&amp;rdquo;&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;&lt;i&gt;Correction&lt;/i&gt;: the smiley program in the video was written by Ken Thompson.&#xA;I got it from Dennis Ritchie, the more meticulous archivist of the pair.&#xA;&lt;/p&gt;&#xA;&#xA;</content>
</entry>
<entry>
<title>Minimal Boolean Formulas</title>
<id>tag:research.swtch.com,2012:research.swtch.com/boolean</id>
<link rel="alternate" href="http://research.swtch.com/boolean"></link>
<published>2011-05-18T00:00:00-04:00</published>
<updated>2011-05-18T00:00:00-04:00</updated>
<summary type="text">Simplify equations with God</summary>
<content type="html">&#xA;&lt;p&gt;&lt;style type=&#34;text/css&#34;&gt;&#xA;p { line-height: 150%; }&#xA;blockquote { text-align: left; }&#xA;pre.alg { font-family: sans-serif; font-size: 100%; margin-left: 60px; }&#xA;td, th { padding-left; 5px; padding-right: 5px; vertical-align: top; }&#xA;#times td { text-align: right; }&#xA;table { padding-top: 1em; padding-bottom: 1em; }&#xA;#find td { text-align: center; }&#xA;&lt;/style&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;&lt;a href=&#34;http://oeis.org/A056287&#34;&gt;28&lt;/a&gt;. &#xA;That&#39;s the minimum number of AND or OR operators&#xA;you need in order to write any Boolean function of five variables.&#xA;&lt;a href=&#34;http://alexhealy.net/&#34;&gt;Alex Healy&lt;/a&gt; and I computed that in April 2010. Until then,&#xA;I believe no one had ever known that little fact.&#xA;This post describes how we computed it&#xA;and how we almost got scooped by &lt;a href=&#34;http://research.swtch.com/2011/01/knuth-volume-4a.html&#34;&gt;Knuth&#39;s Volume 4A&lt;/a&gt;&#xA;which considers the problem for AND, OR, and XOR.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;A Naive Brute Force Approach&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Any Boolean function of two variables&#xA;can be written with at most 3 AND or OR operators: the parity function&#xA;on two variables X XOR Y is (X AND Y&#39;) OR (X&#39; AND Y), where X&#39; denotes&#xA;&amp;ldquo;not X.&amp;rdquo; We can shorten the notation by writing AND and OR&#xA;like multiplication and addition: X XOR Y = X*Y&#39; + X&#39;*Y.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;For three variables, parity is also a hardest function, requiring 9 operators:&#xA;X XOR Y XOR Z = (X*Z&#39;+X&#39;*Z+Y&#39;)*(X*Z+X&#39;*Z&#39;+Y).&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;For four variables, parity is still a hardest function, requiring 15 operators:&#xA;W XOR X XOR Y XOR Z = (X*Z&#39;+X&#39;*Z+W&#39;*Y+W*Y&#39;)*(X*Z+X&#39;*Z&#39;+W*Y+W&#39;*Y&#39;).&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The sequence so far prompts a few questions. Is parity always a hardest function?&#xA;Does the minimum number of operators alternate between 2&lt;sup&gt;n&lt;/sup&gt;&amp;#8722;1 and 2&lt;sup&gt;n&lt;/sup&gt;+1?&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;I computed these results in January 2001 after hearing&#xA;the problem from Neil Sloane, who suggested it as a variant&#xA;of a similar problem first studied by Claude Shannon.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The program I wrote to compute a(4) computes the minimum number of&#xA;operators for every Boolean function of n variables&#xA;in order to find the largest minimum over all functions.&#xA;There are 2&lt;sup&gt;4&lt;/sup&gt; = 16 settings of four variables, and each function&#xA;can pick its own value for each setting, so there are 2&lt;sup&gt;16&lt;/sup&gt; different&#xA;functions. To make matters worse, you build new functions&#xA;by taking pairs of old functions and joining them with AND or OR.&#xA;2&lt;sup&gt;16&lt;/sup&gt; different functions means 2&lt;sup&gt;16&lt;/sup&gt;&amp;#183;2&lt;sup&gt;16&lt;/sup&gt; = 2&lt;sup&gt;32&lt;/sup&gt; pairs of functions.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The program I wrote was a mangling of the Floyd-Warshall&#xA;all-pairs shortest paths algorithm. That algorithm is:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Floyd-Warshall all pairs shortest path&#xA;func compute():&#xA; for each node i&#xA; for each node j&#xA; dist[i][j] = direct distance, or &amp;#8734;&#xA; &#xA; for each node k&#xA; for each node i&#xA; for each node j&#xA; d = dist[i][k] + dist[k][j]&#xA; if d &amp;lt; dist[i][j]&#xA; dist[i][j] = d&#xA; return&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The algorithm begins with the distance table dist[i][j] set to&#xA;an actual distance if i is connected to j and infinity otherwise.&#xA;Then each round updates the table to account for paths&#xA;going through the node k: if it&#39;s shorter to go from i to k to j,&#xA;it saves that shorter distance in the table. The nodes are &#xA;numbered from 0 to n, so the variables i, j, k are simply integers.&#xA;Because there are only n nodes, we know we&#39;ll be done after&#xA;the outer loop finishes.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The program I wrote to find minimum Boolean formula sizes is&#xA;an adaptation, substituting formula sizes for distance.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Algorithm 1&#xA;func compute()&#xA; for each function f&#xA; size[f] = &amp;#8734;&#xA; &#xA; for each single variable function f = v&#xA; size[f] = 0&#xA; &#xA; loop&#xA; changed = false&#xA; for each function f&#xA; for each function g&#xA; d = size[f] + 1 + size[g]&#xA; if d &amp;lt; size[f OR g]&#xA; size[f OR g] = d&#xA; changed = true&#xA; if d &amp;lt; size[f AND g]&#xA; size[f AND g] = d&#xA; changed = true&#xA; if not changed&#xA; return&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Algorithm 1 runs the same kind of iterative update loop as the Floyd-Warshall algorithm,&#xA;but it isn&#39;t as obvious when you can stop, because you don&#39;t&#xA;know the maximum formula size beforehand.&#xA;So it runs until a round doesn&#39;t find any new functions to make,&#xA;iterating until it finds a fixed point.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The pseudocode above glosses over some details, such as&#xA;the fact that the per-function loops can iterate over a&#xA;queue of functions known to have finite size, so that each&#xA;loop omits the functions that aren&#39;t&#xA;yet known. That&#39;s only a constant factor improvement,&#xA;but it&#39;s a useful one.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Another important detail missing above&#xA;is the representation of functions. The most convenient&#xA;representation is a binary truth table.&#xA;For example,&#xA;if we are computing the complexity of two-variable functions,&#xA;there are four possible inputs, which we can number as follows.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&lt;th&gt;X &lt;th&gt;Y &lt;th&gt;Value&#xA;&lt;tr&gt;&lt;td&gt;false &lt;td&gt;false &lt;td&gt;00&lt;sub&gt;2&lt;/sub&gt; = 0&#xA;&lt;tr&gt;&lt;td&gt;false &lt;td&gt;true &lt;td&gt;01&lt;sub&gt;2&lt;/sub&gt; = 1&#xA;&lt;tr&gt;&lt;td&gt;true &lt;td&gt;false &lt;td&gt;10&lt;sub&gt;2&lt;/sub&gt; = 2&#xA;&lt;tr&gt;&lt;td&gt;true &lt;td&gt;true &lt;td&gt;11&lt;sub&gt;2&lt;/sub&gt; = 3&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The functions are then the 4-bit numbers giving the value of the&#xA;function for each input. For example, function 13 = 1101&lt;sub&gt;2&lt;/sub&gt;&#xA;is true for all inputs except X=false Y=true.&#xA;Three-variable functions correspond to 3-bit inputs generating 8-bit truth tables,&#xA;and so on.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;This representation has two key advantages. The first is that &#xA;the numbering is dense, so that you can implement a map keyed&#xA;by function using a simple array. The second is that the operations&#xA;&amp;ldquo;f AND g&amp;rdquo; and &amp;ldquo;f OR g&amp;rdquo; can be implemented using&#xA;bitwise operators: the truth table for &amp;ldquo;f AND g&amp;rdquo; is the bitwise&#xA;AND of the truth tables for f and g.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;That program worked well enough in 2001 to compute the&#xA;minimum number of operators necessary to write any&#xA;1-, 2-, 3-, and 4-variable Boolean function. Each round&#xA;takes asymptotically O(2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt;&amp;#183;2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt;) = O(2&lt;sup&gt;2&lt;sup&gt;n+1&lt;/sup&gt;&lt;/sup&gt;) time, and the number of&#xA;rounds needed is O(the final answer). The answer for n=4&#xA;is 15, so the computation required on the order of&#xA;15&amp;#183;2&lt;sup&gt;2&lt;sup&gt;5&lt;/sup&gt;&lt;/sup&gt; = 15&amp;#183;2&lt;sup&gt;32&lt;/sup&gt; iterations of the innermost loop.&#xA;That was plausible on the computer I was using at&#xA;the time, but the answer for n=5, likely around 30,&#xA;would need 30&amp;#183;2&lt;sup&gt;64&lt;/sup&gt; iterations to compute, which&#xA;seemed well out of reach.&#xA;At the time, it seemed plausible that parity was always&#xA;a hardest function and that the minimum size would&#xA;continue to alternate between 2&lt;sup&gt;n&lt;/sup&gt;&amp;#8722;1 and 2&lt;sup&gt;n&lt;/sup&gt;+1.&#xA;It&#39;s a nice pattern.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Exploiting Symmetry&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Five years later, though, Alex Healy and I got to talking about this sequence,&#xA;and Alex shot down both conjectures using results from the theory&#xA;of circuit complexity. (Theorists!) Neil Sloane added this note to&#xA;the &lt;a href=&#34;http://oeis.org/history?seq=A056287&#34;&gt;entry for the sequence&lt;/a&gt; in his Online Encyclopedia of Integer Sequences:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;&lt;tt&gt;&#xA;%E A056287 Russ Cox conjectures that X&lt;sub&gt;1&lt;/sub&gt; XOR ... XOR X&lt;sub&gt;n&lt;/sub&gt; is always a worst f and that a(5) = 33 and a(6) = 63. But (Jan 27 2006) Alex Healy points out that this conjecture is definitely false for large n. So what is a(5)?&#xA;&lt;/tt&gt;&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Indeed. What is a(5)? No one knew, and it wasn&#39;t obvious how to find out.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;In January 2010, Alex and I started looking into ways to &#xA;speed up the computation for a(5). 30&amp;#183;2&lt;sup&gt;64&lt;/sup&gt; is too many&#xA;iterations but maybe we could find ways to cut that number.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;In general, if we can identify a class of functions f whose&#xA;members are guaranteed to have the same complexity,&#xA;then we can save just one representative of the class as&#xA;long as we recreate the entire class in the loop body.&#xA;What used to be:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each function f&#xA; for each function g&#xA; visit f AND g&#xA; visit f OR g&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;can be rewritten as&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each canonical function f&#xA; for each canonical function g&#xA; for each ff equivalent to f&#xA; for each gg equivalent to g&#xA; visit ff AND gg&#xA; visit ff OR gg&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That doesn&#39;t look like an improvement: it&#39;s doing all&#xA;the same work. But it can open the door to new optimizations&#xA;depending on the equivalences chosen.&#xA;For example, the functions &amp;ldquo;f&amp;rdquo; and &amp;ldquo;&amp;#172;f&amp;rdquo; are guaranteed&#xA;to have the same complexity, by &lt;a href=&#34;http://en.wikipedia.org/wiki/De_Morgan&#39;s_laws&#34;&gt;DeMorgan&#39;s laws&lt;/a&gt;.&#xA;If we keep just one of &#xA;those two on the lists that &amp;ldquo;for each function&amp;rdquo; iterates over,&#xA;we can unroll the inner two loops, producing:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each canonical function f&#xA; for each canonical function g&#xA; visit f OR g&#xA; visit f AND g&#xA; visit &amp;#172;f OR g&#xA; visit &amp;#172;f AND g&#xA; visit f OR &amp;#172;g&#xA; visit f AND &amp;#172;g&#xA; visit &amp;#172;f OR &amp;#172;g&#xA; visit &amp;#172;f AND &amp;#172;g&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That&#39;s still not an improvement, but it&#39;s no worse.&#xA;Each of the two loops considers half as many functions&#xA;but the inner iteration is four times longer.&#xA;Now we can notice that half of tests aren&#39;t&#xA;worth doing: &amp;ldquo;f AND g&amp;rdquo; is the negation of&#xA;&amp;ldquo;&amp;#172;f OR &amp;#172;g,&amp;rdquo; and so on, so only half&#xA;of them are necessary.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Let&#39;s suppose that when choosing between &amp;ldquo;f&amp;rdquo; and &amp;ldquo;&amp;#172;f&amp;rdquo;&#xA;we keep the one that is false when presented with all true inputs.&#xA;(This has the nice property that &lt;code&gt;f ^ (int32(f) &amp;gt;&amp;gt; 31)&lt;/code&gt;&#xA;is the truth table for the canonical form of &lt;code&gt;f&lt;/code&gt;.)&#xA;Then we can tell which combinations above will produce&#xA;canonical functions when f and g are already canonical:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each canonical function f&#xA; for each canonical function g&#xA; visit f OR g&#xA; visit f AND g&#xA; visit &amp;#172;f AND g&#xA; visit f AND &amp;#172;g&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That&#39;s a factor of two improvement over the original loop.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Another observation is that permuting&#xA;the inputs to a function doesn&#39;t change its complexity:&#xA;&amp;ldquo;f(V, W, X, Y, Z)&amp;rdquo; and &amp;ldquo;f(Z, Y, X, W, V)&amp;rdquo; will have the same&#xA;minimum size. For complex functions, each of the &#xA;5! = 120 permutations will produce a different truth table.&#xA;A factor of 120 reduction in storage is good but again&#xA;we have the problem of expanding the class in the&#xA;iteration. This time, there&#39;s a different trick for reducing&#xA;the work in the innermost iteration.&#xA;Since we only need to produce one member of&#xA;the equivalence class, it doesn&#39;t make sense to&#xA;permute the inputs to both f and g. Instead,&#xA;permuting just the inputs to f while fixing g&#xA;is guaranteed to hit at least one member &#xA;of each class that permuting both f and g would.&#xA;So we gain the factor of 120 twice in the loops&#xA;and lose it once in the iteration, for a net savings&#xA;of 120.&#xA;(In some ways, this is the same trick we did with &amp;ldquo;f&amp;rdquo; vs &amp;ldquo;&amp;#172;f.&amp;rdquo;)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;A final observation is that negating any of the inputs&#xA;to the function doesn&#39;t change its complexity,&#xA;because X and X&#39; have the same complexity.&#xA;The same argument we used for permutations applies&#xA;here, for another constant factor of 2&lt;sup&gt;5&lt;/sup&gt; = 32.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The code stores a single function for each equivalence class&#xA;and then recomputes the equivalent functions for f, but not g.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each canonical function f&#xA; for each function ff equivalent to f&#xA; for each canonical function g&#xA; visit ff OR g&#xA; visit ff AND g&#xA; visit &amp;#172;ff AND g&#xA; visit ff AND &amp;#172;g&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;In all, we just got a savings of 2&amp;#183;120&amp;#183;32 = 7680,&#xA;cutting the total number of iterations from 30&amp;#183;2&lt;sup&gt;64&lt;/sup&gt; = 5&amp;#215;10&lt;sup&gt;20&lt;/sup&gt;&#xA;to 7&amp;#215;10&lt;sup&gt;16&lt;/sup&gt;. If you figure we can do around &#xA;10&lt;sup&gt;9&lt;/sup&gt; iterations per second, that&#39;s still 800 days of CPU time.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The full algorithm at this point is:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Algorithm 2&#xA;func compute():&#xA; for each function f&#xA; size[f] = &amp;#8734;&#xA; &#xA; for each single variable function f = v&#xA; size[f] = 0&#xA; &#xA; loop&#xA; changed = false&#xA; for each canonical function f&#xA; for each function ff equivalent to f&#xA; for each canonical function g&#xA; d = size[ff] + 1 + size[g]&#xA; changed |= visit(d, ff OR g)&#xA; changed |= visit(d, ff AND g)&#xA; changed |= visit(d, ff AND &amp;#172;g)&#xA; changed |= visit(d, &amp;#172;ff AND g)&#xA; if not changed&#xA; return&#xA;&#xA;func visit(d, fg):&#xA; if size[fg] != &amp;#8734;&#xA; return false&#xA; &#xA; record fg as canonical&#xA;&#xA; for each function ffgg equivalent to fg&#xA; size[ffgg] = d&#xA; return true&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The helper function &amp;ldquo;visit&amp;rdquo; must set the size not only of its argument fg&#xA;but also all equivalent functions under permutation or inversion of the inputs,&#xA;so that future tests will see that they have been computed.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Methodical Exploration&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;There&#39;s one final improvement we can make.&#xA;The approach of looping until things stop changing&#xA;considers each function pair multiple times&#xA;as their sizes go down. Instead, we can consider functions&#xA;in order of complexity, so that the main loop&#xA;builds first all the functions of minimum complexity 1,&#xA;then all the functions of minimum complexity 2,&#xA;and so on. If we do that, we&#39;ll consider each function pair at most once.&#xA;We can stop when all functions are accounted for.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Applying this idea to Algorithm 1 (before canonicalization) yields:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Algorithm 3&#xA;func compute()&#xA; for each function f&#xA; size[f] = &amp;#8734;&#xA; &#xA; for each single variable function f = v&#xA; size[f] = 0&#xA; &#xA; for k = 1 to &amp;#8734;&#xA; for each function f&#xA; for each function g of size k &amp;#8722; size(f) &amp;#8722; 1&#xA; if size[f AND g] == &amp;#8734;&#xA; size[f AND g] = k&#xA; nsize++&#xA; if size[f OR g] == &amp;#8734;&#xA; size[f OR g] = k&#xA; nsize++&#xA; if nsize == 2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt;&#xA; return&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Applying the idea to Algorithm 2 (after canonicalization) yields:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Algorithm 4&#xA;func compute():&#xA; for each function f&#xA; size[f] = &amp;#8734;&#xA; &#xA; for each single variable function f = v&#xA; size[f] = 0&#xA; &#xA; for k = 1 to &amp;#8734;&#xA; for each canonical function f&#xA; for each function ff equivalent to f&#xA; for each canonical function g of size k &amp;#8722; size(f) &amp;#8722; 1&#xA; visit(k, ff OR g)&#xA; visit(k, ff AND g)&#xA; visit(k, ff AND &amp;#172;g)&#xA; visit(k, &amp;#172;ff AND g)&#xA; if nvisited == 2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt;&#xA; return&#xA;&#xA;func visit(d, fg):&#xA; if size[fg] != &amp;#8734;&#xA; return&#xA; &#xA; record fg as canonical&#xA;&#xA; for each function ffgg equivalent to fg&#xA; if size[ffgg] != &amp;#8734;&#xA; size[ffgg] = d&#xA; nvisited += 2 // counts ffgg and &amp;#172;ffgg&#xA; return&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The original loop in Algorithms 1 and 2 considered each pair f, g in every&#xA;iteration of the loop after they were computed.&#xA;The new loop in Algorithms 3 and 4 considers each pair f, g only once,&#xA;when k = size(f) + size(g) + 1. This removes the &#xA;leading factor of 30 (the number of times we&#xA;expected the first loop to run) from our estimation&#xA;of the run time.&#xA;Now the expected number of iterations is around&#xA;2&lt;sup&gt;64&lt;/sup&gt;/7680 = 2.4&amp;#215;10&lt;sup&gt;15&lt;/sup&gt;. If we can do 10&lt;sup&gt;9&lt;/sup&gt; iterations&#xA;per second, that&#39;s only 28 days of CPU time,&#xA;which I can deliver if you can wait a month.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Our estimate does not include the fact that not all function pairs need&#xA;to be considered. For example, if the maximum size is 30, then the&#xA;functions of size 14 need never be paired against the functions of size 16,&#xA;because any result would have size 14+1+16 = 31.&#xA;So even 2.4&amp;#215;10&lt;sup&gt;15&lt;/sup&gt; is an overestimate, but it&#39;s in the right ballpark.&#xA;(With hindsight I can report that only 1.7&amp;#215;10&lt;sup&gt;14&lt;/sup&gt; pairs need to be considered&#xA;but also that our estimate of 10&lt;sup&gt;9&lt;/sup&gt; iterations&#xA;per second was optimistic. The actual calculation ran for 20 days,&#xA;an average of about 10&lt;sup&gt;8&lt;/sup&gt; iterations per second.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Endgame: Directed Search&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;A month is still a long time to wait, and we can do better.&#xA;Near the end (after k is bigger than, say, 22), we are exploring&#xA;the fairly large space of function pairs in hopes of finding a&#xA;fairly small number of remaining functions.&#xA;At that point it makes sense to change from the&#xA;bottom-up &amp;ldquo;bang things together and see what we make&amp;rdquo;&#xA;to the top-down &amp;ldquo;try to make this one of these specific functions.&amp;rdquo;&#xA;That is, the core of the current search is:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each canonical function f&#xA; for each function ff equivalent to f&#xA; for each canonical function g of size k &amp;#8722; size(f) &amp;#8722; 1&#xA; visit(k, ff OR g)&#xA; visit(k, ff AND g)&#xA; visit(k, ff AND &amp;#172;g)&#xA; visit(k, &amp;#172;ff AND g)&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;We can change it to:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;for each missing function fg&#xA; for each canonical function g&#xA; for all possible f such that one of these holds&#xA; * fg = f OR g&#xA; * fg = f AND g&#xA; * fg = &amp;#172;f AND g&#xA; * fg = f AND &amp;#172;g&#xA; if size[f] == k &amp;#8722; size(g) &amp;#8722; 1&#xA; visit(k, fg)&#xA; next fg&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;By the time we&#39;re at the end, exploring all the possible f to make&#xA;the missing functions&amp;#8212;a directed search&amp;#8212;is much less work than&#xA;the brute force of exploring all combinations.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;As an example, suppose we are looking for f such that fg = f OR g.&#xA;The equation is only possible to satisfy if fg OR g == fg. &#xA;That is, if g has any extraneous 1 bits, no f will work, so we can move on.&#xA;Otherwise, the remaining condition is that&#xA;f AND &amp;#172;g == fg AND &amp;#172;g. That is, for the bit positions where g is 0, f must match fg.&#xA;The other bits of f (the bits where g has 1s)&#xA;can take any value.&#xA;We can enumerate the possible f values by recursively trying all&#xA;possible values for the &amp;ldquo;don&#39;t care&amp;rdquo; bits.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;func find(x, any, xsize):&#xA; if size(x) == xsize&#xA; return x&#xA; while any != 0&#xA; bit = any AND &amp;#8722;any // rightmost 1 bit in any&#xA; any = any AND &amp;#172;bit&#xA; if f = find(x OR bit, any, xsize) succeeds&#xA; return f&#xA; return failure&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;It doesn&#39;t matter which 1 bit we choose for the recursion,&#xA;but finding the rightmost 1 bit is cheap: it is isolated by the&#xA;(admittedly surprising) expression &amp;ldquo;any AND &amp;#8722;any.&amp;rdquo;&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Given &lt;code&gt;find&lt;/code&gt;, the loop above can try these four cases:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table id=find&gt;&#xA;&lt;tr&gt;&lt;th&gt;Formula &lt;th&gt;Condition &lt;th&gt;Base x &lt;th&gt;&amp;ldquo;Any&amp;rdquo; bits&#xA;&lt;tr&gt;&lt;td&gt;fg = f OR g &lt;td&gt;fg OR g == fg &lt;td&gt;fg AND &amp;#172;g &lt;td&gt;g&#xA;&lt;tr&gt;&lt;td&gt;fg = f OR &amp;#172;g &lt;td&gt;fg OR &amp;#172;g == fg &lt;td&gt;fg AND g &lt;td&gt;&amp;#172;g&#xA;&lt;tr&gt;&lt;td&gt;&amp;#172;fg = f OR g &lt;td&gt;&amp;#172;fg OR g == fg &lt;td&gt;&amp;#172;fg AND &amp;#172;g &lt;td&gt;g&#xA;&lt;tr&gt;&lt;td&gt;&amp;#172;fg = f OR &amp;#172;g &lt;td&gt;&amp;#172;fg OR &amp;#172;g == &amp;#172;fg &lt;td&gt;&amp;#172;fg AND g &lt;td&gt;&amp;#172;g&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Rewriting the Boolean expressions to use only the four OR forms&#xA;means that we only need to write the &amp;ldquo;adding bits&amp;rdquo; version of find.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The final algorithm is:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=&#34;indent alg&#34;&gt;&#xA;// Algorithm 5&#xA;func compute():&#xA; for each function f&#xA; size[f] = &amp;#8734;&#xA; &#xA; for each single variable function f = v&#xA; size[f] = 0&#xA; &#xA; // Generate functions.&#xA; for k = 1 to max_generate&#xA; for each canonical function f&#xA; for each function ff equivalent to f&#xA; for each canonical function g of size k &amp;#8722; size(f) &amp;#8722; 1&#xA; visit(k, ff OR g)&#xA; visit(k, ff AND g)&#xA; visit(k, ff AND &amp;#172;g)&#xA; visit(k, &amp;#172;ff AND g)&#xA;&#xA; // Search for functions.&#xA; for k = max_generate+1 to &amp;#8734;&#xA; for each missing function fg&#xA; for each canonical function g&#xA; fsize = k &amp;#8722; size(g) &amp;#8722; 1&#xA; if fg OR g == fg&#xA; if f = find(fg AND &amp;#172;g, g, fsize) succeeds&#xA; visit(k, fg)&#xA; next fg&#xA; if fg OR &amp;#172;g == fg&#xA; if f = find(fg AND g, &amp;#172;g, fsize) succeeds&#xA; visit(k, fg)&#xA; next fg&#xA; if &amp;#172;fg OR g == &amp;#172;fg&#xA; if f = find(&amp;#172;fg AND &amp;#172;g, g, fsize) succeeds&#xA; visit(k, fg)&#xA; next fg&#xA; if &amp;#172;fg OR &amp;#172;g == &amp;#172;fg&#xA; if f = find(&amp;#172;fg AND g, &amp;#172;g, fsize) succeeds&#xA; visit(k, fg)&#xA; next fg&#xA; if nvisited == 2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt;&#xA; return&#xA;&#xA;func visit(d, fg):&#xA; if size[fg] != &amp;#8734;&#xA; return&#xA; &#xA; record fg as canonical&#xA;&#xA; for each function ffgg equivalent to fg&#xA; if size[ffgg] != &amp;#8734;&#xA; size[ffgg] = d&#xA; nvisited += 2 // counts ffgg and &amp;#172;ffgg&#xA; return&#xA;&#xA;func find(x, any, xsize):&#xA; if size(x) == xsize&#xA; return x&#xA; while any != 0&#xA; bit = any AND &amp;#8722;any // rightmost 1 bit in any&#xA; any = any AND &amp;#172;bit&#xA; if f = find(x OR bit, any, xsize) succeeds&#xA; return f&#xA; return failure&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;To get a sense of the speedup here, and to check my work,&#xA;I ran the program using both algorithms&#xA;on a 2.53 GHz Intel Core 2 Duo E7200.&#xA;&lt;/p&gt;&#xA;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table id=times&gt;&#xA;&lt;tr&gt;&lt;th&gt; &lt;th colspan=3&gt;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212; # of Functions &amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&lt;th colspan=2&gt;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212; Time &amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&#xA;&lt;tr&gt;&lt;th&gt;Size &lt;th&gt;Canonical &lt;th&gt;All &lt;th&gt;All, Cumulative &lt;th&gt;Generate &lt;th&gt;Search&#xA;&lt;tr&gt;&lt;td&gt;0 &lt;td&gt;1 &lt;td&gt;10 &lt;td&gt;10&#xA;&lt;tr&gt;&lt;td&gt;1 &lt;td&gt;2 &lt;td&gt;82 &lt;td&gt;92 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;3.4 minutes&#xA;&lt;tr&gt;&lt;td&gt;2 &lt;td&gt;2 &lt;td&gt;640 &lt;td&gt;732 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;7.2 minutes&#xA;&lt;tr&gt;&lt;td&gt;3 &lt;td&gt;7 &lt;td&gt;4420 &lt;td&gt;5152 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;12.3 minutes&#xA;&lt;tr&gt;&lt;td&gt;4 &lt;td&gt;19 &lt;td&gt;25276 &lt;td&gt;29696 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;30.1 minutes&#xA;&lt;tr&gt;&lt;td&gt;5 &lt;td&gt;44 &lt;td&gt;117440 &lt;td&gt;147136 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;1.3 hours&#xA;&lt;tr&gt;&lt;td&gt;6 &lt;td&gt;142 &lt;td&gt;515040 &lt;td&gt;662176 &lt;td&gt;&amp;lt; 0.1 seconds &lt;td&gt;3.5 hours&#xA;&lt;tr&gt;&lt;td&gt;7 &lt;td&gt;436 &lt;td&gt;1999608 &lt;td&gt;2661784 &lt;td&gt;0.2 seconds &lt;td&gt;11.6 hours&#xA;&lt;tr&gt;&lt;td&gt;8 &lt;td&gt;1209 &lt;td&gt;6598400 &lt;td&gt;9260184 &lt;td&gt;0.6 seconds &lt;td&gt;1.7 days&#xA;&lt;tr&gt;&lt;td&gt;9 &lt;td&gt;3307 &lt;td&gt;19577332 &lt;td&gt;28837516 &lt;td&gt;1.7 seconds &lt;td&gt;4.9 days&#xA;&lt;tr&gt;&lt;td&gt;10 &lt;td&gt;7741 &lt;td&gt;50822560 &lt;td&gt;79660076 &lt;td&gt;4.6 seconds &lt;td&gt;[ 10 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;11 &lt;td&gt;17257 &lt;td&gt;114619264 &lt;td&gt;194279340 &lt;td&gt;10.8 seconds &lt;td&gt;[ 20 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;12 &lt;td&gt;31851 &lt;td&gt;221301008 &lt;td&gt;415580348 &lt;td&gt;21.7 seconds &lt;td&gt;[ 50 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;13 &lt;td&gt;53901 &lt;td&gt;374704776 &lt;td&gt;790285124 &lt;td&gt;38.5 seconds &lt;td&gt;[ 80 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;14 &lt;td&gt;75248 &lt;td&gt;533594528 &lt;td&gt;1323879652 &lt;td&gt;58.7 seconds &lt;td&gt;[ 100 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;15 &lt;td&gt;94572 &lt;td&gt;667653642 &lt;td&gt;1991533294 &lt;td&gt;1.5 minutes &lt;td&gt;[ 120 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;16 &lt;td&gt;98237 &lt;td&gt;697228760 &lt;td&gt;2688762054 &lt;td&gt;2.1 minutes &lt;td&gt;[ 120 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;17 &lt;td&gt;89342 &lt;td&gt;628589440 &lt;td&gt;3317351494 &lt;td&gt;4.1 minutes &lt;td&gt;[ 90 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;18 &lt;td&gt;66951 &lt;td&gt;468552896 &lt;td&gt;3785904390 &lt;td&gt;9.1 minutes &lt;td&gt;[ 50 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;19 &lt;td&gt;41664 &lt;td&gt;287647616 &lt;td&gt;4073552006 &lt;td&gt;23.4 minutes &lt;td&gt;[ 30 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;20 &lt;td&gt;21481 &lt;td&gt;144079832 &lt;td&gt;4217631838 &lt;td&gt;57.0 minutes &lt;td&gt;[ 10 days ? ]&#xA;&lt;tr&gt;&lt;td&gt;21 &lt;td&gt;8680 &lt;td&gt;55538224 &lt;td&gt;4273170062 &lt;td&gt;2.4 hours &lt;td&gt;2.5 days&#xA;&lt;tr&gt;&lt;td&gt;22 &lt;td&gt;2730 &lt;td&gt;16099568 &lt;td&gt;4289269630 &lt;td&gt;5.2 hours &lt;td&gt;11.7 hours&#xA;&lt;tr&gt;&lt;td&gt;23 &lt;td&gt;937 &lt;td&gt;4428800 &lt;td&gt;4293698430 &lt;td&gt;11.2 hours &lt;td&gt;2.2 hours&#xA;&lt;tr&gt;&lt;td&gt;24 &lt;td&gt;228 &lt;td&gt;959328 &lt;td&gt;4294657758 &lt;td&gt;22.0 hours &lt;td&gt;33.2 minutes&#xA;&lt;tr&gt;&lt;td&gt;25 &lt;td&gt;103 &lt;td&gt;283200 &lt;td&gt;4294940958 &lt;td&gt;1.7 days &lt;td&gt;4.0 minutes&#xA;&lt;tr&gt;&lt;td&gt;26 &lt;td&gt;21 &lt;td&gt;22224 &lt;td&gt;4294963182 &lt;td&gt;2.9 days &lt;td&gt;42 seconds&#xA;&lt;tr&gt;&lt;td&gt;27 &lt;td&gt;10 &lt;td&gt;3602 &lt;td&gt;4294966784 &lt;td&gt;4.7 days &lt;td&gt;2.4 seconds&#xA;&lt;tr&gt;&lt;td&gt;28 &lt;td&gt;3 &lt;td&gt;512 &lt;td&gt;4294967296 &lt;td&gt;[ 7 days ? ] &lt;td&gt;0.1 seconds&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The bracketed times are estimates based on the work involved: I did not&#xA;wait that long for the intermediate search steps.&#xA;The search algorithm is quite a bit worse than generate until there are&#xA;very few functions left to find.&#xA;However, it comes in handy just when it is most useful: when the&#xA;generate algorithm has slowed to a crawl.&#xA;If we run generate through formulas of size 22 and then switch&#xA;to search for 23 onward, we can run the whole computation in&#xA;just over half a day of CPU time.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The computation of a(5) identified the sizes of all 616,126&#xA;canonical Boolean functions of 5 inputs.&#xA;In contrast, there are &lt;a href=&#34;http://oeis.org/A000370&#34;&gt;just over 200 trillion canonical Boolean functions of 6 inputs&lt;/a&gt;.&#xA;Determining a(6) is unlikely to happen by brute force computation, no matter what clever tricks we use.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Adding XOR&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;We&#39;ve assumed the use of just AND and OR as our&#xA;basis for the Boolean formulas. If we also allow XOR, functions&#xA;can be written using many fewer operators.&#xA;In particular, a hardest function for the 1-, 2-, 3-, and 4-input&#xA;cases&amp;#8212;parity&amp;#8212;is now trivial.&#xA;Knuth examines the complexity of 5-input Boolean functions&#xA;using AND, OR, and XOR in detail in &lt;a href=&#34;http://www-cs-faculty.stanford.edu/~uno/taocp.html&#34;&gt;The Art of Computer Programming, Volume 4A&lt;/a&gt;.&#xA;Section 7.1.2&#39;s Algorithm L is the same as our Algorithm 3 above,&#xA;given for computing 4-input functions.&#xA;Knuth mentions that to adapt it for 5-input functions one must&#xA;treat only canonical functions and gives results for 5-input functions&#xA;with XOR allowed.&#xA;So another way to check our work is to add XOR to our Algorithm 4&#xA;and check that our results match Knuth&#39;s.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Because the minimum formula sizes are smaller (at most 12), the&#xA;computation of sizes with XOR is much faster than before:&#xA;&lt;/p&gt;&#xA;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&lt;th&gt; &lt;th&gt;&lt;th colspan=5&gt;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212; # of Functions &amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&amp;#8212;&lt;th&gt;&#xA;&lt;tr&gt;&lt;th&gt;Size &lt;th width=10&gt;&lt;th&gt;Canonical &lt;th width=10&gt;&lt;th&gt;All &lt;th width=10&gt;&lt;th&gt;All, Cumulative &lt;th width=10&gt;&lt;th&gt;Time&#xA;&lt;tr&gt;&lt;td align=right&gt;0 &lt;td&gt;&lt;td align=right&gt;1 &lt;td&gt;&lt;td align=right&gt;10 &lt;td&gt;&lt;td align=right&gt;10 &lt;td&gt;&lt;td&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;1 &lt;td&gt;&lt;td align=right&gt;3 &lt;td&gt;&lt;td align=right&gt;102 &lt;td&gt;&lt;td align=right&gt;112 &lt;td&gt;&lt;td align=right&gt;&amp;lt; 0.1 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;2 &lt;td&gt;&lt;td align=right&gt;5 &lt;td&gt;&lt;td align=right&gt;1140 &lt;td&gt;&lt;td align=right&gt;1252 &lt;td&gt;&lt;td align=right&gt;&amp;lt; 0.1 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;3 &lt;td&gt;&lt;td align=right&gt;20 &lt;td&gt;&lt;td align=right&gt;11570 &lt;td&gt;&lt;td align=right&gt;12822 &lt;td&gt;&lt;td align=right&gt;&amp;lt; 0.1 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;4 &lt;td&gt;&lt;td align=right&gt;93 &lt;td&gt;&lt;td align=right&gt;109826 &lt;td&gt;&lt;td align=right&gt;122648 &lt;td&gt;&lt;td align=right&gt;&amp;lt; 0.1 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;5 &lt;td&gt;&lt;td align=right&gt;366 &lt;td&gt;&lt;td align=right&gt;936440 &lt;td&gt;&lt;td align=right&gt;1059088 &lt;td&gt;&lt;td align=right&gt;0.1 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;6 &lt;td&gt;&lt;td align=right&gt;1730 &lt;td&gt;&lt;td align=right&gt;7236880 &lt;td&gt;&lt;td align=right&gt;8295968 &lt;td&gt;&lt;td align=right&gt;0.7 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;7 &lt;td&gt;&lt;td align=right&gt;8782 &lt;td&gt;&lt;td align=right&gt;47739088 &lt;td&gt;&lt;td align=right&gt;56035056 &lt;td&gt;&lt;td align=right&gt;4.5 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;8 &lt;td&gt;&lt;td align=right&gt;40297 &lt;td&gt;&lt;td align=right&gt;250674320 &lt;td&gt;&lt;td align=right&gt;306709376 &lt;td&gt;&lt;td align=right&gt;24.0 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;9 &lt;td&gt;&lt;td align=right&gt;141422 &lt;td&gt;&lt;td align=right&gt;955812256 &lt;td&gt;&lt;td align=right&gt;1262521632 &lt;td&gt;&lt;td align=right&gt;95.5 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;10 &lt;td&gt;&lt;td align=right&gt;273277 &lt;td&gt;&lt;td align=right&gt;1945383936 &lt;td&gt;&lt;td align=right&gt;3207905568 &lt;td&gt;&lt;td align=right&gt;200.7 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;11 &lt;td&gt;&lt;td align=right&gt;145707 &lt;td&gt;&lt;td align=right&gt;1055912608 &lt;td&gt;&lt;td align=right&gt;4263818176 &lt;td&gt;&lt;td align=right&gt;121.2 seconds&#xA;&lt;tr&gt;&lt;td align=right&gt;12 &lt;td&gt;&lt;td align=right&gt;4423 &lt;td&gt;&lt;td align=right&gt;31149120 &lt;td&gt;&lt;td align=right&gt;4294967296 &lt;td&gt;&lt;td align=right&gt;65.0 seconds&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Knuth does not discuss anything like Algorithm 5,&#xA;because the search for specific functions does not apply to&#xA;the AND, OR, and XOR basis. XOR is a non-monotone&#xA;function (it can both turn bits on and turn bits off), so&#xA;there is no test like our &amp;ldquo;&lt;code&gt;if fg OR g == fg&lt;/code&gt;&amp;rdquo;&#xA;and no small set of &amp;ldquo;don&#39;t care&amp;rdquo; bits to trim the search for f.&#xA;The search for an appropriate f in the XOR case would have&#xA;to try all f of the right size, which is exactly what Algorithm 4 already does.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Volume 4A also considers the problem of building minimal circuits,&#xA;which are like formulas but can use common subexpressions additional times for free,&#xA;and the problem of building the shallowest possible circuits.&#xA;See Section 7.1.2 for all the details.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Code and Web Site&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The web site &lt;a href=&#34;http://boolean-oracle.swtch.com&#34;&gt;boolean-oracle.swtch.com&lt;/a&gt;&#xA;lets you type in a Boolean expression and gives back the minimal formula for it.&#xA;It uses tables generated while running Algorithm 5; those tables and the&#xA;programs described in this post are also &lt;a href=&#34;http://boolean-oracle.swtch.com/about&#34;&gt;available on the site&lt;/a&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Postscript: Generating All Permutations and Inversions&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The algorithms given above depend crucially on the step&#xA;&amp;ldquo;&lt;code&gt;for each function ff equivalent to f&lt;/code&gt;,&amp;rdquo;&#xA;which generates all the ff obtained by permuting or inverting inputs to f,&#xA;but I did not explain how to do that.&#xA;We already saw that we can manipulate the binary truth table representation&#xA;directly to turn &lt;code&gt;f&lt;/code&gt; into &lt;code&gt;&amp;#172;f&lt;/code&gt; and to compute&#xA;combinations of functions.&#xA;We can also manipulate the binary representation directly to&#xA;invert a specific input or swap a pair of adjacent inputs.&#xA;Using those operations we can cycle through all the equivalent functions.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;To invert a specific input,&#xA;let&#39;s consider the structure of the truth table.&#xA;The index of a bit in the truth table encodes the inputs for that entry.&#xA;For example, the low bit of the index gives the value of the first input.&#xA;So the even-numbered bits&amp;#8212;at indices 0, 2, 4, 6, ...&amp;#8212;correspond to&#xA;the first input being false, while the odd-numbered bits&amp;#8212;at indices 1, 3, 5, 7, ...&amp;#8212;correspond&#xA;to the first input being true.&#xA;Changing just that bit in the index corresponds to changing the&#xA;single variable, so indices 0, 1 differ only in the value of the first input,&#xA;as do 2, 3, and 4, 5, and 6, 7, and so on.&#xA;Given the truth table for f(V, W, X, Y, Z) we can compute&#xA;the truth table for f(&amp;#172;V, W, X, Y, Z) by swapping adjacent bit pairs&#xA;in the original truth table.&#xA;Even better, we can do all the swaps in parallel using a bitwise&#xA;operation.&#xA;To invert a different input, we swap larger runs of bits.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&lt;th&gt;Function &lt;th width=10&gt; &lt;th&gt;Truth Table (&lt;span style=&#34;font-weight: normal;&#34;&gt;&lt;code&gt;f&lt;/code&gt; = f(V, W, X, Y, Z)&lt;/span&gt;)&#xA;&lt;tr&gt;&lt;td&gt;f(&amp;#172;V, W, X, Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;(f&amp;amp;0x55555555)&amp;lt;&amp;lt;&amp;nbsp;1 | (f&amp;gt;&amp;gt;&amp;nbsp;1)&amp;amp;0x55555555&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, &amp;#172;W, X, Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;(f&amp;amp;0x33333333)&amp;lt;&amp;lt;&amp;nbsp;2 | (f&amp;gt;&amp;gt;&amp;nbsp;2)&amp;amp;0x33333333&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, W, &amp;#172;X, Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;(f&amp;amp;0x0f0f0f0f)&amp;lt;&amp;lt;&amp;nbsp;4 | (f&amp;gt;&amp;gt;&amp;nbsp;4)&amp;amp;0x0f0f0f0f&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, W, X, &amp;#172;Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;(f&amp;amp;0x00ff00ff)&amp;lt;&amp;lt;&amp;nbsp;8 | (f&amp;gt;&amp;gt;&amp;nbsp;8)&amp;amp;0x00ff00ff&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, W, X, Y, &amp;#172;Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;(f&amp;amp;0x0000ffff)&amp;lt;&amp;lt;16 | (f&amp;gt;&amp;gt;16)&amp;amp;0x0000ffff&lt;/code&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Being able to invert a specific input lets us consider all possible&#xA;inversions by building them up one at a time.&#xA;The &lt;a href=&#34;http://oeis.org/A003188&#34;&gt;Gray code&lt;/a&gt; lets us&#xA;enumerate all possible 5-bit input codes while changing only 1 bit at&#xA;a time as we move from one input to the next:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;0, 1, 3, 2, 6, 7, 5, 4, &lt;br&gt;&#xA;12, 13, 15, 14, 10, 11, 9, 8, &lt;br&gt;&#xA;24, 25, 27, 26, 30, 31, 29, 28, &lt;br&gt;&#xA;20, 21, 23, 22, 18, 19, 17, 16&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;This minimizes&#xA;the number of inversions we need: to consider all 32 cases, we only&#xA;need 31 inversion operations.&#xA;In contrast, visiting the 5-bit input codes in the usual binary order 0, 1, 2, 3, 4, ...&#xA;would often need to change multiple bits, like when changing from 3 to 4.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;To swap a pair of adjacent inputs, we can again take advantage of the truth table.&#xA;For a pair of inputs, there are four cases: 00, 01, 10, and 11. We can leave the&#xA;00 and 11 cases alone, because they are invariant under swapping,&#xA;and concentrate on swapping the 01 and 10 bits.&#xA;The first two inputs change most often in the truth table: each run of 4 bits&#xA;corresponds to those four cases.&#xA;In each run, we want to leave the first and fourth alone and swap the second and third.&#xA;For later inputs, the four cases consist of sections of bits instead of single bits.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&lt;th&gt;Function &lt;th width=10&gt; &lt;th&gt;Truth Table (&lt;span style=&#34;font-weight: normal;&#34;&gt;&lt;code&gt;f&lt;/code&gt; = f(V, W, X, Y, Z)&lt;/span&gt;)&#xA;&lt;tr&gt;&lt;td&gt;f(&lt;b&gt;W, V&lt;/b&gt;, X, Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;f&amp;amp;0x99999999 | (f&amp;amp;0x22222222)&amp;lt;&amp;lt;1 | (f&amp;gt;&amp;gt;1)&amp;amp;0x22222222&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, &lt;b&gt;X, W&lt;/b&gt;, Y, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;f&amp;amp;0xc3c3c3c3 | (f&amp;amp;0x0c0c0c0c)&amp;lt;&amp;lt;1 | (f&amp;gt;&amp;gt;1)&amp;amp;0x0c0c0c0c&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, W, &lt;b&gt;Y, X&lt;/b&gt;, Z) &lt;td&gt;&lt;td&gt;&lt;code&gt;f&amp;amp;0xf00ff00f | (f&amp;amp;0x00f000f0)&amp;lt;&amp;lt;1 | (f&amp;gt;&amp;gt;1)&amp;amp;0x00f000f0&lt;/code&gt;&#xA;&lt;tr&gt;&lt;td&gt;f(V, W, X, &lt;b&gt;Z, Y&lt;/b&gt;) &lt;td&gt;&lt;td&gt;&lt;code&gt;f&amp;amp;0xff0000ff | (f&amp;amp;0x0000ff00)&amp;lt;&amp;lt;8 | (f&amp;gt;&amp;gt;8)&amp;amp;0x0000ff00&lt;/code&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Being able to swap a pair of adjacent inputs lets us consider all&#xA;possible permutations by building them up one at a time.&#xA;Again it is convenient to have a way to visit all permutations by&#xA;applying only one swap at a time.&#xA;Here Volume 4A comes to the rescue.&#xA;Section 7.2.1.2 is titled &amp;ldquo;Generating All Permutations,&amp;rdquo; and Knuth delivers&#xA;many algorithms to do just that.&#xA;The most convenient for our purposes is Algorithm P, which&#xA;generates a sequence that considers all permutations exactly once&#xA;with only a single swap of adjacent inputs between steps.&#xA;Knuth calls it Algorithm P because it corresponds to the&#xA;&amp;ldquo;Plain changes&amp;rdquo; algorithm used by &lt;a href=&#34;http://en.wikipedia.org/wiki/Change_ringing&#34;&gt;bell ringers in 17th century England&lt;/a&gt;&#xA;to ring a set of bells in all possible permutations.&#xA;The algorithm is described in a manuscript written around 1653!&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;We can examine all possible permutations and inversions by&#xA;nesting a loop over all permutations inside a loop over all inversions,&#xA;and in fact that&#39;s what my program does.&#xA;Knuth does one better, though: his Exercise 7.2.1.2-20&#xA;suggests that it is possible to build up all the possibilities&#xA;using only adjacent swaps and inversion of the first input.&#xA;Negating arbitrary inputs is not hard, though, and still does&#xA;minimal work, so the code sticks with Gray codes and Plain changes.&#xA;&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
<entry>
<title>Zip Files All The Way Down</title>
<id>tag:research.swtch.com,2012:research.swtch.com/zip</id>
<link rel="alternate" href="http://research.swtch.com/zip"></link>
<published>2010-03-18T00:00:00-04:00</published>
<updated>2010-03-18T00:00:00-04:00</updated>
<summary type="text">Did you think it was turtles?</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=lp&gt;&#xA;Stephen Hawking begins &lt;i&gt;&lt;a href=&#34;http://www.amazon.com/-/dp/0553380168&#34;&gt;A Brief History of Time&lt;/a&gt;&lt;/i&gt; with this story:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;&lt;p class=pp&gt;&#xA;A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: &amp;ldquo;What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise.&amp;rdquo; The scientist gave a superior smile before replying, &amp;ldquo;What is the tortoise standing on?&amp;rdquo; &amp;ldquo;You&#39;re very clever, young man, very clever,&amp;rdquo; said the old lady. &amp;ldquo;But it&#39;s turtles all the way down!&amp;rdquo;&#xA;&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Scientists today are pretty sure that the universe is not actually turtles all the way down,&#xA;but we can create that kind of situation in other contexts.&#xA;For example, here we have &lt;a href=&#34;http://www.youtube.com/watch?v=Y-gqMTt3IUg&#34;&gt;video monitors all the way down&lt;/a&gt;&#xA;and &lt;a href=&#34;http://www.amazon.com/gp/customer-media/product-gallery/0387900926/ref=cm_ciu_pdp_images_all&#34;&gt;set theory books all the way down&lt;/a&gt;,&#xA;and &lt;a href=&#34;http://blog.makezine.com/archive/2009/01/thousands_of_shopping_carts_stake_o.html&#34;&gt;shopping carts all the way down&lt;/a&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;And here&#39;s a computer storage equivalent: &#xA;look inside &lt;a href=&#34;http://swtch.com/r.zip&#34;&gt;&lt;code&gt;r.zip&lt;/code&gt;&lt;/a&gt;.&#xA;It&#39;s zip files all the way down:&#xA;each one contains another zip file under the name &lt;code&gt;r/r.zip&lt;/code&gt;.&#xA;(For the die-hard Unix fans, &lt;a href=&#34;http://swtch.com/r.tar.gz&#34;&gt;&lt;code&gt;r.tar.gz&lt;/code&gt;&lt;/a&gt; is&#xA;gzipped tar files all the way down.)&#xA;Like the line of shopping carts, it never ends,&#xA;because it loops back onto itself: the zip file contains itself!&#xA;And it&#39;s probably less work to put together a self-reproducing zip file&#xA;than to put together all those shopping carts,&#xA;at least if you&#39;re the kind of person who would read this blog.&#xA;This post explains how.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Before we get to self-reproducing zip files, though,&#xA;we need to take a brief detour into self-reproducing programs.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Self-reproducing programs&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The idea of self-reproducing programs dates back to the 1960s.&#xA;My favorite statement of the problem is the one Ken Thompson gave in his 1983 Turing Award address:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;&lt;p class=pp&gt;&#xA;In college, before video games, we would amuse ourselves by posing programming exercises. One of the favorites was to write the shortest self-reproducing program. Since this is an exercise divorced from reality, the usual vehicle was FORTRAN. Actually, FORTRAN was the language of choice for the same reason that three-legged races are popular.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;More precisely stated, the problem is to write a source program that, when compiled and executed, will produce as output an exact copy of its source. If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it. The part about &amp;ldquo;shortest&amp;rdquo; was just an incentive to demonstrate skill and determine a winner.&#xA;&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;&lt;b&gt;Spoiler alert!&lt;/b&gt;&#xA;I agree: if you have never done this, I urge you to try it on your own.&#xA;The internet makes it so easy to look things up that it&#39;s refreshing&#xA;to discover something yourself once in a while.&#xA;Go ahead and spend a few days figuring out. This blog will still be here&#xA;when you get back.&#xA;(If you don&#39;t mind the spoilers, the entire &lt;a href=&#34;http://cm.bell-labs.com/who/ken/trust.html&#34;&gt;Turing award address&lt;/a&gt; is worth reading.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;br&gt;&lt;br&gt;&#xA;&lt;i&gt;(Spoiler blocker.)&lt;/i&gt;&#xA;&lt;br&gt;&#xA;&lt;a href=&#34;http://www.robertwechsler.com/projects.html&#34;&gt;&lt;img src=&#34;http://research.swtch.com/applied_geometry.jpg&#34;&gt;&lt;/a&gt;&#xA;&lt;br&gt;&#xA;&lt;i&gt;&lt;a href=&#34;http://www.robertwechsler.com/projects.html&#34;&gt;http://www.robertwechsler.com/projects.html&lt;/a&gt;&lt;/i&gt;&#xA;&lt;br&gt;&lt;br&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Let&#39;s try to write a Python program that prints itself.&#xA;It will probably be a &lt;code&gt;print&lt;/code&gt; statement, so here&#39;s a first attempt,&#xA;run at the interpreter prompt:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;&amp;gt;&amp;gt;&amp;gt; print &#39;&lt;span style=&#34;color: #005500&#34;&gt;hello&lt;/span&gt;&#39;&#xA;hello&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That didn&#39;t quite work. But now we know what the program is, so let&#39;s print it:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;&amp;gt;&amp;gt;&amp;gt; print &#34;&lt;span style=&#34;color: #005500&#34;&gt;print &#39;hello&#39;&lt;/span&gt;&#34;&#xA;print &#39;hello&#39;&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That didn&#39;t quite work either. The problem is that when you execute&#xA;a simple print statement, it only prints part of itself: the argument to the print.&#xA;We need a way to print the rest of the program too.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The trick is to use recursion: you write a string that is the whole program,&#xA;but with itself missing, and then you plug it into itself before passing it to print.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;&amp;gt;&amp;gt;&amp;gt; s = &#39;&lt;span style=&#34;color: #005500&#34;&gt;print %s&lt;/span&gt;&#39;; print s % repr(s)&#xA;print &#39;print %s&#39;&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Not quite, but closer: the problem is that the string &lt;code&gt;s&lt;/code&gt; isn&#39;t actually&#xA;the program. But now we know the general form of the program:&#xA;&lt;code&gt;s = &#39;&lt;span style=&#34;color: #005500&#34;&gt;%s&lt;/span&gt;&#39;; print s % repr(s)&lt;/code&gt;.&#xA;That&#39;s the string to use.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;&amp;gt;&amp;gt;&amp;gt; s = &#39;&lt;span style=&#34;color: #005500&#34;&gt;s = %s; print s %% repr(s)&lt;/span&gt;&#39;; print s % repr(s)&#xA;s = &#39;s = %s; print s %% repr(s)&#39;; print s % repr(s)&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Recursion for the win.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;This form of self-reproducing program is often called a &lt;a href=&#34;http://en.wikipedia.org/wiki/Quine_(computing)&#34;&gt;quine&lt;/a&gt;, &#xA;in honor of the philosopher and logician W. V. O. Quine,&#xA;who discovered the paradoxical sentence:&#xA;&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&amp;ldquo;Yields falsehood when preceded by its quotation&amp;rdquo;&lt;br&gt;yields falsehood when preceded by its quotation.&#xA;&lt;/blockquote&gt;&#xA;&lt;p class=lp&gt;&#xA;The simplest English form of a self-reproducing quine is a command like:&#xA;&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;Print this, followed by its quotation:&lt;br&gt;&amp;ldquo;Print this, followed by its quotation:&amp;rdquo;&#xA;&lt;/blockquote&gt;&#xA;&lt;p class=lp&gt;&#xA;There&#39;s nothing particularly special about Python that makes quining possible.&#xA;The most elegant quine I know is a Scheme program that is a direct, if somewhat inscrutable, translation of that&#xA;sentiment:&#xA;&lt;/p&gt;&#xA;&lt;pre class=indent&gt;&#xA;((lambda (x) `&lt;span style=&#34;color: #005500&#34;&gt;(&lt;/span&gt;,x &lt;span style=&#34;color: #005500&#34;&gt;&#39;&lt;/span&gt;,x&lt;span style=&#34;color: #005500&#34;&gt;)&lt;/span&gt;)&#xA;&#39;&lt;span style=&#34;color: #005500&#34;&gt;(lambda (x) `(,x &#39;,x))&lt;/span&gt;)&#xA;&lt;/pre&gt;&#xA;&lt;p class=lp&gt;&#xA;I think the Go version is a clearer translation, at least as far as the quoting is concerned:&#xA;&lt;/p&gt;&#xA;&lt;pre class=indent&gt;&#xA;/* Go quine */&#xA;package main&#xA;import &#34;&lt;span style=&#34;color: #005500&#34;&gt;fmt&lt;/span&gt;&#34;&#xA;func main() {&#xA; fmt.Printf(&#34;&lt;span style=&#34;color: #005500&#34;&gt;%s%c%s%c\n&lt;/span&gt;&#34;, q, 0x60, q, 0x60)&#xA;}&#xA;var q = `&lt;span style=&#34;color: #005500&#34;&gt;/* Go quine */&#xA;package main&#xA;import &#34;fmt&#34;&#xA;func main() {&#xA; fmt.Printf(&#34;%s%c%s%c\n&#34;, q, 0x60, q, 0x60)&#xA;}&#xA;var q = &lt;/span&gt;`&#xA;&lt;/pre&gt;&#xA;&lt;p class=lp&gt;(I&#39;ve colored the data literals green throughout to make it clear what is program and what is data.)&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;The Go program has the interesting property that, ignoring the pesky newline&#xA;at the end, the entire program is the same thing twice (&lt;code&gt;/* Go quine */ ... q = `&lt;/code&gt;).&#xA;That got me thinking: maybe it&#39;s possible to write a self-reproducing program&#xA;using only a repetition operator.&#xA;And you know what programming language has essentially only a repetition operator?&#xA;The language used to encode Lempel-Ziv compressed files&#xA;like the ones used by &lt;code&gt;gzip&lt;/code&gt; and &lt;code&gt;zip&lt;/code&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;h3&gt;Self-reproducing Lempel-Ziv programs&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Lempel-Ziv compressed data is a stream of instructions with two basic&#xA;opcodes: &lt;code&gt;literal(&lt;/code&gt;&lt;i&gt;n&lt;/i&gt;&lt;code&gt;)&lt;/code&gt; followed by &#xA;&lt;i&gt;n&lt;/i&gt; bytes of data means write those &lt;i&gt;n&lt;/i&gt; bytes into the&#xA;decompressed output,&#xA;and &lt;code&gt;repeat(&lt;/code&gt;&lt;i&gt;d&lt;/i&gt;&lt;code&gt;,&lt;/code&gt; &lt;i&gt;n&lt;/i&gt;&lt;code&gt;)&lt;/code&gt;&#xA;means look backward &lt;i&gt;d&lt;/i&gt; bytes from the current location&#xA;in the decompressed output and copy the &lt;i&gt;n&lt;/i&gt; bytes you find there&#xA;into the output stream.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The programming exercise, then, is this: write a Lempel-Ziv program&#xA;using just those two opcodes that prints itself when run.&#xA;In other words, write a compressed data stream that decompresses to itself.&#xA;Feel free to assume any reasonable encoding for the &lt;code&gt;literal&lt;/code&gt;&#xA;and &lt;code&gt;repeat&lt;/code&gt; opcodes.&#xA;For the grand prize, find a program that decompresses to &#xA;itself surrounded by an arbitrary prefix and suffix,&#xA;so that the sequence could be embedded in an actual &lt;code&gt;gzip&lt;/code&gt;&#xA;or &lt;code&gt;zip&lt;/code&gt; file, which has a fixed-format header and trailer.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;&lt;b&gt;Spoiler alert!&lt;/b&gt;&#xA;I urge you to try this on your own before continuing to read.&#xA;It&#39;s a great way to spend a lazy afternoon, and you have&#xA;one critical advantage that I didn&#39;t: you know there is a solution.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;br&gt;&lt;br&gt;&#xA;&lt;i&gt;(Spoiler blocker.)&lt;/i&gt;&#xA;&lt;br&gt;&#xA;&lt;a href=&#34;&#34;&gt;&lt;img src=&#34;http://research.swtch.com/the_best_circular_bike(sbcc_sbma_students_roof).jpg&#34;&gt;&lt;/a&gt;&#xA;&lt;br&gt;&#xA;&lt;i&gt;&lt;a href=&#34;http://www.robertwechsler.com/thebest.html&#34;&gt;http://www.robertwechsler.com/thebest.html&lt;/a&gt;&lt;/i&gt;&#xA;&lt;br&gt;&lt;br&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;By the way, here&#39;s &lt;a href=&#34;http://swtch.com/r.gz&#34;&gt;&lt;code&gt;r.gz&lt;/code&gt;&lt;/a&gt;, gzip files all the way down.&#xA;&#xA;&lt;pre class=indent&gt;&#xA;$ gunzip &amp;lt; r.gz &amp;gt; r&#xA;$ cmp r r.gz&#xA;$&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;The nice thing about &lt;code&gt;r.gz&lt;/code&gt; is that even broken web browsers&#xA;that ordinarily decompress downloaded gzip data before storing it to disk&#xA;will handle this file correctly!&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Enough stalling to hide the spoilers.&#xA;Let&#39;s use this shorthand to describe Lempel-Ziv instructions:&#xA;&lt;code&gt;L&lt;/code&gt;&lt;i&gt;n&lt;/i&gt; and &lt;code&gt;R&lt;/code&gt;&lt;i&gt;n&lt;/i&gt; are&#xA;shorthand for &lt;code&gt;literal(&lt;/code&gt;&lt;i&gt;n&lt;/i&gt;&lt;code&gt;)&lt;/code&gt; and&#xA;&lt;code&gt;repeat(&lt;/code&gt;&lt;i&gt;n&lt;/i&gt;&lt;code&gt;,&lt;/code&gt; &lt;i&gt;n&lt;/i&gt;&lt;code&gt;)&lt;/code&gt;,&#xA;and the program assumes that each code is one byte.&#xA;&lt;code&gt;L0&lt;/code&gt; is therefore the Lempel-Ziv no-op;&#xA;&lt;code&gt;L5&lt;/code&gt; &lt;code&gt;hello&lt;/code&gt; prints &lt;code&gt;hello&lt;/code&gt;;&#xA;and so does &lt;code&gt;L3&lt;/code&gt; &lt;code&gt;hel&lt;/code&gt; &lt;code&gt;R1&lt;/code&gt; &lt;code&gt;L1&lt;/code&gt; &lt;code&gt;o&lt;/code&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Here&#39;s a Lempel-Ziv program that prints itself.&#xA;(Each line is one instruction.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&lt;center&gt;&#xA;&lt;table border=0&gt;&#xA;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Code&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Output&lt;/th&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;no-op&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;no-op&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;no-op&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;L0 L0 L0 L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0 L0 L0 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0 L0 L0 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R4 L4 R4 L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;L0 L0 L0 L0&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L0 L0 L0 L0&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&lt;br&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;(The two columns Code and Output contain the same byte sequence.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The interesting core of this program is the 6-byte sequence&#xA;&lt;code&gt;L4 R4 L4 R4 L4 R4&lt;/code&gt;, which prints the 8-byte sequence &lt;code&gt;R4 L4 R4 L4 R4 L4 R4 L4&lt;/code&gt;.&#xA;That is, it prints itself with an extra byte before and after.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;When we were trying to write the self-reproducing Python program,&#xA;the basic problem was that the print statement was always longer&#xA;than what it printed. We solved that problem with recursion,&#xA;computing the string to print by plugging it into itself.&#xA;Here we took a different approach.&#xA;The Lempel-Ziv program is&#xA;particularly repetitive, so that a repeated substring ends up&#xA;containing the entire fragment. The recursion is in the&#xA;representation of the program rather than its execution.&#xA;Either way, that fragment is the crucial point.&#xA;Before the final &lt;code&gt;R4&lt;/code&gt;, the output lags behind the input.&#xA;Once it executes, the output is one code ahead.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The &lt;code&gt;L0&lt;/code&gt; no-ops are plugged into &#xA;a more general variant of the program, which can reproduce itself&#xA;with the addition of an arbitrary three-byte prefix and suffix:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&lt;center&gt;&#xA;&lt;table border=0&gt;&#xA;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Code&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Output&lt;/th&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;&lt;i&gt;aa bb cc&lt;/i&gt; L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&lt;i&gt;aa bb cc&lt;/i&gt; L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&lt;i&gt;aa bb cc&lt;/i&gt; L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R4 L4 R4 L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R4 &lt;i&gt;xx yy zz&lt;/i&gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 &lt;i&gt;xx yy zz&lt;/i&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;R4 &lt;i&gt;xx yy zz&lt;/i&gt;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&lt;br&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;(The byte sequence in the Output column is &lt;code&gt;&lt;i&gt;aa bb cc&lt;/i&gt;&lt;/code&gt;, then&#xA;the byte sequence from the Code column, then &lt;code&gt;&lt;i&gt;xx yy zz&lt;/i&gt;&lt;/code&gt;.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;It took me the better part of a quiet Sunday to get this far,&#xA;but by the time I got here I knew the game was over&#xA;and that I&#39;d won.&#xA;From all that experimenting, I knew it was easy to create&#xA;a program fragment that printed itself minus a few instructions&#xA;or even one that printed an arbitrary prefix &#xA;and then itself, minus a few instructions.&#xA;The extra &lt;code&gt;aa bb cc&lt;/code&gt; in the output&#xA;provides a place to attach such a program fragment.&#xA;Similarly, it&#39;s easy to create a fragment to attach&#xA;to the &lt;code&gt;xx yy zz&lt;/code&gt; that prints itself,&#xA;minus the first three instructions, plus an arbitrary suffix.&#xA;We can use that generality to attach an appropriate &#xA;header and trailer.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Here is the final program, which prints itself surrounded by an&#xA;arbitrary prefix and suffix.&#xA;&lt;code&gt;[P]&lt;/code&gt; denotes the &lt;i&gt;p&lt;/i&gt;-byte compressed form of the prefix &lt;code&gt;P&lt;/code&gt;;&#xA;similarly, &lt;code&gt;[S]&lt;/code&gt; denotes the &lt;i&gt;s&lt;/i&gt;-byte compressed form of the suffix &lt;code&gt;S&lt;/code&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&lt;center&gt;&#xA;&lt;table border=0&gt;&#xA;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Code&lt;/th&gt;&lt;th width=30&gt;&lt;/th&gt;&lt;th&gt;Output&lt;/th&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print prefix&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;[P]&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;P&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;print &lt;/i&gt;p&lt;i&gt;+1 bytes&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; &lt;span style=&#34;color: #005500&#34;&gt;[P] L&lt;/span&gt;&lt;/code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;[P] L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;repeat last &lt;/i&gt;p&lt;i&gt;+1 printed bytes&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;[P] L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;print 1 byte&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L1 &lt;span style=&#34;color: #005500&#34;&gt;R&lt;/span&gt;&lt;/code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;print 1 byte&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L1 &lt;span style=&#34;color: #005500&#34;&gt;L1&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L1&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R&lt;/span&gt;&lt;/code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&#34;color: #005500&#34;&gt; L1 L1 L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; L1 L1 L4&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; L1 L1 L4&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R4 L4 R4 L4&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4 L4 R4 L4&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print 4 bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L4 &lt;span style=&#34;color: #005500&#34;&gt;R4 L0 L0 L&lt;/span&gt;&lt;/code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4 L0 L0 L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;repeat last 4 printed bytes&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R4 L0 L0 L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;no-op&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L0&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;no-op&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L0&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;print &lt;/i&gt;s&lt;i&gt;+1 bytes&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;L&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; &lt;span style=&#34;color: #005500&#34;&gt;R&lt;/span&gt;&lt;/code&gt;&lt;span style=&#34;color: #005500&#34;&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;/span&gt;&lt;code&gt;&lt;span style=&#34;color: #005500&#34;&gt; [S]&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; [S]&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;repeat last &lt;/i&gt;s&lt;i&gt;+1 bytes&lt;/i&gt;&lt;/span&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;&lt;code&gt; [S]&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td align=right&gt;&lt;i&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;print suffix&lt;/span&gt;&lt;/i&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;[S]&lt;/code&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;/td&gt;&#xA; &lt;td&gt;&lt;code&gt;S&lt;/code&gt;&lt;/td&gt;&#xA;&lt;/tr&gt;&#xA;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&lt;br&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;(The byte sequence in the Output column is &lt;code&gt;&lt;i&gt;P&lt;/i&gt;&lt;/code&gt;, then&#xA;the byte sequence from the Code column, then &lt;code&gt;&lt;i&gt;S&lt;/i&gt;&lt;/code&gt;.)&#xA;&lt;/p&gt;&#xA;&#xA;&#xA;&lt;h3&gt;Self-reproducing zip files&lt;/h3&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Now the rubber meets the road.&#xA;We&#39;ve solved the main theoretical obstacle to making a self-reproducing&#xA;zip file, but there are a couple practical obstacles&#xA;still in our way.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The first obstacle is to translate our self-reproducing Lempel-Ziv program,&#xA;written in simplified opcodes, into the real opcode encoding.&#xA;&lt;a href=&#34;http://www.ietf.org/rfc/rfc1951.txt&#34;&gt;RFC 1951&lt;/a&gt; describes the DEFLATE format used in both gzip and zip: a sequence of blocks, each of which&#xA;is a sequence of opcodes encoded using Huffman codes.&#xA;Huffman codes assign different length bit strings&#xA;to different opcodes,&#xA;breaking our assumption above that opcodes have&#xA;fixed length.&#xA;But wait!&#xA;We can, with some care, find a set of fixed-size encodings&#xA;that says what we need to be able to express.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;In DEFLATE, there are literal blocks and opcode blocks.&#xA;The header at the beginning of a literal block is 5 bytes:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/zip1.png&#34;&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;If the translation of our &lt;code&gt;L&lt;/code&gt; opcodes above&#xA;are 5 bytes each, the translation of the &lt;code&gt;R&lt;/code&gt; opcodes&#xA;must also be 5 bytes each, with all the byte counts&#xA;above scaled by a factor of 5.&#xA;(For example, &lt;code&gt;L4&lt;/code&gt; now has a 20-byte argument,&#xA;and &lt;code&gt;R4&lt;/code&gt; repeats the last 20 bytes of output.)&#xA;The opcode block&#xA;with a single &lt;code&gt;repeat(20,20)&lt;/code&gt; instruction falls well short of&#xA;5 bytes:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/zip2.png&#34;&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&#xA;&lt;p class=lp&gt;Luckily, an opcode block containing two&#xA;&lt;code&gt;repeat(20,10)&lt;/code&gt; instructions has the same effect and is exactly 5 bytes:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/zip3.png&#34;&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Encoding the other sized repeats&#xA;(&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;p&lt;/i&gt;+1&lt;/span&gt; and &#xA;&lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt;)&#xA;takes more effort&#xA;and some sleazy tricks, but it turns out that &#xA;we can design 5-byte codes that repeat any amount &#xA;from 9 to 64 bytes.&#xA;For example, here are the repeat blocks for 10 bytes and for 40 bytes:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/zip4.png&#34;&gt;&#xA;&lt;br&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/zip5.png&#34;&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The repeat block for 10 bytes is two bits too short,&#xA;but every repeat block is followed by a literal block,&#xA;which starts with three zero bits and then padding&#xA;to the next byte boundary.&#xA;If a repeat block ends two bits short of a byte&#xA;but is followed by a literal block, the literal block&#39;s&#xA;padding will insert the extra two bits.&#xA;Similarly, the repeat block for 40 bytes is five bits too long,&#xA;but they&#39;re all zero bits.&#xA;Starting a literal block five bits too late&#xA;steals the bits from the padding.&#xA;Both of these tricks only work because the last 7 bits of&#xA;any repeat block are zero and the bits in the first byte&#xA;of any literal block are also zero,&#xA;so the boundary isn&#39;t directly visible.&#xA;If the literal block started with a one bit,&#xA;this sleazy trick wouldn&#39;t work.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;The second obstacle is that zip archives (and gzip files)&#xA;record a CRC32 checksum of the uncompressed data.&#xA;Since the uncompressed data is the zip archive,&#xA;the data being checksummed includes the checksum itself.&#xA;So we need to find a value &lt;i&gt;x&lt;/i&gt; such that writing &lt;i&gt;x&lt;/i&gt; into&#xA;the checksum field causes the file to checksum to &lt;i&gt;x&lt;/i&gt;.&#xA;Recursion strikes back.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;The CRC32 checksum computation interprets the entire file as a big number and computes&#xA;the remainder when you divide that number by a specific constant&#xA;using a specific kind of division.&#xA;We could go through the effort of setting up the appropriate&#xA;equations and solving for &lt;i&gt;x&lt;/i&gt;.&#xA;But frankly, we&#39;ve already solved one nasty recursive puzzle&#xA;today, and &lt;a href=&#34;http://www.youtube.com/watch?v=TQBLTB5f3j0&#34;&gt;enough is enough&lt;/a&gt;.&#xA;There are only four billion possibilities for &lt;i&gt;x&lt;/i&gt;:&#xA;we can write a program to try each in turn, until it finds one that works.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;If you want to recreate these files yourself, there are a&#xA;few more minor obstacles, like making sure the tar file is a multiple&#xA;of 512 bytes and compressing the rather large zip trailer to&#xA;at most 59 bytes so that &lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;&lt;i&gt;s&lt;/i&gt;+1&lt;/span&gt; is&#xA;at most &lt;code&gt;R&lt;/code&gt;&lt;span style=&#34;font-size: 0.8em;&#34;&gt;64&lt;/span&gt;.&#xA;But they&#39;re just a simple matter of programming.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;So there you have it:&#xA;&lt;code&gt;&lt;a href=&#34;http://swtch.com/r.gz&#34;&gt;r.gz&lt;/a&gt;&lt;/code&gt; (gzip files all the way down),&#xA;&lt;code&gt;&lt;a href=&#34;http://swtch.com/r.tar.gz&#34;&gt;r.tar.gz&lt;/a&gt;&lt;/code&gt; (gzipped tar files all the way down),&#xA;and&#xA;&lt;code&gt;&lt;a href=&#34;http://swtch.com/r.zip&#34;&gt;r.zip&lt;/a&gt;&lt;/code&gt; (zip files all the way down).&#xA;I regret that I have been unable to find any programs&#xA;that insist on decompressing these files recursively, ad infinitum.&#xA;It would have been fun to watch them squirm, but &#xA;it looks like much less sophisticated&#xA;&lt;a href=&#34;http://en.wikipedia.org/wiki/Zip_bomb&#34;&gt;zip bombs&lt;/a&gt; have spoiled the fun.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;If you&#39;re feeling particularly ambitious, here is&#xA;&lt;a href=&#34;http://swtch.com/rgzip.go&#34;&gt;rgzip.go&lt;/a&gt;,&#xA;the &lt;a href=&#34;http://golang.org/&#34;&gt;Go&lt;/a&gt; program that generated these files.&#xA;I wonder if you can create a zip file that contains a gzipped tar file&#xA;that contains the original zip file.&#xA;Ken Thompson suggested trying to make a zip file that&#xA;contains a slightly larger copy of itself, recursively,&#xA;so that as you dive down the chain of zip files&#xA;each one gets a little bigger.&#xA;(If you do manage either of these, please leave a comment.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&lt;p class=lp&gt;&lt;font size=-1&gt;P.S. I can&#39;t end the post without sharing my favorite self-reproducing program: the one-line shell script &lt;code&gt;#!/bin/cat&lt;/code&gt;&lt;/font&gt;.&#xA;&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA; &lt;/div&gt;&#xA; &lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&#xA;</content>
</entry>
<entry>
<title>UTF-8: Bits, Bytes, and Benefits</title>
<id>tag:research.swtch.com,2012:research.swtch.com/utf8</id>
<link rel="alternate" href="http://research.swtch.com/utf8"></link>
<published>2010-03-05T00:00:00-05:00</published>
<updated>2010-03-05T00:00:00-05:00</updated>
<summary type="text">The reasons to switch to UTF-8</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=pp&gt;&#xA;UTF-8 is a way to encode Unicode code points&amp;#8212;integer values from&#xA;0 through 10FFFF&amp;#8212;into a byte stream,&#xA;and it is far simpler than many people realize.&#xA;The easiest way to make it confusing or complicated&#xA;is to treat it as a black box, never looking inside.&#xA;So let&#39;s start by looking inside. Here it is:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;table cellspacing=5 cellpadding=0 border=0&gt;&#xA;&lt;tr height=10&gt;&lt;th colspan=4&gt;&lt;/th&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;th align=center colspan=2&gt;Unicode code points&lt;/th&gt;&lt;th width=10&gt;&lt;th align=center&gt;UTF-8 encoding (binary)&lt;/th&gt;&lt;/tr&gt;&#xA;&lt;tr height=10&gt;&lt;td colspan=4&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;00-7F&lt;/td&gt;&lt;td&gt;(7 bits)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td align=right&gt;0&lt;i&gt;tuvwxyz&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;0080-07FF&lt;/td&gt;&lt;td&gt;(11 bits)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td align=right&gt;110&lt;i&gt;pqrst&lt;/i&gt;&amp;nbsp;10&lt;i&gt;uvwxyz&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right&gt;0800-FFFF&lt;/td&gt;&lt;td&gt;(16 bits)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td align=right&gt;1110&lt;i&gt;jklm&lt;/i&gt;&amp;nbsp;10&lt;i&gt;npqrst&lt;/i&gt;&amp;nbsp;10&lt;i&gt;uvwxyz&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;tr&gt;&lt;td align=right valign=top&gt;010000-10FFFF&lt;/td&gt;&lt;td&gt;(21 bits)&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td align=right valign=top&gt;11110&lt;i&gt;efg&lt;/i&gt;&amp;nbsp;10&lt;i&gt;hijklm&lt;/i&gt; 10&lt;i&gt;npqrst&lt;/i&gt;&amp;nbsp;10&lt;i&gt;uvwxyz&lt;/i&gt;&lt;/td&gt;&#xA;&lt;tr height=10&gt;&lt;td colspan=4&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The convenient properties of UTF-8 are all consequences of the choice of encoding.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;i&gt;All ASCII files are already UTF-8 files.&lt;/i&gt;&lt;br&gt;&#xA;The first 128 Unicode code points are the 7-bit ASCII character set,&#xA;and UTF-8 preserves their one-byte encoding.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;ASCII bytes always represent themselves in UTF-8 files. They never appear as part of other UTF-8 sequences.&lt;/i&gt;&lt;br&gt;&#xA;All the non-ASCII UTF-8 sequences consist of bytes&#xA;with the high bit set, so if you see the byte 0x7A in a UTF-8 file,&#xA;you can be sure it represents the character &lt;code&gt;z&lt;/code&gt;.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;ASCII bytes are always represented as themselves in UTF-8 files. They cannot be hidden inside multibyte UTF-8 sequences.&lt;/i&gt;&lt;br&gt;&#xA;The ASCII &lt;code&gt;z&lt;/code&gt; 01111010 cannot be encoded as a two-byte UTF-8 sequence&#xA;11000001 10111010&lt;/code&gt;. Code points must be encoded using the shortest&#xA;possible sequence.&#xA;A corollary is that decoders must detect long-winded sequences as invalid.&#xA;In practice, it is useful for a decoder to use the Unicode replacement&#xA;character, code point FFFD, as the decoding of an invalid UTF-8 sequence&#xA;rather than stop processing the text.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;UTF-8 is self-synchronizing.&lt;/i&gt;&lt;br&gt;&#xA;Let&#39;s call a byte of the form 10&lt;i&gt;xxxxxx&lt;/i&gt;&#xA;a continuation byte.&#xA;Every UTF-8 sequence is a byte that is not a continuation byte&#xA;followed by zero or more continuation bytes.&#xA;If you start processing a UTF-8 file at an arbitrary point,&#xA;you might not be at the beginning of a UTF-8 encoding,&#xA;but you can easily find one: skip over&#xA;continuation bytes until you find a non-continuation byte.&#xA;(The same applies to scanning backward.)&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;Substring search is just byte string search.&lt;/i&gt;&lt;br&gt;&#xA;Properties 2, 3, and 4 imply that given a string&#xA;of correctly encoded UTF-8, the only way those bytes&#xA;can appear in a larger UTF-8 text is when they represent the&#xA;same code points. So you can use any 8-bit safe byte at a time &#xA;search function, like &lt;code&gt;strchr&lt;/code&gt; or &lt;code&gt;strstr&lt;/code&gt;, to run the search.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;Most programs that handle 8-bit files safely can handle UTF-8 safely.&lt;/i&gt;&lt;br&gt;&#xA;This also follows from Properties 2, 3, and 4.&#xA;I say &amp;ldquo;most&amp;rdquo; programs, because programs that&#xA;take apart a byte sequence expecting one character per byte&#xA;will not behave correctly, but very few programs do that.&#xA;It is far more common to split input at newline characters,&#xA;or split whitespace-separated fields, or do other similar parsing&#xA;around specific ASCII characters.&#xA;For example, Unix tools like cat, cmp, cp, diff, echo, head, tail, and tee&#xA;can process UTF-8 files as if they were plain ASCII files.&#xA;Most operating system kernels should also be able to handle&#xA;UTF-8 file names without any special arrangement, since the&#xA;only operations done on file names are comparisons&#xA;and splitting at &lt;code&gt;/&lt;/code&gt;.&#xA;In contrast, tools like grep, sed, and wc, which inspect arbitrary&#xA;individual characters, do need modification.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;UTF-8 sequences sort in code point order.&lt;/i&gt;&lt;br&gt;&#xA;You can verify this by inspecting the encodings in the table above.&#xA;This means that Unix tools like join, ls, and sort (without options) don&#39;t need to handle&#xA;UTF-8 specially.&#xA;&lt;/li&gt;&#xA;&#xA;&lt;li&gt;&lt;i&gt;UTF-8 has no &amp;ldquo;byte order.&amp;rdquo;&lt;/i&gt;&lt;br&gt;&#xA;UTF-8 is a byte encoding. It is not little endian or big endian.&#xA;Unicode defines a byte order mark (BOM) code point FFFE,&#xA;which are used to determine the byte order of a stream of&#xA;raw 16-bit values, like UCS-2 or UTF-16.&#xA;It has no place in a UTF-8 file.&#xA;Some programs like to write a UTF-8-encoded BOM&#xA;at the beginning of UTF-8 files, but this is unnecessary&#xA;(and annoying to programs that don&#39;t expect it).&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;UTF-8 does give up the ability to do random&#xA;access using code point indices.&#xA;Programs that need to jump to the &lt;i&gt;n&lt;/i&gt;th&#xA;Unicode code point in a file or on a line&amp;#8212;text editors are the canonical example&amp;#8212;will&#xA;typically convert incoming UTF-8 to an internal representation&#xA;like an array of code points and then convert back to UTF-8&#xA;for output,&#xA;but most programs are simpler when written to manipulate UTF-8 directly.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Programs that make UTF-8 more complicated than it needs to be&#xA;are typically trying to be too general,&#xA;not wanting to make assumptions that might not be true of&#xA;other encodings.&#xA;But there are good tools to convert other encodings to UTF-8,&#xA;and it is slowly becoming the standard encoding:&#xA;even the fraction of web pages&#xA;written in UTF-8 is&#xA;&lt;a href=&#34;http://googleblog.blogspot.com/2010/01/unicode-nearing-50-of-web.html&#34;&gt;nearing 50%&lt;/a&gt;.&#xA;UTF-8 was explicitly designed&#xA;to have these nice properties. Take advantage of them.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;For more on UTF-8, see &amp;ldquo;&lt;a href=&#34;http://plan9.bell-labs.com/sys/doc/utf.html&#34;&gt;Hello World&#xA;or&#xA;Καλημέρα κόσμε&#xA;or&#xA;こんにちは 世界&lt;/a&gt;,&amp;rdquo; by Rob Pike&#xA;and Ken Thompson, and also this &lt;a href=&#34;http://www.cl.cam.ac.uk/~mgk25/ucs/utf-8-history.txt&#34;&gt;history&lt;/a&gt;.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&#xA;&lt;font size=-1&gt;&#xA;&lt;p class=lp&gt;&#xA;Notes: Property 6 assumes the tools do not strip the high bit from each byte.&#xA;Such mangling was common years ago but is very uncommon now.&#xA;Property 7 assumes the comparison is done treating&#xA;the bytes as unsigned, but such behavior is mandated&#xA;by the ANSI C standard for &lt;code&gt;memcmp&lt;/code&gt;,&#xA;&lt;code&gt;strcmp&lt;/code&gt;, and &lt;code&gt;strncmp&lt;/code&gt;.&#xA;&lt;/p&gt;&#xA;&lt;/font&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
<entry>
<title>Computing History at Bell Labs</title>
<id>tag:research.swtch.com,2012:research.swtch.com/bell-labs</id>
<link rel="alternate" href="http://research.swtch.com/bell-labs"></link>
<published>2008-04-09T00:00:00-04:00</published>
<updated>2008-04-09T00:00:00-04:00</updated>
<summary type="text">Doug McIlroy&#39;s rememberances</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=pp&gt;&#xA;In 1997, on his retirement from Bell Labs, &lt;a href=&#34;http://www.cs.dartmouth.edu/~doug/&#34;&gt;Doug McIlroy&lt;/a&gt; gave a&#xA;fascinating talk about the &amp;ldquo;&lt;a href=&#34;https://web.archive.org/web/20081022192943/http://cm.bell-labs.com/cm/cs/doug97.html&#34;&gt;&lt;b&gt;History of Computing at Bell Labs&lt;/b&gt;&lt;/a&gt;.&amp;rdquo;&#xA;Almost ten years ago I transcribed the audio but never did anything with it.&#xA;The transcript is below.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;My favorite parts of the talk are the description of the bi-quinary decimal relay calculator&#xA;and the description of a team that spent over a year tracking down a race condition bug in&#xA;a missile detector (reliability was king: today you&#39;d just stamp&#xA;&amp;ldquo;cannot reproduce&amp;rdquo; and send the report back).&#xA;But the whole thing contains many fantastic stories.&#xA;It&#39;s well worth the read or listen.&#xA;I also like his recollection of programming using cards: &amp;ldquo;It&#39;s the kind of thing you can be nostalgic about, but it wasn&#39;t actually fun.&amp;rdquo;&#xA;&lt;/p&gt;&#xA;&#xA;&#xA;&lt;p class=pp&gt;&#xA;For more information, Bernard D. Holbrook and W. Stanley Brown&#39;s 1982&#xA;technical report&#xA;&#xA;&amp;ldquo;&lt;a href=&#34;cstr99.pdf&#34;&gt;A History of Computing Research at Bell Laboratories (1937-1975)&lt;/a&gt;&amp;rdquo;&#xA;covers the earlier history in more detail.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p&gt;&lt;i&gt;Corrections added August 19, 2009. Links updated May 16, 2018.&lt;/i&gt;&lt;/p&gt;&#xA;&#xA;&lt;br&gt;&#xA;&lt;br&gt;&#xA;&#xA;&lt;p class=lp&gt;&lt;i&gt;Transcript of &amp;ldquo;&lt;a href=&#34;https://web.archive.org/web/20081022192943/http://cm.bell-labs.com/cm/cs/doug97.html&#34;&gt;History of Computing at Bell Labs:&lt;/a&gt;&amp;rdquo;&lt;/i&gt;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Computing at Bell Labs is certainly an outgrowth of the&#xA;&lt;a href=&#34;https://web.archive.org/web/20080622172015/http://cm.bell-labs.com/cm/ms/history/history.html&#34;&gt;mathematics department&lt;/a&gt;, which grew from that first hiring&#xA;in 1897, G A Campbell. When Bell Labs was formally founded&#xA;in 1925, what it had been was the engineering department&#xA;of Western Electric.&#xA;When it was formally founded in 1925,&#xA;almost from the beginning there was a math department with Thornton Fry as the department head, and if you look at some of Fry&#39;s work, it turns out that&#xA;he was fussing around in 1929 with trying to discover&#xA;information theory. It didn&#39;t actually gel until twenty years later with Shannon.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;1:10&lt;/span&gt;&#xA;Of course, most of the mathematics at that time was continuous.&#xA;One was interested in analyzing circuits and propagation. And indeed, this is what led to the growth of computing in Bell Laboratories. The computations could not all be done symbolically. There were not closed form solutions. There was lots of numerical computation done.&#xA;The math department had a fair stable of computers,&#xA;which in those days meant people. [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;2:00&lt;/span&gt;&#xA;And in the late &#39;30s, &lt;a href=&#34;http://en.wikipedia.org/wiki/George_Stibitz&#34;&gt;George Stibitz&lt;/a&gt; had an idea that some of&#xA;the work that they were doing on hand calculators might be&#xA;automated by using some of the equipment that the Bell System&#xA;was installing in central offices, namely relay circuits.&#xA;He went home, and on his kitchen table, he built out of relays&#xA;a binary arithmetic circuit. He decided that binary was really&#xA;the right way to compute.&#xA;However, when he finally came to build some equipment,&#xA;he determined that binary to decimal conversion and&#xA;decimal to binary conversion was a drag, and he didn&#39;t&#xA;want to put it in the equipment, and so he finally built&#xA;in 1939, a relay calculator that worked in decimal,&#xA;and it worked in complex arithmetic.&#xA;Do you have a hand calculator now that does complex arithmetic?&#xA;Ten-digit, I believe, complex computations: add, subtract,&#xA;multiply, and divide.&#xA;The I/O equipment was teletypes, so essentially all the stuff to make such&#xA;machines out of was there.&#xA;Since the I/O was teletypes, it could be remotely accessed,&#xA;and there were in fact four stations in the West Street Laboratories&#xA;of Bell Labs. West Street is down on the left side of Manhattan.&#xA;I had the good fortune to work there one summer, right next to a&#xA;district where you&#39;re likely to get bowled over by rolling ?beads? hanging from racks or tumbling ?cabbages?. The building is still there. It&#39;s called &lt;a href=&#34;http://query.nytimes.com/gst/fullpage.html?res=950DE3DB1F38F931A35751C0A96F948260&#34;&gt;Westbeth Apartments&lt;/a&gt;. It&#39;s now an artist&#39;s colony.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;4:29&lt;/span&gt;&#xA;Anyway, in West Street, there were four separate remote stations from which the complex calculator could be accessed. It was not time sharing. You actually reserved your time on the machine, and only one of the four terminals worked at a time.&#xA;In 1940, this machine was shown off to the world at the AMS annual convention, which happened to be held in Hanover at Dartmouth that year, and mathematicians could wonder at remote computing, doing computation on an electromechanical calculator at 300 miles away.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;5:22&lt;/span&gt;&#xA;Stibitz went on from there to make a whole series of relay machines. Many of them were made for the government during the war. They were named, imaginatively, Mark I through Mark VI.&#xA;I have read some of his patents. They&#39;re kind of fun. One is a patent on conditional transfer. [laughter] And how do you do a conditional transfer?&#xA;Well these gadgets were, the relay calculator was run from your fingers, I mean the complex calculator.&#xA;The later calculators, of course, if your fingers were a teletype, you could perfectly well feed a paper tape in,&#xA;because that was standard practice. And these later machines were intended really to be run more from paper tape.&#xA;And the conditional transfer was this: you had two teletypes, and there&#39;s a code that says &#34;time to read from the other teletype&#34;. Loops were of course easy to do. You take paper and [laughter; presumably Doug curled a piece of paper to form a physical loop].&#xA;These machines never got to the point of having stored programs.&#xA;But they got quite big. I saw, one of them was here in 1954, and I did see it, behind glass, and if you&#39;ve ever seen these machines in the, there&#39;s one in the Franklin Institute in Philadelphia, and there&#39;s one in the Science Museum in San Jose, you know these machines that drop balls that go wandering sliding around and turning battle wheels and ringing bells and who knows what. It kind of looked like that.&#xA;It was a very quiet room, with just a little clicking of relays, which is what a central office used to be like. It was the one air-conditioned room in Murray Hill, I think. This machine ran, the Mark VI, well I think that was the Mark V, the Mark VI actually went to Aberdeen.&#xA;This machine ran for a good number of years, probably six, eight.&#xA;And it is said that it never made an undetected error. [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;8:30&lt;/span&gt;&#xA;What that means is that it never made an error that it did not diagnose itself and stop.&#xA;Relay technology was very very defensive. The telephone switching system had to work. It was full of self-checking,&#xA;and so were the calculators, so were the calculators that Stibitz made.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;9:04&lt;/span&gt;&#xA;Arithmetic was done in bi-quinary, a two out of five representation for decimal integers, and if there weren&#39;t exactly two out of five relays activated it would stop.&#xA;This machine ran unattended over the weekends. People would&#xA;bring their tapes in, and the operator would paste everybody&#39;s tapes together.&#xA;There was a beginning of job code on the tape and there was also a time indicator.&#xA;If the machine ran out of time, it automatically stopped and went to the next job. If the machine caught itself in an error, it backed up to the current job and tried it again.&#xA;They would load this machine on Friday night, and on Monday morning, all the tapes, all the entries would be available on output tapes.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Question: I take it they were using a different representation for loops&#xA;and conditionals by then.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Doug: Loops were done actually by they would run back and forth across the tape now, on this machine.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;10:40&lt;/span&gt;&#xA;Then came the transistor in &#39;48.&#xA;At Whippany, they actually had a transistorized computer, which was a respectable minicomputer, a box about this big, running in 1954, it ran from 1954 to 1956 solidly as a test run.&#xA;The notion was that this computer might fly in an airplane.&#xA;And during that two-year test run, one diode failed.&#xA;In 1957, this machine called &lt;a href=&#34;http://www.cedmagic.com/history/tradic-transistorized.html&#34;&gt;TRADIC&lt;/a&gt;, did in fact fly in an airplane, but to the best of my knowledge, that machine was a demonstration machine. It didn&#39;t turn into a production machine.&#xA;About that time, we started buying commercial machines.&#xA;It&#39;s wonderful to think about the set of different architectures that existed in that time. The first machine we got was called a &lt;a href=&#34;http://www.columbia.edu/acis/history/cpc.html&#34;&gt;CPC from IBM&lt;/a&gt;. And all it was was a big accounting machine with a very special plugboard on the side that provided an interpreter for doing ten-digit decimal arithmetic, including&#xA;opcodes for the trig functions and square root.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;12:30&lt;/span&gt;&#xA;It was also not a computer as we know it today,&#xA;because it wasn&#39;t stored program, it had twenty-four memory locations as I recall, and it took its program instead of from tapes, from cards. This was not a total advantage. A tape didn&#39;t get into trouble if you dropped it on the floor. [laughter].&#xA;CPC, the operator would stand in front of it, and there, you&#xA;would go through loops by taking cards out, it took human intervention, to take the cards out of the output of the card reader and put them in the ?top?. I actually ran some programs on the CPC ?...?. It&#39;s the kind of thing you can be nostalgic about, but it wasn&#39;t actually fun.&#xA;[laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;13:30&lt;/span&gt;&#xA;The next machine was an &lt;a href=&#34;http://www.columbia.edu/acis/history/650.html&#34;&gt;IBM 650&lt;/a&gt;, and here, this was a stored program, with the memory being on drum. There was no operating system for it. It came with a manual: this is what the machine does. And Michael Wolontis made an interpreter called the &lt;a href=&#34;http://hopl.info/showlanguage2.prx?exp=6497&#34;&gt;L1 interpreter&lt;/a&gt; for this machine, so you could actually program in, the manual told you how to program in binary, and L1 allowed you to give something like 10 for add and 9 for subtract, and program in decimal instead. And of course that machine required interesting optimization, because it was a nice thing if the next program step were stored somewhere -- each program step had the address of the following step in it, and you would try to locate them around the drum so to minimize latency. So there were all kinds of optimizers around, but I don&#39;t think Bell Labs made ?...? based on this called &#34;soap&#34; from Carnegie Mellon. That machine didn&#39;t last very long. Fortunately, a machine with core memory came out from IBM in about &#39;56, the 704. Bell Labs was a little slow in getting one, in &#39;58. Again, the machine came without an operating system.&#xA;In fact, but it did have Fortran, which really changed the world.&#xA;It suddenly made it easy to write programs. But the way Fortran came from IBM, it came with a thing called the Fortran Stop Book.&#xA;This was a list of what happened, a diagnostic would execute the halt instruction, the operator would go read the panel lights and discover where the machine had stopped, you would then go look up in the stop book what that meant.&#xA;Bell Labs, with George Mealy and Glenn Hanson, made an operating system, and one of the things they did was to bring the stop book to heel. They took the compiler, replaced all the stop instructions with jumps to somewhere, and allowed the program instead of stopping to go on to the next trial.&#xA;By the time I arrived at Bell Labs in 1958, this thing was running nicely.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;16:36&lt;/span&gt;&#xA;Bell Labs continued to be a major player in operating systems.&#xA;This was called BESYS. BE was the share abbreviation for Bell Labs. Each company that belonged to Share, which was the IBM users group, ahd a two letter abbreviation. It&#39;s hard to imagine taking all the computer users now and giving them a two-letter abbreviation. BESYS went through many generations, up to BESYS 5, I believe. Each one with innovations. IBM delivered a machine, the 7090, in 1960. This machine had interrupts in it, but IBM didn&#39;t use them. But BESYS did. And that sent IBM back to the drawing board to make it work. [Laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;17:48&lt;/span&gt;&#xA;Rob Pike: It also didn&#39;t have memory protection.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Doug: It didn&#39;t have memory protection either, and a lot of people actually got IBM to put memory protection in the 7090, so that one could leave the operating system resident in the presence of a wild program, an idea that the PC didn&#39;t discover until, last year or something like that. [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Big players then, &lt;a href=&#34;http://en.wikipedia.org/wiki/Richard_Hamming&#34;&gt;Dick Hamming&lt;/a&gt;, a name that I&#39;m sure everybody knows,&#xA;was sort of the numerical analysis guru, and a seer.&#xA;He liked to make outrageous predictions. He predicted in 1960, that half of Bell Labs was going to be busy doing something with computers eventually.&#xA;?...? exaggerating some ?...? abstract in his thought.&#xA;He was wrong.&#xA;Half was a gross underestimate. Dick Hamming retired twenty years ago, and just this June he completed his full twenty years term in the Navy, which entitles him again to retire from the Naval Postgraduate Institute in Monterey. Stibitz, incidentally died, I think within the last year.&#xA;He was doing medical instrumentation at Dartmouth essentially, near the end.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;20:00&lt;/span&gt;&#xA;Various problems intrigued, besides the numerical problems, which in fact were stock in trade, and were the real justification for buying machines, until at least the &#39;70s I would say. But some non-numerical problems had begun to tickle the palette of the math department. Even G A Campbell got interested in graph theory, the reason being he wanted to think of all the possible ways you could take the three wires and the various parts of the telephone and connect them together, and try permutations to see what you could do about reducing side ?...? by putting things into the various parts of the circuit, and devised every possibly way of connecting the telephone up. And that was sort of the beginning of combinatorics at Bell Labs. John Reardon, a mathematician parlayed this into a major subject. Two problems which are now deemed as computing problems, have intrigued the math department for a very long time, and those are the minimum spanning tree problem, and the wonderfully ?comment about Joe Kruskal, laughter?&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;21:50&lt;/span&gt;&#xA;And in the 50s Bob Prim and Kruskal, who I don&#39;t think worked on the Labs at that point, invented algorithms for the minimum spanning tree. Somehow or other, computer scientists usually learn these algorithms, one of the two at least, as Dijkstra&#39;s algorithm, but he was a latecomer.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Another pet was the traveling salesman. There&#39;s been a long list of people at Bell Labs who played with that: Shen Lin and Ron Graham and David Johnson and dozens more, oh and ?...?. And then another problem is the Steiner minimum spanning tree, where you&#39;re allowed to add points to the graph. Every one of these problems grew, actually had a justification in telephone billing. One jurisdiction or another would specify that the way you bill for a private line network was in one jurisdiction by the minimum spanning tree. In another jurisdiction, by the traveling salesman route. NP-completeness wasn&#39;t a word in the vocabulary of ?...? [laughter]. And the &lt;a href=&#34;http://en.wikipedia.org/wiki/Steiner_tree&#34;&gt;Steiner problem&lt;/a&gt; came up because customers discovered they could beat the system by inventing offices in the middle of Tennessee that had nothing to do with their business, but they could put the office at a Steiner point and reduce their phone bill by adding to what the service that the Bell System had to give them. So all of these problems actually had some justification in billing besides the fun.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;24:15&lt;/span&gt;&#xA;Come the 60s, we actually started to hire people for computing per se. I was perhaps the third person who was hired with a Ph.D. to help take care of the computers and I&#39;m told that the then director and head of the math department, Hendrick Bode, had said to his people, &#34;yeah, you can hire this guy, instead of a real mathematician, but what&#39;s he gonna be doing in five years?&#34; [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;25:02&lt;/span&gt;&#xA;Nevertheless, we started hiring for real in about &#39;67. Computer science got split off from the math department. I had the good fortune to move into the office that I&#39;ve been in ever since then. Computing began to make, get a personality of its own. One of the interesting people that came to Bell Labs for a while was Hao Wang. Is his name well known? [Pause] One nod. Hao Wang was a philosopher and logician, and we got a letter from him in England out of the blue saying &#34;hey you know, can I come and use your computers? I have an idea about theorem proving.&#34; There was theorem proving in the air in the late 50s, and it was mostly pretty thin stuff. Obvious that the methods being proposed wouldn&#39;t possibly do anything more difficult than solve tic-tac-toe problems by enumeration. Wang had a notion that he could mechanically prove theorems in the style of Whitehead and Russell&#39;s great treatise Principia Mathematica in the early patr of the century. He came here, learned how to program in machine language, and took all of Volume I of Principia Mathematica --&#xA;if you&#39;ve ever hefted Principia, well that&#39;s about all it&#39;s good for, it&#39;s a real good door stop. It&#39;s really big. But it&#39;s theorem after theorem after theorem in propositional calculus. Of course, there&#39;s a decision procedure for propositional calculus, but he was proving them more in the style of Whitehead and Russell. And when he finally got them all coded and put them into the computer, he proved the entire contents of this immense book in eight minutes.&#xA;This was actually a neat accomplishment. Also that was the beginning of all the language theory. We hired people like &lt;a href=&#34;http://www1.cs.columbia.edu/~aho/&#34;&gt;Al Aho&lt;/a&gt; and &lt;a href=&#34;http://infolab.stanford.edu/~ullman/&#34;&gt;Jeff Ullman&lt;/a&gt;, who probed around every possible model of grammars, syntax, and all of the things that are now in the standard undergraduate curriculum, were pretty well nailed down here, on syntax and finite state machines and so on were pretty well nailed down in the 60s. Speaking of finite state machines, in the 50s, both Mealy and Moore, who have two of the well-known models of finite state machines, were here.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;28:40&lt;/span&gt;&#xA;During the 60s, we undertook an enormous development project in the guise of research, which was &lt;a href=&#34;http://www.multicians.org/&#34;&gt;MULTICS&lt;/a&gt;, and it was the notion of MULTICS was computing was the public utility of the future. Machines were very expensive, and ?indeed? like you don&#39;t own your own electric generator, you rely on the power company to do generation for you, and it was seen that this was a good way to do computing -- time sharing -- and it was also recognized that shared data was a very good thing. MIT pioneered this and Bell Labs joined in on the MULTICS project, and this occupied five years of system programming effort, until Bell Labs pulled out, because it turned out that MULTICS was too ambitious for the hardware at the time, and also with 80 people on it was not exactly a research project. But, that led to various people who were on the project, in particular &lt;a href=&#34;http://en.wikipedia.org/wiki/Ken_Thompson&#34;&gt;Ken Thompson&lt;/a&gt; -- right there -- to think about how to -- &lt;a href=&#34;http://en.wikipedia.org/wiki/Dennis_Ritchie&#34;&gt;Dennis Ritchie&lt;/a&gt; and Rudd Canaday were in on this too -- to think about how you might make a pleasant operating system with a little less resources.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;30:30&lt;/span&gt;&#xA;And Ken found -- this is a story that&#39;s often been told, so I won&#39;t go into very much of unix -- Ken found an old machine cast off in the corner, the &lt;a href=&#34;http://en.wikipedia.org/wiki/GE-600_series&#34;&gt;PDP-7&lt;/a&gt;, and put up this little operating system on it, and we had immense &lt;a href=&#34;http://en.wikipedia.org/wiki/GE-600_series&#34;&gt;GE635&lt;/a&gt; available at the comp center at the time, and I remember as the department head, muscling in to use this little computer to be, to get to be Unix&#39;s first user, customer, because it was so much pleasanter to use this tiny machine than it was to use the big and capable machine in the comp center. And of course the rest of the story is known to everybody and has affected all college campuses in the country.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;31:33&lt;/span&gt;&#xA;Along with the operating system work, there was a fair amount of language work done at Bell Labs. Often curious off-beat languages. One of my favorites was called &lt;a href=&#34;http://hopl.murdoch.edu.au/showlanguage.prx?exp=6937&amp;language=BLODI-B&#34;&gt;Blodi&lt;/a&gt;, B L O D I, a block diagram compiler by Kelly and Vyssotsky. Perhaps the most interesting early uses of computers in the sense of being unexpected, were those that came from the acoustics research department, and what the Blodi compiler was invented in the acoustic research department for doing digital simulations of sample data system. DSPs are classic sample data systems,&#xA;where instead of passing analog signals around, you pass around streams of numerical values. And Blodi allowed you to say here&#39;s a delay unit, here&#39;s an amplifier, here&#39;s an adder, the standard piece parts for a sample data system, and each one was described on a card, and with description of what it&#39;s wired to. It was then compiled into one enormous single straight line loop for one time step. Of course, you had to rearrange the code because some one part of the sample data system would feed another and produce really very efficient 7090 code for simulating sample data systems.&#xA;By in large, from that time forth, the acoustic department stopped making hardware. It was much easier to do signal processing digitally than previous ways that had been analog. Blodi had an interesting property. It was the only programming language I know where -- this is not my original observation, Vyssotsky said -- where you could take the deck of cards, throw it up the stairs, and pick them up at the bottom of the stairs, feed them into the computer again, and get the same program out. Blodi had two, aside from syntax diagnostics, it did have one diagnostic when it would fail to compile, and that was &#34;somewhere in your system is a loop that consists of all delays or has no delays&#34; and you can imagine how they handled that.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;35:09&lt;/span&gt;&#xA;Another interesting programming language of the 60s was &lt;a href=&#34;http://www.knowltonmosaics.com/&#34;&gt;Ken Knowlten&lt;/a&gt;&#39;s &lt;a href=&#34;http://beflix.com/beflix.php&#34;&gt;Beflix&lt;/a&gt;. This was for making movies on something with resolution kind of comparable to 640x480, really coarse, and the&#xA;programming notion in here was bugs. You put on your grid a bunch of bugs, and each bug carried along some data as baggage,&#xA;and then you would do things like cellular automata operations. You could program it or you could kind of let it go by itself. If a red bug is next to a blue bug then it turns into a green bug on the following step and so on. &lt;span style=&#34;font-size: 0.7em;&#34;&gt;36:28&lt;/span&gt; He and Lillian Schwartz made some interesting abstract movies at the time. It also did some interesting picture processing. One wonderful picture of a reclining nude, something about the size of that blackboard over there, all made of pixels about a half inch high each with a different little picture in it, picked out for their density, and so if you looked at it close up it consisted of pickaxes and candles and dogs, and if you looked at it far enough away, it was a &lt;a href=&#34;http://blog.the-eg.com/2007/12/03/ken-knowlton-mosaics/&#34;&gt;reclining nude&lt;/a&gt;. That picture got a lot of play all around the country.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Lorinda Cherry: That was with Leon, wasn&#39;t it? That was with &lt;a href=&#34;https://en.wikipedia.org/wiki/Leon_Harmon&#34;&gt;Leon Harmon&lt;/a&gt;.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Doug: Was that Harmon?&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Lorinda: ?...?&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Doug: Harmon was also an interesting character. He did more things than pictures. I&#39;m glad you reminded me of him. I had him written down here. Harmon was a guy who among other things did a block diagram compiler for writing a handwriting recognition program. I never did understand how his scheme worked, and in fact I guess it didn&#39;t work too well. [laughter]&#xA;It didn&#39;t do any production ?things? but it was an absolutely&#xA;immense sample data circuit for doing handwriting recognition.&#xA;Harmon&#39;s most famous work was trying to estimate the information content in a face. And every one of these pictures which are a cliche now, that show a face digitized very coarsely, go back to Harmon&#39;s &lt;a href=&#34;https://web.archive.org/web/20080807162812/http://www.doubletakeimages.com/history.htm&#34;&gt;first psychological experiments&lt;/a&gt;, when he tried to find out how many bits of picture he needed to try to make a face recognizable. He went around and digitized about 256 faces from Bell Labs and did real psychological experiments asking which faces could be distinguished from other ones. I had the good fortune to have one of the most distinguishable faces, and consequently you&#39;ll find me in freshman psychology texts through no fault of my own.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;39:15&lt;/span&gt;&#xA;Another thing going on the 60s was the halting beginning here of interactive computing. And again the credit has to go to the acoustics research department, for good and sufficient reason. They wanted to be able to feed signals into the machine, and look at them, and get them back out. They bought yet another weird architecture machine called the &lt;a href=&#34;http://www.piercefuller.com/library/pb250.html&#34;&gt;Packard Bell 250&lt;/a&gt;, where the memory elements were &lt;a href=&#34;http://en.wikipedia.org/wiki/Delay_line_memory&#34;&gt;mercury delay lines&lt;/a&gt;.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Question: Packard Bell?&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Doug: Packard Bell, same one that makes PCs today.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;40:10&lt;/span&gt;&#xA;They hung this off of the comp center 7090 and put in a scheme for quickly shipping jobs into the job stream on the 7090. The Packard Bell was the real-time terminal that you could play with and repair stuff, ?...? off the 7090, get it back, and then you could play it. From that grew some graphics machines also, built by ?...? et al. And it was one of the old graphics machines&#xA;in fact that Ken picked up to build Unix on.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;40:55&lt;/span&gt;&#xA;Another thing that went on in the acoustics department was synthetic speech and music. &lt;a href=&#34;http://csounds.com/mathews/index.html&#34;&gt;Max Mathews&lt;/a&gt;, who was the the director of the department has long been interested in computer music. In fact since retirement he spent a lot of time with Pierre Boulez in Paris at a wonderful institute with lots of money simply for making synthetic music. He had a language called Music 5. Synthetic speech or, well first of all simply speech processing was pioneered particularly by &lt;a href=&#34;http://en.wikipedia.org/wiki/John_Larry_Kelly,_Jr&#34;&gt;John Kelly&lt;/a&gt;. I remember my first contact with speech processing. It was customary for computer operators, for the benefit of computer operators, to put a loudspeaker on the low bit of some register on the machine, and normally the operator would just hear kind of white noise. But if you got into a loop, suddenly the machine would scream, and this signal could be used to the operator &#34;oh the machines in a loop. Go stop it and go on to the next job.&#34; I remember feeding them an Ackermann&#39;s function routine once. [laughter] They were right. It was a silly loop. But anyway. One day, the operators were ?...?. The machine started singing. Out of the blue. &amp;ldquo;Help! I&#39;m caught in a loop.&amp;rdquo;. [laughter] And in a broad Texas accent, which was the recorded voice of John Kelly.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;43:14&lt;/span&gt;&#xA;However. From there Kelly went on to do some speech synthesis. Of course there&#39;s been a lot more speech synthesis work done since, by &lt;span style=&#34;font-size: 0.7em;&#34;&gt;43:31&lt;/span&gt; folks like Cecil Coker, Joe Olive. But they produced a record, which unfortunately I can&#39;t play because records are not modern anymore. And everybody got one in the Bell Labs Record, which is a magazine, contained once a record from the acoustics department, with both speech and music and one very famous combination where the computer played and sang &#34;A Bicycle Built For Two&#34;.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;?...?&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;44:32&lt;/span&gt;&#xA;At the same time as all this stuff is going on here, needless&#xA;to say computing is going on in the rest of the Labs. it was about early 1960 when the math department lost its monopoly on computing machines and other people started buying them too, but for switching. The first experiments with switching computers were operational in around 1960. They were planned for several years prior to that; essentially as soon as the transistor was invented, the making of electronic rather than electromechanical switching machines was anticipated. Part of the saga of the switching machines is cheap memory. These machines had enormous memories -- thousands of words. [laughter] And it was said that the present worth of each word of memory that programmers saved across the Bell System was something like eleven dollars, as I recall. And it was worthwhile to struggle to save some memory. Also, programs were permanent. You were going to load up the switching machine with switching program and that was going to run. You didn&#39;t change it every minute or two. And it would be cheaper to put it in read only memory than in core memory. And there was a whole series of wild read-only memories, both tried and built.&#xA;The first experimental Essex System had a thing called the flying spot store&#xA;which was large photographic plates with bits on them and CRTs projecting on the plates and you would detect underneath on the photodetector whether the bit was set or not. That was the program store of Essex. The program store of the first ESS systems consisted of twistors, which I actually am not sure I understand to this day, but they consist of iron wire with a copper wire wrapped around them and vice versa. There were also experiments with an IC type memory called the waffle iron. Then there was a period when magnetic bubbles were all the rage. As far as I know, although microelectronics made a lot of memory, most of the memory work at Bell Labs has not had much effect on ?...?. Nice tries though.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;48:28&lt;/span&gt;&#xA;Another thing that folks began to work on was the application of (and of course, right from the start) computers to data processing. When you owned equipment scattered through every street in the country, and you have a hundred million customers, and you have bills for a hundred million transactions a day, there&#39;s really some big data processing going on. And indeed in the early 60s, AT&amp;T was thinking of making its own data processing computers solely for billing. Somehow they pulled out of that, and gave all the technology to IBM, and one piece of that technology went into use in high end equipment called tractor tapes. Inch wide magnetic tapes that would be used for a while.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;49:50&lt;/span&gt;&#xA;By in large, although Bell Labs has participated until fairly recently in data processing in quite a big way, AT&amp;T never really quite trusted the Labs to do it right because here is where the money is. I can recall one occasion when during strike of temporary employees, a fill-in employee like from the&#xA;Laboratories and so on, lost a day&#39;s billing tape in Chicago. And that was a million dollars. And that&#39;s generally speaking the money people did not until fairly recently trust Bell Labs to take good care of money, even though they trusted the Labs very well to make extremely reliable computing equipment for switches.&#xA;The downtime on switches is still spectacular by any industry standards. The design for the first ones was two hours down in 40 years, and the design was met. Great emphasis on reliability and redundancy, testing.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;51:35&lt;/span&gt;&#xA;Another branch of computing was for the government. The whole Whippany Laboratories [time check]&#xA;Whippany, where we took on contracts for the government particularly in the computing era in anti-missile defense, missile defense, and underwater sound. Missile defense was a very impressive undertaking. It was about in the early &#39;63 time frame when it was estimated the amount of computation to do a reasonable job of tracking incoming missiles would be 30 M floating point operations a second. In the day of the Cray that doesn&#39;t sound like a great lot, but it&#39;s more than your high end PCs can do. And the machines were supposed to be reliable. They designed the machines at Whippany, a twelve-processor multiprocessor, to no specs, enormously rugged, one watt transistors. This thing in real life performed remarkably well. There were sixty-five missile shots, tests across the Pacific Ocean ?...? and Lorinda Cherry here actually sat there waiting for them to come in. [laughter] And only a half dozen of them really failed. As a measure of the interest in reliability, one of them failed apparently due to processor error. Two people were assigned to look at the dumps, enormous amounts of telemetry and logging information were taken during these tests, which are truly expensive to run. Two people were assigned to look at the dumps. A year later they had not found the trouble. The team was beefed up. They finally decided that there was a race condition in one circuit. They then realized that this particular kind of race condition had not been tested for in all the simulations. They went back and simulated the entire hardware system to see if its a remote possibility of any similar cases, found twelve of them, and changed the hardware. But to spend over a year looking for a bug is a sign of what reliability meant.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;54:56&lt;/span&gt;&#xA;Since I&#39;m coming up on the end of an hour, one could go on and on and on,&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Crowd: go on, go on. [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;55:10&lt;/span&gt;&#xA;Doug: I think I&#39;d like to end up by mentioning a few of the programs that have been written at Bell Labs that I think are most surprising. Of course there are lots of grand programs that have been written.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;I already mentioned the block diagram compiler.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Another really remarkable piece of work was &lt;a href=&#34;eqn.pdf&#34;&gt;eqn&lt;/a&gt;, the equation&#xA;typesetting language, which has been imitated since, by Lorinda Cherry and Brian Kernighan. The notion of taking an auditory syntax, the way people talk about equations, but only talk, this was not borrowed from any written notation before, getting the auditory one down on paper, that was very successful and surprising.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Another of my favorites, and again Lorinda Cherry was in this one, with Bob Morris, was typo. This was a program for finding spelling errors. It didn&#39;t know the first thing about spelling. It would read a document, measure its statistics, and print out the words of the document in increasing order of what it thought the likelihood of that word having come from the same statistical source as the document. The words that did not come from the statistical source of the document were likely to be typos, and now I mean typos as distinct from spelling errors, where you actually hit the wrong key. Those tend to be off the wall, whereas phonetic spelling errors you&#39;ll never find. And this worked remarkably well. Typing errors would come right up to the top of the list. A really really neat program.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;57:50&lt;/span&gt;&#xA;Another one of my favorites was by Brenda Baker called &lt;a href=&#34;http://doi.acm.org/10.1145/800168.811545&#34;&gt;struct&lt;/a&gt;, which took Fortran programs and converted them into a structured programming language called Ratfor, which was Fortran with C syntax. This seemed like a possible undertaking, like something you do by the seat of the pants and you get something out. In fact, folks at Lockheed had done things like that before. But Brenda managed to find theorems that said there&#39;s really only one form, there&#39;s a canonical form into which you can structure a Fortran program, and she did this. It took your Fortran program, completely mashed it, put it out perhaps in almost certainly a different order than it was in Fortran connected by GOTOs, without any GOTOs, and the really remarkable thing was that authors of the program who clearly knew the way they wrote it in the first place, preferred it after it had been rearranged by Brendan. I was astonished at the outcome of that project.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;59:19&lt;/span&gt;&#xA;Another first that happened around here was by Fred Grampp, who got interested in computer security. One day he decided he would make a program for sniffing the security arrangements on a computer, as a service: Fred would never do anything crooked. [laughter] This particular program did a remarkable job, and founded a whole minor industry within the company. A department was set up to take this idea and parlay it, and indeed ever since there has been some improvement in the way computer centers are managed, at least until we got Berkeley Unix.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;60:24&lt;/span&gt;&#xA;And the last interesting program that I have time to mention is one by &lt;a href=&#34;http://www.cs.jhu.edu/~kchurch/&#34;&gt;Ken Church&lt;/a&gt;. He was dealing with -- text processing has always been a continuing ?...? of the research, and in some sense it has an application to our business because we&#39;re handling speech, but he got into consulting with the department in North Carolina that has to translate manuals. There are millions of pages of manuals in the Bell System and its successors, and ever since we&#39;ve gone global, these things had to get translated into many languages.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;61:28&lt;/span&gt;&#xA;To help in this, he was making tools which would put up on the screen, graphed on the screen quickly a piece of text and its translation, because a translator, particularly a technical translator, wants to know, the last time we mentioned this word how was it translated. You don&#39;t want to be creative in translating technical text. You&#39;d like to be able to go back into the archives and pull up examples of translated text. And the neat thing here is the idea for how do you align texts in two languages. You&#39;ve got the original, you&#39;ve got the translated one, how do you bring up on the screen, the two sentences that go together? And the following scam worked beautifully. This is on western languages. &lt;span style=&#34;font-size: 0.7em;&#34;&gt;62:33&lt;/span&gt;&#xA;Simply look for common four letter tetragrams, four letter combinations between the two and as best as you can, line them up as nearly linearly with the lengths of the two types as possible. And this &lt;a href=&#34;church-tetragram.pdf&#34;&gt;very simple idea&lt;/a&gt; works like storm. Something for nothing. I like that.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;63:10&lt;/span&gt;&#xA;The last thing is one slogan that sort of got started with Unix and is just rife within the industry now. Software tools. We were making software tools in Unix before we knew we were, just like the Molière character was amazed at discovering he&#39;d been speaking prose all his life. [laughter] But then &lt;a href=&#34;http://www.amazon.com/-/dp/020103669X&#34;&gt;Kernighan and Plauger&lt;/a&gt; came along and christened what was going on, making simple generally useful and compositional programs to do one thing and do it well and to fit together. They called it software tools, made a book, wrote a book, and this notion now is abroad in the industry. And it really did begin all up in the little attic room where you [points?] sat for many years writing up here.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt; Oh I forgot to. I haven&#39;t used any slides. I&#39;ve brought some, but I don&#39;t like looking at bullets and you wouldn&#39;t either, and I forgot to show you the one exhibit I brought, which I borrowed from Bob Kurshan. When Bell Labs was founded, it had of course some calculating machines, and it had one wonderful computer. This. That was bought in 1918. There&#39;s almost no other computing equipment from any time prior to ten years ago that still exists in Bell Labs. This is an &lt;a href=&#34;http://infolab.stanford.edu/pub/voy/museum/pictures/display/2-5-Mechanical.html&#34;&gt;integraph&lt;/a&gt;. It has two styluses. You trace a curve on a piece of paper with one stylus and the other stylus draws the indefinite integral here. There was somebody in the math department who gave this service to the whole company, with about 24 hours turnaround time, calculating integrals. Our recent vice president Arno Penzias actually did, he calculated integrals differently, with a different background. He had a chemical balance, and he cut the curves out of the paper and weighed them. This was bought in 1918, so it&#39;s eighty years old. It used to be shiny metal, it&#39;s a little bit rusty now. But it still works.&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&lt;span style=&#34;font-size: 0.7em;&#34;&gt;66:30&lt;/span&gt;&#xA;Well, that&#39;s a once over lightly of a whole lot of things that have gone on at Bell Labs. It&#39;s just such a fun place that one I said I just could go on and on. If you&#39;re interested, there actually is a history written. This is only one of about six volumes, &lt;a href=&#34;http://www.amazon.com/gp/product/0932764061&#34;&gt;this&lt;/a&gt; is the one that has the mathematical computer sciences, the kind of things that I&#39;ve mostly talked about here. A few people have copies of them. For some reason, the AT&amp;T publishing house thinks that because they&#39;re history they&#39;re obsolete, and they stopped printing them. [laughter]&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;Thank you, and that&#39;s all.&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
<entry>
<title>Using Uninitialized Memory for Fun and Profit</title>
<id>tag:research.swtch.com,2012:research.swtch.com/sparse</id>
<link rel="alternate" href="http://research.swtch.com/sparse"></link>
<published>2008-03-14T00:00:00-04:00</published>
<updated>2008-03-14T00:00:00-04:00</updated>
<summary type="text">An unusual but very useful data structure</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=lp&gt;&#xA;This is the story of a clever trick that&#39;s been around for&#xA;at least 35 years, in which array values can be left&#xA;uninitialized and then read during normal operations,&#xA;yet the code behaves correctly no matter what garbage&#xA;is sitting in the array.&#xA;Like the best programming tricks, this one is the right tool for the &#xA;job in certain situations.&#xA;The sleaziness of uninitialized data&#xA;access is offset by performance improvements:&#xA;some important operations change from linear &#xA;to constant time.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Alfred Aho, John Hopcroft, and Jeffrey Ullman&#39;s 1974 book &#xA;&lt;i&gt;The Design and Analysis of Computer Algorithms&lt;/i&gt;&#xA;hints at the trick in an exercise (Chapter 2, exercise 2.12):&#xA;&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;Develop a technique to initialize an entry of a matrix to zero&#xA;the first time it is accessed, thereby eliminating the &lt;i&gt;O&lt;/i&gt;(||&lt;i&gt;V&lt;/i&gt;||&lt;sup&gt;2&lt;/sup&gt;) time&#xA;to initialize an adjacency matrix.&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Jon Bentley&#39;s 1986 book &lt;a href=&#34;http://www.cs.bell-labs.com/cm/cs/pearls/&#34;&gt;&lt;i&gt;Programming Pearls&lt;/i&gt;&lt;/a&gt; expands&#xA;on the exercise (Column 1, exercise 8; &lt;a href=&#34;http://www.cs.bell-labs.com/cm/cs/pearls/sec016.html&#34;&gt;exercise 9&lt;/a&gt; in the Second Edition):&#xA;&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&#xA;One problem with trading more space for less time is that &#xA;initializing the space can itself take a great deal of time.&#xA;Show how to circumvent this problem by designing a technique&#xA;to initialize an entry of a vector to zero the first time it is&#xA;accessed. Your scheme should use constant time for initialization&#xA;and each vector access; you may use extra space proportional&#xA;to the size of the vector. Because this method reduces &#xA;initialization time by using even more space, it should be&#xA;considered only when space is cheap, time is dear, and &#xA;the vector is sparse.&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Aho, Hopcroft, and Ullman&#39;s exercise talks about a matrix and &#xA;Bentley&#39;s exercise talks about a vector, but for now let&#39;s consider&#xA;just a simple set of integers.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;One popular representation of a set of &lt;i&gt;n&lt;/i&gt; integers ranging&#xA;from 0 to &lt;i&gt;m&lt;/i&gt; is a bit vector, with 1 bits at the&#xA;positions corresponding to the integers in the set.&#xA;Adding a new integer to the set, removing an integer&#xA;from the set, and checking whether a particular integer&#xA;is in the set are all very fast constant-time operations&#xA;(just a few bit operations each).&#xA;Unfortunately, two important operations are slow:&#xA;iterating over all the elements in the set &#xA;takes time &lt;i&gt;O&lt;/i&gt;(&lt;i&gt;m&lt;/i&gt;), as does clearing the set.&#xA;If the common case is that &#xA;&lt;i&gt;m&lt;/i&gt; is much larger than &lt;i&gt;n&lt;/i&gt;&#xA;(that is, the set is only sparsely&#xA;populated) and iterating or clearing the set &#xA;happens frequently, then it could be better to&#xA;use a representation that makes those operations&#xA;more efficient. That&#39;s where the trick comes in.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Preston Briggs and Linda Torczon&#39;s 1993 paper,&#xA;&amp;ldquo;&lt;a href=&#34;http://citeseer.ist.psu.edu/briggs93efficient.html&#34;&gt;&lt;b&gt;An Efficient Representation for Sparse Sets&lt;/b&gt;&lt;/a&gt;,&amp;rdquo;&#xA;describes the trick in detail.&#xA;Their solution represents the sparse set using an integer&#xA;array named &lt;code&gt;dense&lt;/code&gt; and an integer &lt;code&gt;n&lt;/code&gt;&#xA;that counts the number of elements in &lt;code&gt;dense&lt;/code&gt;.&#xA;The &lt;i&gt;dense&lt;/i&gt; array is simply a packed list of the elements in the&#xA;set, stored in order of insertion.&#xA;If the set contains the elements 5, 1, and 4, then &lt;code&gt;n = 3&lt;/code&gt; and&#xA;&lt;code&gt;dense[0] = 5&lt;/code&gt;, &lt;code&gt;dense[1] = 1&lt;/code&gt;, &lt;code&gt;dense[2] = 4&lt;/code&gt;:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/sparse0.png&#34; /&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Together &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;dense&lt;/code&gt; are&#xA;enough information to reconstruct the set, but this representation&#xA;is not very fast.&#xA;To make it fast, Briggs and Torczon&#xA;add a second array named &lt;code&gt;sparse&lt;/code&gt;&#xA;which maps integers to their indices in &lt;code&gt;dense&lt;/code&gt;.&#xA;Continuing the example,&#xA;&lt;code&gt;sparse[5] = 0&lt;/code&gt;, &lt;code&gt;sparse[1] = 1&lt;/code&gt;, &#xA;&lt;code&gt;sparse[4] = 2&lt;/code&gt;.&#xA;Essentially, the set is a pair of arrays that point at&#xA;each other:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/sparse0b.png&#34; /&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Adding a member to the set requires updating both of these arrays:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;add-member(i):&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dense[n] = i&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sparse[i] = n&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n++&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;It&#39;s not as efficient as flipping a bit in a bit vector, but it&#39;s &#xA;still very fast and constant time. &#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;To check whether &lt;code&gt;i&lt;/code&gt; is in the set, you verify that&#xA;the two arrays point at each other for that element:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;is-member(i):&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return sparse[i] &amp;lt; n &amp;&amp; dense[sparse[i]] == i&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;If &lt;code&gt;i&lt;/code&gt; is not in the set, then &lt;i&gt;it doesn&#39;t matter what &lt;code&gt;sparse[i]&lt;/code&gt; is set to&lt;/i&gt;:&#xA;either &lt;code&gt;sparse[i]&lt;/code&gt;&#xA;will be bigger than &lt;code&gt;n&lt;/code&gt; or it will point at a value in &#xA;&lt;code&gt;dense&lt;/code&gt; that doesn&#39;t point back at it.&#xA;Either way, we&#39;re not fooled. For example, suppose &lt;code&gt;sparse&lt;/code&gt;&#xA;actually looks like:&#xA;&lt;/p&gt;&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/sparse1.png&#34; /&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;&lt;code&gt;Is-member&lt;/code&gt; knows to ignore&#xA;members of sparse that point past &lt;code&gt;n&lt;/code&gt; or that&#xA;point at cells in &lt;code&gt;dense&lt;/code&gt; that don&#39;t point back,&#xA;ignoring the grayed out entries:&#xA;&#xA;&lt;center&gt;&#xA;&lt;img src=&#34;http://research.swtch.com/sparse2.png&#34; /&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Notice what just happened:&#xA;&lt;code&gt;sparse&lt;/code&gt; can have &lt;i&gt;any arbitrary values&lt;/i&gt; in&#xA;the positions for integers not in the set, &#xA;those values actually get used during membership&#xA;tests, and yet the membership test behaves correctly!&#xA;(This would drive &lt;a href=&#34;http://valgrind.org/&#34;&gt;valgrind&lt;/a&gt; nuts.)&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Clearing the set can be done in constant time:&#xA;&lt;/p&gt;&#xA;&lt;pre class=indent&gt;&#xA;clear-set():&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n = 0&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;Zeroing &lt;code&gt;n&lt;/code&gt; effectively clears &#xA;&lt;code&gt;dense&lt;/code&gt; (the code only ever accesses&#xA;entries in dense with indices less than &lt;code&gt;n&lt;/code&gt;), and&#xA;&lt;code&gt;sparse&lt;/code&gt; can be uninitialized, so there&#39;s no &#xA;need to clear out the old values.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;This sparse set representation has one more trick up its sleeve:&#xA;the &lt;code&gt;dense&lt;/code&gt; array allows an &#xA;efficient implementation of set iteration.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;pre class=indent&gt;&#xA;iterate():&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(i=0; i&amp;lt;n; i++)&#xA;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;yield dense[i]&#xA;&lt;/pre&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Let&#39;s compare the run times of a bit vector &#xA;implementation against the sparse set:&#xA;&lt;/p&gt;&#xA;&lt;center&gt;&#xA;&lt;table&gt;&#xA;&lt;tr&gt;&#xA; &lt;td&gt;&lt;i&gt;Operation&lt;/i&gt;&#xA; &lt;td align=center width=10&gt;&#xA; &lt;td align=center&gt;&lt;i&gt;Bit Vector&lt;/i&gt;&#xA; &lt;td align=center width=10&gt;&#xA; &lt;td align=center&gt;&lt;i&gt;Sparse set&lt;/i&gt;&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td&gt;is-member&#xA; &lt;td&gt;&#xA; &lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(1)&#xA; &lt;td&gt; &#xA; &lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(1)&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td&gt;add-member&#xA; &lt;td&gt;&#xA; &lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(1)&#xA; &lt;td&gt;&#xA; &lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(1)&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td&gt;clear-set&#xA; &lt;td&gt;&lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;m&lt;/i&gt;)&#xA; &lt;td&gt;&lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(1)&#xA;&lt;/tr&gt;&#xA;&lt;tr&gt;&#xA; &lt;td&gt;iterate&#xA; &lt;td&gt;&lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;m&lt;/i&gt;)&#xA; &lt;td&gt;&lt;td align=center&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&#xA;&lt;/tr&gt;&#xA;&lt;/table&gt;&#xA;&lt;/center&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The sparse set is as fast or faster than bit vectors for&#xA;every operation. The only problem is the space cost:&#xA;two words replace each bit.&#xA;Still, there are times when the speed differences are enough&#xA;to balance the added memory cost.&#xA;Briggs and Torczon point out that liveness sets used &#xA;during register allocation inside a compiler are usually&#xA;small and are cleared very frequently, making sparse sets the&#xA;representation of choice.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Another situation where sparse sets are the better choice&#xA;is work queue-based graph traversal algorithms.&#xA;Iteration over sparse sets visits elements&#xA;in the order they were inserted (above, 5, 1, 4),&#xA;so that new entries inserted during the iteration&#xA;will be visited later in the same iteration.&#xA;In contrast, iteration over bit vectors visits elements in&#xA;integer order (1, 4, 5), so that new elements inserted&#xA;during traversal might be missed, requiring repeated&#xA;iterations.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Returning to the original exercises, it is trivial to change&#xA;the set into a vector (or matrix) by making &lt;code&gt;dense&lt;/code&gt;&#xA;an array of index-value pairs instead of just indices.&#xA;Alternately, one might add the value to the &lt;code&gt;sparse&lt;/code&gt;&#xA;array or to a new array.&#xA;The relative space overhead isn&#39;t as bad if you would have been&#xA;storing values anyway.&#xA;&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Briggs and Torczon&#39;s paper implements additional set&#xA;operations and examines performance speedups from&#xA;using sparse sets inside a real compiler.&#xA;&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
<entry>
<title>Play Tic-Tac-Toe with Knuth</title>
<id>tag:research.swtch.com,2012:research.swtch.com/tictactoe</id>
<link rel="alternate" href="http://research.swtch.com/tictactoe"></link>
<published>2008-01-25T00:00:00-05:00</published>
<updated>2008-01-25T00:00:00-05:00</updated>
<summary type="text">The only winning move is not to play.</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=lp&gt;Section 7.1.2 of the &lt;b&gt;&lt;a href=&#34;http://www-cs-faculty.stanford.edu/~knuth/taocp.html#vol4&#34;&gt;Volume 4 pre-fascicle 0A&lt;/a&gt;&lt;/b&gt; of Donald Knuth&#39;s &lt;i&gt;The Art of Computer Programming&lt;/i&gt; is titled &amp;#8220;Boolean Evaluation.&amp;#8221; In it, Knuth considers the construction of a set of nine boolean functions telling the correct next move in an optimal game of tic-tac-toe. In a footnote, Knuth tells this story:&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&lt;p class=lp&gt;This setup is based on an exhibit from the early 1950s at the Museum of Science and Industry in Chicago, where the author was first introduced to the magic of switching circuits. The machine in Chicago, designed by researchers at Bell Telephone Laboratories, allowed me to go first; yet I soon discovered there was no way to defeat it. Therefore I decided to move as stupidly as possible, hoping that the designers had not anticipated such bizarre behavior. In fact I allowed the machine to reach a position where it had two winning moves; and it seized &lt;i&gt;both&lt;/i&gt; of them! Moving twice is of course a flagrant violation of the rules, so I had won a moral victory even though the machine had announced that I had lost.&lt;/p&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;That story alone is fairly amusing. But turning the page, the reader finds a quotation from Charles Babbage&#39;s &lt;i&gt;&lt;a href=&#34;http://onlinebooks.library.upenn.edu/webbin/book/lookupid?key=olbp36384&#34;&gt;Passages from the Life of a Philosopher&lt;/a&gt;&lt;/i&gt;, published in 1864:&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&lt;p class=lp&gt;I commenced an examination of a game called &amp;#8220;tit-tat-to&amp;#8221; ... to ascertain what number of combinations were required for all the possible variety of moves and situations. I found this to be comparatively insignificant. ... A difficulty, however, arose of a novel kind. When the automaton had to move, it might occur that there were two different moves, each equally conducive to his winning the game. ... Unless, also, some provision were made, the machine would attempt two contradictory motions.&lt;/p&gt;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;&#xA;The only real winning move is not to play.&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
<entry>
<title>Crabs, the bitmap terror!</title>
<id>tag:research.swtch.com,2012:research.swtch.com/crabs</id>
<link rel="alternate" href="http://research.swtch.com/crabs"></link>
<published>2008-01-09T00:00:00-05:00</published>
<updated>2008-01-09T00:00:00-05:00</updated>
<summary type="text">A destructive, pointless violation of the rules</summary>
<content type="html">&#xA;&lt;p&gt;&lt;p class=lp&gt;Today, window systems seem as inevitable as hierarchical file systems, a fundamental building block of computer systems. But it wasn&#39;t always that way. This paper could only have been written in the beginning, when everything about user interfaces was up for grabs.&lt;/p&gt;&#xA;&#xA;&lt;blockquote&gt;&lt;p class=lp&gt;A bitmap screen is a graphic universe where windows, cursors and icons live in harmony, cooperating with each other to achieve functionality and esthetics. A lot of effort goes into making this universe consistent, the basic law being that every window is a self contained, protected world. In particular, (1) a window shall not be affected by the internal activities of another window. (2) A window shall not be affected by activities of the window system not concerning it directly, i.e. (2.1) it shall not notice being obscured (partially or totally) by other windows or obscuring (partially or totally) other windows, (2.2) it shall not see the &lt;i&gt;image&lt;/i&gt; of the cursor sliding on its surface (it can only ask for its position).&lt;/p&gt;&#xA;&#xA;&lt;p class=pp&gt;&#xA;Of course it is difficult to resist the temptation to break these rules. Violations can be destructive or non-destructive, useful or pointless. Useful non-destructive violations include programs printing out an image of the screen, or magnifying part of the screen in a &lt;i&gt;lens&lt;/i&gt; window. Useful destructive violations are represented by the &lt;i&gt;pen&lt;/i&gt; program, which allows one to scribble on the screen. Pointless non-destructive violations include a magnet program, where a moving picture of a magnet attracts the cursor, so that one has to continuously pull away from it to keep working. The first pointless, destructive program we wrote was &lt;i&gt;crabs&lt;/i&gt;.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&#xA;&lt;p class=lp&gt;As the crabs walk over the screen, they leave gray behind, &amp;#8220;erasing&amp;#8221; the apps underfoot:&lt;/p&gt;&#xA;&lt;blockquote&gt;&lt;img src=&#34;http://research.swtch.com/crabs1.png&#34;&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p class=lp&gt;&#xA;For the rest of the story, see Luca Cardelli&#39;s &amp;#8220;&lt;a style=&#34;font-weight: bold;&#34; href=&#34;http://lucacardelli.name/Papers/Crabs.pdf&#34;&gt;Crabs: the bitmap terror!&lt;/a&gt;&amp;#8221; (6.7MB). Additional details in &amp;#8220;&lt;a href=&#34;http://lucacardelli.name/Papers/Crabs%20%28History%20and%20Screen%20Dumps%29.pdf&#34;&gt;Crabs (History and Screen Dumps)&lt;/a&gt;&amp;#8221; (57.1MB).&lt;/p&gt;&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;</content>
</entry>
</feed>