| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import random |
| 12 |
from functools import lru_cache |
| 13 |
from fractions import Fraction |
| 14 |
from math import ceil |
| 15 |
|
| 16 |
|
| 17 |
R = 2**32 |
| 18 |
Q = 8380417 |
| 19 |
Qinv = pow(Q, -1, R) |
| 20 |
NQinv = pow(-Q, -1, R) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
def round_even(x): |
| 29 |
return 2 * round(x / 2) |
| 30 |
|
| 31 |
|
| 32 |
@lru_cache(maxsize=None) |
| 33 |
def barrett_twiddle(b): |
| 34 |
"""Compute twiddle required for Barrett multiplications |
| 35 |
via doubling-high-multiply.""" |
| 36 |
return round_even(b * R / Q) // 2 |
| 37 |
|
| 38 |
|
| 39 |
def sqrdmulh_i32(a, b): |
| 40 |
"""Doubling multiply high with rounding""" |
| 41 |
|
| 42 |
|
| 43 |
return (2 * a * b + 2**31) // 2**32 |
| 44 |
|
| 45 |
|
| 46 |
def barmul(a, b): |
| 47 |
"""Compute doubling Barrett multiplication of a and b""" |
| 48 |
b_twiddle = barrett_twiddle(b) |
| 49 |
return a * b - Q * sqrdmulh_i32(a, b_twiddle) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def lift_signed_i32(x): |
| 58 |
"""Returns signed canonical representative modulo R=2^32.""" |
| 59 |
x = x % R |
| 60 |
if x >= R // 2: |
| 61 |
x -= R |
| 62 |
return x |
| 63 |
|
| 64 |
|
| 65 |
@lru_cache(maxsize=None) |
| 66 |
def montmul_neg_twiddle(b): |
| 67 |
return (b * NQinv) % R |
| 68 |
|
| 69 |
|
| 70 |
@lru_cache(maxsize=None) |
| 71 |
def montmul_pos_twiddle(b): |
| 72 |
return (b * Qinv) % R |
| 73 |
|
| 74 |
|
| 75 |
def montmul_neg(a, b): |
| 76 |
b_twiddle = montmul_neg_twiddle(b) |
| 77 |
return (a * b + Q * lift_signed_i32(a * b_twiddle)) // R |
| 78 |
|
| 79 |
|
| 80 |
def montmul_pos(a, b): |
| 81 |
b_twiddle = montmul_pos_twiddle(b) |
| 82 |
return (a * b - Q * lift_signed_i32(a * b_twiddle)) // R |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
def test_random(f, test_name, num_tests=10000000, bound_a=R // 2, bound_b=Q // 2): |
| 91 |
print(f"Randomly checking {test_name} ({num_tests} tests)...") |
| 92 |
for i in range(num_tests): |
| 93 |
if i % 100000 == 0: |
| 94 |
print(f"... run {i} tests ({((i * 1000) // num_tests)/10}%)") |
| 95 |
a = random.randrange(-bound_a, bound_a) |
| 96 |
b = random.randrange(-bound_b, bound_b) |
| 97 |
f(a, b) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
""" |
| 105 |
For Barrett multiplication implemented with sqrdmulh, Corollary 2 of @[NeonNTT] |
| 106 |
shows the bound |
| 107 |
|
| 108 |
|barmul(a, b)| <= |a| |bR mod⟦⟧ q| / R + q / 2, |
| 109 |
|
| 110 |
where ⟦⟧ denotes round-to-nearest-even-integer approximation (thus |
| 111 |
|bR mod⟦⟧ q| < q). In particular, knowing that a fits inside int32_t (thus |
| 112 |
|a| <= R/2) already implies |barmul(a, b)| < q. |
| 113 |
""" |
| 114 |
|
| 115 |
|
| 116 |
@lru_cache(maxsize=None) |
| 117 |
def modq_even(a): |
| 118 |
return a - Q * round_even(a / Q) |
| 119 |
|
| 120 |
|
| 121 |
def barmul_bound(a, b): |
| 122 |
bp = modq_even(b * R) |
| 123 |
return Fraction(abs(a) * abs(bp), R) + Fraction(Q, 2) |
| 124 |
|
| 125 |
|
| 126 |
def barmul_bound_test(a, b): |
| 127 |
ab = barmul(a, b) |
| 128 |
bound = barmul_bound(a, b) |
| 129 |
if abs(ab) > bound: |
| 130 |
print(f"barmul_bound_test failure for (a,b)={(a,b)}") |
| 131 |
print(f"barmul(a,b): {ab}") |
| 132 |
print(f"bound: {bound}") |
| 133 |
assert False |
| 134 |
|
| 135 |
|
| 136 |
def barmul_bound_test_random(): |
| 137 |
test_random( |
| 138 |
barmul_bound_test, |
| 139 |
"bound on Barrett multiplication, as used in AArch64 [I]NTT", |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
def barmul_bound_test_exceeds_3_quarters(): |
| 144 |
""" |
| 145 |
This example shows an extreme but legitimate case where the Barrett |
| 146 |
multiplication in the last butterfly of AArch64 INTT computes an output with |
| 147 |
magnitude larger than ceil(3q/4). |
| 148 |
|
| 149 |
The specific zeta used in that butterly can be found at the 32nd to last |
| 150 |
entry of the array mld_aarch64_intt_zetas_layer123456[] in |
| 151 |
dev/aarch64_clean/src/aarch64_zetas.c. |
| 152 |
""" |
| 153 |
zeta = -294725 |
| 154 |
a_worst = -2145337212 |
| 155 |
a_zeta_worst = 6390331 |
| 156 |
assert abs(a_worst) <= 256 * (Q - 1) |
| 157 |
assert barmul(a_worst, zeta) == a_zeta_worst |
| 158 |
assert abs(a_zeta_worst) > ceil(Fraction(3 * Q, 4)) |
| 159 |
|
| 160 |
|
| 161 |
barmul_bound_test_random() |
| 162 |
barmul_bound_test_exceeds_3_quarters() |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
""" |
| 171 |
In @[Survey_Hwang23, Section 2.2], the author noted the bound* |
| 172 |
|
| 173 |
|montmul(a, b)| <= (q/2) (1 + |a|/R). |
| 174 |
|
| 175 |
In particular, knowing that a fits inside int32_t (thus |a| <= R/2) already |
| 176 |
implies |montmul(a, b)| <= 3q/4 < ceil(3q/4). |
| 177 |
|
| 178 |
(*) Strictly speaking, they considered the negative/additive variant |
| 179 |
montmul_neg(a, b), but the exact same bound and proof also work for the |
| 180 |
positive/subtractive variant montmul_pos(a, b). |
| 181 |
""" |
| 182 |
|
| 183 |
|
| 184 |
def montmul_pos_const_bound(a): |
| 185 |
return Fraction(Q, 2) * (1 + Fraction(abs(a), R)) |
| 186 |
|
| 187 |
|
| 188 |
def montmul_pos_const_bound_test(a, b): |
| 189 |
ab = montmul_pos(a, b) |
| 190 |
bound = montmul_pos_const_bound(a) |
| 191 |
if abs(ab) > bound: |
| 192 |
print(f"montmul_pos_const_bound_test failure for (a,b)={(a,b)}") |
| 193 |
print(f"montmul_pos(a,b): {ab}") |
| 194 |
print(f"bound: {bound}") |
| 195 |
assert False |
| 196 |
|
| 197 |
|
| 198 |
def montmul_pos_const_bound_test_random(): |
| 199 |
test_random( |
| 200 |
montmul_pos_const_bound_test, |
| 201 |
"bound on Montgomery multiplication with constant, as used in AVX2 [I]NTT", |
| 202 |
) |
| 203 |
|
| 204 |
|
| 205 |
def montmul_pos_const_bound_tight(): |
| 206 |
""" |
| 207 |
This example shows that, unless we know more about a or b, the bound |
| 208 |
|montmul(a, b)| < MONTMUL_BOUND := ceil(3q/4) is the tightest exclusive |
| 209 |
bound. |
| 210 |
""" |
| 211 |
a_worst = -R // 2 |
| 212 |
b_worst = -(Q - 3) // 2 |
| 213 |
ab_worst = montmul_pos(a_worst, b_worst) |
| 214 |
bound = ceil(Fraction(3 * Q, 4)) |
| 215 |
assert ab_worst == bound - 1 |
| 216 |
|
| 217 |
|
| 218 |
montmul_pos_const_bound_test_random() |
| 219 |
montmul_pos_const_bound_tight() |