| 1 |
From Coq Require Import Bool. |
| 2 |
From Coq Require Import Derive. |
| 3 |
From Coq Require Import ZArith Lia. |
| 4 |
From Coq Require Import List. Import ListNotations. |
| 5 |
Require Import Crypto.COperationSpecifications. Import COperationSpecifications.BarrettReduction. |
| 6 |
Require Import Rewriter.Language.Language. Import Language.Compilers. |
| 7 |
Require Import Crypto.Language.API. Import Language.API.Compilers. |
| 8 |
Require Import Rewriter.Language.Wf. Import Language.Wf.Compilers. |
| 9 |
Require Import Crypto.PushButtonSynthesis.BarrettReduction. |
| 10 |
Require Import Crypto.Fancy.Compiler. |
| 11 |
Require Import Crypto.Fancy.Prod. |
| 12 |
Require Import Crypto.Fancy.Spec. |
| 13 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 14 |
Require Import Crypto.Util.ZUtil.EquivModulo. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 16 |
Require Import Crypto.Util.Notations. |
| 17 |
Local Open Scope Z_scope. |
| 18 |
|
| 19 |
Module Barrett256. |
| 20 |
|
| 21 |
Definition M := Eval lazy in (2^256-2^224+2^192+2^96-1). |
| 22 |
Definition machine_wordsize := 256. |
| 23 |
|
| 24 |
Local Instance : Primitives.Options.PipelineOptions |
| 25 |
:= let _ := Primitives.Options.default_PipelineOptions in |
| 26 |
{| Primitives.Options.widen_carry := false |
| 27 |
; Primitives.Options.widen_bytes := true |
| 28 |
; Primitives.Options.unfold_value_barrier := true |}. |
| 29 |
|
| 30 |
Derive barrett_red256 |
| 31 |
SuchThat (barrett_red M machine_wordsize = ErrorT.Success barrett_red256) |
| 32 |
As barrett_red256_eq. |
| 33 |
Proof. lazy; reflexivity. Qed. |
| 34 |
|
| 35 |
Lemma Wf_barrett_red256 : API.Wf barrett_red256. |
| 36 |
Proof using Type. eapply Wf_barrett_red, barrett_red256_eq. Qed. |
| 37 |
|
| 38 |
Lemma barrett_red256_correct : |
| 39 |
COperationSpecifications.BarrettReduction.barrett_red_correct machine_wordsize M (API.Interp barrett_red256). |
| 40 |
Proof. |
| 41 |
apply barrett_red_correct with (machine_wordsize:=machine_wordsize) (requests:=[]). |
| 42 |
{ lazy. reflexivity. } |
| 43 |
{ apply barrett_red256_eq. } |
| 44 |
Qed. |
| 45 |
|
| 46 |
Definition muLow := Eval lazy in (2 ^ (2 * machine_wordsize) / M) mod (2^machine_wordsize). |
| 47 |
|
| 48 |
Definition barrett_red256_fancy' (xLow xHigh RegMuLow RegMod RegZero error : positive) := |
| 49 |
of_Expr 6%positive |
| 50 |
(make_consts [(RegMuLow, muLow); (RegMod, M); (RegZero, 0)]) |
| 51 |
barrett_red256 |
| 52 |
(xLow, (xHigh, tt)) |
| 53 |
error. |
| 54 |
Derive barrett_red256_fancy |
| 55 |
SuchThat (forall xLow xHigh RegMuLow RegMod RegZero, |
| 56 |
barrett_red256_fancy xLow xHigh RegMuLow RegMod RegZero = barrett_red256_fancy' xLow xHigh RegMuLow RegMod RegZero) |
| 57 |
As barrett_red256_fancy_eq. |
| 58 |
Proof. |
| 59 |
intros. |
| 60 |
lazy - [ADD ADDC SUB SUBC MUL128LL MUL128LU MUL128UL MUL128UU RSHI SELC SELM SELL ADDM]. |
| 61 |
reflexivity. |
| 62 |
Qed. |
| 63 |
|
| 64 |
Local Ltac wf_subgoal := |
| 65 |
repeat first [ progress cbn [List.In type.and_for_each_lhs_of_arrow fst snd List.app List.map] |
| 66 |
| apply expr.wf_smart_App_curried |
| 67 |
| progress intros |
| 68 |
| exfalso; assumption |
| 69 |
| apply conj |
| 70 |
| exact I |
| 71 |
| solve [ eauto 50 using or_introl, or_intror, eq_refl with nocore ] ]. |
| 72 |
Local Ltac valid_expr_subgoal := |
| 73 |
repeat match goal with |
| 74 |
| _ => progress intros |
| 75 |
| |- context [valid_ident] => econstructor |
| 76 |
| |- context[valid_scalar] => econstructor |
| 77 |
| |- context [valid_carry] => econstructor |
| 78 |
| _ => reflexivity |
| 79 |
| |- _ <> None => cbn; congruence |
| 80 |
| |- of_prefancy_scalar _ _ _ _ = _ => cbn; solve [eauto] |
| 81 |
end. |
| 82 |
|
| 83 |
Lemma barrett_red256_fancy_correct : |
| 84 |
forall xLow xHigh error, |
| 85 |
0 <= xLow < 2 ^ machine_wordsize -> |
| 86 |
0 <= xHigh < M -> |
| 87 |
let RegZero := 1%positive in |
| 88 |
let RegMod := 2%positive in |
| 89 |
let RegMuLow := 3%positive in |
| 90 |
let RegxHigh := 4%positive in |
| 91 |
let RegxLow := 5%positive in |
| 92 |
let consts_list := [(RegMuLow, muLow); (RegMod, M); (RegZero, 0)] in |
| 93 |
let arg_list := [(RegxHigh, xHigh); (RegxLow, xLow)] in |
| 94 |
let ctx := make_ctx (consts_list ++ arg_list) in |
| 95 |
let carry_flag := false in |
| 96 |
let last_wrote := (fun x : CC.code => |
| 97 |
match x with |
| 98 |
| CC.C => RegZero |
| 99 |
| _ => RegxHigh (* xHigh needs to have written M; others unused *) |
| 100 |
end) in |
| 101 |
let cc := make_cc last_wrote ctx carry_flag in |
| 102 |
interp Pos.eqb wordmax cc_spec (barrett_red256_fancy RegxLow RegxHigh RegMuLow RegMod RegZero error) cc ctx = (xLow + 2 ^ machine_wordsize * xHigh) mod M. |
| 103 |
Proof. |
| 104 |
intros. |
| 105 |
rewrite barrett_red256_fancy_eq. |
| 106 |
cbv [barrett_red256_fancy']. |
| 107 |
rewrite <-barrett_red256_correct by auto. |
| 108 |
eapply of_Expr_correct with (x2 := (xLow, (xHigh, tt))). |
| 109 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. |
| 110 |
intuition; Prod.inversion_prod; subst; cbv. break_innermost_match; congruence. } |
| 111 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. |
| 112 |
intuition; Prod.inversion_prod; subst; cbv; congruence. } |
| 113 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. tauto. } |
| 114 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. |
| 115 |
intuition; Prod.inversion_prod; subst; cbv; congruence. } |
| 116 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. |
| 117 |
match goal with |- context [_ mod ?m] => change m with (2 ^ machine_wordsize) end. |
| 118 |
assert (M < 2 ^ machine_wordsize) by (cbv; congruence). |
| 119 |
assert (0 <= muLow < 2 ^ machine_wordsize) by (split; cbv; congruence). |
| 120 |
intuition; Prod.inversion_prod; subst; apply Z.mod_small; lia. } |
| 121 |
{ cbn; intros; subst RegZero RegMod RegMuLow RegxHigh RegxLow. |
| 122 |
match goal with |- context [_ mod ?m] => change m with (2 ^ machine_wordsize) end. |
| 123 |
assert (M < 2 ^ machine_wordsize) by (cbv; congruence). |
| 124 |
assert (0 <= muLow < 2 ^ machine_wordsize) by (split; cbv; congruence). |
| 125 |
intuition; Prod.inversion_prod; subst; apply Z.mod_small; lia. } |
| 126 |
{ repeat first [ eapply expr.wf_Proper_list, Wf_barrett_red256 |
| 127 |
| progress cbv [make_pairs consts_list arg_list] |
| 128 |
| wf_subgoal ]. } |
| 129 |
{ cbn. cbv [muLow M]. |
| 130 |
repeat (match goal with |
| 131 |
| _ => eapply valid_LetInZZ |
| 132 |
| _ => eapply valid_LetInZ |
| 133 |
end; [ solve [valid_expr_subgoal] | intros ]). |
| 134 |
econstructor. valid_expr_subgoal. } |
| 135 |
{ reflexivity. } |
| 136 |
Qed. |
| 137 |
|
| 138 |
Import Spec.Registers. |
| 139 |
|
| 140 |
Definition barrett_red256_alloc' xLow xHigh RegMuLow := |
| 141 |
fun errorP errorR => |
| 142 |
allocate register |
| 143 |
positive Pos.eqb |
| 144 |
errorR |
| 145 |
(barrett_red256_fancy 1000%positive 1001%positive 1002%positive 1003%positive 1004%positive errorP) |
| 146 |
[r2;r3;r4;r5;r6;r7;r8;r9;r10;r5;r11;r6;r12;r13;r14;r15;r16;r17;r18;r19;r20;r21;r22;r23;r24;r25;r26;r27;r28;r29] |
| 147 |
(fun n => if n =? 1000 then xLow |
| 148 |
else if n =? 1001 then xHigh |
| 149 |
else if n =? 1002 then RegMuLow |
| 150 |
else if n =? 1003 then RegMod |
| 151 |
else if n =? 1004 then RegZero |
| 152 |
else errorR)%positive. |
| 153 |
Derive barrett_red256_alloc |
| 154 |
SuchThat (barrett_red256_alloc = barrett_red256_alloc') |
| 155 |
As barrett_red256_alloc_eq. |
| 156 |
Proof. |
| 157 |
intros. |
| 158 |
cbv [barrett_red256_alloc' barrett_red256_fancy]. |
| 159 |
cbn. subst barrett_red256_alloc. |
| 160 |
reflexivity. |
| 161 |
Qed. |
| 162 |
|
| 163 |
Local Notation interp := (interp reg_eqb wordmax cc_spec). |
| 164 |
Lemma barrett_red256_alloc_equivalent errorP errorR cc_start_state start_context : |
| 165 |
forall x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5 extra_reg, |
| 166 |
NoDup [x; xHigh; RegMuLow; scratchp1; scratchp2; scratchp3; scratchp4; scratchp5; extra_reg; RegMod; RegZero] -> |
| 167 |
0 <= start_context x < 2^machine_wordsize -> |
| 168 |
0 <= start_context xHigh < 2^machine_wordsize -> |
| 169 |
0 <= start_context RegMuLow < 2^machine_wordsize -> |
| 170 |
0 <= start_context RegZero < 2^machine_wordsize -> |
| 171 |
interp |
| 172 |
(barrett_red256_alloc r0 r1 r30 errorP errorR) cc_start_state |
| 173 |
(fun r => if reg_eqb r r0 |
| 174 |
then start_context x |
| 175 |
else if reg_eqb r r1 |
| 176 |
then start_context xHigh |
| 177 |
else if reg_eqb r r30 |
| 178 |
then start_context RegMuLow |
| 179 |
else start_context r) |
| 180 |
= interp (Prod.MulMod x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5) cc_start_state start_context. |
| 181 |
Proof. |
| 182 |
intros. |
| 183 |
let r := eval compute in (2^machine_wordsize) in |
| 184 |
replace (2^machine_wordsize) with r in * by reflexivity. |
| 185 |
cbv [Prod.MulMod barrett_red256_alloc]. |
| 186 |
|
| 187 |
(* Extract proofs that no registers are equal to each other *) |
| 188 |
repeat match goal with |
| 189 |
| H : NoDup _ |- _ => inversion H; subst; clear H |
| 190 |
| H : ~ In _ _ |- _ => cbv [In] in H |
| 191 |
| H : ~ (_ \/ _) |- _ => apply Decidable.not_or in H; destruct H |
| 192 |
| H : ~ False |- _ => clear H |
| 193 |
end. |
| 194 |
|
| 195 |
Time repeat match goal with |
| 196 |
| _ => progress prove_programs_equivalent |
| 197 |
| _ => rewrite interp_Mul256x256 with (tmp2 := extra_reg) by |
| 198 |
(match goal with |
| 199 |
| |- _ <> _ => assumption || congruence |
| 200 |
| _ => push_value_unused |
| 201 |
end) |
| 202 |
| _ => |
| 203 |
rewrite swap_add_chain by |
| 204 |
repeat match goal with |
| 205 |
| |- _ <> _ => assumption || congruence |
| 206 |
| _ => progress simplify_with_register_properties |
| 207 |
| |- _ = 2 ^ 256 => reflexivity |
| 208 |
| |- flags_unused _ _ => |
| 209 |
cbv [flags_unused]; intros; prove_programs_equivalent (* TODO: there is probably a faster way to do this one *) |
| 210 |
end |
| 211 |
end. |
| 212 |
|
| 213 |
cbv [Spec.interp]. |
| 214 |
simplify_with_register_properties. |
| 215 |
reflexivity. |
| 216 |
Qed. |
| 217 |
|
| 218 |
Lemma prod_barrett_red256_correct : |
| 219 |
forall (cc_start_state : CC.state) (* starting carry flags *) |
| 220 |
(start_context : register -> Z) (* starting register values *) |
| 221 |
(x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5 extra_reg : register), (* registers to use in computation *) |
| 222 |
NoDup [x; xHigh; RegMuLow; scratchp1; scratchp2; scratchp3; scratchp4; scratchp5; extra_reg; RegMod; RegZero] -> (* registers are unique *) |
| 223 |
0 <= start_context x < 2^machine_wordsize -> |
| 224 |
0 <= start_context xHigh < M -> |
| 225 |
start_context RegMuLow = muLow -> |
| 226 |
start_context RegMod = M -> |
| 227 |
start_context RegZero = 0 -> |
| 228 |
cc_start_state.(CC.cc_m) = cc_spec CC.M (start_context xHigh) -> |
| 229 |
let X := start_context x + 2^machine_wordsize * start_context xHigh in |
| 230 |
interp (Prod.MulMod x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5) cc_start_state start_context = X mod M. |
| 231 |
Proof. |
| 232 |
intros. subst X. |
| 233 |
assert (0 <= start_context xHigh < 2^machine_wordsize) by (cbv [M] in *; cbn; lia). |
| 234 |
let r := (eval compute in (2 ^ machine_wordsize)) in |
| 235 |
replace (2^machine_wordsize) with r in * by reflexivity. |
| 236 |
|
| 237 |
erewrite <-barrett_red256_fancy_correct with (error:=100000%positive) by eauto. |
| 238 |
rewrite <-barrett_red256_alloc_equivalent with (errorR := RegZero) (errorP := 1%positive) (extra_reg:=extra_reg) |
| 239 |
by (auto; cbv [M muLow] in *; cbn; auto with lia). |
| 240 |
|
| 241 |
match goal with |
| 242 |
|- context [make_cc ?last_wrote ?ctx ?carry] => |
| 243 |
let e := fresh in |
| 244 |
let He := fresh in |
| 245 |
remember (make_cc last_wrote ctx carry) as e eqn:He; |
| 246 |
cbv [make_ctx app make_cc] in He; |
| 247 |
cbn [Pos.eqb] in He; autorewrite with zsimplify in He; |
| 248 |
subst e |
| 249 |
end. |
| 250 |
|
| 251 |
repeat match goal with |
| 252 |
H : context [start_context] |- _ => |
| 253 |
rewrite <-H end. |
| 254 |
|
| 255 |
cbv [barrett_red256_alloc barrett_red256_fancy]. |
| 256 |
repeat step. |
| 257 |
reflexivity. |
| 258 |
Qed. |
| 259 |
End Barrett256. |
| 260 |
|
| 261 |
Section with_notations. |
| 262 |
Import Crypto.Language.IdentifiersBasicGENERATED.Compilers. |
| 263 |
Import Crypto.Util.ZRange. |
| 264 |
Local Open Scope zrange_scope. |
| 265 |
Local Open Scope Z_scope. |
| 266 |
Local Open Scope expr_scope. |
| 267 |
Local Notation uint256 := r[0 ~> 115792089237316195423570985008687907853269984665640564039457584007913129639935]%zrange. |
| 268 |
Local Set Printing Width 500. |
| 269 |
Redirect "Crypto.Fancy.Barrett256.barrett_red256" Print Barrett256.barrett_red256. |
| 270 |
End with_notations. |
| 271 |
|
| 272 |
Import Registers. |
| 273 |
|
| 274 |
(* Notations to make code more readable *) |
| 275 |
Local Notation "i rd x y ; cont" := (Instr i rd (x, y) cont) (at level 40, cont at level 200, format "i rd x y ; '//' cont"). |
| 276 |
Local Notation "i rd x y z ; cont" := (Instr i rd (x, y, z) cont) (at level 40, cont at level 200, format "i rd x y z ; '//' cont"). |
| 277 |
|
| 278 |
(* Barrett reference code: *) |
| 279 |
Local Set Printing Width 150. |
| 280 |
Redirect "Crypto.Fancy.Barrett256.Prod.MulMod" Eval cbv beta iota delta [Prod.MulMod Prod.Mul256x256] in Prod.MulMod. |
| 281 |
(* |
| 282 |
= fun x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5 : register => |
| 283 |
let q1Bottom256 := scratchp1 in |
| 284 |
let muSelect := scratchp2 in |
| 285 |
let q2 := scratchp3 in |
| 286 |
let q2High := scratchp4 in |
| 287 |
let q2High2 := scratchp5 in |
| 288 |
let q3 := scratchp1 in |
| 289 |
let r2 := scratchp2 in |
| 290 |
let r2High := scratchp3 in |
| 291 |
let maybeM := scratchp1 in |
| 292 |
SELM muSelect RegMuLow RegZero; |
| 293 |
RSHI 255 q1Bottom256 xHigh x; |
| 294 |
MUL128LL q2 q1Bottom256 RegMuLow; |
| 295 |
MUL128UU q2High q1Bottom256 RegMuLow; |
| 296 |
MUL128UL scratchp5 q1Bottom256 RegMuLow; |
| 297 |
ADD 128 q2 q2 scratchp5; |
| 298 |
ADDC (-128) q2High q2High scratchp5; |
| 299 |
MUL128LU scratchp5 q1Bottom256 RegMuLow; |
| 300 |
ADD 128 q2 q2 scratchp5; |
| 301 |
ADDC (-128) q2High q2High scratchp5; |
| 302 |
RSHI 255 q2High2 RegZero xHigh; |
| 303 |
ADD 0 q2High q2High q1Bottom256; |
| 304 |
ADDC 0 q2High2 q2High2 RegZero; |
| 305 |
ADD 0 q2High q2High muSelect; |
| 306 |
ADDC 0 q2High2 q2High2 RegZero; |
| 307 |
RSHI 1 q3 q2High2 q2High; |
| 308 |
MUL128LL r2 RegMod q3; |
| 309 |
MUL128UU r2High RegMod q3; |
| 310 |
MUL128UL scratchp4 RegMod q3; |
| 311 |
ADD 128 r2 r2 scratchp4; |
| 312 |
ADDC (-128) r2High r2High scratchp4; |
| 313 |
MUL128LU scratchp4 RegMod q3; |
| 314 |
ADD 128 r2 r2 scratchp4; |
| 315 |
ADDC (-128) r2High r2High scratchp4; |
| 316 |
SUB 0 muSelect x r2; |
| 317 |
SUBC 0 xHigh xHigh r2High; |
| 318 |
SELL maybeM RegMod RegZero; |
| 319 |
SUB 0 q3 muSelect maybeM; |
| 320 |
ADDM x q3 RegZero RegMod; |
| 321 |
Ret x |
| 322 |
*) |
| 323 |
|
| 324 |
(* Remove Redirect to see proof statement and remaining admitted statements. *) |
| 325 |
Redirect "Crypto.Fancy.Barrett256.prod_barrett_red256_correct" Check Barrett256.prod_barrett_red256_correct. |
| 326 |
Redirect "Crypto.Fancy.Barrett256.prod_barrett_red256_correct.Assumptions" Print Assumptions Barrett256.prod_barrett_red256_correct. |