| 1 |
From Coq Require Import ZArith QArith Qabs Qround. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Util.QUtil. |
| 4 |
|
| 5 |
(* |
| 6 |
This is a formalization of (a part of) section 2.4 of the following paper |
| 7 |
Efficient Multiplication of Somewhat Small Integers Using Number-Theoretic Transforms |
| 8 |
Hanno Becker, Vincent Hwang, Matthias J. Kannwischer, Lorenz Panny, Bo-Yin Yang |
| 9 |
IWSEC 2022 |
| 10 |
*) |
| 11 |
|
| 12 |
Local Open Scope Q_scope. |
| 13 |
|
| 14 |
Section Qround_half_up. |
| 15 |
Local Coercion inject_Z : Z >-> Q. |
| 16 |
Local Coercion Zpos : positive >-> Z. |
| 17 |
|
| 18 |
Definition Qround_half_up (x: Q): Z := Qfloor (x + (1#2))%Q. |
| 19 |
|
| 20 |
Lemma Qfloor_approx (x: Q): |
| 21 |
Qabs (x - Qfloor x) < 1%Z. |
| 22 |
Proof. |
| 23 |
rewrite Qabs_Qlt_condition. |
| 24 |
pose proof (Qfloor_le x) as H1. |
| 25 |
pose proof (Qlt_floor x) as H2. |
| 26 |
destruct x as (x_num & x_den). |
| 27 |
cbv [Qlt Qle] in *. simpl in *. lia. |
| 28 |
Qed. |
| 29 |
|
| 30 |
Lemma Qround_half_up_approx (x: Q): |
| 31 |
Qabs (x - Qround_half_up x) <= 1#2. |
| 32 |
Proof. |
| 33 |
rewrite Qabs_Qle_condition. |
| 34 |
pose proof (Qfloor_le (x + (1#2))) as H1. |
| 35 |
pose proof (Qlt_floor (x + (1#2))) as H2. |
| 36 |
destruct x as (x_num & x_den). |
| 37 |
cbv [Qlt Qle Qplus Qround_half_up] in *. simpl in *. lia. |
| 38 |
Qed. |
| 39 |
|
| 40 |
Lemma Qfloor_diff (x1 x2: Q) (Hlt: (Qfloor x1 < Qfloor x2)%Z): |
| 41 |
exists (n: Z), x1 < n <= x2. |
| 42 |
Proof. |
| 43 |
exists (Qfloor x2). |
| 44 |
pose proof (Qlt_floor x1) as H1. |
| 45 |
pose proof (Qfloor_le x2) as H2. |
| 46 |
split; [|assumption]. |
| 47 |
apply (Qlt_le_trans _ _ _ H1). |
| 48 |
cbv [Qle]; simpl. lia. |
| 49 |
Qed. |
| 50 |
|
| 51 |
Lemma Qround_half_up_diff (x1 x2: Q) |
| 52 |
(Hlt: (Qround_half_up x1 < Qround_half_up x2)%Z): |
| 53 |
exists (n: Z), x1 < n + (1#2) <= x2. |
| 54 |
Proof. |
| 55 |
unfold Qround_half_up in Hlt. |
| 56 |
apply Qfloor_diff in Hlt. |
| 57 |
destruct Hlt as (n & Hlt & Hle). |
| 58 |
exists (n - 1)%Z. |
| 59 |
destruct x1 as (x_num1 & x_den1). |
| 60 |
destruct x2 as (x_num2 & x_den2). |
| 61 |
cbv [Qlt Qle] in *; simpl in *. |
| 62 |
split; try lia. |
| 63 |
Qed. |
| 64 |
|
| 65 |
Lemma Qround_half_up_eq (a: Z) (b: positive): |
| 66 |
Qround_half_up (a#b) = if (2 * Z.modulo a b <? b)%Z then Qfloor (a#b) else ((Qfloor (a#b)) + 1)%Z. |
| 67 |
Proof. |
| 68 |
cbv [Qround_half_up Qplus Qnum Qden]. rewrite Z.mul_1_l. |
| 69 |
repeat rewrite Qmake_Qdiv, <- Zdiv_Qdiv. |
| 70 |
pose proof (Z.mod_pos_bound a b ltac:(lia)) as Hab. |
| 71 |
pose proof (Zlt_cases (2 * Z.modulo a b)%Z b) as Hcond. |
| 72 |
pose proof (Z_div_mod_eq_full a b) as Ha. |
| 73 |
symmetry; apply (Z.div_unique_pos _ _ _ (if (2 * Z.modulo a b <? b)%Z then (2 * Z.modulo a b + b)%Z else (2 * Z.modulo a b - b)%Z)). |
| 74 |
all: destruct (_ <? _)%Z; lia. |
| 75 |
Qed. |
| 76 |
End Qround_half_up. |
| 77 |
|
| 78 |
Section SignedMod. |
| 79 |
Local Coercion inject_Z : Z >-> Q. |
| 80 |
Local Coercion Zpos : positive >-> Z. |
| 81 |
|
| 82 |
Definition mod_approx (approx: Q -> Z) (a: Z) (b: positive): Z := |
| 83 |
(a - b * approx (a#b))%Z. |
| 84 |
|
| 85 |
Lemma Zmod_is_mod_approx_floor: |
| 86 |
forall (a: Z) (b: positive), |
| 87 |
Z.modulo a b = mod_approx Qfloor a b. |
| 88 |
Proof. |
| 89 |
intros. unfold mod_approx. |
| 90 |
rewrite Qmake_Qdiv, <- Zdiv_Qdiv. |
| 91 |
apply Z.mod_eq. discriminate. |
| 92 |
Qed. |
| 93 |
|
| 94 |
Definition signed_mod: Z -> positive -> Z := mod_approx Qround_half_up. |
| 95 |
|
| 96 |
Lemma signed_mod_eq_Zmod (a: Z) (q: positive): |
| 97 |
signed_mod a q = if (2 * Z.modulo a q <? q)%Z then Z.modulo a q else (Z.modulo a q - q)%Z. |
| 98 |
Proof. |
| 99 |
cbv [signed_mod mod_approx]. |
| 100 |
rewrite Qround_half_up_eq. |
| 101 |
destruct (_ <? _)%Z; rewrite Zmod_is_mod_approx_floor; [reflexivity|]. |
| 102 |
cbv [mod_approx]; lia. |
| 103 |
Qed. |
| 104 |
|
| 105 |
Lemma Zmod_eq_signed_mod (a: Z) (q: positive): |
| 106 |
Z.modulo a q = if (2 * Z.modulo a q <? q)%Z then signed_mod a q else (signed_mod a q + q)%Z. |
| 107 |
Proof. rewrite signed_mod_eq_Zmod; destruct (_ <? _)%Z; lia. Qed. |
| 108 |
|
| 109 |
Lemma signed_mod_Zmod (a: Z) (q: positive): |
| 110 |
signed_mod a q = signed_mod (Z.modulo a q) q. |
| 111 |
Proof. |
| 112 |
do 2 rewrite signed_mod_eq_Zmod. |
| 113 |
rewrite Z.mod_mod by congruence. reflexivity. |
| 114 |
Qed. |
| 115 |
|
| 116 |
Lemma Zmod_signed_mod (a: Z) (q: positive): |
| 117 |
Z.modulo a q = Z.modulo (signed_mod a q) q. |
| 118 |
Proof. |
| 119 |
rewrite signed_mod_eq_Zmod. |
| 120 |
destruct (_ <? _)%Z; [rewrite Z.mod_mod by congruence; reflexivity|]. |
| 121 |
rewrite Zminus_mod_idemp_l, Zminus_mod, Z.mod_same, Z.sub_0_r, Z.mod_mod by congruence. |
| 122 |
reflexivity. |
| 123 |
Qed. |
| 124 |
|
| 125 |
Lemma signed_mod_mod (a: Z) (q: positive): |
| 126 |
signed_mod (signed_mod a q) q = signed_mod a q. |
| 127 |
Proof. |
| 128 |
rewrite (signed_mod_eq_Zmod a q) at 1. symmetry. |
| 129 |
destruct (_ <? _)%Z; [apply signed_mod_Zmod|]. |
| 130 |
rewrite (signed_mod_Zmod (_ - _)%Z). |
| 131 |
rewrite Zminus_mod_idemp_l, Zminus_mod, Z.mod_same, Z.sub_0_r, Z.mod_mod by congruence. |
| 132 |
apply signed_mod_Zmod. |
| 133 |
Qed. |
| 134 |
End SignedMod. |
| 135 |
|
| 136 |
Section RefinedBarrettReduction. |
| 137 |
Local Coercion inject_Z : Z >-> Q. |
| 138 |
Local Coercion Zpos : positive >-> Z. |
| 139 |
|
| 140 |
(* Barrett reduction is the special case with b = 1 *) |
| 141 |
Definition barrett_multiplication_approx |
| 142 |
(approx: Q -> Z) (R a b: Z) (q: positive): Z := |
| 143 |
(a * b - q * Qround_half_up ((a * (approx ((b * R)#q)))%Z / R))%Z. |
| 144 |
|
| 145 |
(* Not the same bounds on |a| and |b| as in the paper, as theirs cannot be proved *) |
| 146 |
Lemma barrett_multiplication_approx_correct |
| 147 |
(approx: Q -> Z) (M R a b: Z) (k q: positive) |
| 148 |
(Hk: Qabs (((b * R)#q) - approx ((b * R)#q)) <= 1#(Pos.pow 2 k)) |
| 149 |
(HOddq: Z.Odd q) (HR: R = Z.pow 2 (M - 1 + Z.log2 q - Z.log2_up (Z.abs b))%Z) |
| 150 |
(HM: (2 <= M)%Z) (Ha: (Z.abs a <= Z.pow 2 (M - 1))%Z) |
| 151 |
(Hb: (Z.abs b <= Z.pow 2 (M - 2))%Z) |
| 152 |
(Ha': (Z.abs a <= Z.pow 2 ((M - 2) - ((Z.log2_up (Z.abs b)) - (k - 1))))%Z): |
| 153 |
barrett_multiplication_approx approx R a b q = signed_mod (a * b)%Z q. |
| 154 |
Proof. |
| 155 |
assert (Hlog2b: (Z.log2_up (Z.abs b) <= M - 2)%Z). |
| 156 |
{ refine (proj1 (Log2.Z.log2_up_le_pow2_full _ _ _) Hb). lia. } |
| 157 |
pose proof (Z.log2_nonneg q) as Hlog2q_nonneg. |
| 158 |
pose proof (Z.pow_pos_nonneg 2 (M - 1 + Z.log2 q - Z.log2_up (Z.abs b))%Z ltac:(lia) ltac:(lia)) as HRpos. |
| 159 |
unfold barrett_multiplication_approx, signed_mod, mod_approx. |
| 160 |
assert (Qround_half_up ((a * approx (b * R # q))%Z / R) = Qround_half_up (a * b # q)) as ->; [|reflexivity]. |
| 161 |
match goal with |- ?x = ?y => destruct (Z.eq_dec x y) as [?|Hne]; auto end. |
| 162 |
match goal with |- Qround_half_up ?x = Qround_half_up ?y => set (delta := x - y) end. |
| 163 |
assert (Hdelta: Qabs delta == ((Qabs a) / R) * Qabs ((b * R # q) - approx (b * R # q))). |
| 164 |
{ unfold delta. rewrite Qabs_Qminus. |
| 165 |
assert ((a * b)#q == (a / R) * ((b * R) # q)) as ->. |
| 166 |
{ rewrite Qmake_Qdiv, Qmake_Qdiv. |
| 167 |
repeat rewrite inject_Z_mult. field. |
| 168 |
split; [discriminate|]. cbv [Qeq]; simpl. |
| 169 |
rewrite HR. lia. } |
| 170 |
assert ((a * approx _)%Z / R == a / R * approx (b * R # q)) as ->. |
| 171 |
{ repeat rewrite inject_Z_mult. field. |
| 172 |
cbv [Qeq]; simpl. rewrite HR; lia. } |
| 173 |
assert (a / R * _ - a / R * _ == a / R * ((b * R # q) - approx (b * R # q))) as ->. |
| 174 |
{ field. cbv [Qeq]; simpl; rewrite HR; lia. } |
| 175 |
rewrite Qabs_Qmult. assert (Qabs (a / R) == Qabs a / R) as ->; [|reflexivity]. |
| 176 |
cbv [Qdiv]. rewrite Qabs_Qmult, Qabs_Qinv. |
| 177 |
rewrite (Qabs_pos R); [reflexivity|]. |
| 178 |
cbv [Qle]; simpl. rewrite HR; lia. } |
| 179 |
match goal with |- ?x = ?y => assert (x < y \/ y < x)%Z as Hlt by lia end. |
| 180 |
assert (Hdelta': 1#(2 * q)%positive <= Qabs delta). |
| 181 |
{ destruct Hlt as [Hlt|Hlt]; apply Qround_half_up_diff in Hlt; destruct Hlt as (n & Hlo & Hhi). |
| 182 |
- apply (proj1 (Qle_minus_iff _ _)) in Hhi. |
| 183 |
apply Qopp_lt_compat in Hlo. |
| 184 |
apply (proj2 (Qplus_lt_r _ _ ((a * b # q)))) in Hlo. |
| 185 |
setoid_replace ((a * b # q) + - ((a * approx (b * R # q))%Z / R)) with (- delta) in Hlo. |
| 186 |
2:{ unfold delta. unfold Qminus. rewrite Qopp_plus, Qopp_involutive, Qplus_comm. reflexivity. } |
| 187 |
rewrite <- Qabs_opp. apply Qabs_ge. |
| 188 |
refine (Qle_trans _ ((a * b # q) + - (n + (1 # 2))) _ _ _). |
| 189 |
2: apply Qlt_le_weak; assumption. |
| 190 |
clear -HOddq Hhi. |
| 191 |
cbv [Qopp Qplus Qle] in *. simpl in *. |
| 192 |
assert (0 < (a * b * 2 + - (n * 2 + 1) * q))%Z; [|nia]. |
| 193 |
destruct (Z.eq_dec 0 (a * b * 2 + - (n * 2 + 1) * q)%Z) as [He|?]; [|lia]. |
| 194 |
exfalso. apply (Z.Even_Odd_False ((n * 2 + 1) * q)%Z). |
| 195 |
+ exists (a * b)%Z. lia. |
| 196 |
+ destruct HOddq as (? & ->). exists (2 * n * x + n + x)%Z. lia. |
| 197 |
- apply (proj1 (Qlt_minus_iff _ _)) in Hlo. |
| 198 |
apply (proj2 (Qplus_le_l _ _ (- (a * b # q)))) in Hhi. |
| 199 |
apply Qabs_ge. unfold delta. unfold Qminus. |
| 200 |
refine (Qle_trans _ _ _ _ Hhi). |
| 201 |
clear -HOddq Hlo. |
| 202 |
cbv [Qopp Qplus Qlt Qle] in *. simpl in *. |
| 203 |
assert (0 < ((n * 2 + 1) * q + - (a * b) * 2))%Z; [|nia]. |
| 204 |
lia. } |
| 205 |
exfalso. assert (Qabs delta < 1 # 2 * q) as X; [|apply (Qlt_not_le _ _ X); auto]. |
| 206 |
rewrite Hdelta. |
| 207 |
pose proof (Qle_lt_or_eq _ _ (Qabs_nonneg a)) as [Hpos|Hz]. |
| 208 |
- refine (Qle_lt_trans _ ((Qabs a / R) * (1 # 2 ^ k)) _ _ _). |
| 209 |
+ apply Qmult_le_l; auto. |
| 210 |
apply Qlt_shift_div_l; cbv [Qmult Qlt] in *; simpl in *; auto. |
| 211 |
rewrite HR; lia. |
| 212 |
+ assert ((Qabs a / R) * (1 # 2 ^ k) == Qabs a / (R * 2 ^ k)%Z) as ->. |
| 213 |
{ rewrite HR, <- Z.pow_add_r; try lia. |
| 214 |
rewrite <- (Z2Pos.id (Z.pow 2 _)) by lia. |
| 215 |
rewrite <- (Z2Pos.id (Z.pow 2 (_ + k))) by lia. |
| 216 |
rewrite <- (Zabs_Qabs a xH), <- (Qmake_Qdiv (Z.abs a) _), <- (Qmake_Qdiv (Z.abs a) _). |
| 217 |
cbv [Qmult Qeq Qnum Qden]. |
| 218 |
rewrite Z2Pos.id by lia. |
| 219 |
rewrite Pos2Z.inj_mul, Z2Pos.id by lia. |
| 220 |
rewrite Pos2Z.inj_pow, <- Z.pow_add_r by lia. lia. } |
| 221 |
rewrite HR, <- Z.pow_add_r; try lia. |
| 222 |
rewrite <- (Z2Pos.id (Z.pow _ _)) by lia. |
| 223 |
rewrite <- (Zabs_Qabs a xH), <- (Qmake_Qdiv (Z.abs a) (Z.to_pos _)). |
| 224 |
cbv [Qlt Qnum Qden]. rewrite Z2Pos.id by lia. |
| 225 |
apply (Z.le_lt_trans _ (2 ^ (M - 2 - (Z.log2_up (Z.abs b) - (k - 1))) * (2 * Z.pos q))%Z); [nia|]. |
| 226 |
rewrite Z.mul_1_l, Z.mul_assoc, (Z.mul_comm _ 2), <- Z.pow_succ_r by lia. |
| 227 |
rewrite <- Z.add_1_r. |
| 228 |
apply (Z.lt_le_trans _ (2 ^ (M - 2 - (Z.log2_up (Z.abs b) - (k - 1)) + 1) * Z.pow 2 (Z.succ (Z.log2 q)))%Z). |
| 229 |
* apply Zmult_lt_compat_l; [|apply Z.log2_spec; lia]. |
| 230 |
apply Z.pow_pos_nonneg; lia. |
| 231 |
* rewrite <- Z.add_1_l, <- Z.pow_add_r; [|lia|lia]. |
| 232 |
apply Z.pow_le_mono_r; [lia|]. lia. |
| 233 |
- rewrite <- Hz. cbv [Qmult Qlt]; simpl. lia. |
| 234 |
Qed. |
| 235 |
|
| 236 |
(* Assumes R_pow, c and v are precomputed *) |
| 237 |
(* M is usually the bitwidth *) |
| 238 |
(* R_pow := M - 1 + Z.log2 q *) |
| 239 |
(* c := approx ((Z.pow 2 R_pow)#q) *) |
| 240 |
(* v := Z.shiftl 1 (R_pow - 1) *) |
| 241 |
Definition barrett_reduce_approx (R_pow c v a: Z) (q: positive): Z := |
| 242 |
let t := (a * c)%Z in |
| 243 |
let t := (t + v)%Z in |
| 244 |
let t := Z.shiftr t R_pow in |
| 245 |
(a - q * t)%Z. |
| 246 |
|
| 247 |
Lemma barrett_reduce_approx_eq (approx: Q -> Z) (M R_pow c v a: Z) (q: positive) |
| 248 |
(HM: (2 <= M)%Z) |
| 249 |
(HR_pow: R_pow = (M - 1 + Z.log2 q)%Z) |
| 250 |
(Hc: c = approx ((Z.pow 2 R_pow)#q)) |
| 251 |
(Hv: v = Z.shiftl 1 (R_pow - 1)): |
| 252 |
barrett_reduce_approx R_pow c v a q = barrett_multiplication_approx approx (Z.pow 2 (M - 1 + Z.log2 q)%Z) a 1%Z q. |
| 253 |
Proof. |
| 254 |
cbv [barrett_multiplication_approx barrett_reduce_approx Qround_half_up]. |
| 255 |
rewrite Z.mul_1_r, Z.mul_1_l. |
| 256 |
subst v; rewrite Z.shiftl_1_l. rewrite <- HR_pow, <- Hc. |
| 257 |
assert (Hle: (0 <= R_pow - 1)%Z) by (rewrite HR_pow; pose proof (Z.log2_nonneg q); lia). |
| 258 |
assert (_ + _ == (a * c + 2 ^ (R_pow - 1))%Z / (Z.pow 2 R_pow)) as ->. |
| 259 |
{ rewrite <- (Z2Pos.id (Z.pow 2 R_pow)) by (apply Z.pow_pos_nonneg; lia). |
| 260 |
do 2 rewrite <- (Qmake_Qdiv). |
| 261 |
cbv [Qplus Qnum Qden]. rewrite Z.mul_1_l. |
| 262 |
rewrite <- (Qmult_1_r (_ # (Z.to_pos (Z.pow 2 R_pow)))). |
| 263 |
assert (1 == 2#2) as -> by reflexivity. |
| 264 |
cbv [Qmult Qnum Qden]. rewrite Z.mul_add_distr_r. |
| 265 |
rewrite (Z.mul_comm (Z.pow _ _) 2). |
| 266 |
rewrite <- Z.pow_succ_r by assumption. |
| 267 |
assert (Z.succ _ = R_pow)%Z as -> by lia. |
| 268 |
rewrite Z2Pos.id; [reflexivity|]. apply Z.pow_pos_nonneg; lia. } |
| 269 |
rewrite <- Zdiv_Qdiv, <- Z.shiftr_div_pow2 by lia. reflexivity. |
| 270 |
Qed. |
| 271 |
|
| 272 |
Lemma barrett_reduce_approx_correct |
| 273 |
(approx: Q -> Z) (M R_pow c v a: Z) (k q: positive) |
| 274 |
(HR_pow: R_pow = (M - 1 + Z.log2 q)%Z) |
| 275 |
(Hc: c = approx ((Z.pow 2 R_pow)#q)) |
| 276 |
(Hv: v = Z.shiftl 1 (R_pow - 1)) |
| 277 |
(Hk: Qabs (((Z.pow 2 (M - 1 + Z.log2 q)%Z)#q) - approx ((Z.pow 2 (M - 1 + Z.log2 q)%Z)#q)) <= 1#(Pos.pow 2 k)) |
| 278 |
(HOddq: Z.Odd q) (HM: (2 <= M)%Z) (Ha: (Z.abs a <= Z.pow 2 (M - 1))%Z) |
| 279 |
(Ha': (Z.abs a <= Z.pow 2 ((M - 2) + (k - 1)))%Z): |
| 280 |
barrett_reduce_approx R_pow c v a q = signed_mod a q. |
| 281 |
Proof. |
| 282 |
erewrite barrett_reduce_approx_eq by eassumption. |
| 283 |
erewrite barrett_multiplication_approx_correct; eauto. |
| 284 |
- rewrite Z.mul_1_r; reflexivity. |
| 285 |
- rewrite Z.mul_1_l. exact Hk. |
| 286 |
- assert (Z.log2_up _ = 0)%Z as -> by reflexivity. |
| 287 |
rewrite Z.sub_0_r. reflexivity. |
| 288 |
- assert (Z.abs 1 = Z.pow 2 0)%Z as -> by reflexivity. |
| 289 |
apply Z.pow_le_mono_r; lia. |
| 290 |
- assert (Z.log2_up _ = 0)%Z as -> by reflexivity. |
| 291 |
assert (M - 2 - (0 - (k - 1)) = M - 2 + (k - 1))%Z as -> by lia. |
| 292 |
exact Ha'. |
| 293 |
Qed. |
| 294 |
End RefinedBarrettReduction. |
| 295 |
|
| 296 |
Module ExampleMLKEM. |
| 297 |
Local Coercion inject_Z : Z >-> Q. |
| 298 |
Local Coercion Zpos : positive >-> Z. |
| 299 |
Definition M := 16%Z. |
| 300 |
Definition q := 3329%positive. |
| 301 |
Definition k := 2%positive. |
| 302 |
|
| 303 |
Definition R_pow := Eval compute in (M - 1 + Z.log2 q)%Z. (* 26%Z *) |
| 304 |
Definition c := Eval compute in Qround_half_up ((Z.pow 2 R_pow)#q). (* 20159%Z *) |
| 305 |
Definition v := Eval compute in Z.shiftl 1 (R_pow - 1)%Z. (* 33554432%Z *) |
| 306 |
|
| 307 |
Lemma Hk: Qabs (((Z.pow 2 (M - 1 + Z.log2 q)%Z)#q) - Qround_half_up ((Z.pow 2 (M - 1 + Z.log2 q)%Z)#q)) <= 1#(Pos.pow 2 k). |
| 308 |
Proof. compute. congruence. Qed. |
| 309 |
|
| 310 |
Lemma HOddq: Z.Odd q. Proof. exists (q/2)%Z. reflexivity. Qed. |
| 311 |
|
| 312 |
(* For comparison https://github.com/pq-crystals/kyber/blob/main/ref/reduce.c *) |
| 313 |
(* int16_t barrett_reduce(int16_t a) { *) |
| 314 |
(* int16_t t; *) |
| 315 |
(* const int16_t v = ((1<<26) + KYBER_Q/2)/KYBER_Q; *) |
| 316 |
|
| 317 |
(* t = ((int32_t)v*a + (1<<25)) >> 26; *) |
| 318 |
(* t *= KYBER_Q; *) |
| 319 |
(* return a - t; *) |
| 320 |
(* } *) |
| 321 |
|
| 322 |
Definition mlkem_barrett_reduce (a: Z): Z := |
| 323 |
let t := (a * 20159)%Z in |
| 324 |
let t := (t + 33554432)%Z in |
| 325 |
let t := Z.shiftr t 26 in |
| 326 |
(a - q * t)%Z. |
| 327 |
|
| 328 |
Lemma MLKEM_barrett_reduce_correct (a: Z) (Ha: (Z.abs a <= Z.pow 2 15)%Z): |
| 329 |
mlkem_barrett_reduce a = signed_mod a q. |
| 330 |
Proof. |
| 331 |
assert (mlkem_barrett_reduce a = barrett_reduce_approx R_pow c v a q) as -> by reflexivity. |
| 332 |
apply (barrett_reduce_approx_correct Qround_half_up M R_pow c v a k q ltac:(reflexivity) ltac:(reflexivity) ltac:(reflexivity) Hk HOddq ltac:(unfold M; lia) ltac:(apply Ha) ltac:(apply Ha)). |
| 333 |
Qed. |
| 334 |
End ExampleMLKEM. |