Add poly1305 python implementation
This commit is contained in:
54
ChaCha20_Poly1305_64/sim/do_poly_1305.py
Normal file
54
ChaCha20_Poly1305_64/sim/do_poly_1305.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
def mask_r(r: int) -> int:
|
||||||
|
r_bytes = r.to_bytes(16, "little")
|
||||||
|
|
||||||
|
r_masked = bytearray(r_bytes)
|
||||||
|
r_masked[3] &= 15;
|
||||||
|
r_masked[7] &= 15;
|
||||||
|
r_masked[11] &= 15;
|
||||||
|
r_masked[15] &= 15;
|
||||||
|
r_masked[4] &= 252;
|
||||||
|
r_masked[8] &= 252;
|
||||||
|
r_masked[12] &= 252;
|
||||||
|
|
||||||
|
|
||||||
|
r_masked = int.from_bytes(r_masked, "little")
|
||||||
|
|
||||||
|
return r_masked
|
||||||
|
|
||||||
|
|
||||||
|
def poly1305(message: bytes, r: int, s: int):
|
||||||
|
r = mask_r(r)
|
||||||
|
p = 2**130-5
|
||||||
|
acc = 0
|
||||||
|
|
||||||
|
blocks = [int.from_bytes(message[i:i+16], "little") for i in range(0, len(message), 16)]
|
||||||
|
|
||||||
|
for block in blocks:
|
||||||
|
byte_length = (block.bit_length() + 7) // 8
|
||||||
|
|
||||||
|
block += 1 << (8*byte_length)
|
||||||
|
|
||||||
|
acc = ((acc+block)*r) % p
|
||||||
|
|
||||||
|
acc += s
|
||||||
|
|
||||||
|
return acc & (2**128-1)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
r = 0xa806d542fe52447f336d555778bed685
|
||||||
|
s = 0x1bf54941aff6bf4afdb20dfb8a800301
|
||||||
|
|
||||||
|
golden_result = 0xa927010caf8b2bc2c6365130c11d06a8
|
||||||
|
|
||||||
|
msg = b"Cryptographic Forum Research Group"
|
||||||
|
|
||||||
|
result = poly1305(msg, r, s)
|
||||||
|
|
||||||
|
print(f"{golden_result:x}")
|
||||||
|
print(f"{result:x}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user