| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Derive. |
| 3 |
From Coq Require Import List. |
| 4 |
Require Import Crypto.ArithmeticCPS.Core. |
| 5 |
Require Import Crypto.ArithmeticCPS.ModOps. |
| 6 |
Require Import Crypto.Arithmetic.Partition. |
| 7 |
Require Import Crypto.ArithmeticCPS.Saturated. |
| 8 |
Require Import Crypto.Util.CPSUtil. |
| 9 |
Require Import Crypto.Util.CPSNotations. |
| 10 |
Require Import Crypto.Util.LetIn. |
| 11 |
Require Import Crypto.Util.ListUtil. |
| 12 |
|
| 13 |
Require Import Crypto.Util.Notations. |
| 14 |
Local Open Scope Z_scope. |
| 15 |
Import CPSBindNotations. |
| 16 |
Local Open Scope cps_scope. |
| 17 |
|
| 18 |
Module BaseConversion (Import RT : Runtime). |
| 19 |
Module Import Deps. |
| 20 |
Module Export Positional := Positional RT. |
| 21 |
Module Rows := Rows RT. |
| 22 |
Module Export Saturated. |
| 23 |
Module Associational := ArithmeticCPS.Saturated.Associational RT. |
| 24 |
End Saturated. |
| 25 |
Module Export Core. |
| 26 |
Module Associational := ArithmeticCPS.Core.Associational RT. |
| 27 |
End Core. |
| 28 |
End Deps. |
| 29 |
Section BaseConversion. |
| 30 |
Context (sw dw : nat -> Z) (* source/destination weight functions *). |
| 31 |
|
| 32 |
Definition convert_bases_cps (sn dn : nat) (p : list Z) : ~> list Z := |
| 33 |
(p' <- Positional.from_associational_cps dw dn (Positional.to_associational sw sn p); |
| 34 |
chained_carries_no_reduce_cps dw dn p' (seq 0 (pred dn))). |
| 35 |
|
| 36 |
Definition to_associational_cps n m p : ~> list (Z * Z) := |
| 37 |
(p' <- convert_bases_cps n m p; |
| 38 |
return (Positional.to_associational dw m p')). |
| 39 |
|
| 40 |
(* TODO : move to Associational? *) |
| 41 |
Section reorder. |
| 42 |
Definition reordering_carry_cps (w fw : Z) (p : list (Z * Z)) : ~> _ := |
| 43 |
fun T => fold_right_cps2 (fun t acc => |
| 44 |
r <- Associational.carryterm_cps w fw t; |
| 45 |
return (if fst t =? w then acc ++ r else r ++ acc)) nil p. |
| 46 |
End reorder. |
| 47 |
|
| 48 |
(* carry at specified indices in dw, then use Rows.flatten to convert to Positional with sw *) |
| 49 |
Definition from_associational_cps idxs n (p : list (Z * Z)) : ~> list Z := |
| 50 |
(* important not to use Positional.carry here; we don't want to accumulate yet *) |
| 51 |
(p' <- Associational.bind_snd_cps p; |
| 52 |
p' <- (fun T => fold_right_cps2 (fun i acc => reordering_carry_cps (dw i) (dw (S i) / dw i) acc) p' (rev idxs)); |
| 53 |
p' <- Rows.from_associational_cps sw n p'; |
| 54 |
p' <- Rows.flatten_cps sw n p'; |
| 55 |
return (fst p')). |
| 56 |
|
| 57 |
(* Derive from_associational_inlined |
| 58 |
SuchThat (forall idxs n p, |
| 59 |
from_associational_inlined idxs n p = from_associational idxs n p) |
| 60 |
As from_associational_inlined_correct. |
| 61 |
Proof. |
| 62 |
intros. |
| 63 |
cbv beta iota delta [from_associational reordering_carry Associational.carryterm]. |
| 64 |
cbv beta iota delta [Let_In]. (* inlines all shifts/lands from carryterm *) |
| 65 |
cbv beta iota delta [from_associational Rows.from_associational Columns.from_associational]. |
| 66 |
cbv beta iota delta [Let_In]. (* inlines the shifts from place *) |
| 67 |
subst from_associational_inlined; reflexivity. |
| 68 |
Qed. |
| 69 |
|
| 70 |
Derive to_associational_inlined |
| 71 |
SuchThat (forall n m p, |
| 72 |
to_associational_inlined n m p = to_associational n m p) |
| 73 |
As to_associational_inlined_correct. |
| 74 |
Proof. |
| 75 |
intros. |
| 76 |
cbv beta iota delta [ to_associational convert_bases |
| 77 |
Positional.to_associational |
| 78 |
Positional.from_associational |
| 79 |
chained_carries_no_reduce |
| 80 |
carry |
| 81 |
Associational.carry |
| 82 |
Associational.carryterm |
| 83 |
]. |
| 84 |
cbv beta iota delta [Let_In]. |
| 85 |
subst to_associational_inlined; reflexivity. |
| 86 |
Qed. |
| 87 |
*) |
| 88 |
(* carry chain that aligns terms in the intermediate weight with the final weight *) |
| 89 |
Definition aligned_carries (log_dw_sw nout : nat) |
| 90 |
:= (map (fun i => ((log_dw_sw * (i + 1)) - 1))%nat (seq 0 nout)). |
| 91 |
|
| 92 |
Section mul_converted. |
| 93 |
(* Definition mul_converted |
| 94 |
n1 n2 (* lengths in original format *) |
| 95 |
m1 m2 (* lengths in converted format *) |
| 96 |
(n3 : nat) (* final length *) |
| 97 |
(idxs : list nat) (* carries to do -- this helps preemptively line up weights *) |
| 98 |
(p1 p2 : list Z) := |
| 99 |
let p1_a := to_associational n1 m1 p1 in |
| 100 |
let p2_a := to_associational n2 m2 p2 in |
| 101 |
let p3_a := Associational.mul p1_a p2_a in |
| 102 |
from_associational idxs n3 p3_a. |
| 103 |
*) |
| 104 |
End mul_converted. |
| 105 |
End BaseConversion. |
| 106 |
|
| 107 |
(* multiply two (n*k)-bit numbers by converting them to n k-bit limbs each, multiplying, then converting back *) |
| 108 |
Section widemul. |
| 109 |
Context (log2base : Z) (log2base_pos : 0 < log2base). |
| 110 |
Context (m n : nat) (m_nz : m <> 0%nat) (n_nz : n <> 0%nat) (n_le_log2base : Z.of_nat n <= log2base). |
| 111 |
Let dw : nat -> Z := weight (log2base / Z.of_nat n) 1. |
| 112 |
Let sw : nat -> Z := weight log2base 1. |
| 113 |
Let mn := (m * n)%nat. |
| 114 |
Let nout := (m * 2)%nat. |
| 115 |
|
| 116 |
(*Definition widemul a b := mul_converted sw dw m m mn mn nout (aligned_carries n nout) a b.*) |
| 117 |
|
| 118 |
(* Derive widemul_inlined |
| 119 |
SuchThat (forall a b, |
| 120 |
length a = m -> |
| 121 |
length b = m -> |
| 122 |
widemul_inlined a b = Partition.partition sw nout (seval m a * seval m b)) |
| 123 |
As widemul_inlined_correct. |
| 124 |
Proof. |
| 125 |
intros. |
| 126 |
rewrite <-widemul_correct by auto. |
| 127 |
cbv beta iota delta [widemul mul_converted]. |
| 128 |
rewrite <-to_associational_inlined_correct with (p:=a). |
| 129 |
rewrite <-to_associational_inlined_correct with (p:=b). |
| 130 |
rewrite <-from_associational_inlined_correct. |
| 131 |
subst widemul_inlined; reflexivity. |
| 132 |
Qed. |
| 133 |
|
| 134 |
Derive widemul_inlined_reverse |
| 135 |
SuchThat (forall a b, |
| 136 |
length a = m -> |
| 137 |
length b = m -> |
| 138 |
widemul_inlined_reverse a b = Partition.partition sw nout (seval m a * seval m b)) |
| 139 |
As widemul_inlined_reverse_correct. |
| 140 |
Proof. |
| 141 |
intros. |
| 142 |
rewrite <-widemul_inlined_correct by assumption. |
| 143 |
cbv [widemul_inlined]. |
| 144 |
match goal with |- _ = from_associational_inlined sw dw ?idxs ?n ?p => |
| 145 |
transitivity (from_associational_inlined sw dw idxs n (rev p)); |
| 146 |
[ | transitivity (from_associational sw dw idxs n p); [ | reflexivity ] ](* reverse to make addc chains line up *) |
| 147 |
end. |
| 148 |
{ subst widemul_inlined_reverse; reflexivity. } |
| 149 |
{ rewrite from_associational_inlined_correct by auto. |
| 150 |
cbv [from_associational]. |
| 151 |
rewrite !Rows.flatten_correct by eauto using Rows.length_from_associational. |
| 152 |
rewrite !Rows.eval_from_associational by auto. |
| 153 |
f_equal. |
| 154 |
rewrite !eval_carries, !Associational.bind_snd_correct, !Associational.eval_rev by auto. |
| 155 |
reflexivity. } |
| 156 |
Qed.*) |
| 157 |
End widemul. |
| 158 |
End BaseConversion. |