| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import coqutil.Byte. |
| 5 |
Require Import coqutil.Word.Interface. |
| 6 |
Require Import bedrock2.Semantics. |
| 7 |
Require Import Crypto.Arithmetic.Core. |
| 8 |
Require Import Crypto.Arithmetic.ModOps. |
| 9 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 10 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 11 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 12 |
Require Import Crypto.Bedrock.Field.Synthesis.Generic.Bignum. |
| 13 |
Require Import Crypto.Bedrock.Specs.Field. |
| 14 |
Require Import Crypto.COperationSpecifications. |
| 15 |
Require Import Crypto.Util.ZRange. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 18 |
|
| 19 |
Section Representation. |
| 20 |
Context |
| 21 |
{width BW word mem locals ext_spec varname_gen error} |
| 22 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 23 |
Context {field_parameters : FieldParameters} |
| 24 |
{p_ok : Types.ok}. |
| 25 |
Context (n n_bytes : nat) (weight : nat -> Z) |
| 26 |
(bounds : Type) |
| 27 |
(list_in_bounds : bounds -> list Z -> Prop) |
| 28 |
(loose_bounds tight_bounds byte_bounds : bounds) |
| 29 |
(relax_bounds : |
| 30 |
forall X : list Z, |
| 31 |
list_in_bounds tight_bounds X -> |
| 32 |
list_in_bounds loose_bounds X) |
| 33 |
(*Transformation to applied to list before evaluating. For Solinas, this is the identity, |
| 34 |
and for word-by-word Montgomery, the argument must be pulled out of the Montgomery domain*) |
| 35 |
(eval_transformation : list Z -> list Z). |
| 36 |
|
| 37 |
Definition eval_words : list word -> F M_pos := |
| 38 |
fun ws => |
| 39 |
F.of_Z _ (Positional.eval weight n (eval_transformation (map word.unsigned ws))). |
| 40 |
|
| 41 |
Definition eval_bytes : list byte -> F M_pos := |
| 42 |
fun bs => |
| 43 |
F.of_Z _ (Positional.eval |
| 44 |
(ModOps.weight 8 1) |
| 45 |
n_bytes |
| 46 |
(map byte.unsigned bs)). |
| 47 |
|
| 48 |
Local Instance frep : FieldRepresentation := { |
| 49 |
feval := eval_words; |
| 50 |
feval_bytes := eval_bytes; |
| 51 |
felem_size_in_words := n; |
| 52 |
encoded_felem_size_in_bytes := n_bytes; |
| 53 |
bytes_in_bounds bs := list_in_bounds byte_bounds (map byte.unsigned bs); |
| 54 |
bounds := bounds; |
| 55 |
bounded_by bs ws := list_in_bounds bs (map word.unsigned ws); |
| 56 |
loose_bounds := loose_bounds; |
| 57 |
tight_bounds := tight_bounds; |
| 58 |
}. |
| 59 |
|
| 60 |
Context (felem_size_ok : felem_size_in_bytes <= 2 ^ width). |
| 61 |
|
| 62 |
Local Instance frep_ok : FieldRepresentation_ok. |
| 63 |
Proof. |
| 64 |
split. cbn [bounded_by frep]; intros. apply relax_bounds; auto. |
| 65 |
apply felem_size_ok. |
| 66 |
Qed. |
| 67 |
End Representation. |