Debian Codegraph @

src/ArithmeticCPS/Freeze.v

raw
1 From Coq Require Import ZArith Lia.
2 Require Import QArith_base Qround Crypto.Util.QUtil.
3 Require Import Crypto.ArithmeticCPS.BaseConversion.
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.Decidable.
11 Require Import Crypto.Util.ListUtil.
12 Require Import Crypto.Util.Prod.
13 Require Import Crypto.Util.Tactics.BreakMatch.
14 Require Import Crypto.Util.Tactics.DestructHead.
15 Require Import Crypto.Util.ZUtil.EquivModulo.
16 Require Import Crypto.Util.ZUtil.Opp.
17 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
18 Require Import Crypto.Util.ZUtil.Tactics.PeelLe.
19 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
20 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
21
22 Require Import Crypto.Util.Notations.
23 Local Open Scope Z_scope.
24
25 Import CPSBindNotations.
26 Local Open Scope cps_scope.
27
28 (* TODO: rename this module? (Should it be, e.g., [Rows.freeze]?) *)
29 Module Freeze (Import RT : Runtime).
30 Module Import Deps.
31 Module Rows := Rows RT.
32 End Deps.
33 Section Freeze.
34 Context (weight : nat -> Z).
35
36 Definition freeze_cps n mask (m p:list Z) : ~> list Z :=
37 (p_carry <- Rows.sub_cps weight n p m;
38 let '(p, carry) := p_carry in
39 r_carry <- Rows.conditional_add_cps weight n mask (-carry)%RT p m;
40 let '(r, carry) := r_carry in
41 return r).
42 End Freeze.
43 End Freeze.
44
45 Module FreezeModOps (Import RT : Runtime).
46 Module Import Deps.
47 Module Export Positional := Positional RT.
48 Module Export Freeze := Freeze RT.
49 Module BaseConversion := BaseConversion RT.
50 Module Export Core.
51 Module Associational := ArithmeticCPS.Core.Associational RT.
52 End Core.
53 End Deps.
54 Section mod_ops.
55 Local Coercion Z.of_nat : nat >-> Z.
56 Local Coercion QArith_base.inject_Z : Z >-> Q.
57 (* Design constraints:
58 - inputs must be [Z] (b/c reification does not support Q)
59 - internal structure must not match on the arguments (b/c reification does not support [positive]) *)
60 Context (limbwidth_num limbwidth_den : Z)
61 (limbwidth_good : 0 < limbwidth_den <= limbwidth_num)
62 (s : Z)
63 (c : list (Z*Z))
64 (n : nat)
65 (bitwidth : Z)
66 (m_enc : list Z)
67 (Hn_nz : n <> 0%nat).
68 Local Notation bytes_weight := (@weight 8 1).
69 Local Notation weight := (@weight limbwidth_num limbwidth_den).
70 Let m := (s - Associational.eval c).
71
72 Context (Hs : s = weight n).
73 Context (c_small : 0 < Associational.eval c < weight n)
74 (m_enc_bounded : List.map (BinInt.Z.land (Z.ones bitwidth)) m_enc = m_enc)
75 (m_enc_correct : Positional.eval weight n m_enc = m)
76 (Hm_enc_len : length m_enc = n).
77
78 Definition bytes_n
79 := Eval cbv [Qceiling Qdiv inject_Z Qfloor Qmult Qopp Qnum Qden Qinv Pos.mul]
80 in Z.to_nat (Qceiling (Z.log2_up (weight n) / 8)).
81
82 Definition to_bytes_cps (v : list Z) : ~> _
83 := BaseConversion.convert_bases_cps weight bytes_weight n bytes_n v.
84
85 Definition from_bytes_cps (v : list Z) : ~> _
86 := BaseConversion.convert_bases_cps bytes_weight weight bytes_n n v.
87
88 Definition freeze_to_bytesmod_cps (f : list Z) : ~> list Z
89 := (f <- freeze_cps weight n (Z.ones bitwidth) m_enc f; to_bytes_cps f).
90
91 Definition to_bytesmod_cps (f : list Z) : ~> list Z
92 := to_bytes_cps f.
93 Definition to_bytesmod (f : list Z) : list Z := to_bytesmod_cps f _ id.
94
95 Definition from_bytesmod_cps (f : list Z) : ~> list Z
96 := from_bytes_cps f.
97 Definition from_bytesmod (f : list Z) : list Z := from_bytesmod_cps f _ id.
98 End mod_ops.
99 End FreezeModOps.