| 1 |
Require Import bedrock2.Array. |
| 2 |
Require Import bedrock2.bottom_up_simpl. |
| 3 |
Require Import bedrock2.FE310CSemantics. |
| 4 |
Require Import bedrock2.Loops. |
| 5 |
Require Import bedrock2.Map.Separation. |
| 6 |
Require Import bedrock2.Map.SeparationLogic. |
| 7 |
Require Import bedrock2.NotationsCustomEntry. |
| 8 |
Require Import bedrock2.ProgramLogic. |
| 9 |
Require Import bedrock2.Scalars. |
| 10 |
Require Import bedrock2.Semantics. |
| 11 |
Require Import bedrock2.SepAutoArray. |
| 12 |
Require Import bedrock2.Syntax. |
| 13 |
Require Import bedrock2.WeakestPrecondition. |
| 14 |
Require Import bedrock2.WeakestPreconditionProperties. |
| 15 |
Require Import bedrock2.ZnWords. |
| 16 |
Require Import compiler.MMIO. |
| 17 |
Require Import compiler.Pipeline. |
| 18 |
Require Import compiler.Symbols. |
| 19 |
Require Import coqutil.Byte. |
| 20 |
Require Import coqutil.Map.Interface. |
| 21 |
Require Import coqutil.Map.OfListWord. |
| 22 |
From coqutil.Tactics Require Import Tactics letexists eabstract rdelta reference_to_string ident_of_string. |
| 23 |
Require Import coqutil.Word.Bitwidth32. |
| 24 |
Require Import coqutil.Word.Bitwidth. |
| 25 |
Require Import coqutil.Word.Interface. |
| 26 |
Require Import coqutil.Word.Naive. |
| 27 |
From Coq Require Import Init.Byte. |
| 28 |
From Coq Require Import List. |
| 29 |
From Coq Require Import String. |
| 30 |
From Coq Require Import ZArith. |
| 31 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 32 |
Require Import Crypto.Bedrock.Field.Synthesis.New.UnsaturatedSolinas. |
| 33 |
Require Import Crypto.Bedrock.End2End.X25519.Field25519. |
| 34 |
Require Import Crypto.Bedrock.Specs.Field. |
| 35 |
Require Import Crypto.Spec.Curve25519. |
| 36 |
Require Import Crypto.Util.Decidable. |
| 37 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 38 |
Require Import Curves.Edwards.XYZT.Basic. |
| 39 |
Require Import Curves.Edwards.XYZT.Precomputed. |
| 40 |
Require Import Curves.Edwards.XYZT.Readdition. |
| 41 |
Require Import Lia. |
| 42 |
Require Crypto.Bedrock.Field.Synthesis.New.Signature. |
| 43 |
Local Open Scope string_scope. |
| 44 |
Local Open Scope Z_scope. |
| 45 |
Import LittleEndianList. |
| 46 |
Import ListNotations. |
| 47 |
Import ProgramLogic.Coercions. |
| 48 |
Import WeakestPrecondition. |
| 49 |
|
| 50 |
Local Existing Instance field_parameters. |
| 51 |
Local Existing Instance frep25519. |
| 52 |
Local Existing Instance frep25519_ok. |
| 53 |
|
| 54 |
(* Size of a field element in bytes. This is the same as computing eval in felem_size_bytes, but we want a notation instead of definition. *) |
| 55 |
Local Notation felem_size := 40. |
| 56 |
|
| 57 |
(* Notations help treat curve points like C structs. To avoid notation clashes, projective coordinates are capitalized. *) |
| 58 |
|
| 59 |
(* Member access notation for projective points: (X, Y, Z, Ta, Tb). *) |
| 60 |
Local Notation "A .X" := (A) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 61 |
Local Notation "A .Y" := (expr.op Syntax.bopname.add A (felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 62 |
Local Notation "A .Z" := (expr.op Syntax.bopname.add A (felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 63 |
Local Notation "A .Ta" := (expr.op Syntax.bopname.add A (felem_size + felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 64 |
Local Notation "A .Tb" := (expr.op Syntax.bopname.add A (felem_size + felem_size + felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 65 |
|
| 66 |
(* Member access notation for precomputed points: (half_ypx, half_ymx, xyd). *) |
| 67 |
Local Notation "A .half_ypx" := (A) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 68 |
Local Notation "A .half_ymx" := (expr.op Syntax.bopname.add A (felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 69 |
Local Notation "A .xyd" := (expr.op Syntax.bopname.add A (felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 70 |
|
| 71 |
(* Member access notation for cached points: (half_YmX, half_YpX, Z, Td). *) |
| 72 |
Local Notation "A .half_YmX" := (A) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 73 |
Local Notation "A .half_YpX" := (expr.op Syntax.bopname.add A (felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 74 |
Local Notation "A .Z" := (expr.op Syntax.bopname.add A (felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 75 |
Local Notation "A .Td" := (expr.op Syntax.bopname.add A (felem_size + felem_size + felem_size)) (in custom bedrock_expr at level 2, left associativity, only parsing). |
| 76 |
|
| 77 |
(* Adds a precomputed point p_b to projective point p_a and puts the result in p_out. *) |
| 78 |
Definition add_precomputed := func! (p_out, p_a, p_b) { |
| 79 |
stackalloc felem_size as YpX1; |
| 80 |
fe25519_add(YpX1, p_a.Y, p_a.X); |
| 81 |
stackalloc felem_size as YmX1; |
| 82 |
fe25519_sub(YmX1, p_a.Y, p_a.X); |
| 83 |
stackalloc felem_size as A; |
| 84 |
fe25519_mul(A, YmX1, p_b.half_ymx); |
| 85 |
stackalloc felem_size as B; |
| 86 |
fe25519_mul(B, YpX1, p_b.half_ypx); |
| 87 |
stackalloc felem_size as T1; |
| 88 |
fe25519_mul(T1, p_a.Ta, p_a.Tb); |
| 89 |
stackalloc felem_size as C; |
| 90 |
fe25519_mul(C, p_b.xyd, T1); |
| 91 |
fe25519_sub(p_out.Ta, B, A); |
| 92 |
stackalloc felem_size as F; |
| 93 |
fe25519_sub(F, p_a.Z, C); |
| 94 |
stackalloc felem_size as G; |
| 95 |
fe25519_add(G, p_a.Z, C); |
| 96 |
fe25519_add(p_out.Tb, B, A); |
| 97 |
fe25519_mul(p_out.X, p_out.Ta, F); |
| 98 |
fe25519_mul(p_out.Y, G, p_out.Tb); |
| 99 |
fe25519_mul(p_out.Z, F, G) |
| 100 |
}. |
| 101 |
|
| 102 |
(* Doubles a projective point. Equivalent of m1double in src/Curves/Edwards/XYZT/Basic.v *) |
| 103 |
Definition double := func! (p_out, p_a) { |
| 104 |
stackalloc felem_size as trX; |
| 105 |
fe25519_square(trX, p_a.X); |
| 106 |
stackalloc felem_size as trZ; |
| 107 |
fe25519_square(trZ, p_a.Y); |
| 108 |
stackalloc felem_size as t0; |
| 109 |
fe25519_square(t0, p_a.Z); |
| 110 |
stackalloc felem_size as trT; |
| 111 |
fe25519_carry_add(trT, t0, t0); |
| 112 |
stackalloc felem_size as rY; |
| 113 |
fe25519_add(rY, p_a.X, p_a.Y); |
| 114 |
fe25519_square(t0, rY); |
| 115 |
fe25519_carry_add(p_out.Tb, trZ, trX); |
| 116 |
stackalloc felem_size as cZ; |
| 117 |
fe25519_carry_sub(cZ, trZ, trX); |
| 118 |
fe25519_sub(p_out.Ta, t0, p_out.Tb); |
| 119 |
stackalloc felem_size as cT; |
| 120 |
fe25519_sub(cT, trT, cZ); |
| 121 |
fe25519_mul(p_out.X, p_out.Ta, cT); |
| 122 |
fe25519_mul(p_out.Y, p_out.Tb, cZ); |
| 123 |
fe25519_mul(p_out.Z, cZ, cT) |
| 124 |
}. |
| 125 |
|
| 126 |
(* Converts a projective point p_a to a cached point p_out to prepare it for readdition. |
| 127 |
Uses the field's parameter d, which is passed as p_d for now. *) |
| 128 |
Definition to_cached := func! (p_out, p_a, p_d) { |
| 129 |
fe25519_sub(p_out.half_YmX, p_a.Y, p_a.X); |
| 130 |
fe25519_half(p_out.half_YmX, p_out.half_YmX); (* An implementation doesn't exist yet, work with spec for now. *) |
| 131 |
fe25519_add(p_out.half_YpX, p_a.Y, p_a.X); |
| 132 |
fe25519_half(p_out.half_YpX, p_out.half_YpX); |
| 133 |
fe25519_mul(p_out.Td, p_a.Ta, p_a.Tb); |
| 134 |
fe25519_mul(p_out.Td, p_out.Td, p_d); |
| 135 |
fe25519_copy(p_out.Z, p_a.Z) |
| 136 |
}. |
| 137 |
|
| 138 |
(* Equivalent of m1_readd in src/Curves/Edwards/XYZT/Readdition.v |
| 139 |
Adds a projective point p_a and cached point p_c and stores the result as projective point in p_out. *) |
| 140 |
Definition readd := func! (p_out, p_a, p_c) { |
| 141 |
stackalloc felem_size as A; |
| 142 |
fe25519_sub(A, p_a.Y, p_a.X); |
| 143 |
fe25519_mul(A, A, p_c.half_YmX); |
| 144 |
stackalloc felem_size as B; |
| 145 |
fe25519_add(B, p_a.Y, p_a.X); |
| 146 |
fe25519_mul(B, B, p_c.half_YpX); |
| 147 |
stackalloc felem_size as C; |
| 148 |
fe25519_mul(C, p_a.Ta, p_a.Tb); |
| 149 |
fe25519_mul(C, C, p_c.Td); |
| 150 |
stackalloc felem_size as D; |
| 151 |
fe25519_mul(D, p_a.Z, p_c.Z); |
| 152 |
fe25519_sub(p_out.Ta, B, A); |
| 153 |
stackalloc felem_size as F; |
| 154 |
fe25519_sub(F, D, C); |
| 155 |
stackalloc felem_size as G; |
| 156 |
fe25519_add(G, D, C); |
| 157 |
fe25519_add(p_out.Tb, B, A); |
| 158 |
fe25519_mul(p_out.X, p_out.Ta, F); |
| 159 |
fe25519_mul(p_out.Y, G, p_out.Tb); |
| 160 |
fe25519_mul(p_out.Z, F, G) |
| 161 |
}. |
| 162 |
|
| 163 |
Section WithParameters. |
| 164 |
Context {two_lt_M: 2 < M_pos}. |
| 165 |
(* TODO: Can we provide actual values/proofs for these, rather than just sticking them in the context? *) |
| 166 |
Context {char_ge_3 : (@Ring.char_ge (F M_pos) Logic.eq F.zero F.one F.opp F.add F.sub F.mul (BinNat.N.succ_pos BinNat.N.two))}. |
| 167 |
Context {field:@Algebra.Hierarchy.field (F M_pos) Logic.eq F.zero F.one F.opp F.add F.sub F.mul F.inv F.div}. |
| 168 |
Context {a d: F M_pos} |
| 169 |
{nonzero_a : a <> F.zero} |
| 170 |
{square_a : exists sqrt_a, (F.mul sqrt_a sqrt_a) = a} |
| 171 |
{nonsquare_d : forall x, (F.mul x x) <> d}. |
| 172 |
Context {a_eq_minus1:a = F.opp F.one} {twice_d} {k_eq_2d:twice_d = (F.add d d)} {nonzero_d: d<>F.zero}. |
| 173 |
|
| 174 |
Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing). |
| 175 |
|
| 176 |
Local Notation FElem := (FElem(FieldRepresentation:=frep25519)). |
| 177 |
Local Notation felem_size_in_bytes := (felem_size_in_bytes(FieldRepresentation:=frep25519)). |
| 178 |
Local Notation bounded_by := (bounded_by(FieldRepresentation:=frep25519)). |
| 179 |
Local Notation word := (Naive.word 32). |
| 180 |
Local Notation felem := (felem(FieldRepresentation:=frep25519)). |
| 181 |
Local Notation point := (Extended.point(Feq:=Logic.eq)(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(d:=d)). |
| 182 |
Local Notation cached := (cached(Fzero:=F.zero)(Fadd:=F.add)(Fmul:=F.mul)(a:=a)(d:=d)). |
| 183 |
Local Notation cached_coordinates := (cached_coordinates(Fzero:=F.zero)(Fadd:=F.add)(Fdiv:=F.div)(Fmul:=F.mul)(Fsub:=F.sub)(Feq:=Logic.eq)(a:=a)(d:=d)). |
| 184 |
Local Notation precomputed_coordinates := (precomputed_coordinates(Fone:=F.one)(Fadd:=F.add)(Fmul:=F.mul)(Fsub:=F.sub)(Feq:=Logic.eq)(a:=a)(d:=d)). |
| 185 |
Local Notation m1double := |
| 186 |
(Extended.m1double(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one) |
| 187 |
(Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div) |
| 188 |
(field:=field)(char_ge_3:=char_ge_3)(Feq_dec:=F.eq_dec) |
| 189 |
(a:=a)(d:=d)(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d) |
| 190 |
(a_eq_minus1:=a_eq_minus1)(twice_d:=twice_d)(k_eq_2d:=k_eq_2d)). |
| 191 |
Local Notation m1_prep := |
| 192 |
(m1_prep(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one) |
| 193 |
(Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div) |
| 194 |
(field:=field)(char_ge_3:=char_ge_3)(Feq_dec:=F.eq_dec) |
| 195 |
(a:=a)(d:=d)(nonzero_a:=nonzero_a)(a_eq_minus1:=a_eq_minus1) |
| 196 |
(twice_d:=twice_d)(k_eq_2d:=k_eq_2d)(nonzero_d:=nonzero_d)). |
| 197 |
Local Notation m1_readd := |
| 198 |
(m1_readd(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one) |
| 199 |
(Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div) |
| 200 |
(field:=field)(char_ge_3:=char_ge_3)(Feq_dec:=F.eq_dec) |
| 201 |
(a:=a)(d:=d)(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d) |
| 202 |
(a_eq_minus1:=a_eq_minus1)(twice_d:=twice_d)(k_eq_2d:=k_eq_2d)(nonzero_d:=nonzero_d)). |
| 203 |
Local Notation m1add_precomputed_coordinates := |
| 204 |
(m1add_precomputed_coordinates(F:=F M_pos)(Feq:=Logic.eq)(Fzero:=F.zero)(Fone:=F.one) |
| 205 |
(Fopp:=F.opp)(Fadd:=F.add)(Fsub:=F.sub)(Fmul:=F.mul)(Finv:=F.inv)(Fdiv:=F.div) |
| 206 |
(field:=field)(char_ge_3:=char_ge_3)(Feq_dec:=F.eq_dec) |
| 207 |
(a:=a)(d:=d)(nonzero_a:=nonzero_a)(square_a:=square_a)(nonsquare_d:=nonsquare_d) |
| 208 |
(a_eq_minus1:=a_eq_minus1)). |
| 209 |
|
| 210 |
Local Notation "p .+ n" := (word.add p (word.of_Z n)) (at level 50, format "p .+ n", left associativity). |
| 211 |
|
| 212 |
Definition projective_repr (P : point) := { p | let '(x,y,z,ta,tb) := p in |
| 213 |
proj1_sig P = (feval x, feval y, feval z, feval ta, feval tb) /\ |
| 214 |
bounded_by tight_bounds x /\ bounded_by tight_bounds y /\ bounded_by tight_bounds z /\ |
| 215 |
bounded_by loose_bounds ta /\ bounded_by loose_bounds tb }. |
| 216 |
Local Notation "c 'p5@' p" := (let '(x,y,z,ta,tb) := proj1_sig c in sep (sep (sep (sep (FElem (p) x) (FElem (p .+ felem_size) y)) |
| 217 |
(FElem (p .+ (felem_size + felem_size)) z)) (FElem (p .+ (felem_size + felem_size + felem_size)) ta)) |
| 218 |
(FElem (p .+ (felem_size + felem_size + felem_size + felem_size)) tb)) (at level 10, format "c 'p5@' p"). |
| 219 |
|
| 220 |
|
| 221 |
Definition precomputed_repr (P : precomputed_point) := { p | let '(half_ymx, half_ypx, xyd) := p in |
| 222 |
precomputed_coordinates P = (feval half_ymx, feval half_ypx, feval xyd) /\ |
| 223 |
bounded_by loose_bounds half_ymx /\ bounded_by loose_bounds half_ypx /\ bounded_by loose_bounds xyd }. |
| 224 |
Local Notation "c 'p3@' p" := (let '(half_ymx, half_ypx, xyd) := proj1_sig c in sep (sep (FElem (p) half_ymx) (FElem (p .+ felem_size) half_ypx)) |
| 225 |
(FElem (p .+ (felem_size + felem_size)) xyd)) (at level 10, format "c 'p3@' p"). |
| 226 |
|
| 227 |
Definition cached_repr (P : cached) := { p | let '(half_ymx, half_ypx,z,td) := p in |
| 228 |
cached_coordinates P = (feval half_ymx, feval half_ypx, feval z, feval td) /\ |
| 229 |
bounded_by loose_bounds half_ymx /\ bounded_by loose_bounds half_ypx /\bounded_by loose_bounds z /\ bounded_by loose_bounds td }. |
| 230 |
Local Notation "c 'p4@' p" := (let '(half_ymx, half_ypx,z,td) := proj1_sig c in sep (sep (sep (FElem (p) half_ymx) (FElem (p .+ felem_size) half_ypx)) |
| 231 |
(FElem (p .+ (felem_size + felem_size)) z)) (FElem (p .+ (felem_size + felem_size + felem_size)) td)) (at level 10, format "c 'p4@' p"). |
| 232 |
|
| 233 |
Instance spec_of_fe25519_half : spec_of "fe25519_half" := |
| 234 |
fnspec! "fe25519_half" |
| 235 |
(result_location input_location: word) / (old_result input: felem) |
| 236 |
(R: _ -> Prop), |
| 237 |
{ requires t m := |
| 238 |
bounded_by loose_bounds input /\ |
| 239 |
(exists Ra, m =* (FElem input_location input) * Ra) /\ |
| 240 |
m =* (FElem result_location old_result) * R; |
| 241 |
ensures t' m' := |
| 242 |
t = t' /\ |
| 243 |
exists result, |
| 244 |
bounded_by tight_bounds result /\ |
| 245 |
feval result = F.div (feval input) (F.add F.one F.one) /\ |
| 246 |
m' =* (FElem result_location result) * R}. |
| 247 |
|
| 248 |
Global Instance spec_of_add_precomputed : spec_of "add_precomputed" := |
| 249 |
fnspec! "add_precomputed" |
| 250 |
(p_out p_a p_b: word) / (a: point) (b: precomputed_point) (a_repr: projective_repr a) (b_repr: precomputed_repr b) (out : list byte) (R: _ -> Prop), { |
| 251 |
requires t m := |
| 252 |
m =* out $@ p_out * a_repr p5@ p_a * b_repr p3@ p_b * R/\ |
| 253 |
Datatypes.length out = Z.to_nat (5 * felem_size); |
| 254 |
ensures t' m' := |
| 255 |
t = t' /\ |
| 256 |
exists a_plus_b : projective_repr (m1add_precomputed_coordinates a b), |
| 257 |
m' =* a_plus_b p5@ p_out * a_repr p5@ p_a * b_repr p3@ p_b * R |
| 258 |
}. |
| 259 |
Global Instance spec_of_double : spec_of "double" := |
| 260 |
fnspec! "double" |
| 261 |
(p_out p_a: word) / |
| 262 |
(a: point) (a_repr: projective_repr a) (out : list byte) (R: _ -> Prop), { |
| 263 |
requires t m := |
| 264 |
m =* out $@ p_out * a_repr p5@ p_a * R /\ |
| 265 |
Datatypes.length out = Z.to_nat (5 * felem_size); |
| 266 |
ensures t' m' := |
| 267 |
t = t' /\ |
| 268 |
exists a_double: projective_repr (m1double a), |
| 269 |
m' =* a_double p5@ p_out * a_repr p5@ p_a * R |
| 270 |
}. |
| 271 |
|
| 272 |
Global Instance spec_of_to_cached: spec_of "to_cached" := |
| 273 |
fnspec! "to_cached" |
| 274 |
(p_out p_a p_d: word) / |
| 275 |
(a: point) (a_repr: projective_repr a) (d1: felem) (out : list byte) (R: _ -> Prop), { |
| 276 |
requires t m := |
| 277 |
m =* out $@ p_out * a_repr p5@ p_a * FElem p_d d1 * R /\ |
| 278 |
Datatypes.length out = Z.to_nat (4 * felem_size) /\ |
| 279 |
d = feval d1 /\ |
| 280 |
bounded_by tight_bounds d1; |
| 281 |
ensures t' m' := |
| 282 |
t = t' /\ |
| 283 |
exists a_cached: cached_repr (m1_prep a), |
| 284 |
m' =* a_cached p4@ p_out * a_repr p5@ p_a * FElem p_d d1 * R |
| 285 |
}. |
| 286 |
|
| 287 |
Global Instance spec_of_readd : spec_of "readd" := |
| 288 |
fnspec! "readd" |
| 289 |
(p_out p_a p_c: word) / |
| 290 |
(a: point) (c: cached) (a_repr: projective_repr a) (c_repr: cached_repr c) (out : list byte) (R : _ -> Prop), { |
| 291 |
requires t m := |
| 292 |
m =* out $@ p_out * a_repr p5@ p_a * c_repr p4@ p_c * R /\ |
| 293 |
Datatypes.length out = Z.to_nat (5 * felem_size); |
| 294 |
ensures t' m' := |
| 295 |
t = t' /\ |
| 296 |
exists a_plus_c: projective_repr (m1_readd a c), |
| 297 |
m' =* a_plus_c p5@ p_out * a_repr p5@ p_a * c_repr p4@ p_c * R |
| 298 |
}. |
| 299 |
|
| 300 |
Local Instance spec_of_fe25519_square : spec_of "fe25519_square" := Field.spec_of_UnOp un_square. |
| 301 |
Local Instance spec_of_fe25519_mul : spec_of "fe25519_mul" := Field.spec_of_BinOp bin_mul. |
| 302 |
Local Instance spec_of_fe25519_add : spec_of "fe25519_add" := Field.spec_of_BinOp bin_add. |
| 303 |
Local Instance spec_of_fe25519_carry_add : spec_of "fe25519_carry_add" := Field.spec_of_BinOp bin_carry_add. |
| 304 |
Local Instance spec_of_fe25519_sub : spec_of "fe25519_sub" := Field.spec_of_BinOp bin_sub. |
| 305 |
Local Instance spec_of_fe25519_carry_sub : spec_of "fe25519_carry_sub" := Field.spec_of_BinOp bin_carry_sub. |
| 306 |
Local Instance spec_of_fe25519_from_word : spec_of "fe25519_from_word" := Field.spec_of_from_word. |
| 307 |
Local Instance spec_of_fe26619_copy: spec_of "fe25519_copy" := Field.spec_of_felem_copy. |
| 308 |
|
| 309 |
Local Arguments word.rep : simpl never. |
| 310 |
Local Arguments word.wrap : simpl never. |
| 311 |
Local Arguments word.unsigned : simpl never. |
| 312 |
Local Arguments word.of_Z : simpl never. |
| 313 |
Local Arguments word.add : simpl never. |
| 314 |
|
| 315 |
Local Arguments feval : simpl never. |
| 316 |
|
| 317 |
Local Ltac destruct_points := |
| 318 |
repeat match goal with |
| 319 |
| _ => progress destruct_head' @Readdition.cached |
| 320 |
| _ => progress destruct_head' cached_repr |
| 321 |
| _ => progress destruct_head' @Extended.point |
| 322 |
| _ => progress destruct_head' projective_repr |
| 323 |
| _ => progress destruct_head' @precomputed_point |
| 324 |
| _ => progress destruct_head' precomputed_repr |
| 325 |
| _ => progress destruct_head' prod |
| 326 |
| _ => progress destruct_head' and |
| 327 |
| _ => progress lazy beta match zeta delta [precomputed_coordinates cached_coordinates proj1_sig] in * |
| 328 |
end. |
| 329 |
|
| 330 |
Local Ltac cbv_bounds H := |
| 331 |
cbv [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds] in H; |
| 332 |
cbv [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds]. |
| 333 |
|
| 334 |
Local Ltac solve_bounds := |
| 335 |
repeat match goal with |
| 336 |
| H: bounded_by loose_bounds ?x |- bounded_by loose_bounds ?x => apply H |
| 337 |
| H: bounded_by tight_bounds ?x |- bounded_by tight_bounds ?x => apply H |
| 338 |
| H: bounded_by tight_bounds ?x |- bounded_by loose_bounds ?x => apply relax_bounds |
| 339 |
| H: bounded_by _ ?x |- bounded_by _ ?x => cbv_bounds H |
| 340 |
end. |
| 341 |
|
| 342 |
Ltac skipn_firstn_length := |
| 343 |
change felem_size_in_bytes with 40 in *; listZnWords. |
| 344 |
|
| 345 |
Ltac split_stack_at_n_in stack p n H := rewrite <- (firstn_skipn n stack) in H; |
| 346 |
rewrite (map.of_list_word_at_app_n _ _ _ n) in H; try skipn_firstn_length; |
| 347 |
let D := fresh in unshelve(epose (sep_eq_putmany _ _ (map.adjacent_arrays_disjoint_n p (firstn n stack) (skipn n stack) n _ _)) as D); try skipn_firstn_length; |
| 348 |
seprewrite_in D H; rewrite ?skipn_skipn in H; bottom_up_simpl_in_hyp H; clear D. |
| 349 |
|
| 350 |
Local Ltac solve_mem := |
| 351 |
try match goal with | |- exists _ : _ -> Prop, _%sep _ => eexists end; |
| 352 |
match goal with | H : _ %sep ?m |- _ %sep ?m => bottom_up_simpl_in_goal_nop_ok end; |
| 353 |
match goal with |
| 354 |
| |- _%sep _ => ecancel_assumption_impl |
| 355 |
| H: ?P%sep ?m |- ?G%sep ?m => (* Solve Placeholder goals when a fixed size list is given *) |
| 356 |
match P with context[map.of_list_word_at ?p ?stack] => |
| 357 |
match G with context[Placeholder ?p _] => |
| 358 |
solve [ cbv [Placeholder]; extract_ex1_and_emp_in_goal; bottom_up_simpl_in_goal_nop_ok; split; [ecancel_assumption | skipn_firstn_length] ] |
| 359 |
end end |
| 360 |
end. |
| 361 |
|
| 362 |
Local Ltac single_step := |
| 363 |
repeat straightline; straightline_call; ssplit; try solve_mem; try solve_bounds. |
| 364 |
|
| 365 |
(* Attempts to find anybytes terms in the goal and rewrites the first corresponding stack hypothesis to byte representation. |
| 366 |
straightline only supports deallocation for byte representation at the moment. *) |
| 367 |
Ltac solve_deallocation := |
| 368 |
lazy [FElem] in *; |
| 369 |
match goal with |
| 370 |
H: ?P%sep _ |- ?G => |
| 371 |
repeat match G with context[anybytes ?p _ _] => |
| 372 |
match P with context[Bignum.Bignum felem_size_in_words p ?v] => |
| 373 |
seprewrite_in (Bignum.Bignum_to_bytes felem_size_in_words p) H |
| 374 |
end |
| 375 |
end; |
| 376 |
extract_ex1_and_emp_in H |
| 377 |
end; |
| 378 |
repeat straightline. |
| 379 |
|
| 380 |
Ltac split_output_stack stack_var ptr_var num_points := |
| 381 |
match goal with |
| 382 |
| H : context[stack_var $@ ptr_var] |- _ => |
| 383 |
split_stack_at_n_in stack_var ptr_var 40%nat H; |
| 384 |
split_stack_at_n_in (skipn 40 stack_var) (ptr_var.+40) 40%nat H; |
| 385 |
split_stack_at_n_in (skipn 80 stack_var) (ptr_var.+80) 40%nat H; |
| 386 |
match num_points with |
| 387 |
| 4 => idtac |
| 388 |
| 5 => |
| 389 |
split_stack_at_n_in (skipn 120 stack_var) (ptr_var.+120) 40%nat H |
| 390 |
end |
| 391 |
end. |
| 392 |
|
| 393 |
Lemma to_cached_ok: program_logic_goal_for_function! to_cached. |
| 394 |
Proof. |
| 395 |
(* Without this, resolution of cbv stalls out Qed. *) |
| 396 |
Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds]. |
| 397 |
|
| 398 |
repeat straightline. |
| 399 |
destruct_points. |
| 400 |
split_output_stack out p_out 4. |
| 401 |
repeat straightline. |
| 402 |
|
| 403 |
repeat single_step. |
| 404 |
|
| 405 |
repeat straightline. |
| 406 |
lazy delta [cached_repr]. |
| 407 |
unshelve eexists. |
| 408 |
eexists (_, _, _, _). |
| 409 |
2: solve_mem. |
| 410 |
lazy match zeta beta delta [bin_model bin_mul bin_add bin_carry_add bin_sub cached_coordinates proj1_sig m1_prep] in *. |
| 411 |
ssplit; try solve_bounds. |
| 412 |
congruence. |
| 413 |
Qed. |
| 414 |
|
| 415 |
Lemma add_precomputed_ok : program_logic_goal_for_function! add_precomputed. |
| 416 |
Proof. |
| 417 |
(* Without this, resolution of cbv stalls out Qed. *) |
| 418 |
Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub un_outbounds bin_outbounds]. |
| 419 |
|
| 420 |
do 4 straightline. |
| 421 |
destruct_points. |
| 422 |
split_output_stack out p_out 5. |
| 423 |
repeat straightline. |
| 424 |
|
| 425 |
Time repeat single_step. (* Avoid performance regressions: Keep this around 90s*) |
| 426 |
|
| 427 |
repeat straightline. |
| 428 |
solve_deallocation. |
| 429 |
|
| 430 |
lazy beta match zeta delta [m1add_precomputed_coordinates projective_repr precomputed_coordinates proj1_sig]. |
| 431 |
unshelve eexists. |
| 432 |
eexists (_, _, _, _, _). |
| 433 |
2: solve_mem. |
| 434 |
ssplit; try solve_bounds. |
| 435 |
lazy [bin_model bin_add bin_mul bin_sub] in *. congruence. |
| 436 |
Qed. |
| 437 |
|
| 438 |
Lemma double_ok : program_logic_goal_for_function! double. |
| 439 |
Proof. |
| 440 |
(* Without this, resolution of cbv stalls out Qed. *) |
| 441 |
Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds]. |
| 442 |
|
| 443 |
do 3 straightline. |
| 444 |
destruct_points. |
| 445 |
split_output_stack out p_out 5. |
| 446 |
repeat straightline. |
| 447 |
|
| 448 |
repeat single_step. |
| 449 |
|
| 450 |
(* Solve the postconditions *) |
| 451 |
repeat straightline. |
| 452 |
solve_deallocation. |
| 453 |
lazy beta match zeta delta [projective_repr proj1_sig m1double bin_model bin_add bin_mul bin_sub bin_carry_add bin_sub bin_carry_sub un_model un_square] in *. |
| 454 |
unshelve eexists. |
| 455 |
eexists (_, _, _, _, _). |
| 456 |
2: solve_mem. |
| 457 |
ssplit; try solve_bounds. |
| 458 |
rewrite F.pow_2_r in *. |
| 459 |
Prod.inversion_prod. congruence. |
| 460 |
Qed. |
| 461 |
|
| 462 |
Lemma readd_ok : program_logic_goal_for_function! readd. |
| 463 |
Proof. |
| 464 |
(* Without this, resolution of cbv stalls out Qed. *) |
| 465 |
Strategy -1000 [un_xbounds bin_xbounds bin_ybounds un_square bin_mul bin_add bin_carry_add bin_sub bin_carry_sub un_outbounds bin_outbounds]. |
| 466 |
|
| 467 |
do 4 straightline. |
| 468 |
destruct_points. |
| 469 |
split_output_stack out p_out 5. |
| 470 |
repeat straightline. |
| 471 |
|
| 472 |
repeat single_step. |
| 473 |
|
| 474 |
repeat straightline. |
| 475 |
solve_deallocation. |
| 476 |
unshelve eexists. |
| 477 |
eexists (_, _, _, _, _). |
| 478 |
2: solve_mem. |
| 479 |
lazy match beta zeta delta [m1_readd proj1_sig bin_model bin_mul bin_add bin_carry_add bin_sub] in *. |
| 480 |
ssplit; try solve_bounds. |
| 481 |
Prod.inversion_prod. congruence. |
| 482 |
Qed. |
| 483 |
|
| 484 |
End WithParameters. |