| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
Require Import Lia. |
| 5 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 6 |
Require Import Crypto.Bedrock.Field.Interface.Representation. |
| 7 |
Require Import Crypto.Bedrock.Field.Synthesis.New.ComputedOp. |
| 8 |
Require Import Crypto.Bedrock.Field.Synthesis.New.UnsaturatedSolinas. |
| 9 |
Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults32. |
| 10 |
Require Import Crypto.Bedrock.Specs.Field. |
| 11 |
Import ListNotations. |
| 12 |
|
| 13 |
(* Parameters for Poly1305 field. *) |
| 14 |
Section Field. |
| 15 |
Definition n : nat := 5. |
| 16 |
Definition s : Z := 2^130. |
| 17 |
Definition c : list (Z * Z) := [(1, 5)]%Z. |
| 18 |
|
| 19 |
Existing Instances Bitwidth32.BW32 |
| 20 |
Defaults32.default_parameters Defaults32.default_parameters_ok. |
| 21 |
Definition prefix : string := "fe1305_"%string. |
| 22 |
|
| 23 |
(* Define Poly1305 field *) |
| 24 |
Instance field_parameters : FieldParameters. |
| 25 |
Proof using Type. |
| 26 |
let M := (eval vm_compute in (Z.to_pos (UnsaturatedSolinas.m s c))) in |
| 27 |
(* dummy 'A' parameter since we don't care about scmula24 here *) |
| 28 |
let a := constr:(F.of_Z M 2) in |
| 29 |
let prefix := constr:("fe1305_"%string) in |
| 30 |
eapply |
| 31 |
(field_parameters_prefixed |
| 32 |
M ((a - F.of_Z _ 2) / F.of_Z _ 4)%F prefix). |
| 33 |
Defined. |
| 34 |
|
| 35 |
#[export] Instance frep1305 : FieldRepresentation := field_representation n s c. |
| 36 |
|
| 37 |
#[export] Instance frep1305_ok : FieldRepresentation_ok(field_representation:=frep1305). |
| 38 |
Proof. |
| 39 |
apply Crypto.Bedrock.Field.Synthesis.New.Signature.field_representation_ok. |
| 40 |
apply UnsaturatedSolinas.relax_valid. |
| 41 |
change felem_size_in_bytes with 20%Z. |
| 42 |
lia. |
| 43 |
Qed. |
| 44 |
|
| 45 |
(* Call fiat-crypto pipeline on all field operations *) |
| 46 |
Instance fe1305_ops : unsaturated_solinas_ops n s c. |
| 47 |
Proof using Type. Time constructor; make_computed_op. Defined. |
| 48 |
|
| 49 |
(**** Translate each field operation into bedrock2 and apply bedrock2 backend |
| 50 |
field pipeline proofs to prove the bedrock2 functions are correct. ****) |
| 51 |
|
| 52 |
Local Notation functions_contain functions f := |
| 53 |
(Interface.map.get functions (fst f) = Some (snd f)). |
| 54 |
|
| 55 |
Derive fe1305_from_bytes |
| 56 |
SuchThat (forall functions, |
| 57 |
functions_contain functions fe1305_from_bytes -> |
| 58 |
spec_of_from_bytes |
| 59 |
(field_representation:=frep1305) |
| 60 |
functions) |
| 61 |
As fe1305_from_bytes_correct. |
| 62 |
Proof. Time derive_bedrock2_func from_bytes_op. Qed. |
| 63 |
|
| 64 |
Derive fe1305_to_bytes |
| 65 |
SuchThat (forall functions, |
| 66 |
functions_contain functions fe1305_to_bytes -> |
| 67 |
spec_of_to_bytes |
| 68 |
(field_representation:=frep1305) |
| 69 |
functions) |
| 70 |
As fe1305_to_bytes_correct. |
| 71 |
Proof. Time derive_bedrock2_func to_bytes_op. Qed. |
| 72 |
|
| 73 |
Derive fe1305_mul |
| 74 |
SuchThat (forall functions, |
| 75 |
functions_contain functions fe1305_mul -> |
| 76 |
spec_of_BinOp bin_mul |
| 77 |
(field_representation:=frep1305) |
| 78 |
functions) |
| 79 |
As fe1305_mul_correct. |
| 80 |
Proof. Time derive_bedrock2_func mul_op. Qed. |
| 81 |
|
| 82 |
Derive fe1305_square |
| 83 |
SuchThat (forall functions, |
| 84 |
functions_contain functions fe1305_square -> |
| 85 |
spec_of_UnOp un_square |
| 86 |
(field_representation:=frep1305) |
| 87 |
functions) |
| 88 |
As fe1305_square_correct. |
| 89 |
Proof. Time derive_bedrock2_func square_op. Qed. |
| 90 |
|
| 91 |
Derive fe1305_add |
| 92 |
SuchThat (forall functions, |
| 93 |
functions_contain functions fe1305_add -> |
| 94 |
spec_of_BinOp bin_add |
| 95 |
(field_representation:=frep1305) |
| 96 |
functions) |
| 97 |
As fe1305_add_correct. |
| 98 |
Proof. Time derive_bedrock2_func add_op. Qed. |
| 99 |
|
| 100 |
Derive fe1305_carry_add |
| 101 |
SuchThat (forall functions, |
| 102 |
functions_contain functions fe1305_carry_add -> |
| 103 |
spec_of_BinOp bin_carry_add |
| 104 |
(field_representation:=frep1305) |
| 105 |
(functions)) |
| 106 |
As fe1305_carry_add_correct. |
| 107 |
Proof. Time derive_bedrock2_func carry_add_op. Qed. |
| 108 |
|
| 109 |
Derive fe1305_sub |
| 110 |
SuchThat (forall functions, |
| 111 |
functions_contain functions fe1305_sub -> |
| 112 |
spec_of_BinOp bin_sub |
| 113 |
(field_representation:=frep1305) |
| 114 |
functions) |
| 115 |
As fe1305_sub_correct. |
| 116 |
Proof. Time derive_bedrock2_func sub_op. Qed. |
| 117 |
|
| 118 |
Derive fe1305_carry_sub |
| 119 |
SuchThat (forall functions, |
| 120 |
functions_contain functions fe1305_carry_sub -> |
| 121 |
spec_of_BinOp bin_carry_sub |
| 122 |
(field_representation:=frep1305) |
| 123 |
(functions)) |
| 124 |
As fe1305_carry_sub_correct. |
| 125 |
Proof. Time derive_bedrock2_func carry_sub_op. Qed. |
| 126 |
End Field. |
| 127 |
|
| 128 |
(* Uncomment below to sanity-check that compilation works *) |
| 129 |
(* |
| 130 |
Require Import bedrock2.Syntax. |
| 131 |
Require Import compiler.Pipeline. |
| 132 |
Require Import compiler.MMIO. |
| 133 |
|
| 134 |
Definition funcs : list (string * func) := |
| 135 |
[ fe1305_mul; |
| 136 |
fe1305_add; |
| 137 |
fe1305_carry_add; |
| 138 |
fe1305_sub; |
| 139 |
fe1305_carry_sub; |
| 140 |
fe1305_square; |
| 141 |
fe1305_to_bytes; |
| 142 |
fe1305_from_bytes ]. |
| 143 |
|
| 144 |
#[local] |
| 145 |
Instance BWM_RV32IM : FlatToRiscvCommon.bitwidth_iset 32 Decode.RV32IM := eq_refl. |
| 146 |
|
| 147 |
Compute compile (compile_ext_call (funname_env:=SortedListString.map)) funcs. |
| 148 |
*) |