messages
listlengths 2
2
|
|---|
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n head: FStar.Tactics.NamedView.term ->\n arg_terms: Prims.list FStar.Tactics.NamedView.term ->\n hoisted_args: Prims.list FStar.Stubs.Reflection.V2.Data.argv\n -> FStar.Tactics.Effect.Tac FStar.Tactics.NamedView.term\nEffect: FStar.Tactics.Effect.Tac\n\nContext:\n(*\n Copyright 2008-2018 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n/// ==========================\n/// Pattern-matching tactics\n/// ==========================\n///\n/// :Author: Clément Pit-Claudel\n/// :Contact: clement.pitclaudel@live.com\n/// :Date: 2017-10-13\n\nmodule FStar.Tactics.PatternMatching\n\nopen FStar.Tactics.V2\n\n/// Contents\n/// ========\n///\n/// 1 Contents\n/// 2 Motivation\n/// 3 Some utility functions\n/// 4 Pattern types\n/// 5 Pattern matching exceptions\n/// 5.1 Types of exceptions\n/// 5.2 The exception monad\n/// 5.3 Liftings\n/// 6 Pattern interpretation\n/// 7 Pattern-matching problems\n/// 7.1 Definitions\n/// 7.2 Resolution\n/// 8 A DSL for pattern-matching\n/// 8.1 Pattern notations\n/// 8.2 Problem notations\n/// 8.3 Continuations\n/// 9 Putting it all together\n/// 10 Examples\n/// 10.1 Simple examples\n/// 10.2 A real-life example\n/// 11 Possible extensions\n/// 12 Notes\n///\n/// Motivation\n/// ==========\n///\n/// Suppose you have a goal of the form ``squash (a == b)``. How do you capture\n/// `a` and `b` for further inspection?\n///\n/// Here's a basic (but cumbersome!) implementation:\n\nlet fetch_eq_side () : Tac (term * term) =\n let g = cur_goal () in\n match inspect g with\n | Tv_App squash (g, _) ->\n (match inspect squash with\n | Tv_UInst squash _\n | Tv_FVar squash ->\n if fv_to_string squash = flatten_name squash_qn then\n (match inspect g with\n | Tv_App eq_type_x (y, _) ->\n (match inspect eq_type_x with\n | Tv_App eq_type (x, _) ->\n (match inspect eq_type with\n | Tv_App eq (typ, _) ->\n (match inspect eq with\n | Tv_UInst eq _\n | Tv_FVar eq ->\n if fv_to_string eq = flatten_name eq2_qn then\n (x, y)\n else fail \"not an equality\"\n | _ -> fail \"not an app2 of fvar: \")\n | _ -> fail \"not an app3\")\n | _ -> fail \"not an app2\")\n | _ -> fail \"not an app under squash\")\n else fail \"not a squash\"\n | _ -> fail \"not an app of fvar at top level\")\n | _ -> fail \"not an app at top level\"\n\n/// …and here's how you could use it:\n\n(* let _ = *)\n(* assert_by_tactic (1 + 1 == 2) *)\n(* (fun () -> let l, r = fetch_eq_side () in *)\n(* print (term_to_string l ^ \" / \" ^ term_to_string r)) *)\n\n/// This file defines pattern-matching primitives that let you write the same\n/// thing like this…\n///\n/// .. code:: fstar\n///\n/// let fetch_eq_side' #a () : Tac (term * term) =\n/// gpm (fun (left right: a) (g: pm_goal (squash (left == right))) ->\n/// (quote left, quote right) <: Tac (term * term))\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (fun () -> let l, r = fetch_eq_side' #int () in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r))\n///\n/// …or, more succinctly, like this:\n///\n/// .. code:: fstar\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (gpm (fun (left right: int) (g: pm_goal (squash (left == right))) ->\n/// let l, r = quote left, quote right in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r) <: Tac unit))\n\n\n/// Some utility functions\n/// ======================\n///\n/// (Skip over this part on a quick read — these are just convenience functions)\n\n\n(** Ensure that tactic `t` fails. **)\nlet mustfail #a (t: unit -> Tac a) (message: string) : Tac unit =\n match trytac t with\n | Some _ -> fail message\n | None -> ()\n\n/// The following two tactics are needed because of issues with the ``Tac``\n/// effect.\n\nlet implies_intro' () : Tac unit =\n let _ = implies_intro () in ()\n\nlet repeat' #a (f: unit -> Tac a) : Tac unit =\n let _ = repeat f in ()\n\nlet and_elim' (h: binding) : Tac unit =\n and_elim (pack (Tv_Var h));\n clear h\n\n(** Use a hypothesis at type a to satisfy a goal at type squash a *)\nlet exact_hyp (a: Type0) (h: namedv) : Tac unit =\n let hd = quote (FStar.Squash.return_squash #a) in\n exact (mk_app hd [((pack (Tv_Var h)), Q_Explicit)])\n\n(** Use a hypothesis h (of type a) to satisfy a goal at type a *)\nlet exact_hyp' (h: namedv): Tac unit =\n exact (pack (Tv_Var h))\n\n/// Pattern types\n/// =============\n///\n/// Patterns are defined using a simple inductive type, mirroring the structure\n/// of ``term_view``.\n\ntype varname = string\n\ntype qn = string\n\ntype pattern =\n| PVar: name: varname -> pattern\n| PQn: qn: qn -> pattern\n| PType: pattern\n| PApp: hd: pattern -> arg: pattern -> pattern\n\nlet desc_of_pattern = function\n| PVar _ -> \"a variable\"\n| PQn qn -> \"a constant (\" ^ qn ^ \")\"\n| PType -> \"Type\"\n| PApp _ _ -> \"a function application\"\n\nlet rec string_of_pattern = function\n| PVar x -> \"?\" ^ x\n| PQn qn -> qn\n| PType -> \"Type\"\n| PApp l r -> \"(\" ^ string_of_pattern l ^ \" \"\n ^ string_of_pattern r ^ \")\"\n\n/// Pattern matching exceptions\n/// ===========================\n///\n/// Pattern-matching is defined as a pure, monadic function (because of issues\n/// with combining DM4F effects, but also because it helps with debugging).\n/// This section defines the exception monad.\n///\n/// Types of exceptions\n/// -------------------\n\nnoeq type match_exception =\n| NameMismatch of qn * qn\n| SimpleMismatch of pattern * term\n| NonLinearMismatch of varname * term * term\n| UnsupportedTermInPattern of term\n| IncorrectTypeInAbsPatBinder of typ\n\nlet term_head t : Tac string =\n match inspect t with\n | Tv_Var bv -> \"Tv_Var\"\n | Tv_BVar fv -> \"Tv_BVar\"\n | Tv_FVar fv -> \"Tv_FVar\"\n | Tv_UInst _ _ -> \"Tv_UInst\"\n | Tv_App f x -> \"Tv_App\"\n | Tv_Abs x t -> \"Tv_Abs\"\n | Tv_Arrow x t -> \"Tv_Arrow\"\n | Tv_Type _ -> \"Tv_Type\"\n | Tv_Refine x t -> \"Tv_Refine\"\n | Tv_Const cst -> \"Tv_Const\"\n | Tv_Uvar i t -> \"Tv_Uvar\"\n | Tv_Let r attrs b t1 t2 -> \"Tv_Let\"\n | Tv_Match t _ branches -> \"Tv_Match\"\n | Tv_AscribedT _ _ _ _ -> \"Tv_AscribedT\"\n | Tv_AscribedC _ _ _ _ -> \"Tv_AscribedC\"\n | Tv_Unknown -> \"Tv_Unknown\"\n | Tv_Unsupp -> \"Tv_Unsupp\"\n\nlet string_of_match_exception = function\n | NameMismatch (qn1, qn2) ->\n \"Match failure (name mismatch): expecting \" ^\n qn1 ^ \", found \" ^ qn2\n | SimpleMismatch (pat, tm) ->\n \"Match failure (sort mismatch): expecting \" ^\n desc_of_pattern pat ^ \", got \" ^ term_to_string tm\n | NonLinearMismatch (nm, t1, t2) ->\n \"Match failure (nonlinear mismatch): variable \" ^ nm ^\n \" needs to match both \" ^ (term_to_string t1) ^\n \" and \" ^ (term_to_string t2)\n | UnsupportedTermInPattern tm ->\n \"Match failure (unsupported term in pattern): \" ^\n term_to_string tm ^ \" (\" ^ term_head tm ^ \")\"\n | IncorrectTypeInAbsPatBinder typ ->\n \"Incorrect type in pattern-matching binder: \" ^\n term_to_string typ ^ \" (use one of ``var``, ``hyp …``, or ``goal …``)\"\n\n/// The exception monad\n/// -------------------\n\nnoeq type match_res a =\n| Success of a\n| Failure of match_exception\n\nlet return #a (x: a) : match_res a =\n Success x\n\nlet (let?) (#a #b: Type)\n (f: match_res a)\n (g: a -> Tac (match_res b))\n : Tac (match_res b) =\n match f with\n | Success aa -> g aa\n | Failure ex -> Failure ex\n\nlet raise #a (ex: match_exception) : match_res a =\n Failure ex\n\n/// Liftings\n/// --------\n///\n/// There's a natural lifting from the exception monad into the tactic effect:\n\nlet lift_exn_tac #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\nlet lift_exn_tactic #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern interpretation\n/// ======================\n///\n/// This section implement pattern-matching. This is strictly a one term, one\n/// pattern implementation — handling cases in which mutliple hypotheses match\n/// the same pattern is done later.\n\ntype bindings = list (varname * term)\nlet string_of_bindings (bindings: bindings) =\n String.concat \"\\n\"\n (map (fun (nm, tm) -> (\">> \" ^ nm ^ \": \" ^ term_to_string tm))\n bindings)\n\n(** Match a pattern against a term.\n`cur_bindings` is a list of bindings collected while matching previous parts of\nthe pattern. Returns a result in the exception monad. **)\nlet rec interp_pattern_aux (pat: pattern) (cur_bindings: bindings) (tm:term)\n : Tac (match_res bindings) =\n let interp_var (v: varname) cur_bindings tm =\n match List.Tot.Base.assoc v cur_bindings with\n | Some tm' -> if term_eq tm tm' then return cur_bindings\n else raise (NonLinearMismatch (v, tm, tm'))\n | None -> return ((v, tm) :: cur_bindings) in\n let interp_qn (qn: qn) cur_bindings tm =\n match inspect tm with\n | Tv_UInst fv _\n | Tv_FVar fv ->\n if fv_to_string fv = qn then return cur_bindings\n else raise (NameMismatch (qn, (fv_to_string fv)))\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_type cur_bindings tm =\n match inspect tm with\n | Tv_Type _ -> return cur_bindings\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_app (p_hd p_arg: (p:pattern{p << pat})) cur_bindings tm =\n match inspect tm with\n | Tv_App hd (arg, _) ->\n let? with_hd = interp_pattern_aux p_hd cur_bindings hd in\n let? with_arg = interp_pattern_aux p_arg with_hd arg in\n return with_arg\n | _ -> raise (SimpleMismatch (pat, tm)) in\n match pat with\n | PVar var -> interp_var var cur_bindings tm\n | PQn qn -> interp_qn qn cur_bindings tm\n | PType -> interp_type cur_bindings tm\n | PApp p_hd p_arg -> interp_app p_hd p_arg cur_bindings tm\n\n(** Match a pattern `pat` against a term.\nReturns a result in the exception monad. **)\nlet interp_pattern (pat: pattern) : term -> Tac (match_res bindings) =\n fun (tm: term) ->\n let? rev_bindings = interp_pattern_aux pat [] tm in\n return (List.Tot.Base.rev rev_bindings)\n\n(** Match a term `tm` against a pattern `pat`.\nRaises an exception if the match fails. This is mostly useful for debugging:\nuse ``mgw`` to capture matches. **)\nlet match_term pat (tm : term) : Tac bindings =\n match interp_pattern pat (norm_term [] tm) with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern-matching problems\n/// =========================\n///\n/// Generalizing past single-term single-pattern problems, we obtain the\n/// following notions of pattern-matching problems and solutions:\n\nlet debug msg : Tac unit = () // print msg\n\n/// Definitions\n/// -----------\n\nlet absvar = binding\ntype hypothesis = binding\n\n/// A matching problem is composed of holes (``mp_vars``), hypothesis patterns\n/// (``mp_hyps``), and a goal pattern (``mp_goal``).\n\nnoeq type matching_problem =\n { mp_vars: list varname;\n mp_hyps: list (varname * pattern);\n mp_goal: option pattern }\n\nlet string_of_matching_problem mp =\n let vars =\n String.concat \", \" mp.mp_vars in\n let hyps =\n String.concat \"\\n \"\n (List.Tot.Base.map (fun (nm, pat) ->\n nm ^ \": \" ^ (string_of_pattern pat)) mp.mp_hyps) in\n let goal = match mp.mp_goal with\n | None -> \"_\"\n | Some pat -> string_of_pattern pat in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \"\\n\" ^\n \" goal: \" ^ goal ^ \" }\"\n\n/// A solution is composed of terms captured to mach the holes, and binders\n/// captured to match hypothesis patterns.\n\nnoeq type matching_solution =\n { ms_vars: list (varname * term);\n ms_hyps: list (varname * hypothesis) }\n\nlet string_of_matching_solution ms =\n let vars =\n String.concat \"\\n \"\n (map (fun (varname, tm) ->\n varname ^ \": \" ^ (term_to_string tm)) ms.ms_vars) in\n let hyps =\n String.concat \"\\n \"\n (map (fun (nm, binding) ->\n nm ^ \": \" ^ (binding_to_string binding)) ms.ms_hyps) in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \" }\"\n\n(** Find a varname in an association list; fail if it can't be found. **)\nlet assoc_varname_fail (#b: Type) (key: varname) (ls: list (varname * b))\n : Tac b =\n match List.Tot.Base.assoc key ls with\n | None -> fail (\"Not found: \" ^ key)\n | Some x -> x\n\nlet ms_locate_hyp (a: Type) (solution: matching_solution)\n (name: varname) : Tac hypothesis =\n assoc_varname_fail name solution.ms_hyps\n\nlet ms_locate_var (a: Type) (solution: matching_solution)\n (name: varname) : Tac a =\n unquote #a (assoc_varname_fail name solution.ms_vars)\n\nlet ms_locate_unit (a: Type) _solution _binder_name : Tac unit =\n ()\n\n/// Resolution\n/// ----------\n///\n/// Solving a matching problem is a two-steps process: find an initial\n/// assignment for holes based on the goal pattern, then find a set of\n/// hypotheses matching hypothesis patterns.\n///\n/// Note that the implementation takes a continuation of type\n/// ``matching_solution -> Tac a``. This continuation is needed because we want\n/// users to be able to provide extra criteria on matching solutions (most\n/// commonly, this criterion is that a particular tactic should run\n/// successfuly).\n///\n/// This makes it easy to implement a simple for of search through the context,\n/// where one can find a hypothesis matching a particular predicate by\n/// constructing a trivial matching problem and passing the predicate as the\n/// continuation.\n\n(** Scan ``hypotheses`` for a match for ``pat`` that lets ``body`` succeed.\n\n``name`` is used to refer to the hypothesis matched in the final solution.\n``part_sol`` includes bindings gathered while matching previous solutions. **)\nlet rec solve_mp_for_single_hyp #a\n (name: varname)\n (pat: pattern)\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (part_sol: matching_solution)\n : Tac a =\n match hypotheses with\n | [] ->\n fail #a \"No matching hypothesis\"\n | h :: hs ->\n or_else // Must be in ``Tac`` here to run `body`\n (fun () ->\n match interp_pattern_aux pat part_sol.ms_vars (type_of_binding h) with\n | Failure ex ->\n fail (\"Failed to match hyp: \" ^ (string_of_match_exception ex))\n | Success bindings ->\n let ms_hyps = (name, h) :: part_sol.ms_hyps in\n body ({ part_sol with ms_vars = bindings; ms_hyps = ms_hyps }))\n (fun () ->\n solve_mp_for_single_hyp name pat hs body part_sol)\n\n(** Scan ``hypotheses`` for matches for ``mp_hyps`` that lets ``body``\nsucceed. **)\nlet rec solve_mp_for_hyps #a\n (mp_hyps: list (varname * pattern))\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (partial_solution: matching_solution)\n : Tac a =\n match mp_hyps with\n | [] -> body partial_solution\n | (name, pat) :: pats ->\n solve_mp_for_single_hyp name pat hypotheses\n (solve_mp_for_hyps pats hypotheses body)\n partial_solution\n\n(** Solve a matching problem.\n\nThe solution returned is constructed to ensure that the continuation ``body``\nsucceeds: this implements the usual backtracking-match semantics. **)\nlet solve_mp #a (problem: matching_problem)\n (hypotheses: list hypothesis) (goal: term)\n (body: matching_solution -> Tac a)\n : Tac a =\n let goal_ps =\n match problem.mp_goal with\n | None -> { ms_vars = []; ms_hyps = [] }\n | Some pat ->\n match interp_pattern pat goal with\n | Failure ex -> fail (\"Failed to match goal: \" ^ (string_of_match_exception ex))\n | Success bindings -> { ms_vars = bindings; ms_hyps = [] } in\n solve_mp_for_hyps #a problem.mp_hyps hypotheses body goal_ps\n\n/// A DSL for pattern-matching\n/// ==========================\n///\n/// Using pattern-matching problems as defined above is relatively cumbersome,\n/// so we now introduce a lightweight notation, in two steps: pattern notations,\n/// and matching-problem notations.\n///\n/// Pattern notations\n/// -----------------\n///\n/// The first part of our pattern-matching syntax is pattern notations: we\n/// provide a reflective function which constructs a pattern from a term:\n/// variables are holes, free variables are constants, and applications are\n/// application patterns.\n\n(* FIXME: MOVE *)\nlet name_of_namedv (x:namedv) : Tac string =\n unseal (inspect_namedv x).ppname\n\n(** Compile a term `tm` into a pattern. **)\nlet rec pattern_of_term_ex tm : Tac (match_res pattern) =\n match inspect tm with\n | Tv_Var bv ->\n return (PVar (name_of_namedv bv))\n | Tv_FVar fv\n | Tv_UInst fv _ ->\n let qn = fv_to_string fv in\n return (PQn qn)\n | Tv_Type _ ->\n return PType\n | Tv_App f (x, _) ->\n let? fpat = pattern_of_term_ex f in\n let? xpat = pattern_of_term_ex x in\n return (PApp fpat xpat)\n | _ -> raise (UnsupportedTermInPattern tm)\n\n(** β-reduce a term `tm`.\nThis is useful to remove needles function applications introduced by F*, like\n``(fun a b c -> a) 1 2 3``. **)\nlet beta_reduce (tm: term) : Tac term =\n norm_term [] tm\n\n(** Compile a term `tm` into a pattern. **)\nlet pattern_of_term tm : Tac pattern =\n match pattern_of_term_ex tm with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Problem notations\n/// -----------------\n///\n/// We then introduce a DSL for matching problems, best explained on the\n/// following example::\n///\n/// (fun (a b c: ①) (h1 h2 h3: hyp ②) (g: pm_goal ③) → ④)\n///\n/// This notation is intended to express a pattern-matching problems with three\n/// holes ``a``, ``b``, and ``c`` of type ①, matching hypotheses ``h1``, ``h2``,\n/// and ``h3`` against pattern ② and the goal against the pattern ③. The body\n/// of the notation (④) is then run with appropriate terms bound to ``a``,\n/// ``b``, and ``c``, appropriate binders bound to ``h1``, ``h2``, and ``h3``,\n/// and ``()`` bound to ``g``.\n///\n/// We call these patterns ``abspat``s (abstraction patterns), and we provide\n/// facilities to parse them into matching problems, and to run their bodies\n/// against a particular matching solution.\n\n// We used to annotate variables with an explicit 'var' marker, but then that\n// var annotation leaked into the types of other hypotheses due to type\n// inference, requiring non-trivial normalization.\n\n// let var (a: Type) = a\nlet hyp (a: Type) = binding\nlet pm_goal (a: Type) = unit\n\nlet hyp_qn = `%hyp\nlet goal_qn = `%pm_goal\n\nnoeq type abspat_binder_kind =\n| ABKVar of typ\n| ABKHyp\n| ABKGoal\n\nlet string_of_abspat_binder_kind = function\n | ABKVar _ -> \"varname\"\n | ABKHyp -> \"hyp\"\n | ABKGoal -> \"goal\"\n\nnoeq type abspat_argspec =\n { asa_name: absvar;\n asa_kind: abspat_binder_kind }\n\n// We must store this continuation, because recomputing it yields different\n// names when the binders are re-opened.\ntype abspat_continuation =\n list abspat_argspec * term\n\nlet type_of_named_binder (nb : binder) : term =\n nb.sort\n\nlet classify_abspat_binder (b : binder): Tac (abspat_binder_kind * term) =\n let varname = \"v\" in\n let hyp_pat = PApp (PQn hyp_qn) (PVar varname) in\n let goal_pat = PApp (PQn goal_qn) (PVar varname) in\n\n let typ = type_of_named_binder b in\n match interp_pattern hyp_pat typ with\n | Success [(_, hyp_typ)] -> ABKHyp, hyp_typ\n | Success _ -> fail \"classifiy_abspat_binder: impossible (1)\"\n | Failure _ ->\n match interp_pattern goal_pat typ with\n | Success [(_, goal_typ)] -> ABKGoal, goal_typ\n | Success _ -> fail \"classifiy_abspat_binder: impossible (2)\"\n | Failure _ -> ABKVar typ, typ\n\n(** Split an abstraction `tm` into a list of binders and a body. **)\nlet rec binders_and_body_of_abs tm : Tac (list binder * term) =\n match inspect tm with\n | Tv_Abs binder tm ->\n let binders, body = binders_and_body_of_abs tm in\n binder :: binders, body\n | _ -> [], tm\n\nlet cleanup_abspat (t: term) : Tac term =\n norm_term [] t\n\n\nlet name_of_named_binder (nb : binder) : Tac string =\n unseal nb.ppname\n\n(** Parse a notation into a matching problem and a continuation.\n\nPattern-matching notations are of the form ``(fun binders… -> continuation)``,\nwhere ``binders`` are of one of the forms ``var …``, ``hyp …``, or ``goal …``.\n``var`` binders are typed holes to be used in other binders; ``hyp`` binders\nindicate a pattern to be matched against hypotheses; and ``goal`` binders match\nthe goal.\n\n\nA reduction phase is run to ensure that the pattern looks reasonable; it is\nneeded because F* tends to infer arguments in β-expanded form.\n\nThe continuation returned can't directly be applied to a pattern-matching\nsolution; see ``interp_abspat_continuation`` below for that. **)\nlet matching_problem_of_abs (tm: term)\n : Tac (matching_problem * abspat_continuation) =\n\n let binders, body = binders_and_body_of_abs (cleanup_abspat tm) in\n debug (\"Got binders: \" ^ (String.concat \", \"\n (map (fun b -> name_of_named_binder b <: Tac string) binders)));\n\n let classified_binders : list (binder & string & abspat_binder_kind & typ) =\n map (fun binder ->\n let bv_name = name_of_named_binder binder in\n debug (\"Got binder: \" ^ bv_name ^ \"; type is \" ^\n term_to_string (type_of_named_binder binder));\n let binder_kind, typ = classify_abspat_binder binder in\n (binder, bv_name, binder_kind, typ))\n binders in\n\n let problem =\n fold_left\n (fun problem (binder, bv_name, binder_kind, typ) ->\n debug (\"Compiling binder \" ^ name_of_named_binder binder ^\n \", classified as \" ^ string_of_abspat_binder_kind binder_kind ^\n \", with type \" ^ term_to_string typ);\n match binder_kind with\n | ABKVar _ -> { problem with mp_vars = bv_name :: problem.mp_vars }\n | ABKHyp -> { problem with mp_hyps = (bv_name, (pattern_of_term typ))\n :: problem.mp_hyps }\n | ABKGoal -> { problem with mp_goal = Some (pattern_of_term typ) })\n ({ mp_vars = []; mp_hyps = []; mp_goal = None })\n classified_binders in\n\n let continuation =\n let abspat_argspec_of_binder xx : Tac abspat_argspec =\n match xx with | (binder, xx, binder_kind, yy) ->\n { asa_name = binder_to_binding binder; asa_kind = binder_kind } in\n (map abspat_argspec_of_binder classified_binders, tm) in\n\n let mp =\n { mp_vars = List.Tot.Base.rev #varname problem.mp_vars;\n mp_hyps = List.Tot.Base.rev #(varname * pattern) problem.mp_hyps;\n mp_goal = problem.mp_goal } in\n\n debug (\"Got matching problem: \" ^ (string_of_matching_problem mp));\n mp, continuation\n\n/// Continuations\n/// -------------\n///\n/// Parsing an abspat yields a matching problem and a continuation of type\n/// ``abspat_continuation``, which is essentially just a list of binders and a\n/// term (the body of the abstraction pattern).\n\n(** Get the (quoted) type expected by a specific kind of abspat binder. **)\nlet arg_type_of_binder_kind binder_kind : Tac term =\n match binder_kind with\n | ABKVar typ -> typ\n | ABKHyp -> `binder\n | ABKGoal -> `unit\n\n(** Retrieve the function used to locate a value for a given abspat binder. **)\nlet locate_fn_of_binder_kind binder_kind =\n match binder_kind with\n | ABKVar _ -> `ms_locate_var\n | ABKHyp -> `ms_locate_hyp\n | ABKGoal -> `ms_locate_unit\n\n(** Construct a term fetching the value of an abspat argument from a quoted\nmatching solution ``solution_term``. **)\nlet abspat_arg_of_abspat_argspec solution_term (argspec: abspat_argspec)\n : Tac term =\n let loc_fn = locate_fn_of_binder_kind argspec.asa_kind in\n let name_tm = pack (Tv_Const (C_String (unseal argspec.asa_name.ppname))) in\n let locate_args = [(arg_type_of_binder_kind argspec.asa_kind, Q_Explicit);\n (solution_term, Q_Explicit); (name_tm, Q_Explicit)] in\n mk_app loc_fn locate_args\n\n(** Specialize a continuation of type ``abspat_continuation``.\nThis constructs a fully applied version of `continuation`, but it requires a\nquoted solution to be passed in. **)\n\nPartial implementation:\nlet rec hoist_and_apply (head: term) (arg_terms: list term) (hoisted_args: list argv) : Tac term =\n \n"
},
{
"role": "assistant",
"content": "match arg_terms with\n| [] -> mk_app head (List.rev hoisted_args)\n| arg_term :: rest ->\n let n = List.Tot.length hoisted_args in\n let nb:binder =\n {\n ppname = seal (\"x\" ^ string_of_int n);\n sort = pack Tv_Unknown;\n uniq = fresh ();\n qual = Q_Explicit;\n attrs = []\n }\n in\n pack (Tv_Let false\n []\n nb\n arg_term\n (hoist_and_apply head\n rest\n ((pack (Tv_Var (binder_to_namedv nb)), Q_Explicit) :: hoisted_args)))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> idx: Prims.nat{idx < Prims.pow2 n}\n -> Prims.GTot MerkleTree.Spec.padded_hash\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nPartial implementation:\nlet mt_get #_ #_ mt idx =\n \n"
},
{
"role": "assistant",
"content": "S.index mt idx"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n name: FStar.Tactics.PatternMatching.varname ->\n pat: FStar.Tactics.PatternMatching.pattern ->\n hypotheses: Prims.list FStar.Tactics.PatternMatching.hypothesis ->\n body: (_: FStar.Tactics.PatternMatching.matching_solution -> FStar.Tactics.Effect.Tac a) ->\n part_sol: FStar.Tactics.PatternMatching.matching_solution\n -> FStar.Tactics.Effect.Tac a\nEffect: FStar.Tactics.Effect.Tac\n\nContext:\n(*\n Copyright 2008-2018 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n/// ==========================\n/// Pattern-matching tactics\n/// ==========================\n///\n/// :Author: Clément Pit-Claudel\n/// :Contact: clement.pitclaudel@live.com\n/// :Date: 2017-10-13\n\nmodule FStar.Tactics.PatternMatching\n\nopen FStar.Tactics.V2\n\n/// Contents\n/// ========\n///\n/// 1 Contents\n/// 2 Motivation\n/// 3 Some utility functions\n/// 4 Pattern types\n/// 5 Pattern matching exceptions\n/// 5.1 Types of exceptions\n/// 5.2 The exception monad\n/// 5.3 Liftings\n/// 6 Pattern interpretation\n/// 7 Pattern-matching problems\n/// 7.1 Definitions\n/// 7.2 Resolution\n/// 8 A DSL for pattern-matching\n/// 8.1 Pattern notations\n/// 8.2 Problem notations\n/// 8.3 Continuations\n/// 9 Putting it all together\n/// 10 Examples\n/// 10.1 Simple examples\n/// 10.2 A real-life example\n/// 11 Possible extensions\n/// 12 Notes\n///\n/// Motivation\n/// ==========\n///\n/// Suppose you have a goal of the form ``squash (a == b)``. How do you capture\n/// `a` and `b` for further inspection?\n///\n/// Here's a basic (but cumbersome!) implementation:\n\nlet fetch_eq_side () : Tac (term * term) =\n let g = cur_goal () in\n match inspect g with\n | Tv_App squash (g, _) ->\n (match inspect squash with\n | Tv_UInst squash _\n | Tv_FVar squash ->\n if fv_to_string squash = flatten_name squash_qn then\n (match inspect g with\n | Tv_App eq_type_x (y, _) ->\n (match inspect eq_type_x with\n | Tv_App eq_type (x, _) ->\n (match inspect eq_type with\n | Tv_App eq (typ, _) ->\n (match inspect eq with\n | Tv_UInst eq _\n | Tv_FVar eq ->\n if fv_to_string eq = flatten_name eq2_qn then\n (x, y)\n else fail \"not an equality\"\n | _ -> fail \"not an app2 of fvar: \")\n | _ -> fail \"not an app3\")\n | _ -> fail \"not an app2\")\n | _ -> fail \"not an app under squash\")\n else fail \"not a squash\"\n | _ -> fail \"not an app of fvar at top level\")\n | _ -> fail \"not an app at top level\"\n\n/// …and here's how you could use it:\n\n(* let _ = *)\n(* assert_by_tactic (1 + 1 == 2) *)\n(* (fun () -> let l, r = fetch_eq_side () in *)\n(* print (term_to_string l ^ \" / \" ^ term_to_string r)) *)\n\n/// This file defines pattern-matching primitives that let you write the same\n/// thing like this…\n///\n/// .. code:: fstar\n///\n/// let fetch_eq_side' #a () : Tac (term * term) =\n/// gpm (fun (left right: a) (g: pm_goal (squash (left == right))) ->\n/// (quote left, quote right) <: Tac (term * term))\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (fun () -> let l, r = fetch_eq_side' #int () in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r))\n///\n/// …or, more succinctly, like this:\n///\n/// .. code:: fstar\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (gpm (fun (left right: int) (g: pm_goal (squash (left == right))) ->\n/// let l, r = quote left, quote right in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r) <: Tac unit))\n\n\n/// Some utility functions\n/// ======================\n///\n/// (Skip over this part on a quick read — these are just convenience functions)\n\n\n(** Ensure that tactic `t` fails. **)\nlet mustfail #a (t: unit -> Tac a) (message: string) : Tac unit =\n match trytac t with\n | Some _ -> fail message\n | None -> ()\n\n/// The following two tactics are needed because of issues with the ``Tac``\n/// effect.\n\nlet implies_intro' () : Tac unit =\n let _ = implies_intro () in ()\n\nlet repeat' #a (f: unit -> Tac a) : Tac unit =\n let _ = repeat f in ()\n\nlet and_elim' (h: binding) : Tac unit =\n and_elim (pack (Tv_Var h));\n clear h\n\n(** Use a hypothesis at type a to satisfy a goal at type squash a *)\nlet exact_hyp (a: Type0) (h: namedv) : Tac unit =\n let hd = quote (FStar.Squash.return_squash #a) in\n exact (mk_app hd [((pack (Tv_Var h)), Q_Explicit)])\n\n(** Use a hypothesis h (of type a) to satisfy a goal at type a *)\nlet exact_hyp' (h: namedv): Tac unit =\n exact (pack (Tv_Var h))\n\n/// Pattern types\n/// =============\n///\n/// Patterns are defined using a simple inductive type, mirroring the structure\n/// of ``term_view``.\n\ntype varname = string\n\ntype qn = string\n\ntype pattern =\n| PVar: name: varname -> pattern\n| PQn: qn: qn -> pattern\n| PType: pattern\n| PApp: hd: pattern -> arg: pattern -> pattern\n\nlet desc_of_pattern = function\n| PVar _ -> \"a variable\"\n| PQn qn -> \"a constant (\" ^ qn ^ \")\"\n| PType -> \"Type\"\n| PApp _ _ -> \"a function application\"\n\nlet rec string_of_pattern = function\n| PVar x -> \"?\" ^ x\n| PQn qn -> qn\n| PType -> \"Type\"\n| PApp l r -> \"(\" ^ string_of_pattern l ^ \" \"\n ^ string_of_pattern r ^ \")\"\n\n/// Pattern matching exceptions\n/// ===========================\n///\n/// Pattern-matching is defined as a pure, monadic function (because of issues\n/// with combining DM4F effects, but also because it helps with debugging).\n/// This section defines the exception monad.\n///\n/// Types of exceptions\n/// -------------------\n\nnoeq type match_exception =\n| NameMismatch of qn * qn\n| SimpleMismatch of pattern * term\n| NonLinearMismatch of varname * term * term\n| UnsupportedTermInPattern of term\n| IncorrectTypeInAbsPatBinder of typ\n\nlet term_head t : Tac string =\n match inspect t with\n | Tv_Var bv -> \"Tv_Var\"\n | Tv_BVar fv -> \"Tv_BVar\"\n | Tv_FVar fv -> \"Tv_FVar\"\n | Tv_UInst _ _ -> \"Tv_UInst\"\n | Tv_App f x -> \"Tv_App\"\n | Tv_Abs x t -> \"Tv_Abs\"\n | Tv_Arrow x t -> \"Tv_Arrow\"\n | Tv_Type _ -> \"Tv_Type\"\n | Tv_Refine x t -> \"Tv_Refine\"\n | Tv_Const cst -> \"Tv_Const\"\n | Tv_Uvar i t -> \"Tv_Uvar\"\n | Tv_Let r attrs b t1 t2 -> \"Tv_Let\"\n | Tv_Match t _ branches -> \"Tv_Match\"\n | Tv_AscribedT _ _ _ _ -> \"Tv_AscribedT\"\n | Tv_AscribedC _ _ _ _ -> \"Tv_AscribedC\"\n | Tv_Unknown -> \"Tv_Unknown\"\n | Tv_Unsupp -> \"Tv_Unsupp\"\n\nlet string_of_match_exception = function\n | NameMismatch (qn1, qn2) ->\n \"Match failure (name mismatch): expecting \" ^\n qn1 ^ \", found \" ^ qn2\n | SimpleMismatch (pat, tm) ->\n \"Match failure (sort mismatch): expecting \" ^\n desc_of_pattern pat ^ \", got \" ^ term_to_string tm\n | NonLinearMismatch (nm, t1, t2) ->\n \"Match failure (nonlinear mismatch): variable \" ^ nm ^\n \" needs to match both \" ^ (term_to_string t1) ^\n \" and \" ^ (term_to_string t2)\n | UnsupportedTermInPattern tm ->\n \"Match failure (unsupported term in pattern): \" ^\n term_to_string tm ^ \" (\" ^ term_head tm ^ \")\"\n | IncorrectTypeInAbsPatBinder typ ->\n \"Incorrect type in pattern-matching binder: \" ^\n term_to_string typ ^ \" (use one of ``var``, ``hyp …``, or ``goal …``)\"\n\n/// The exception monad\n/// -------------------\n\nnoeq type match_res a =\n| Success of a\n| Failure of match_exception\n\nlet return #a (x: a) : match_res a =\n Success x\n\nlet (let?) (#a #b: Type)\n (f: match_res a)\n (g: a -> Tac (match_res b))\n : Tac (match_res b) =\n match f with\n | Success aa -> g aa\n | Failure ex -> Failure ex\n\nlet raise #a (ex: match_exception) : match_res a =\n Failure ex\n\n/// Liftings\n/// --------\n///\n/// There's a natural lifting from the exception monad into the tactic effect:\n\nlet lift_exn_tac #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\nlet lift_exn_tactic #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern interpretation\n/// ======================\n///\n/// This section implement pattern-matching. This is strictly a one term, one\n/// pattern implementation — handling cases in which mutliple hypotheses match\n/// the same pattern is done later.\n\ntype bindings = list (varname * term)\nlet string_of_bindings (bindings: bindings) =\n String.concat \"\\n\"\n (map (fun (nm, tm) -> (\">> \" ^ nm ^ \": \" ^ term_to_string tm))\n bindings)\n\n(** Match a pattern against a term.\n`cur_bindings` is a list of bindings collected while matching previous parts of\nthe pattern. Returns a result in the exception monad. **)\nlet rec interp_pattern_aux (pat: pattern) (cur_bindings: bindings) (tm:term)\n : Tac (match_res bindings) =\n let interp_var (v: varname) cur_bindings tm =\n match List.Tot.Base.assoc v cur_bindings with\n | Some tm' -> if term_eq tm tm' then return cur_bindings\n else raise (NonLinearMismatch (v, tm, tm'))\n | None -> return ((v, tm) :: cur_bindings) in\n let interp_qn (qn: qn) cur_bindings tm =\n match inspect tm with\n | Tv_UInst fv _\n | Tv_FVar fv ->\n if fv_to_string fv = qn then return cur_bindings\n else raise (NameMismatch (qn, (fv_to_string fv)))\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_type cur_bindings tm =\n match inspect tm with\n | Tv_Type _ -> return cur_bindings\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_app (p_hd p_arg: (p:pattern{p << pat})) cur_bindings tm =\n match inspect tm with\n | Tv_App hd (arg, _) ->\n let? with_hd = interp_pattern_aux p_hd cur_bindings hd in\n let? with_arg = interp_pattern_aux p_arg with_hd arg in\n return with_arg\n | _ -> raise (SimpleMismatch (pat, tm)) in\n match pat with\n | PVar var -> interp_var var cur_bindings tm\n | PQn qn -> interp_qn qn cur_bindings tm\n | PType -> interp_type cur_bindings tm\n | PApp p_hd p_arg -> interp_app p_hd p_arg cur_bindings tm\n\n(** Match a pattern `pat` against a term.\nReturns a result in the exception monad. **)\nlet interp_pattern (pat: pattern) : term -> Tac (match_res bindings) =\n fun (tm: term) ->\n let? rev_bindings = interp_pattern_aux pat [] tm in\n return (List.Tot.Base.rev rev_bindings)\n\n(** Match a term `tm` against a pattern `pat`.\nRaises an exception if the match fails. This is mostly useful for debugging:\nuse ``mgw`` to capture matches. **)\nlet match_term pat (tm : term) : Tac bindings =\n match interp_pattern pat (norm_term [] tm) with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern-matching problems\n/// =========================\n///\n/// Generalizing past single-term single-pattern problems, we obtain the\n/// following notions of pattern-matching problems and solutions:\n\nlet debug msg : Tac unit = () // print msg\n\n/// Definitions\n/// -----------\n\nlet absvar = binding\ntype hypothesis = binding\n\n/// A matching problem is composed of holes (``mp_vars``), hypothesis patterns\n/// (``mp_hyps``), and a goal pattern (``mp_goal``).\n\nnoeq type matching_problem =\n { mp_vars: list varname;\n mp_hyps: list (varname * pattern);\n mp_goal: option pattern }\n\nlet string_of_matching_problem mp =\n let vars =\n String.concat \", \" mp.mp_vars in\n let hyps =\n String.concat \"\\n \"\n (List.Tot.Base.map (fun (nm, pat) ->\n nm ^ \": \" ^ (string_of_pattern pat)) mp.mp_hyps) in\n let goal = match mp.mp_goal with\n | None -> \"_\"\n | Some pat -> string_of_pattern pat in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \"\\n\" ^\n \" goal: \" ^ goal ^ \" }\"\n\n/// A solution is composed of terms captured to mach the holes, and binders\n/// captured to match hypothesis patterns.\n\nnoeq type matching_solution =\n { ms_vars: list (varname * term);\n ms_hyps: list (varname * hypothesis) }\n\nlet string_of_matching_solution ms =\n let vars =\n String.concat \"\\n \"\n (map (fun (varname, tm) ->\n varname ^ \": \" ^ (term_to_string tm)) ms.ms_vars) in\n let hyps =\n String.concat \"\\n \"\n (map (fun (nm, binding) ->\n nm ^ \": \" ^ (binding_to_string binding)) ms.ms_hyps) in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \" }\"\n\n(** Find a varname in an association list; fail if it can't be found. **)\nlet assoc_varname_fail (#b: Type) (key: varname) (ls: list (varname * b))\n : Tac b =\n match List.Tot.Base.assoc key ls with\n | None -> fail (\"Not found: \" ^ key)\n | Some x -> x\n\nlet ms_locate_hyp (a: Type) (solution: matching_solution)\n (name: varname) : Tac hypothesis =\n assoc_varname_fail name solution.ms_hyps\n\nlet ms_locate_var (a: Type) (solution: matching_solution)\n (name: varname) : Tac a =\n unquote #a (assoc_varname_fail name solution.ms_vars)\n\nlet ms_locate_unit (a: Type) _solution _binder_name : Tac unit =\n ()\n\n/// Resolution\n/// ----------\n///\n/// Solving a matching problem is a two-steps process: find an initial\n/// assignment for holes based on the goal pattern, then find a set of\n/// hypotheses matching hypothesis patterns.\n///\n/// Note that the implementation takes a continuation of type\n/// ``matching_solution -> Tac a``. This continuation is needed because we want\n/// users to be able to provide extra criteria on matching solutions (most\n/// commonly, this criterion is that a particular tactic should run\n/// successfuly).\n///\n/// This makes it easy to implement a simple for of search through the context,\n/// where one can find a hypothesis matching a particular predicate by\n/// constructing a trivial matching problem and passing the predicate as the\n/// continuation.\n\n(** Scan ``hypotheses`` for a match for ``pat`` that lets ``body`` succeed.\n\n``name`` is used to refer to the hypothesis matched in the final solution.\n\nPartial implementation:\nlet rec solve_mp_for_single_hyp\n #a\n (name: varname)\n (pat: pattern)\n (hypotheses: list hypothesis)\n (body: (matching_solution -> Tac a))\n (part_sol: matching_solution)\n : Tac a =\n \n"
},
{
"role": "assistant",
"content": "match hypotheses with\n| [] -> fail #a \"No matching hypothesis\"\n| h :: hs ->\n or_else (fun () ->\n match interp_pattern_aux pat part_sol.ms_vars (type_of_binding h) with\n | Failure ex -> fail (\"Failed to match hyp: \" ^ (string_of_match_exception ex))\n | Success bindings ->\n let ms_hyps = (name, h) :: part_sol.ms_hyps in\n body ({ part_sol with ms_vars = bindings; ms_hyps = ms_hyps }))\n (fun () -> solve_mp_for_single_hyp name pat hs body part_sol)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: b: FStar.Tactics.NamedView.binder\n -> FStar.Tactics.Effect.Tac\n (FStar.Tactics.PatternMatching.abspat_binder_kind * FStar.Tactics.NamedView.term)\nEffect: FStar.Tactics.Effect.Tac\n\nContext:\n(*\n Copyright 2008-2018 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n/// ==========================\n/// Pattern-matching tactics\n/// ==========================\n///\n/// :Author: Clément Pit-Claudel\n/// :Contact: clement.pitclaudel@live.com\n/// :Date: 2017-10-13\n\nmodule FStar.Tactics.PatternMatching\n\nopen FStar.Tactics.V2\n\n/// Contents\n/// ========\n///\n/// 1 Contents\n/// 2 Motivation\n/// 3 Some utility functions\n/// 4 Pattern types\n/// 5 Pattern matching exceptions\n/// 5.1 Types of exceptions\n/// 5.2 The exception monad\n/// 5.3 Liftings\n/// 6 Pattern interpretation\n/// 7 Pattern-matching problems\n/// 7.1 Definitions\n/// 7.2 Resolution\n/// 8 A DSL for pattern-matching\n/// 8.1 Pattern notations\n/// 8.2 Problem notations\n/// 8.3 Continuations\n/// 9 Putting it all together\n/// 10 Examples\n/// 10.1 Simple examples\n/// 10.2 A real-life example\n/// 11 Possible extensions\n/// 12 Notes\n///\n/// Motivation\n/// ==========\n///\n/// Suppose you have a goal of the form ``squash (a == b)``. How do you capture\n/// `a` and `b` for further inspection?\n///\n/// Here's a basic (but cumbersome!) implementation:\n\nlet fetch_eq_side () : Tac (term * term) =\n let g = cur_goal () in\n match inspect g with\n | Tv_App squash (g, _) ->\n (match inspect squash with\n | Tv_UInst squash _\n | Tv_FVar squash ->\n if fv_to_string squash = flatten_name squash_qn then\n (match inspect g with\n | Tv_App eq_type_x (y, _) ->\n (match inspect eq_type_x with\n | Tv_App eq_type (x, _) ->\n (match inspect eq_type with\n | Tv_App eq (typ, _) ->\n (match inspect eq with\n | Tv_UInst eq _\n | Tv_FVar eq ->\n if fv_to_string eq = flatten_name eq2_qn then\n (x, y)\n else fail \"not an equality\"\n | _ -> fail \"not an app2 of fvar: \")\n | _ -> fail \"not an app3\")\n | _ -> fail \"not an app2\")\n | _ -> fail \"not an app under squash\")\n else fail \"not a squash\"\n | _ -> fail \"not an app of fvar at top level\")\n | _ -> fail \"not an app at top level\"\n\n/// …and here's how you could use it:\n\n(* let _ = *)\n(* assert_by_tactic (1 + 1 == 2) *)\n(* (fun () -> let l, r = fetch_eq_side () in *)\n(* print (term_to_string l ^ \" / \" ^ term_to_string r)) *)\n\n/// This file defines pattern-matching primitives that let you write the same\n/// thing like this…\n///\n/// .. code:: fstar\n///\n/// let fetch_eq_side' #a () : Tac (term * term) =\n/// gpm (fun (left right: a) (g: pm_goal (squash (left == right))) ->\n/// (quote left, quote right) <: Tac (term * term))\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (fun () -> let l, r = fetch_eq_side' #int () in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r))\n///\n/// …or, more succinctly, like this:\n///\n/// .. code:: fstar\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (gpm (fun (left right: int) (g: pm_goal (squash (left == right))) ->\n/// let l, r = quote left, quote right in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r) <: Tac unit))\n\n\n/// Some utility functions\n/// ======================\n///\n/// (Skip over this part on a quick read — these are just convenience functions)\n\n\n(** Ensure that tactic `t` fails. **)\nlet mustfail #a (t: unit -> Tac a) (message: string) : Tac unit =\n match trytac t with\n | Some _ -> fail message\n | None -> ()\n\n/// The following two tactics are needed because of issues with the ``Tac``\n/// effect.\n\nlet implies_intro' () : Tac unit =\n let _ = implies_intro () in ()\n\nlet repeat' #a (f: unit -> Tac a) : Tac unit =\n let _ = repeat f in ()\n\nlet and_elim' (h: binding) : Tac unit =\n and_elim (pack (Tv_Var h));\n clear h\n\n(** Use a hypothesis at type a to satisfy a goal at type squash a *)\nlet exact_hyp (a: Type0) (h: namedv) : Tac unit =\n let hd = quote (FStar.Squash.return_squash #a) in\n exact (mk_app hd [((pack (Tv_Var h)), Q_Explicit)])\n\n(** Use a hypothesis h (of type a) to satisfy a goal at type a *)\nlet exact_hyp' (h: namedv): Tac unit =\n exact (pack (Tv_Var h))\n\n/// Pattern types\n/// =============\n///\n/// Patterns are defined using a simple inductive type, mirroring the structure\n/// of ``term_view``.\n\ntype varname = string\n\ntype qn = string\n\ntype pattern =\n| PVar: name: varname -> pattern\n| PQn: qn: qn -> pattern\n| PType: pattern\n| PApp: hd: pattern -> arg: pattern -> pattern\n\nlet desc_of_pattern = function\n| PVar _ -> \"a variable\"\n| PQn qn -> \"a constant (\" ^ qn ^ \")\"\n| PType -> \"Type\"\n| PApp _ _ -> \"a function application\"\n\nlet rec string_of_pattern = function\n| PVar x -> \"?\" ^ x\n| PQn qn -> qn\n| PType -> \"Type\"\n| PApp l r -> \"(\" ^ string_of_pattern l ^ \" \"\n ^ string_of_pattern r ^ \")\"\n\n/// Pattern matching exceptions\n/// ===========================\n///\n/// Pattern-matching is defined as a pure, monadic function (because of issues\n/// with combining DM4F effects, but also because it helps with debugging).\n/// This section defines the exception monad.\n///\n/// Types of exceptions\n/// -------------------\n\nnoeq type match_exception =\n| NameMismatch of qn * qn\n| SimpleMismatch of pattern * term\n| NonLinearMismatch of varname * term * term\n| UnsupportedTermInPattern of term\n| IncorrectTypeInAbsPatBinder of typ\n\nlet term_head t : Tac string =\n match inspect t with\n | Tv_Var bv -> \"Tv_Var\"\n | Tv_BVar fv -> \"Tv_BVar\"\n | Tv_FVar fv -> \"Tv_FVar\"\n | Tv_UInst _ _ -> \"Tv_UInst\"\n | Tv_App f x -> \"Tv_App\"\n | Tv_Abs x t -> \"Tv_Abs\"\n | Tv_Arrow x t -> \"Tv_Arrow\"\n | Tv_Type _ -> \"Tv_Type\"\n | Tv_Refine x t -> \"Tv_Refine\"\n | Tv_Const cst -> \"Tv_Const\"\n | Tv_Uvar i t -> \"Tv_Uvar\"\n | Tv_Let r attrs b t1 t2 -> \"Tv_Let\"\n | Tv_Match t _ branches -> \"Tv_Match\"\n | Tv_AscribedT _ _ _ _ -> \"Tv_AscribedT\"\n | Tv_AscribedC _ _ _ _ -> \"Tv_AscribedC\"\n | Tv_Unknown -> \"Tv_Unknown\"\n | Tv_Unsupp -> \"Tv_Unsupp\"\n\nlet string_of_match_exception = function\n | NameMismatch (qn1, qn2) ->\n \"Match failure (name mismatch): expecting \" ^\n qn1 ^ \", found \" ^ qn2\n | SimpleMismatch (pat, tm) ->\n \"Match failure (sort mismatch): expecting \" ^\n desc_of_pattern pat ^ \", got \" ^ term_to_string tm\n | NonLinearMismatch (nm, t1, t2) ->\n \"Match failure (nonlinear mismatch): variable \" ^ nm ^\n \" needs to match both \" ^ (term_to_string t1) ^\n \" and \" ^ (term_to_string t2)\n | UnsupportedTermInPattern tm ->\n \"Match failure (unsupported term in pattern): \" ^\n term_to_string tm ^ \" (\" ^ term_head tm ^ \")\"\n | IncorrectTypeInAbsPatBinder typ ->\n \"Incorrect type in pattern-matching binder: \" ^\n term_to_string typ ^ \" (use one of ``var``, ``hyp …``, or ``goal …``)\"\n\n/// The exception monad\n/// -------------------\n\nnoeq type match_res a =\n| Success of a\n| Failure of match_exception\n\nlet return #a (x: a) : match_res a =\n Success x\n\nlet (let?) (#a #b: Type)\n (f: match_res a)\n (g: a -> Tac (match_res b))\n : Tac (match_res b) =\n match f with\n | Success aa -> g aa\n | Failure ex -> Failure ex\n\nlet raise #a (ex: match_exception) : match_res a =\n Failure ex\n\n/// Liftings\n/// --------\n///\n/// There's a natural lifting from the exception monad into the tactic effect:\n\nlet lift_exn_tac #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\nlet lift_exn_tactic #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern interpretation\n/// ======================\n///\n/// This section implement pattern-matching. This is strictly a one term, one\n/// pattern implementation — handling cases in which mutliple hypotheses match\n/// the same pattern is done later.\n\ntype bindings = list (varname * term)\nlet string_of_bindings (bindings: bindings) =\n String.concat \"\\n\"\n (map (fun (nm, tm) -> (\">> \" ^ nm ^ \": \" ^ term_to_string tm))\n bindings)\n\n(** Match a pattern against a term.\n`cur_bindings` is a list of bindings collected while matching previous parts of\nthe pattern. Returns a result in the exception monad. **)\nlet rec interp_pattern_aux (pat: pattern) (cur_bindings: bindings) (tm:term)\n : Tac (match_res bindings) =\n let interp_var (v: varname) cur_bindings tm =\n match List.Tot.Base.assoc v cur_bindings with\n | Some tm' -> if term_eq tm tm' then return cur_bindings\n else raise (NonLinearMismatch (v, tm, tm'))\n | None -> return ((v, tm) :: cur_bindings) in\n let interp_qn (qn: qn) cur_bindings tm =\n match inspect tm with\n | Tv_UInst fv _\n | Tv_FVar fv ->\n if fv_to_string fv = qn then return cur_bindings\n else raise (NameMismatch (qn, (fv_to_string fv)))\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_type cur_bindings tm =\n match inspect tm with\n | Tv_Type _ -> return cur_bindings\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_app (p_hd p_arg: (p:pattern{p << pat})) cur_bindings tm =\n match inspect tm with\n | Tv_App hd (arg, _) ->\n let? with_hd = interp_pattern_aux p_hd cur_bindings hd in\n let? with_arg = interp_pattern_aux p_arg with_hd arg in\n return with_arg\n | _ -> raise (SimpleMismatch (pat, tm)) in\n match pat with\n | PVar var -> interp_var var cur_bindings tm\n | PQn qn -> interp_qn qn cur_bindings tm\n | PType -> interp_type cur_bindings tm\n | PApp p_hd p_arg -> interp_app p_hd p_arg cur_bindings tm\n\n(** Match a pattern `pat` against a term.\nReturns a result in the exception monad. **)\nlet interp_pattern (pat: pattern) : term -> Tac (match_res bindings) =\n fun (tm: term) ->\n let? rev_bindings = interp_pattern_aux pat [] tm in\n return (List.Tot.Base.rev rev_bindings)\n\n(** Match a term `tm` against a pattern `pat`.\nRaises an exception if the match fails. This is mostly useful for debugging:\nuse ``mgw`` to capture matches. **)\nlet match_term pat (tm : term) : Tac bindings =\n match interp_pattern pat (norm_term [] tm) with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern-matching problems\n/// =========================\n///\n/// Generalizing past single-term single-pattern problems, we obtain the\n/// following notions of pattern-matching problems and solutions:\n\nlet debug msg : Tac unit = () // print msg\n\n/// Definitions\n/// -----------\n\nlet absvar = binding\ntype hypothesis = binding\n\n/// A matching problem is composed of holes (``mp_vars``), hypothesis patterns\n/// (``mp_hyps``), and a goal pattern (``mp_goal``).\n\nnoeq type matching_problem =\n { mp_vars: list varname;\n mp_hyps: list (varname * pattern);\n mp_goal: option pattern }\n\nlet string_of_matching_problem mp =\n let vars =\n String.concat \", \" mp.mp_vars in\n let hyps =\n String.concat \"\\n \"\n (List.Tot.Base.map (fun (nm, pat) ->\n nm ^ \": \" ^ (string_of_pattern pat)) mp.mp_hyps) in\n let goal = match mp.mp_goal with\n | None -> \"_\"\n | Some pat -> string_of_pattern pat in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \"\\n\" ^\n \" goal: \" ^ goal ^ \" }\"\n\n/// A solution is composed of terms captured to mach the holes, and binders\n/// captured to match hypothesis patterns.\n\nnoeq type matching_solution =\n { ms_vars: list (varname * term);\n ms_hyps: list (varname * hypothesis) }\n\nlet string_of_matching_solution ms =\n let vars =\n String.concat \"\\n \"\n (map (fun (varname, tm) ->\n varname ^ \": \" ^ (term_to_string tm)) ms.ms_vars) in\n let hyps =\n String.concat \"\\n \"\n (map (fun (nm, binding) ->\n nm ^ \": \" ^ (binding_to_string binding)) ms.ms_hyps) in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \" }\"\n\n(** Find a varname in an association list; fail if it can't be found. **)\nlet assoc_varname_fail (#b: Type) (key: varname) (ls: list (varname * b))\n : Tac b =\n match List.Tot.Base.assoc key ls with\n | None -> fail (\"Not found: \" ^ key)\n | Some x -> x\n\nlet ms_locate_hyp (a: Type) (solution: matching_solution)\n (name: varname) : Tac hypothesis =\n assoc_varname_fail name solution.ms_hyps\n\nlet ms_locate_var (a: Type) (solution: matching_solution)\n (name: varname) : Tac a =\n unquote #a (assoc_varname_fail name solution.ms_vars)\n\nlet ms_locate_unit (a: Type) _solution _binder_name : Tac unit =\n ()\n\n/// Resolution\n/// ----------\n///\n/// Solving a matching problem is a two-steps process: find an initial\n/// assignment for holes based on the goal pattern, then find a set of\n/// hypotheses matching hypothesis patterns.\n///\n/// Note that the implementation takes a continuation of type\n/// ``matching_solution -> Tac a``. This continuation is needed because we want\n/// users to be able to provide extra criteria on matching solutions (most\n/// commonly, this criterion is that a particular tactic should run\n/// successfuly).\n///\n/// This makes it easy to implement a simple for of search through the context,\n/// where one can find a hypothesis matching a particular predicate by\n/// constructing a trivial matching problem and passing the predicate as the\n/// continuation.\n\n(** Scan ``hypotheses`` for a match for ``pat`` that lets ``body`` succeed.\n\n``name`` is used to refer to the hypothesis matched in the final solution.\n``part_sol`` includes bindings gathered while matching previous solutions. **)\nlet rec solve_mp_for_single_hyp #a\n (name: varname)\n (pat: pattern)\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (part_sol: matching_solution)\n : Tac a =\n match hypotheses with\n | [] ->\n fail #a \"No matching hypothesis\"\n | h :: hs ->\n or_else // Must be in ``Tac`` here to run `body`\n (fun () ->\n match interp_pattern_aux pat part_sol.ms_vars (type_of_binding h) with\n | Failure ex ->\n fail (\"Failed to match hyp: \" ^ (string_of_match_exception ex))\n | Success bindings ->\n let ms_hyps = (name, h) :: part_sol.ms_hyps in\n body ({ part_sol with ms_vars = bindings; ms_hyps = ms_hyps }))\n (fun () ->\n solve_mp_for_single_hyp name pat hs body part_sol)\n\n(** Scan ``hypotheses`` for matches for ``mp_hyps`` that lets ``body``\nsucceed. **)\nlet rec solve_mp_for_hyps #a\n (mp_hyps: list (varname * pattern))\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (partial_solution: matching_solution)\n : Tac a =\n match mp_hyps with\n | [] -> body partial_solution\n | (name, pat) :: pats ->\n solve_mp_for_single_hyp name pat hypotheses\n (solve_mp_for_hyps pats hypotheses body)\n partial_solution\n\n(** Solve a matching problem.\n\nThe solution returned is constructed to ensure that the continuation ``body``\nsucceeds: this implements the usual backtracking-match semantics. **)\nlet solve_mp #a (problem: matching_problem)\n (hypotheses: list hypothesis) (goal: term)\n (body: matching_solution -> Tac a)\n : Tac a =\n let goal_ps =\n match problem.mp_goal with\n | None -> { ms_vars = []; ms_hyps = [] }\n | Some pat ->\n match interp_pattern pat goal with\n | Failure ex -> fail (\"Failed to match goal: \" ^ (string_of_match_exception ex))\n | Success bindings -> { ms_vars = bindings; ms_hyps = [] } in\n solve_mp_for_hyps #a problem.mp_hyps hypotheses body goal_ps\n\n/// A DSL for pattern-matching\n/// ==========================\n///\n/// Using pattern-matching problems as defined above is relatively cumbersome,\n/// so we now introduce a lightweight notation, in two steps: pattern notations,\n/// and matching-problem notations.\n///\n/// Pattern notations\n/// -----------------\n///\n/// The first part of our pattern-matching syntax is pattern notations: we\n/// provide a reflective function which constructs a pattern from a term:\n/// variables are holes, free variables are constants, and applications are\n/// application patterns.\n\n(* FIXME: MOVE *)\nlet name_of_namedv (x:namedv) : Tac string =\n unseal (inspect_namedv x).ppname\n\n(** Compile a term `tm` into a pattern. **)\nlet rec pattern_of_term_ex tm : Tac (match_res pattern) =\n match inspect tm with\n | Tv_Var bv ->\n return (PVar (name_of_namedv bv))\n | Tv_FVar fv\n | Tv_UInst fv _ ->\n let qn = fv_to_string fv in\n return (PQn qn)\n | Tv_Type _ ->\n return PType\n | Tv_App f (x, _) ->\n let? fpat = pattern_of_term_ex f in\n let? xpat = pattern_of_term_ex x in\n return (PApp fpat xpat)\n | _ -> raise (UnsupportedTermInPattern tm)\n\n(** β-reduce a term `tm`.\nThis is useful to remove needles function applications introduced by F*, like\n``(fun a b c -> a) 1 2 3``. **)\nlet beta_reduce (tm: term) : Tac term =\n norm_term [] tm\n\n(** Compile a term `tm` into a pattern. **)\nlet pattern_of_term tm : Tac pattern =\n match pattern_of_term_ex tm with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Problem notations\n/// -----------------\n///\n/// We then introduce a DSL for matching problems, best explained on the\n/// following example::\n///\n/// (fun (a b c: ①) (h1 h2 h3: hyp ②) (g: pm_goal ③) → ④)\n///\n/// This notation is intended to express a pattern-matching problems with three\n/// holes ``a``, ``b``, and ``c`` of type ①, matching hypotheses ``h1``, ``h2``,\n/// and ``h3`` against pattern ② and the goal against the pattern ③. The body\n/// of the notation (④) is then run with appropriate terms bound to ``a``,\n/// ``b``, and ``c``, appropriate binders bound to ``h1``, ``h2``, and ``h3``,\n/// and ``()`` bound to ``g``.\n///\n/// We call these patterns ``abspat``s (abstraction patterns), and we provide\n/// facilities to parse them into matching problems, and to run their bodies\n/// against a particular matching solution.\n\n// We used to annotate variables with an explicit 'var' marker, but then that\n// var annotation leaked into the types of other hypotheses due to type\n// inference, requiring non-trivial normalization.\n\n// let var (a: Type) = a\nlet hyp (a: Type) = binding\nlet pm_goal (a: Type) = unit\n\nlet hyp_qn = `%hyp\nlet goal_qn = `%pm_goal\n\nnoeq type abspat_binder_kind =\n| ABKVar of typ\n| ABKHyp\n| ABKGoal\n\nlet string_of_abspat_binder_kind = function\n | ABKVar _ -> \"varname\"\n | ABKHyp -> \"hyp\"\n | ABKGoal -> \"goal\"\n\nnoeq type abspat_argspec =\n { asa_name: absvar;\n asa_kind: abspat_binder_kind }\n\n// We must store this continuation, because recomputing it yields different\n// names when the binders are re-opened.\ntype abspat_continuation =\n list abspat_argspec * term\n\nlet type_of_named_binder (nb : binder) : term =\n nb.sort\n\nPartial implementation:\nlet classify_abspat_binder (b: binder) : Tac (abspat_binder_kind * term) =\n \n"
},
{
"role": "assistant",
"content": "let varname = \"v\" in\nlet hyp_pat = PApp (PQn hyp_qn) (PVar varname) in\nlet goal_pat = PApp (PQn goal_qn) (PVar varname) in\nlet typ = type_of_named_binder b in\nmatch interp_pattern hyp_pat typ with\n| Success [_, hyp_typ] -> ABKHyp, hyp_typ\n| Success _ -> fail \"classifiy_abspat_binder: impossible (1)\"\n| Failure _ ->\n match interp_pattern goal_pat typ with\n | Success [_, goal_typ] -> ABKGoal, goal_typ\n | Success _ -> fail \"classifiy_abspat_binder: impossible (2)\"\n | Failure _ -> ABKVar typ, typ"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: FStar.Tactics.PatternMatching.abspat_binder_kind -> Prims.string\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2008-2018 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n/// ==========================\n/// Pattern-matching tactics\n/// ==========================\n///\n/// :Author: Clément Pit-Claudel\n/// :Contact: clement.pitclaudel@live.com\n/// :Date: 2017-10-13\n\nmodule FStar.Tactics.PatternMatching\n\nopen FStar.Tactics.V2\n\n/// Contents\n/// ========\n///\n/// 1 Contents\n/// 2 Motivation\n/// 3 Some utility functions\n/// 4 Pattern types\n/// 5 Pattern matching exceptions\n/// 5.1 Types of exceptions\n/// 5.2 The exception monad\n/// 5.3 Liftings\n/// 6 Pattern interpretation\n/// 7 Pattern-matching problems\n/// 7.1 Definitions\n/// 7.2 Resolution\n/// 8 A DSL for pattern-matching\n/// 8.1 Pattern notations\n/// 8.2 Problem notations\n/// 8.3 Continuations\n/// 9 Putting it all together\n/// 10 Examples\n/// 10.1 Simple examples\n/// 10.2 A real-life example\n/// 11 Possible extensions\n/// 12 Notes\n///\n/// Motivation\n/// ==========\n///\n/// Suppose you have a goal of the form ``squash (a == b)``. How do you capture\n/// `a` and `b` for further inspection?\n///\n/// Here's a basic (but cumbersome!) implementation:\n\nlet fetch_eq_side () : Tac (term * term) =\n let g = cur_goal () in\n match inspect g with\n | Tv_App squash (g, _) ->\n (match inspect squash with\n | Tv_UInst squash _\n | Tv_FVar squash ->\n if fv_to_string squash = flatten_name squash_qn then\n (match inspect g with\n | Tv_App eq_type_x (y, _) ->\n (match inspect eq_type_x with\n | Tv_App eq_type (x, _) ->\n (match inspect eq_type with\n | Tv_App eq (typ, _) ->\n (match inspect eq with\n | Tv_UInst eq _\n | Tv_FVar eq ->\n if fv_to_string eq = flatten_name eq2_qn then\n (x, y)\n else fail \"not an equality\"\n | _ -> fail \"not an app2 of fvar: \")\n | _ -> fail \"not an app3\")\n | _ -> fail \"not an app2\")\n | _ -> fail \"not an app under squash\")\n else fail \"not a squash\"\n | _ -> fail \"not an app of fvar at top level\")\n | _ -> fail \"not an app at top level\"\n\n/// …and here's how you could use it:\n\n(* let _ = *)\n(* assert_by_tactic (1 + 1 == 2) *)\n(* (fun () -> let l, r = fetch_eq_side () in *)\n(* print (term_to_string l ^ \" / \" ^ term_to_string r)) *)\n\n/// This file defines pattern-matching primitives that let you write the same\n/// thing like this…\n///\n/// .. code:: fstar\n///\n/// let fetch_eq_side' #a () : Tac (term * term) =\n/// gpm (fun (left right: a) (g: pm_goal (squash (left == right))) ->\n/// (quote left, quote right) <: Tac (term * term))\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (fun () -> let l, r = fetch_eq_side' #int () in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r))\n///\n/// …or, more succinctly, like this:\n///\n/// .. code:: fstar\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (gpm (fun (left right: int) (g: pm_goal (squash (left == right))) ->\n/// let l, r = quote left, quote right in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r) <: Tac unit))\n\n\n/// Some utility functions\n/// ======================\n///\n/// (Skip over this part on a quick read — these are just convenience functions)\n\n\n(** Ensure that tactic `t` fails. **)\nlet mustfail #a (t: unit -> Tac a) (message: string) : Tac unit =\n match trytac t with\n | Some _ -> fail message\n | None -> ()\n\n/// The following two tactics are needed because of issues with the ``Tac``\n/// effect.\n\nlet implies_intro' () : Tac unit =\n let _ = implies_intro () in ()\n\nlet repeat' #a (f: unit -> Tac a) : Tac unit =\n let _ = repeat f in ()\n\nlet and_elim' (h: binding) : Tac unit =\n and_elim (pack (Tv_Var h));\n clear h\n\n(** Use a hypothesis at type a to satisfy a goal at type squash a *)\nlet exact_hyp (a: Type0) (h: namedv) : Tac unit =\n let hd = quote (FStar.Squash.return_squash #a) in\n exact (mk_app hd [((pack (Tv_Var h)), Q_Explicit)])\n\n(** Use a hypothesis h (of type a) to satisfy a goal at type a *)\nlet exact_hyp' (h: namedv): Tac unit =\n exact (pack (Tv_Var h))\n\n/// Pattern types\n/// =============\n///\n/// Patterns are defined using a simple inductive type, mirroring the structure\n/// of ``term_view``.\n\ntype varname = string\n\ntype qn = string\n\ntype pattern =\n| PVar: name: varname -> pattern\n| PQn: qn: qn -> pattern\n| PType: pattern\n| PApp: hd: pattern -> arg: pattern -> pattern\n\nlet desc_of_pattern = function\n| PVar _ -> \"a variable\"\n| PQn qn -> \"a constant (\" ^ qn ^ \")\"\n| PType -> \"Type\"\n| PApp _ _ -> \"a function application\"\n\nlet rec string_of_pattern = function\n| PVar x -> \"?\" ^ x\n| PQn qn -> qn\n| PType -> \"Type\"\n| PApp l r -> \"(\" ^ string_of_pattern l ^ \" \"\n ^ string_of_pattern r ^ \")\"\n\n/// Pattern matching exceptions\n/// ===========================\n///\n/// Pattern-matching is defined as a pure, monadic function (because of issues\n/// with combining DM4F effects, but also because it helps with debugging).\n/// This section defines the exception monad.\n///\n/// Types of exceptions\n/// -------------------\n\nnoeq type match_exception =\n| NameMismatch of qn * qn\n| SimpleMismatch of pattern * term\n| NonLinearMismatch of varname * term * term\n| UnsupportedTermInPattern of term\n| IncorrectTypeInAbsPatBinder of typ\n\nlet term_head t : Tac string =\n match inspect t with\n | Tv_Var bv -> \"Tv_Var\"\n | Tv_BVar fv -> \"Tv_BVar\"\n | Tv_FVar fv -> \"Tv_FVar\"\n | Tv_UInst _ _ -> \"Tv_UInst\"\n | Tv_App f x -> \"Tv_App\"\n | Tv_Abs x t -> \"Tv_Abs\"\n | Tv_Arrow x t -> \"Tv_Arrow\"\n | Tv_Type _ -> \"Tv_Type\"\n | Tv_Refine x t -> \"Tv_Refine\"\n | Tv_Const cst -> \"Tv_Const\"\n | Tv_Uvar i t -> \"Tv_Uvar\"\n | Tv_Let r attrs b t1 t2 -> \"Tv_Let\"\n | Tv_Match t _ branches -> \"Tv_Match\"\n | Tv_AscribedT _ _ _ _ -> \"Tv_AscribedT\"\n | Tv_AscribedC _ _ _ _ -> \"Tv_AscribedC\"\n | Tv_Unknown -> \"Tv_Unknown\"\n | Tv_Unsupp -> \"Tv_Unsupp\"\n\nlet string_of_match_exception = function\n | NameMismatch (qn1, qn2) ->\n \"Match failure (name mismatch): expecting \" ^\n qn1 ^ \", found \" ^ qn2\n | SimpleMismatch (pat, tm) ->\n \"Match failure (sort mismatch): expecting \" ^\n desc_of_pattern pat ^ \", got \" ^ term_to_string tm\n | NonLinearMismatch (nm, t1, t2) ->\n \"Match failure (nonlinear mismatch): variable \" ^ nm ^\n \" needs to match both \" ^ (term_to_string t1) ^\n \" and \" ^ (term_to_string t2)\n | UnsupportedTermInPattern tm ->\n \"Match failure (unsupported term in pattern): \" ^\n term_to_string tm ^ \" (\" ^ term_head tm ^ \")\"\n | IncorrectTypeInAbsPatBinder typ ->\n \"Incorrect type in pattern-matching binder: \" ^\n term_to_string typ ^ \" (use one of ``var``, ``hyp …``, or ``goal …``)\"\n\n/// The exception monad\n/// -------------------\n\nnoeq type match_res a =\n| Success of a\n| Failure of match_exception\n\nlet return #a (x: a) : match_res a =\n Success x\n\nlet (let?) (#a #b: Type)\n (f: match_res a)\n (g: a -> Tac (match_res b))\n : Tac (match_res b) =\n match f with\n | Success aa -> g aa\n | Failure ex -> Failure ex\n\nlet raise #a (ex: match_exception) : match_res a =\n Failure ex\n\n/// Liftings\n/// --------\n///\n/// There's a natural lifting from the exception monad into the tactic effect:\n\nlet lift_exn_tac #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\nlet lift_exn_tactic #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern interpretation\n/// ======================\n///\n/// This section implement pattern-matching. This is strictly a one term, one\n/// pattern implementation — handling cases in which mutliple hypotheses match\n/// the same pattern is done later.\n\ntype bindings = list (varname * term)\nlet string_of_bindings (bindings: bindings) =\n String.concat \"\\n\"\n (map (fun (nm, tm) -> (\">> \" ^ nm ^ \": \" ^ term_to_string tm))\n bindings)\n\n(** Match a pattern against a term.\n`cur_bindings` is a list of bindings collected while matching previous parts of\nthe pattern. Returns a result in the exception monad. **)\nlet rec interp_pattern_aux (pat: pattern) (cur_bindings: bindings) (tm:term)\n : Tac (match_res bindings) =\n let interp_var (v: varname) cur_bindings tm =\n match List.Tot.Base.assoc v cur_bindings with\n | Some tm' -> if term_eq tm tm' then return cur_bindings\n else raise (NonLinearMismatch (v, tm, tm'))\n | None -> return ((v, tm) :: cur_bindings) in\n let interp_qn (qn: qn) cur_bindings tm =\n match inspect tm with\n | Tv_UInst fv _\n | Tv_FVar fv ->\n if fv_to_string fv = qn then return cur_bindings\n else raise (NameMismatch (qn, (fv_to_string fv)))\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_type cur_bindings tm =\n match inspect tm with\n | Tv_Type _ -> return cur_bindings\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_app (p_hd p_arg: (p:pattern{p << pat})) cur_bindings tm =\n match inspect tm with\n | Tv_App hd (arg, _) ->\n let? with_hd = interp_pattern_aux p_hd cur_bindings hd in\n let? with_arg = interp_pattern_aux p_arg with_hd arg in\n return with_arg\n | _ -> raise (SimpleMismatch (pat, tm)) in\n match pat with\n | PVar var -> interp_var var cur_bindings tm\n | PQn qn -> interp_qn qn cur_bindings tm\n | PType -> interp_type cur_bindings tm\n | PApp p_hd p_arg -> interp_app p_hd p_arg cur_bindings tm\n\n(** Match a pattern `pat` against a term.\nReturns a result in the exception monad. **)\nlet interp_pattern (pat: pattern) : term -> Tac (match_res bindings) =\n fun (tm: term) ->\n let? rev_bindings = interp_pattern_aux pat [] tm in\n return (List.Tot.Base.rev rev_bindings)\n\n(** Match a term `tm` against a pattern `pat`.\nRaises an exception if the match fails. This is mostly useful for debugging:\nuse ``mgw`` to capture matches. **)\nlet match_term pat (tm : term) : Tac bindings =\n match interp_pattern pat (norm_term [] tm) with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern-matching problems\n/// =========================\n///\n/// Generalizing past single-term single-pattern problems, we obtain the\n/// following notions of pattern-matching problems and solutions:\n\nlet debug msg : Tac unit = () // print msg\n\n/// Definitions\n/// -----------\n\nlet absvar = binding\ntype hypothesis = binding\n\n/// A matching problem is composed of holes (``mp_vars``), hypothesis patterns\n/// (``mp_hyps``), and a goal pattern (``mp_goal``).\n\nnoeq type matching_problem =\n { mp_vars: list varname;\n mp_hyps: list (varname * pattern);\n mp_goal: option pattern }\n\nlet string_of_matching_problem mp =\n let vars =\n String.concat \", \" mp.mp_vars in\n let hyps =\n String.concat \"\\n \"\n (List.Tot.Base.map (fun (nm, pat) ->\n nm ^ \": \" ^ (string_of_pattern pat)) mp.mp_hyps) in\n let goal = match mp.mp_goal with\n | None -> \"_\"\n | Some pat -> string_of_pattern pat in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \"\\n\" ^\n \" goal: \" ^ goal ^ \" }\"\n\n/// A solution is composed of terms captured to mach the holes, and binders\n/// captured to match hypothesis patterns.\n\nnoeq type matching_solution =\n { ms_vars: list (varname * term);\n ms_hyps: list (varname * hypothesis) }\n\nlet string_of_matching_solution ms =\n let vars =\n String.concat \"\\n \"\n (map (fun (varname, tm) ->\n varname ^ \": \" ^ (term_to_string tm)) ms.ms_vars) in\n let hyps =\n String.concat \"\\n \"\n (map (fun (nm, binding) ->\n nm ^ \": \" ^ (binding_to_string binding)) ms.ms_hyps) in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \" }\"\n\n(** Find a varname in an association list; fail if it can't be found. **)\nlet assoc_varname_fail (#b: Type) (key: varname) (ls: list (varname * b))\n : Tac b =\n match List.Tot.Base.assoc key ls with\n | None -> fail (\"Not found: \" ^ key)\n | Some x -> x\n\nlet ms_locate_hyp (a: Type) (solution: matching_solution)\n (name: varname) : Tac hypothesis =\n assoc_varname_fail name solution.ms_hyps\n\nlet ms_locate_var (a: Type) (solution: matching_solution)\n (name: varname) : Tac a =\n unquote #a (assoc_varname_fail name solution.ms_vars)\n\nlet ms_locate_unit (a: Type) _solution _binder_name : Tac unit =\n ()\n\n/// Resolution\n/// ----------\n///\n/// Solving a matching problem is a two-steps process: find an initial\n/// assignment for holes based on the goal pattern, then find a set of\n/// hypotheses matching hypothesis patterns.\n///\n/// Note that the implementation takes a continuation of type\n/// ``matching_solution -> Tac a``. This continuation is needed because we want\n/// users to be able to provide extra criteria on matching solutions (most\n/// commonly, this criterion is that a particular tactic should run\n/// successfuly).\n///\n/// This makes it easy to implement a simple for of search through the context,\n/// where one can find a hypothesis matching a particular predicate by\n/// constructing a trivial matching problem and passing the predicate as the\n/// continuation.\n\n(** Scan ``hypotheses`` for a match for ``pat`` that lets ``body`` succeed.\n\n``name`` is used to refer to the hypothesis matched in the final solution.\n``part_sol`` includes bindings gathered while matching previous solutions. **)\nlet rec solve_mp_for_single_hyp #a\n (name: varname)\n (pat: pattern)\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (part_sol: matching_solution)\n : Tac a =\n match hypotheses with\n | [] ->\n fail #a \"No matching hypothesis\"\n | h :: hs ->\n or_else // Must be in ``Tac`` here to run `body`\n (fun () ->\n match interp_pattern_aux pat part_sol.ms_vars (type_of_binding h) with\n | Failure ex ->\n fail (\"Failed to match hyp: \" ^ (string_of_match_exception ex))\n | Success bindings ->\n let ms_hyps = (name, h) :: part_sol.ms_hyps in\n body ({ part_sol with ms_vars = bindings; ms_hyps = ms_hyps }))\n (fun () ->\n solve_mp_for_single_hyp name pat hs body part_sol)\n\n(** Scan ``hypotheses`` for matches for ``mp_hyps`` that lets ``body``\nsucceed. **)\nlet rec solve_mp_for_hyps #a\n (mp_hyps: list (varname * pattern))\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (partial_solution: matching_solution)\n : Tac a =\n match mp_hyps with\n | [] -> body partial_solution\n | (name, pat) :: pats ->\n solve_mp_for_single_hyp name pat hypotheses\n (solve_mp_for_hyps pats hypotheses body)\n partial_solution\n\n(** Solve a matching problem.\n\nThe solution returned is constructed to ensure that the continuation ``body``\nsucceeds: this implements the usual backtracking-match semantics. **)\nlet solve_mp #a (problem: matching_problem)\n (hypotheses: list hypothesis) (goal: term)\n (body: matching_solution -> Tac a)\n : Tac a =\n let goal_ps =\n match problem.mp_goal with\n | None -> { ms_vars = []; ms_hyps = [] }\n | Some pat ->\n match interp_pattern pat goal with\n | Failure ex -> fail (\"Failed to match goal: \" ^ (string_of_match_exception ex))\n | Success bindings -> { ms_vars = bindings; ms_hyps = [] } in\n solve_mp_for_hyps #a problem.mp_hyps hypotheses body goal_ps\n\n/// A DSL for pattern-matching\n/// ==========================\n///\n/// Using pattern-matching problems as defined above is relatively cumbersome,\n/// so we now introduce a lightweight notation, in two steps: pattern notations,\n/// and matching-problem notations.\n///\n/// Pattern notations\n/// -----------------\n///\n/// The first part of our pattern-matching syntax is pattern notations: we\n/// provide a reflective function which constructs a pattern from a term:\n/// variables are holes, free variables are constants, and applications are\n/// application patterns.\n\n(* FIXME: MOVE *)\nlet name_of_namedv (x:namedv) : Tac string =\n unseal (inspect_namedv x).ppname\n\n(** Compile a term `tm` into a pattern. **)\nlet rec pattern_of_term_ex tm : Tac (match_res pattern) =\n match inspect tm with\n | Tv_Var bv ->\n return (PVar (name_of_namedv bv))\n | Tv_FVar fv\n | Tv_UInst fv _ ->\n let qn = fv_to_string fv in\n return (PQn qn)\n | Tv_Type _ ->\n return PType\n | Tv_App f (x, _) ->\n let? fpat = pattern_of_term_ex f in\n let? xpat = pattern_of_term_ex x in\n return (PApp fpat xpat)\n | _ -> raise (UnsupportedTermInPattern tm)\n\n(** β-reduce a term `tm`.\nThis is useful to remove needles function applications introduced by F*, like\n``(fun a b c -> a) 1 2 3``. **)\nlet beta_reduce (tm: term) : Tac term =\n norm_term [] tm\n\n(** Compile a term `tm` into a pattern. **)\nlet pattern_of_term tm : Tac pattern =\n match pattern_of_term_ex tm with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Problem notations\n/// -----------------\n///\n/// We then introduce a DSL for matching problems, best explained on the\n/// following example::\n///\n/// (fun (a b c: ①) (h1 h2 h3: hyp ②) (g: pm_goal ③) → ④)\n///\n/// This notation is intended to express a pattern-matching problems with three\n/// holes ``a``, ``b``, and ``c`` of type ①, matching hypotheses ``h1``, ``h2``,\n/// and ``h3`` against pattern ② and the goal against the pattern ③. The body\n/// of the notation (④) is then run with appropriate terms bound to ``a``,\n/// ``b``, and ``c``, appropriate binders bound to ``h1``, ``h2``, and ``h3``,\n/// and ``()`` bound to ``g``.\n///\n/// We call these patterns ``abspat``s (abstraction patterns), and we provide\n/// facilities to parse them into matching problems, and to run their bodies\n/// against a particular matching solution.\n\n// We used to annotate variables with an explicit 'var' marker, but then that\n// var annotation leaked into the types of other hypotheses due to type\n// inference, requiring non-trivial normalization.\n\n// let var (a: Type) = a\nlet hyp (a: Type) = binding\nlet pm_goal (a: Type) = unit\n\nlet hyp_qn = `%hyp\nlet goal_qn = `%pm_goal\n\nnoeq type abspat_binder_kind =\n| ABKVar of typ\n| ABKHyp\n| ABKGoal\n\nPartial implementation:\nlet string_of_abspat_binder_kind =\n \n"
},
{
"role": "assistant",
"content": "function\n| ABKVar _ -> \"varname\"\n| ABKHyp -> \"hyp\"\n| ABKGoal -> \"goal\""
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> Prims.GTot (MerkleTree.Spec.merkle_tree (n - 1))\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nPartial implementation:\nlet mt_next_lv #_ #f #n mt =\n \n"
},
{
"role": "assistant",
"content": "hs_next_lv #_ #f #(pow2 (n - 1)) mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n ws: Hacl.Spec.SHA2.Vec.ws_spec a m ->\n j: Prims.nat{j < Hacl.Spec.SHA2.Vec.lanes a m} ->\n i: Prims.nat{i < 16}\n -> FStar.Pervasives.Lemma\n (ensures\n (let l = Hacl.Spec.SHA2.Vec.lanes a m in\n (Lib.IntVector.vec_v (Hacl.Spec.SHA2.Vec.transpose_ws8 ws).[ i ]).[ j ] ==\n (Lib.IntVector.vec_v ws.[ (i / l) * l + j ]).[ i % l ]))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Hacl.Spec.SHA2.Lemmas\n\nopen FStar.Mul\nopen Lib.IntTypes\nopen Lib.Sequence\nopen Lib.IntVector\nopen Lib.IntVector.Transpose\n\nopen Spec.Hash.Definitions\nopen Hacl.Spec.SHA2.Vec\n\n\n#set-options \"--z3rlimit 50 --fuel 0 --ifuel 1\"\n\nval transpose_ws4_lemma_ij:\n #a:sha2_alg\n -> #m:m_spec{lanes a m == 4} // lanes a m * lanes a m = 16\n -> ws:ws_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 16} ->\n Lemma\n (let l = lanes a m in\n (vec_v (transpose_ws4 ws).[i]).[j] == (vec_v ws.[i / l * l + j]).[i % l])\n\nlet transpose_ws4_lemma_ij #a #m ws j i =\n let l = lanes a m in\n let i_sub = i / l in\n let j_sub = i % l in\n assert (i_sub * l + j_sub == i);\n\n let vs = sub ws (i_sub * l) l in\n eq_intro (sub (transpose_ws4 ws) (i_sub * l) l) (transpose4x4_lseq vs);\n //assert ((transpose_ws4 ws).[i] == (sub (transpose_ws4 ws) (i_sub * l) l).[j_sub]);\n //assert ((transpose_ws4 ws).[i] == (transpose4x4_lseq vs).[j_sub]);\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v (transpose4x4_lseq vs).[j_sub]).[j]);\n transpose4x4_lemma vs;\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v vs.[j]).[j_sub]);\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v ws.[i_sub * l + j]).[j_sub])\n\n\nval transpose_ws8_lemma_ij:\n #a:sha2_alg\n -> #m:m_spec{lanes a m == 8}\n -> ws:ws_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 16} ->\n Lemma\n (let l = lanes a m in\n (vec_v (transpose_ws8 ws).[i]).[j] == (vec_v ws.[i / l * l + j]).[i % l])\n\nPartial implementation:\nlet transpose_ws8_lemma_ij #a #m ws j i =\n \n"
},
{
"role": "assistant",
"content": "let l = lanes a m in\nlet i_sub = i / l in\nlet j_sub = i % l in\nassert (i_sub * l + j_sub == i);\nlet vs = sub ws (i_sub * l) l in\neq_intro (sub (transpose_ws8 ws) (i_sub * l) l) (transpose8x8_lseq vs);\nassert ((vec_v (transpose_ws8 ws).[ i ]).[ j ] == (vec_v (transpose8x8_lseq vs).[ j_sub ]).[ j ]);\ntranspose8x8_lemma vs;\nassert ((vec_v (transpose_ws8 ws).[ i ]).[ j ] == (vec_v ws.[ i_sub * lanes a m + j ]).[ j_sub ])"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: MerkleTree.Spec.hash_fun_t\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nPartial implementation:\nlet sha256_compress src1 src2 =\n \n"
},
{
"role": "assistant",
"content": "let sz = Spec.Hash.Definitions.SHA2_256 in\nlet hash_alg = Spec.Hash.Definitions.SHA2_256 in\nlet acc = Spec.Agile.Hash.init hash_alg in\nlet acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\nSpec.Agile.Hash.finish hash_alg acc ()"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: GradedMonad.monoid Prims.nat\nEffect: Prims.Tot\n\nContext:\nmodule GradedMonad\n\n(* NB: this is the old version of the graded monad that has a monoid\nindex parameter everywhere instead of a constraint on the type. We\nkeep it for CI purposes. See doc/book/code/GradedMonad.fst for better\nversion. *)\n\n#set-options \"--warn_error -350\"\n\n//SNIPPET_START:monoid$\nclass monoid (a:Type) =\n{\n op : a -> a -> a;\n one : a;\n properties: squash (\n (forall (x:a). op one x == x /\\ op x one == x) /\\\n (forall (x y z:a). op x (op y z) == op (op x y) z)\n );\n}\n\nPartial implementation:\n[@@ FStar.Tactics.Typeclasses.tcinstance]\nlet monoid_nat_plus:monoid nat =\n \n"
},
{
"role": "assistant",
"content": "{ op = (fun (x: nat) (y: nat) -> x + y); one = 0; properties = () }"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n\n -> FStar.Pervasives.Lemma\n (ensures\n FStar.Seq.Base.equal (MerkleTree.Spec.mt_next_lv (MerkleTree.Spec.mt_right mt))\n (MerkleTree.Spec.mt_right (MerkleTree.Spec.mt_next_lv mt)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n\nPartial implementation:\nlet mt_next_lv_mt_right #hsz #f #n mt =\n \n"
},
{
"role": "assistant",
"content": "hs_next_lv_slice #hsz #f #(pow2 (n - 1)) mt (pow2 (n - 2)) (pow2 (n - 1))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: n: Prims.pos -> mt: MerkleTree.Spec.merkle_tree n -> nmt: MerkleTree.Spec.merkle_tree (n - 1)\n -> Prims.GTot Type0\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n\nPartial implementation:\nlet mt_next_rel #hsz #f n mt nmt =\n \n"
},
{
"role": "assistant",
"content": "hs_next_rel #hsz #f (pow2 (n - 1)) mt nmt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> MerkleTree.Spec.merkle_tree (n - 1)\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nPartial implementation:\nlet mt_right #_ #n mt =\n \n"
},
{
"role": "assistant",
"content": "S.slice mt (pow2 (n - 1)) (pow2 n)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n n: Prims.nat ->\n hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n} ->\n nhs: MerkleTree.Spec.hashes{FStar.Seq.Base.length nhs = n}\n -> Prims.GTot Type0\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n\nPartial implementation:\nlet hs_next_rel #hsz #f n hs nhs =\n \n"
},
{
"role": "assistant",
"content": "forall (i: nat{i < n}).\n S.index nhs i == padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n j: Prims.nat ->\n n: Prims.pos{j <= Prims.pow2 n} ->\n mt1: MerkleTree.Spec.merkle_tree n ->\n mt2: MerkleTree.Spec.merkle_tree n\n -> FStar.Pervasives.Lemma\n (requires FStar.Seq.Base.equal (FStar.Seq.Base.slice mt1 0 j) (FStar.Seq.Base.slice mt2 0 j))\n (ensures\n FStar.Seq.Base.equal (FStar.Seq.Base.slice (MerkleTree.Spec.mt_next_lv mt1) 0 (j / 2))\n (FStar.Seq.Base.slice (MerkleTree.Spec.mt_next_lv mt2) 0 (j / 2)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n\nPartial implementation:\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n \n"
},
{
"role": "assistant",
"content": "hs_next_lv_equiv #_ #f j (pow2 (n - 1)) mt1 mt2"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n st: Hacl.Spec.SHA2.Vec.state_spec a m ->\n j: Prims.nat{j < Hacl.Spec.SHA2.Vec.lanes a m} ->\n i: Prims.nat{i < 8 * Spec.Hash.Definitions.word_length a}\n -> FStar.Pervasives.Lemma\n (ensures\n (let l = Hacl.Spec.SHA2.Vec.lanes a m in\n let ind = 8 * j + i / Spec.Hash.Definitions.word_length a in\n FStar.Seq.Base.index (Lib.IntVector.vec_v (Hacl.Spec.SHA2.Vec.transpose_state st).[ ind / l\n ])\n (ind % l) ==\n FStar.Seq.Base.index (Hacl.Spec.SHA2.Vec.state_spec_v st).[ j ]\n (i / Spec.Hash.Definitions.word_length a)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Hacl.Spec.SHA2.Lemmas\n\nopen FStar.Mul\nopen Lib.IntTypes\nopen Lib.Sequence\nopen Lib.IntVector\nopen Lib.IntVector.Transpose\n\nopen Spec.Hash.Definitions\nopen Hacl.Spec.SHA2.Vec\n\n\n#set-options \"--z3rlimit 50 --fuel 0 --ifuel 1\"\n\nval transpose_ws4_lemma_ij:\n #a:sha2_alg\n -> #m:m_spec{lanes a m == 4} // lanes a m * lanes a m = 16\n -> ws:ws_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 16} ->\n Lemma\n (let l = lanes a m in\n (vec_v (transpose_ws4 ws).[i]).[j] == (vec_v ws.[i / l * l + j]).[i % l])\n\nlet transpose_ws4_lemma_ij #a #m ws j i =\n let l = lanes a m in\n let i_sub = i / l in\n let j_sub = i % l in\n assert (i_sub * l + j_sub == i);\n\n let vs = sub ws (i_sub * l) l in\n eq_intro (sub (transpose_ws4 ws) (i_sub * l) l) (transpose4x4_lseq vs);\n //assert ((transpose_ws4 ws).[i] == (sub (transpose_ws4 ws) (i_sub * l) l).[j_sub]);\n //assert ((transpose_ws4 ws).[i] == (transpose4x4_lseq vs).[j_sub]);\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v (transpose4x4_lseq vs).[j_sub]).[j]);\n transpose4x4_lemma vs;\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v vs.[j]).[j_sub]);\n assert ((vec_v (transpose_ws4 ws).[i]).[j] == (vec_v ws.[i_sub * l + j]).[j_sub])\n\n\nval transpose_ws8_lemma_ij:\n #a:sha2_alg\n -> #m:m_spec{lanes a m == 8}\n -> ws:ws_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 16} ->\n Lemma\n (let l = lanes a m in\n (vec_v (transpose_ws8 ws).[i]).[j] == (vec_v ws.[i / l * l + j]).[i % l])\n\nlet transpose_ws8_lemma_ij #a #m ws j i =\n let l = lanes a m in\n let i_sub = i / l in\n let j_sub = i % l in\n assert (i_sub * l + j_sub == i);\n\n let vs = sub ws (i_sub * l) l in\n eq_intro (sub (transpose_ws8 ws) (i_sub * l) l) (transpose8x8_lseq vs);\n assert ((vec_v (transpose_ws8 ws).[i]).[j] == (vec_v (transpose8x8_lseq vs).[j_sub]).[j]);\n transpose8x8_lemma vs;\n assert ((vec_v (transpose_ws8 ws).[i]).[j] == (vec_v ws.[i_sub * lanes a m + j]).[j_sub])\n\n\nval transpose_ws_lemma_ij:\n #a:sha2_alg\n -> #m:m_spec{is_supported a m}\n -> ws:ws_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 16} ->\n Lemma\n (let l = lanes a m in\n ((ws_spec_v (transpose_ws ws)).[j]).[i] == (vec_v ws.[i / l * l + j]).[i % l])\n\nlet transpose_ws_lemma_ij #a #m ws j i =\n assert (((ws_spec_v (transpose_ws ws)).[j]).[i] == (vec_v (transpose_ws ws).[i]).[j]);\n match lanes a m with\n | 1 -> ()\n | 4 -> transpose_ws4_lemma_ij #a #m ws j i\n | 8 -> transpose_ws8_lemma_ij #a #m ws j i\n\n\nval transpose_state4_lemma:\n #a:sha2_alg\n -> #m:m_spec{lanes a m == 4}\n -> st:state_spec a m\n -> j:nat{j < lanes a m}\n -> i:nat{i < 8 * word_length a} ->\n Lemma\n (let l = lanes a m in\n let ind = 8 * j + i / word_length a in\n Seq.index (vec_v (transpose_state st).[ind / l]) (ind % l) ==\n Seq.index (state_spec_v st).[j] (i / word_length a))\n\nPartial implementation:\nlet transpose_state4_lemma #a #m st j i =\n \n"
},
{
"role": "assistant",
"content": "let r0 = transpose4x4_lseq (sub st 0 4) in\ntranspose4x4_lemma (sub st 0 4);\nlet r1 = transpose4x4_lseq (sub st 4 4) in\ntranspose4x4_lemma (sub st 4 4)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n}\n -> Prims.GTot (nhs: MerkleTree.Spec.hashes{FStar.Seq.Base.length nhs = n})\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nPartial implementation:\nlet rec hs_next_lv #hsz #f #n hs =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen S.empty\nelse\n S.cons (padded_hash_fun #hsz f hs.[ 0 ] hs.[ 1 ])\n (hs_next_lv #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> Prims.GTot MerkleTree.Spec.padded_hash\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n\nPartial implementation:\nlet rec mt_get_root #hsz #f #n mt =\n \n"
},
{
"role": "assistant",
"content": "if n = 0 then mt.[ 0 ] else mt_get_root #_ #f (mt_next_lv #_ #f mt)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: n: Prims.nat -> i: Prims.nat{i <= Prims.pow2 n} -> Type0\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nPartial implementation:\nlet rpmt (#hsz: pos) (#f: hash_fun_t) (n: nat) (i: nat{i <= pow2 n}) =\n \n"
},
{
"role": "assistant",
"content": "mt:\nmerkle_tree #hsz n\n {raw_hashes #_ #f (S.slice mt 0 i) /\\ pad_hashes #_ #f (S.slice mt i (S.length mt))}"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> MerkleTree.Spec.merkle_tree (n - 1)\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nPartial implementation:\nlet mt_left #_ #n mt =\n \n"
},
{
"role": "assistant",
"content": "S.slice mt 0 (pow2 (n - 1))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n} -> i: Prims.nat{i < n}\n -> FStar.Pervasives.Lemma\n (ensures\n (MerkleTree.Spec.hs_next_lv hs).[ i ] ==\n MerkleTree.Spec.padded_hash_fun f hs.[ 2 * i ] hs.[ 2 * i + 1 ])\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n\nPartial implementation:\nlet rec hs_next_lv_index #hsz #f #n hs i =\n \n"
},
{
"role": "assistant",
"content": "if n = 0 || i = 0\nthen ()\nelse hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i\n -> FStar.Pervasives.Lemma\n (ensures MerkleTree.Spec.pad_hashes mt <==> HPad? (MerkleTree.Spec.mt_get_root mt))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #_ #f (n-1) (if i <= pow2 (n-1) then 0 else i - pow2 (n-1))\nlet rpmt_right #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n-1) - i) (pow2 n - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n-1)) i;\n mt_right mt\n\n/// Two right-padded Merkle trees collide when\n/// 1) they have the same height (`n`) and number of raw hashes (`i`),\n/// 2) their contents differ, and\n/// 3) their roots are same.\n\n// fournet: we may want to work towards removing 1) using a hash prefix\nnoeq\ntype mt_collide (#hsz:pos) (#f:hash_fun_t #hsz) (n:nat) (i:nat{i <= pow2 n}) = | Collision:\n mt1:rpmt #_ #f n i -> mt2:rpmt #_ #f n i {\n mt1 =!= mt2 /\\\n mt_get_root #_ #f #_ mt1 == mt_get_root #_ #f #_ mt2 } -> mt_collide #_ #f n i\n\nnoeq\ntype hash2_raw_collide = | Collision2:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n lh1:hash -> rh1:hash ->\n lh2:hash -> rh2:hash {\n (lh1 =!= lh2 \\/ rh1 =!= rh2) /\\\n f lh1 rh1 == f lh2 rh2 } -> hash2_raw_collide\n\n/// Auxiliary lemmas for the proof\n\nval rpmt_pad_hashes_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> pad_hashes #_ #f mt )\nlet rpmt_pad_hashes_0 #_ #_ #n #i mt = ()\n\nval rpmt_pad_hashes_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} ->\n mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? mt.[0])\nlet rpmt_pad_hashes_index_0 #_ #_ #n #i mt = ()\n\nval mt_get_root_pad_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n ->\n Lemma (HPad? mt.[0] <==> HPad? (mt_get_root #_ #f mt))\nlet rec mt_get_root_pad_index_0 #hsz #f #n (mt:merkle_tree #hsz n) =\n if n = 0 then ()\n else\n let mt:merkle_tree #hsz (n-1) = mt_next_lv #_ #f #n mt in\n mt_get_root_pad_index_0 #_ #f #(n-1) mt\n\n#pop-options\n\nval rpmt_get_root_pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n\nPartial implementation:\nlet rpmt_get_root_pad_hashes #_ #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "rpmt_pad_hashes_index_0 #_ #f mt;\nmt_get_root_pad_index_0 #_ #f mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes{MerkleTree.Spec.raw_hashes hs}\n -> Prims.Tot (FStar.Seq.Base.seq MerkleTree.Spec.hash)\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n\nPartial implementation:\nlet rec raw_hashes_raws #hsz #f hs =\n \n"
},
{
"role": "assistant",
"content": "if S.length hs = 0\nthen S.empty\nelse S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: n: Prims.pos -> mt: MerkleTree.Spec.merkle_tree n -> nmt: MerkleTree.Spec.merkle_tree (n - 1)\n -> FStar.Pervasives.Lemma (requires MerkleTree.Spec.mt_next_rel n mt nmt)\n (ensures FStar.Seq.Base.equal nmt (MerkleTree.Spec.mt_next_lv mt))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n\nPartial implementation:\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n \n"
},
{
"role": "assistant",
"content": "hs_next_rel_next_lv #_ #f (pow2 (n - 1)) mt nmt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n\n -> FStar.Pervasives.Lemma\n (ensures\n FStar.Seq.Base.equal (MerkleTree.Spec.mt_next_lv (MerkleTree.Spec.mt_left mt))\n (MerkleTree.Spec.mt_left (MerkleTree.Spec.mt_next_lv mt)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n\nPartial implementation:\nlet mt_next_lv_mt_left #hsz #f #n mt =\n \n"
},
{
"role": "assistant",
"content": "hs_next_lv_slice #_ #f #(pow2 (n - 1)) mt 0 (pow2 (n - 2))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: c0: Prims.int -> c1: Prims.int -> c2: Prims.int -> c3: Prims.int -> Prims.int\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_fast_mul1 (a:nat)\n (b a0 a1 a2 a3\n ba0_hi ba0_lo\n ba1_hi ba1_lo\n ba2_hi ba2_lo\n ba3_hi ba3_lo\n s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n\n pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0)\n )\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b)\n =\n assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b*a0) (b*a1) (b*a2) (b*a3)) int_canon;\n //lemma_prod_bounds ba0_hi ba0_lo b a0;\n //lemma_prod_bounds ba1_hi ba1_lo b a1;\n //lemma_prod_bounds ba2_hi ba2_lo b a2;\n //lemma_prod_bounds ba3_hi ba3_lo b a3;\n ()\n\n\nlet lemma_addition (a d:nat) (a0 a1 a2 a3 d0 d1 d2 d3 d4:nat64)\n (s0 s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n d = pow2_five d0 d1 d2 d3 d4 /\\\n (let s0', c0 = add_carry a0 d0 0 in\n let s1', c1 = add_carry a1 d1 c0 in\n let s2', c2 = add_carry a2 d2 c1 in\n let s3', c3 = add_carry a3 d3 c2 in\n let s4', c4 = add_carry d4 0 c3 in\n s0 == s0' /\\\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0))\n (ensures a + d == pow2_five s0 s1 s2 s3 s4)\n =\n ()\n\nlet lemma_carry_wide (a0 a1 a2 a3 a4 a5 a6 a7\n d0 d1 d2 d3 carry\n d0' d1' d2' d3':nat64) : Lemma\n (requires pow2_five d0 d1 d2 d3 carry == 38 * pow2_four a4 a5 a6 a7 + pow2_four a0 a1 a2 a3 /\\\n pow2_four d0' d1' d2' d3' % prime == ((pow2_four d0 d1 d2 d3) + carry * pow2_256) % prime)\n (ensures (pow2_four d0' d1' d2' d3') % prime == (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime)\n =\n calc (==) {\n pow2_four d0' d1' d2' d3' % prime;\n == { calc (==) {\n (pow2_four d0 d1 d2 d3) + carry * pow2_256;\n == { _ by (int_canon()) }\n pow2_five d0 d1 d2 d3 carry;\n }\n }\n pow2_five d0 d1 d2 d3 carry % prime;\n == {}\n (pow2_four a0 a1 a2 a3 + 38 * pow2_four a4 a5 a6 a7) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) (pow2_four a4 a5 a6 a7) }\n (pow2_four a0 a1 a2 a3 + pow2_256 * pow2_four a4 a5 a6 a7) % prime;\n == { _ by (int_canon()) }\n (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime;\n }\n\nPartial implementation:\nlet pow2int_four (c0 c1 c2 c3: int) : int =\n \n"
},
{
"role": "assistant",
"content": "c0 + c1 * pow2_64 + c2 * pow2_128 + c3 * pow2_192"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes -> Prims.Tot Type0\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n\nPartial implementation:\nlet rec raw_hashes #hsz #f hs =\n \n"
},
{
"role": "assistant",
"content": "if S.length hs = 0 then True else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes -> i: Prims.nat -> j: Prims.nat{i <= j && j <= FStar.Seq.Base.length hs}\n -> FStar.Pervasives.Lemma (requires MerkleTree.Spec.raw_hashes hs)\n (ensures MerkleTree.Spec.raw_hashes (FStar.Seq.Base.slice hs i j))\n (decreases j - i)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n\nPartial implementation:\nlet rec raw_hashes_slice #hsz #f hs i j =\n \n"
},
{
"role": "assistant",
"content": "if i = j\nthen ()\nelse\n (raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: tm: FStar.Tactics.NamedView.term\n -> FStar.Tactics.Effect.Tac\n (FStar.Tactics.PatternMatching.matching_problem *\n FStar.Tactics.PatternMatching.abspat_continuation)\nEffect: FStar.Tactics.Effect.Tac\n\nContext:\n(*\n Copyright 2008-2018 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n/// ==========================\n/// Pattern-matching tactics\n/// ==========================\n///\n/// :Author: Clément Pit-Claudel\n/// :Contact: clement.pitclaudel@live.com\n/// :Date: 2017-10-13\n\nmodule FStar.Tactics.PatternMatching\n\nopen FStar.Tactics.V2\n\n/// Contents\n/// ========\n///\n/// 1 Contents\n/// 2 Motivation\n/// 3 Some utility functions\n/// 4 Pattern types\n/// 5 Pattern matching exceptions\n/// 5.1 Types of exceptions\n/// 5.2 The exception monad\n/// 5.3 Liftings\n/// 6 Pattern interpretation\n/// 7 Pattern-matching problems\n/// 7.1 Definitions\n/// 7.2 Resolution\n/// 8 A DSL for pattern-matching\n/// 8.1 Pattern notations\n/// 8.2 Problem notations\n/// 8.3 Continuations\n/// 9 Putting it all together\n/// 10 Examples\n/// 10.1 Simple examples\n/// 10.2 A real-life example\n/// 11 Possible extensions\n/// 12 Notes\n///\n/// Motivation\n/// ==========\n///\n/// Suppose you have a goal of the form ``squash (a == b)``. How do you capture\n/// `a` and `b` for further inspection?\n///\n/// Here's a basic (but cumbersome!) implementation:\n\nlet fetch_eq_side () : Tac (term * term) =\n let g = cur_goal () in\n match inspect g with\n | Tv_App squash (g, _) ->\n (match inspect squash with\n | Tv_UInst squash _\n | Tv_FVar squash ->\n if fv_to_string squash = flatten_name squash_qn then\n (match inspect g with\n | Tv_App eq_type_x (y, _) ->\n (match inspect eq_type_x with\n | Tv_App eq_type (x, _) ->\n (match inspect eq_type with\n | Tv_App eq (typ, _) ->\n (match inspect eq with\n | Tv_UInst eq _\n | Tv_FVar eq ->\n if fv_to_string eq = flatten_name eq2_qn then\n (x, y)\n else fail \"not an equality\"\n | _ -> fail \"not an app2 of fvar: \")\n | _ -> fail \"not an app3\")\n | _ -> fail \"not an app2\")\n | _ -> fail \"not an app under squash\")\n else fail \"not a squash\"\n | _ -> fail \"not an app of fvar at top level\")\n | _ -> fail \"not an app at top level\"\n\n/// …and here's how you could use it:\n\n(* let _ = *)\n(* assert_by_tactic (1 + 1 == 2) *)\n(* (fun () -> let l, r = fetch_eq_side () in *)\n(* print (term_to_string l ^ \" / \" ^ term_to_string r)) *)\n\n/// This file defines pattern-matching primitives that let you write the same\n/// thing like this…\n///\n/// .. code:: fstar\n///\n/// let fetch_eq_side' #a () : Tac (term * term) =\n/// gpm (fun (left right: a) (g: pm_goal (squash (left == right))) ->\n/// (quote left, quote right) <: Tac (term * term))\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (fun () -> let l, r = fetch_eq_side' #int () in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r))\n///\n/// …or, more succinctly, like this:\n///\n/// .. code:: fstar\n///\n/// let _ =\n/// assert_by_tactic (1 + 1 == 2)\n/// (gpm (fun (left right: int) (g: pm_goal (squash (left == right))) ->\n/// let l, r = quote left, quote right in\n/// print (term_to_string l ^ \" / \" ^ term_to_string r) <: Tac unit))\n\n\n/// Some utility functions\n/// ======================\n///\n/// (Skip over this part on a quick read — these are just convenience functions)\n\n\n(** Ensure that tactic `t` fails. **)\nlet mustfail #a (t: unit -> Tac a) (message: string) : Tac unit =\n match trytac t with\n | Some _ -> fail message\n | None -> ()\n\n/// The following two tactics are needed because of issues with the ``Tac``\n/// effect.\n\nlet implies_intro' () : Tac unit =\n let _ = implies_intro () in ()\n\nlet repeat' #a (f: unit -> Tac a) : Tac unit =\n let _ = repeat f in ()\n\nlet and_elim' (h: binding) : Tac unit =\n and_elim (pack (Tv_Var h));\n clear h\n\n(** Use a hypothesis at type a to satisfy a goal at type squash a *)\nlet exact_hyp (a: Type0) (h: namedv) : Tac unit =\n let hd = quote (FStar.Squash.return_squash #a) in\n exact (mk_app hd [((pack (Tv_Var h)), Q_Explicit)])\n\n(** Use a hypothesis h (of type a) to satisfy a goal at type a *)\nlet exact_hyp' (h: namedv): Tac unit =\n exact (pack (Tv_Var h))\n\n/// Pattern types\n/// =============\n///\n/// Patterns are defined using a simple inductive type, mirroring the structure\n/// of ``term_view``.\n\ntype varname = string\n\ntype qn = string\n\ntype pattern =\n| PVar: name: varname -> pattern\n| PQn: qn: qn -> pattern\n| PType: pattern\n| PApp: hd: pattern -> arg: pattern -> pattern\n\nlet desc_of_pattern = function\n| PVar _ -> \"a variable\"\n| PQn qn -> \"a constant (\" ^ qn ^ \")\"\n| PType -> \"Type\"\n| PApp _ _ -> \"a function application\"\n\nlet rec string_of_pattern = function\n| PVar x -> \"?\" ^ x\n| PQn qn -> qn\n| PType -> \"Type\"\n| PApp l r -> \"(\" ^ string_of_pattern l ^ \" \"\n ^ string_of_pattern r ^ \")\"\n\n/// Pattern matching exceptions\n/// ===========================\n///\n/// Pattern-matching is defined as a pure, monadic function (because of issues\n/// with combining DM4F effects, but also because it helps with debugging).\n/// This section defines the exception monad.\n///\n/// Types of exceptions\n/// -------------------\n\nnoeq type match_exception =\n| NameMismatch of qn * qn\n| SimpleMismatch of pattern * term\n| NonLinearMismatch of varname * term * term\n| UnsupportedTermInPattern of term\n| IncorrectTypeInAbsPatBinder of typ\n\nlet term_head t : Tac string =\n match inspect t with\n | Tv_Var bv -> \"Tv_Var\"\n | Tv_BVar fv -> \"Tv_BVar\"\n | Tv_FVar fv -> \"Tv_FVar\"\n | Tv_UInst _ _ -> \"Tv_UInst\"\n | Tv_App f x -> \"Tv_App\"\n | Tv_Abs x t -> \"Tv_Abs\"\n | Tv_Arrow x t -> \"Tv_Arrow\"\n | Tv_Type _ -> \"Tv_Type\"\n | Tv_Refine x t -> \"Tv_Refine\"\n | Tv_Const cst -> \"Tv_Const\"\n | Tv_Uvar i t -> \"Tv_Uvar\"\n | Tv_Let r attrs b t1 t2 -> \"Tv_Let\"\n | Tv_Match t _ branches -> \"Tv_Match\"\n | Tv_AscribedT _ _ _ _ -> \"Tv_AscribedT\"\n | Tv_AscribedC _ _ _ _ -> \"Tv_AscribedC\"\n | Tv_Unknown -> \"Tv_Unknown\"\n | Tv_Unsupp -> \"Tv_Unsupp\"\n\nlet string_of_match_exception = function\n | NameMismatch (qn1, qn2) ->\n \"Match failure (name mismatch): expecting \" ^\n qn1 ^ \", found \" ^ qn2\n | SimpleMismatch (pat, tm) ->\n \"Match failure (sort mismatch): expecting \" ^\n desc_of_pattern pat ^ \", got \" ^ term_to_string tm\n | NonLinearMismatch (nm, t1, t2) ->\n \"Match failure (nonlinear mismatch): variable \" ^ nm ^\n \" needs to match both \" ^ (term_to_string t1) ^\n \" and \" ^ (term_to_string t2)\n | UnsupportedTermInPattern tm ->\n \"Match failure (unsupported term in pattern): \" ^\n term_to_string tm ^ \" (\" ^ term_head tm ^ \")\"\n | IncorrectTypeInAbsPatBinder typ ->\n \"Incorrect type in pattern-matching binder: \" ^\n term_to_string typ ^ \" (use one of ``var``, ``hyp …``, or ``goal …``)\"\n\n/// The exception monad\n/// -------------------\n\nnoeq type match_res a =\n| Success of a\n| Failure of match_exception\n\nlet return #a (x: a) : match_res a =\n Success x\n\nlet (let?) (#a #b: Type)\n (f: match_res a)\n (g: a -> Tac (match_res b))\n : Tac (match_res b) =\n match f with\n | Success aa -> g aa\n | Failure ex -> Failure ex\n\nlet raise #a (ex: match_exception) : match_res a =\n Failure ex\n\n/// Liftings\n/// --------\n///\n/// There's a natural lifting from the exception monad into the tactic effect:\n\nlet lift_exn_tac #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\nlet lift_exn_tactic #a #b (f: a -> match_res b) (aa: a) : Tac b =\n match f aa with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern interpretation\n/// ======================\n///\n/// This section implement pattern-matching. This is strictly a one term, one\n/// pattern implementation — handling cases in which mutliple hypotheses match\n/// the same pattern is done later.\n\ntype bindings = list (varname * term)\nlet string_of_bindings (bindings: bindings) =\n String.concat \"\\n\"\n (map (fun (nm, tm) -> (\">> \" ^ nm ^ \": \" ^ term_to_string tm))\n bindings)\n\n(** Match a pattern against a term.\n`cur_bindings` is a list of bindings collected while matching previous parts of\nthe pattern. Returns a result in the exception monad. **)\nlet rec interp_pattern_aux (pat: pattern) (cur_bindings: bindings) (tm:term)\n : Tac (match_res bindings) =\n let interp_var (v: varname) cur_bindings tm =\n match List.Tot.Base.assoc v cur_bindings with\n | Some tm' -> if term_eq tm tm' then return cur_bindings\n else raise (NonLinearMismatch (v, tm, tm'))\n | None -> return ((v, tm) :: cur_bindings) in\n let interp_qn (qn: qn) cur_bindings tm =\n match inspect tm with\n | Tv_UInst fv _\n | Tv_FVar fv ->\n if fv_to_string fv = qn then return cur_bindings\n else raise (NameMismatch (qn, (fv_to_string fv)))\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_type cur_bindings tm =\n match inspect tm with\n | Tv_Type _ -> return cur_bindings\n | _ -> raise (SimpleMismatch (pat, tm)) in\n let interp_app (p_hd p_arg: (p:pattern{p << pat})) cur_bindings tm =\n match inspect tm with\n | Tv_App hd (arg, _) ->\n let? with_hd = interp_pattern_aux p_hd cur_bindings hd in\n let? with_arg = interp_pattern_aux p_arg with_hd arg in\n return with_arg\n | _ -> raise (SimpleMismatch (pat, tm)) in\n match pat with\n | PVar var -> interp_var var cur_bindings tm\n | PQn qn -> interp_qn qn cur_bindings tm\n | PType -> interp_type cur_bindings tm\n | PApp p_hd p_arg -> interp_app p_hd p_arg cur_bindings tm\n\n(** Match a pattern `pat` against a term.\nReturns a result in the exception monad. **)\nlet interp_pattern (pat: pattern) : term -> Tac (match_res bindings) =\n fun (tm: term) ->\n let? rev_bindings = interp_pattern_aux pat [] tm in\n return (List.Tot.Base.rev rev_bindings)\n\n(** Match a term `tm` against a pattern `pat`.\nRaises an exception if the match fails. This is mostly useful for debugging:\nuse ``mgw`` to capture matches. **)\nlet match_term pat (tm : term) : Tac bindings =\n match interp_pattern pat (norm_term [] tm) with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Pattern-matching problems\n/// =========================\n///\n/// Generalizing past single-term single-pattern problems, we obtain the\n/// following notions of pattern-matching problems and solutions:\n\nlet debug msg : Tac unit = () // print msg\n\n/// Definitions\n/// -----------\n\nlet absvar = binding\ntype hypothesis = binding\n\n/// A matching problem is composed of holes (``mp_vars``), hypothesis patterns\n/// (``mp_hyps``), and a goal pattern (``mp_goal``).\n\nnoeq type matching_problem =\n { mp_vars: list varname;\n mp_hyps: list (varname * pattern);\n mp_goal: option pattern }\n\nlet string_of_matching_problem mp =\n let vars =\n String.concat \", \" mp.mp_vars in\n let hyps =\n String.concat \"\\n \"\n (List.Tot.Base.map (fun (nm, pat) ->\n nm ^ \": \" ^ (string_of_pattern pat)) mp.mp_hyps) in\n let goal = match mp.mp_goal with\n | None -> \"_\"\n | Some pat -> string_of_pattern pat in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \"\\n\" ^\n \" goal: \" ^ goal ^ \" }\"\n\n/// A solution is composed of terms captured to mach the holes, and binders\n/// captured to match hypothesis patterns.\n\nnoeq type matching_solution =\n { ms_vars: list (varname * term);\n ms_hyps: list (varname * hypothesis) }\n\nlet string_of_matching_solution ms =\n let vars =\n String.concat \"\\n \"\n (map (fun (varname, tm) ->\n varname ^ \": \" ^ (term_to_string tm)) ms.ms_vars) in\n let hyps =\n String.concat \"\\n \"\n (map (fun (nm, binding) ->\n nm ^ \": \" ^ (binding_to_string binding)) ms.ms_hyps) in\n \"\\n{ vars: \" ^ vars ^ \"\\n\" ^\n \" hyps: \" ^ hyps ^ \" }\"\n\n(** Find a varname in an association list; fail if it can't be found. **)\nlet assoc_varname_fail (#b: Type) (key: varname) (ls: list (varname * b))\n : Tac b =\n match List.Tot.Base.assoc key ls with\n | None -> fail (\"Not found: \" ^ key)\n | Some x -> x\n\nlet ms_locate_hyp (a: Type) (solution: matching_solution)\n (name: varname) : Tac hypothesis =\n assoc_varname_fail name solution.ms_hyps\n\nlet ms_locate_var (a: Type) (solution: matching_solution)\n (name: varname) : Tac a =\n unquote #a (assoc_varname_fail name solution.ms_vars)\n\nlet ms_locate_unit (a: Type) _solution _binder_name : Tac unit =\n ()\n\n/// Resolution\n/// ----------\n///\n/// Solving a matching problem is a two-steps process: find an initial\n/// assignment for holes based on the goal pattern, then find a set of\n/// hypotheses matching hypothesis patterns.\n///\n/// Note that the implementation takes a continuation of type\n/// ``matching_solution -> Tac a``. This continuation is needed because we want\n/// users to be able to provide extra criteria on matching solutions (most\n/// commonly, this criterion is that a particular tactic should run\n/// successfuly).\n///\n/// This makes it easy to implement a simple for of search through the context,\n/// where one can find a hypothesis matching a particular predicate by\n/// constructing a trivial matching problem and passing the predicate as the\n/// continuation.\n\n(** Scan ``hypotheses`` for a match for ``pat`` that lets ``body`` succeed.\n\n``name`` is used to refer to the hypothesis matched in the final solution.\n``part_sol`` includes bindings gathered while matching previous solutions. **)\nlet rec solve_mp_for_single_hyp #a\n (name: varname)\n (pat: pattern)\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (part_sol: matching_solution)\n : Tac a =\n match hypotheses with\n | [] ->\n fail #a \"No matching hypothesis\"\n | h :: hs ->\n or_else // Must be in ``Tac`` here to run `body`\n (fun () ->\n match interp_pattern_aux pat part_sol.ms_vars (type_of_binding h) with\n | Failure ex ->\n fail (\"Failed to match hyp: \" ^ (string_of_match_exception ex))\n | Success bindings ->\n let ms_hyps = (name, h) :: part_sol.ms_hyps in\n body ({ part_sol with ms_vars = bindings; ms_hyps = ms_hyps }))\n (fun () ->\n solve_mp_for_single_hyp name pat hs body part_sol)\n\n(** Scan ``hypotheses`` for matches for ``mp_hyps`` that lets ``body``\nsucceed. **)\nlet rec solve_mp_for_hyps #a\n (mp_hyps: list (varname * pattern))\n (hypotheses: list hypothesis)\n (body: matching_solution -> Tac a)\n (partial_solution: matching_solution)\n : Tac a =\n match mp_hyps with\n | [] -> body partial_solution\n | (name, pat) :: pats ->\n solve_mp_for_single_hyp name pat hypotheses\n (solve_mp_for_hyps pats hypotheses body)\n partial_solution\n\n(** Solve a matching problem.\n\nThe solution returned is constructed to ensure that the continuation ``body``\nsucceeds: this implements the usual backtracking-match semantics. **)\nlet solve_mp #a (problem: matching_problem)\n (hypotheses: list hypothesis) (goal: term)\n (body: matching_solution -> Tac a)\n : Tac a =\n let goal_ps =\n match problem.mp_goal with\n | None -> { ms_vars = []; ms_hyps = [] }\n | Some pat ->\n match interp_pattern pat goal with\n | Failure ex -> fail (\"Failed to match goal: \" ^ (string_of_match_exception ex))\n | Success bindings -> { ms_vars = bindings; ms_hyps = [] } in\n solve_mp_for_hyps #a problem.mp_hyps hypotheses body goal_ps\n\n/// A DSL for pattern-matching\n/// ==========================\n///\n/// Using pattern-matching problems as defined above is relatively cumbersome,\n/// so we now introduce a lightweight notation, in two steps: pattern notations,\n/// and matching-problem notations.\n///\n/// Pattern notations\n/// -----------------\n///\n/// The first part of our pattern-matching syntax is pattern notations: we\n/// provide a reflective function which constructs a pattern from a term:\n/// variables are holes, free variables are constants, and applications are\n/// application patterns.\n\n(* FIXME: MOVE *)\nlet name_of_namedv (x:namedv) : Tac string =\n unseal (inspect_namedv x).ppname\n\n(** Compile a term `tm` into a pattern. **)\nlet rec pattern_of_term_ex tm : Tac (match_res pattern) =\n match inspect tm with\n | Tv_Var bv ->\n return (PVar (name_of_namedv bv))\n | Tv_FVar fv\n | Tv_UInst fv _ ->\n let qn = fv_to_string fv in\n return (PQn qn)\n | Tv_Type _ ->\n return PType\n | Tv_App f (x, _) ->\n let? fpat = pattern_of_term_ex f in\n let? xpat = pattern_of_term_ex x in\n return (PApp fpat xpat)\n | _ -> raise (UnsupportedTermInPattern tm)\n\n(** β-reduce a term `tm`.\nThis is useful to remove needles function applications introduced by F*, like\n``(fun a b c -> a) 1 2 3``. **)\nlet beta_reduce (tm: term) : Tac term =\n norm_term [] tm\n\n(** Compile a term `tm` into a pattern. **)\nlet pattern_of_term tm : Tac pattern =\n match pattern_of_term_ex tm with\n | Success bb -> bb\n | Failure ex -> Tactics.fail (string_of_match_exception ex)\n\n/// Problem notations\n/// -----------------\n///\n/// We then introduce a DSL for matching problems, best explained on the\n/// following example::\n///\n/// (fun (a b c: ①) (h1 h2 h3: hyp ②) (g: pm_goal ③) → ④)\n///\n/// This notation is intended to express a pattern-matching problems with three\n/// holes ``a``, ``b``, and ``c`` of type ①, matching hypotheses ``h1``, ``h2``,\n/// and ``h3`` against pattern ② and the goal against the pattern ③. The body\n/// of the notation (④) is then run with appropriate terms bound to ``a``,\n/// ``b``, and ``c``, appropriate binders bound to ``h1``, ``h2``, and ``h3``,\n/// and ``()`` bound to ``g``.\n///\n/// We call these patterns ``abspat``s (abstraction patterns), and we provide\n/// facilities to parse them into matching problems, and to run their bodies\n/// against a particular matching solution.\n\n// We used to annotate variables with an explicit 'var' marker, but then that\n// var annotation leaked into the types of other hypotheses due to type\n// inference, requiring non-trivial normalization.\n\n// let var (a: Type) = a\nlet hyp (a: Type) = binding\nlet pm_goal (a: Type) = unit\n\nlet hyp_qn = `%hyp\nlet goal_qn = `%pm_goal\n\nnoeq type abspat_binder_kind =\n| ABKVar of typ\n| ABKHyp\n| ABKGoal\n\nlet string_of_abspat_binder_kind = function\n | ABKVar _ -> \"varname\"\n | ABKHyp -> \"hyp\"\n | ABKGoal -> \"goal\"\n\nnoeq type abspat_argspec =\n { asa_name: absvar;\n asa_kind: abspat_binder_kind }\n\n// We must store this continuation, because recomputing it yields different\n// names when the binders are re-opened.\ntype abspat_continuation =\n list abspat_argspec * term\n\nlet type_of_named_binder (nb : binder) : term =\n nb.sort\n\nlet classify_abspat_binder (b : binder): Tac (abspat_binder_kind * term) =\n let varname = \"v\" in\n let hyp_pat = PApp (PQn hyp_qn) (PVar varname) in\n let goal_pat = PApp (PQn goal_qn) (PVar varname) in\n\n let typ = type_of_named_binder b in\n match interp_pattern hyp_pat typ with\n | Success [(_, hyp_typ)] -> ABKHyp, hyp_typ\n | Success _ -> fail \"classifiy_abspat_binder: impossible (1)\"\n | Failure _ ->\n match interp_pattern goal_pat typ with\n | Success [(_, goal_typ)] -> ABKGoal, goal_typ\n | Success _ -> fail \"classifiy_abspat_binder: impossible (2)\"\n | Failure _ -> ABKVar typ, typ\n\n(** Split an abstraction `tm` into a list of binders and a body. **)\nlet rec binders_and_body_of_abs tm : Tac (list binder * term) =\n match inspect tm with\n | Tv_Abs binder tm ->\n let binders, body = binders_and_body_of_abs tm in\n binder :: binders, body\n | _ -> [], tm\n\nlet cleanup_abspat (t: term) : Tac term =\n norm_term [] t\n\n\nlet name_of_named_binder (nb : binder) : Tac string =\n unseal nb.ppname\n\n(** Parse a notation into a matching problem and a continuation.\n\nPattern-matching notations are of the form ``(fun binders… -> continuation)``,\nwhere ``binders`` are of one of the forms ``var …``, ``hyp …``, or ``goal …``.\n``var`` binders are typed holes to be used in other binders; ``hyp`` binders\nindicate a pattern to be matched against hypotheses; and ``goal`` binders match\nthe goal.\n\n\nA reduction phase is run to ensure that the pattern looks reasonable; it is\nneeded because F* tends to infer arguments in β-expanded form.\n\nThe continuation returned can't directly be applied to a pattern-matching\n\nPartial implementation:\nlet matching_problem_of_abs (tm: term) : Tac (matching_problem * abspat_continuation) =\n \n"
},
{
"role": "assistant",
"content": "let binders, body = binders_and_body_of_abs (cleanup_abspat tm) in\ndebug (\"Got binders: \" ^\n (String.concat \", \" (map (fun b -> name_of_named_binder b <: Tac string) binders)));\nlet classified_binders:list (binder & string & abspat_binder_kind & typ) =\n map (fun binder ->\n let bv_name = name_of_named_binder binder in\n debug (\"Got binder: \" ^\n bv_name ^ \"; type is \" ^ term_to_string (type_of_named_binder binder));\n let binder_kind, typ = classify_abspat_binder binder in\n (binder, bv_name, binder_kind, typ))\n binders\nin\nlet problem =\n fold_left (fun problem (binder, bv_name, binder_kind, typ) ->\n debug (\"Compiling binder \" ^\n name_of_named_binder binder ^\n \", classified as \" ^\n string_of_abspat_binder_kind binder_kind ^ \", with type \" ^ term_to_string typ);\n match binder_kind with\n | ABKVar _ -> { problem with mp_vars = bv_name :: problem.mp_vars }\n | ABKHyp -> { problem with mp_hyps = (bv_name, (pattern_of_term typ)) :: problem.mp_hyps }\n | ABKGoal -> { problem with mp_goal = Some (pattern_of_term typ) })\n ({ mp_vars = []; mp_hyps = []; mp_goal = None })\n classified_binders\nin\nlet continuation =\n let abspat_argspec_of_binder xx : Tac abspat_argspec =\n match xx with\n | binder, xx, binder_kind, yy -> { asa_name = binder_to_binding binder; asa_kind = binder_kind }\n in\n (map abspat_argspec_of_binder classified_binders, tm)\nin\nlet mp =\n {\n mp_vars = List.Tot.Base.rev #varname problem.mp_vars;\n mp_hyps = List.Tot.Base.rev #(varname * pattern) problem.mp_hyps;\n mp_goal = problem.mp_goal\n }\nin\ndebug (\"Got matching problem: \" ^ (string_of_matching_problem mp));\nmp, continuation"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: p: MerkleTree.Spec.path n -> idx: Prims.nat{idx < Prims.pow2 n} -> h: MerkleTree.Spec.padded_hash\n -> Prims.GTot MerkleTree.Spec.padded_hash\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n\nPartial implementation:\nlet rec mt_verify_ #hsz #f #n p idx h =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen h\nelse\n mt_verify_ #_\n #f\n #(n - 1)\n (S.tail p)\n (idx / 2)\n (if idx % 2 = 0 then padded_hash_fun #_ f h (S.head p) else padded_hash_fun #_ f (S.head p) h)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes -> i: Prims.nat{i < FStar.Seq.Base.length hs}\n -> FStar.Pervasives.Lemma (requires MerkleTree.Spec.raw_hashes hs)\n (ensures HRaw? hs.[ i ])\n (decreases i)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n\nPartial implementation:\nlet rec raw_hashes_index #hsz #f hs i =\n \n"
},
{
"role": "assistant",
"content": "if i = 0 then () else raw_hashes_index #_ #f (S.tail hs) (i - 1)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i\n -> FStar.Pervasives.Lemma (ensures i > 0 <==> HRaw? (MerkleTree.Spec.mt_get_root mt))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #_ #f (n-1) (if i <= pow2 (n-1) then 0 else i - pow2 (n-1))\nlet rpmt_right #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n-1) - i) (pow2 n - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n-1)) i;\n mt_right mt\n\n/// Two right-padded Merkle trees collide when\n/// 1) they have the same height (`n`) and number of raw hashes (`i`),\n/// 2) their contents differ, and\n/// 3) their roots are same.\n\n// fournet: we may want to work towards removing 1) using a hash prefix\nnoeq\ntype mt_collide (#hsz:pos) (#f:hash_fun_t #hsz) (n:nat) (i:nat{i <= pow2 n}) = | Collision:\n mt1:rpmt #_ #f n i -> mt2:rpmt #_ #f n i {\n mt1 =!= mt2 /\\\n mt_get_root #_ #f #_ mt1 == mt_get_root #_ #f #_ mt2 } -> mt_collide #_ #f n i\n\nnoeq\ntype hash2_raw_collide = | Collision2:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n lh1:hash -> rh1:hash ->\n lh2:hash -> rh2:hash {\n (lh1 =!= lh2 \\/ rh1 =!= rh2) /\\\n f lh1 rh1 == f lh2 rh2 } -> hash2_raw_collide\n\n/// Auxiliary lemmas for the proof\n\nval rpmt_pad_hashes_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> pad_hashes #_ #f mt )\nlet rpmt_pad_hashes_0 #_ #_ #n #i mt = ()\n\nval rpmt_pad_hashes_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} ->\n mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? mt.[0])\nlet rpmt_pad_hashes_index_0 #_ #_ #n #i mt = ()\n\nval mt_get_root_pad_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n ->\n Lemma (HPad? mt.[0] <==> HPad? (mt_get_root #_ #f mt))\nlet rec mt_get_root_pad_index_0 #hsz #f #n (mt:merkle_tree #hsz n) =\n if n = 0 then ()\n else\n let mt:merkle_tree #hsz (n-1) = mt_next_lv #_ #f #n mt in\n mt_get_root_pad_index_0 #_ #f #(n-1) mt\n\n#pop-options\n\nval rpmt_get_root_pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? (mt_get_root #_ #f mt))\nlet rpmt_get_root_pad_hashes #_ #f #n #i mt =\n rpmt_pad_hashes_index_0 #_ #f mt;\n mt_get_root_pad_index_0 #_ #f mt\n\nval rpmt_get_root_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> HPad? (mt_get_root #_ #f mt))\nlet rpmt_get_root_pad #_ #f #n #i mt =\n rpmt_get_root_pad_hashes #_ #f mt;\n rpmt_pad_hashes_0 #_ #f mt\n\nval rpmt_get_root_raw:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n\nPartial implementation:\nlet rpmt_get_root_raw #hsz #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "allow_inversion (padded_hash #hsz);\nrpmt_get_root_pad #_ #f mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i\n -> FStar.Pervasives.Lemma (ensures i = 0 <==> HPad? (MerkleTree.Spec.mt_get_root mt))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #_ #f (n-1) (if i <= pow2 (n-1) then 0 else i - pow2 (n-1))\nlet rpmt_right #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n-1) - i) (pow2 n - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n-1)) i;\n mt_right mt\n\n/// Two right-padded Merkle trees collide when\n/// 1) they have the same height (`n`) and number of raw hashes (`i`),\n/// 2) their contents differ, and\n/// 3) their roots are same.\n\n// fournet: we may want to work towards removing 1) using a hash prefix\nnoeq\ntype mt_collide (#hsz:pos) (#f:hash_fun_t #hsz) (n:nat) (i:nat{i <= pow2 n}) = | Collision:\n mt1:rpmt #_ #f n i -> mt2:rpmt #_ #f n i {\n mt1 =!= mt2 /\\\n mt_get_root #_ #f #_ mt1 == mt_get_root #_ #f #_ mt2 } -> mt_collide #_ #f n i\n\nnoeq\ntype hash2_raw_collide = | Collision2:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n lh1:hash -> rh1:hash ->\n lh2:hash -> rh2:hash {\n (lh1 =!= lh2 \\/ rh1 =!= rh2) /\\\n f lh1 rh1 == f lh2 rh2 } -> hash2_raw_collide\n\n/// Auxiliary lemmas for the proof\n\nval rpmt_pad_hashes_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> pad_hashes #_ #f mt )\nlet rpmt_pad_hashes_0 #_ #_ #n #i mt = ()\n\nval rpmt_pad_hashes_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} ->\n mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? mt.[0])\nlet rpmt_pad_hashes_index_0 #_ #_ #n #i mt = ()\n\nval mt_get_root_pad_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n ->\n Lemma (HPad? mt.[0] <==> HPad? (mt_get_root #_ #f mt))\nlet rec mt_get_root_pad_index_0 #hsz #f #n (mt:merkle_tree #hsz n) =\n if n = 0 then ()\n else\n let mt:merkle_tree #hsz (n-1) = mt_next_lv #_ #f #n mt in\n mt_get_root_pad_index_0 #_ #f #(n-1) mt\n\n#pop-options\n\nval rpmt_get_root_pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? (mt_get_root #_ #f mt))\nlet rpmt_get_root_pad_hashes #_ #f #n #i mt =\n rpmt_pad_hashes_index_0 #_ #f mt;\n mt_get_root_pad_index_0 #_ #f mt\n\nval rpmt_get_root_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n\nPartial implementation:\nlet rpmt_get_root_pad #_ #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "rpmt_get_root_pad_hashes #_ #f mt;\nrpmt_pad_hashes_0 #_ #f mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i -> FStar.Seq.Base.seq MerkleTree.Spec.hash\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nPartial implementation:\nlet rpmt_raws #hsz #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "raw_hashes_raws #_ #f (S.slice mt 0 i)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n j: Prims.nat ->\n n: Prims.pos{j <= 2 * n} ->\n hs1: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs1 = 2 * n} ->\n hs2: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs2 = 2 * n}\n -> FStar.Pervasives.Lemma\n (requires FStar.Seq.Base.equal (FStar.Seq.Base.slice hs1 0 j) (FStar.Seq.Base.slice hs2 0 j))\n (ensures\n FStar.Seq.Base.equal (FStar.Seq.Base.slice (MerkleTree.Spec.hs_next_lv hs1) 0 (j / 2))\n (FStar.Seq.Base.slice (MerkleTree.Spec.hs_next_lv hs2) 0 (j / 2)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n\nPartial implementation:\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n \n"
},
{
"role": "assistant",
"content": "forall_intro (hs_next_lv_index #_ #f #n hs1);\nforall_intro (hs_next_lv_index #_ #f #n hs2);\nlet hs1' = hs_next_lv #_ #f #n hs1 in\nlet hs2' = hs_next_lv #_ #f #n hs2 in\nassert (forall (i: nat{i < j / 2}).\n hs1'.[ i ] == padded_hash_fun #hsz f hs1.[ 2 * i ] hs1.[ 2 * i + 1 ]);\nassert (forall (i: nat{i < j / 2}).\n hs2'.[ i ] == padded_hash_fun #hsz f hs2.[ 2 * i ] hs2.[ 2 * i + 1 ]);\nassert (forall (i: nat{i < j}). (S.slice hs1 0 j).[ i ] == (S.slice hs2 0 j).[ i ]);\nassert (forall (i: nat{i < j}). hs1.[ i ] == hs2.[ i ]);\nassert (forall (i: nat{i < j / 2}). hs1.[ 2 * i ] == hs2.[ 2 * i ]);\nassert (forall (i: nat{i < j / 2}). hs1.[ 2 * i + 1 ] == hs2.[ 2 * i + 1 ]);\nassert (forall (i: nat{i < j / 2}). hs1'.[ i ] == hs2'.[ i ])"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> i: Prims.nat{i < Prims.pow2 n}\n -> Prims.GTot (MerkleTree.Spec.path n)\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n\nPartial implementation:\nlet rec mt_get_path #hsz #f #n t i =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen S.empty\nelse\n S.cons (if i % 2 = 0 then t.[ i + 1 ] else t.[ i - 1 ])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes -> i: Prims.nat -> j: Prims.nat{i <= j && j <= FStar.Seq.Base.length hs}\n -> FStar.Pervasives.Lemma (requires MerkleTree.Spec.pad_hashes hs)\n (ensures MerkleTree.Spec.pad_hashes (FStar.Seq.Base.slice hs i j))\n (decreases j - i)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n\nPartial implementation:\nlet rec pad_hashes_slice #hsz #f hs i j =\n \n"
},
{
"role": "assistant",
"content": "if i = j then () else pad_hashes_slice #_ #f hs (i + 1) j"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n\n -> FStar.Pervasives.Lemma\n (ensures\n MerkleTree.Spec.mt_get_root mt ==\n MerkleTree.Spec.padded_hash_fun f\n (MerkleTree.Spec.mt_get_root (MerkleTree.Spec.mt_left mt))\n (MerkleTree.Spec.mt_get_root (MerkleTree.Spec.mt_right mt)))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n\nPartial implementation:\nlet rec mt_get_root_step #hsz #f #n mt =\n \n"
},
{
"role": "assistant",
"content": "if n = 1\nthen ()\nelse\n (mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i\n -> MerkleTree.Spec.rpmt (n - 1)\n (match i <= Prims.pow2 (n - 1) with\n | true -> i\n | _ -> Prims.pow2 (n - 1))\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n\nPartial implementation:\nlet rpmt_left #hsz #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "if i <= pow2 (n - 1)\nthen pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n - 1) - i)\nelse raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n - 1));\nmt_left mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n -> idx: Prims.nat{idx < Prims.pow2 n}\n -> FStar.Pervasives.Lemma\n (ensures\n (MerkleTree.Spec.mt_next_lv mt).[ idx / 2 ] ==\n (match idx % 2 = 0 with\n | true -> MerkleTree.Spec.padded_hash_fun f mt.[ idx ] mt.[ idx + 1 ]\n | _ -> MerkleTree.Spec.padded_hash_fun f mt.[ idx - 1 ] mt.[ idx ]))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n\nPartial implementation:\nlet mt_next_lv_get #hsz #f #n mt idx =\n \n"
},
{
"role": "assistant",
"content": "hs_next_lv_get #_ #f #(pow2 (n - 1)) mt idx"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n} ->\n i: Prims.nat ->\n j: Prims.nat{i <= j && j <= n}\n -> FStar.Pervasives.Lemma\n (ensures\n FStar.Seq.Base.equal (MerkleTree.Spec.hs_next_lv (FStar.Seq.Base.slice hs (2 * i) (2 * j)))\n (FStar.Seq.Base.slice (MerkleTree.Spec.hs_next_lv hs) i j)) (decreases j - i)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n\nPartial implementation:\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n \n"
},
{
"role": "assistant",
"content": "if i = j\nthen ()\nelse\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[ 0 ] x.[ 1 ])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: t: MerkleTree.Spec.merkle_tree n -> i: Prims.nat{i < Prims.pow2 n}\n -> FStar.Pervasives.Lemma\n (ensures\n MerkleTree.Spec.mt_verify_ (MerkleTree.Spec.mt_get_path t i) i (MerkleTree.Spec.mt_get t i) ==\n MerkleTree.Spec.mt_get_root t)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n\nPartial implementation:\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen ()\nelse\n (assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[ idx + 1 ] else mt.[ idx - 1 ]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n n: Prims.nat ->\n hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n} ->\n nhs: MerkleTree.Spec.hashes{FStar.Seq.Base.length nhs = n}\n -> FStar.Pervasives.Lemma (requires MerkleTree.Spec.hs_next_rel n hs nhs)\n (ensures FStar.Seq.Base.equal nhs (MerkleTree.Spec.hs_next_lv hs))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n\nPartial implementation:\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen ()\nelse hs_next_rel_next_lv #_ #f (n - 1) (S.slice hs 2 (S.length hs)) (S.slice nhs 1 (S.length nhs))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.rpmt n i\n -> MerkleTree.Spec.rpmt (n - 1)\n (match i <= Prims.pow2 (n - 1) with\n | true -> 0\n | _ -> i - Prims.pow2 (n - 1))\nEffect: Prims.Tot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n\nPartial implementation:\nlet rpmt_right #hsz #f #n #i mt =\n \n"
},
{
"role": "assistant",
"content": "if i <= pow2 (n - 1)\nthen pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n - 1) - i) (pow2 n - i)\nelse raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n - 1)) i;\nmt_right mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: mt: MerkleTree.Spec.merkle_tree n\n -> FStar.Pervasives.Lemma (ensures HPad? mt.[ 0 ] <==> HPad? (MerkleTree.Spec.mt_get_root mt))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #_ #f (n-1) (if i <= pow2 (n-1) then 0 else i - pow2 (n-1))\nlet rpmt_right #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n-1) - i) (pow2 n - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n-1)) i;\n mt_right mt\n\n/// Two right-padded Merkle trees collide when\n/// 1) they have the same height (`n`) and number of raw hashes (`i`),\n/// 2) their contents differ, and\n/// 3) their roots are same.\n\n// fournet: we may want to work towards removing 1) using a hash prefix\nnoeq\ntype mt_collide (#hsz:pos) (#f:hash_fun_t #hsz) (n:nat) (i:nat{i <= pow2 n}) = | Collision:\n mt1:rpmt #_ #f n i -> mt2:rpmt #_ #f n i {\n mt1 =!= mt2 /\\\n mt_get_root #_ #f #_ mt1 == mt_get_root #_ #f #_ mt2 } -> mt_collide #_ #f n i\n\nnoeq\ntype hash2_raw_collide = | Collision2:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n lh1:hash -> rh1:hash ->\n lh2:hash -> rh2:hash {\n (lh1 =!= lh2 \\/ rh1 =!= rh2) /\\\n f lh1 rh1 == f lh2 rh2 } -> hash2_raw_collide\n\n/// Auxiliary lemmas for the proof\n\nval rpmt_pad_hashes_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> pad_hashes #_ #f mt )\nlet rpmt_pad_hashes_0 #_ #_ #n #i mt = ()\n\nval rpmt_pad_hashes_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} ->\n mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? mt.[0])\nlet rpmt_pad_hashes_index_0 #_ #_ #n #i mt = ()\n\nval mt_get_root_pad_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n ->\n\nPartial implementation:\nlet rec mt_get_root_pad_index_0 #hsz #f #n (mt: merkle_tree #hsz n) =\n \n"
},
{
"role": "assistant",
"content": "if n = 0\nthen ()\nelse\n let mt:merkle_tree #hsz (n - 1) = mt_next_lv #_ #f #n mt in\n mt_get_root_pad_index_0 #_ #f #(n - 1) mt"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: r: Vale.X64.Machine_s.reg_64 -> FStar.Pervasives.Native.option (Vale.Interop.X64.reg_nat 3)\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nPartial implementation:\nlet of_reg (r: MS.reg_64) : option (IX64.reg_nat 3) =\n \n"
},
{
"role": "assistant",
"content": "match r with\n| 5 -> Some 0\n| 4 -> Some 1\n| 3 -> Some 2\n| _ -> None"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Vale.Interop.Base.td\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n\nPartial implementation:\nlet t64_no_mod =\n \n"
},
{
"role": "assistant",
"content": "TD_Buffer TUInt64 TUInt64 ({ modified = false; strict_disjointness = false; taint = MS.Secret })"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hs: MerkleTree.Spec.hashes{FStar.Seq.Base.length hs = 2 * n} -> idx: Prims.nat{idx < 2 * n}\n -> FStar.Pervasives.Lemma\n (ensures\n (MerkleTree.Spec.hs_next_lv hs).[ idx / 2 ] ==\n (match idx % 2 = 0 with\n | true -> MerkleTree.Spec.padded_hash_fun f hs.[ idx ] hs.[ idx + 1 ]\n | _ -> MerkleTree.Spec.padded_hash_fun f hs.[ idx - 1 ] hs.[ idx ]))\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n\nPartial implementation:\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n \n"
},
{
"role": "assistant",
"content": "if idx < 2 then () else hs_next_lv_get #_ #f #(n - 1) (S.slice hs 2 (S.length hs)) (idx - 2)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Vale.Interop.Base.td\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n\nPartial implementation:\nlet t64_mod =\n \n"
},
{
"role": "assistant",
"content": "TD_Buffer TUInt64 TUInt64 default_bq"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Prims.eqtype\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nPartial implementation:\nlet uint64 =\n \n"
},
{
"role": "assistant",
"content": "UInt64.t"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Vale.Interop.Base.td\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n\nPartial implementation:\nlet tuint64 =\n \n"
},
{
"role": "assistant",
"content": "TD_Base TUInt64"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: _ -> Prims.bool\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nPartial implementation:\nlet cswap_xmms_modified =\n \n"
},
{
"role": "assistant",
"content": "fun _ -> false"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\nPartial implementation:\nlet b64 =\n \n"
},
{
"role": "assistant",
"content": "buf_t TUInt64 TUInt64"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Vale.X64.Decls.va_code\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nPartial implementation:\nlet code_cswap =\n \n"
},
{
"role": "assistant",
"content": "FU.va_code_Cswap2 ()"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Prims.list Prims.string\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nlet of_reg (r:MS.reg_64) : option (IX64.reg_nat 3) = match r with\n | 5 -> Some 0 // rdi\n | 4 -> Some 1 // rsi\n | 3 -> Some 2 // rdx\n | _ -> None\n\nlet of_arg (i:IX64.reg_nat 3) : MS.reg_64 = match i with\n | 0 -> MS.rRdi\n | 1 -> MS.rRsi\n | 2 -> MS.rRdx\n\nlet arg_reg : IX64.arg_reg_relation 3 = IX64.Rel of_reg of_arg\n\n(* Here's the type expected for the cswap wrapper *)\n[@__reduce__]\nlet lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.as_lowstar_sig_t_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n []\n _\n _\n // The boolean here doesn't matter\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\n(* And here's the cswap wrapper itself *)\nlet lowstar_cswap : lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.wrap_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\nlet lowstar_cswap_normal_t : normal lowstar_cswap_t\n = as_normal_t #lowstar_cswap_t lowstar_cswap\n\nopen Vale.AsLowStar.MemoryHelpers\n\nlet cswap2 bit p0 p1\n = DV.length_eq (get_downview p0);\n DV.length_eq (get_downview p1);\n let (x, _) = lowstar_cswap_normal_t bit p0 p1 () in\n ()\n\nPartial implementation:\nlet cswap_comments:list string =\n \n"
},
{
"role": "assistant",
"content": "[\"Computes p1 <- bit ? p2 : p1 in constant time\"]"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: n: Prims.nat -> Prims.string\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nlet of_reg (r:MS.reg_64) : option (IX64.reg_nat 3) = match r with\n | 5 -> Some 0 // rdi\n | 4 -> Some 1 // rsi\n | 3 -> Some 2 // rdx\n | _ -> None\n\nlet of_arg (i:IX64.reg_nat 3) : MS.reg_64 = match i with\n | 0 -> MS.rRdi\n | 1 -> MS.rRsi\n | 2 -> MS.rRdx\n\nlet arg_reg : IX64.arg_reg_relation 3 = IX64.Rel of_reg of_arg\n\n(* Here's the type expected for the cswap wrapper *)\n[@__reduce__]\nlet lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.as_lowstar_sig_t_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n []\n _\n _\n // The boolean here doesn't matter\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\n(* And here's the cswap wrapper itself *)\nlet lowstar_cswap : lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.wrap_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\nlet lowstar_cswap_normal_t : normal lowstar_cswap_t\n = as_normal_t #lowstar_cswap_t lowstar_cswap\n\nopen Vale.AsLowStar.MemoryHelpers\n\nlet cswap2 bit p0 p1\n = DV.length_eq (get_downview p0);\n DV.length_eq (get_downview p1);\n let (x, _) = lowstar_cswap_normal_t bit p0 p1 () in\n ()\n\nlet cswap_comments : list string = \n [\"Computes p1 <- bit ? p2 : p1 in constant time\"]\n\nPartial implementation:\nlet cswap_names (n: nat) : string =\n \n"
},
{
"role": "assistant",
"content": "match n with\n| 0 -> \"bit\"\n| 1 -> \"p1\"\n| 2 -> \"p2\"\n| _ -> \"\""
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: x: Prims.nat -> y: Prims.nat\n -> FStar.Pervasives.Lemma\n (ensures\n (x - y * Vale.Curve25519.Fast_defs.pow2_256) % Vale.Curve25519.Fast_defs.prime ==\n (x - y * 38) % Vale.Curve25519.Fast_defs.prime)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_fast_mul1 (a:nat)\n (b a0 a1 a2 a3\n ba0_hi ba0_lo\n ba1_hi ba1_lo\n ba2_hi ba2_lo\n ba3_hi ba3_lo\n s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n\n pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0)\n )\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b)\n =\n assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b*a0) (b*a1) (b*a2) (b*a3)) int_canon;\n //lemma_prod_bounds ba0_hi ba0_lo b a0;\n //lemma_prod_bounds ba1_hi ba1_lo b a1;\n //lemma_prod_bounds ba2_hi ba2_lo b a2;\n //lemma_prod_bounds ba3_hi ba3_lo b a3;\n ()\n\n\nlet lemma_addition (a d:nat) (a0 a1 a2 a3 d0 d1 d2 d3 d4:nat64)\n (s0 s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n d = pow2_five d0 d1 d2 d3 d4 /\\\n (let s0', c0 = add_carry a0 d0 0 in\n let s1', c1 = add_carry a1 d1 c0 in\n let s2', c2 = add_carry a2 d2 c1 in\n let s3', c3 = add_carry a3 d3 c2 in\n let s4', c4 = add_carry d4 0 c3 in\n s0 == s0' /\\\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0))\n (ensures a + d == pow2_five s0 s1 s2 s3 s4)\n =\n ()\n\nlet lemma_carry_wide (a0 a1 a2 a3 a4 a5 a6 a7\n d0 d1 d2 d3 carry\n d0' d1' d2' d3':nat64) : Lemma\n (requires pow2_five d0 d1 d2 d3 carry == 38 * pow2_four a4 a5 a6 a7 + pow2_four a0 a1 a2 a3 /\\\n pow2_four d0' d1' d2' d3' % prime == ((pow2_four d0 d1 d2 d3) + carry * pow2_256) % prime)\n (ensures (pow2_four d0' d1' d2' d3') % prime == (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime)\n =\n calc (==) {\n pow2_four d0' d1' d2' d3' % prime;\n == { calc (==) {\n (pow2_four d0 d1 d2 d3) + carry * pow2_256;\n == { _ by (int_canon()) }\n pow2_five d0 d1 d2 d3 carry;\n }\n }\n pow2_five d0 d1 d2 d3 carry % prime;\n == {}\n (pow2_four a0 a1 a2 a3 + 38 * pow2_four a4 a5 a6 a7) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) (pow2_four a4 a5 a6 a7) }\n (pow2_four a0 a1 a2 a3 + pow2_256 * pow2_four a4 a5 a6 a7) % prime;\n == { _ by (int_canon()) }\n (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime;\n }\n\n\nlet pow2int_four (c0 c1 c2 c3:int) : int = c0 + c1 * pow2_64 + c2 * pow2_128 + c3 * pow2_192\n\nPartial implementation:\nlet lemma_mul_pow256_sub (x y: nat) : Lemma ((x - y * pow2_256) % prime == (x - y * 38) % prime) =\n \n"
},
{
"role": "assistant",
"content": "assert_norm (pow2_256 % prime == 38);\nFStar.Math.Lemmas.lemma_mod_mul_distr_r (- y) pow2_256 prime;\nFStar.Math.Lemmas.lemma_mod_mul_distr_r (- y) 38 prime;\nassert ((- y * pow2_256) % prime == (- y * 38) % prime);\nFStar.Math.Lemmas.modulo_add prime x (- (y * pow2_256)) (- (y * 38))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: FStar.Pervasives.norm [\n FStar.Pervasives.iota;\n FStar.Pervasives.zeta;\n FStar.Pervasives.delta_attr [\"Vale.Arch.HeapTypes_s.__reduce__\"; \"FStar.BigOps.__reduce__\"];\n FStar.Pervasives.delta_only [\n \"Vale.Interop.Base.uu___is_TD_Buffer\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint\";\n \"Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace\";\n \"FStar.FunctionalExtensionality.on_dom\"; \"FStar.FunctionalExtensionality.on\";\n \"FStar.List.Tot.Base.fold_right_gtot\"; \"FStar.List.Tot.Base.map_gtot\";\n \"FStar.List.Tot.Base.length\"; \"FStar.Pervasives.Native.fst\"; \"FStar.Pervasives.Native.snd\";\n \"FStar.Pervasives.Native.__proj__Mktuple2__item___1\";\n \"FStar.Pervasives.Native.__proj__Mktuple2__item___2\"\n ];\n FStar.Pervasives.primops;\n FStar.Pervasives.simplify\n ]\n Vale.Inline.X64.Fswap_inline.lowstar_cswap_t\n<:\nType0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nlet of_reg (r:MS.reg_64) : option (IX64.reg_nat 3) = match r with\n | 5 -> Some 0 // rdi\n | 4 -> Some 1 // rsi\n | 3 -> Some 2 // rdx\n | _ -> None\n\nlet of_arg (i:IX64.reg_nat 3) : MS.reg_64 = match i with\n | 0 -> MS.rRdi\n | 1 -> MS.rRsi\n | 2 -> MS.rRdx\n\nlet arg_reg : IX64.arg_reg_relation 3 = IX64.Rel of_reg of_arg\n\n(* Here's the type expected for the cswap wrapper *)\n[@__reduce__]\nlet lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.as_lowstar_sig_t_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n []\n _\n _\n // The boolean here doesn't matter\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\n(* And here's the cswap wrapper itself *)\nlet lowstar_cswap : lowstar_cswap_t =\n assert_norm (List.length cswap_dom + List.length ([]<:list arg) <= 3);\n IX64.wrap_weak\n 3\n arg_reg\n cswap_regs_modified\n cswap_xmms_modified\n code_cswap\n cswap_dom\n (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))\n\nPartial implementation:\nlet lowstar_cswap_normal_t:normal lowstar_cswap_t =\n \n"
},
{
"role": "assistant",
"content": "as_normal_t #lowstar_cswap_t lowstar_cswap"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: r: Steel.GhostPCMReference.ref a pcm -> v: a -> Steel.Effect.Common.vprop\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2020 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\nmodule Steel.GhostPCMReference\n(* A ghost variant of Steel.PCMReference *)\nopen FStar.PCM\nopen FStar.Ghost\n\nopen Steel.Memory\nopen Steel.Effect.Atomic\nopen Steel.Effect\nmodule Mem = Steel.Memory\nmodule P = Steel.PCMReference\n\nlet ref (a:Type) (p:pcm a) = erased (Steel.Memory.ref a p)\n\n/// Its selector is non-informative (it is unit)\n\nPartial implementation:\nlet pts_to (#a: Type u#1) (#pcm: pcm a) (r: ref a pcm) ([@@@ smt_fallback]v: a) =\n \n"
},
{
"role": "assistant",
"content": "to_vprop (Steel.Memory.pts_to r v)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n a0: Vale.Def.Types_s.nat64 ->\n a1: Vale.Def.Types_s.nat64 ->\n a2: Vale.Def.Types_s.nat64 ->\n a3: Vale.Def.Types_s.nat64 ->\n b: Vale.Def.Types_s.nat64 ->\n d0: Vale.Def.Types_s.nat64 ->\n d1: Vale.Def.Types_s.nat64 ->\n d2: Vale.Def.Types_s.nat64 ->\n d3: Vale.Def.Types_s.nat64 ->\n carry: Vale.Def.Types_s.nat64\n -> FStar.Pervasives.Lemma\n (requires\n Vale.Curve25519.Fast_defs.pow2_five d0 d1 d2 d3 carry ==\n Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 * b /\\ b < 131072)\n (ensures carry * 38 < Vale.Curve25519.FastHybrid_helpers.pow2_63)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_fast_mul1 (a:nat)\n (b a0 a1 a2 a3\n ba0_hi ba0_lo\n ba1_hi ba1_lo\n ba2_hi ba2_lo\n ba3_hi ba3_lo\n s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n\n pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0)\n )\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b)\n =\n assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b*a0) (b*a1) (b*a2) (b*a3)) int_canon;\n //lemma_prod_bounds ba0_hi ba0_lo b a0;\n //lemma_prod_bounds ba1_hi ba1_lo b a1;\n //lemma_prod_bounds ba2_hi ba2_lo b a2;\n //lemma_prod_bounds ba3_hi ba3_lo b a3;\n ()\n\n\nlet lemma_addition (a d:nat) (a0 a1 a2 a3 d0 d1 d2 d3 d4:nat64)\n (s0 s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n d = pow2_five d0 d1 d2 d3 d4 /\\\n (let s0', c0 = add_carry a0 d0 0 in\n let s1', c1 = add_carry a1 d1 c0 in\n let s2', c2 = add_carry a2 d2 c1 in\n let s3', c3 = add_carry a3 d3 c2 in\n let s4', c4 = add_carry d4 0 c3 in\n s0 == s0' /\\\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0))\n (ensures a + d == pow2_five s0 s1 s2 s3 s4)\n =\n ()\n\nlet lemma_carry_wide (a0 a1 a2 a3 a4 a5 a6 a7\n d0 d1 d2 d3 carry\n d0' d1' d2' d3':nat64) : Lemma\n (requires pow2_five d0 d1 d2 d3 carry == 38 * pow2_four a4 a5 a6 a7 + pow2_four a0 a1 a2 a3 /\\\n pow2_four d0' d1' d2' d3' % prime == ((pow2_four d0 d1 d2 d3) + carry * pow2_256) % prime)\n (ensures (pow2_four d0' d1' d2' d3') % prime == (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime)\n =\n calc (==) {\n pow2_four d0' d1' d2' d3' % prime;\n == { calc (==) {\n (pow2_four d0 d1 d2 d3) + carry * pow2_256;\n == { _ by (int_canon()) }\n pow2_five d0 d1 d2 d3 carry;\n }\n }\n pow2_five d0 d1 d2 d3 carry % prime;\n == {}\n (pow2_four a0 a1 a2 a3 + 38 * pow2_four a4 a5 a6 a7) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) (pow2_four a4 a5 a6 a7) }\n (pow2_four a0 a1 a2 a3 + pow2_256 * pow2_four a4 a5 a6 a7) % prime;\n == { _ by (int_canon()) }\n (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime;\n }\n\n\nlet pow2int_four (c0 c1 c2 c3:int) : int = c0 + c1 * pow2_64 + c2 * pow2_128 + c3 * pow2_192\n\n#reset-options \"--z3rlimit 10 --fuel 0 --ifuel 0\"\nlet lemma_mul_pow256_sub (x y:nat) :\n Lemma ((x - y * pow2_256) % prime == (x - y * 38) % prime)\n =\n assert_norm (pow2_256 % prime == 38);\n FStar.Math.Lemmas.lemma_mod_mul_distr_r (-y) pow2_256 prime;\n FStar.Math.Lemmas.lemma_mod_mul_distr_r (-y) 38 prime;\n assert ((-y * pow2_256) % prime == (- y * 38) % prime);\n\n FStar.Math.Lemmas.modulo_add prime x (- (y * pow2_256)) (- (y * 38))\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_carry_sub_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_four a0' a1' a2' a3' - carry * pow2_256 == pow2_four a0 a1 a2 a3 - carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' - carry * 38 >= 0 /\\\n (pow2_four (a0' - carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 - carry_in * pow2_256) % prime)\n =\n assert (a0' - carry * 38 >= 0);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 - carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_sub (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 - carry_in * 38) % prime;\n == {}\n (pow2_four a0' a1' a2' a3' - (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_sub (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' - (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') - (carry * 38);\n == { _ by (int_canon()) }\n pow2int_four (a0' - carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' - carry * 38) a1' a2' a3') % prime;\n };\n ()\n\nPartial implementation:\nlet lemma_fmul (a0 a1 a2 a3 b d0 d1 d2 d3 carry: nat64)\n : Lemma (requires pow2_five d0 d1 d2 d3 carry == (pow2_four a0 a1 a2 a3) * b /\\ b < 131072)\n (ensures carry * 38 < pow2_63) =\n \n"
},
{
"role": "assistant",
"content": "assert (pow2_four a0 a1 a2 a3 < pow2_256);\nassert_norm (131072 == pow2 17);\nlemma_mul_bounds_le (pow2_four a0 a1 a2 a3) pow2_256 b (pow2 17);\nassert ((pow2_four a0 a1 a2 a3) * b <= pow2_256 * pow2 17);\nlemma_mul_bounds_le b b (pow2_four a0 a1 a2 a3) (pow2_four a0 a1 a2 a3);\nassert ((pow2_four a0 a1 a2 a3) * b <= pow2_256 * pow2 17)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: a: Type -> p: FStar.PCM.pcm a -> Type0\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2020 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\nmodule Steel.GhostPCMReference\n(* A ghost variant of Steel.PCMReference *)\nopen FStar.PCM\nopen FStar.Ghost\n\nopen Steel.Memory\nopen Steel.Effect.Atomic\nopen Steel.Effect\nmodule Mem = Steel.Memory\nmodule P = Steel.PCMReference\n\nPartial implementation:\nlet ref (a: Type) (p: pcm a) =\n \n"
},
{
"role": "assistant",
"content": "erased (Steel.Memory.ref a p)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: r: Steel.GhostPCMReference.ref a p -> fact: Steel.Memory.property a -> Type0\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2020 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\nmodule Steel.GhostPCMReference\n(* A ghost variant of Steel.PCMReference *)\nopen FStar.PCM\nopen FStar.Ghost\n\nopen Steel.Memory\nopen Steel.Effect.Atomic\nopen Steel.Effect\nmodule Mem = Steel.Memory\nmodule P = Steel.PCMReference\n\nlet ref (a:Type) (p:pcm a) = erased (Steel.Memory.ref a p)\n\n/// Its selector is non-informative (it is unit)\n[@@__reduce__]\nlet pts_to (#a:Type u#1) (#pcm:pcm a) (r:ref a pcm) ([@@@smt_fallback]v:a)\n = to_vprop (Steel.Memory.pts_to r v)\n\nlet alloc (#o:inames)\n (#a:Type)\n (#pcm:pcm a)\n (x:a)\n : SteelGhost\n (ref a pcm) o\n (emp)\n (fun r -> pts_to r x)\n (requires fun _ -> pcm.refine x)\n (ensures fun _ _ _ -> True)\n = rewrite_slprop emp (to_vprop Mem.emp) (fun _ -> reveal_emp ());\n FStar.PCM.compatible_refl pcm x;\n let r = as_atomic_action_ghost (alloc_action o x) in\n r\n\nlet read (#o:inames)\n (#a:Type)\n (#pcm:pcm a)\n (#v0:a)\n (r:ref a pcm)\n : SteelGhost a o\n (pts_to r v0)\n (fun _ -> pts_to r v0)\n (requires fun _ -> True)\n (ensures fun _ v _ -> compatible pcm v0 v)\n = let v = as_atomic_action_ghost (sel_action o r v0) in\n v\n\nlet write (#o:inames)\n (#a:Type)\n (#pcm:pcm a)\n (r:ref a pcm)\n (v0:a)\n (v1:a)\n : SteelGhost unit o\n (pts_to r v0)\n (fun _ -> pts_to r v1)\n (requires fun _ -> frame_preserving pcm v0 v1 /\\ pcm.refine v1)\n (ensures fun _ _ _ -> True)\n = as_atomic_action_ghost (upd_action o r v0 v1)\n\nlet upd_gen (#o:inames)\n (#a:Type)\n (#p:pcm a)\n (r:ref a p)\n (x y:a)\n (f:frame_preserving_upd p x y)\n : SteelGhostT unit o\n (pts_to r x)\n (fun _ -> pts_to r y)\n = as_atomic_action_ghost (Steel.Memory.upd_gen o r x y f)\n\nlet share (#o:inames)\n (#a:Type)\n (#p:pcm a)\n (r:ref a p)\n (v:a)\n (v0:a)\n (v1:a)\n : SteelGhost unit o\n (pts_to r v)\n (fun _ -> pts_to r v0 `star` pts_to r v1)\n (requires fun _ ->\n composable p v0 v1 /\\\n v == op p v0 v1)\n (ensures fun _ _ _ -> True)\n = P.split r v v0 v1\n\nlet gather (#o:inames)\n (#a:Type)\n (#p:FStar.PCM.pcm a)\n (r:ref a p)\n (v0:a)\n (v1:a)\n : SteelGhostT (_:unit{composable p v0 v1}) o\n (pts_to r v0 `star` pts_to r v1)\n (fun _ -> pts_to r (op p v0 v1))\n = P.gather r v0 v1\n\nPartial implementation:\nlet witnessed (#a: Type) (#p: pcm a) (r: ref a p) (fact: property a) : Type0 =\n \n"
},
{
"role": "assistant",
"content": "Steel.Memory.witnessed r fact"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n a0: Vale.Def.Types_s.nat64 ->\n a1: Vale.Def.Types_s.nat64 ->\n a2: Vale.Def.Types_s.nat64 ->\n a3: Vale.Def.Types_s.nat64 ->\n a0': Vale.Def.Types_s.nat64 ->\n a1': Vale.Def.Types_s.nat64 ->\n a2': Vale.Def.Types_s.nat64 ->\n a3': Vale.Def.Types_s.nat64 ->\n carry_in: Vale.Def.Types_s.nat64 ->\n carry: Vale.Curve25519.Fast_defs.bit\n -> FStar.Pervasives.Lemma\n (requires\n Vale.Curve25519.Fast_defs.pow2_four a0' a1' a2' a3' -\n carry * Vale.Curve25519.Fast_defs.pow2_256 ==\n Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 - carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < Vale.Def.Words_s.pow2_64)\n (ensures\n a0' - carry * 38 >= 0 /\\\n Vale.Curve25519.Fast_defs.pow2_four (a0' - carry * 38) a1' a2' a3' %\n Vale.Curve25519.Fast_defs.prime ==\n (Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 -\n carry_in * Vale.Curve25519.Fast_defs.pow2_256) %\n Vale.Curve25519.Fast_defs.prime)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_fast_mul1 (a:nat)\n (b a0 a1 a2 a3\n ba0_hi ba0_lo\n ba1_hi ba1_lo\n ba2_hi ba2_lo\n ba3_hi ba3_lo\n s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n\n pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0)\n )\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b)\n =\n assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b*a0) (b*a1) (b*a2) (b*a3)) int_canon;\n //lemma_prod_bounds ba0_hi ba0_lo b a0;\n //lemma_prod_bounds ba1_hi ba1_lo b a1;\n //lemma_prod_bounds ba2_hi ba2_lo b a2;\n //lemma_prod_bounds ba3_hi ba3_lo b a3;\n ()\n\n\nlet lemma_addition (a d:nat) (a0 a1 a2 a3 d0 d1 d2 d3 d4:nat64)\n (s0 s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n d = pow2_five d0 d1 d2 d3 d4 /\\\n (let s0', c0 = add_carry a0 d0 0 in\n let s1', c1 = add_carry a1 d1 c0 in\n let s2', c2 = add_carry a2 d2 c1 in\n let s3', c3 = add_carry a3 d3 c2 in\n let s4', c4 = add_carry d4 0 c3 in\n s0 == s0' /\\\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0))\n (ensures a + d == pow2_five s0 s1 s2 s3 s4)\n =\n ()\n\nlet lemma_carry_wide (a0 a1 a2 a3 a4 a5 a6 a7\n d0 d1 d2 d3 carry\n d0' d1' d2' d3':nat64) : Lemma\n (requires pow2_five d0 d1 d2 d3 carry == 38 * pow2_four a4 a5 a6 a7 + pow2_four a0 a1 a2 a3 /\\\n pow2_four d0' d1' d2' d3' % prime == ((pow2_four d0 d1 d2 d3) + carry * pow2_256) % prime)\n (ensures (pow2_four d0' d1' d2' d3') % prime == (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime)\n =\n calc (==) {\n pow2_four d0' d1' d2' d3' % prime;\n == { calc (==) {\n (pow2_four d0 d1 d2 d3) + carry * pow2_256;\n == { _ by (int_canon()) }\n pow2_five d0 d1 d2 d3 carry;\n }\n }\n pow2_five d0 d1 d2 d3 carry % prime;\n == {}\n (pow2_four a0 a1 a2 a3 + 38 * pow2_four a4 a5 a6 a7) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) (pow2_four a4 a5 a6 a7) }\n (pow2_four a0 a1 a2 a3 + pow2_256 * pow2_four a4 a5 a6 a7) % prime;\n == { _ by (int_canon()) }\n (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime;\n }\n\n\nlet pow2int_four (c0 c1 c2 c3:int) : int = c0 + c1 * pow2_64 + c2 * pow2_128 + c3 * pow2_192\n\n#reset-options \"--z3rlimit 10 --fuel 0 --ifuel 0\"\nlet lemma_mul_pow256_sub (x y:nat) :\n Lemma ((x - y * pow2_256) % prime == (x - y * 38) % prime)\n =\n assert_norm (pow2_256 % prime == 38);\n FStar.Math.Lemmas.lemma_mod_mul_distr_r (-y) pow2_256 prime;\n FStar.Math.Lemmas.lemma_mod_mul_distr_r (-y) 38 prime;\n assert ((-y * pow2_256) % prime == (- y * 38) % prime);\n\n FStar.Math.Lemmas.modulo_add prime x (- (y * pow2_256)) (- (y * 38))\n\nPartial implementation:\nlet lemma_carry_sub_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in: nat64) (carry: bit)\n : Lemma\n (requires\n pow2_four a0' a1' a2' a3' - carry * pow2_256 == pow2_four a0 a1 a2 a3 - carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures\n a0' - carry * 38 >= 0 /\\\n (pow2_four (a0' - carry * 38) a1' a2' a3') % prime ==\n (pow2_four a0 a1 a2 a3 - carry_in * pow2_256) % prime) =\n \n"
},
{
"role": "assistant",
"content": "assert (a0' - carry * 38 >= 0);\ncalc ( == ) {\n (pow2_four a0 a1 a2 a3 - carry_in * pow2_256) % prime;\n ( == ) { lemma_mul_pow256_sub (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 - carry_in * 38) % prime;\n ( == ) { () }\n (pow2_four a0' a1' a2' a3' - (carry * pow2_256)) % prime;\n ( == ) { lemma_mul_pow256_sub (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' - (carry * 38)) % prime;\n ( == ) { calc ( == ) {\n (pow2_four a0' a1' a2' a3') - (carry * 38);\n ( == ) { FStar.Tactics.Effect.synth_by_tactic (fun _ -> (int_canon ())) }\n pow2int_four (a0' - carry * 38) a1' a2' a3';\n } }\n (pow2_four (a0' - carry * 38) a1' a2' a3') % prime;\n};\n()"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Prims.int\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\nPartial implementation:\nlet size_k_w_256 =\n \n"
},
{
"role": "assistant",
"content": "64"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Prims.int\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n\nPartial implementation:\nlet block_length =\n \n"
},
{
"role": "assistant",
"content": "4 * size_block_w_256"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Prims.int\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n\nPartial implementation:\nlet size_block_w_256 =\n \n"
},
{
"role": "assistant",
"content": "16"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n\nPartial implementation:\nlet block_w =\n \n"
},
{
"role": "assistant",
"content": "m: seq word {length m = size_block_w_256}"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n a0: Vale.Def.Types_s.nat64 ->\n a1: Vale.Def.Types_s.nat64 ->\n a2: Vale.Def.Types_s.nat64 ->\n a3: Vale.Def.Types_s.nat64 ->\n a4: Vale.Def.Types_s.nat64 ->\n a5: Vale.Def.Types_s.nat64 ->\n a6: Vale.Def.Types_s.nat64 ->\n a7: Vale.Def.Types_s.nat64 ->\n d0: Vale.Def.Types_s.nat64 ->\n d1: Vale.Def.Types_s.nat64 ->\n d2: Vale.Def.Types_s.nat64 ->\n d3: Vale.Def.Types_s.nat64 ->\n carry: Vale.Def.Types_s.nat64 ->\n d0': Vale.Def.Types_s.nat64 ->\n d1': Vale.Def.Types_s.nat64 ->\n d2': Vale.Def.Types_s.nat64 ->\n d3': Vale.Def.Types_s.nat64\n -> FStar.Pervasives.Lemma\n (requires\n Vale.Curve25519.Fast_defs.pow2_five d0 d1 d2 d3 carry ==\n 38 * Vale.Curve25519.Fast_defs.pow2_four a4 a5 a6 a7 +\n Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 /\\\n Vale.Curve25519.Fast_defs.pow2_four d0' d1' d2' d3' % Vale.Curve25519.Fast_defs.prime ==\n (Vale.Curve25519.Fast_defs.pow2_four d0 d1 d2 d3 +\n carry * Vale.Curve25519.Fast_defs.pow2_256) %\n Vale.Curve25519.Fast_defs.prime)\n (ensures\n Vale.Curve25519.Fast_defs.pow2_four d0' d1' d2' d3' % Vale.Curve25519.Fast_defs.prime ==\n Vale.Curve25519.Fast_defs.pow2_eight a0 a1 a2 a3 a4 a5 a6 a7 %\n Vale.Curve25519.Fast_defs.prime)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\n#reset-options \"--z3rlimit 30 --max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\nlet lemma_fast_mul1 (a:nat)\n (b a0 a1 a2 a3\n ba0_hi ba0_lo\n ba1_hi ba1_lo\n ba2_hi ba2_lo\n ba3_hi ba3_lo\n s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n\n pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0)\n )\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b)\n =\n assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b*a0) (b*a1) (b*a2) (b*a3)) int_canon;\n //lemma_prod_bounds ba0_hi ba0_lo b a0;\n //lemma_prod_bounds ba1_hi ba1_lo b a1;\n //lemma_prod_bounds ba2_hi ba2_lo b a2;\n //lemma_prod_bounds ba3_hi ba3_lo b a3;\n ()\n\n\nlet lemma_addition (a d:nat) (a0 a1 a2 a3 d0 d1 d2 d3 d4:nat64)\n (s0 s1 s2 s3 s4:nat64) : Lemma\n (requires a = pow2_four a0 a1 a2 a3 /\\\n d = pow2_five d0 d1 d2 d3 d4 /\\\n (let s0', c0 = add_carry a0 d0 0 in\n let s1', c1 = add_carry a1 d1 c0 in\n let s2', c2 = add_carry a2 d2 c1 in\n let s3', c3 = add_carry a3 d3 c2 in\n let s4', c4 = add_carry d4 0 c3 in\n s0 == s0' /\\\n s1 == s1' /\\\n s2 == s2' /\\\n s3 == s3' /\\\n s4 == s4' /\\\n c4 == 0))\n (ensures a + d == pow2_five s0 s1 s2 s3 s4)\n =\n ()\n\nPartial implementation:\nlet lemma_carry_wide (a0 a1 a2 a3 a4 a5 a6 a7 d0 d1 d2 d3 carry d0' d1' d2' d3': nat64)\n : Lemma\n (requires\n pow2_five d0 d1 d2 d3 carry == 38 * pow2_four a4 a5 a6 a7 + pow2_four a0 a1 a2 a3 /\\\n pow2_four d0' d1' d2' d3' % prime == ((pow2_four d0 d1 d2 d3) + carry * pow2_256) % prime)\n (ensures (pow2_four d0' d1' d2' d3') % prime == (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime) =\n \n"
},
{
"role": "assistant",
"content": "calc ( == ) {\n pow2_four d0' d1' d2' d3' % prime;\n ( == ) { calc ( == ) {\n (pow2_four d0 d1 d2 d3) + carry * pow2_256;\n ( == ) { FStar.Tactics.Effect.synth_by_tactic (fun _ -> (int_canon ())) }\n pow2_five d0 d1 d2 d3 carry;\n } }\n pow2_five d0 d1 d2 d3 carry % prime;\n ( == ) { () }\n (pow2_four a0 a1 a2 a3 + 38 * pow2_four a4 a5 a6 a7) % prime;\n ( == ) { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) (pow2_four a4 a5 a6 a7) }\n (pow2_four a0 a1 a2 a3 + pow2_256 * pow2_four a4 a5 a6 a7) % prime;\n ( == ) { FStar.Tactics.Effect.synth_by_tactic (fun _ -> (int_canon ())) }\n (pow2_eight a0 a1 a2 a3 a4 a5 a6 a7) % prime;\n}"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\n\nPartial implementation:\nlet hash256 =\n \n"
},
{
"role": "assistant",
"content": "m: Seq.seq word {Seq.length m = 8}"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\n\nPartial implementation:\nlet counter =\n \n"
},
{
"role": "assistant",
"content": "nat"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Vale.SHA.PPC64LE.SHA_helpers.counter -> _: Vale.SHA.PPC64LE.SHA_helpers.block_w\n -> Vale.Def.Words_s.nat32\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nPartial implementation:\nlet sigma_0_0_partial =\n \n"
},
{
"role": "assistant",
"content": "opaque_make sigma_0_0_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n a0: Vale.Def.Types_s.nat64 ->\n a1: Vale.Def.Types_s.nat64 ->\n a2: Vale.Def.Types_s.nat64 ->\n a3: Vale.Def.Types_s.nat64 ->\n a0': Vale.Def.Types_s.nat64 ->\n a1': Vale.Def.Types_s.nat64 ->\n a2': Vale.Def.Types_s.nat64 ->\n a3': Vale.Def.Types_s.nat64 ->\n carry_in: Vale.Def.Types_s.nat64 ->\n carry: Vale.Curve25519.Fast_defs.bit\n -> FStar.Pervasives.Lemma\n (requires\n Vale.Curve25519.Fast_defs.pow2_five a0' a1' a2' a3' carry ==\n Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < Vale.Def.Words_s.pow2_64)\n (ensures\n a0' + carry * 38 < Vale.Def.Words_s.pow2_64 /\\\n Vale.Curve25519.Fast_defs.pow2_four (a0' + carry * 38) a1' a2' a3' %\n Vale.Curve25519.Fast_defs.prime ==\n (Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 +\n carry_in * Vale.Curve25519.Fast_defs.pow2_256) %\n Vale.Curve25519.Fast_defs.prime)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nPartial implementation:\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in: nat64) (carry: bit)\n : Lemma\n (requires\n pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures\n a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime ==\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime) =\n \n"
},
{
"role": "assistant",
"content": "assert (a0' + carry * 38 < pow2_64);\ncalc ( == ) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n ( == ) { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n ( == ) { () }\n (pow2_five a0' a1' a2' a3' carry) % prime;\n ( == ) { FStar.Tactics.Effect.synth_by_tactic (fun _ -> (int_canon ())) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n ( == ) { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n ( == ) { calc ( == ) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n ( == ) { FStar.Tactics.Effect.synth_by_tactic (fun _ -> (int_canon ())) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n } }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n};\n()"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Prims.unit\n -> FStar.Pervasives.Lemma\n (ensures\n Vale.SHA.PPC64LE.SHA_helpers.sigma_1_0_partial ==\n Vale.SHA.PPC64LE.SHA_helpers.sigma_1_0_partial_def)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n\nPartial implementation:\nlet sigma_1_0_partial_reveal =\n \n"
},
{
"role": "assistant",
"content": "opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Prims.unit\n -> FStar.Pervasives.Lemma\n (ensures\n Vale.SHA.PPC64LE.SHA_helpers.sigma_0_1_partial ==\n Vale.SHA.PPC64LE.SHA_helpers.sigma_0_1_partial_def)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n\nPartial implementation:\nlet sigma_0_1_partial_reveal =\n \n"
},
{
"role": "assistant",
"content": "opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Prims.unit\n -> FStar.Pervasives.Lemma\n (ensures\n Vale.SHA.PPC64LE.SHA_helpers.sigma_0_0_partial ==\n Vale.SHA.PPC64LE.SHA_helpers.sigma_0_0_partial_def)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n\nPartial implementation:\nlet sigma_0_0_partial_reveal =\n \n"
},
{
"role": "assistant",
"content": "opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: x: a -> Vale.Interop.Base.normal a\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\n\nPartial implementation:\nlet as_normal_t (#a: Type) (x: a) : normal a =\n \n"
},
{
"role": "assistant",
"content": "x"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n _: Vale.SHA.PPC64LE.SHA_helpers.counter ->\n _: Vale.SHA.PPC64LE.SHA_helpers.block_w ->\n _: Vale.SHA.PPC64LE.SHA_helpers.hash256\n -> Vale.Def.Words_s.nat32\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nPartial implementation:\nlet sigma_1_0_partial =\n \n"
},
{
"role": "assistant",
"content": "opaque_make sigma_1_0_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n _: Vale.SHA.PPC64LE.SHA_helpers.counter ->\n _: Vale.SHA.PPC64LE.SHA_helpers.block_w ->\n _: Vale.SHA.PPC64LE.SHA_helpers.hash256\n -> Vale.Def.Words_s.nat32\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_0_partial = opaque_make sigma_1_0_partial_def\nirreducible let sigma_1_0_partial_reveal = opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def\n\nval lemma_sha256_sigma2 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[0]))\n (ensures (sigma256_1_0 src.hi3 == sigma_1_0_partial t block hash_orig))\n\nPartial implementation:\nlet sigma_1_1_partial =\n \n"
},
{
"role": "assistant",
"content": "opaque_make sigma_1_1_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Vale.SHA.PPC64LE.SHA_helpers.counter -> _: Vale.SHA.PPC64LE.SHA_helpers.block_w\n -> Vale.Def.Words_s.nat32\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nPartial implementation:\nlet sigma_0_1_partial =\n \n"
},
{
"role": "assistant",
"content": "opaque_make sigma_0_1_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: Prims.unit\n -> FStar.Pervasives.Lemma\n (ensures\n Vale.SHA.PPC64LE.SHA_helpers.sigma_1_1_partial ==\n Vale.SHA.PPC64LE.SHA_helpers.sigma_1_1_partial_def)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_0_partial = opaque_make sigma_1_0_partial_def\nirreducible let sigma_1_0_partial_reveal = opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def\n\nval lemma_sha256_sigma2 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[0]))\n (ensures (sigma256_1_0 src.hi3 == sigma_1_0_partial t block hash_orig))\n\nval sigma_1_1_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n\nPartial implementation:\nlet sigma_1_1_partial_reveal =\n \n"
},
{
"role": "assistant",
"content": "opaque_revealer (`%sigma_1_1_partial) sigma_1_1_partial sigma_1_1_partial_def"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: block: Vale.SHA.PPC64LE.SHA_helpers.block_w -> hash: Vale.SHA.PPC64LE.SHA_helpers.hash256\n -> Vale.SHA.PPC64LE.SHA_helpers.hash256\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_0_partial = opaque_make sigma_1_0_partial_def\nirreducible let sigma_1_0_partial_reveal = opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def\n\nval lemma_sha256_sigma2 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[0]))\n (ensures (sigma256_1_0 src.hi3 == sigma_1_0_partial t block hash_orig))\n\nval sigma_1_1_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_1_partial = opaque_make sigma_1_1_partial_def\nirreducible let sigma_1_1_partial_reveal = opaque_revealer (`%sigma_1_1_partial) sigma_1_1_partial sigma_1_1_partial_def\n\nval lemma_sha256_sigma3 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[4]))\n (ensures (sigma256_1_1 src.hi3 == sigma_1_1_partial t block hash_orig))\n\nval make_seperated_hash (a b c d e f g h:nat32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a /\\\n hash.[1] == nat32_to_word b /\\\n hash.[2] == nat32_to_word c /\\\n hash.[3] == nat32_to_word d /\\\n hash.[4] == nat32_to_word e /\\\n hash.[5] == nat32_to_word f /\\\n hash.[6] == nat32_to_word g /\\\n hash.[7] == nat32_to_word h\n )\n\nval make_seperated_hash_quad32 (a b c d e f g h:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a.hi3 /\\\n hash.[1] == nat32_to_word b.hi3 /\\\n hash.[2] == nat32_to_word c.hi3 /\\\n hash.[3] == nat32_to_word d.hi3 /\\\n hash.[4] == nat32_to_word e.hi3 /\\\n hash.[5] == nat32_to_word f.hi3 /\\\n hash.[6] == nat32_to_word g.hi3 /\\\n hash.[7] == nat32_to_word h.hi3\n )\n\nval lemma_make_seperated_hash (hash:hash256) (a b c d e f g h:quad32) : Lemma\n (requires length hash == 8 /\\\n a.hi3 == word_to_nat32 hash.[0] /\\\n b.hi3 == word_to_nat32 hash.[1] /\\\n c.hi3 == word_to_nat32 hash.[2] /\\\n d.hi3 == word_to_nat32 hash.[3] /\\\n e.hi3 == word_to_nat32 hash.[4] /\\\n f.hi3 == word_to_nat32 hash.[5] /\\\n g.hi3 == word_to_nat32 hash.[6] /\\\n h.hi3 == word_to_nat32 hash.[7])\n (ensures hash == make_seperated_hash_quad32 a b c d e f g h)\n\nval lemma_vsel32 (a b c:nat32) : Lemma\n (ensures (isel32 a b c = (iand32 c a) *^ (iand32 (inot32 c) b)))\n\nval ch_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ (iand32 (inot32 x) z))\n\nval lemma_eq_maj_xvsel32 (a b c:nat32) : Lemma\n (ensures (isel32 c b (a *^ b) = (iand32 a b) *^ ((iand32 a c) *^ (iand32 b c))))\n\nval maj_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ ((iand32 x z) *^ (iand32 y z)))\n\nval lemma_sigma_0_0_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_0 (ws_opaque block (t-15)) == sigma_0_0_partial t block))\n\nval lemma_sigma_0_1_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_1 (ws_opaque block (t-2)) == sigma_0_1_partial t block))\n\nval lemma_sigma_1_0_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_0 (word_to_nat32 ((repeat_range_vale t block hash_orig).[0])) == sigma_1_0_partial t block hash_orig))\n\nval lemma_sigma_1_1_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_1 (word_to_nat32 ((repeat_range_vale t block hash_orig).[4])) == sigma_1_1_partial t block hash_orig))\n\n(* Abbreviations and lemmas for the code itself *)\nlet k_reqs (k_seq:seq quad32) : prop0 =\n length k_seq == size_k_w_256 / 4 /\\\n (forall i . {:pattern (index k_seq i)} 0 <= i /\\ i < (size_k_w_256/4) ==>\n (k_seq.[i]).lo0 == word_to_nat32 (k.[4 * i]) /\\\n (k_seq.[i]).lo1 == word_to_nat32 (k.[4 * i + 1]) /\\\n (k_seq.[i]).hi2 == word_to_nat32 (k.[4 * i + 2]) /\\\n (k_seq.[i]).hi3 == word_to_nat32 (k.[4 * i + 3]))\n\nlet quads_to_block_be (qs:seq quad32) : block_w\n =\n let nat32_seq = Vale.Def.Words.Seq_s.seq_four_to_seq_BE qs in\n let f (n:nat{n < 16}) : word = nat32_to_word (if n < length nat32_seq then nat32_seq.[n] else 0) in\n init 16 f\n\nval lemma_quads_to_block_be (qs:seq quad32) : Lemma\n (requires length qs == 4)\n (ensures\n (let block = quads_to_block_be qs in\n forall i . {:pattern (index qs i)} 0 <= i /\\ i < 4 ==>\n (qs.[i]).hi3 == ws_opaque block (4 * i + 0) /\\\n (qs.[i]).hi2 == ws_opaque block (4 * i + 1) /\\\n (qs.[i]).lo1 == ws_opaque block (4 * i + 2) /\\\n (qs.[i]).lo0 == ws_opaque block (4 * i + 3)))\n\nlet k_index (ks:seq quad32) (i:nat) : nat32 =\n if length ks = size_k_w_256 / 4 && i < size_k_w_256 then four_select ks.[(i/4)] (i % 4)\n else 0\n\n\nval lemma_shuffle_core_properties (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (let hash = Spec.Loops.repeat_range 0 t (shuffle_core_opaque block) hash_orig in\n let h = Spec.Loops.repeat_range 0 (t + 1) (shuffle_core_opaque block) hash_orig in\n let a0 = word_to_nat32 hash.[0] in\n let b0 = word_to_nat32 hash.[1] in\n let c0 = word_to_nat32 hash.[2] in\n let d0 = word_to_nat32 hash.[3] in\n let e0 = word_to_nat32 hash.[4] in\n let f0 = word_to_nat32 hash.[5] in\n let g0 = word_to_nat32 hash.[6] in\n let h0 = word_to_nat32 hash.[7] in\n let t1 = add_wrap (add_wrap (add_wrap (add_wrap h0 (sigma256_1_1 e0)) (ch_256 e0 f0 g0)) (word_to_nat32 k.[t])) (ws_opaque block t) in\n let t2 = add_wrap (sigma256_1_0 a0) (maj_256 a0 b0 c0) in\n word_to_nat32 h.[0] == add_wrap t1 t2 /\\\n word_to_nat32 h.[1] == a0 /\\\n word_to_nat32 h.[2] == b0 /\\\n word_to_nat32 h.[3] == c0 /\\\n word_to_nat32 h.[4] == add_wrap d0 t1 /\\\n word_to_nat32 h.[5] == e0 /\\\n word_to_nat32 h.[6] == f0 /\\\n word_to_nat32 h.[7] == g0))\n\nval lemma_ws_opaque (block:block_w) (t:counter) : Lemma\n (requires 16 <= t && t < size_k_w_256)\n (ensures (let sigma0 = sigma256_0_0 (ws_opaque block (t - 15)) in\n let sigma1 = sigma256_0_1 (ws_opaque block (t - 2)) in\n ws_opaque block t == add_wrap (add_wrap (add_wrap sigma1 (ws_opaque block (t - 7))) sigma0) (ws_opaque block (t - 16))))\n\nPartial implementation:\nlet repeat_range_vale_64 (block: block_w) (hash: hash256) =\n \n"
},
{
"role": "assistant",
"content": "Spec.Loops.repeat_range 0 64 (shuffle_core_opaque block) hash"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: _: MerkleTree.Spec.mt_collide n i -> Prims.GTot MerkleTree.Spec.hash2_raw_collide\nEffect: Prims.GTot\n\nContext:\nmodule MerkleTree.Spec\n\nopen FStar.Classical\nopen FStar.Mul\nopen FStar.Seq\n\nmodule S = FStar.Seq\n\n#set-options \"--max_fuel 0 --max_ifuel 0 --z3rlimit 10\"\n\n// For SHA2_256, this is is a sequence of 32 bytes\n// These are secret bytes, hence not an eqtype\ntype hash (#hsz:pos) = b:Spec.Hash.Definitions.bytes { Seq.length b = hsz }\n\ntype hash_fun_t (#hsz:pos) = hash #hsz -> hash #hsz -> GTot (hash #hsz)\n\nval sha256_compress: hash_fun_t #32\nlet sha256_compress src1 src2 =\n let sz = Spec.Hash.Definitions.SHA2_256 in\n let hash_alg = Spec.Hash.Definitions.SHA2_256 in\n let acc = Spec.Agile.Hash.init hash_alg in\n let acc = Spec.Agile.Hash.update hash_alg acc (S.append src1 src2) in\n Spec.Agile.Hash.finish hash_alg acc ()\n\n/// For simplicity, we will specify the root for a sequence of [i]\n/// tags where [i <= 2^n] as the root of a full binary tree with [2^n]\n/// leaves obtained by padding the sequence with dummies. This\n/// requires extending the definitions of hashes and hash functions. Our\n/// extended definition of hash justifies skipping any concrete\n/// computation on dummies.\nnoeq\ntype padded_hash #hsz =\n| HRaw: hr:hash #hsz -> padded_hash #hsz\n| HPad // right padding to make the size of a Merkle tree a power of 2\n\nval padded_hash_fun: (#hsz:pos) -> (f:hash_fun_t #hsz) -> (lh:padded_hash #hsz) -> (rh:padded_hash #hsz) -> GTot (padded_hash #hsz)\nlet padded_hash_fun #hsz f lh rh =\n allow_inversion (padded_hash #hsz);\n match lh, rh with\n | HPad , _ -> HPad\n | _ , HPad -> lh\n | HRaw lhr, HRaw rhr -> HRaw (f lhr rhr)\n\nnoextract\nval hashes (#hsz:pos): Type0\nlet hashes #hsz = S.seq (padded_hash #hsz)\n\ntype merkle_tree (#hsz:pos) n = hs:hashes #hsz {S.length hs = pow2 n}\n\nval mt_get: #hsz:pos -> #n:nat -> mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} -> GTot (padded_hash #hsz)\nlet mt_get #_ #_ mt idx = S.index mt idx\n\nunfold let op_String_Access (#hsz:pos) = S.index #(padded_hash #hsz)\n\n#push-options \"--max_fuel 1\"\n\nval mt_left: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_left #_ #n mt = S.slice mt 0 (pow2 (n-1))\n\nval mt_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n -> merkle_tree #hsz (n-1)\nlet mt_right #_ #n mt = S.slice mt (pow2 (n-1)) (pow2 n)\n\nval mt_left_right: #hsz:pos -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (S.equal mt (mt_left mt @| mt_right mt))\nlet mt_left_right #_ #_ mt = ()\n\nval hs_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes #hsz {S.length hs = 2 * n} -> GTot (nhs:hashes #hsz {S.length nhs = n})\nlet rec hs_next_lv #hsz #f #n hs =\n if n = 0 then S.empty\n else S.cons\n (padded_hash_fun #hsz f hs.[0] hs.[1])\n (hs_next_lv #hsz #f #(n-1) (S.slice hs 2 (S.length hs)))\n\nval hs_next_lv_index: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> hs:hashes{S.length hs = 2 * n} -> i:nat{i < n} ->\n Lemma ((hs_next_lv #hsz #f #n hs).[i] == padded_hash_fun #hsz f hs.[2 * i] hs.[2 * i + 1])\nlet rec hs_next_lv_index #hsz #f #n hs i =\n if n = 0 || i = 0 then ()\n else hs_next_lv_index #hsz #f #(n - 1) (S.slice hs 2 (S.length hs)) (i - 1)\n\nval hs_next_lv_slice:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n hs:hashes{S.length hs = 2 * n} -> i:nat -> j:nat{i <= j && j <= n} ->\n Lemma (requires True)\n (ensures S.equal (hs_next_lv #hsz #f #(j - i) (S.slice hs (2 * i) (2 * j)))\n (S.slice (hs_next_lv #hsz #f #n hs) i j))\n (decreases (j - i))\nlet rec hs_next_lv_slice #hsz #f #n hs i j =\n if i = j then ()\n else begin\n let x = S.slice hs (2 * i) (2 * j) in\n assert (S.equal (hs_next_lv #hsz #f #(j - i) x)\n (S.cons (padded_hash_fun #hsz f x.[0] x.[1])\n (hs_next_lv #hsz #f #(j - i - 1) (S.slice x 2 (S.length x)))));\n hs_next_lv_slice #hsz #f #n hs (i + 1) j;\n hs_next_lv_index #hsz #f #n hs i\n end\n\nval mt_next_lv: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n -> GTot (merkle_tree #hsz (n-1))\nlet mt_next_lv #_ #f #n mt = hs_next_lv #_ #f #(pow2 (n-1)) mt\n\nval mt_next_lv_mt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_left mt)) (mt_left (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_left #hsz #f #n mt =\n hs_next_lv_slice #_ #f #(pow2 (n-1)) mt 0 (pow2 (n-2))\n\nval mt_next_lv_mt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat{1 < n} -> mt:merkle_tree #hsz n ->\n Lemma (S.equal (mt_next_lv #_ #f #_ (mt_right mt)) (mt_right (mt_next_lv #_ #f #_ mt)))\nlet mt_next_lv_mt_right #hsz #f #n mt =\n hs_next_lv_slice #hsz #f #(pow2 (n-1)) mt (pow2 (n-2)) (pow2 (n-1))\n\nval hs_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= 2 * n} ->\n hs1:hashes{S.length hs1 = 2 * n} ->\n hs2:hashes{S.length hs2 = 2 * n} ->\n Lemma (requires S.equal (S.slice hs1 0 j) (S.slice hs2 0 j))\n (ensures S.equal (S.slice (hs_next_lv #hsz #f #n hs1) 0 (j / 2))\n (S.slice (hs_next_lv #hsz #f #n hs2) 0 (j / 2)))\nlet hs_next_lv_equiv #hsz #f j n hs1 hs2 =\n forall_intro (hs_next_lv_index #_ #f #n hs1);\n forall_intro (hs_next_lv_index #_ #f #n hs2);\n let hs1' = hs_next_lv #_ #f #n hs1 in\n let hs2' = hs_next_lv #_ #f #n hs2 in\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == padded_hash_fun #hsz f hs1.[2 * i] hs1.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs2'.[i] == padded_hash_fun #hsz f hs2.[2 * i] hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j}). (S.slice hs1 0 j).[i] == (S.slice hs2 0 j).[i]);\n assert (forall (i:nat{i < j}). hs1.[i] == hs2.[i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i] == hs2.[2 * i]);\n assert (forall (i:nat{i < j / 2}). hs1.[2 * i + 1] == hs2.[2 * i + 1]);\n assert (forall (i:nat{i < j / 2}). hs1'.[i] == hs2'.[i])\n\nval mt_next_lv_equiv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n j:nat -> n:pos{j <= pow2 n} ->\n mt1:merkle_tree #hsz n -> mt2:merkle_tree #hsz n ->\n Lemma (requires S.equal (S.slice mt1 0 j) (S.slice mt2 0 j))\n (ensures S.equal (S.slice (mt_next_lv #_ #f #_ mt1) 0 (j / 2))\n (S.slice (mt_next_lv #_ #f #_ mt2) 0 (j / 2)))\nlet mt_next_lv_equiv #hsz #f j n mt1 mt2 =\n hs_next_lv_equiv #_ #f j (pow2 (n-1)) mt1 mt2\n\nval hs_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes #hsz {S.length hs = 2 * n} ->\n nhs:hashes #hsz {S.length nhs = n} ->\n GTot Type0\nlet hs_next_rel #hsz #f n hs nhs =\n forall (i:nat{i < n}).\n S.index nhs i ==\n padded_hash_fun #hsz f (S.index hs (2 * i)) (S.index hs (2 * i + 1))\n\nval mt_next_rel:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n GTot Type0\nlet mt_next_rel #hsz #f n mt nmt =\n hs_next_rel #hsz #f (pow2 (n-1)) mt nmt\n\nval hs_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:nat ->\n hs:hashes{S.length hs = 2 * n} ->\n nhs:hashes{S.length nhs = n} ->\n Lemma (requires hs_next_rel #_ #f n hs nhs)\n (ensures S.equal nhs (hs_next_lv #_ #f #n hs))\nlet rec hs_next_rel_next_lv #hsz #f n hs nhs =\n if n = 0 then ()\n else hs_next_rel_next_lv #_ #f (n - 1)\n (S.slice hs 2 (S.length hs))\n (S.slice nhs 1 (S.length nhs))\n\nval mt_next_rel_next_lv:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures S.equal nmt (mt_next_lv #_ #f mt))\nlet mt_next_rel_next_lv #hsz #f n mt nmt =\n hs_next_rel_next_lv #_ #f (pow2 (n-1)) mt nmt\n\nval mt_next_rel_upd_even:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i) v)\n (S.upd nmt i (padded_hash_fun #hsz f v (S.index mt (2 * i + 1)))))\nlet mt_next_rel_upd_even #hsz #f n mt nmt i v = ()\n\n#push-options \"--z3rlimit 10 --initial_fuel 1 --max_fuel 1 --initial_ifuel 1 --max_ifuel 1\"\nval mt_next_rel_upd_even_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree #hsz (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash #hsz ->\n Lemma (requires (mt_next_rel #_ #f n mt nmt) /\\ (S.index mt (2 * i + 1) == HPad))\n (ensures (mt_next_rel #_ #f n (S.upd mt (2 * i) v) (S.upd nmt i v)))\nlet mt_next_rel_upd_even_pad #hsz #f n mt nmt i v = ()\n#pop-options\n\nval mt_next_rel_upd_odd:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n n:pos ->\n mt:merkle_tree #hsz n ->\n nmt:merkle_tree (n - 1) ->\n i:nat{i < pow2 (n-1)} ->\n v:padded_hash ->\n Lemma (requires mt_next_rel #_ #f n mt nmt)\n (ensures mt_next_rel #_ #f n\n (S.upd mt (2 * i + 1) v)\n (S.upd nmt i (padded_hash_fun #_ f (S.index mt (2 * i)) v)))\nlet mt_next_rel_upd_odd #hsz #f n mt nmt i v = ()\n\n// fournet: just [root]?\nval mt_get_root:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n -> GTot (padded_hash #hsz)\nlet rec mt_get_root #hsz #f #n mt =\n if n = 0 then mt.[0]\n else mt_get_root #_ #f (mt_next_lv #_ #f mt)\n\n#push-options \"--initial_fuel 2 --max_fuel 2\"\n\nval mt_get_root_step: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> mt:merkle_tree #hsz n ->\n Lemma (mt_get_root #_ #f mt ==\n padded_hash_fun #_ f (mt_get_root #_ #f (mt_left mt)) (mt_get_root #_ #f (mt_right mt)))\nlet rec mt_get_root_step #hsz #f #n mt =\n if n = 1 then ()\n else begin\n mt_get_root_step #_ #f (mt_next_lv #_ #f mt);\n mt_next_lv_mt_left #_ #f mt;\n mt_next_lv_mt_right #_ #f mt\n end\n\n#pop-options\n\ntype path #hsz n = S.lseq (padded_hash #hsz) n\n\n/// We first specify full paths, including padding.\n\nval mt_get_path:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n mt:merkle_tree #hsz n -> i:nat{i < pow2 n} -> GTot (path #hsz n)\nlet rec mt_get_path #hsz #f #n t i =\n if n = 0 then S.empty\n else S.cons\n (if i % 2 = 0 then t.[i + 1] else t.[i - 1])\n (mt_get_path #_ #f (mt_next_lv #_ #f t) (i / 2))\n\nval mt_verify_:\n #hsz:pos -> #f:hash_fun_t #hsz ->#n:nat ->\n p:path #hsz n -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> GTot (padded_hash #hsz)\nlet rec mt_verify_ #hsz #f #n p idx h =\n if n = 0 then h\n else mt_verify_ #_ #f #(n-1) (S.tail p) (idx / 2)\n (if idx % 2 = 0\n then padded_hash_fun #_ f h (S.head p)\n else padded_hash_fun #_ f (S.head p) h)\n\nval mt_verify:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n p:(path #hsz n) -> idx:nat{idx < pow2 n} -> padded_hash #hsz -> padded_hash #hsz -> GTot prop\nlet mt_verify #hsz #f #n p idx h rt =\n rt == mt_verify_ #_ #f p idx h\n\n\n/// Correctness: the root of a tree is correctly recomputed from any of its paths\n\nval hs_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n hs:hashes{S.length hs = 2 * n} -> idx:nat{idx < 2 * n} ->\n Lemma ((hs_next_lv #_ #f #n hs).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f hs.[idx] hs.[idx + 1]\n else padded_hash_fun #_ f hs.[idx - 1] hs.[idx]))\nlet rec hs_next_lv_get #hsz #f #n hs idx =\n if idx < 2 then ()\n else hs_next_lv_get #_ #f #(n-1) (S.slice hs 2 (S.length hs)) (idx - 2)\n\nval mt_next_lv_get:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos ->\n mt:merkle_tree #hsz n -> idx:nat{idx < pow2 n} ->\n Lemma (\n (mt_next_lv #_ #f mt).[idx / 2] ==\n (if idx % 2 = 0\n then padded_hash_fun #_ f mt.[idx] mt.[idx + 1]\n else padded_hash_fun #_ f mt.[idx - 1] mt.[idx]))\nlet mt_next_lv_get #hsz #f #n mt idx =\n hs_next_lv_get #_ #f #(pow2 (n-1)) mt idx\n\nval mt_get_path_ok_:\n #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat ->\n t:merkle_tree #hsz n -> i:nat{i < pow2 n} ->\n Lemma (mt_verify_ #_ #f (mt_get_path #_ #f t i) i (mt_get t i) == mt_get_root #_ #f t)\nlet rec mt_get_path_ok_ #hsz #f #n mt idx =\n if n = 0 then ()\n else begin\n assert (S.head (mt_get_path #_ #f mt idx) ==\n (if idx % 2 = 0 then mt.[idx + 1] else mt.[idx - 1]));\n assert (S.equal (S.tail (mt_get_path #_ #f mt idx))\n (mt_get_path #_ #f (mt_next_lv #_ #f mt) (idx / 2)));\n mt_get_path_ok_ #_ #f (mt_next_lv #_ #f mt) (idx / 2);\n mt_next_lv_get #_ #f mt idx\n end\n\n\n/// Security: we reduce tree collisions to collisions on the hash\n/// compression function. Such collisions yield collisions on the SHA2\n/// standard (by adding the same length and padding to the\n/// accumulators).\n///\n/// One complication addressed in the proof is the handling of\n/// implicit padding.\n\n/// All hashes in a sequence are raw hashes, not padding\nval raw_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Tot Type0 (decreases (S.length hs))\nlet rec raw_hashes #hsz #f hs =\n if S.length hs = 0 then True\n else (HRaw? (S.head hs) /\\ raw_hashes #_ #f (S.tail hs))\n\nval raw_hashes_raws:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes{raw_hashes #hsz #f hs} ->\n Tot (S.seq (hash #hsz)) (decreases (S.length hs))\nlet rec raw_hashes_raws #hsz #f hs =\n if S.length hs = 0 then S.empty\n else S.cons (HRaw?.hr (S.head hs)) (raw_hashes_raws #_ #f (S.tail hs))\n\nval raw_hashes_index:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat{i < S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures HRaw? #hsz hs.[i])\n (decreases i)\nlet rec raw_hashes_index #hsz #f hs i =\n if i = 0 then ()\n else raw_hashes_index #_ #f (S.tail hs) (i - 1)\n\nval raw_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires raw_hashes #_ #f hs)\n (ensures raw_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec raw_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else (\n raw_hashes_index #_ #f hs i;\n raw_hashes_slice #_ #f hs (i + 1) j)\n\n/// All hashes in a sequence are just padding\nval pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes #hsz -> Type0\nlet pad_hashes #hsz #f hs =\n S.equal hs (S.create (S.length hs) HPad)\n\nval pad_hashes_slice:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n hs:hashes -> i:nat -> j:nat{i <= j && j <= S.length hs} ->\n Lemma (requires pad_hashes #_ #f hs)\n (ensures pad_hashes #_ #f (S.slice hs i j))\n (decreases (j - i))\nlet rec pad_hashes_slice #hsz #f hs i j =\n if i = j then ()\n else pad_hashes_slice #_ #f hs (i + 1) j\n\n/// Right-padded Merkle tree, a tree refinement\n\nlet rpmt (#hsz:pos) (#f:hash_fun_t) (n:nat) (i:nat{i <= pow2 n}) =\n mt:merkle_tree #hsz n {\n raw_hashes #_ #f (S.slice mt 0 i) /\\\n pad_hashes #_ #f (S.slice mt i (S.length mt)) }\n\nval rpmt_raws: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #hsz #f n i -> S.seq (hash #hsz)\nlet rpmt_raws #hsz #f #n #i mt = raw_hashes_raws #_ #f (S.slice mt 0 i)\n\nval rpmt_i_0: #hsz:pos -> #f:hash_fun_t #hsz -> #n:nat -> mt:rpmt #hsz #f n 0 ->\n Lemma (S.equal mt (S.create (pow2 n) (HPad #hsz)))\nlet rpmt_i_0 #hsz #f #n mt = ()\n\nval rpmt_left: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #hsz #f (n-1) (if i <= pow2 (n-1) then i else pow2 (n-1))\nlet rpmt_left #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) 0 (pow2 (n-1) - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) 0 (pow2 (n-1));\n mt_left mt\n\n#push-options \"--z3rlimit 40\"\n\nval rpmt_right: #hsz:pos -> #f:hash_fun_t #hsz -> #n:pos -> #i:nat{i <= pow2 n} -> rpmt #hsz #f n i ->\n rpmt #_ #f (n-1) (if i <= pow2 (n-1) then 0 else i - pow2 (n-1))\nlet rpmt_right #hsz #f #n #i mt =\n if i <= pow2 (n-1)\n then pad_hashes_slice #_ #f (S.slice mt i (S.length mt)) (pow2 (n-1) - i) (pow2 n - i)\n else raw_hashes_slice #_ #f (S.slice mt 0 i) (pow2 (n-1)) i;\n mt_right mt\n\n/// Two right-padded Merkle trees collide when\n/// 1) they have the same height (`n`) and number of raw hashes (`i`),\n/// 2) their contents differ, and\n/// 3) their roots are same.\n\n// fournet: we may want to work towards removing 1) using a hash prefix\nnoeq\ntype mt_collide (#hsz:pos) (#f:hash_fun_t #hsz) (n:nat) (i:nat{i <= pow2 n}) = | Collision:\n mt1:rpmt #_ #f n i -> mt2:rpmt #_ #f n i {\n mt1 =!= mt2 /\\\n mt_get_root #_ #f #_ mt1 == mt_get_root #_ #f #_ mt2 } -> mt_collide #_ #f n i\n\nnoeq\ntype hash2_raw_collide = | Collision2:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n lh1:hash -> rh1:hash ->\n lh2:hash -> rh2:hash {\n (lh1 =!= lh2 \\/ rh1 =!= rh2) /\\\n f lh1 rh1 == f lh2 rh2 } -> hash2_raw_collide\n\n/// Auxiliary lemmas for the proof\n\nval rpmt_pad_hashes_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> pad_hashes #_ #f mt )\nlet rpmt_pad_hashes_0 #_ #_ #n #i mt = ()\n\nval rpmt_pad_hashes_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} ->\n mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? mt.[0])\nlet rpmt_pad_hashes_index_0 #_ #_ #n #i mt = ()\n\nval mt_get_root_pad_index_0:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> mt:merkle_tree #hsz n ->\n Lemma (HPad? mt.[0] <==> HPad? (mt_get_root #_ #f mt))\nlet rec mt_get_root_pad_index_0 #hsz #f #n (mt:merkle_tree #hsz n) =\n if n = 0 then ()\n else\n let mt:merkle_tree #hsz (n-1) = mt_next_lv #_ #f #n mt in\n mt_get_root_pad_index_0 #_ #f #(n-1) mt\n\n#pop-options\n\nval rpmt_get_root_pad_hashes:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (pad_hashes #_ #f mt <==> HPad? (mt_get_root #_ #f mt))\nlet rpmt_get_root_pad_hashes #_ #f #n #i mt =\n rpmt_pad_hashes_index_0 #_ #f mt;\n mt_get_root_pad_index_0 #_ #f mt\n\nval rpmt_get_root_pad:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i = 0 <==> HPad? (mt_get_root #_ #f mt))\nlet rpmt_get_root_pad #_ #f #n #i mt =\n rpmt_get_root_pad_hashes #_ #f mt;\n rpmt_pad_hashes_0 #_ #f mt\n\nval rpmt_get_root_raw:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n #n:nat -> #i:nat{i <= pow2 n} -> mt:rpmt #_ #f n i ->\n Lemma (i > 0 <==> HRaw? (mt_get_root #_ #f mt))\nlet rpmt_get_root_raw #hsz #f #n #i mt =\n allow_inversion (padded_hash #hsz);\n rpmt_get_root_pad #_ #f mt\n\n#push-options \"--z3rlimit 100\"\n\nval extract:\n #hsz:pos -> #f:hash_fun_t #hsz ->\n\nPartial implementation:\nlet rec extract #hsz #f #n #i (Collision t1 t2) =\n \n"
},
{
"role": "assistant",
"content": "assert (n = 0 ==> S.equal t1 t2);\nmt_left_right t1;\nmt_left_right t2;\nmt_get_root_step #_ #f t1;\nmt_get_root_step #_ #f t2;\nrpmt_get_root_pad t1;\nassert (i <> 0);\nlet l1 = rpmt_left t1 in\nlet l2 = rpmt_left t2 in\nlet r1 = rpmt_right t1 in\nlet r2 = rpmt_right t2 in\nif i <= pow2 (n - 1)\nthen\n (rpmt_get_root_pad r1;\n rpmt_get_root_pad r2;\n rpmt_i_0 #_ #f r1;\n rpmt_i_0 #_ #f r2;\n extract (Collision l1 l2))\nelse\n (rpmt_get_root_raw l1;\n rpmt_get_root_raw l2;\n rpmt_get_root_raw r1;\n rpmt_get_root_raw r2;\n let HRaw lh1 = mt_get_root #_ #f l1 in\n let HRaw lh2 = mt_get_root #_ #f l2 in\n let HRaw rh1 = mt_get_root #_ #f r1 in\n let HRaw rh2 = mt_get_root #_ #f r2 in\n if\n StrongExcludedMiddle.strong_excluded_middle (lh1 =!= lh2) ||\n StrongExcludedMiddle.strong_excluded_middle (rh1 =!= rh2)\n then Collision2 #_ #f lh1 rh1 lh2 rh2\n else\n if StrongExcludedMiddle.strong_excluded_middle (l1 == l2)\n then extract (Collision r1 r2)\n else extract (Collision l1 l2))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n a: Prims.nat ->\n b: Vale.Def.Types_s.nat64 ->\n a0: Vale.Def.Types_s.nat64 ->\n a1: Vale.Def.Types_s.nat64 ->\n a2: Vale.Def.Types_s.nat64 ->\n a3: Vale.Def.Types_s.nat64 ->\n ba0_hi: Vale.Def.Types_s.nat64 ->\n ba0_lo: Vale.Def.Types_s.nat64 ->\n ba1_hi: Vale.Def.Types_s.nat64 ->\n ba1_lo: Vale.Def.Types_s.nat64 ->\n ba2_hi: Vale.Def.Types_s.nat64 ->\n ba2_lo: Vale.Def.Types_s.nat64 ->\n ba3_hi: Vale.Def.Types_s.nat64 ->\n ba3_lo: Vale.Def.Types_s.nat64 ->\n s1: Vale.Def.Types_s.nat64 ->\n s2: Vale.Def.Types_s.nat64 ->\n s3: Vale.Def.Types_s.nat64 ->\n s4: Vale.Def.Types_s.nat64\n -> FStar.Pervasives.Lemma\n (requires\n a = Vale.Curve25519.Fast_defs.pow2_four a0 a1 a2 a3 /\\\n Vale.Def.Words_s.pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n Vale.Def.Words_s.pow2_64 * ba1_hi + ba1_lo == b * a1 /\\\n Vale.Def.Words_s.pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n Vale.Def.Words_s.pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n (let _ = Vale.Curve25519.Fast_defs.add_carry ba1_lo ba0_hi 0 in\n (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' c1 = _ in\n let _ = Vale.Curve25519.Fast_defs.add_carry ba2_lo ba1_hi c1 in\n (let FStar.Pervasives.Native.Mktuple2 #_ #_ s2' c2 = _ in\n let _ = Vale.Curve25519.Fast_defs.add_carry ba3_lo ba2_hi c2 in\n (let FStar.Pervasives.Native.Mktuple2 #_ #_ s3' c3 = _ in\n let _ = Vale.Curve25519.Fast_defs.add_carry ba3_hi 0 c3 in\n (let FStar.Pervasives.Native.Mktuple2 #_ #_ s4' c4 = _ in\n s1 == s1' /\\ s2 == s2' /\\ s3 == s3' /\\ s4 == s4' /\\ c4 == 0)\n <:\n Prims.logical)\n <:\n Prims.logical)\n <:\n Prims.logical)\n <:\n Prims.logical)) (ensures Vale.Curve25519.Fast_defs.pow2_five ba0_lo s1 s2 s3 s4 == a * b)\nEffect: FStar.Pervasives.Lemma\n\nContext:\nmodule Vale.Curve25519.FastHybrid_helpers\n\nopen Vale.Def.Words_s\nopen Vale.Def.Types_s\nopen FStar.Mul\nopen FStar.Tactics\nopen FStar.Tactics.CanonCommSemiring\nopen Vale.Curve25519.Fast_defs\nopen Vale.Curve25519.Fast_lemmas_internal\n\n#reset-options \"--max_fuel 0 --max_ifuel 0 --using_facts_from '* -FStar.Tactics -FStar.Reflection'\"\n\nlet lemma_carry_prime (a0 a1 a2 a3 a0' a1' a2' a3' carry_in:nat64) (carry:bit) : Lemma\n (requires pow2_five a0' a1' a2' a3' carry == pow2_four a0 a1 a2 a3 + carry_in * 38 /\\\n carry_in * 38 - 1 + 38 < pow2_64)\n (ensures a0' + carry * 38 < pow2_64 /\\\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime == (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime)\n =\n assert (a0' + carry * 38 < pow2_64);\n\n calc (==) {\n (pow2_four a0 a1 a2 a3 + carry_in * pow2_256) % prime;\n == { lemma_mul_pow256_add (pow2_four a0 a1 a2 a3) carry_in }\n (pow2_four a0 a1 a2 a3 + carry_in * 38) % prime;\n == {}\n (pow2_five a0' a1' a2' a3' carry) % prime;\n == { _ by (int_canon()) }\n (pow2_four a0' a1' a2' a3' + (carry * pow2_256)) % prime;\n == { lemma_mul_pow256_add (pow2_four a0' a1' a2' a3') carry }\n (pow2_four a0' a1' a2' a3' + (carry * 38)) % prime;\n == { calc (==) {\n (pow2_four a0' a1' a2' a3') + (carry * 38);\n == { _ by (int_canon()) }\n pow2_four (a0' + carry * 38) a1' a2' a3';\n }\n }\n (pow2_four (a0' + carry * 38) a1' a2' a3') % prime;\n };\n ()\n\nPartial implementation:\nlet lemma_fast_mul1\n (a: nat)\n (b a0 a1 a2 a3 ba0_hi ba0_lo ba1_hi ba1_lo ba2_hi ba2_lo ba3_hi ba3_lo s1 s2 s3 s4: nat64)\n : Lemma\n (requires\n a = pow2_four a0 a1 a2 a3 /\\ pow2_64 * ba0_hi + ba0_lo == b * a0 /\\\n pow2_64 * ba1_hi + ba1_lo == b * a1 /\\ pow2_64 * ba2_hi + ba2_lo == b * a2 /\\\n pow2_64 * ba3_hi + ba3_lo == b * a3 /\\\n (let s1', c1 = add_carry ba1_lo ba0_hi 0 in\n let s2', c2 = add_carry ba2_lo ba1_hi c1 in\n let s3', c3 = add_carry ba3_lo ba2_hi c2 in\n let s4', c4 = add_carry ba3_hi 0 c3 in\n s1 == s1' /\\ s2 == s2' /\\ s3 == s3' /\\ s4 == s4' /\\ c4 == 0))\n (ensures pow2_five ba0_lo s1 s2 s3 s4 == a * b) =\n \n"
},
{
"role": "assistant",
"content": "assert_by_tactic (b * pow2_four a0 a1 a2 a3 == pow2_four (b * a0) (b * a1) (b * a2) (b * a3))\n int_canon;\n()"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n max: Prims.nat{max < Vale.SHA.PPC64LE.SHA_helpers.size_k_w_256} ->\n block: Vale.SHA.PPC64LE.SHA_helpers.block_w ->\n hash: Vale.SHA.PPC64LE.SHA_helpers.hash256\n -> Vale.SHA.PPC64LE.SHA_helpers.hash256\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\nPartial implementation:\nlet repeat_range_vale (max: nat{max < size_k_w_256}) (block: block_w) (hash: hash256) =\n \n"
},
{
"role": "assistant",
"content": "Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\nPartial implementation:\nlet bytes_blocks =\n \n"
},
{
"role": "assistant",
"content": "l: bytes{Seq.length l % block_length = 0}"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: x: Vale.Interop.Base.normal a -> a\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\nPartial implementation:\nlet as_t (#a: Type) (x: normal a) : a =\n \n"
},
{
"role": "assistant",
"content": "x"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u10: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n e: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.V2.Data.var\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nPartial implementation:\nlet return_post_with_eq (u: universe) (a e p: term) (x: var) : term =\n \n"
},
{
"role": "assistant",
"content": "let x_tm = RT.var_as_term x in\nlet eq2_tm = mk_eq2 u a x_tm e in\nlet p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in\nlet star_tm = mk_star p_app_x (mk_pure eq2_tm) in\nmk_abs a Q_Explicit (RT.subst_term star_tm [RT.ND x 0])"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u20: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n e: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.V2.Data.var\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nPartial implementation:\nlet return_stt_comp (u: universe) (a e p: term) (x: var) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_stt_comp u a (pack_ln (Tv_App p (e, Q_Explicit))) (return_post_with_eq u a e p x)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nPartial implementation:\nlet neutral_fv =\n \n"
},
{
"role": "assistant",
"content": "pack_ln (Tv_FVar (pack_fv neutral_lid))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: hash: Vale.SHA.PPC64LE.SHA_helpers.hash256 -> blocks: Vale.SHA.PPC64LE.SHA_helpers.bytes\n -> Vale.SHA.PPC64LE.SHA_helpers.hash256\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n\nPartial implementation:\nlet update_multi_opaque_vale (hash: hash256) (blocks: bytes) : hash256 =\n \n"
},
{
"role": "assistant",
"content": "if length blocks % size_k_w_256 = 0\nthen\n let b:bytes_blocks = blocks in\n update_multi_opaque hash b\nelse hash"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u37: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nPartial implementation:\nlet return_stt_noeq_comp (u: universe) (a x p: term) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: ks: FStar.Seq.Base.seq Vale.Def.Types_s.quad32 -> i: Prims.nat -> Vale.Def.Words_s.nat32\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_0_partial = opaque_make sigma_1_0_partial_def\nirreducible let sigma_1_0_partial_reveal = opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def\n\nval lemma_sha256_sigma2 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[0]))\n (ensures (sigma256_1_0 src.hi3 == sigma_1_0_partial t block hash_orig))\n\nval sigma_1_1_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_1_partial = opaque_make sigma_1_1_partial_def\nirreducible let sigma_1_1_partial_reveal = opaque_revealer (`%sigma_1_1_partial) sigma_1_1_partial sigma_1_1_partial_def\n\nval lemma_sha256_sigma3 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[4]))\n (ensures (sigma256_1_1 src.hi3 == sigma_1_1_partial t block hash_orig))\n\nval make_seperated_hash (a b c d e f g h:nat32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a /\\\n hash.[1] == nat32_to_word b /\\\n hash.[2] == nat32_to_word c /\\\n hash.[3] == nat32_to_word d /\\\n hash.[4] == nat32_to_word e /\\\n hash.[5] == nat32_to_word f /\\\n hash.[6] == nat32_to_word g /\\\n hash.[7] == nat32_to_word h\n )\n\nval make_seperated_hash_quad32 (a b c d e f g h:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a.hi3 /\\\n hash.[1] == nat32_to_word b.hi3 /\\\n hash.[2] == nat32_to_word c.hi3 /\\\n hash.[3] == nat32_to_word d.hi3 /\\\n hash.[4] == nat32_to_word e.hi3 /\\\n hash.[5] == nat32_to_word f.hi3 /\\\n hash.[6] == nat32_to_word g.hi3 /\\\n hash.[7] == nat32_to_word h.hi3\n )\n\nval lemma_make_seperated_hash (hash:hash256) (a b c d e f g h:quad32) : Lemma\n (requires length hash == 8 /\\\n a.hi3 == word_to_nat32 hash.[0] /\\\n b.hi3 == word_to_nat32 hash.[1] /\\\n c.hi3 == word_to_nat32 hash.[2] /\\\n d.hi3 == word_to_nat32 hash.[3] /\\\n e.hi3 == word_to_nat32 hash.[4] /\\\n f.hi3 == word_to_nat32 hash.[5] /\\\n g.hi3 == word_to_nat32 hash.[6] /\\\n h.hi3 == word_to_nat32 hash.[7])\n (ensures hash == make_seperated_hash_quad32 a b c d e f g h)\n\nval lemma_vsel32 (a b c:nat32) : Lemma\n (ensures (isel32 a b c = (iand32 c a) *^ (iand32 (inot32 c) b)))\n\nval ch_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ (iand32 (inot32 x) z))\n\nval lemma_eq_maj_xvsel32 (a b c:nat32) : Lemma\n (ensures (isel32 c b (a *^ b) = (iand32 a b) *^ ((iand32 a c) *^ (iand32 b c))))\n\nval maj_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ ((iand32 x z) *^ (iand32 y z)))\n\nval lemma_sigma_0_0_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_0 (ws_opaque block (t-15)) == sigma_0_0_partial t block))\n\nval lemma_sigma_0_1_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_1 (ws_opaque block (t-2)) == sigma_0_1_partial t block))\n\nval lemma_sigma_1_0_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_0 (word_to_nat32 ((repeat_range_vale t block hash_orig).[0])) == sigma_1_0_partial t block hash_orig))\n\nval lemma_sigma_1_1_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_1 (word_to_nat32 ((repeat_range_vale t block hash_orig).[4])) == sigma_1_1_partial t block hash_orig))\n\n(* Abbreviations and lemmas for the code itself *)\nlet k_reqs (k_seq:seq quad32) : prop0 =\n length k_seq == size_k_w_256 / 4 /\\\n (forall i . {:pattern (index k_seq i)} 0 <= i /\\ i < (size_k_w_256/4) ==>\n (k_seq.[i]).lo0 == word_to_nat32 (k.[4 * i]) /\\\n (k_seq.[i]).lo1 == word_to_nat32 (k.[4 * i + 1]) /\\\n (k_seq.[i]).hi2 == word_to_nat32 (k.[4 * i + 2]) /\\\n (k_seq.[i]).hi3 == word_to_nat32 (k.[4 * i + 3]))\n\nlet quads_to_block_be (qs:seq quad32) : block_w\n =\n let nat32_seq = Vale.Def.Words.Seq_s.seq_four_to_seq_BE qs in\n let f (n:nat{n < 16}) : word = nat32_to_word (if n < length nat32_seq then nat32_seq.[n] else 0) in\n init 16 f\n\nval lemma_quads_to_block_be (qs:seq quad32) : Lemma\n (requires length qs == 4)\n (ensures\n (let block = quads_to_block_be qs in\n forall i . {:pattern (index qs i)} 0 <= i /\\ i < 4 ==>\n (qs.[i]).hi3 == ws_opaque block (4 * i + 0) /\\\n (qs.[i]).hi2 == ws_opaque block (4 * i + 1) /\\\n (qs.[i]).lo1 == ws_opaque block (4 * i + 2) /\\\n (qs.[i]).lo0 == ws_opaque block (4 * i + 3)))\n\nPartial implementation:\nlet k_index (ks: seq quad32) (i: nat) : nat32 =\n \n"
},
{
"role": "assistant",
"content": "if length ks = size_k_w_256 / 4 && i < size_k_w_256 then four_select ks.[ (i / 4) ] (i % 4) else 0"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u55: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n e: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.V2.Data.var\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nPartial implementation:\nlet return_stt_atomic_comp (u: universe) (a e p: term) (x: var) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_stt_atomic_comp neutral_fv\n u\n a\n emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: i: Vale.Interop.X64.reg_nat 3 -> Vale.X64.Machine_s.reg_64\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nlet of_reg (r:MS.reg_64) : option (IX64.reg_nat 3) = match r with\n | 5 -> Some 0 // rdi\n | 4 -> Some 1 // rsi\n | 3 -> Some 2 // rdx\n | _ -> None\n\nPartial implementation:\nlet of_arg (i: IX64.reg_nat 3) : MS.reg_64 =\n \n"
},
{
"role": "assistant",
"content": "match i with\n| 0 -> MS.rRdi\n| 1 -> MS.rRsi\n| 2 -> MS.rRdx"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n post: FStar.Stubs.Reflection.Types.term ->\n a: FStar.Stubs.Reflection.Types.term ->\n ret_t: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.Types.term\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nlet neutral_fv = pack_ln (Tv_FVar (pack_fv neutral_lid))\nlet return_stt_atomic_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_atomic_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return u a e p)\n (return_stt_atomic_comp u a e p x))\n\nlet return_stt_atomic_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_atomic_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return_noeq u a x p)\n (return_stt_atomic_noeq_comp u a x p))\n\nlet return_stt_ghost_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_ghost_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_ghost_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.ghost_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return u a e p)\n (return_stt_ghost_comp u a e p x))\n\nlet return_stt_ghost_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_ghost_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_ghost_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.ghost_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return_noeq u a x p)\n (return_stt_ghost_noeq_comp u a x p))\n\n\n(*\n\n g |- inv : bool -> vprop\n g |- cond : stt<0> bool (exists_ inv) inv\n g |- body : stt<0> unit (inv true) (fun _ -> exists_ inv)\n -------------------------------------------------------------------------\n g |- while inv cond body : stt<0> unit (exists_ inv) (fun _ -> inv false)\n\n*)\n\nval while_typing\n (#g:env)\n (#inv:term)\n (#cond:term)\n (#body:term)\n (inv_typing:RT.tot_typing g\n inv\n (mk_arrow (bool_tm, Q_Explicit) vprop_tm))\n (cond_typing:RT.tot_typing g\n cond\n (mk_stt_comp uzero bool_tm (mk_exists uzero bool_tm inv) inv))\n (body_typing:RT.tot_typing g\n body\n (mk_stt_comp uzero unit_tm\n (pack_ln (Tv_App inv (true_tm, Q_Explicit)))\n (mk_abs unit_tm Q_Explicit (mk_exists uzero bool_tm inv))))\n : RT.tot_typing g\n (mk_while inv cond body)\n (mk_stt_comp uzero unit_tm (mk_exists uzero bool_tm inv)\n (mk_abs unit_tm Q_Explicit (pack_ln (Tv_App inv (false_tm, Q_Explicit)))))\n\nlet par_post (u:universe) (aL aR:term) (postL postR:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let postL = pack_ln (Tv_App postL (mk_fst u u aL aR x_tm, Q_Explicit)) in\n let postR = pack_ln (Tv_App postR (mk_snd u u aL aR x_tm, Q_Explicit)) in\n let post = mk_star postL postR in\n RT.subst_term post [ RT.ND x 0 ]\n\nval par_typing\n (#g:env)\n (#u:universe)\n (#aL #aR:term)\n (#preL #postL:term)\n (#preR #postR:term)\n (#eL #eR:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (aL_typing:RT.tot_typing g aL (pack_ln (Tv_Type u)))\n (aR_typing:RT.tot_typing g aR (pack_ln (Tv_Type u)))\n (preL_typing:RT.tot_typing g preL vprop_tm)\n (postL_typing:RT.tot_typing g postL (mk_arrow (aL, Q_Explicit) vprop_tm))\n (preR_typing:RT.tot_typing g preR vprop_tm)\n (postR_typing:RT.tot_typing g postR (mk_arrow (aR, Q_Explicit) vprop_tm))\n (eL_typing:RT.tot_typing g eL (mk_stt_comp u aL preL postL))\n (eR_typing:RT.tot_typing g eR (mk_stt_comp u aR preR postR))\n\n : GTot (RT.tot_typing g\n (mk_par u aL aR preL postL preR postR eL eR)\n (mk_stt_comp u (mk_tuple2 u u aL aR)\n (mk_star preL preR)\n (mk_abs (mk_tuple2 u u aL aR) Q_Explicit (par_post u aL aR postL postR x))))\n\nval exists_inversion\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (e_typing:RT.tot_typing g\n (mk_exists u a p)\n vprop_tm)\n : GTot (RT.tot_typing g\n p\n (mk_arrow (a, Q_Explicit) vprop_tm))\n\n(*\n\n g |- a : Type u\n g |- p : a -> vprop\n ----------------------------------------------------------------\n g |- elim_exists<u> #a p : stt_ghost<u> a empty (exists_<u> p) (fun x -> p (reveal x))\n\n*)\n\nlet elim_exists_post_body (u:universe) (a:term) (p:term) (x:var) =\n let x_tm = RT.var_as_term x in\n let reveal_x = mk_reveal u a x_tm in\n let post = pack_ln (Tv_App p (reveal_x, Q_Explicit)) in\n RT.subst_term post [ RT.ND x 0 ]\n\nlet elim_exists_post (u:universe) (a:term) (p:term) (x:var) =\n let erased_a = mk_erased u a in\n mk_abs erased_a Q_Explicit (elim_exists_post_body u a p x)\n\nval elim_exists_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n : GTot (RT.tot_typing g\n (mk_elim_exists u a p)\n (mk_stt_ghost_comp\n u\n (mk_erased u a)\n (mk_exists u a p)\n (elim_exists_post u a p x)))\n\n(*\n\n g |- a : Type u\n g |- p : a -> vprop\n g |- e : vprop\n -------------------------------------------------------------------------\n g |- intro_exists<u> #a p e : stt_ghost<0> unit empty (p e) (fun _ -> exists_ p)\n\n*)\nval intro_exists_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (#e:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n (e_typing:RT.ghost_typing g e a)\n : GTot (RT.tot_typing g\n (mk_intro_exists u a p e)\n (mk_stt_ghost_comp uzero unit_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (mk_abs unit_tm Q_Explicit (mk_exists u a p))))\n\n(*\n\n g |- a : Type u\n g |- p : vprop\n g |- q : a -> vprop\n ------------------------------------------\n g |- stt_admit a p q : stt a p q\n\n*)\n\nval stt_admit_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (#q:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (p_typing:RT.tot_typing g p vprop_tm)\n (q_typing:RT.tot_typing g q (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_admit u a p q)\n (mk_stt_comp u a p q))\n \nval stt_atomic_admit_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (#q:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (p_typing:RT.tot_typing g p vprop_tm)\n (q_typing:RT.tot_typing g q (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_admit u a p q)\n (mk_stt_atomic_comp neutral_fv u a emp_inames_tm p q))\n\nval stt_ghost_admit_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (#q:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (p_typing:RT.tot_typing g p vprop_tm)\n (q_typing:RT.tot_typing g q (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_admit u a p q)\n (mk_stt_ghost_comp u a p q))\n\nval rewrite_typing\n (#g:env)\n (#p:term)\n (#q:term)\n (p_typing:RT.tot_typing g p vprop_tm)\n (q_typing:RT.tot_typing g q vprop_tm)\n (equiv:RT.tot_typing g (`()) (stt_vprop_equiv p q))\n\n\t\t: GTot (RT.tot_typing g\n (mk_rewrite p q)\n (mk_stt_ghost_comp\n uzero\n unit_tm\n p\n (mk_abs unit_tm Q_Explicit q)))\n\n// mk_star pre (mk_pts_to a (RT.bound_var 0) full_perm_tm init\nlet with_local_body_pre (pre:term) (a:term) (x:term) (init:term) : term =\n let pts_to : term =\n mk_pts_to a x full_perm_tm init in\n mk_star pre pts_to\n\n//\n// post has 0 db index free\n//\nlet with_local_body_post_body (post:term) (a:term) (x:term) : term =\n // exists_ (R.pts_to r full_perm)\n let exists_tm =\n mk_exists (pack_universe Uv_Zero) a\n (mk_abs a Q_Explicit\n (mk_pts_to a x full_perm_tm (RT.bound_var 0))) in\n mk_star post exists_tm\n\nPartial implementation:\nlet with_local_body_post (post a ret_t x: term) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_abs ret_t Q_Explicit (with_local_body_post_body post a x)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u134: FStar.Stubs.Reflection.Types.universe ->\n aL: FStar.Stubs.Reflection.Types.term ->\n aR: FStar.Stubs.Reflection.Types.term ->\n postL: FStar.Stubs.Reflection.Types.term ->\n postR: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.V2.Data.var\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nlet neutral_fv = pack_ln (Tv_FVar (pack_fv neutral_lid))\nlet return_stt_atomic_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_atomic_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return u a e p)\n (return_stt_atomic_comp u a e p x))\n\nlet return_stt_atomic_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_atomic_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return_noeq u a x p)\n (return_stt_atomic_noeq_comp u a x p))\n\nlet return_stt_ghost_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_ghost_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_ghost_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.ghost_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return u a e p)\n (return_stt_ghost_comp u a e p x))\n\nlet return_stt_ghost_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_ghost_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_ghost_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.ghost_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return_noeq u a x p)\n (return_stt_ghost_noeq_comp u a x p))\n\n\n(*\n\n g |- inv : bool -> vprop\n g |- cond : stt<0> bool (exists_ inv) inv\n g |- body : stt<0> unit (inv true) (fun _ -> exists_ inv)\n -------------------------------------------------------------------------\n g |- while inv cond body : stt<0> unit (exists_ inv) (fun _ -> inv false)\n\n*)\n\nval while_typing\n (#g:env)\n (#inv:term)\n (#cond:term)\n (#body:term)\n (inv_typing:RT.tot_typing g\n inv\n (mk_arrow (bool_tm, Q_Explicit) vprop_tm))\n (cond_typing:RT.tot_typing g\n cond\n (mk_stt_comp uzero bool_tm (mk_exists uzero bool_tm inv) inv))\n (body_typing:RT.tot_typing g\n body\n (mk_stt_comp uzero unit_tm\n (pack_ln (Tv_App inv (true_tm, Q_Explicit)))\n (mk_abs unit_tm Q_Explicit (mk_exists uzero bool_tm inv))))\n : RT.tot_typing g\n (mk_while inv cond body)\n (mk_stt_comp uzero unit_tm (mk_exists uzero bool_tm inv)\n (mk_abs unit_tm Q_Explicit (pack_ln (Tv_App inv (false_tm, Q_Explicit)))))\n\nPartial implementation:\nlet par_post (u: universe) (aL aR postL postR: term) (x: var) : term =\n \n"
},
{
"role": "assistant",
"content": "let x_tm = RT.var_as_term x in\nlet postL = pack_ln (Tv_App postL (mk_fst u u aL aR x_tm, Q_Explicit)) in\nlet postR = pack_ln (Tv_App postR (mk_snd u u aL aR x_tm, Q_Explicit)) in\nlet post = mk_star postL postR in\nRT.subst_term post [RT.ND x 0]"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u72: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nlet neutral_fv = pack_ln (Tv_FVar (pack_fv neutral_lid))\nlet return_stt_atomic_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_atomic_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return u a e p)\n (return_stt_atomic_comp u a e p x))\n\nPartial implementation:\nlet return_stt_atomic_noeq_comp (u: universe) (a x p: term) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_stt_atomic_comp neutral_fv u a emp_inames_tm (pack_ln (Tv_App p (x, Q_Explicit))) p"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u107: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nlet neutral_fv = pack_ln (Tv_FVar (pack_fv neutral_lid))\nlet return_stt_atomic_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_atomic_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return u a e p)\n (return_stt_atomic_comp u a e p x))\n\nlet return_stt_atomic_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_atomic_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return_noeq u a x p)\n (return_stt_atomic_noeq_comp u a x p))\n\nlet return_stt_ghost_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_ghost_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_ghost_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.ghost_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return u a e p)\n (return_stt_ghost_comp u a e p x))\n\nPartial implementation:\nlet return_stt_ghost_noeq_comp (u: universe) (a x p: term) : term =\n \n"
},
{
"role": "assistant",
"content": "mk_stt_ghost_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: \n u174: FStar.Stubs.Reflection.Types.universe ->\n a: FStar.Stubs.Reflection.Types.term ->\n p: FStar.Stubs.Reflection.Types.term ->\n x: FStar.Stubs.Reflection.V2.Data.var\n -> FStar.Stubs.Reflection.Types.term\nEffect: Prims.Tot\n\nContext:\n(*\n Copyright 2023 Microsoft Research\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*)\n\nmodule Pulse.Steel.Wrapper.Typing\n\nopen FStar.Reflection.V2\nopen Pulse.Reflection.Util\n\nmodule RT = FStar.Reflection.Typing\n\nlet return_post_with_eq (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let eq2_tm = mk_eq2 u a x_tm e in\n let p_app_x = pack_ln (Tv_App p (x_tm, Q_Explicit)) in \n let star_tm = mk_star p_app_x (mk_pure eq2_tm) in\n mk_abs a Q_Explicit (RT.subst_term star_tm [ RT.ND x 0 ])\n\nlet return_stt_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return u a e p)\n (return_stt_comp u a e p x))\n\nlet return_stt_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_return_noeq u a x p)\n (return_stt_noeq_comp u a x p))\n\nlet neutral_fv = pack_ln (Tv_FVar (pack_fv neutral_lid))\nlet return_stt_atomic_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_atomic_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.tot_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return u a e p)\n (return_stt_atomic_comp u a e p x))\n\nlet return_stt_atomic_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_atomic_comp neutral_fv u a emp_inames_tm (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_atomic_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.tot_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_atomic_return_noeq u a x p)\n (return_stt_atomic_noeq_comp u a x p))\n\nlet return_stt_ghost_comp (u:universe) (a:term) (e:term) (p:term) (x:var) : term =\n mk_stt_ghost_comp u a\n (pack_ln (Tv_App p (e, Q_Explicit)))\n (return_post_with_eq u a e p x)\n\nval return_stt_ghost_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#e:term)\n (#p:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (e_typing:RT.ghost_typing g e a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return u a e p)\n (return_stt_ghost_comp u a e p x))\n\nlet return_stt_ghost_noeq_comp (u:universe) (a:term) (x:term) (p:term) : term =\n mk_stt_ghost_comp u a (pack_ln (Tv_App p (x, Q_Explicit))) p\n\nval return_stt_ghost_noeq_typing\n (#g:env)\n (#u:universe)\n (#a:term)\n (#x:term)\n (#p:term)\n (a_typing:RT.tot_typing g a (pack_ln (Tv_Type u)))\n (x_typing:RT.ghost_typing g x a)\n (p_typing:RT.tot_typing g p (mk_arrow (a, Q_Explicit) vprop_tm))\n\n : GTot (RT.tot_typing g\n (mk_stt_ghost_return_noeq u a x p)\n (return_stt_ghost_noeq_comp u a x p))\n\n\n(*\n\n g |- inv : bool -> vprop\n g |- cond : stt<0> bool (exists_ inv) inv\n g |- body : stt<0> unit (inv true) (fun _ -> exists_ inv)\n -------------------------------------------------------------------------\n g |- while inv cond body : stt<0> unit (exists_ inv) (fun _ -> inv false)\n\n*)\n\nval while_typing\n (#g:env)\n (#inv:term)\n (#cond:term)\n (#body:term)\n (inv_typing:RT.tot_typing g\n inv\n (mk_arrow (bool_tm, Q_Explicit) vprop_tm))\n (cond_typing:RT.tot_typing g\n cond\n (mk_stt_comp uzero bool_tm (mk_exists uzero bool_tm inv) inv))\n (body_typing:RT.tot_typing g\n body\n (mk_stt_comp uzero unit_tm\n (pack_ln (Tv_App inv (true_tm, Q_Explicit)))\n (mk_abs unit_tm Q_Explicit (mk_exists uzero bool_tm inv))))\n : RT.tot_typing g\n (mk_while inv cond body)\n (mk_stt_comp uzero unit_tm (mk_exists uzero bool_tm inv)\n (mk_abs unit_tm Q_Explicit (pack_ln (Tv_App inv (false_tm, Q_Explicit)))))\n\nlet par_post (u:universe) (aL aR:term) (postL postR:term) (x:var) : term =\n let x_tm = RT.var_as_term x in\n let postL = pack_ln (Tv_App postL (mk_fst u u aL aR x_tm, Q_Explicit)) in\n let postR = pack_ln (Tv_App postR (mk_snd u u aL aR x_tm, Q_Explicit)) in\n let post = mk_star postL postR in\n RT.subst_term post [ RT.ND x 0 ]\n\nval par_typing\n (#g:env)\n (#u:universe)\n (#aL #aR:term)\n (#preL #postL:term)\n (#preR #postR:term)\n (#eL #eR:term)\n (x:var{None? (RT.lookup_bvar g x)})\n (aL_typing:RT.tot_typing g aL (pack_ln (Tv_Type u)))\n (aR_typing:RT.tot_typing g aR (pack_ln (Tv_Type u)))\n (preL_typing:RT.tot_typing g preL vprop_tm)\n (postL_typing:RT.tot_typing g postL (mk_arrow (aL, Q_Explicit) vprop_tm))\n (preR_typing:RT.tot_typing g preR vprop_tm)\n (postR_typing:RT.tot_typing g postR (mk_arrow (aR, Q_Explicit) vprop_tm))\n (eL_typing:RT.tot_typing g eL (mk_stt_comp u aL preL postL))\n (eR_typing:RT.tot_typing g eR (mk_stt_comp u aR preR postR))\n\n : GTot (RT.tot_typing g\n (mk_par u aL aR preL postL preR postR eL eR)\n (mk_stt_comp u (mk_tuple2 u u aL aR)\n (mk_star preL preR)\n (mk_abs (mk_tuple2 u u aL aR) Q_Explicit (par_post u aL aR postL postR x))))\n\nval exists_inversion\n (#g:env)\n (#u:universe)\n (#a:term)\n (#p:term)\n (e_typing:RT.tot_typing g\n (mk_exists u a p)\n vprop_tm)\n : GTot (RT.tot_typing g\n p\n (mk_arrow (a, Q_Explicit) vprop_tm))\n\n(*\n\n g |- a : Type u\n g |- p : a -> vprop\n ----------------------------------------------------------------\n g |- elim_exists<u> #a p : stt_ghost<u> a empty (exists_<u> p) (fun x -> p (reveal x))\n\n*)\n\nlet elim_exists_post_body (u:universe) (a:term) (p:term) (x:var) =\n let x_tm = RT.var_as_term x in\n let reveal_x = mk_reveal u a x_tm in\n let post = pack_ln (Tv_App p (reveal_x, Q_Explicit)) in\n RT.subst_term post [ RT.ND x 0 ]\n\nPartial implementation:\nlet elim_exists_post (u: universe) (a p: term) (x: var) =\n \n"
},
{
"role": "assistant",
"content": "let erased_a = mk_erased u a in\nmk_abs erased_a Q_Explicit (elim_exists_post_body u a p x)"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: Type0\nEffect: Prims.Tot\n\nContext:\nmodule Vale.Inline.X64.Fswap_inline\n\nopen FStar.Mul\nopen FStar.HyperStack.ST\nmodule HS = FStar.HyperStack\nmodule B = LowStar.Buffer\nmodule DV = LowStar.BufferView.Down\nopen Vale.Def.Types_s\n\nopen Vale.Interop.Base\nmodule IX64 = Vale.Interop.X64\nmodule VSig = Vale.AsLowStar.ValeSig\nmodule LSig = Vale.AsLowStar.LowStarSig\nmodule ME = Vale.X64.Memory\nmodule V = Vale.X64.Decls\nmodule IA = Vale.Interop.Assumptions\nmodule W = Vale.AsLowStar.Wrapper\nopen Vale.X64.MemoryAdapters\nmodule VS = Vale.X64.State\nmodule MS = Vale.X64.Machine_s\nmodule PR = Vale.X64.Print_Inline_s\n\nmodule FU = Vale.Curve25519.X64.FastUtil\n\nlet uint64 = UInt64.t\n\n(* A little utility to trigger normalization in types *)\nlet as_t (#a:Type) (x:normal a) : a = x\nlet as_normal_t (#a:Type) (x:a) : normal a = x\n\n[@__reduce__]\nlet b64 = buf_t TUInt64 TUInt64\n[@__reduce__]\nlet t64_mod = TD_Buffer TUInt64 TUInt64 default_bq\n[@__reduce__]\nlet t64_no_mod = TD_Buffer TUInt64 TUInt64 ({modified=false; strict_disjointness=false; taint=MS.Secret})\n[@__reduce__]\nlet tuint64 = TD_Base TUInt64\n\n[@__reduce__]\nlet cswap_dom: IX64.arity_ok 3 td =\n let y = [tuint64; t64_mod; t64_mod] in\n assert_norm (List.length y = 3);\n y\n\n(* Need to rearrange the order of arguments *)\n[@__reduce__]\nlet cswap_pre : VSig.vale_pre cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state) ->\n FU.va_req_Cswap2 c va_s0\n (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1)\n\n[@__reduce__]\nlet cswap_post : VSig.vale_post cswap_dom =\n fun (c:V.va_code)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n (va_s1:V.va_state)\n (f:V.va_fuel) ->\n FU.va_ens_Cswap2 c va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) va_s1 f\n\n#set-options \"--z3rlimit 50\"\n\nlet cswap_regs_modified: MS.reg_64 -> bool = fun (r:MS.reg_64) ->\n let open MS in\n if r = rRdi || r = rR8 || r = rR9 || r = rR10 then true\n else false\n\nlet cswap_xmms_modified = fun _ -> false\n\n[@__reduce__]\nlet cswap_lemma'\n (code:V.va_code)\n (_win:bool)\n (bit:uint64)\n (p0:b64)\n (p1:b64)\n (va_s0:V.va_state)\n : Ghost (V.va_state & V.va_fuel)\n (requires\n cswap_pre code bit p0 p1 va_s0)\n (ensures (fun (va_s1, f) ->\n V.eval_code code va_s0 f va_s1 /\\\n VSig.vale_calling_conventions va_s0 va_s1 cswap_regs_modified cswap_xmms_modified /\\\n cswap_post code bit p0 p1 va_s0 va_s1 f /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p0) /\\\n ME.buffer_readable (VS.vs_get_vale_heap va_s1) (as_vale_buffer p1) /\\\n ME.buffer_writeable (as_vale_buffer p0) /\\\n ME.buffer_writeable (as_vale_buffer p1) /\\\n ME.modifies (ME.loc_union (ME.loc_buffer (as_vale_buffer p0))\n (ME.loc_union (ME.loc_buffer (as_vale_buffer p1))\n ME.loc_none)) (VS.vs_get_vale_heap va_s0) (VS.vs_get_vale_heap va_s1)\n )) =\n let va_s1, f = FU.va_lemma_Cswap2 code va_s0 (UInt64.v bit) (as_vale_buffer p0) (as_vale_buffer p1) in\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p0;\n Vale.AsLowStar.MemoryHelpers.buffer_writeable_reveal ME.TUInt64 ME.TUInt64 p1;\n (va_s1, f)\n\n(* Prove that cswap_lemma' has the required type *)\nlet cswap_lemma = as_t #(VSig.vale_sig cswap_regs_modified cswap_xmms_modified cswap_pre cswap_post) cswap_lemma'\n\nlet code_cswap = FU.va_code_Cswap2 ()\n\nlet of_reg (r:MS.reg_64) : option (IX64.reg_nat 3) = match r with\n | 5 -> Some 0 // rdi\n | 4 -> Some 1 // rsi\n | 3 -> Some 2 // rdx\n | _ -> None\n\nlet of_arg (i:IX64.reg_nat 3) : MS.reg_64 = match i with\n | 0 -> MS.rRdi\n | 1 -> MS.rRsi\n | 2 -> MS.rRdx\n\nlet arg_reg : IX64.arg_reg_relation 3 = IX64.Rel of_reg of_arg\n\n(* Here's the type expected for the cswap wrapper *)\n\nPartial implementation:\nlet lowstar_cswap_t =\n \n"
},
{
"role": "assistant",
"content": "assert_norm (List.length cswap_dom + List.length ([] <: list arg) <= 3);\nIX64.as_lowstar_sig_t_weak 3 arg_reg cswap_regs_modified cswap_xmms_modified code_cswap cswap_dom []\n _ _ (W.mk_prediction code_cswap cswap_dom [] (cswap_lemma code_cswap IA.win))"
}
] |
[
{
"role": "user",
"content": "Complete this F* function with a correct proof.\n\nType: s: FStar.Seq.Base.seq Vale.Def.Types_s.quad32 -> hash_orig: Vale.SHA.PPC64LE.SHA_helpers.hash256\n -> Prims.Tot Vale.SHA.PPC64LE.SHA_helpers.hash256\nEffect: Prims.Tot\n\nContext:\nmodule Vale.SHA.PPC64LE.SHA_helpers\n\nopen FStar.Mul\nopen Vale.Def.Prop_s\nopen Vale.Def.Opaque_s\nopen Vale.Def.Types_s\nopen Vale.Def.Words_s\nopen Vale.Def.Words.Seq_s\nopen FStar.Seq\nopen Vale.Arch.Types\nopen Vale.Def.Sel\nopen Vale.SHA2.Wrapper\nopen Vale.Def.Words.Four_s\n\nunfold let (.[]) = FStar.Seq.index\n\n#reset-options \"--max_fuel 0 --max_ifuel 0\"\n\n// Specialize these definitions (from Spec.SHA2.fst) for SHA256\nunfold let size_k_w_256 = 64\nval word:Type0\n(* Number of words for a block size *)\nlet size_block_w_256 = 16\n(* Define the size block in bytes *)\nlet block_length =\n 4 (*word_length a*) * size_block_w_256\nlet block_w = m:seq word {length m = size_block_w_256}\nlet counter = nat\nval k : (s:seq word {length s = size_k_w_256})\nlet hash256 = m:Seq.seq word {Seq.length m = 8}\n\n(* Input data. *)\ntype byte = UInt8.t\ntype bytes = Seq.seq byte\n\n(* Input data, multiple of a block length. *)\nlet bytes_blocks =\n l:bytes { Seq.length l % block_length = 0 }\n\n// Hide various SHA2 definitions\nval ws_opaque (b:block_w) (t:counter{t < size_k_w_256}):nat32\nval shuffle_core_opaque (block:block_w) (hash:hash256) (t:counter{t < size_k_w_256}):hash256\nval update_multi_opaque (hash:hash256) (blocks:bytes_blocks):hash256\nval update_multi_transparent (hash:hash256) (blocks:bytes_blocks):hash256\n\n// Hide some functions that operate on words & bytes\nval word_to_nat32 (x:word) : nat32\nval nat32_to_word (x:nat32) : word\n\n//unfold let bytes_blocks256 = bytes_blocks SHA2_256\nlet repeat_range_vale (max:nat { max < size_k_w_256}) (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 max (shuffle_core_opaque block) hash\nlet update_multi_opaque_vale (hash:hash256) (blocks:bytes) : hash256 =\n if length blocks % size_k_w_256 = 0 then let b:bytes_blocks = blocks in update_multi_opaque hash b else hash\n\nval make_ordered_hash (abcd efgh:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word abcd.lo0 /\\\n hash.[1] == nat32_to_word abcd.lo1 /\\\n hash.[2] == nat32_to_word abcd.hi2 /\\\n hash.[3] == nat32_to_word abcd.hi3 /\\\n hash.[4] == nat32_to_word efgh.lo0 /\\\n hash.[5] == nat32_to_word efgh.lo1 /\\\n hash.[6] == nat32_to_word efgh.hi2 /\\\n hash.[7] == nat32_to_word efgh.hi3\n )\n\nval update_block (hash:hash256) (block:block_w): hash256\n\nval lemma_update_multi_opaque_vale_is_update_multi (hash:hash256) (blocks:bytes) : Lemma\n (requires length blocks % 64 = 0)\n (ensures update_multi_opaque_vale hash blocks == update_multi_transparent hash blocks)\n\nval sigma_0_0_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_0_partial = opaque_make sigma_0_0_partial_def\nirreducible let sigma_0_0_partial_reveal = opaque_revealer (`%sigma_0_0_partial) sigma_0_0_partial sigma_0_0_partial_def\n\nval lemma_sha256_sigma0 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-15))\n (ensures (sigma256_0_0 src.hi3 == sigma_0_0_partial t block))\n\nval sigma_0_1_partial_def (t:counter) (block:block_w) : nat32\n[@\"opaque_to_smt\"] let sigma_0_1_partial = opaque_make sigma_0_1_partial_def\nirreducible let sigma_0_1_partial_reveal = opaque_revealer (`%sigma_0_1_partial) sigma_0_1_partial sigma_0_1_partial_def\n\nval lemma_sha256_sigma1 (src:quad32) (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256 /\\\n src.hi3 == ws_opaque block (t-2))\n (ensures (sigma256_0_1 src.hi3 == sigma_0_1_partial t block))\n\nval sigma_1_0_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_0_partial = opaque_make sigma_1_0_partial_def\nirreducible let sigma_1_0_partial_reveal = opaque_revealer (`%sigma_1_0_partial) sigma_1_0_partial sigma_1_0_partial_def\n\nval lemma_sha256_sigma2 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[0]))\n (ensures (sigma256_1_0 src.hi3 == sigma_1_0_partial t block hash_orig))\n\nval sigma_1_1_partial_def (t:counter) (block:block_w) (hash_orig:hash256) : nat32\n[@\"opaque_to_smt\"] let sigma_1_1_partial = opaque_make sigma_1_1_partial_def\nirreducible let sigma_1_1_partial_reveal = opaque_revealer (`%sigma_1_1_partial) sigma_1_1_partial sigma_1_1_partial_def\n\nval lemma_sha256_sigma3 (src:quad32) (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256 /\\\n src.hi3 == word_to_nat32 ((repeat_range_vale t block hash_orig).[4]))\n (ensures (sigma256_1_1 src.hi3 == sigma_1_1_partial t block hash_orig))\n\nval make_seperated_hash (a b c d e f g h:nat32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a /\\\n hash.[1] == nat32_to_word b /\\\n hash.[2] == nat32_to_word c /\\\n hash.[3] == nat32_to_word d /\\\n hash.[4] == nat32_to_word e /\\\n hash.[5] == nat32_to_word f /\\\n hash.[6] == nat32_to_word g /\\\n hash.[7] == nat32_to_word h\n )\n\nval make_seperated_hash_quad32 (a b c d e f g h:quad32): Pure (hash256)\n (requires True)\n (ensures fun hash ->\n length hash == 8 /\\\n hash.[0] == nat32_to_word a.hi3 /\\\n hash.[1] == nat32_to_word b.hi3 /\\\n hash.[2] == nat32_to_word c.hi3 /\\\n hash.[3] == nat32_to_word d.hi3 /\\\n hash.[4] == nat32_to_word e.hi3 /\\\n hash.[5] == nat32_to_word f.hi3 /\\\n hash.[6] == nat32_to_word g.hi3 /\\\n hash.[7] == nat32_to_word h.hi3\n )\n\nval lemma_make_seperated_hash (hash:hash256) (a b c d e f g h:quad32) : Lemma\n (requires length hash == 8 /\\\n a.hi3 == word_to_nat32 hash.[0] /\\\n b.hi3 == word_to_nat32 hash.[1] /\\\n c.hi3 == word_to_nat32 hash.[2] /\\\n d.hi3 == word_to_nat32 hash.[3] /\\\n e.hi3 == word_to_nat32 hash.[4] /\\\n f.hi3 == word_to_nat32 hash.[5] /\\\n g.hi3 == word_to_nat32 hash.[6] /\\\n h.hi3 == word_to_nat32 hash.[7])\n (ensures hash == make_seperated_hash_quad32 a b c d e f g h)\n\nval lemma_vsel32 (a b c:nat32) : Lemma\n (ensures (isel32 a b c = (iand32 c a) *^ (iand32 (inot32 c) b)))\n\nval ch_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ (iand32 (inot32 x) z))\n\nval lemma_eq_maj_xvsel32 (a b c:nat32) : Lemma\n (ensures (isel32 c b (a *^ b) = (iand32 a b) *^ ((iand32 a c) *^ (iand32 b c))))\n\nval maj_256 (x y z:nat32):Pure(nat32)\n (requires True)\n (ensures fun a -> a == (iand32 x y) *^ ((iand32 x z) *^ (iand32 y z)))\n\nval lemma_sigma_0_0_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_0 (ws_opaque block (t-15)) == sigma_0_0_partial t block))\n\nval lemma_sigma_0_1_partial (t:counter) (block:block_w) : Lemma\n (requires 16 <= t /\\ t < size_k_w_256)\n (ensures (sigma256_0_1 (ws_opaque block (t-2)) == sigma_0_1_partial t block))\n\nval lemma_sigma_1_0_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_0 (word_to_nat32 ((repeat_range_vale t block hash_orig).[0])) == sigma_1_0_partial t block hash_orig))\n\nval lemma_sigma_1_1_partial (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (sigma256_1_1 (word_to_nat32 ((repeat_range_vale t block hash_orig).[4])) == sigma_1_1_partial t block hash_orig))\n\n(* Abbreviations and lemmas for the code itself *)\nlet k_reqs (k_seq:seq quad32) : prop0 =\n length k_seq == size_k_w_256 / 4 /\\\n (forall i . {:pattern (index k_seq i)} 0 <= i /\\ i < (size_k_w_256/4) ==>\n (k_seq.[i]).lo0 == word_to_nat32 (k.[4 * i]) /\\\n (k_seq.[i]).lo1 == word_to_nat32 (k.[4 * i + 1]) /\\\n (k_seq.[i]).hi2 == word_to_nat32 (k.[4 * i + 2]) /\\\n (k_seq.[i]).hi3 == word_to_nat32 (k.[4 * i + 3]))\n\nlet quads_to_block_be (qs:seq quad32) : block_w\n =\n let nat32_seq = Vale.Def.Words.Seq_s.seq_four_to_seq_BE qs in\n let f (n:nat{n < 16}) : word = nat32_to_word (if n < length nat32_seq then nat32_seq.[n] else 0) in\n init 16 f\n\nval lemma_quads_to_block_be (qs:seq quad32) : Lemma\n (requires length qs == 4)\n (ensures\n (let block = quads_to_block_be qs in\n forall i . {:pattern (index qs i)} 0 <= i /\\ i < 4 ==>\n (qs.[i]).hi3 == ws_opaque block (4 * i + 0) /\\\n (qs.[i]).hi2 == ws_opaque block (4 * i + 1) /\\\n (qs.[i]).lo1 == ws_opaque block (4 * i + 2) /\\\n (qs.[i]).lo0 == ws_opaque block (4 * i + 3)))\n\nlet k_index (ks:seq quad32) (i:nat) : nat32 =\n if length ks = size_k_w_256 / 4 && i < size_k_w_256 then four_select ks.[(i/4)] (i % 4)\n else 0\n\n\nval lemma_shuffle_core_properties (t:counter) (block:block_w) (hash_orig:hash256) : Lemma\n (requires t < size_k_w_256)\n (ensures (let hash = Spec.Loops.repeat_range 0 t (shuffle_core_opaque block) hash_orig in\n let h = Spec.Loops.repeat_range 0 (t + 1) (shuffle_core_opaque block) hash_orig in\n let a0 = word_to_nat32 hash.[0] in\n let b0 = word_to_nat32 hash.[1] in\n let c0 = word_to_nat32 hash.[2] in\n let d0 = word_to_nat32 hash.[3] in\n let e0 = word_to_nat32 hash.[4] in\n let f0 = word_to_nat32 hash.[5] in\n let g0 = word_to_nat32 hash.[6] in\n let h0 = word_to_nat32 hash.[7] in\n let t1 = add_wrap (add_wrap (add_wrap (add_wrap h0 (sigma256_1_1 e0)) (ch_256 e0 f0 g0)) (word_to_nat32 k.[t])) (ws_opaque block t) in\n let t2 = add_wrap (sigma256_1_0 a0) (maj_256 a0 b0 c0) in\n word_to_nat32 h.[0] == add_wrap t1 t2 /\\\n word_to_nat32 h.[1] == a0 /\\\n word_to_nat32 h.[2] == b0 /\\\n word_to_nat32 h.[3] == c0 /\\\n word_to_nat32 h.[4] == add_wrap d0 t1 /\\\n word_to_nat32 h.[5] == e0 /\\\n word_to_nat32 h.[6] == f0 /\\\n word_to_nat32 h.[7] == g0))\n\nval lemma_ws_opaque (block:block_w) (t:counter) : Lemma\n (requires 16 <= t && t < size_k_w_256)\n (ensures (let sigma0 = sigma256_0_0 (ws_opaque block (t - 15)) in\n let sigma1 = sigma256_0_1 (ws_opaque block (t - 2)) in\n ws_opaque block t == add_wrap (add_wrap (add_wrap sigma1 (ws_opaque block (t - 7))) sigma0) (ws_opaque block (t - 16))))\n\nlet repeat_range_vale_64 (block:block_w) (hash:hash256) =\n Spec.Loops.repeat_range 0 64 (shuffle_core_opaque block) hash\n\nval update_lemma (a b c d e f g h a_old b_old c_old d_old e_old f_old g_old h_old a' b' c' d' e' f' g' h':quad32) (block:block_w) : Lemma\n (requires (let hash_orig = make_seperated_hash_quad32 a_old b_old c_old d_old e_old f_old g_old h_old in\n make_seperated_hash_quad32 a b c d e f g h ==\n repeat_range_vale_64 block hash_orig /\\\n a' == add_wrap_quad32 a a_old /\\\n b' == add_wrap_quad32 b b_old /\\\n c' == add_wrap_quad32 c c_old /\\\n d' == add_wrap_quad32 d d_old /\\\n e' == add_wrap_quad32 e e_old /\\\n f' == add_wrap_quad32 f f_old /\\\n g' == add_wrap_quad32 g g_old /\\\n h' == add_wrap_quad32 h h_old))\n (ensures (let hash_orig = make_seperated_hash_quad32 a_old b_old c_old d_old e_old f_old g_old h_old in\n make_seperated_hash_quad32 a' b' c' d' e' f' g' h' == update_block hash_orig block))\n\nPartial implementation:\nlet rec update_multi_quads (s: seq quad32) (hash_orig: hash256)\n : Tot (hash256) (decreases (length s)) =\n \n"
},
{
"role": "assistant",
"content": "if length s < 4\nthen hash_orig\nelse\n let prefix, qs = split s (length s - 4) in\n let h_prefix = update_multi_quads prefix hash_orig in\n let hash = update_block h_prefix (quads_to_block_be qs) in\n hash"
}
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.