Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ilitteri committed Sep 29, 2023
1 parent 48f5591 commit 4142dbd
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions scripts/montgomery.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import math

# 2^256
# # 2^256
R = 115792089237316195423570985008687907853269984665640564039457584007913129639936
R_PRIME = 20988524275117001072002809824448087578619730785600314334253784976379291040311
# R^2 = (2^256)^2 = 2^512
R2 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096
# R^3 = (2^256)^3 = 2^768
R3 = 1552518092300708935148979488462502555256886017116696611139052038026050952686376886330878408828646477950487730697131073206171580044114814391444287275041181139204454976020849905550265285631598444825262999193716468750892846853816057856
# R3 % N
R3_MOD_N = 14921786541159648185948152738563080959093619838510245177710943249661917737183
# R_PRIME = 20988524275117001072002809824448087578619730785600314334253784976379291040311
# # R^2 = (2^256)^2 = 2^512
# R2 = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096
# # R^3 = (2^256)^3 = 2^768
# R3 = 1552518092300708935148979488462502555256886017116696611139052038026050952686376886330878408828646477950487730697131073206171580044114814391444287275041181139204454976020849905550265285631598444825262999193716468750892846853816057856
# # R3 % N
# R3_MOD_N = 14921786541159648185948152738563080959093619838510245177710943249661917737183
# Fp
N = 21888242871839275222246405745257275088696311157297823662689037894645226208583
N = int(0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff)
# R2 % N
R2_MOD_N = 3096616502983703923843567936837374451735540968419076528771170197431451843209
R2_MOD_N = 134799733323198995502561713907086292154532538166959272814710328655875
# N' -> NN' ≡ −1 mod R
N_PRIME = 111032442853175714102588374283752698368366046808579839647964533820976443843465
N_PRIME = 115792089210356248768974548684794254293921932838497980611635986753331132366849

ONE = 6350874878119819312338956282401532409788428879151445726012394534686998597021
TWO = 12701749756239638624677912564803064819576857758302891452024789069373997194042
Expand Down Expand Up @@ -129,3 +129,17 @@ def add(augend, addend):

def sub(minuend, subtrahend):
return add(minuend, N - subtrahend)

x = 0x18905f76a53755c679fb732b7762251075ba95fc5fedb60179e730d418a9143c
y = 0x8571ff1825885d85d2e88688dd21f3258b4ab8e4ba19e45cddf25357ce95560a
a = into(115792089210356248762697446949407573530086143415290314195533631308867097853948)
b = into(41058363725152142129326129780047268409114441015993725554835256314039467401291)
assert(mul(y, y) == add(mul(x, mul(x, x)), add(mul(a, x), b)))
print(y*y)
print(hex(y))
print(hex(mul(y, y)))
print(hex(mul(x, mul(x, x))))
print(hex(mul(a, x)))
print(hex(add(mul(a, x), b)))
print(hex(add(mul(x, mul(x, x)), add(mul(a, x), b))))
print(mul(y, y) == add(mul(x, mul(x, x)), add(mul(a, x), b)))

0 comments on commit 4142dbd

Please sign in to comment.