Debian Codegraph @

src/Bedrock/Field/Synthesis/Examples/p224_64_new.v

raw
1 From Coq Require Import String.
2 From Coq Require Import List.
3 From Coq Require Import ZArith.
4 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
5 Require Import Crypto.Bedrock.Field.Interface.Representation.
6 Require Import Crypto.Bedrock.Field.Synthesis.New.ComputedOp.
7 Require Import Crypto.Bedrock.Field.Synthesis.New.WordByWordMontgomery.
8 Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults32.
9 Require Import Crypto.Bedrock.Specs.Field.
10 Import ListNotations.
11 Require Import bedrock2.WeakestPreconditionProperties.
12 Require Import bedrock2.WeakestPrecondition.
13 Require Import bedrock2.ProgramLogic.
14 Require Import bedrock2.Map.Separation.
15 Require Import bedrock2.Map.SeparationLogic.
16 Require Import bedrock2.Syntax.
17 Local Open Scope string_scope.
18 Local Infix "*" := sep : sep_scope.
19 Delimit Scope sep_scope with sep.
20 Local Notation function_t := ((String.string * (list String.string * list String.string * Syntax.cmd.cmd))%type).
21 Local Notation functions_t := (list function_t).
22 Local Open Scope sep_scope.
23
24 (* Parameters for p224 field. *)
25 Section Field.
26 Definition n : nat := 4.
27 Definition m : Z := (2^224 - 2^96 + 1)%Z.
28
29 Existing Instances Bitwidth32.BW32
30 Defaults32.default_parameters Defaults32.default_parameters_ok.
31 Definition prefix : string := "p224_"%string.
32
33 (* Define p224 field *)
34 Instance field_parameters : FieldParameters.
35 Proof using Type.
36 let M := (eval vm_compute in (Z.to_pos (m))) in
37 (* curve 'A' parameter *)
38 let a := constr:(F.of_Z M (m - 3)) in
39 let prefix := constr:("p224_felem_"%string) in
40 eapply
41 (field_parameters_prefixed
42 M ((a - F.of_Z _ 2) / F.of_Z _ 4)%F prefix).
43 Defined.
44
45 Definition to_mont_string := prefix ++ "to_mont".
46 Definition from_mont_string := prefix ++ "from_mont".
47
48 (* Call fiat-crypto pipeline on all field operations *)
49 Instance p224_ops : @word_by_word_Montgomery_ops from_mont_string to_mont_string _ _ _ _ _ _ _ _ _ _ (WordByWordMontgomery.n m machine_wordsize) m.
50 Proof using Type. Time constructor; make_computed_op. Defined.
51
52 Instance frep224 : FieldRepresentation := field_representation m.
53 Instance frep224_raw : FieldRepresentation := field_representation_raw m.
54 Instance frep224_ok : FieldRepresentation_ok (field_representation:=frep224).
55 Proof.
56 apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok.
57 intros. assumption.
58 let c := eval lazy in felem_size_in_bytes in change felem_size_in_bytes with c.
59 Lia.lia.
60 Defined.
61 Instance frep224_raw_ok : FieldRepresentation_ok (field_representation:=frep224_raw).
62 Proof.
63 apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok.
64 intros. assumption.
65 let c := eval lazy in felem_size_in_bytes in change felem_size_in_bytes with c.
66 Lia.lia.
67 Defined.
68
69
70 (**** Translate each field operation into bedrock2 and apply bedrock2 backend
71 field pipeline proofs to prove the bedrock2 functions are correct. ****)
72
73 Local Ltac begin_derive_bedrock2_func :=
74 lazymatch goal with
75 | |- context [spec_of_BinOp bin_mul] => eapply mul_func_correct
76 | |- context [spec_of_UnOp un_square] => eapply square_func_correct
77 | |- context [spec_of_BinOp bin_add] => eapply add_func_correct
78 | |- context [spec_of_BinOp bin_sub] => eapply sub_func_correct
79 | |- context [spec_of_UnOp un_opp] => eapply opp_func_correct
80 (* | |- context [spec_of_UnOp un_scmula24] => eapply scmula24_func_correct *)
81 | |- context [spec_of_from_bytes] => eapply from_bytes_func_correct
82 | |- context [spec_of_to_bytes] => eapply to_bytes_func_correct
83 end.
84
85 Ltac epair :=
86 lazymatch goal with
87 | f := _ : string * Syntax.func |- _ =>
88 let p := open_constr:((_, _)) in
89 unify f p;
90 subst f
91 end.
92
93 Ltac derive_bedrock2_func op :=
94 epair;
95 begin_derive_bedrock2_func;
96 (* this goal fills in the evar, so do it first for [abstract] to be happy *)
97 try lazymatch goal with
98 | |- _ = b2_func _ => vm_compute; reflexivity
99 end;
100 (* solve all the remaining goals *)
101 lazymatch goal with
102 | |- _ = @ErrorT.Success ?ErrT unit tt =>
103 abstract (vm_cast_no_check (@eq_refl _ (@ErrorT.Success ErrT unit tt)))
104 | |- Func.valid_func _ =>
105 eapply Func.valid_func_bool_iff;
106 abstract vm_cast_no_check (eq_refl true)
107 | |- (_ = _)%Z => vm_compute; reflexivity
108 | |- (felem_size_in_bytes <= 2 ^ _)%Z => apply felem_size_ok
109 end.
110
111 Local Notation functions_contain functions f :=
112 (Interface.map.get functions (fst f) = Some (snd f)).
113
114 Derive p224_from_bytes
115 SuchThat (forall functions,
116 functions_contain functions p224_from_bytes ->
117 spec_of_from_bytes
118 (field_representation:=frep224_raw)
119 functions)
120 As p224_from_bytes_correct.
121 Proof. Time derive_bedrock2_func from_bytes_op. Qed.
122
123 Derive p224_to_bytes
124 SuchThat (forall functions,
125 functions_contain functions p224_to_bytes ->
126 spec_of_to_bytes
127 (field_representation:=frep224_raw)
128 functions)
129 As p224_to_bytes_correct.
130 Proof. Time derive_bedrock2_func to_bytes_op. Qed.
131
132 Derive p224_mul
133 SuchThat (forall functions,
134 functions_contain functions p224_mul ->
135 spec_of_BinOp bin_mul
136 (field_representation:=frep224)
137 functions)
138 As p224_mul_correct.
139 Proof. Time derive_bedrock2_func mul_op. Qed.
140
141 Derive p224_square
142 SuchThat (forall functions,
143 functions_contain functions p224_square ->
144 spec_of_UnOp un_square
145 (field_representation:=frep224)
146 functions)
147 As p224_square_correct.
148 Proof. Time derive_bedrock2_func square_op. Qed.
149
150 Derive p224_add
151 SuchThat (forall functions,
152 functions_contain functions p224_add ->
153 spec_of_BinOp bin_add
154 (field_representation:=frep224)
155 functions)
156 As p224_add_correct.
157 Proof. Time derive_bedrock2_func add_op. Qed.
158
159 Derive p224_sub
160 SuchThat (forall functions,
161 functions_contain functions p224_sub ->
162 spec_of_BinOp bin_sub
163 (field_representation:=frep224)
164 functions)
165 As p224_sub_correct.
166 Proof. Time derive_bedrock2_func sub_op. Qed.
167
168 (*TODO: adapt derive_bedrock2_func to also derive the remaining functions.*)
169 Derive p224_from_mont
170 SuchThat (forall functions,
171 functions_contain functions p224_from_mont ->
172 spec_of_UnOp un_from_mont
173 (field_representation:=frep224)
174 functions)
175 As p224_from_mont_correct.
176 Proof.
177 epair.
178 eapply (from_mont_func_correct _ _ _ from_mont_string to_mont_string).
179 - vm_compute; reflexivity.
180 - apply felem_size_ok.
181 - eapply Func.valid_func_bool_iff. abstract vm_cast_no_check (eq_refl true).
182 Unshelve.
183 + auto.
184 + auto.
185 Qed.
186
187 Derive p224_to_mont
188 SuchThat (forall functions,
189 functions_contain functions p224_to_mont ->
190 spec_of_UnOp un_to_mont
191 (field_representation:=frep224)
192 functions)
193 As to_from_mont_correct.
194 Proof.
195 epair.
196 eapply (to_mont_func_correct _ _ _ from_mont_string to_mont_string).
197 - vm_compute; reflexivity.
198 - apply felem_size_ok.
199 - eapply Func.valid_func_bool_iff. abstract vm_cast_no_check (eq_refl true).
200 Unshelve. all: auto.
201 Qed.
202
203 Derive p224_select_znz
204 SuchThat (forall functions,
205 functions_contain functions p224_select_znz ->
206 spec_of_selectznz
207 (field_representation:=frep224)
208 functions)
209 As select_znz_correct.
210 Proof.
211 epair.
212 eapply select_znz_func_correct. 1,2:auto.
213 - vm_compute; reflexivity.
214 - apply felem_size_ok.
215 - eapply Func.valid_func_bool_iff. abstract vm_cast_no_check (eq_refl true).
216 Qed.
217 End Field.