Debian Codegraph @

src/Bedrock/End2End/X25519/Field25519.v

raw
1 Require Import Crypto.Spec.Curve25519.
2 From Coq Require Import String. Local Open Scope string_scope.
3 From Coq Require Import List.
4 From Coq Require Import ZArith.
5 Require Import coqutil.Word.Bitwidth32.
6 Require Import coqutil.Macros.WithBaseName.
7 Require Import Crypto.Arithmetic.PrimeFieldTheorems.
8 Require Import Crypto.Bedrock.Field.Common.Names.VarnameGenerator.
9 Require Import Crypto.Bedrock.Field.Interface.Representation.
10 Require Import Crypto.Bedrock.Field.Synthesis.New.ComputedOp.
11 Require Import Crypto.Bedrock.Field.Synthesis.New.UnsaturatedSolinas.
12 Require Import Crypto.Bedrock.Field.Translation.Parameters.FE310.
13 Require Import Crypto.Bedrock.Specs.Field.
14 Import ListNotations.
15
16 #[export]
17 Existing Instances Naive.word Naive.word32_ok BW32.
18 #[export]
19 Existing Instances SortedListWord.map SortedListWord.ok.
20
21 (* Parameters for Curve25519 field (32-bit machine). *)
22 Section Field.
23 Context {ext_spec: Semantics.ExtSpec} {ext_spec_ok : Semantics.ext_spec.ok ext_spec}.
24 Definition n : nat := 10.
25 Definition s : Z := 2^255.
26 Definition c : list (Z * Z) := [(1, 19)]%Z.
27
28 Definition prefix : string := "fe25519_"%string.
29
30 (* Note: Cannot use Defaults32.default_parameters here as the parameters for
31 the bedrock2 backend because it uses BasicC32Semantics.ext_spec, while we
32 will eventually want to plug in FE310CSemantics.ext_spec. TODO: is there a
33 way to prove the ext_spec doesn't matter here, since we're not using any MMIO? *)
34 (* Parameters for the fiat-crypto bedrock2 backend *)
35 Instance translation_parameters : Types.parameters
36 (varname_gen := default_varname_gen)
37 (error := Syntax.expr.var Defaults.ERROR)
38 := tt.
39 Instance translation_parameters_ok : Types.ok.
40 Proof using ext_spec_ok. constructor; try exact _; apply prefix_name_gen_unique. Qed.
41
42 Instance field_parameters : FieldParameters :=
43 field_parameters_prefixed Curve25519.p Curve25519.M.a24 "fe25519_"%string.
44
45 #[export] Instance frep25519 : FieldRepresentation := field_representation n s c.
46
47 (* Call fiat-crypto pipeline on all field operations *)
48 Instance fe25519_ops : unsaturated_solinas_ops (ext_spec:=ext_spec) n s c.
49 Proof using Type. Time constructor; make_computed_op. Defined.
50
51 #[export] Instance frep25519_ok : FieldRepresentation_ok(field_representation:=frep25519).
52 Proof.
53 apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok.
54 apply UnsaturatedSolinas.relax_valid.
55 change felem_size_in_bytes with 40%Z. Lia.lia.
56 Qed.
57
58 (**** Translate each field operation into bedrock2 and apply bedrock2 backend
59 field pipeline proofs to prove the bedrock2 functions are correct. ****)
60
61 Derive fe25519_from_bytes
62 SuchThat (forall functions,
63 Interface.map.get functions "fe25519_from_bytes" = Some fe25519_from_bytes ->
64 spec_of_from_bytes
65 (ext_spec:=ext_spec)
66 (field_representation:=frep25519)
67 functions)
68 As fe25519_from_bytes_correct.
69 Proof. Time derive_bedrock2_func from_bytes_op. Qed.
70
71 Derive fe25519_to_bytes
72 SuchThat (forall functions,
73 Interface.map.get functions "fe25519_to_bytes" = Some fe25519_to_bytes ->
74 spec_of_to_bytes
75 (field_representation:=frep25519)
76 functions)
77 As fe25519_to_bytes_correct.
78 Proof. Time derive_bedrock2_func to_bytes_op. Qed.
79
80 Derive fe25519_copy
81 SuchThat (forall functions,
82 Interface.map.get functions "fe25519_copy" = Some fe25519_copy ->
83 spec_of_felem_copy
84 (field_representation:=frep25519)
85 functions)
86 As fe25519_copy_correct.
87 Proof. Time derive_bedrock2_func felem_copy_op. Qed.
88
89 Derive fe25519_from_word
90 SuchThat (forall functions,
91 Interface.map.get functions "fe25519_from_word" = Some fe25519_from_word ->
92 spec_of_from_word
93 (field_representation:=frep25519)
94 functions)
95 As fe25519_from_word_correct.
96 Proof. Time derive_bedrock2_func from_word_op. Qed.
97
98 Derive fe25519_mul
99 SuchThat (forall functions,
100 Interface.map.get functions "fe25519_mul" = Some fe25519_mul ->
101 spec_of_BinOp bin_mul
102 (field_representation:=frep25519)
103 functions)
104 As fe25519_mul_correct.
105 Proof. Time derive_bedrock2_func mul_op. Qed.
106
107 Derive fe25519_square
108 SuchThat (forall functions,
109 Interface.map.get functions "fe25519_square" = Some fe25519_square ->
110 spec_of_UnOp un_square
111 (field_representation:=frep25519)
112 functions)
113 As fe25519_square_correct.
114 Proof. Time derive_bedrock2_func square_op. Qed.
115
116 Derive fe25519_add
117 SuchThat (forall functions,
118 Interface.map.get functions "fe25519_add" = Some fe25519_add ->
119 spec_of_BinOp bin_add
120 (field_representation:=frep25519)
121 functions)
122 As fe25519_add_correct.
123 Proof. Time derive_bedrock2_func add_op. Qed.
124
125 Derive fe25519_carry_add
126 SuchThat (forall functions,
127 Interface.map.get functions "fe25519_carry_add" = Some fe25519_carry_add ->
128 spec_of_BinOp bin_carry_add
129 (field_representation:=frep25519)
130 functions)
131 As fe25519_carry_add_correct.
132 Proof. Time derive_bedrock2_func carry_add_op. Qed.
133
134 Derive fe25519_sub
135 SuchThat (forall functions,
136 Interface.map.get functions "fe25519_sub" = Some fe25519_sub ->
137 spec_of_BinOp bin_sub
138 (field_representation:=frep25519)
139 functions)
140 As fe25519_sub_correct.
141 Proof. Time derive_bedrock2_func sub_op. Qed.
142
143 Derive fe25519_carry_sub
144 SuchThat (forall functions,
145 Interface.map.get functions "fe25519_carry_sub" = Some fe25519_carry_sub ->
146 spec_of_BinOp bin_carry_sub
147 (field_representation:=frep25519)
148 functions)
149 As fe25519_carry_sub_correct.
150 Proof. Time derive_bedrock2_func carry_sub_op. Qed.
151
152 Derive fe25519_scmula24
153 SuchThat (forall functions,
154 Interface.map.get functions "fe25519_scmula24" = Some fe25519_scmula24 ->
155 spec_of_UnOp un_scmula24
156 (field_representation:=frep25519)
157 functions)
158 As fe25519_scmula24_correct.
159 Proof. Time derive_bedrock2_func scmula24_op. Qed.
160 End Field.