| 1 |
From Coq Require Import Lia. |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import List. |
| 4 |
From Coq Require Import ZArith. |
| 5 |
Require Import bedrock2.Syntax. |
| 6 |
Require Import Crypto.Arithmetic.Core. |
| 7 |
Require Import Crypto.Spec.ModularArithmetic. |
| 8 |
Require Import Crypto.Arithmetic.ModularArithmeticTheorems. |
| 9 |
Require Import Crypto.Arithmetic.UniformWeight. |
| 10 |
Require Import Crypto.Arithmetic.WordByWordMontgomery. |
| 11 |
Require Import Crypto.Bedrock.Specs.Field. |
| 12 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 13 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 14 |
Require Import Crypto.Bedrock.Field.Common.Arrays.ByteBounds. |
| 15 |
Require Import Crypto.Bedrock.Field.Common.Names.VarnameGenerator. |
| 16 |
Require Import Crypto.Bedrock.Field.Synthesis.New.ComputedOp. |
| 17 |
Require Import Crypto.Bedrock.Field.Synthesis.New.Signature. |
| 18 |
Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults. |
| 19 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Func. |
| 20 |
Require Import Crypto.BoundsPipeline. |
| 21 |
Require Import Crypto.COperationSpecifications. |
| 22 |
Require Import Crypto.PushButtonSynthesis.WordByWordMontgomery. |
| 23 |
Require Import Crypto.Language.API. |
| 24 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 25 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 26 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 27 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 28 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 29 |
Import ListNotations API.Compilers Types.Notations. |
| 30 |
|
| 31 |
Class word_by_word_Montgomery_ops |
| 32 |
{from_mont to_mont : string} |
| 33 |
{width BW word mem locals ext_spec varname_gen error} |
| 34 |
{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error} |
| 35 |
{field_parameters : FieldParameters} |
| 36 |
{n m} : Type := |
| 37 |
{ mul_op : |
| 38 |
computed_op |
| 39 |
(WordByWordMontgomery.mul m width) Field.mul |
| 40 |
list_binop_insizes list_binop_outsizes (list_binop_inlengths n); |
| 41 |
add_op : |
| 42 |
computed_op |
| 43 |
(WordByWordMontgomery.add m width) Field.add |
| 44 |
list_binop_insizes list_binop_outsizes (list_binop_inlengths n); |
| 45 |
sub_op : |
| 46 |
computed_op |
| 47 |
(WordByWordMontgomery.sub m width) Field.sub |
| 48 |
list_binop_insizes list_binop_outsizes (list_binop_inlengths n); |
| 49 |
opp_op : |
| 50 |
computed_op |
| 51 |
(WordByWordMontgomery.opp m width) Field.opp |
| 52 |
list_unop_insizes list_unop_outsizes (list_unop_inlengths n); |
| 53 |
square_op : |
| 54 |
computed_op |
| 55 |
(WordByWordMontgomery.square m width) |
| 56 |
Field.square |
| 57 |
list_unop_insizes list_unop_outsizes (list_unop_inlengths n); |
| 58 |
felem_copy_op : |
| 59 |
computed_op |
| 60 |
(WordByWordMontgomery.copy m width) Field.felem_copy |
| 61 |
list_unop_insizes list_unop_outsizes (list_unop_inlengths n); |
| 62 |
from_bytes_op : |
| 63 |
computed_op |
| 64 |
(WordByWordMontgomery.from_bytes m width) |
| 65 |
Field.from_bytes |
| 66 |
from_bytes_insizes from_bytes_outsizes (from_bytes_inlengths |
| 67 |
(n_bytes m)); |
| 68 |
to_bytes_op : |
| 69 |
computed_op |
| 70 |
(WordByWordMontgomery.to_bytes m width) |
| 71 |
Field.to_bytes |
| 72 |
to_bytes_insizes to_bytes_outsizes (to_bytes_inlengths n); |
| 73 |
from_mont_op : |
| 74 |
computed_op |
| 75 |
(WordByWordMontgomery.from_montgomery m width) |
| 76 |
from_mont |
| 77 |
list_unop_insizes list_unop_outsizes (list_unop_inlengths n); |
| 78 |
to_mont_op : |
| 79 |
computed_op |
| 80 |
(WordByWordMontgomery.to_montgomery m width) |
| 81 |
to_mont |
| 82 |
list_unop_insizes list_unop_outsizes (list_unop_inlengths n); |
| 83 |
select_znz_op : |
| 84 |
computed_op |
| 85 |
(WordByWordMontgomery.selectznz m width) |
| 86 |
Field.select_znz |
| 87 |
list_selectznz_insizes list_selectznz_outsizes (list_selectznz_inlengths n) |
| 88 |
}. |
| 89 |
|
| 90 |
Arguments word_by_word_Montgomery_ops {_ _ _ _ _ _ _ _ _ _ _ _} n. |
| 91 |
|
| 92 |
(** We need to tell [check_args] that we are requesting these functions in order to get the relevant properties out *) |
| 93 |
Notation necessary_requests := ["to_bytes"; "from_bytes"]%string (only parsing). |
| 94 |
|
| 95 |
Section WordByWordMontgomery. |
| 96 |
Context |
| 97 |
{width BW word mem locals ext_spec error} |
| 98 |
{parameters_sentinel : @parameters width BW word mem locals ext_spec default_varname_gen error} |
| 99 |
{field_parameters : FieldParameters} |
| 100 |
{field_parameters_ok : FieldParameters_ok} |
| 101 |
{ok : Types.ok}. |
| 102 |
|
| 103 |
Context (m : Z) |
| 104 |
(M_eq : M = m) |
| 105 |
(check_args_ok : |
| 106 |
check_args m width necessary_requests (ErrorT.Success tt) |
| 107 |
= ErrorT.Success tt) |
| 108 |
. |
| 109 |
Definition r' := @Field.r' width field_parameters. |
| 110 |
|
| 111 |
Lemma m_big : (2 < m)%Z. |
| 112 |
Proof using check_args_ok. |
| 113 |
pose proof (use_curve_good m width _ check_args_ok) as H. |
| 114 |
apply (OrdersEx.Z_as_DT.le_neq 2 m). split; try lia. |
| 115 |
destruct (m =? 2)%Z eqn:eq; try lia. |
| 116 |
assert (H' : ((r width * PushButtonSynthesis.WordByWordMontgomery.r' m width) mod m)%Z = |
| 117 |
1%Z) by apply H. |
| 118 |
apply Z.eqb_eq in eq. clear H. |
| 119 |
rewrite Z.mul_mod in H'; try lia. rewrite eq in H'. cbv [r] in H'. |
| 120 |
rewrite PullPush.Z.pow_mod_push in H'; [| cbv; auto]. |
| 121 |
rewrite Z_mod_same in H'; try lia. |
| 122 |
rewrite Z.pow_0_l in H'; try lia. |
| 123 |
pose proof (use_curve_good m width _ check_args_ok); lia. |
| 124 |
Qed. |
| 125 |
|
| 126 |
Lemma gcd_aux' : forall n m (e : nat), (Z.gcd n m = 1)%Z -> (Z.gcd (n ^ (Z.of_nat e)) m = 1)%Z. |
| 127 |
Proof using Type. |
| 128 |
intros. induction e; auto. |
| 129 |
- destruct m0; auto. |
| 130 |
- apply Znumtheory.Zgcd_1_rel_prime. |
| 131 |
apply Znumtheory.rel_prime_sym. |
| 132 |
apply Zpow_facts.rel_prime_Zpower_r; try lia. |
| 133 |
apply Znumtheory.rel_prime_sym. |
| 134 |
apply Znumtheory.Zgcd_1_rel_prime. auto. |
| 135 |
Qed. |
| 136 |
|
| 137 |
Lemma r'_correct : ((2 ^ (width) * r' ) mod M = 1)%Z. (*Not very elegant proof...*) |
| 138 |
Proof using M_eq check_args_ok. |
| 139 |
cbv [r' Field.r' Field.r]. |
| 140 |
assert (H1mod : (1 = 1 mod M)%Z). |
| 141 |
{ |
| 142 |
rewrite Zmod_small; split; try lia; rewrite M_eq; eapply use_curve_good; eauto. |
| 143 |
} |
| 144 |
rewrite H1mod. |
| 145 |
apply (ModInv.Z.modinv_correct). |
| 146 |
- rewrite M_eq. pose proof (use_curve_good m width _ check_args_ok). try lia. |
| 147 |
- rewrite Z.gcd_abs_l. apply Z.bezout_1_gcd. cbv [Z.Bezout]. |
| 148 |
pose proof (use_curve_good _ _ _ check_args_ok) as H'. |
| 149 |
do 11 destruct H' as [_ H']. destruct H' as [H' _]. |
| 150 |
pose proof (Modulo.Z.div_mod'' (r width * PushButtonSynthesis.WordByWordMontgomery.r' m width) m) as H. |
| 151 |
assert (H1 : (m <> 0)%Z). |
| 152 |
{ |
| 153 |
pose proof (use_curve_good _ _ _ check_args_ok); lia. |
| 154 |
} |
| 155 |
specialize(H H1). eexists. eexists. cbv [r] in *. |
| 156 |
apply (f_equal (fun y => y - m * |
| 157 |
(2 ^ width * PushButtonSynthesis.WordByWordMontgomery.r' m width / m))%Z) in H. |
| 158 |
rewrite OrdersEx.Z_as_DT.add_simpl_r in H. |
| 159 |
rewrite <- H in H'. clear H1 H. |
| 160 |
assert (H0 : (forall x y, x - y = x + (- y))%Z) by auto with zarith. |
| 161 |
rewrite H0 in H'. clear H0. |
| 162 |
assert (H : (forall x y, - (x * y) = (- y) * x)%Z) by auto with zarith. |
| 163 |
rewrite H in H'. rewrite Z.mul_comm in H'. rewrite M_eq. apply H'. |
| 164 |
Qed. |
| 165 |
|
| 166 |
Local Notation n := (WordByWordMontgomery.n m width). |
| 167 |
|
| 168 |
Context (from_mont : string) |
| 169 |
(to_mont : string) |
| 170 |
(ops : word_by_word_Montgomery_ops n m) |
| 171 |
mul_func add_func sub_func opp_func square_func |
| 172 |
felem_copy_func from_bytes_func to_bytes_func |
| 173 |
from_mont_func to_mont_func select_znz_func |
| 174 |
(mul_func_eq : mul_func = b2_func mul_op) |
| 175 |
(add_func_eq : add_func = b2_func add_op) |
| 176 |
(sub_func_eq : sub_func = b2_func sub_op) |
| 177 |
(opp_func_eq : opp_func = b2_func opp_op) |
| 178 |
(square_func_eq : square_func = b2_func square_op) |
| 179 |
(felem_copy_func_eq : felem_copy_func = b2_func felem_copy_op) |
| 180 |
(from_bytes_func_eq : from_bytes_func = b2_func from_bytes_op) |
| 181 |
(to_bytes_func_eq : to_bytes_func = b2_func to_bytes_op) |
| 182 |
(from_mont_func_eq : from_mont_func = b2_func from_mont_op) |
| 183 |
(to_mont_func_eq : to_mont_func = b2_func to_mont_op) |
| 184 |
(select_znz_func_eq : select_znz_func = b2_func (@select_znz_op from_mont to_mont _ _ _ _ _ _ _ _ _ _ _ _ _)). |
| 185 |
|
| 186 |
Local Notation weight := (uweight width) (only parsing). |
| 187 |
Definition eval_trans := (WordByWordMontgomery.from_montgomerymod width n m (WordByWordMontgomery.m' m width)). |
| 188 |
Definition eval_raw : (list Z -> list Z) := fun x => x. |
| 189 |
|
| 190 |
Inductive bounds_type := (*Bounds type for WBW. must distinguish between bounds for lists of words and lists of bytes*) |
| 191 |
| wordlist |
| 192 |
| bytelist |
| 193 |
. |
| 194 |
|
| 195 |
Definition list_in_bounds bounds_type x := |
| 196 |
match bounds_type with |
| 197 |
| wordlist => WordByWordMontgomery.valid width n m x |
| 198 |
| bytelist => WordByWordMontgomery.valid 8 (n_bytes m) m x |
| 199 |
end. |
| 200 |
|
| 201 |
Local Instance field_representation_raw : FieldRepresentation |
| 202 |
:= Signature.field_representation |
| 203 |
n (n_bytes m) weight bounds_type |
| 204 |
wordlist |
| 205 |
wordlist |
| 206 |
bytelist |
| 207 |
list_in_bounds eval_raw. |
| 208 |
|
| 209 |
Local Instance field_representation : FieldRepresentation |
| 210 |
:= field_representation |
| 211 |
n (n_bytes m) weight bounds_type |
| 212 |
wordlist |
| 213 |
wordlist |
| 214 |
bytelist |
| 215 |
list_in_bounds eval_trans. |
| 216 |
|
| 217 |
Context (felem_size_ok : (felem_size_in_bytes <= 2 ^ width)%Z). |
| 218 |
|
| 219 |
Local Ltac specialize_correctness_hyp Hcorrect := |
| 220 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 221 |
field_representation Signature.field_representation |
| 222 |
Representation.frep Representation.eval_bytes |
| 223 |
Representation.eval_words |
| 224 |
bin_model bin_xbounds bin_ybounds |
| 225 |
un_model un_xbounds |
| 226 |
] in *; |
| 227 |
lazymatch type of Hcorrect with |
| 228 |
| forall x y, ?Px x -> ?Py y -> _ => |
| 229 |
match goal with |
| 230 |
| Hx : Px ?x, Hy : Py ?y |- _ => |
| 231 |
specialize (Hcorrect x y Hx Hy) |
| 232 |
end |
| 233 |
| forall x, ?Px x -> _ => |
| 234 |
match goal with |
| 235 |
| Hx : Px ?x |- _ => |
| 236 |
specialize (Hcorrect x Hx) |
| 237 |
end |
| 238 |
end. |
| 239 |
|
| 240 |
Lemma loose_bounds_eq : Field.loose_bounds = wordlist. |
| 241 |
Proof using Type. reflexivity. Qed. |
| 242 |
Lemma tight_bounds_eq : Field.tight_bounds = wordlist. |
| 243 |
Proof using Type. reflexivity. Qed. |
| 244 |
|
| 245 |
(* TODO: move to coqutil.Datatypes.List *) |
| 246 |
Lemma Forall_repeat : forall {A} (R : A -> Prop) n x, |
| 247 |
R x -> Forall R (repeat x n). |
| 248 |
Proof using. clear. |
| 249 |
intros; induction n; intros; cbn [repeat]; |
| 250 |
constructor; auto. |
| 251 |
Qed. |
| 252 |
|
| 253 |
Local Hint Resolve |
| 254 |
(* relax_valid *) |
| 255 |
func_eq |
| 256 |
inname_gen_varname_gen_disjoint |
| 257 |
outname_gen_varname_gen_disjoint |
| 258 |
(* length_tight_bounds *) |
| 259 |
(* length_loose_bounds *) |
| 260 |
(* length_byte_bounds *) |
| 261 |
(* byte_bounds_tighter_than *) |
| 262 |
: helpers. |
| 263 |
|
| 264 |
Local Hint Unfold |
| 265 |
Solinas.selectznz_correct |
| 266 |
Solinas.from_bytes_correct |
| 267 |
Solinas.to_bytes_correct |
| 268 |
Solinas.carry_mul_correct |
| 269 |
Solinas.carry_square_correct |
| 270 |
Solinas.carry_scmul_const_correct |
| 271 |
Solinas.add_correct |
| 272 |
Solinas.sub_correct |
| 273 |
Solinas.opp_correct |
| 274 |
Solinas.carry_correct |
| 275 |
Solinas.zero_correct |
| 276 |
Solinas.one_correct : solinas_specs. |
| 277 |
|
| 278 |
(* We wrap the tactic in [once] so it doesn't take forever to fail *) |
| 279 |
Ltac handle_side_conditions := |
| 280 |
once |
| 281 |
(lazymatch goal with |
| 282 |
| |- API.Wf (res ?op) |
| 283 |
=> generalize (res op), (res_eq op); |
| 284 |
(* Kludge around auto being bad (COQBUG(https://github.com/coq/coq/issues/14190)) and eauto not respecting Hint Opaque *) |
| 285 |
typeclasses eauto with wf_op_cache; |
| 286 |
let e := lazymatch goal with | |- forall res, ?e = _ -> API.Wf _ => e end in |
| 287 |
idtac "Warning: Falling back to manually proving pipeline well-formedness for" e; |
| 288 |
PipelineTactics.prove_pipeline_wf () |
| 289 |
| |- context [Field.tight_bounds] => rewrite tight_bounds_eq |
| 290 |
| |- context [Field.loose_bounds] => rewrite loose_bounds_eq |
| 291 |
| _ => idtac |
| 292 |
end; |
| 293 |
lazymatch goal with |
| 294 |
| |- context [expr.interp] => |
| 295 |
cbv [expr.Interp] in *; autounfold with solinas_specs in * |
| 296 |
| _ => eauto with helpers |
| 297 |
end). |
| 298 |
|
| 299 |
Hint Resolve relax_list_Z_bounded_by partition_bounded_by : bounds. |
| 300 |
|
| 301 |
Ltac simpl_map_unsigned := |
| 302 |
lazymatch goal with |
| 303 |
| |- context [map Interface.word.unsigned |
| 304 |
(map Interface.word.of_Z _)] => |
| 305 |
rewrite map_unsigned_of_Z; |
| 306 |
erewrite MaxBounds.map_word_wrap_bounded |
| 307 |
by eauto with bounds |
| 308 |
| |- context [map Byte.byte.unsigned |
| 309 |
(map Byte.byte.of_Z _)] => |
| 310 |
rewrite byte_map_unsigned_of_Z; |
| 311 |
erewrite map_byte_wrap_bounded |
| 312 |
by eauto with bounds |
| 313 |
end. |
| 314 |
Ltac FtoZ := |
| 315 |
apply F.eq_of_Z_iff; rewrite ?F.to_Z_of_Z; |
| 316 |
cbv [M] in M_eq; rewrite ?M_eq; pull_Zmod. |
| 317 |
|
| 318 |
(* Ltac bounds_length := simpl; intros; erewrite length_list_Z_bounded_by; eauto; try apply length_tight_bounds; try apply length_loose_bounds. *) |
| 319 |
Lemma valid_bounded_by_prime_bounds x : |
| 320 |
(check_args m width [] (ErrorT.Success tt) |
| 321 |
= ErrorT.Success tt) -> |
| 322 |
WordByWordMontgomery.valid width n m x -> |
| 323 |
list_Z_bounded_by (prime_bounds m width) x. |
| 324 |
Proof using Type. |
| 325 |
intros; unshelve eapply bounded_by_prime_bounds_of_valid; eauto. |
| 326 |
Qed. |
| 327 |
|
| 328 |
Ltac maxbounds_from_valid := intros _ Hvalid; destruct Hvalid as [Hsmall _]; rewrite Hsmall; apply MaxBounds.partition_bounded_by. |
| 329 |
|
| 330 |
Lemma valid_max_bounds n0 : forall x, @WordByWordMontgomery.valid width n0 m x ->list_Z_bounded_by (@MaxBounds.max_bounds width n0) x. |
| 331 |
Proof using ok. |
| 332 |
intros. destruct H. rewrite H. eapply MaxBounds.partition_bounded_by. |
| 333 |
Qed. |
| 334 |
|
| 335 |
Lemma valid_length : forall x, WordByWordMontgomery.valid width n m x -> length x = n. |
| 336 |
Proof using Type. |
| 337 |
intros. destruct H. erewrite WordByWordMontgomery.length_small; eauto. |
| 338 |
Qed. |
| 339 |
|
| 340 |
Lemma mul_func_correct : |
| 341 |
valid_func (res mul_op _) -> |
| 342 |
forall functions, Interface.map.get functions Field.mul = Some mul_func -> |
| 343 |
spec_of_BinOp bin_mul functions. |
| 344 |
Proof using M_eq check_args_ok mul_func_eq ok felem_size_ok. |
| 345 |
cbv [spec_of_BinOp bin_mul]. rewrite mul_func_eq. intros. |
| 346 |
pose proof mul_correct |
| 347 |
m width _ ltac:(eassumption) _ (res_eq mul_op) |
| 348 |
as Hcorrect. |
| 349 |
eapply list_binop_correct with (res:=res mul_op); [ .. | eassumption ]; |
| 350 |
try handle_side_conditions; [| | eapply valid_max_bounds; eauto | apply valid_length]. |
| 351 |
{ |
| 352 |
(* output *value* is correct *) |
| 353 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 354 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 355 |
field_representation Signature.field_representation |
| 356 |
Representation.frep Representation.eval_bytes |
| 357 |
Representation.eval_words |
| 358 |
bin_model bin_xbounds bin_ybounds |
| 359 |
un_model un_xbounds eval_trans |
| 360 |
] in *. |
| 361 |
specialize (Hcorrect (map Interface.word.unsigned x) (map Interface.word.unsigned y) H1 H2). |
| 362 |
FtoZ. rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 363 |
2: { |
| 364 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 365 |
} |
| 366 |
destruct Hcorrect. auto. |
| 367 |
} |
| 368 |
{ (* output *bounds* are correct *) |
| 369 |
intros. apply Hcorrect; auto. } |
| 370 |
Qed. |
| 371 |
|
| 372 |
Lemma square_func_correct : |
| 373 |
valid_func (res square_op _) -> |
| 374 |
forall functions, Interface.map.get functions Field.square = Some square_func -> |
| 375 |
spec_of_UnOp un_square functions. |
| 376 |
Proof using M_eq check_args_ok ok square_func_eq felem_size_ok. |
| 377 |
cbv [spec_of_UnOp un_square]. rewrite square_func_eq. intros. |
| 378 |
pose proof square_correct |
| 379 |
_ _ _ ltac:(eassumption) _ (res_eq square_op) |
| 380 |
as Hcorrect. |
| 381 |
eapply list_unop_correct with (res:=res square_op); [ .. | eassumption ]; |
| 382 |
handle_side_conditions; [ | | apply valid_max_bounds | apply valid_length ]. |
| 383 |
{ (* output *value* is correct *) |
| 384 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 385 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 386 |
field_representation Signature.field_representation |
| 387 |
Representation.frep Representation.eval_bytes |
| 388 |
Representation.eval_words |
| 389 |
bin_model bin_xbounds bin_ybounds |
| 390 |
un_model un_xbounds eval_trans |
| 391 |
] in *. |
| 392 |
specialize (Hcorrect (map Interface.word.unsigned x) H1). |
| 393 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 394 |
2: { |
| 395 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 396 |
} |
| 397 |
destruct Hcorrect. auto. assert ( forall (x : Z), (x ^ 2 = x * x)%Z) by auto with zarith. |
| 398 |
rewrite F.pow_2_r. rewrite <- F.of_Z_mul. FtoZ. |
| 399 |
auto. |
| 400 |
} |
| 401 |
{ (* output *bounds* are correct *) |
| 402 |
intros. apply Hcorrect; auto. } |
| 403 |
Qed. |
| 404 |
|
| 405 |
Lemma add_func_correct : |
| 406 |
valid_func (res add_op _) -> |
| 407 |
forall functions, Interface.map.get functions Field.add = Some add_func -> |
| 408 |
spec_of_BinOp bin_add functions. |
| 409 |
Proof using M_eq check_args_ok add_func_eq ok felem_size_ok. |
| 410 |
cbv [spec_of_BinOp bin_add]. rewrite add_func_eq. intros. |
| 411 |
pose proof add_correct |
| 412 |
_ _ _ ltac:(eassumption) _ (res_eq add_op) |
| 413 |
as Hcorrect. |
| 414 |
eapply list_binop_correct with (res:=res add_op); [ .. | eassumption ]; |
| 415 |
handle_side_conditions; [ | | apply valid_max_bounds | apply valid_length ]. |
| 416 |
{ (* output *value* is correct *) |
| 417 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 418 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 419 |
field_representation Signature.field_representation |
| 420 |
Representation.frep Representation.eval_bytes |
| 421 |
Representation.eval_words |
| 422 |
bin_model bin_xbounds bin_ybounds |
| 423 |
un_model un_xbounds eval_trans |
| 424 |
] in *. |
| 425 |
specialize (Hcorrect (map Interface.word.unsigned x) (map Interface.word.unsigned y) H1 H2). |
| 426 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 427 |
2: { |
| 428 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 429 |
} |
| 430 |
destruct Hcorrect. FtoZ. |
| 431 |
auto. |
| 432 |
} |
| 433 |
{ (* output *bounds* are correct *) |
| 434 |
intros. apply Hcorrect; auto. } |
| 435 |
Qed. |
| 436 |
|
| 437 |
Lemma sub_func_correct : |
| 438 |
valid_func (res sub_op _) -> |
| 439 |
forall functions, Interface.map.get functions Field.sub = Some sub_func -> |
| 440 |
spec_of_BinOp bin_sub functions. |
| 441 |
Proof using M_eq check_args_ok sub_func_eq ok felem_size_ok. |
| 442 |
cbv [spec_of_BinOp bin_sub]. rewrite sub_func_eq. intros. |
| 443 |
pose proof sub_correct |
| 444 |
_ _ _ ltac:(eassumption) _ (res_eq sub_op) |
| 445 |
as Hcorrect. |
| 446 |
eapply list_binop_correct with (res:=res sub_op); [ .. | eassumption ]; |
| 447 |
handle_side_conditions; [ | | apply valid_max_bounds| apply valid_length ]. |
| 448 |
{ (* output *value* is correct *) |
| 449 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 450 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 451 |
field_representation Signature.field_representation |
| 452 |
Representation.frep Representation.eval_bytes |
| 453 |
Representation.eval_words |
| 454 |
bin_model bin_xbounds bin_ybounds |
| 455 |
un_model un_xbounds eval_trans |
| 456 |
] in *. |
| 457 |
specialize (Hcorrect (map Interface.word.unsigned x) (map Interface.word.unsigned y) H1 H2). |
| 458 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 459 |
2: { |
| 460 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 461 |
} |
| 462 |
destruct Hcorrect. rewrite <- F.of_Z_sub. FtoZ. |
| 463 |
auto. } |
| 464 |
{ (* output *bounds* are correct *) |
| 465 |
intros. apply Hcorrect; auto. } |
| 466 |
Qed. |
| 467 |
|
| 468 |
Lemma opp_func_correct : |
| 469 |
valid_func (res opp_op _) -> |
| 470 |
forall functions, Interface.map.get functions Field.opp = Some opp_func -> |
| 471 |
spec_of_UnOp un_opp functions. |
| 472 |
Proof using M_eq check_args_ok opp_func_eq ok felem_size_ok. |
| 473 |
cbv [spec_of_UnOp un_opp]. rewrite opp_func_eq. intros. |
| 474 |
pose proof opp_correct |
| 475 |
_ _ _ ltac:(eassumption) _ (res_eq opp_op) |
| 476 |
as Hcorrect. |
| 477 |
|
| 478 |
eapply list_unop_correct with (res:=res opp_op); [ .. | eassumption ]; |
| 479 |
handle_side_conditions; [ | | apply valid_max_bounds | apply valid_length ]. |
| 480 |
{ (* output *value* is correct *) |
| 481 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 482 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 483 |
field_representation Signature.field_representation |
| 484 |
Representation.frep Representation.eval_bytes |
| 485 |
Representation.eval_words |
| 486 |
bin_model bin_xbounds bin_ybounds |
| 487 |
un_model un_xbounds eval_trans |
| 488 |
] in *. |
| 489 |
specialize (Hcorrect (map Interface.word.unsigned x) H1). |
| 490 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 491 |
2: { |
| 492 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 493 |
} |
| 494 |
destruct Hcorrect. FtoZ. |
| 495 |
auto. |
| 496 |
} |
| 497 |
{ (* output *bounds* are correct *) |
| 498 |
intros. apply Hcorrect; auto. } |
| 499 |
Qed. |
| 500 |
|
| 501 |
Lemma list_Z_bounded_by_unsigned (xs : list (@Interface.word.rep _ word)) : |
| 502 |
list_Z_bounded_by |
| 503 |
(Primitives.saturated_bounds (List.length xs) width) |
| 504 |
(map Interface.word.unsigned xs). |
| 505 |
Proof using parameters_sentinel ok. |
| 506 |
induction xs; cbn; [reflexivity|]. |
| 507 |
eapply list_Z_bounded_by_cons; split; [|assumption]. |
| 508 |
eapply Bool.andb_true_iff; split; eapply Z.leb_le; |
| 509 |
cbv [Primitives.word_bound]; cbn. |
| 510 |
{ eapply Properties.word.unsigned_range. } |
| 511 |
{ eapply Le.Z.le_sub_1_iff, Properties.word.unsigned_range. } |
| 512 |
Qed. |
| 513 |
|
| 514 |
Lemma felem_copy_func_correct : |
| 515 |
valid_func (res felem_copy_op _) -> |
| 516 |
forall functions, |
| 517 |
Interface.map.get functions Field.felem_copy = Some felem_copy_func -> |
| 518 |
(@spec_of_felem_copy _ _ _ _ _ _ _ field_representation_raw) functions. |
| 519 |
Proof using M_eq check_args_ok felem_copy_func_eq ok felem_size_ok. |
| 520 |
cbv [spec_of_felem_copy]. rewrite felem_copy_func_eq. intros. |
| 521 |
pose proof copy_correct |
| 522 |
_ _ _ ltac:(eassumption) _ (res_eq felem_copy_op) |
| 523 |
as Hcorrect. |
| 524 |
|
| 525 |
eapply felem_copy_correct; [ .. | eassumption | eassumption ]; |
| 526 |
repeat handle_side_conditions; [ | ]; intros. |
| 527 |
{ (* output *value* is correct *) |
| 528 |
unshelve erewrite (proj1 (Hcorrect _ _)); cycle 1. |
| 529 |
{ rewrite map_map, List.map_ext_id; trivial; intros. |
| 530 |
rewrite ?Word.Interface.word.of_Z_unsigned; trivial. } |
| 531 |
{ rewrite <- H2. exact (list_Z_bounded_by_unsigned x0). } } |
| 532 |
{ (* output *bounds* are correct *) |
| 533 |
intros. apply Hcorrect; auto. } |
| 534 |
Qed. |
| 535 |
|
| 536 |
Lemma from_bytes_func_correct : |
| 537 |
valid_func (res from_bytes_op _) -> |
| 538 |
forall functions, |
| 539 |
Interface.map.get functions Field.from_bytes = Some from_bytes_func -> |
| 540 |
(@spec_of_from_bytes _ _ _ _ _ _ _ field_representation_raw) functions. |
| 541 |
Proof using M_eq check_args_ok from_bytes_func_eq ok felem_size_ok. |
| 542 |
cbv [spec_of_from_bytes]. rewrite from_bytes_func_eq. intros. |
| 543 |
pose proof from_bytes_correct |
| 544 |
_ _ _ ltac:(eassumption) _ (res_eq from_bytes_op) |
| 545 |
as Hcorrect. |
| 546 |
|
| 547 |
eapply Signature.from_bytes_correct with (res:=res from_bytes_op); |
| 548 |
[ .. | eassumption | eassumption ]; |
| 549 |
handle_side_conditions; [ apply valid_max_bounds | apply valid_length | | | ]. |
| 550 |
{ (* output length *) |
| 551 |
cbv [list_in_bounds WordByWordMontgomery.valid WordByWordMontgomery.small ] in *. |
| 552 |
intuition idtac. rewrite H5. rewrite Partition.length_partition; trivial. } |
| 553 |
|
| 554 |
{ (* output *value* is correct *) |
| 555 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 556 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 557 |
field_representation Signature.field_representation |
| 558 |
Representation.frep Representation.eval_bytes |
| 559 |
Representation.eval_words |
| 560 |
bin_model bin_xbounds bin_ybounds |
| 561 |
un_model un_xbounds eval_trans |
| 562 |
] in *. |
| 563 |
specialize (Hcorrect (map Byte.byte.unsigned bs0) H2). |
| 564 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 565 |
2: { |
| 566 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 567 |
} |
| 568 |
destruct Hcorrect. |
| 569 |
FtoZ; auto. |
| 570 |
} |
| 571 |
{ (* output *bounds* are correct *) |
| 572 |
intros. apply Hcorrect; auto. } |
| 573 |
Qed. |
| 574 |
|
| 575 |
Lemma to_bytes_func_correct : |
| 576 |
valid_func (res to_bytes_op _) -> |
| 577 |
forall functions, |
| 578 |
Interface.map.get functions Field.to_bytes = Some to_bytes_func -> |
| 579 |
(@spec_of_to_bytes _ _ _ _ _ _ _ field_representation_raw) functions. |
| 580 |
Proof using M_eq check_args_ok ok to_bytes_func_eq. |
| 581 |
cbv [spec_of_to_bytes]. rewrite to_bytes_func_eq. intros. |
| 582 |
pose proof to_bytes_correct |
| 583 |
_ _ _ ltac:(eassumption) _ (res_eq to_bytes_op) |
| 584 |
as Hcorrect. |
| 585 |
|
| 586 |
eapply Signature.to_bytes_correct with (res:=res to_bytes_op); |
| 587 |
[ .. | eassumption | eassumption ]; |
| 588 |
handle_side_conditions; cbv [list_in_bounds]; [ | | | ]. |
| 589 |
{ |
| 590 |
intros. apply byte_bounds_range_iff; split; eauto. |
| 591 |
- destruct H2. eapply WordByWordMontgomery.length_small; eauto. |
| 592 |
- destruct H2. cbv [WordByWordMontgomery.small] in *. |
| 593 |
rewrite H2. apply partition_bytes_range. |
| 594 |
} |
| 595 |
{ |
| 596 |
intros. destruct H2. apply WordByWordMontgomery.length_small in H2. rewrite H2. eauto. |
| 597 |
} |
| 598 |
{ (* output *value* is correct *) |
| 599 |
intros. cbv [feval]. simpl. |
| 600 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 601 |
field_representation Signature.field_representation |
| 602 |
Representation.frep Representation.eval_bytes |
| 603 |
Representation.eval_words |
| 604 |
bin_model bin_xbounds bin_ybounds |
| 605 |
un_model un_xbounds eval_trans |
| 606 |
] in *. |
| 607 |
|
| 608 |
specialize (Hcorrect (map Interface.word.unsigned x0) H2). |
| 609 |
rewrite Hcorrect. cbv [M] in M_eq. rewrite M_eq. auto. |
| 610 |
} |
| 611 |
{ (* output *bounds* are correct *) |
| 612 |
intros. rewrite Hcorrect by auto. |
| 613 |
cbn [bytes_in_bounds Representation.frep |
| 614 |
Signature.field_representation]. |
| 615 |
erewrite ByteBounds.byte_map_unsigned_of_Z, |
| 616 |
ByteBounds.map_byte_wrap_bounded |
| 617 |
by apply ByteBounds.partition_bounded_by. |
| 618 |
cbv [bounded_by] in *; simpl in H0. |
| 619 |
(*TODO: use valid_partition_small*) |
| 620 |
{ |
| 621 |
split. |
| 622 |
- unfold WordByWordMontgomery.small. unfold WordByWordMontgomery.eval. rewrite Partition.eval_partition. |
| 623 |
2: { |
| 624 |
apply uwprops. lia. |
| 625 |
} |
| 626 |
rewrite Zmod_small. |
| 627 |
2: { destruct H2. unfold WordByWordMontgomery.eval in H3. auto. } |
| 628 |
cbv [uweight]. |
| 629 |
rewrite Zmod_small; auto. split. |
| 630 |
+ destruct H2. cbv [WordByWordMontgomery.eval] in H3. cbv [uweight] in H3. lia. |
| 631 |
+ destruct H2. cbv [WordByWordMontgomery.eval] in H3. cbv [uweight] in H3. destruct H3. |
| 632 |
assert ( m < ModOps.weight 8 1 (n_bytes m))%Z. |
| 633 |
{ |
| 634 |
pose proof (use_curve_good _ _ _ check_args_ok) as U. |
| 635 |
assert (m < s m)%Z by lia. |
| 636 |
cbv [uweight] in U. lia. |
| 637 |
} |
| 638 |
lia. |
| 639 |
- cbv [WordByWordMontgomery.eval]. rewrite Partition.eval_partition. |
| 640 |
2: { |
| 641 |
apply uwprops; lia. |
| 642 |
} |
| 643 |
split. |
| 644 |
+ apply Z_mod_lt. destruct (uwprops 8); try lia. |
| 645 |
cbv [uweight] in *. specialize (weight_positive (n_bytes m)). lia. |
| 646 |
+ rewrite Zmod_small; [| split]. |
| 647 |
* apply Z_mod_lt. pose proof (use_curve_good _ _ _ check_args_ok); lia. |
| 648 |
* apply Z_mod_lt. pose proof (use_curve_good _ _ _ check_args_ok); lia. |
| 649 |
* pose proof (use_curve_good _ _ _ check_args_ok). |
| 650 |
assert (m < s m)%Z by lia. |
| 651 |
cbv [uweight] in *. |
| 652 |
eapply Z.lt_trans. |
| 653 |
{ |
| 654 |
apply Z_mod_lt. pose proof (use_curve_good _ _ _ check_args_ok). lia. |
| 655 |
} |
| 656 |
lia. |
| 657 |
} |
| 658 |
} |
| 659 |
Qed. |
| 660 |
|
| 661 |
Lemma m_nz : m <> 0%Z. |
| 662 |
Proof using check_args_ok. |
| 663 |
epose proof (use_curve_good _ _ _ check_args_ok). lia. |
| 664 |
Qed. |
| 665 |
|
| 666 |
(*Correctness proof of from- and to_montgomery are somewhat messy. *) |
| 667 |
Lemma from_mont_func_correct : |
| 668 |
valid_func (res from_mont_op _) -> |
| 669 |
forall functions, |
| 670 |
Interface.map.get functions from_mont = Some from_mont_func -> |
| 671 |
(@spec_of_UnOp _ _ _ _ _ _ _ _ from_mont) un_from_mont functions. |
| 672 |
Proof using M_eq check_args_ok ok from_mont_func_eq felem_size_ok. |
| 673 |
clear field_parameters_ok. |
| 674 |
cbv [spec_of_UnOp un_from_mont]. rewrite from_mont_func_eq. intros. |
| 675 |
pose proof from_montgomery_correct |
| 676 |
_ _ _ ltac:(eassumption) _ (res_eq from_mont_op) |
| 677 |
as Hcorrect. |
| 678 |
|
| 679 |
eapply list_unop_correct with (res:=res from_mont_op); [ .. | eassumption ]; |
| 680 |
handle_side_conditions; [ | | apply valid_max_bounds | apply valid_length ]. |
| 681 |
{ (* output *value* is correct *) |
| 682 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 683 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 684 |
field_representation Signature.field_representation |
| 685 |
Representation.frep Representation.eval_bytes |
| 686 |
Representation.eval_words |
| 687 |
bin_model bin_xbounds bin_ybounds |
| 688 |
un_model un_xbounds eval_trans |
| 689 |
] in *. |
| 690 |
specialize (Hcorrect (map Interface.word.unsigned x) H1). |
| 691 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 692 |
2: { |
| 693 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 694 |
} |
| 695 |
destruct Hcorrect. FtoZ. pose proof (WordByWordMontgomery.from_montgomerymod_correct width n m (@Field.r' width field_parameters) (m' m width)) as Hcorrect. |
| 696 |
cbv [WordByWordMontgomery.eval] in *. |
| 697 |
edestruct Hcorrect as [Hvalue Hvalid]; [| | | | | | eapply H3| ]; try eapply use_curve_good; try eassumption; [pose proof r'_correct as Htemp; cbv [r' M] in Htemp; rewrite M_eq in Htemp; eauto |]. |
| 698 |
rewrite Hvalue. rewrite Z.mul_mod; try apply m_nz. rewrite H2. rewrite <- Z.mul_mod; try apply m_nz. |
| 699 |
symmetry. rewrite Z.mul_mod; try apply m_nz. cbv [list_in_bounds] in *. clear H3. |
| 700 |
edestruct Hcorrect as [Hvalue' _]; [| | | | | | eapply H1 |]; try eapply use_curve_good; try eassumption; [pose proof r'_correct as Htemp; cbv [r' M] in Htemp; rewrite M_eq in Htemp; eauto |]. |
| 701 |
rewrite Hvalue'. rewrite <- Z.mul_mod; try apply m_nz. |
| 702 |
cbv [Field.r' PushButtonSynthesis.WordByWordMontgomery.r' Field.r r M felem_size_in_words]. rewrite M_eq. |
| 703 |
auto. |
| 704 |
} |
| 705 |
{ (* output *bounds* are correct *) |
| 706 |
intros. apply Hcorrect; auto. } |
| 707 |
Qed. |
| 708 |
|
| 709 |
Lemma to_mont_func_correct : |
| 710 |
valid_func (res to_mont_op _) -> |
| 711 |
forall functions, |
| 712 |
Interface.map.get functions to_mont = Some to_mont_func -> |
| 713 |
(@spec_of_UnOp _ _ _ _ _ _ _ _ to_mont) un_to_mont functions. |
| 714 |
Proof using M_eq check_args_ok ok to_mont_func_eq felem_size_ok. |
| 715 |
cbv [spec_of_UnOp un_to_mont]. rewrite to_mont_func_eq. intros ? ? GetF. |
| 716 |
pose proof to_montgomery_correct |
| 717 |
_ _ _ ltac:(eassumption) _ (res_eq to_mont_op) |
| 718 |
as Hcorrect. |
| 719 |
|
| 720 |
eapply list_unop_correct with (res:=res to_mont_op); [ .. | eassumption ]; |
| 721 |
handle_side_conditions; [ | | apply valid_max_bounds | apply valid_length]. |
| 722 |
{ (* output *value* is correct *) |
| 723 |
intros. cbv [feval]. simpl. cbv [Representation.eval_words]. simpl. |
| 724 |
cbv [feval feval_bytes bounded_by bytes_in_bounds Field.loose_bounds |
| 725 |
field_representation Signature.field_representation |
| 726 |
Representation.frep Representation.eval_bytes |
| 727 |
Representation.eval_words |
| 728 |
bin_model bin_xbounds bin_ybounds |
| 729 |
un_model un_xbounds eval_trans |
| 730 |
] in *. |
| 731 |
specialize (Hcorrect (map Interface.word.unsigned x) H0). |
| 732 |
rewrite map_unsigned_of_Z. erewrite (MaxBounds.map_word_wrap_bounded). |
| 733 |
2: { |
| 734 |
eapply valid_max_bounds; eauto. destruct Hcorrect; eauto. |
| 735 |
} |
| 736 |
destruct Hcorrect. FtoZ. |
| 737 |
cbv [WordByWordMontgomery.eval] in *. |
| 738 |
rewrite H1. |
| 739 |
pose proof (WordByWordMontgomery.from_montgomerymod_correct width n m (@Field.r' width field_parameters) (m' m width)) as Hcorrect. |
| 740 |
cbv [WordByWordMontgomery.eval] in *. |
| 741 |
edestruct Hcorrect as [Hvalue _]; [ | | | | | | apply H0 |]; try eapply use_curve_good; try eassumption; |
| 742 |
[pose proof r'_correct as Htemp; cbv [r' M] in Htemp; rewrite M_eq in Htemp; auto| ]. |
| 743 |
rewrite Z.mul_mod; try apply m_nz. rewrite Hvalue. rewrite <- Z.mul_mod; try apply m_nz. |
| 744 |
rewrite <- Z.mul_assoc. rewrite Z.mul_mod; try apply m_nz. |
| 745 |
lazymatch goal with |
| 746 |
| |- _ = (_ * (?prd)%Z mod _)%Z => eassert (Hr' : prd = _) |
| 747 |
| _ => idtac |
| 748 |
end. |
| 749 |
{ |
| 750 |
pose proof (r'_correct) as Htemp. rewrite <- Z.pow_mul_l. rewrite PullPush.Z.mod_pow_full. |
| 751 |
rewrite Z.mul_comm. |
| 752 |
cbv [Field.r]. cbv [r' M] in Htemp. rewrite M_eq in Htemp. |
| 753 |
rewrite Htemp. rewrite Z.pow_1_l; auto with zarith. |
| 754 |
} |
| 755 |
rewrite Hr'. assert (H1' : (1 mod m = 1)%Z). |
| 756 |
{ |
| 757 |
apply Zmod_small; split; [lia| ]. pose proof m_big. lia. |
| 758 |
} |
| 759 |
rewrite H1'. rewrite Z.mul_1_r. rewrite Zmod_mod. auto. |
| 760 |
} |
| 761 |
{ (* output *bounds* are correct *) |
| 762 |
intros. apply Hcorrect; auto. } |
| 763 |
Qed. |
| 764 |
|
| 765 |
Lemma select_znz_func_correct : |
| 766 |
valid_func (res select_znz_op _) -> |
| 767 |
forall functions, Interface.map.get functions select_znz = Some select_znz_func -> |
| 768 |
spec_of_selectznz functions. |
| 769 |
Proof using M_eq check_args_ok select_znz_func_eq ok felem_size_ok. |
| 770 |
cbv [spec_of_selectznz]. rewrite select_znz_func_eq. intros ? ? GetF * HH. |
| 771 |
rename x into x', y into y'. |
| 772 |
pose proof selectznz_correct |
| 773 |
_ _ _ ltac:(eassumption) _ (res_eq select_znz_op) |
| 774 |
as Hcorrect. |
| 775 |
eapply Signature.select_znz_correct with (res:=res select_znz_op); |
| 776 |
[ .. | eassumption | eassumption ]; |
| 777 |
handle_side_conditions. intros x y c H0 H1 H2. |
| 778 |
unfold COperationSpecifications.WordByWordMontgomery.selectznz_correct in Hcorrect. |
| 779 |
edestruct (@bit_range_eq 1 (Interface.word.unsigned c) H2) as [Hbit | Hbit]. |
| 780 |
- specialize (Hcorrect (Interface.word.unsigned c) (map Interface.word.unsigned x) (map Interface.word.unsigned y) H2 ltac:(eauto) ltac:(eauto)). |
| 781 |
destruct Hcorrect as [H4 H5]. rewrite Hbit in H4. simpl in H4. rewrite Hbit. simpl. auto. |
| 782 |
- specialize (Hcorrect (Interface.word.unsigned c) (map Interface.word.unsigned x) (map Interface.word.unsigned y) H2 ltac:(eauto) ltac:(eauto)). |
| 783 |
destruct Hcorrect as [H4 H5]. rewrite Hbit in H4. simpl in H4. rewrite Hbit. simpl. auto. |
| 784 |
Qed. |
| 785 |
|
| 786 |
End WordByWordMontgomery. |
| 787 |
|
| 788 |
|
| 789 |
(* Prototyping full pipeline: *) |
| 790 |
|
| 791 |
From Coq Require Import String. |
| 792 |
Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults64. |
| 793 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.ValidComputable.Func. |
| 794 |
|
| 795 |
(* TODO: move somewhere common *) |
| 796 |
Definition field_parameters_prefixed |
| 797 |
M_pos a24 (prefix: string) : FieldParameters := |
| 798 |
Build_FieldParameters |
| 799 |
M_pos a24 |
| 800 |
(prefix ++ "mul") |
| 801 |
(prefix ++ "add") |
| 802 |
(prefix ++ "carry_add_dontuse") |
| 803 |
(prefix ++ "sub") |
| 804 |
(prefix ++ "carry_sub_dontuse") |
| 805 |
(prefix ++ "opp") |
| 806 |
(prefix ++ "square") |
| 807 |
(prefix ++ "scmula24") |
| 808 |
(prefix ++ "inv") |
| 809 |
(prefix ++ "from_bytes") |
| 810 |
(prefix ++ "to_bytes") |
| 811 |
(prefix ++ "select_znz") |
| 812 |
(prefix ++ "felem_copy") |
| 813 |
(prefix ++ "from_word") |
| 814 |
. |
| 815 |
|
| 816 |
|
| 817 |
Require Import bedrock2.ProgramLogic. |
| 818 |
|
| 819 |
(* Section Tests. |
| 820 |
Definition m := (2^224 - 2^96 + 1)%Z. |
| 821 |
Definition prefix : string := "p224_"%string. |
| 822 |
|
| 823 |
Existing Instances Defaults64.default_parameters default_parameters_ok. |
| 824 |
Definition n := Eval vm_compute in (WordByWordMontgomery.n m machine_wordsize). |
| 825 |
|
| 826 |
Instance field_parameters : FieldParameters. |
| 827 |
Proof using Type. |
| 828 |
let M := (eval vm_compute in (Z.to_pos (m))) in |
| 829 |
(* Curve25519 "A" parameter (see section 4.1 of RFC 7748) *) |
| 830 |
let a := constr:(F.of_Z M 486662) in |
| 831 |
let prefix := constr:("p224_"%string) in |
| 832 |
eapply |
| 833 |
(field_parameters_prefixed |
| 834 |
M ((a - F.of_Z _ 2) / F.of_Z _ 4)%F prefix). |
| 835 |
Defined. |
| 836 |
|
| 837 |
Instance p224_ops : word_by_word_Montgomery_ops n m. |
| 838 |
Proof using Type. Time constructor; make_computed_op. Defined. |
| 839 |
|
| 840 |
Local Ltac begin_derive_bedrock2_func := |
| 841 |
lazymatch goal with |
| 842 |
| |- context [spec_of_BinOp bin_mul] => eapply mul_func_correct |
| 843 |
| |- context [spec_of_UnOp un_square] => eapply square_func_correct |
| 844 |
| |- context [spec_of_BinOp bin_add] => eapply add_func_correct |
| 845 |
| |- context [spec_of_BinOp bin_sub] => eapply sub_func_correct |
| 846 |
| |- context [spec_of_UnOp un_opp] => eapply opp_func_correct |
| 847 |
(* | |- context [spec_of_UnOp un_scmula24] => eapply scmula24_func_correct *) |
| 848 |
| |- context [spec_of_from_bytes] => eapply from_bytes_func_correct |
| 849 |
| |- context [spec_of_to_bytes] => eapply to_bytes_func_correct |
| 850 |
end. |
| 851 |
|
| 852 |
Local Ltac derive_bedrock2_func op := |
| 853 |
begin_derive_bedrock2_func; |
| 854 |
(* this goal fills in the evar, so do it first for [abstract] to be happy *) |
| 855 |
try lazymatch goal with |
| 856 |
| |- _ = b2_func _ => vm_compute; reflexivity |
| 857 |
end; |
| 858 |
(* solve all the remaining goals *) |
| 859 |
lazymatch goal with |
| 860 |
| |- _ = @ErrorT.Success ?ErrT unit tt => |
| 861 |
abstract (vm_cast_no_check (@eq_refl _ (@ErrorT.Success ErrT unit tt))) |
| 862 |
| |- list_Z_tighter_than _ _ => |
| 863 |
abstract (vm_cast_no_check (@eq_refl bool true)) |
| 864 |
| |- valid_func _ => |
| 865 |
eapply valid_func_bool_iff; |
| 866 |
abstract vm_cast_no_check (eq_refl true) |
| 867 |
| |- (_ = _)%Z => vm_compute; reflexivity |
| 868 |
| |- _ = default_varname_gen => vm_compute; reflexivity |
| 869 |
end. |
| 870 |
|
| 871 |
Derive p224_to_bytes |
| 872 |
SuchThat (forall functions, |
| 873 |
spec_of_to_bytes |
| 874 |
(field_representation:=field_representation_raw m) |
| 875 |
(p224_to_bytes :: functions)) |
| 876 |
As p224_to_bytes_correct. |
| 877 |
Proof. derive_bedrock2_func to_bytes_op. Qed. |
| 878 |
|
| 879 |
Derive p224_mul |
| 880 |
SuchThat (forall functions, |
| 881 |
spec_of_BinOp bin_mul |
| 882 |
(field_representation := field_representation m) |
| 883 |
(p224_mul :: functions)) |
| 884 |
As p22_mul_correct. |
| 885 |
Proof. derive_bedrock2_func mul_op. Qed. |
| 886 |
|
| 887 |
|
| 888 |
End Tests. *) |