| 1 |
Require Import bedrock2.Semantics. |
| 2 |
Require Import Rupicola.Lib.Api. Import bedrock2.WeakestPrecondition. |
| 3 |
Require Import Crypto.Algebra.Group. |
| 4 |
Require Import Crypto.Algebra.Hierarchy. |
| 5 |
Require Import Crypto.Algebra.ScalarMult. |
| 6 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 7 |
|
| 8 |
Class GroupParameters := |
| 9 |
{ (** mathematical parameters **) |
| 10 |
G : Type; |
| 11 |
eq : G -> G -> Prop; |
| 12 |
add : G -> G -> G; |
| 13 |
zero : G; |
| 14 |
opp : G -> G; |
| 15 |
scalarmult : Z -> G -> G; |
| 16 |
|
| 17 |
(** function names for bedrock2 **) |
| 18 |
scmul : string; |
| 19 |
}. |
| 20 |
|
| 21 |
Class GroupParameters_ok {group_parameters : GroupParameters} := |
| 22 |
{ group_ok : @group G eq add zero opp; |
| 23 |
scalarmult_ok : @is_scalarmult G eq add zero opp scalarmult; |
| 24 |
}. |
| 25 |
|
| 26 |
Class GroupRepresentation {G : Type} {width} {BW:Bitwidth.Bitwidth width} {word : word width} {mem : map.map word byte} := |
| 27 |
{ gelem : Type; |
| 28 |
grepresents : gelem -> G -> Prop; |
| 29 |
GElem : word -> gelem -> mem -> Prop; |
| 30 |
}. |
| 31 |
|
| 32 |
Section FunctionSpecs. |
| 33 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 34 |
Context {locals: map.map String.string word}. |
| 35 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 36 |
Context {group_parameters : GroupParameters} |
| 37 |
{group_representaton : GroupRepresentation (G:=G)} |
| 38 |
{scalarbytes : nat}. |
| 39 |
|
| 40 |
(* N.B. spec_of_scmul has only one separation-logic condition for now because |
| 41 |
using multiple results in problems with stack allocation. Should be further |
| 42 |
looked into. *) |
| 43 |
Instance spec_of_scmul : spec_of scmul := |
| 44 |
fnspec! scmul (pout px pk : word) |
| 45 |
/ (x out : gelem) bs (X : G) R, |
| 46 |
{ requires tr mem := |
| 47 |
length bs = scalarbytes /\ |
| 48 |
grepresents x X |
| 49 |
/\ (GElem pout out * GElem px x * array ptsto (word.of_Z 1) pk bs * R)%sep mem; |
| 50 |
ensures tr' mem' := |
| 51 |
tr = tr' /\ |
| 52 |
exists (xk : gelem), |
| 53 |
grepresents xk (scalarmult (LittleEndianList.le_combine bs) X) |
| 54 |
/\ (GElem pout xk * GElem px x * array ptsto (word.of_Z 1) pk bs * R)%sep mem' }. |
| 55 |
End FunctionSpecs. |
| 56 |
|
| 57 |
Global Existing Instance spec_of_scmul. |