F★ for the masses News from the F★ team! This blog covers new language features, general discussions, theoretical roadblocks, engineering challenges, and pretty much the daily life of any language implementor. https://fstarlang.github.io/ Mon, 11 Aug 2025 19:20:28 +0000 Mon, 11 Aug 2025 19:20:28 +0000 Jekyll v3.10.0 F* Summer Schools 2019 <p>Here are a few summer schools that will have F* courses and that are coming up soon:</p> <ul> <li><a href="https://www.cs.uoregon.edu/research/summerschool/summer19/">The Oregon Programming Languages Summer School</a>, Eugene, OR, USA (School: June 17-29, <a href="https://fstar-lang.org/oplss2019">materials</a>)</li> <li><a href="http://resources.mpi-inf.mpg.de/departments/rg1/conferences/vtsa19">Summer School on Verification Technology, Systems, and Applications</a>, Luxembourg (School: 1-5 July, <a href="https://prosecco.gforge.inria.fr/personal/hritcu/teaching/vtsa2019/">materials</a>)</li> <li><a href="https://eci2019.dc.uba.ar/cursos.html">Escuela de Ciencias Informáticas</a>, Buenos Aires, Argentina (School: 22-26 July)</li> <li><a href="https://www.cl.cam.ac.uk/events/metaprog/2019/index.html">Second International Summer School on Metaprogramming</a>, Schloss Dagstuhl, Germany (30 June, School: 11-16 August)</li> </ul> <p>The last two ones include a part about meta-programming in F* (tactics etc).</p> Wed, 17 Apr 2019 00:00:00 +0000 https://fstarlang.github.io/general/2019/04/17/summer-schools.html https://fstarlang.github.io/general/2019/04/17/summer-schools.html general F* v0.9.6.0 released <p>On 18 May 2018 we released <a href="https://github.com/FStarLang/FStar/releases/tag/v0.9.6.0">F* v0.9.6.0</a>.</p> <p>A large number of people contributed to this release: thanks to all!</p> <h3 id="main-new-features">Main new features</h3> <ul> <li> <p>Meta-F*: A metaprogramming and tactic framework, as described in this <a href="https://arxiv.org/abs/1803.06547">report</a>. Code samples are in examples/tactics, examples/native_tactics and the <code class="language-plaintext highlighter-rouge">FStar.Tactics</code> and <code class="language-plaintext highlighter-rouge">FStar.Reflection</code> libraries. Many people contributed a lot to this work, especially Guido Martinez.</p> </li> <li> <p>Improved type inference with two-phase typechecking: We now build verification conditions for a program after a first phase of type inference. This improves inference of implicit arguments and reduces our trust in the type inference. Thanks to Aseem Rastogi!</p> </li> <li> <p>Caching typechecked modules: F* emits “.checked” files, an on-disk representation of a typechecked module that can be read back later. This significantly reduces the time to load a module’s dependencies.</p> </li> </ul> <!--excerpt--> <h3 id="many-other-improvements">Many other improvements</h3> <p>A sampling of improvements across the entire tool chain:</p> <ul> <li> <p>Resolving several syntactic ambiguities in the parser</p> </li> <li> <p>A correct pretty printer for surface terms, using <code class="language-plaintext highlighter-rouge">fstar --indent</code></p> </li> <li> <p>A new dependence analysis to support incremental compilation for larger projects</p> </li> <li> <p>Overhauling the higher order unification algorithm, both in the representation of meta-variables and in the handling of unfolding, leading to significant performance and robustness improvements (see https://github.com/FStarLang/FStar/wiki/Design-note:-Revising-the-unifier)</p> </li> <li> <p>Automatic generation of interfaces for modules and tighter enforcement of abstraction boundaries (see https://github.com/FStarLang/FStar/wiki/Revised-checking-of-a-module’s-interface)</p> </li> <li> <p>Improvements to the SMT encoding, removing axioms that lead to performance problems and reducing brittleness related to optimizations in the encoding, notably shallow vs deep encodings</p> </li> <li> <p>Improved type-based erasure for extraction</p> </li> <li> <p>Several new and improved libraries, including a revised treatment of footprints for Low* programs, in <code class="language-plaintext highlighter-rouge">FStar.Modifies</code></p> </li> <li> <p>And work by many people in Project Everest whose use of F* drove a lot of the work in this release.</p> </li> <li> <p>Plus many other improvements and changes as described in https://github.com/FStarLang/FStar/blob/v0.9.6.0/CHANGES.md</p> </li> <li> <p>And <a href="https://github.com/FStarLang/FStar/issues?q=is%3Aissue+is%3Aclosed+closed%3A%222017-08-23+..+2018-05-17%22">180 closed github issues</a></p> </li> </ul> Tue, 22 May 2018 00:00:00 +0000 https://fstarlang.github.io/general/2018/05/22/fstar-v0.9.6.0-released.html https://fstarlang.github.io/general/2018/05/22/fstar-v0.9.6.0-released.html general Lens-indexed lenses <p>In many functional programming languages, <em>lenses</em> are an increasingly popular, highly composable, way of structuring bidirectional data access, i.e., operations to both read and functionally update parts of a composite object. There are many introductory tutorials about lenses on the web: one that I found to be particularly gentle is by <a href="http://www.haskellforall.com/2012/01/haskell-for-mainstream-programmers_28.html">Gabriel Gonzalez</a>. I’ll borrow some of his ideas to introduce lenses briefly here, although, of course, I’ll work in F* rather than Haskell.</p> <p>Doing it in F* raises a couple of interesting challenges. First, programming with mutable references and destructive updates is common in F*: so, unlike some other lens libraries, ours must support mutation. Second, like everything else in F*, our lens library focuses on verification: we need a way to specify and prove properties about lenses in a way that does not compromise the inherent composability of lenses—composability being their primary appeal. My solution, the <em>lens-indexed lens</em>, is analogous to the <a href="https://www.fstar-lang.org/papers/mumon/paper.pdf">monad-indexed monad</a>, a structure at the core of the design of F* itself.</p> <!--excerpt--> <h2 id="updating-nested-records-clumsy-even-with-primitive-syntax">Updating nested records: Clumsy, even with primitive syntax</h2> <p>Consider the following simple representation of a circle:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// A simple 2d point defined as a pair of integers type point = { x:int; y:int; } // A circle is a point and a radius type circle = { center: point; radius: nat } </code></pre></div></div> <p>Using the record notation to define our types gives us some primitive syntax to access the fields of a <code class="language-plaintext highlighter-rouge">circle</code> and <code class="language-plaintext highlighter-rouge">point</code>. But, despite the primitive support, it’s not very convenient to use. Here’s some code to move the circle in the <code class="language-plaintext highlighter-rouge">x</code>-direction:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let move_x (delta:int) (c:circle) = { c with center = {c.center with x = c.center.x + delta} } </code></pre></div></div> <p>Not pretty. But, by default, that’s basically what it looks like in most ML-like languages, including OCaml, Haskell etc. We’d much prefer to write something like <code class="language-plaintext highlighter-rouge">c.center.x &lt;- c.center.x + delta</code>. Lenses are a clever way of designing a library of abstract “getters” and “setters” that allow you to do just that.</p> <h2 id="pure-lenses-for-composable-bidirectional-access-to-data-structures">Pure lenses for composable, bidirectional access to data structures</h2> <p>A <code class="language-plaintext highlighter-rouge">lens a b</code> is pair of a getter and a setter: given an <code class="language-plaintext highlighter-rouge">a</code>-typed value <code class="language-plaintext highlighter-rouge">v</code>, a <code class="language-plaintext highlighter-rouge">lens a b</code> allows you to</p> <ol> <li> <p><code class="language-plaintext highlighter-rouge">get</code> a <code class="language-plaintext highlighter-rouge">b</code>-typed component out of <code class="language-plaintext highlighter-rouge">v</code></p> </li> <li> <p>create another <code class="language-plaintext highlighter-rouge">a</code>-typed value by updating the same <code class="language-plaintext highlighter-rouge">b</code>-typed component in <code class="language-plaintext highlighter-rouge">v</code></p> </li> </ol> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// A `lens a b` focuses on the `b` component of `a` // It provides a `get` to access the component // And a `put` to update and `a` by updating its `b` component type lens a b = { get: a -&gt; b; put: b -&gt; a -&gt; a } </code></pre></div></div> <p>For instance, given a <code class="language-plaintext highlighter-rouge">point</code>, we can define two lenses, <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code>, to read and write each of its fields, and lenses <code class="language-plaintext highlighter-rouge">center</code> and <code class="language-plaintext highlighter-rouge">radius</code> to focus on each of the fields of a circle.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let x : lens point int = { get = (fun p -&gt; p.x); put = (fun x' p -&gt; {p with x=x'}) } let y : lens point int = { get = (fun p -&gt; p.y); put = (fun y' p -&gt; {p with y=y'}) } let center : lens circle point = { get = (fun c -&gt; c.center); put = (fun p c -&gt; {c with center=p}} } let radius : lens circle int = { get = (fun c -&gt; c.radius); put = (fun r c -&gt; {c with radius=r}} } </code></pre></div></div> <p>This four definitions are rather boring: one could imagine automatically generating them with a bit of meta-programming (maybe we’ll have a future post about that).</p> <p>But, now comes the fun part. Lenses are easily composable: <code class="language-plaintext highlighter-rouge">l |. m</code>: composes lenses, building an “access path” that extends the focus of <code class="language-plaintext highlighter-rouge">l</code> with <code class="language-plaintext highlighter-rouge">m</code>. (Note <code class="language-plaintext highlighter-rouge">#a</code>, <code class="language-plaintext highlighter-rouge">#b</code> and <code class="language-plaintext highlighter-rouge">#c</code> below bind implicit type arguments.)</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let ( |. ) #a #b #c (m:lens a b) (l:lens b c) : lens a c = { get = (fun x -&gt; l.get (m.get x)); put = (fun x y -&gt; m.put (l.put x (m.get y)) y) } </code></pre></div></div> <p>We can now define:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let move_x (delta:int) (c:circle) = (center |. x).put ((center |. x).get c + delta) c </code></pre></div></div> <p>That may not look like much of an improvement, but even with F*’s poor support for custom operators, it’s easy to re-define a couple of common infix operators to make it better.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// `x.(|l|)`: accesses the l-focused component let ( .(||) ) #a #b (x:a) (l:lens a b) : b = l.get x // `x.(|l|) &lt;- v`: updates the l-focused component of x with v let ( .(||)&lt;- ) #a #b (x:a) (l:lens a b) (v:b) : a = l.put v x </code></pre></div></div> <p>This lets us write:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let move_x (delta:int) (c:circle) = c.(| center |. x |) &lt;- c.(| center |. x |) + delta </code></pre></div></div> <p>which is pretty close to what we were aiming for.</p> <h2 id="lenses-on-mutable-data">Lenses on mutable data</h2> <p>Just to be clear, despite appearances, <code class="language-plaintext highlighter-rouge">c.(| center |. x |) &lt;- c.(| center |. x |) + delta</code> does not actually mutate <code class="language-plaintext highlighter-rouge">c</code>. It creates a new circle that differs from <code class="language-plaintext highlighter-rouge">c</code> in the <code class="language-plaintext highlighter-rouge">x</code> field of its <code class="language-plaintext highlighter-rouge">center</code>, leaving the original <code class="language-plaintext highlighter-rouge">c</code> unchanged.</p> <p>That’s nice, but we would also like a way to access and update in place the mutable fields of a record. In a language like OCaml, an object’s fields may contains mutable references to heap-allocated values. In such a setting, it’s easy to define a lens like:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let deref : lens (ref a) a = { get = (fun r -&gt; !r); put = (fun v r -&gt; r := v) } </code></pre></div></div> <p>But, this won’t do in F*: for starters, the <code class="language-plaintext highlighter-rouge">get</code> and <code class="language-plaintext highlighter-rouge">put</code> fields of a <code class="language-plaintext highlighter-rouge">lens</code> are expected to be pure, total functions and the fields of <code class="language-plaintext highlighter-rouge">deref</code> are certainly not pure: they read or write to the heap. F*, like Haskell, forces us to confess to our impurities.</p> <p>If we were in Haskell, we could define the type of a stateful lens (in F* pseudo-syntax) like so:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>type st_lens a b = { st_get : a -&gt; ST b; st_put: b -&gt; a -&gt; ST a } </code></pre></div></div> <p>And we could give <code class="language-plaintext highlighter-rouge">deref</code> the type <code class="language-plaintext highlighter-rouge">st_lens (ref a) a</code>. But, this still won’t do in F*. With verification in mind, stateful computations must</p> <ol> <li> <p>be accompanied by a precise specification, and</p> </li> <li> <p>whatever specification we give a stateful lens, it needs to be stable under composition: the composition of stateful lenses needs to also be a stateful lens (e.g., we should be able to define a double dereference to read an write an <code class="language-plaintext highlighter-rouge">a</code> from/to a <code class="language-plaintext highlighter-rouge">ref (ref a)</code>, by composing lens from <code class="language-plaintext highlighter-rouge">ref (ref a)</code> to a <code class="language-plaintext highlighter-rouge">ref a</code> and again from a <code class="language-plaintext highlighter-rouge">ref a</code> to an <code class="language-plaintext highlighter-rouge">a</code>).</p> </li> </ol> <h2 id="a-precise-specification-for-the-deref-lens">A precise specification for the <code class="language-plaintext highlighter-rouge">deref</code> lens</h2> <p>Let’s look again at the <code class="language-plaintext highlighter-rouge">get</code> and <code class="language-plaintext highlighter-rouge">put</code> of the <code class="language-plaintext highlighter-rouge">deref</code> lens.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let get (r:ref a) = !r let put (v:a) (r:ref a) = r := v </code></pre></div></div> <p>These two functions are the most primitive operations available on references and, of course, we can give them precise specifications in F*.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>val get (r:ref a) : ST a (requires (fun h -&gt; True)) (ensures (fun h0 x h1 -&gt; h0==h1 /\ x == sel h0 r)) val put (v:a) (r:ref a) : ST unit (requires (fun h -&gt; True)) (ensures (fun h0 () h1 -&gt; h1 == upd h0 r v)) </code></pre></div></div> <p>Here <code class="language-plaintext highlighter-rouge">ST a (requires pre) (ensures post)</code> is the type of a stateful computation whose pre-condition on the input heap is <code class="language-plaintext highlighter-rouge">pre</code>, and whose post-condition <code class="language-plaintext highlighter-rouge">post</code> relates the initial heap <code class="language-plaintext highlighter-rouge">h0</code> to the result and the final heap <code class="language-plaintext highlighter-rouge">h1</code>.</p> <h2 id="viewing-the-spec-of-a-stateful-lens-as-a-pure-lens">Viewing the spec of a stateful lens as a pure lens</h2> <p>The specification of <code class="language-plaintext highlighter-rouge">get</code> describes the return value by getting it from the initial heap <code class="language-plaintext highlighter-rouge">h0</code> and the reference <code class="language-plaintext highlighter-rouge">r</code>; the spec of <code class="language-plaintext highlighter-rouge">put</code> describes the final heap <code class="language-plaintext highlighter-rouge">h1</code> by putting a new value into the initial heap <code class="language-plaintext highlighter-rouge">h0</code>. Viewed differently, the specification of <code class="language-plaintext highlighter-rouge">get</code> and <code class="language-plaintext highlighter-rouge">put</code> is itself a lens, a lens between a pair of <code class="language-plaintext highlighter-rouge">heap * ref a</code> and an <code class="language-plaintext highlighter-rouge">a</code>. Let’s call such a lens an <code class="language-plaintext highlighter-rouge">hlens</code>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let hlens a b = lens (heap * a) b let ref_hlens #a : hlens (ref a) a = { get = (fun (h,r) -&gt; sel h r); put = (fun v (h, r) -&gt; upd h r v) } </code></pre></div></div> <p>We can then specify stateful lenses that perform destructive updates based on their underlying <code class="language-plaintext highlighter-rouge">hlenses</code>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// st_lens l: a stateful lens between a and b type st_lens #a #b (l:hlens a b) = { st_get : (x:a -&gt; ST b (requires (fun h -&gt; True)) (ensures (fun h0 x h1 -&gt; h0==h1 /\ y == l.get (h0, x)))); st_put : (y:b -&gt; x:a -&gt; ST a (requires (fun h -&gt; True)) (ensures (fun h0 x' h1 -&gt; h1, x' == l.put y (h0, x))) } </code></pre></div></div> <p>Given an <code class="language-plaintext highlighter-rouge">l:hlens a b</code>, an <code class="language-plaintext highlighter-rouge">st_lens l</code> is a stateful lens between <code class="language-plaintext highlighter-rouge">a</code> and <code class="language-plaintext highlighter-rouge">b</code> that can perform destructive updates and whose behavior is fully specified by <code class="language-plaintext highlighter-rouge">l</code>.</p> <p>And we can finally specify the <code class="language-plaintext highlighter-rouge">deref</code> stateful lens:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let deref : st_lens ref_hlens = { st_get = (fun r -&gt; !r); st_put = (fun v r -&gt; r := v) } </code></pre></div></div> <h2 id="composing-stateful-lenses">Composing stateful lenses</h2> <p>Happily, stateful lenses compose nicely: the composition of an <code class="language-plaintext highlighter-rouge">st_lens l</code> and an <code class="language-plaintext highlighter-rouge">st_lens m</code> is fully specified by <code class="language-plaintext highlighter-rouge">l |. m</code>, the composition of <code class="language-plaintext highlighter-rouge">l</code> and <code class="language-plaintext highlighter-rouge">m</code>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let ( |.. ) #a #b #c (#l:hlens a b) (#m:hlens b c) (sl:st_lens l) (sm:st_lens m) : st_lens (l |. m) = { st_get = (fun (x:a) -&gt; sm.st_get (sl.st_get x)); st_put = (fun (z:c) (x:a) -&gt; sl.st_put (sm.st_put z (sl.st_get x)) x) } </code></pre></div></div> <p>And any pure lens <code class="language-plaintext highlighter-rouge">l</code> is easily lifted to a stateful lens whose specification is the trivial lifting of <code class="language-plaintext highlighter-rouge">l</code> itself to an <code class="language-plaintext highlighter-rouge">hlens</code>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let as_hlens #a #b (l:lens a b) : hlens a b = { get = (fun (h, x) -&gt; x.(|l|)); put = (fun y (h, x) -&gt; h, (x.(|l|) &lt;- y)); } let as_stlens #a #b (l:lens a b) : stlens (as_hlens l) = { st_get = (fun (x:a) -&gt; x.(|l|)); st_put = (fun (y:b) (x:a) -&gt; x.(|l|) &lt;- y) } </code></pre></div></div> <p>These liftings of pure lenses to stateful lenses are convenient to fold in as part of the lens composition, lifting pure lenses as they are composed with stateful lenses, either on the left or the right.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let ( |^. ) #a #b #c (m:lens a b) (#l:hlens b c) (sl:stlens l) = (as_stlens m) |.. sl let ( |.^ ) #a #b #c (#l:hlens a b) (sl:stlens l) (m:lens b c) = sl |.. as_stlens m </code></pre></div></div> <p>We can also define some shorthands, <code class="language-plaintext highlighter-rouge">x.[sl]</code> and <code class="language-plaintext highlighter-rouge">x.[sl] &lt;- v</code>, to apply a stateful lens <code class="language-plaintext highlighter-rouge">sl</code> to access or mutate an object <code class="language-plaintext highlighter-rouge">x</code>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let ( .[] ) #a #b (#l:hlens a b) (x:a) (sl:stlens l) = sl.st_get x let ( .[]&lt;- ) #a #b (#l:hlens a b) (x:a) (sl:stlens l) (y:b) = let _ = sl.st_put y x in () </code></pre></div></div> <h2 id="stateful-lenses-at-work">Stateful lenses at work</h2> <p>We can now revisit our original example of circles and points, this time defining them to support destructive updates.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>type point = { x:ref int; y:ref int; } type circle = { center: ref point; radius: ref nat } </code></pre></div></div> <p>These are just the types we had before, but now with mutable references in each field. Pure lenses to access and mutate each field are easy to define, as before.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let center : lens circle (ref point) = { get = (fun c -&gt; c.center); put = (fun p c -&gt; {c with center = p}) } let x : lens point (ref int) = { get = (fun p -&gt; p.x); put = (fun x p -&gt; {p with x = x}) } ... </code></pre></div></div> <p>Using a combination of pure and stateful lenses, we can mutate the <code class="language-plaintext highlighter-rouge">x</code> field of a circle’s center easily.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let move_x delta c = let l = center |^. deref |.^ x |.. deref in c.[l] &lt;- c.[l] + delta </code></pre></div></div> <p>Notice that since a lens is a first-class value, the access path from a circle to the contents of the <code class="language-plaintext highlighter-rouge">x</code>-field of its center is a value that can be constructed once, bound to a variable (<code class="language-plaintext highlighter-rouge">l</code>) and re-used as needed.</p> <p>Of course, we also want to be able to specify <code class="language-plaintext highlighter-rouge">move_x</code>. But, since every stateful lens is fully specified by a pure lens, specifying <code class="language-plaintext highlighter-rouge">move_x</code> is now pretty easy in terms of its action on the heap. Here’s the type of <code class="language-plaintext highlighter-rouge">move_x</code>:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>val move_x (delta:int) (c:circle) : ST unit (requires (fun _ -&gt; True)) (ensures (fun h0 _ h1 -&gt; let l = center |^. deref |.^ x |.. deref in (h1, c) == (c.(h0, l) &lt;- c.(h0, l) + delta))) </code></pre></div></div> <p>We can specify it using just the same lens that we used in its implementation, except using the <code class="language-plaintext highlighter-rouge">hlens</code> that’s the pure counterpart of the stateful lens <code class="language-plaintext highlighter-rouge">l</code>. Since the type of a stateful lens always mentions its corresponding <code class="language-plaintext highlighter-rouge">hlens</code>, we can easily define functions to to specify a stateful lenses effect.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let ( .() ) #a #b (#l:hlens a b) (x:a) (hs:(heap * stlens l)) = l.get (fst hs, x) let ( .()&lt;- ) #a #b (#l:hlens a b) (x:a) (hs:(heap * stlens l)) (y:b) = l.put y (fst hs, x) </code></pre></div></div> <p>Without these lenses, here’s the mouthful we’d have had to write instead.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let move_x2 (delta:int) (c:circle) : ST unit (requires (fun _ -&gt; True)) (ensures (fun h _ h' -&gt; let rp = c.center in let p = sel h rp in let rx = p.x in let h1 = upd h rx (sel h rx + delta) in let p = sel h1 rp in let h2 = upd h1 rp ({p with x = rx}) in h' == h2)) = c.[center |^. v |.^ x |.. v] &lt;- (c.[center |^. v |.^ x |.. v] + delta) </code></pre></div></div> <p>Worse than the verbosity, it actually took me nearly half an hour to write this specification, requiring peeling back the layers of abstraction to figure out the exact order in which the reads and writes were occurring!</p> <h2 id="takeaways">Takeaways</h2> <p>Lenses are a powerful way to structure data access and mutation. With stateful lens-indexed lenses, we can use them to implement and specify destructive updates of nested data structures in a compact and composable manner.</p> <p>You can see a fully detailed code listing for this blog post <a href="https://github.com/FStarLang/FStar/blob/master/examples/data_structures/StatefulLens.fst">here</a>.</p> <p>Lens libraries in other languages are much more extensive—I’ve merely <del>scratched the surface</del> ground out the most basic lens combinators.</p> Fri, 12 Jan 2018 09:43:00 +0000 https://fstarlang.github.io/general/2018/01/12/lens-indexed-lenses.html https://fstarlang.github.io/general/2018/01/12/lens-indexed-lenses.html general printf* <p>It must have been around 2002 when I first read a paper by Lennart Augustsson about a language called <a href="https://dl.acm.org/citation.cfm?doid=289423.289451">Cayenne</a>. It was the first time I had come across dependent types. Lennart’s first example motivating the need for (and the power of) dependent types is <code class="language-plaintext highlighter-rouge">printf</code>: a function used by legions of programmers that is, in an essential way, very dependently typed. Having worked since then on many programming languages with various forms of dependent types, now, 15 years later, a language that I contribute to has a proper, dependently typed <code class="language-plaintext highlighter-rouge">printf</code>. Well, a <code class="language-plaintext highlighter-rouge">sprintf</code>, but Cayenne’s example was also a <code class="language-plaintext highlighter-rouge">sprintf</code>. This post is about explaining how <code class="language-plaintext highlighter-rouge">sprintf</code> works in F*.</p> <!--excerpt--> <h2 id="what-is-sprintf">What is <code class="language-plaintext highlighter-rouge">sprintf</code>?</h2> <p>Some variant of <code class="language-plaintext highlighter-rouge">sprintf</code> appears in the standard library of many programming languages—it allows for a string to be formatted and assembled from a number of programmer-provided values in a convenient way. A basic usage is like this:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> let format_date (month:string) (day:nat) (year:nat) = sprintf "%s %d, %d" month day year </code></pre></div></div> <p>Where, <code class="language-plaintext highlighter-rouge">format_date "Nov." 22 2017</code> returns the string <code class="language-plaintext highlighter-rouge">"Nov. 22, 2017"</code>.</p> <p>Or:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> let format_date (dow:string) (month:string) (day:nat) (year:nat) = sprintf "%s, %s %d, %d" dow month day year </code></pre></div></div> <p>The curious thing here is that <code class="language-plaintext highlighter-rouge">sprintf</code>’s first argument is a “format string”, and the number and types of its remaining arguments <strong>depends</strong> on the format string: i.e., <code class="language-plaintext highlighter-rouge">sprintf</code> is the quintessential dependently typed function.</p> <h2 id="a-sketch-of-sprintf-in-f">A sketch of <code class="language-plaintext highlighter-rouge">sprintf</code> in F*</h2> <p>Here’s one type for a simplified version of <code class="language-plaintext highlighter-rouge">sprintf</code> in F*.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>val sprintf: s:string{valid_format_string s} -&gt; sprintf_type s </code></pre></div></div> <p>Given a first argument <code class="language-plaintext highlighter-rouge">s</code>, a <code class="language-plaintext highlighter-rouge">valid_format_string</code>, the return value has a type <code class="language-plaintext highlighter-rouge">sprintf_type s</code> that is a function of the format string <code class="language-plaintext highlighter-rouge">s</code>.</p> <p>Defining <code class="language-plaintext highlighter-rouge">valid_format_string s</code> is relatively straightforward, e.g., if one wanted to only allow <code class="language-plaintext highlighter-rouge">%d</code> and <code class="language-plaintext highlighter-rouge">%s</code> as format specifiers (with <code class="language-plaintext highlighter-rouge">%%</code> as an escape sequence for printing “%” itself), one could do it like this:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let rec valid_format_string = function | [] -&gt; true | '%'::'d'::s | '%'::'s'::s | '%'::'%'::s -&gt; valid_format_string s | '%'::_ -&gt; false | _::s -&gt; valid_format_string s </code></pre></div></div> <p><code class="language-plaintext highlighter-rouge">sprintf_type s</code> is a little more unusual: it is a function that computes a type from a format string <code class="language-plaintext highlighter-rouge">s</code>. Here’s one version of it:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>let rec sprintf_type (s:string{valid_format_string s}) = match s with | [] -&gt; string | '%'::'d'::s -&gt; (int -&gt; sprintf_type s) | '%'::'s'::s -&gt; (string -&gt; sprintf_type s) | '%'::'%'::s -&gt; sprintf_type_chars s | _::s -&gt; sprintf_type s </code></pre></div></div> <p>Look at that closely: in the first case, if the format string is empty, <code class="language-plaintext highlighter-rouge">sprintf ""</code> just returns a string. If the format string begins with <code class="language-plaintext highlighter-rouge">"%d"</code>, then sprintf expects one more argument, an <code class="language-plaintext highlighter-rouge">int</code>, followed by whatever arguments are needed by the rest of the format string, and so on.</p> <p>Note: I cut a corner there, treating a <code class="language-plaintext highlighter-rouge">string</code> as a list of characters, whereas in F* you have to explicitly coerce a string to a list of characters. You can see the whole program, corners restored, at the end of this post.</p> <h2 id="that-was-simple-why-did-it-take-so-long-for-f-to-get-sprintf">That was simple! Why did it take so long for F* to get <code class="language-plaintext highlighter-rouge">sprintf</code>?</h2> <p>The version of <code class="language-plaintext highlighter-rouge">sprintf</code> provided in F*’s standard library (<code class="language-plaintext highlighter-rouge">FStar.Printf.sprintf</code>) is somewhat more complex than the simple example we’ve just seen. For one, it supports many more format specifiers than just <code class="language-plaintext highlighter-rouge">%d</code> and <code class="language-plaintext highlighter-rouge">%s</code>. But, that’s just a detail. More essentially, in order for callers of <code class="language-plaintext highlighter-rouge">sprintf</code> to be checked efficiently, the type-checker has to <strong>compute</strong> the recursive function <code class="language-plaintext highlighter-rouge">sprintf_type s</code> at type-checking time.</p> <p>This kind of type-level computation is a distinctive feature of many dependently typed languages, a feature that F* acquired in full generality about 2 years ago. Unlike other dependently typed languages, in addition to type-level computation, the F* type-checker also makes use of algebraic and logical reasoning via an SMT solver. Balancing these features has required quite a lot of care: e.g., compute too much in the type-checker and the SMT solver is faced with reasoning about enormous, partially reduced terms; compute too little and the SMT solver has to do a lot of computation, which is not its strong point.</p> <p>For <code class="language-plaintext highlighter-rouge">sprintf</code>, we don’t want to rely on SMT reasoning at all, since everything can be checked simply using type-level computation. For this, F* requires a hint in the type of <code class="language-plaintext highlighter-rouge">sprintf</code>:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>val sprintf : s:string{normalize (valid_format_string s)} -&gt; normalize (sprintf_type s) </code></pre></div></div> <p>Notice the additional calls to <code class="language-plaintext highlighter-rouge">normalize</code>, which signals to F* to fully reduce <code class="language-plaintext highlighter-rouge">valid_format_string s</code> and <code class="language-plaintext highlighter-rouge">sprintf_type s</code> before resorting to SMT reasoning.</p> <h2 id="additional-material">Additional material</h2> <p>See <a href="https://github.com/FStarLang/FStar/blob/master/ulib/FStar.Printf.fst">the actual library</a> (based on F* code that Catalin Hritcu initially modeled after <a href="http://poleiro.info/posts/2013-04-19-type-safe-printf-in-coq.html">Arthur Azevedo de Amorim’s Coq sprintf</a>) for more details about how this works, including how we compile away uses of <code class="language-plaintext highlighter-rouge">sprintf</code> to a bunch of string concatenations.</p> <p>Here’s a small standalone version. Compare it to the version in Cayenne … it’s pretty similar, but it took (well, at least it took me) 15 years to get there! : )</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>module SimplePrintf open FStar.Char open FStar.String let rec valid_format_chars = function | [] -&gt; true | '%'::'d'::s | '%'::'s'::s | '%'::'%'::s -&gt; valid_format_chars s | '%'::_ -&gt; false | _::s -&gt; valid_format_chars s let valid_format_string s = valid_format_chars (list_of_string s) let rec sprintf_type_chars (s:list char) = match s with | [] -&gt; string | '%'::'d'::s -&gt; (int -&gt; sprintf_type_chars s) | '%'::'s'::s -&gt; (string -&gt; sprintf_type_chars s) | '%'::'%'::s -&gt; sprintf_type_chars s | _::s -&gt; sprintf_type_chars s let sprintf_type s = sprintf_type_chars (list_of_string s) let rec sprintf_chars (s:list char{normalize (valid_format_chars s)}) (res:string) : normalize (sprintf_type_chars s) = match s with | [] -&gt; res | '%'::'d'::s -&gt; fun (x:int) -&gt; sprintf_chars s (res ^ string_of_int x) | '%'::'s'::s -&gt; fun (x:string) -&gt; sprintf_chars s (res ^ x) | '%'::'%'::s -&gt; sprintf_chars s (res ^ "%") | x::s -&gt; sprintf_chars s (res ^ string_of_char x) let sprintf (s:string{normalize (valid_format_string s)}) : normalize (sprintf_type s) = sprintf_chars (list_of_string s) "" </code></pre></div></div> Thu, 23 Nov 2017 08:04:00 +0000 https://fstarlang.github.io/general/2017/11/23/sprintfstar.html https://fstarlang.github.io/general/2017/11/23/sprintfstar.html general F* v0.9.5.0 released <p>On 24 August 2017 we released <a href="https://github.com/FStarLang/FStar/releases/tag/v0.9.5.0">F* v0.9.5.0</a>. This is another big release with lots of changes and new features compared to v0.9.4.0.</p> <h3 id="main-new-features">Main new features</h3> <ul> <li>Proofs by reification (see this <a href="https://arxiv.org/abs/1703.00055">paper</a>)</li> <li>A revision of the libraries based on a new formal account of monotonic state (see this <a href="https://arxiv.org/abs/1707.02466">paper</a>)</li> <li>Extraction of programs with user-defined effects</li> <li>Experimental support for tactics</li> <li><a href="https://github.com/FStarLang/FStar/wiki/Editor-support-for-F*">New IDE protocol</a> and <a href="https://github.com/FStarLang/fstar-mode.el">new IDE features</a>: autocompletion, evaluation, real-time syntax checking, jump-to-definition, type-at-point, etc.</li> </ul> <!--excerpt--> <h3 id="changes-and-other-improvements">Changes and other improvements</h3> <ul> <li>A reorganization of the library and a single <code class="language-plaintext highlighter-rouge">fstarlib.cmxa</code> against which to link all F* programs compiled to OCaml (this change is incompatible with previous versions of F*)</li> <li>A new printer of source terms</li> <li>Revised error reporting from failed SMT queries</li> <li>Improved support for separate compilation via a binary format for checked modules</li> <li>Fixed a ton of bugs (<a href="https://github.com/FStarLang/FStar/issues?utf8=%E2%9C%93&amp;q=is%3Aissue%20is%3Aclosed%20closed%3A%222017-02-02%20..%202017-08-23%22%20">179 closed GitHub issues</a>)</li> </ul> Thu, 24 Aug 2017 00:00:00 +0000 https://fstarlang.github.io/general/2017/08/24/fstar-v0.9.5.0-released.html https://fstarlang.github.io/general/2017/08/24/fstar-v0.9.5.0-released.html general Teaching F* <p>Having to teach F* provides strong motivation to dust off the cobwebs and tidy away long forgotten bread crumbs hidden deep down in remote directories to make the language easier to install and use. It is thus no coincidence that major releases have been aligned with some of us going back to school after a long summer of coding to step out there and present the newest features of the language to a crowd of rowdy students.</p> <p>The latest two occasions:</p> <ul> <li> <p>a <a href="https://www.cs.bris.ac.uk/cryptoschool/">Summer School on Computer Aided Analysis of Cryptographic Protocols</a> in Bucharest, Romania, 13-14 September 2016 in which we had two days to teach <a href="http://prosecco.gforge.inria.fr/personal/hritcu/teaching/bucharest-school-2016/">Verifying Cryptographic Protocol Implementations with F*</a>.</p> <p>Who would have thought that F* can be used to introduce students of varying backgrounds to functional programming. This lead to the <a href="http://prosecco.gforge.inria.fr/personal/hritcu/teaching/mpri-jan2017/slides/out/01/slides01.html#/sec-a-gentle-introduction-to-f-">Gentle introduction for F*</a>.</p> <p>This was the first time we used the <a href="https://github.com/FStarLang/FStar/releases/tag/V0.9.4.0">universes version of F*</a> for teaching and we thus had to updated the <a href="https://fstar-lang.org/tutorial/">F* tutorial</a>. We also played with docker builds to provide students with a preconfigured interactive mode, but most students stuck to the tutorial.</p> </li> <li> <p>a MPRI course on <a href="https://wikimpri.dptinfo.ens-cachan.fr/doku.php?id=cours:c-2-30">Cryptographic protocols: formal and computational proofs</a> in which we could spend more time teaching <a href="http://prosecco.gforge.inria.fr/personal/hritcu/teaching/mpri-jan2017/">Program Verification with F*</a>.</p> <p>We for the first time taught low-Level stateful programming with hyperStack. Warning, this is so new that it’s not yet covered by the tutorial.</p> </li> </ul> <p>It is no coincidence that both courses had a decidedly cryptographic focus given that F* is the language of choice of the <a href="https://project-everest.github.io/">Everest project</a>.</p> <p>What features would you like to see included in future research schools? And what do you think are the biggest stumbling blocks when learning and teaching a hot-off-the-press research language like F*?</p> Thu, 09 Feb 2017 21:40:00 +0000 https://fstarlang.github.io/general/2017/02/09/teaching-fstar.html https://fstarlang.github.io/general/2017/02/09/teaching-fstar.html general F* v0.9.4.0 released <p>The F* team is pleased to announce the release of <a href="https://github.com/FStarLang/FStar/releases/tag/V0.9.4.0">F* v0.9.4.0</a>. This is the culmination of exactly one year of hard work from a very quickly expanding F* team. We’re not very good at keeping precise change lists, but here are the main highlights of this release:</p> <ul> <li>Predicative hierarchy of universes with universe polymorphism</li> <li>Uniform syntax between expressions and types allowing rich type-level computation</li> <li><a href="https://www.fstar-lang.org/papers/dm4free/">Dijkstra Monads for Free</a></li> <li><a href="https://github.com/FStarLang/kremlin">Extraction to C via KreMLin</a></li> <li>New parser based on Menhir</li> <li>New pretty printer for surface syntax and <code class="language-plaintext highlighter-rouge">fstar --indent</code></li> <li>Changed default effect to Tot</li> <li>Strict positivity check for inductives</li> <li>New synatax for inductive type projectors and discriminators</li> <li>Better semantics for module open and support for local opens</li> <li>Better dependency analysis</li> <li>Better error locations for Z3 failures</li> <li>Replaced Z3 timeouts with machine independent resource limits</li> <li>Cleaned up libraries and examples (a bit)</li> <li>Improvements to interactive mode</li> <li>Docker builds</li> <li>Fixed a ton of bugs (<a href="https://github.com/FStarLang/FStar/issues?page=3&amp;q=is%3Aissue+is%3Aclosed+closed%3A%222016-02-02+..+2017-02-02%22&amp;utf8=%E2%9C%93">262 closed GitHub issues</a>)</li> </ul> <p>Enjoy the best F* release ever!</p> Thu, 09 Feb 2017 11:13:00 +0000 https://fstarlang.github.io/general/2017/02/09/fstar-v0.9.4.0-released.html https://fstarlang.github.io/general/2017/02/09/fstar-v0.9.4.0-released.html general Introducing KreMlin <p>The work we do these days on F* is often in service of <a href="https://project-everest.github.io/">Project Everest</a>. The goal of Everest is to verify <em>and</em> deploy a drop-in replacement for the HTTPS stack, the protocol using which you are probably reading this page, securely (hopefully). So far, we’ve been focusing most of our efforts in Everest on <a href="https://tlswg.github.io/tls13-spec/">TLS</a>, the protocol at the heart of HTTPS.</p> <p>Right now, I’m stuck in the Eurostar back from our week-long meeting in Cambridge, UK, so it feels like a good time to write down some thoughts about KreMLin, a new compiler backend that we’re using in Everest, that several of us have been working on over the summer, at MSR and INRIA.</p> <p>As a reminder, Everest sets out to verify <em>and</em> deploy secure cryptographic protocols, starting with TLS 1.3. <strong>Deploy</strong> is the salient part: in order to see people adopt our code, we not only need to write and prove our TLS library, but also to</p> <ul> <li>make sure it delivers a level of performance acceptable for browser vendors, and</li> <li>package it in a form that’s palatable for a hardcode Windows developer that started writing C before I was born.</li> </ul> <p>A TLS library can, roughly speaking, be broken down into two parts: the protocol layer that performs the handshake (“libssl”) and the cryptographic layer that actually encrypts the data to be transmitted (“libcrypto”). The handshake connects to the server, says hi, agrees on which algorithms to use, and agrees on some cryptographic parameters. Once parameters have been setup, the cryptographic layer is responsible for encrypting the stream of data.</p> <!--excerpt--> <p>Experience shows that the performance of a TLS library most often boils down to the performance of the underlying cryptography. The handshake is network-bound, but when transmitting a big file, encryption needs to be fast. This means that for Everest, we need super efficient cryptography. Fortunately, many smart people have spent a lot of time and energy writing super-neat C implementations that squeeze the last bit of performance out of your compiler. However, we wish to write and verify our programs in F*, not C.</p> <p>This is where KreMLin comes in. The workflow is as follows: one takes a neatly optimized cryptographic routine, then translates it into F* syntax (“shallow embedding”); using KreMLin, one extracts it back to C, but gets a verified version that pretty much looks like the original. For instance, here’s a bit of F* that implements the main entry point of Chacha20.</p> <div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">let</span> <span class="k">rec</span> <span class="n">counter_mode</span> <span class="n">key</span> <span class="n">iv</span> <span class="n">counter</span> <span class="n">len</span> <span class="n">plaintext</span> <span class="n">ciphertext</span> <span class="o">=</span> <span class="k">if</span> <span class="n">len</span> <span class="o">=^</span> <span class="mi">0</span><span class="n">ul</span> <span class="k">then</span> <span class="bp">()</span> <span class="k">else</span> <span class="k">if</span> <span class="n">len</span> <span class="o">&lt;^</span> <span class="n">blocklen</span> <span class="k">then</span> <span class="c">(* encrypt final partial block *)</span> <span class="k">begin</span> <span class="k">let</span> <span class="n">cipher</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">ciphertext</span> <span class="mi">0</span><span class="n">ul</span> <span class="n">len</span> <span class="k">in</span> <span class="k">let</span> <span class="n">plain</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">plaintext</span> <span class="mi">0</span><span class="n">ul</span> <span class="n">len</span> <span class="k">in</span> <span class="n">prf</span> <span class="n">cipher</span> <span class="n">key</span> <span class="n">iv</span> <span class="n">counter</span> <span class="n">len</span><span class="p">;</span> <span class="n">xor_bytes_inplace</span> <span class="n">cipher</span> <span class="n">plain</span> <span class="n">len</span> <span class="k">end</span> <span class="k">else</span> <span class="c">(* encrypt full block *)</span> <span class="k">begin</span> <span class="k">let</span> <span class="n">cipher</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">ciphertext</span> <span class="mi">0</span><span class="n">ul</span> <span class="n">blocklen</span> <span class="k">in</span> <span class="k">let</span> <span class="n">plain</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">plaintext</span> <span class="mi">0</span><span class="n">ul</span> <span class="n">blocklen</span> <span class="k">in</span> <span class="n">prf</span> <span class="n">cipher</span> <span class="n">key</span> <span class="n">iv</span> <span class="n">counter</span> <span class="n">blocklen</span><span class="p">;</span> <span class="n">xor_bytes_inplace</span> <span class="n">cipher</span> <span class="n">plain</span> <span class="n">blocklen</span><span class="p">;</span> <span class="k">let</span> <span class="n">len</span> <span class="o">=</span> <span class="n">len</span> <span class="o">-^</span> <span class="n">blocklen</span> <span class="k">in</span> <span class="k">let</span> <span class="n">ciphertext</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">ciphertext</span> <span class="n">blocklen</span> <span class="n">len</span> <span class="k">in</span> <span class="k">let</span> <span class="n">plaintext</span> <span class="o">=</span> <span class="n">sub</span> <span class="n">plaintext</span> <span class="n">blocklen</span> <span class="n">len</span> <span class="k">in</span> <span class="n">counter_mode</span> <span class="n">key</span> <span class="n">iv</span> <span class="p">(</span><span class="n">counter</span> <span class="o">+^</span> <span class="mi">1</span><span class="n">ul</span><span class="p">)</span> <span class="n">len</span> <span class="n">plaintext</span> <span class="n">ciphertext</span> <span class="k">end</span> </code></pre></div></div> <p>One goes great lengths to prove the following properties of this piece of F* code.</p> <ul> <li><strong>Memory safety.</strong> We model stack allocation in F* using a new <code class="language-plaintext highlighter-rouge">Stack</code> effect; one may only allocate local mutable variables, or buffers on the stack. Every buffer operation needs to prove that the buffer is still live, and that the index is within bounds. For instance, in the code above, the calls to <code class="language-plaintext highlighter-rouge">sub</code> take a pointer <em>into</em> one of these buffers, and verification happens behind the scenes.</li> <li><strong>Functional correctness.</strong> We have written in this style a bignum library, some elliptic curve operations, stream ciphers and mac algorithms, as well as an AEAD construction. For the math part, for instance, the optimized curve operations are shown to implement the correct mathematical operations.</li> <li><strong>Cryptographic properties.</strong> By using a technique called “idealization”, one can prove two versions of the same code: one that relies on cryptographic assumptions, such as “this function can be replaced by a function that returns random bytes”; and one that actually uses real cryptography instead of <code class="language-plaintext highlighter-rouge">Random.bytes()</code>. The code branches on an <code class="language-plaintext highlighter-rouge">ideal</code> boolean; for cryptographic proof purposes, we consider the <code class="language-plaintext highlighter-rouge">ideal</code> case; for extraction purposes, we only consider the other, “real” case.</li> </ul> <p>F* already performs erasure and extraction for its OCaml backend; the tool I wrote, <strong>KreMLin</strong>, takes it from there and performs further rewritings and transformations so that the code ends up in a limited, first-order, monomorphic subset of F* called Low*. If code falls within the Low* subset, then KreMLin knows how to translate it to C. Here’s what comes out of the tool after extraction:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void Crypto_Symmetric_Chacha20_counter_mode( uint8_t *key, uint8_t *iv, uint32_t counter, uint32_t len, uint8_t *plaintext, uint8_t *ciphertext ) { if (len == UINT32_C(0)) { } else if (len &lt; Crypto_Symmetric_Chacha20_blocklen) { uint8_t *cipher = ciphertext + UINT32_C(0); uint8_t *plain = plaintext + UINT32_C(0); Crypto_Symmetric_Chacha20_prf(cipher, key, iv, counter, len); Buffer_Utils_xor_bytes_inplace(cipher, plain, len); } else { uint8_t *cipher = ciphertext + UINT32_C(0); uint8_t *plain = plaintext + UINT32_C(0); Crypto_Symmetric_Chacha20_prf(cipher, key, iv, counter, Crypto_Symmetric_Chacha20_blocklen); Buffer_Utils_xor_bytes_inplace(cipher, plain, Crypto_Symmetric_Chacha20_blocklen); uint32_t len0 = len - Crypto_Symmetric_Chacha20_blocklen; uint8_t *ciphertext0 = ciphertext + Crypto_Symmetric_Chacha20_blocklen; uint8_t *plaintext0 = plaintext + Crypto_Symmetric_Chacha20_blocklen; Crypto_Symmetric_Chacha20_counter_mode(key, iv, counter + UINT32_C(1), len0, plaintext0, ciphertext0); } } </code></pre></div></div> <p>One can see that the tool goes great length to generate beautiful C: names and control-flow are preserved, and everything is pretty-printed. This is to tackle the second concern I mentioned initially: there is no hope of getting a browser vendor to integrate code written in F*, which would be considered in that setting “not a real language”. In constract, by offering an extracted C version of our library, we have reasonable hope that reviewers can skim the code, convince themselves that it’s legit, and take us a little bit more seriously.</p> <p>We worked out a simulation between Low* and a simplified version of C we dubbed “C*”; this grounds our translation in some theoretical basis. The simulation covers trace preservation: if the program does not have side channels in the first place, then the translation from Low* to C* does not introduce any new side channels. The tool is unverified; we plan to extend the formalism to cover more of the transformations performed by the tool; in the long run, I would like to write KreMLin in F* and certify it, but that seems non-trivial.</p> <p>We have adopted that style for an entire body of cryptographic code (&gt; 10,000 lines of F*, including whitespace and comments), and obtain competitive performance. Right now, the tool and formalism deal with one specific flavor of code that performs stack-based allocation only. However, we are extending it right now to deal with other patterns of allocation, such as allocation on the heap.</p> <p>KreMLin is currently written in OCaml; I re-used a fair number of tricks from my earlier Mezzo project to make writing transformation passes on the internal AST easier. These including monomorphization, inlining, hoisting and rewritings to go from an expression language to a statement language, and some tricks to go from the ML scoping rules to the C ones.</p> <p>KreMLin is <a href="https://github.com/FStarLang/kremlin/">open-source</a>; we have an <a href="https://jonathan.protzenko.fr/papers/ml16.pdf">ML’16 abstract</a> if you’re curious, as well as some <a href="https://jonathan.protzenko.fr/papers/talk-ml16.pdf">slides</a>.</p> Fri, 30 Sep 2016 18:27:00 +0000 https://fstarlang.github.io/general/2016/09/30/introducing-kremlin.html https://fstarlang.github.io/general/2016/09/30/introducing-kremlin.html general Welcome to F*! <p>After many discussions, and in the spirit of the <a href="http://gallium.inria.fr/blog/">Gallium Blog</a> (where I was a regular), the F* team is happy to announce the F*-blog! Expect a variety of posts, ranging from technical digressions about Dijkstra Monads to engineering discussions about parsing technology, and pretty much anything in between.</p> <p>One of our stated goals is to make F* more accessible to beginners; this means making the setup easier, but also writing more documentation, so that people who are <em>not</em> in the vicinity of the F* team can write programs, too. We’ve started an effort on the <a href="https://github.com/FStarLang/FStar/wiki">wiki</a>; the goal of this blog is to complement the wiki and make it easy for F* enthusiasts to keep up with the development; be notified about breaking changes on the <code class="language-plaintext highlighter-rouge">master</code> branch, and more generally make the development process more open.</p> <p>I expect that this blog will also cover related projects, such as <a href="https://github.com/FStarLang/kremlin">KreMLin</a>, our F*-to-C translator, and <a href="https://github.com/mitls/mitls-fstar/">miTLS</a>, our ongoing implementation of TLS 1.3 in F*. Stay tuned! ☭</p> Fri, 26 Aug 2016 20:36:05 +0000 https://fstarlang.github.io/general/2016/08/26/welcome.html https://fstarlang.github.io/general/2016/08/26/welcome.html general