| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
Require Import Crypto.Util.Decidable. |
| 5 |
Require Import Crypto.Spec.MontgomeryCurve. |
| 6 |
Require Import Crypto.Spec.Curve25519. |
| 7 |
Require Import Crypto.Bedrock.Specs.Field. |
| 8 |
Require Import bedrock2.Map.Separation. |
| 9 |
Require Import bedrock2.Syntax. |
| 10 |
Require Import bedrock2Examples.memmove. |
| 11 |
Require Import bedrock2.SepAutoArray. |
| 12 |
Require Import compiler.Pipeline. |
| 13 |
Require Import compiler.Symbols. |
| 14 |
Require Import compiler.MMIO. |
| 15 |
Require Import coqutil.Word.Bitwidth32. |
| 16 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 17 |
Require Import Crypto.Bedrock.Field.Interface.Compilation2. |
| 18 |
Require Import Crypto.Bedrock.Field.Synthesis.New.UnsaturatedSolinas. |
| 19 |
Require Import Crypto.Bedrock.Group.AdditionChains. |
| 20 |
Require Import Crypto.Bedrock.Group.ScalarMult.LadderStep. |
| 21 |
Require Import Crypto.Bedrock.Group.ScalarMult.CSwap. |
| 22 |
Require Import Crypto.Bedrock.Group.ScalarMult.MontgomeryLadder. |
| 23 |
Require Import Crypto.Bedrock.End2End.X25519.Field25519. |
| 24 |
Require Import Crypto.Bedrock.End2End.X25519.clamp. |
| 25 |
Local Open Scope string_scope. |
| 26 |
Import ListNotations. |
| 27 |
|
| 28 |
Local Existing Instance frep25519. |
| 29 |
Local Existing Instance frep25519_ok. |
| 30 |
Derive ladderstep SuchThat (ladderstep = ladderstep_body) As ladderstep_defn. |
| 31 |
Proof. vm_compute. subst; exact eq_refl. Qed. |
| 32 |
|
| 33 |
Derive montladder SuchThat (montladder = montladder_body (Z.to_nat (Z.log2 Curve25519.order))) |
| 34 |
As montladder_defn. |
| 35 |
Proof. vm_compute. subst; exact eq_refl. Qed. |
| 36 |
|
| 37 |
Require Import bedrock2.NotationsCustomEntry. |
| 38 |
|
| 39 |
Definition x25519 := func! (out, sk, pk) { |
| 40 |
stackalloc 32 as K; |
| 41 |
memmove(K, sk, $32); |
| 42 |
clamp(K); |
| 43 |
stackalloc 40 as U; |
| 44 |
fe25519_from_bytes(U, pk); |
| 45 |
stackalloc 40 as OUT; |
| 46 |
montladder(OUT, K, U); |
| 47 |
fe25519_to_bytes(out, OUT) |
| 48 |
}. |
| 49 |
|
| 50 |
Definition x25519_base := func! (out, sk) { |
| 51 |
stackalloc 32 as K; |
| 52 |
memmove(K, sk, $32); |
| 53 |
clamp(K); |
| 54 |
stackalloc 40 as U; |
| 55 |
fe25519_from_word(U, $9); |
| 56 |
stackalloc 40 as OUT; |
| 57 |
montladder(OUT, K, U); |
| 58 |
fe25519_to_bytes(out, OUT) |
| 59 |
}. |
| 60 |
|
| 61 |
Import LittleEndianList. |
| 62 |
Local Coercion F.to_Z : F >-> Z. |
| 63 |
Require Import bedrock2.WeakestPrecondition bedrock2.Semantics bedrock2.ProgramLogic. |
| 64 |
Require Import bedrock2.Syntax bedrock2.Map.SeparationLogic. |
| 65 |
From Coq.Init Require Import Byte. |
| 66 |
Require Import coqutil.Byte. |
| 67 |
Import ProgramLogic.Coercions. |
| 68 |
Local Notation "m =* P" := ((P%sep) m) (at level 70, only parsing) (* experiment*). |
| 69 |
Local Notation "xs $@ a" := (Array.array ptsto (word.of_Z 1) a xs) (at level 10, format "xs $@ a"). |
| 70 |
|
| 71 |
Definition x25519_spec s P := le_split 32 (M.X0 (Curve25519.M.scalarmult (Curve25519.clamp (le_combine s)) P)). |
| 72 |
Lemma length_x25519_spec s P : length (x25519_spec s P) = 32%nat. Proof. apply length_le_split. Qed. |
| 73 |
|
| 74 |
Global Instance spec_of_x25519 : spec_of "x25519" := |
| 75 |
fnspec! "x25519" out sk pk / (o s p : list Byte.byte) P (R : _ -> Prop), |
| 76 |
{ requires t m := m =* s$@sk * p$@pk * o$@out * R /\ |
| 77 |
length s = 32%nat /\ length p = 32%nat /\ length o = 32%nat /\ |
| 78 |
byte.unsigned (nth 31 p x00) <= 0x7f /\ Field.feval_bytes(field_parameters:=field_parameters) p = Curve25519.M.X0 P; |
| 79 |
ensures t' m := t=t' /\ m=* s$@sk ⋆ p$@pk ⋆ R ⋆ (x25519_spec s P)$@out }. |
| 80 |
|
| 81 |
Global Instance spec_of_x25519_base : spec_of "x25519_base" := |
| 82 |
fnspec! "x25519_base" out sk / (o s : list Byte.byte) (R : _ -> Prop), |
| 83 |
{ requires t m := m =* s$@sk * o$@out * R /\ length s = 32%nat /\ length o = 32%nat; |
| 84 |
ensures t' m := t=t' /\ m=* s$@sk ⋆ R ⋆ (x25519_spec s Curve25519.M.B)$@out }. |
| 85 |
|
| 86 |
Local Instance spec_of_memmove_array : spec_of "memmove" := spec_of_memmove_array. |
| 87 |
Local Instance spec_of_fe25519_from_word : spec_of "fe25519_from_word" := Field.spec_of_from_word. |
| 88 |
Local Instance spec_of_fe25519_from_bytes : spec_of "fe25519_from_bytes" := Field.spec_of_from_bytes. |
| 89 |
Local Instance spec_of_fe25519_to_bytes : spec_of "fe25519_to_bytes" := Field.spec_of_to_bytes. |
| 90 |
Local Instance spec_of_montladder : spec_of "montladder" := |
| 91 |
spec_of_montladder (Z.to_nat (Z.log2 Curve25519.order)). |
| 92 |
|
| 93 |
Local Arguments word.rep : simpl never. |
| 94 |
Local Arguments word.wrap : simpl never. |
| 95 |
Local Arguments word.unsigned : simpl never. |
| 96 |
Local Arguments word.of_Z : simpl never. |
| 97 |
|
| 98 |
Lemma x25519_ok : program_logic_goal_for_function! x25519. |
| 99 |
Proof. |
| 100 |
repeat straightline. |
| 101 |
|
| 102 |
straightline_call; ssplit; try ecancel_assumption; repeat straightline; try listZnWords; []. |
| 103 |
straightline_call; ssplit; try ecancel_assumption; repeat straightline; try listZnWords; []. |
| 104 |
|
| 105 |
|
| 106 |
straightline_call; ssplit. |
| 107 |
{ eexists. ecancel_assumption. } |
| 108 |
{ ecancel_assumption_impl. } |
| 109 |
|
| 110 |
{ unfold Field.bytes_in_bounds, frep25519, field_representation, Signature.field_representation, Representation.frep. |
| 111 |
match goal with |- ?P ?x ?z => let y := eval cbv in x in change (P y z) end; cbn. |
| 112 |
repeat (destruct p as [|? p]; try (cbn [length] in *;discriminate); []). |
| 113 |
cbn; cbn [nth] in *. |
| 114 |
cbv [COperationSpecifications.list_Z_bounded_by FoldBool.fold_andb_map map seq]; cbn. |
| 115 |
pose proof byte.unsigned_range as HH. setoid_rewrite <-Le.Z.le_sub_1_iff in HH. cbn in HH. |
| 116 |
setoid_rewrite Zle_is_le_bool in HH. setoid_rewrite <-Bool.andb_true_iff in HH. |
| 117 |
rewrite 31HH; cbn. |
| 118 |
eapply Bool.andb_true_iff; split; trivial. |
| 119 |
eapply Bool.andb_true_iff; split; eapply Zle_is_le_bool; trivial. |
| 120 |
eapply byte.unsigned_range. } |
| 121 |
repeat straightline. |
| 122 |
|
| 123 |
seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 10 a2) H24. { transitivity 40%nat; trivial. } |
| 124 |
|
| 125 |
straightline_call; ssplit. |
| 126 |
{ unfold FElem in *; extract_ex1_and_emp_in_goal; ssplit; try ecancel_assumption_impl. |
| 127 |
all: eauto. |
| 128 |
instantiate (1:=None). exact I. } |
| 129 |
{ reflexivity. } |
| 130 |
{ rewrite ?length_le_split. vm_compute. inversion 1. } |
| 131 |
repeat straightline. |
| 132 |
lazymatch goal with |
| 133 |
| H : Field.feval_bytes ?x = M.X0 ?P, H' : context [montladder_gallina] |- _ => |
| 134 |
rewrite H in H'; unfold M.X0 in H' |
| 135 |
end. |
| 136 |
lazymatch goal with |
| 137 |
| H : context [montladder_gallina] |- _ => |
| 138 |
rewrite (@montladder_gallina_equiv_affine (Curve25519.p) _ _ (Curve25519.field)) with |
| 139 |
(b_nonzero:=Curve25519.M.b_nonzero) (char_ge_3:=Curve25519.char_ge_3) in H; |
| 140 |
[ unfold FElem, Field.FElem in H; extract_ex1_and_emp_in H | Lia.lia | vm_decide | apply M.a2m4_nonsq ] |
| 141 |
end. |
| 142 |
straightline_call; ssplit. |
| 143 |
{ ecancel_assumption. } |
| 144 |
{ transitivity 32%nat; auto. } |
| 145 |
{ eexists. |
| 146 |
unfold FElem, Field.FElem in *; extract_ex1_and_emp_in_goal; ssplit. |
| 147 |
ecancel_assumption. } |
| 148 |
{ intuition idtac. } |
| 149 |
repeat straightline_cleanup. |
| 150 |
repeat straightline. |
| 151 |
|
| 152 |
cbv [Field.FElem] in *. |
| 153 |
repeat seprewrite_in @Bignum.Bignum_to_bytes H32; extract_ex1_and_emp_in H32. |
| 154 |
pose proof length_le_split 32 (Curve25519.clamp (le_combine s)). |
| 155 |
repeat straightline. |
| 156 |
cbv [x25519_spec]. |
| 157 |
use_sep_assumption; cancel. |
| 158 |
rewrite H36, le_combine_split. |
| 159 |
do 7 Morphisms.f_equiv. |
| 160 |
pose proof clamp_range (le_combine s). |
| 161 |
change (Z.of_nat (Z.to_nat (Z.log2 (Z.pos order)))) with 255. |
| 162 |
(rewrite_strat bottomup Z.mod_small); [ reflexivity | .. ]; try Lia.lia. |
| 163 |
Qed. |
| 164 |
|
| 165 |
Lemma x25519_base_ok : program_logic_goal_for_function! x25519_base. |
| 166 |
Proof. |
| 167 |
repeat straightline. |
| 168 |
|
| 169 |
straightline_call; ssplit; try ecancel_assumption; repeat straightline; try listZnWords; []. |
| 170 |
straightline_call; ssplit; try ecancel_assumption; repeat straightline; try listZnWords; []. |
| 171 |
|
| 172 |
straightline_call; ssplit. |
| 173 |
{ cbv [Field.FElem]. cbn. cbv [n]. ecancel_assumption_impl. } |
| 174 |
repeat straightline. |
| 175 |
|
| 176 |
seprewrite_in (@Bignum.Bignum_of_bytes _ _ _ _ _ _ 10 a2) H21. { transitivity 40%nat; trivial. } |
| 177 |
|
| 178 |
straightline_call; ssplit. |
| 179 |
{ unfold FElem, Field.FElem in *; extract_ex1_and_emp_in_goal; ssplit. |
| 180 |
{ use_sep_assumption. cancel; repeat ecancel_step. |
| 181 |
cancel_seps_at_indices 0%nat 0%nat; trivial. cbn [seps]. reflexivity. } |
| 182 |
all : eauto. |
| 183 |
{ instantiate (1:=None). exact I. } } |
| 184 |
{ reflexivity. } |
| 185 |
{ rewrite length_le_split. vm_compute. inversion 1. } |
| 186 |
repeat straightline. |
| 187 |
unfold FElem in H26. extract_ex1_and_emp_in H26. |
| 188 |
straightline_call; ssplit. |
| 189 |
{ ecancel_assumption. } |
| 190 |
{ transitivity 32%nat; auto. } |
| 191 |
{ eexists. ecancel_assumption. } |
| 192 |
{ intuition idtac. } |
| 193 |
repeat straightline_cleanup. |
| 194 |
repeat straightline. |
| 195 |
|
| 196 |
cbv [Field.FElem] in *. |
| 197 |
seprewrite_in @Bignum.Bignum_to_bytes H29. |
| 198 |
seprewrite_in @Bignum.Bignum_to_bytes H29. |
| 199 |
extract_ex1_and_emp_in H29. |
| 200 |
pose proof length_le_split 32 (Curve25519.clamp (le_combine s)). |
| 201 |
|
| 202 |
repeat straightline; intuition eauto. |
| 203 |
cbv [x25519_spec]. |
| 204 |
use_sep_assumption; cancel. |
| 205 |
rewrite H33, le_combine_split. |
| 206 |
do 7 Morphisms.f_equiv. |
| 207 |
pose proof clamp_range (le_combine s). |
| 208 |
change (Z.of_nat (Z.to_nat (Z.log2 (Z.pos order)))) with 255. |
| 209 |
(rewrite_strat bottomup Z.mod_small); [ | Lia.lia .. ]. |
| 210 |
lazymatch goal with |
| 211 |
| |- montladder_gallina _ _ _ ?x = _ => change x with (M.X0 M.B) |
| 212 |
end. |
| 213 |
unfold M.X0. |
| 214 |
rewrite (@montladder_gallina_equiv_affine (Curve25519.p) _ _ (Curve25519.field)) with |
| 215 |
(b_nonzero:=Curve25519.M.b_nonzero) (char_ge_3:=Curve25519.char_ge_3); |
| 216 |
[ | Lia.lia | vm_decide | apply M.a2m4_nonsq ]. |
| 217 |
change (Z.of_nat (Z.to_nat (Z.log2 (Z.pos order)))) with 255. |
| 218 |
(rewrite_strat bottomup Z.mod_small); [ | Lia.lia .. ]. |
| 219 |
reflexivity. |
| 220 |
Qed. |
| 221 |
|
| 222 |
Require Import coqutil.Word.Naive. |
| 223 |
Require Import coqutil.Macros.WithBaseName. |
| 224 |
|
| 225 |
Definition felem_cswap := CSwap.felem_cswap(word:=word32)(field_parameters:=field_parameters)(field_representaton:=field_representation n s c). |
| 226 |
Definition fe25519_inv := fe25519_inv(word:=word32)(field_parameters:=field_parameters). |
| 227 |
|
| 228 |
Definition funcs := |
| 229 |
&[,x25519; x25519_base; |
| 230 |
montladder; |
| 231 |
fe25519_to_bytes; |
| 232 |
fe25519_from_bytes; |
| 233 |
fe25519_from_word; |
| 234 |
felem_cswap; |
| 235 |
fe25519_copy; |
| 236 |
fe25519_inv; |
| 237 |
ladderstep; |
| 238 |
fe25519_mul; |
| 239 |
fe25519_add; |
| 240 |
fe25519_sub; |
| 241 |
fe25519_square; |
| 242 |
fe25519_scmula24; |
| 243 |
clamp; |
| 244 |
memmove ]. |
| 245 |
|
| 246 |
Require Import bedrock2.ToCString. |
| 247 |
Definition montladder_c_module := list_byte_of_string (ToCString.c_module funcs). |
| 248 |
|
| 249 |
Lemma link_montladder : spec_of_montladder (map.of_list funcs). |
| 250 |
Proof. |
| 251 |
unfold spec_of_montladder, ScalarMult.MontgomeryLadder.spec_of_montladder. |
| 252 |
unfold funcs. |
| 253 |
(* use montladder correctness proof *) |
| 254 |
rewrite montladder_defn. |
| 255 |
eapply @montladder_correct; try (typeclasses eauto). |
| 256 |
{ reflexivity. } |
| 257 |
{ Decidable.vm_decide. } |
| 258 |
{ Decidable.vm_decide. } |
| 259 |
{ reflexivity. } |
| 260 |
{ eapply CSwap.cswap_body_correct; [|exact I|reflexivity]. |
| 261 |
unfold field_representation, Signature.field_representation, Representation.frep; cbn; unfold n; cbv; trivial. } |
| 262 |
{ eapply fe25519_copy_correct. reflexivity. } |
| 263 |
{ eapply fe25519_from_word_correct. reflexivity. } |
| 264 |
{ |
| 265 |
cbv [LadderStep.spec_of_ladderstep]; intros. |
| 266 |
rewrite ladderstep_defn. |
| 267 |
eapply @LadderStep.ladderstep_correct; try (typeclasses eauto). |
| 268 |
{ cbv [Core.__rupicola_program_marker]; tauto. } |
| 269 |
{ reflexivity. } |
| 270 |
{ apply fe25519_mul_correct. reflexivity. } |
| 271 |
{ apply fe25519_add_correct. reflexivity. } |
| 272 |
{ apply fe25519_sub_correct. reflexivity. } |
| 273 |
{ apply fe25519_square_correct. reflexivity. } |
| 274 |
{ apply fe25519_scmula24_correct. reflexivity. } |
| 275 |
{ ecancel_assumption. } } |
| 276 |
{ unshelve eapply AdditionChains.fe25519_inv_correct_exp; [exact I|reflexivity| | ]. |
| 277 |
{ apply fe25519_square_correct. reflexivity. } |
| 278 |
{ apply fe25519_mul_correct. reflexivity. } } |
| 279 |
{ apply fe25519_mul_correct. reflexivity. } |
| 280 |
Qed. |