| 1 |
From Coq Require Import ZArith Lia. |
| 2 |
From Coq Require Import Derive. |
| 3 |
Require Import QArith_base Qround Crypto.Util.QUtil. |
| 4 |
Require Import Crypto.ArithmeticCPS.Core. |
| 5 |
Require Import Crypto.Util.CPSUtil. |
| 6 |
Require Import Crypto.Util.CPSNotations. |
| 7 |
Require Import Crypto.Util.ListUtil. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 10 |
|
| 11 |
Require Import Crypto.Util.Notations. |
| 12 |
Local Open Scope Z_scope. |
| 13 |
Import CPSBindNotations. |
| 14 |
Local Open Scope cps_scope. |
| 15 |
|
| 16 |
Section mod_ops. |
| 17 |
Local Coercion Z.of_nat : nat >-> Z. |
| 18 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 19 |
(* Design constraints: |
| 20 |
- inputs must be [Z] (b/c reification does not support Q) |
| 21 |
- internal structure must not match on the arguments (b/c reification does not support [positive]) *) |
| 22 |
Context (limbwidth_num limbwidth_den : Z) |
| 23 |
(limbwidth_good : 0 < limbwidth_den <= limbwidth_num) |
| 24 |
(s : Z) |
| 25 |
(c : list (Z*Z)) |
| 26 |
(n : nat) |
| 27 |
(len_c : nat) |
| 28 |
(idxs : list nat) |
| 29 |
(len_idxs : nat) |
| 30 |
(Hn_nz : n <> 0%nat) |
| 31 |
(Hc : length c = len_c) |
| 32 |
(Hidxs : length idxs = len_idxs). |
| 33 |
Definition weight (i : nat) |
| 34 |
:= 2^(-(-(limbwidth_num * i) / limbwidth_den)). |
| 35 |
End mod_ops. |
| 36 |
(* |
| 37 |
Require Import Crypto.Arithmetic.ModOps. |
| 38 |
Print encodemod. |
| 39 |
*) |
| 40 |
|
| 41 |
Module ModOps (Import RT : Runtime). |
| 42 |
Module Import Deps. |
| 43 |
Module Export Positional := Positional RT. |
| 44 |
End Deps. |
| 45 |
|
| 46 |
Section mod_ops. |
| 47 |
Local Coercion Z.of_nat : nat >-> Z. |
| 48 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 49 |
(* Design constraints: |
| 50 |
- inputs must be [Z] (b/c reification does not support Q) |
| 51 |
- internal structure must not match on the arguments (b/c reification does not support [positive]) *) |
| 52 |
Context (limbwidth_num limbwidth_den : Z) |
| 53 |
(limbwidth_good : 0 < limbwidth_den <= limbwidth_num) |
| 54 |
(s : Z) |
| 55 |
(c : list (Z*Z)) |
| 56 |
(n : nat) |
| 57 |
(len_c : nat) |
| 58 |
(idxs : list nat) |
| 59 |
(len_idxs : nat) |
| 60 |
(Hn_nz : n <> 0%nat) |
| 61 |
(Hc : length c = len_c) |
| 62 |
(Hidxs : length idxs = len_idxs). |
| 63 |
|
| 64 |
Local Notation weight := (weight limbwidth_num limbwidth_den). |
| 65 |
|
| 66 |
Definition carry_mulmod_cps (f g : list Z) : ~> list Z |
| 67 |
:= (fg <- mulmod_cps weight s c n f g; v <- chained_carries_cps weight n s c fg idxs; return v). |
| 68 |
Definition carry_mulmod (f g : list Z) : list Z := carry_mulmod_cps f g _ id. |
| 69 |
|
| 70 |
Definition carry_squaremod_cps (f : list Z) : ~> list Z |
| 71 |
:= (ff <- squaremod_cps weight s c n f; v <- chained_carries_cps weight n s c ff idxs; return v). |
| 72 |
Definition carry_squaremod (f : list Z) : list Z := carry_squaremod_cps f _ id. |
| 73 |
|
| 74 |
Definition carry_scmulmod_cps (x : Z) (f : list Z) : ~> list Z |
| 75 |
:= (x <- encode_cps weight n s c x; |
| 76 |
xf <- mulmod_cps weight s c n x f; |
| 77 |
xf <- chained_carries_cps weight n s c xf idxs; |
| 78 |
return xf). |
| 79 |
Definition carry_scmulmod (x : Z) (f : list Z) : list Z := carry_scmulmod_cps x f _ id. |
| 80 |
|
| 81 |
Definition carrymod_cps (f : list Z) : ~> list Z |
| 82 |
:= chained_carries_cps weight n s c f idxs. |
| 83 |
Definition carrymod (f : list Z) : list Z := carrymod_cps f _ id. |
| 84 |
|
| 85 |
Definition addmod_cps (f g : list Z) : ~> list Z |
| 86 |
:= add_cps weight n f g. |
| 87 |
Definition addmod (f g : list Z) : list Z := addmod_cps f g _ id. |
| 88 |
|
| 89 |
Definition submod_cps (coef : Z) (f g : list Z) : ~> list Z |
| 90 |
:= sub_cps weight n s c coef f g. |
| 91 |
Definition submod (coef : Z) (f g : list Z) : list Z := submod_cps coef f g _ id. |
| 92 |
|
| 93 |
Definition oppmod_cps (coef : Z) (f : list Z) : ~> list Z |
| 94 |
:= opp_cps weight n s c coef f. |
| 95 |
Definition oppmod (coef : Z) (f : list Z) : list Z := oppmod_cps coef f _ id. |
| 96 |
|
| 97 |
Definition encodemod_cps (f : Z) : ~> list Z |
| 98 |
:= encode_cps weight n s c f. |
| 99 |
Definition encodemod (f : Z) : list Z := encodemod_cps f _ id. |
| 100 |
End mod_ops. |
| 101 |
End ModOps. |
| 102 |
|
| 103 |
(* |
| 104 |
Module Import RT_ExtraAx := RT_Extra RuntimeAxioms. |
| 105 |
Module ModOpsAx := ModOps RuntimeAxioms. |
| 106 |
|
| 107 |
Import List.ListNotations. |
| 108 |
Import RuntimeAxioms. |
| 109 |
|
| 110 |
Axiom hide : forall {T} {v : T}, True. |
| 111 |
Time Compute @hide _ (fun f g => ModOpsAx.addmod 51 2 5 (expand_list 0 f 5) (expand_list 0 g 5)). |
| 112 |
Time Compute @hide _ (fun f g => ModOpsAx.carry_mulmod 51 2 (2^255) [(1,19)] 5 (List.seq 0 5 ++ [0; 1])%nat%list (expand_list 0 f 5) (expand_list 0 g 5)). |
| 113 |
Time Eval cbv in @hide _ (fun f g => ModOpsAx.carry_mulmod 51 2 (2^255) [(1,19)] 5 (List.seq 0 5 ++ [0; 1])%nat%list (expand_list 0 f 5) (expand_list 0 g 5)). |
| 114 |
Time Eval lazy in @hide _ (fun f g => ModOpsAx.carry_mulmod 51 2 (2^255) [(1,19)] 5 (List.seq 0 5 ++ [0; 1])%nat%list (expand_list 0 f 5) (expand_list 0 g 5)). |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
Module Import RT_ExtraDef := RT_Extra RuntimeDefinitions. |
| 120 |
Module ModOpsDef := ModOps RuntimeDefinitions. |
| 121 |
Import RuntimeDefinitions. |
| 122 |
|
| 123 |
|
| 124 |
Time Eval cbv_no_rt in @hide _ (fun f g => ModOpsDef.carry_mulmod 51 2 (2^255) [(1,19)] 5 (List.seq 0 5 ++ [0; 1])%nat%list (expand_list 0 f 5) (expand_list 0 g 5)). |
| 125 |
Time Eval lazy_no_rt in @hide _ (fun f g => ModOpsDef.carry_mulmod 51 2 (2^255) [(1,19)] 5 (List.seq 0 5 ++ [0; 1])%nat%list (expand_list 0 f 5) (expand_list 0 g 5)). |
| 126 |
|
| 127 |
(* Goal forall f g : list Z, True. |
| 128 |
intros. |
| 129 |
pose (ModOpsAx.addmod 51 2 5 (expand_list 0 f 5) (expand_list 0 g 5)) as e. |
| 130 |
cbv [ModOpsAx.addmod expand_list expand_list_helper nat_rect List.seq List.app] in e. |
| 131 |
cbv [Z.pow Pos.pow Init.Nat.pred nat_rect fst Z.of_nat Pos.of_succ_nat Pos.succ Z.mul Pos.mul Pos.add Pos.iter Z.opp Z.div Z.div_eucl Z.pos_div_eucl Z.leb Z.compare Pos.compare Pos.compare_cont Z.add Z.ltb Z.sub Z.pos_sub Z.succ_double Z.double Z.pow Z.pow_pos Z.modulo Z.eqb Pos.eqb Pos.compare snd] in e. |
| 132 |
vm_compute in e. |
| 133 |
cbv [ModOpsAx.addmod_cps] |
| 134 |
cbv [ |
| 135 |
ModOpsAx.carry_mulmod ModOpsAx.carry_mulmod_cps cpsbind cpsreturn] in e. |
| 136 |
cbv [ModOpsAx.Deps.Positional.mulmod_cps ModOpsAx.Deps.Positional.from_associational_cps ModOpsAx.Deps.Positional.zeros List.repeat] in e. |
| 137 |
cbv [cpscall] in e. |
| 138 |
set (k := ModOpsAx.Deps.Positional.Deps.Associational.repeat_reduce _ _ _) in (value of e). |
| 139 |
set (k' := k _) in (value of e). |
| 140 |
subst k. |
| 141 |
vm_compute in k'. |
| 142 |
subst k'. |
| 143 |
cbv [fold_right_cps2] in e. |
| 144 |
cbv [fold_right_cps2_specialized] in e. |
| 145 |
cbv [fold_right_cps2_specialized_step] in e. |
| 146 |
cbv [weight Init.Nat.pred ModOpsAx.Deps.Positional.place nat_rect fst Z.of_nat Pos.of_succ_nat Pos.succ Z.mul Pos.mul Pos.add Pos.iter Z.opp Z.div Z.div_eucl Z.pos_div_eucl Z.leb Z.compare Pos.compare Pos.compare_cont Z.add Z.ltb Z.sub Z.pos_sub Z.succ_double Z.double Z.pow Z.pow_pos Z.modulo Z.eqb Pos.eqb Pos.compare snd] in e. |
| 147 |
cbv [ModOpsAx.Deps.Positional.add_to_nth update_nth cpscall] in e. |
| 148 |
set (w := fun i : nat => _) in (value of e). |
| 149 |
cbv [ModOpsAx.Deps.Positional.chained_carries_cps] in e. |
| 150 |
cbv [List.rev List.app id] in e. |
| 151 |
cbv [fold_right_cps2 fold_right_cps2_specialized fold_right_cps2_specialized_step] in e. |
| 152 |
cbv [ModOpsAx.Deps.Positional.carry_reduce_cps] in e. |
| 153 |
cbv [ModOpsAx.Deps.Positional.to_associational] in e. |
| 154 |
unfold ModOpsAx.Deps.Positional.carry_cps at 1 in (value of e). |
| 155 |
cbv [ModOpsAx.Deps.Positional.Deps.Associational.carry_cps] in e. |
| 156 |
cbv [ModOpsAx.Deps.Positional.to_associational List.map List.seq] in e. |
| 157 |
Arguments List.combine _ _ !_ !_. |
| 158 |
cbn [List.combine] in e. |
| 159 |
cbv [flat_map_cps flat_map_cps_specialized] in e. |
| 160 |
cbv [ModOpsAx.Deps.Positional.Deps.Associational.carryterm_cps ] in e. |
| 161 |
cbv [fst snd] in e. |
| 162 |
cbv [w] in e. |
| 163 |
clear w. |
| 164 |
set (w := fun i : nat => _) in (value of e). |
| 165 |
cbv [weight Init.Nat.pred ModOpsAx.Deps.Positional.place nat_rect fst Z.of_nat Pos.of_succ_nat Pos.succ Z.mul Pos.mul Pos.add Pos.iter Z.opp Z.div Z.div_eucl Z.pos_div_eucl Z.leb Z.compare Pos.compare Pos.compare_cont Z.add Z.ltb Z.sub Z.pos_sub Z.succ_double Z.double Z.pow Z.pow_pos Z.modulo Z.eqb Pos.eqb Pos.compare snd] in e. |
| 166 |
cbv [List.app] in e. |
| 167 |
cbv [cpsbind cpsreturn cpscall] in e. |
| 168 |
unfold ModOpsAx.Deps.Positional.from_associational_cps at 1 in (value of e). |
| 169 |
cbv [ModOpsAx.Deps.Positional.zeros List.repeat ModOpsAx.Deps.Positional.add_to_nth fold_right_cps2 fold_right_cps2_specialized fold_right_cps2_specialized_step Init.Nat.pred ModOpsAx.Deps.Positional.place fst snd nat_rect] in e. |
| 170 |
vm_compute in e. |
| 171 |
cbv [] in e. |
| 172 |
subst w; cbv beta iota zeta in e. |
| 173 |
cbv beta iota zeta in |
| 174 |
cbv [cpsbind cpsreturn cpscall ModOpsAx.Deps.Positional.carry_cps] in e. |
| 175 |
cbv [ModOpsAx.Deps.Positional.Deps.Associational.carry_cps ModOpsAx.Deps.Positional.to_associational] in e. |
| 176 |
cbv [List.map List.combine List.seq] in e. |
| 177 |
cbv [flat_map_cps] in e. |
| 178 |
cbv [ModOpsAx.Deps.Positional.from_associational_cps] in e. |
| 179 |
cbv [flat_map_cps_specialized] in e. |
| 180 |
cbv [ModOpsAx.Deps.Positional.Deps.Associational.carryterm_cps] in e. |
| 181 |
cbv [ModOpsAx.Deps.Positional.zeros] in e. |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
cbv [ModOpsAx.Deps.Positional.Deps.Associational.split] in e. |
| 192 |
|
| 193 |
*) |
| 194 |
*) |