Created
March 19, 2021 18:30
-
-
Save daira/a81fbb51b0093e0b820504410c1a4108 to your computer and use it in GitHub Desktop.
rtl addition chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---> up to here is a multiple of 3 :-) | |
x_p = 0b11001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001101001110100111101110000011001001101000010000101001100000111000101110000011110000111100111111000011001100110011001100110011001101 | |
pchain = Chain() | |
pi = pa = 1 | |
for i in range(1, 114): | |
pi = pchain.sqr(pi) | |
if '101110000011001001101000010000101001100000111000101110000011110000111100111111000011001100110011001100110011001101'[113-i] == '1': | |
pa = pchain.mul(pa, pi) | |
b = '1000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100011010001101' | |
assert int(b, 2)*3 == 0b11001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001101001110100111 | |
pj = pchain.sqrmul(pi, 1, pi) | |
for j in range(1, 140): | |
pj = pchain.sqr(pj) | |
if b[139-j] == '1': | |
pa = pchain.mul(pa, pj) | |
assert pa == x_p, "\n" + format(pa, '0255b') + "\n" + format(x_p, '0255b') | |
print(pchain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment