| 1 |
(* Following http://adam.chlipala.net/theses/andreser.pdf chapter 3 *) |
| 2 |
From Coq Require Import ZArith Lia. |
| 3 |
Require Import Crypto.Algebra.Nsatz. |
| 4 |
Require Import Crypto.Util.Tactics.UniquePose Crypto.Util.Decidable. |
| 5 |
Require Import Crypto.Util.Tuple Crypto.Util.Prod Crypto.Util.LetIn. |
| 6 |
Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.NatUtil. |
| 7 |
Require Import QArith_base Qround Crypto.Util.QUtil. |
| 8 |
Require Import Crypto.Algebra.Ring Crypto.Util.Decidable.Bool2Prop. |
| 9 |
Import ListNotations. Local Open Scope Z_scope. |
| 10 |
|
| 11 |
Definition runtime_mul := Z.mul. |
| 12 |
Definition runtime_add := Z.add. |
| 13 |
Declare Scope runtime_scope. |
| 14 |
Delimit Scope runtime_scope with RT. |
| 15 |
Infix "*" := runtime_mul : runtime_scope. |
| 16 |
Infix "+" := runtime_add : runtime_scope. |
| 17 |
|
| 18 |
Module Associational. |
| 19 |
Definition eval (p:list (Z*Z)) : Z := |
| 20 |
fold_right Z.add 0%Z (map (fun t => fst t * snd t) p). |
| 21 |
|
| 22 |
Lemma eval_nil : eval nil = 0. |
| 23 |
Proof. trivial. Qed. |
| 24 |
Lemma eval_cons p q : eval (p::q) = fst p * snd p + eval q. |
| 25 |
Proof. trivial. Qed. |
| 26 |
Lemma eval_app p q: eval (p++q) = eval p + eval q. |
| 27 |
Proof. induction p; rewrite <-?List.app_comm_cons; |
| 28 |
rewrite ?eval_nil, ?eval_cons; nsatz. Qed. |
| 29 |
|
| 30 |
#[global] |
| 31 |
Hint Rewrite eval_nil eval_cons eval_app : push_eval. |
| 32 |
Local Ltac push := autorewrite with |
| 33 |
push_eval push_map push_partition push_flat_map |
| 34 |
push_fold_right push_nth_default cancel_pair. |
| 35 |
|
| 36 |
Lemma eval_map_mul (a x:Z) (p:list (Z*Z)) |
| 37 |
: eval (List.map (fun t => (a*fst t, x*snd t)) p) = a*x*eval p. |
| 38 |
Proof. induction p; push; nsatz. Qed. |
| 39 |
#[global] |
| 40 |
Hint Rewrite eval_map_mul : push_eval. |
| 41 |
|
| 42 |
Definition mul (p q:list (Z*Z)) : list (Z*Z) := |
| 43 |
flat_map (fun t => |
| 44 |
map (fun t' => |
| 45 |
(fst t * fst t', (snd t * snd t')%RT)) |
| 46 |
q) p. |
| 47 |
Lemma eval_mul p q : eval (mul p q) = eval p * eval q. |
| 48 |
Proof. induction p; cbv [mul]; push; nsatz. Qed. |
| 49 |
#[global] |
| 50 |
Hint Rewrite eval_mul : push_eval. |
| 51 |
|
| 52 |
Example base10_2digit_mul (a0:Z) (a1:Z) (b0:Z) (b1:Z) : |
| 53 |
{ab| eval ab = eval [(10,a1);(1,a0)] * eval [(10,b1);(1,b0)]}. |
| 54 |
eexists ?[ab]. |
| 55 |
(* Goal: eval ?ab = eval [(10,a1);(1,a0)] * eval [(10,b1);(1,b0)] *) |
| 56 |
rewrite <-eval_mul. |
| 57 |
(* Goal: eval ?ab = eval (mul [(10,a1);(1,a0)] [(10,b1);(1,b0)]) *) |
| 58 |
cbv -[runtime_mul eval]. |
| 59 |
(* Goal: eval ?ab = eval [(100,(a1*b1));(10,a1*b0);(10,a0*b1);(1,a0*b0)]%RT *) |
| 60 |
trivial. Defined. |
| 61 |
|
| 62 |
Definition split (s:Z) (p:list (Z*Z)) : list (Z*Z) * list (Z*Z) |
| 63 |
:= let hi_lo := partition (fun t => fst t mod s =? 0) p in |
| 64 |
(snd hi_lo, map (fun t => (fst t / s, snd t)) (fst hi_lo)). |
| 65 |
Lemma eval_split s p (s_nz:s<>0) : |
| 66 |
eval (fst (split s p)) + s * eval (snd (split s p)) = eval p. |
| 67 |
Proof. cbv [split]; induction p; |
| 68 |
repeat match goal with |
| 69 |
| |- context[?a/?b] => |
| 70 |
unique pose proof (Z_div_exact_full_2 a b ltac:(trivial) ltac:(trivial)) |
| 71 |
| _ => progress push |
| 72 |
| _ => progress break_match |
| 73 |
| _ => progress nsatz end. Qed. |
| 74 |
|
| 75 |
Lemma reduction_rule a b s c (modulus_nz:s-c<>0) : |
| 76 |
(a + s * b) mod (s - c) = (a + c * b) mod (s - c). |
| 77 |
Proof. replace (a + s * b) with ((a + c*b) + b*(s-c)) by nsatz. |
| 78 |
rewrite Z.add_mod,Z_mod_mult,Z.add_0_r,Z.mod_mod;trivial. Qed. |
| 79 |
|
| 80 |
Definition reduce (s:Z) (c:list _) (p:list _) : list (Z*Z) := |
| 81 |
let lo_hi := split s p in fst lo_hi ++ mul c (snd lo_hi). |
| 82 |
|
| 83 |
Lemma eval_reduce s c p (s_nz:s<>0) (modulus_nz:s-eval c<>0) : |
| 84 |
eval (reduce s c p) mod (s - eval c) = eval p mod (s - eval c). |
| 85 |
Proof. cbv [reduce]; push. |
| 86 |
rewrite <-reduction_rule, eval_split; trivial. Qed. |
| 87 |
#[global] |
| 88 |
Hint Rewrite eval_reduce : push_eval. |
| 89 |
End Associational. |
| 90 |
|
| 91 |
Module Positional. Section Positional. |
| 92 |
Context (weight : nat -> Z) |
| 93 |
(weight_0 : weight 0%nat = 1) |
| 94 |
(weight_nz : forall i, weight i <> 0). |
| 95 |
|
| 96 |
Definition to_associational {n:nat} (xs:tuple Z n) : list (Z*Z) |
| 97 |
:= combine (map weight (List.seq 0 n)) (Tuple.to_list n xs). |
| 98 |
Definition eval {n} x := Associational.eval (@to_associational n x). |
| 99 |
Lemma eval_to_associational {n} x : |
| 100 |
Associational.eval (@to_associational n x) = eval x. |
| 101 |
Proof using Type. trivial. Qed. |
| 102 |
|
| 103 |
(* SKIP over this: zeros, add_to_nth *) |
| 104 |
Local Ltac push := autorewrite with push_eval push_map distr_length |
| 105 |
push_flat_map push_fold_right push_nth_default cancel_pair natsimplify. |
| 106 |
Program Definition zeros n : tuple Z n |
| 107 |
:= Tuple.from_list n (List.map (fun _ => 0) (List.seq 0 n)) _. |
| 108 |
Next Obligation. push; reflexivity. Qed. |
| 109 |
Lemma eval_zeros n : eval (zeros n) = 0. |
| 110 |
Proof using weight_0. |
| 111 |
cbv [eval Associational.eval to_associational zeros]; |
| 112 |
rewrite Tuple.to_list_from_list. |
| 113 |
generalize dependent (List.seq 0 n); intro xs. |
| 114 |
induction xs; simpl; nsatz. Qed. |
| 115 |
Program Definition add_to_nth {n} i x : tuple Z n -> tuple Z n |
| 116 |
:= Tuple.on_tuple (ListUtil.update_nth i (runtime_add x)) _. |
| 117 |
Next Obligation. apply ListUtil.length_update_nth. Defined. |
| 118 |
Lemma eval_add_to_nth {n} (i:nat) (H:(i<n)%nat) (x:Z) (xs:tuple Z n) : |
| 119 |
eval (add_to_nth i x xs) = weight i * x + eval xs. |
| 120 |
Proof using Type. |
| 121 |
cbv [eval to_associational add_to_nth Tuple.on_tuple runtime_add]. |
| 122 |
rewrite !Tuple.to_list_from_list. |
| 123 |
rewrite ListUtil.combine_update_nth_r at 1. |
| 124 |
rewrite <-(update_nth_id i (List.combine _ _)) at 2. |
| 125 |
rewrite <-!(ListUtil.splice_nth_equiv_update_nth_update _ _ |
| 126 |
(weight 0, 0)) by (push; lia); cbv [ListUtil.splice_nth id]. |
| 127 |
repeat match goal with |
| 128 |
| _ => progress push |
| 129 |
| _ => progress break_match |
| 130 |
| _ => progress (apply Zminus_eq; ring_simplify) |
| 131 |
| _ => rewrite <-ListUtil.map_nth_default_always |
| 132 |
end; lia. Qed. |
| 133 |
Hint Rewrite @eval_add_to_nth eval_zeros : push_eval. |
| 134 |
|
| 135 |
Fixpoint place (t:Z*Z) (i:nat) : nat * Z := |
| 136 |
if dec (fst t mod weight i = 0) |
| 137 |
then (i, let c := fst t / weight i in (c * snd t)%RT) |
| 138 |
else match i with S i' => place t i' | O => (O, fst t * snd t)%RT end. |
| 139 |
Lemma place_in_range (t:Z*Z) (n:nat) : (fst (place t n) < S n)%nat. |
| 140 |
Proof using Type. induction n; cbv [place] in *; break_match; autorewrite with cancel_pair; try lia. Qed. |
| 141 |
Lemma weight_place t i : weight (fst (place t i)) * snd (place t i) = fst t * snd t. |
| 142 |
Proof using weight_0 weight_nz. induction i; cbv [place] in *; break_match; push; |
| 143 |
repeat match goal with |- context[?a/?b] => |
| 144 |
unique pose proof (Z_div_exact_full_2 a b ltac:(auto) ltac:(auto)) |
| 145 |
end; nsatz. Qed. |
| 146 |
Hint Rewrite weight_place : push_eval. |
| 147 |
|
| 148 |
Definition from_associational n (p:list (Z*Z)) := |
| 149 |
List.fold_right (fun t => |
| 150 |
let p := place t (pred n) in |
| 151 |
add_to_nth (fst p) (snd p) ) (zeros n) p. |
| 152 |
Lemma eval_from_associational {n} p (n_nz:n<>O) : |
| 153 |
eval (from_associational n p) = Associational.eval p. |
| 154 |
Proof using weight_0 weight_nz. induction p; cbv [from_associational] in *; push; try |
| 155 |
pose proof place_in_range a (pred n); destruct n; cbn [pred] in *; try lia; try nsatz. Qed. |
| 156 |
Hint Rewrite @eval_from_associational : push_eval. |
| 157 |
|
| 158 |
Section mulmod. |
| 159 |
Context (m:Z) (m_nz:m <> 0) (s:Z) (s_nz:s <> 0) |
| 160 |
(c:list (Z*Z)) (Hm:m = s - Associational.eval c). |
| 161 |
Definition mulmod {n} (a b:tuple Z n) : tuple Z n |
| 162 |
:= let a_a := to_associational a in |
| 163 |
let b_a := to_associational b in |
| 164 |
let ab_a := Associational.mul a_a b_a in |
| 165 |
let abm_a := Associational.reduce s c ab_a in |
| 166 |
from_associational n abm_a. |
| 167 |
Lemma eval_mulmod {n} (H:(n<>0)%nat) (f g:tuple Z n) : |
| 168 |
eval (mulmod f g) mod m = (eval f * eval g) mod m. |
| 169 |
Proof using Hm m_nz s_nz weight_0 weight_nz. cbv [mulmod]; rewrite Hm in *; push; trivial. Qed. |
| 170 |
End mulmod. |
| 171 |
End Positional. End Positional. |
| 172 |
|
| 173 |
Import Associational Positional. |
| 174 |
Local Coercion Z.of_nat : nat >-> Z. |
| 175 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 176 |
|
| 177 |
Definition w (i:nat) : Z := 2^Qceiling((25+1/2)*i). |
| 178 |
|
| 179 |
Example base_25_5_mul (f g:tuple Z 10) : |
| 180 |
{ fg : tuple Z 10 | (eval w fg) mod (2^255-19) |
| 181 |
= (eval w f * eval w g) mod (2^255-19) }. |
| 182 |
(* manually assign names to limbs for pretty-printing *) |
| 183 |
destruct f as [[[[[[[[[f9 f8]f7]f6]f5]f4]f3]f2]f1]f0]. |
| 184 |
destruct g as [[[[[[[[[g9 g8]g7]g6]g5]g4]g3]g2]g1]g0]. |
| 185 |
eexists ?[fg]. |
| 186 |
erewrite <-eval_mulmod with (s:=2^255) (c:=[(1,19)]) |
| 187 |
by (try eapply pow_ceil_mul_nat_nonzero; vm_decide). |
| 188 |
(* eval w ?fg mod (2 ^ 255 - 19) = *) |
| 189 |
(* eval w *) |
| 190 |
(* (mulmod w (2^255) [(1, 19)] (f9,f8,f7,f6,f5,f4,f3,f2,f1,f0) *) |
| 191 |
(* (g9,g8,g7,g6,g5,g4,g3,g2,g1,g0)) mod (2^255 - 19) *) |
| 192 |
eapply f_equal2; [|trivial]. eapply f_equal. |
| 193 |
(* ?fg = *) |
| 194 |
(* mulmod w (2 ^ 255) [(1, 19)] (f9, f8, f7, f6, f5, f4, f3, f2, f1, f0) *) |
| 195 |
(* (g9, g8, g7, g6, g5, g4, g3, g2, g1, g0) *) |
| 196 |
cbv -[runtime_mul runtime_add]; cbv [runtime_mul runtime_add]. |
| 197 |
ring_simplify_subterms. |
| 198 |
(* ?fg = |
| 199 |
(f0*g9+ f1*g8+ f2*g7+ f3*g6+ f4*g5+ f5*g4+ f6*g3+ f7*g2+ f8*g1+ f9*g0, |
| 200 |
f0*g8+ 2*f1*g7+ f2*g6+ 2*f3*g5+ f4*g4+ 2*f5*g3+ f6*g2+ 2*f7*g1+ f8*g0+ 38*f9*g9, |
| 201 |
f0*g7+ f1*g6+ f2*g5+ f3*g4+ f4*g3+ f5*g2+ f6*g1+ f7*g0+ 19*f8*g9+ 19*f9*g8, |
| 202 |
f0*g6+ 2*f1*g5+ f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