Debian Codegraph @

src/Bedrock/Secp256k1/Field256k1.v

raw
1 Require Import Coq.Strings.String.
2 Require Import Coq.Lists.List.
3 Require Import Coq.ZArith.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.Defaults64.
9 Require Import Crypto.Bedrock.Specs.Field.
10 Import BasicC64Semantics.
11 Import ListNotations.
12 Import BasicC64Semantics.
13
14 (* Parameters for Secp256k1 field. *)
15 Section Field.
16 Definition m : Z := (2^256 - 2^32 - 977)%Z.
17
18 Local Existing Instances Defaults64.default_parameters Defaults64.default_parameters_ok.
19 Definition prefix : string := "secp256k1_"%string.
20
21 (* Define Secp256k1 field *)
22 Instance field_parameters : FieldParameters.
23 Proof using Type.
24 let M := (eval vm_compute in (Z.to_pos (m))) in
25 (* 'A' parameter *)
26 let a := constr:(F.of_Z M 0) in
27 let prefix := constr:("secp256k1_"%string) in
28 eapply
29 (field_parameters_prefixed
30 M ((a + F.of_Z _ 2) / F.of_Z _ 4)%F prefix).
31 Defined.
32
33 #[export] Instance frep256k1 : Field.FieldRepresentation := field_representation m.
34
35 Definition to_mont_string := (prefix ++ "to_mont")%string.
36 Definition from_mont_string := (prefix ++ "from_mont")%string.
37
38 (* Call fiat-crypto pipeline on all field operations *)
39 Instance secp256k1_ops : @word_by_word_Montgomery_ops from_mont_string to_mont_string _ _ _ _ _ _ _ _ _ _ (WordByWordMontgomery.n m machine_wordsize) m.
40 Proof using Type. Time constructor; make_computed_op. Defined.
41
42 (**** Translate each field operation into bedrock2 and apply bedrock2 backend
43 field pipeline proofs to prove the bedrock2 functions are correct. ****)
44
45 Local Ltac begin_derive_bedrock2_func :=
46 lazymatch goal with
47 | |- context [spec_of_BinOp bin_mul] => eapply mul_func_correct
48 | |- context [spec_of_UnOp un_square] => eapply square_func_correct
49 | |- context [spec_of_BinOp bin_add] => eapply add_func_correct
50 | |- context [spec_of_BinOp bin_sub] => eapply sub_func_correct
51 | |- context [spec_of_UnOp un_opp] => eapply opp_func_correct
52 | |- context [spec_of_from_bytes] => eapply from_bytes_func_correct
53 | |- context [spec_of_to_bytes] => eapply to_bytes_func_correct
54 | |- context [spec_of_selectznz] => eapply select_znz_func_correct
55 | |- context [spec_of_felem_copy] => eapply felem_copy_func_correct
56 | |- context [spec_of_UnOp un_from_mont] => eapply (from_mont_func_correct _ _ _ from_mont_string to_mont_string)
57 | |- context [spec_of_UnOp un_to_mont] => eapply (to_mont_func_correct _ _ _ from_mont_string to_mont_string)
58 end.
59
60 Ltac epair :=
61 lazymatch goal with
62 | f := _ : string * Syntax.func |- _ =>
63 let p := open_constr:((_, _)) in
64 unify f p;
65 subst f
66 end.
67
68 Ltac derive_bedrock2_func op :=
69 epair;
70 begin_derive_bedrock2_func;
71 (* this goal fills in the evar, so do it first for [abstract] to be happy *)
72 try lazymatch goal with
73 | |- _ = b2_func _ => vm_compute; reflexivity
74 end;
75 (* solve all the remaining goals *)
76 lazymatch goal with
77 | |- _ = @ErrorT.Success ?ErrT unit tt =>
78 abstract (vm_cast_no_check (@eq_refl _ (@ErrorT.Success ErrT unit tt)))
79 | |- Func.valid_func _ =>
80 eapply Func.valid_func_bool_iff;
81 abstract vm_cast_no_check (eq_refl true)
82 | |- (_ = _)%Z => vm_compute; reflexivity
83 | |- (felem_size_in_bytes <= 2 ^ _)%Z => apply felem_size_ok
84 end.
85
86 Local Notation functions_contain functions f :=
87 (Interface.map.get functions (fst f) = Some (snd f)).
88
89 #[export] Instance frep256k1_raw : FieldRepresentation := field_representation_raw m.
90 #[export] Instance frep256k1_raw_ok : FieldRepresentation_ok (field_representation:=frep256k1_raw).
91 Proof.
92 apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok.
93 auto.
94 let sz := eval lazy in felem_size_in_bytes in change felem_size_in_bytes with sz.
95 Lia.lia.
96 Qed.
97
98 #[export] Instance frep256k1_ok : FieldRepresentation_ok(field_representation:=frep256k1).
99 Proof.
100 apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok.
101 auto.
102 let sz := eval lazy in felem_size_in_bytes in change felem_size_in_bytes with sz.
103 Lia.lia.
104 Qed.
105
106 Derive secp256k1_felem_copy
107 SuchThat (forall functions,
108 functions_contain functions secp256k1_felem_copy ->
109 spec_of_felem_copy
110 (field_representation:=frep256k1_raw)
111 functions)
112 As secp256k1_felem_copy_correct.
113 Proof. Time derive_bedrock2_func felem_copy_op. Qed.
114
115 Derive secp256k1_from_bytes
116 SuchThat (forall functions,
117 functions_contain functions secp256k1_from_bytes ->
118 spec_of_from_bytes
119 (field_representation:=frep256k1_raw)
120 functions)
121 As secp256k1_from_bytes_correct.
122 Proof. Time derive_bedrock2_func from_bytes_op. Qed.
123
124 Derive secp256k1_to_bytes
125 SuchThat (forall functions,
126 functions_contain functions secp256k1_to_bytes ->
127 spec_of_to_bytes
128 (field_representation:=frep256k1_raw)
129 functions)
130 As secp256k1_to_bytes_correct.
131 Proof. Time derive_bedrock2_func to_bytes_op. Qed.
132
133 Derive secp256k1_opp
134 SuchThat (forall functions,
135 functions_contain functions secp256k1_opp ->
136 spec_of_UnOp un_opp
137 (field_representation:=frep256k1)
138 functions)
139 As secp256k1_opp_correct.
140 Proof. Time derive_bedrock2_func opp_op. Qed.
141
142 Derive secp256k1_mul
143 SuchThat (forall functions,
144 functions_contain functions secp256k1_mul ->
145 spec_of_BinOp bin_mul
146 (field_representation:=frep256k1)
147 functions)
148 As secp256k1_mul_correct.
149 Proof. Time derive_bedrock2_func mul_op. Qed.
150
151 Derive secp256k1_square
152 SuchThat (forall functions,
153 functions_contain functions secp256k1_square ->
154 spec_of_UnOp un_square
155 (field_representation:=frep256k1)
156 functions)
157 As secp256k1_square_correct.
158 Proof. Time derive_bedrock2_func square_op. Qed.
159
160 Derive secp256k1_add
161 SuchThat (forall functions,
162 functions_contain functions secp256k1_add ->
163 spec_of_BinOp bin_add
164 (field_representation:=frep256k1)
165 functions)
166 As secp256k1_add_correct.
167 Proof. Time derive_bedrock2_func add_op. Qed.
168
169 Derive secp256k1_sub
170 SuchThat (forall functions,
171 functions_contain functions secp256k1_sub ->
172 spec_of_BinOp bin_sub
173 (field_representation:=frep256k1)
174 functions)
175 As secp256k1_sub_correct.
176 Proof. Time derive_bedrock2_func sub_op. Qed.
177
178 Derive secp256k1_select_znz
179 SuchThat (forall functions,
180 functions_contain functions secp256k1_select_znz ->
181 spec_of_selectznz
182 (field_representation:=frep256k1)
183 functions)
184 As secp256k1_select_znz_correct.
185 Proof. Time derive_bedrock2_func select_znz_op. Qed.
186
187 Derive secp256k1_from_mont
188 SuchThat (forall functions,
189 functions_contain functions secp256k1_from_mont ->
190 spec_of_UnOp un_from_mont
191 (field_representation:=frep256k1)
192 functions)
193 As secp256k1_from_mont_correct.
194 Proof. Time derive_bedrock2_func from_mont_op. Unshelve. 1,2: auto. Qed.
195
196 Derive secp256k1_to_mont
197 SuchThat (forall functions,
198 functions_contain functions secp256k1_to_mont ->
199 spec_of_UnOp un_to_mont
200 (field_representation:=frep256k1)
201 functions)
202 As secp256k1_to_mont_correct.
203 Proof. Time derive_bedrock2_func to_mont_op. Unshelve. 1,2: auto. Qed.
204 End Field.
205
206 (* Require Import bedrock2.Syntax. *)
207
208 (* Definition funcs : list (string * func) := *)
209 (* [ secp256k1_opp; *)
210 (* secp256k1_mul; *)
211 (* secp256k1_add; *)
212 (* secp256k1_sub; *)
213 (* secp256k1_square; *)
214 (* secp256k1_to_bytes; *)
215 (* secp256k1_from_bytes; *)
216 (* secp256k1_from_mont; *)
217 (* secp256k1_to_mont; *)
218 (* secp256k1_select_znz *)
219 (* ]. *)
220
221 (* Compute ToCString.c_module funcs. *)