Do poly1305 with absolutely no modulo operators
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from typing import List
|
||||
|
||||
from modulo_theory import friendly_modular_mult, friendly_modulo
|
||||
|
||||
def mask_r(r: int) -> int:
|
||||
r_bytes = r.to_bytes(16, "little")
|
||||
|
||||
@@ -40,7 +42,12 @@ def parallel_poly1305(message: bytes, r: int, s: int, lanes: int):
|
||||
r = mask_r(r)
|
||||
p = 2**130-5
|
||||
|
||||
r_powers = [r**i % p for i in range(lanes+1)]
|
||||
r_powers = [1, r]
|
||||
|
||||
for l_pow_log2 in range(3):
|
||||
l_pow = 2**l_pow_log2
|
||||
for r_pow in range(1,l_pow+1):
|
||||
r_powers.append(friendly_modular_mult(r_powers[l_pow], r_powers[r_pow]))
|
||||
|
||||
acc = [0]*lanes
|
||||
|
||||
@@ -53,12 +60,13 @@ def parallel_poly1305(message: bytes, r: int, s: int, lanes: int):
|
||||
idx = i*lanes + j
|
||||
power = min(lanes, len(blocks) - idx)
|
||||
|
||||
# There is a division here but we can get this value somehow else
|
||||
byte_length = (lane.bit_length() + 7) // 8
|
||||
lane += 1 << (8*byte_length)
|
||||
|
||||
acc[j] = ((acc[j] + lane)*(r_powers[power])) % p
|
||||
acc[j] = friendly_modular_mult(acc[j] + lane, r_powers[power])
|
||||
|
||||
combined_acc = sum(acc) % p
|
||||
combined_acc = friendly_modulo(sum(acc), 0)
|
||||
combined_acc += s
|
||||
|
||||
return combined_acc & (2**128-1)
|
||||
|
||||
Reference in New Issue
Block a user