p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
E = EllipticCurve(GF(p), [0, 7])
G = E.point( (0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)) # Base point
import random
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def sign(privkey,nonce,message):
z=message
nonceG=nonce*G
rx,rye=nonceG.xy()
rx=int(rx)
s=(z+int(rx)*privkey)*modinv(nonce,n)%n
return int(rx), int(s), int(z)
#
private=0x12345678901234567890 #example
public_key=private*G
print('public_key:',public_key)
for i in range(1,5):
nonce=random.randrange(2**256-1)
message=random.randrange(2**256-1)
r,s,z = sign(private,nonce,message)
print("k = ",(nonce))
print()
print("r = ",(r))
print("s = ",(s))
print("z = ",(z))
print()
#output
public_key: (105243071627259047368667483594044765021982824084363382876153694762750522988401 :
104125598182348586056118441571689985999353468102348956648162259104921527704151 : 1)
k =
29083908611578474659724835409188629928450877954572645032653586874541462703237
r = 59696872847024287764883588359229125525293276767477693801880266944929876663510
s = 80010627594232692567321382817171148350979277410542815563295883382250157622365
z = 29046398954207568557559337812845639802158825374855453790727264507720942269061
k =
69337410267126143733445433769841521036184947061861184729931918182952636713603
r = 66204497299302794813448884860580533040028016382253230896212796932956850307851
s = 78197288316910247412893934169331566081232042979407260898010071194692833862752
z = 61490995350470215933587146879107314396788082593003439971438111557980048214364
k =
54369543228147138762559582927936238614035377338157105228406927029717132153226
r = 37044516715065049012561472807452925555312743982989311611730725335033602827346
s = 92461946708536229787310797942574898629331316369909556197824596742434968354935
z = 53622079066303081310409199779963535117997475674801702670649253314258639177195
k =
40168673992090659834561921614463767550862354984631432380883735769884152505584
r = 103919090362588117180151567326283890021253888086769542803093556743402383166255
s = 71275504518099351203494061824773539914502255758119765372192658739494934607323
z = 2815976014827110489026899227085751067228569669674380545422790815579479978003
#data.json
{
"curve": "SECP256K1",
"public_key": [
105243071627259047368667483594044765021982824084363382876153694762750522988401,
104125598182348586056118441571689985999353468102348956648162259104921527704151
],
"known_type": "MSB",
"known_bits":
?,
? how to set correct value "signatures": [
{
"r": 59696872847024287764883588359229125525293276767477693801880266944929876663510,
"s": 80010627594232692567321382817171148350979277410542815563295883382250157622365,
"kp":
? how to set correct value },
{
"r": 66204497299302794813448884860580533040028016382253230896212796932956850307851,
"s": 78197288316910247412893934169331566081232042979407260898010071194692833862752,
"kp":
? how to set correct value },
{
"r": 37044516715065049012561472807452925555312743982989311611730725335033602827346,
"s": 92461946708536229787310797942574898629331316369909556197824596742434968354935,
"kp":
? how to set correct value },
{
"r": 103919090362588117180151567326283890021253888086769542803093556743402383166255,
"s": 71275504518099351203494061824773539914502255758119765372192658739494934607323,
"kp":
? how to set correct value }
],
"message": [
29046398954207568557559337812845639802158825374855453790727264507720942269061,
61490995350470215933587146879107314396788082593003439971438111557980048214364,
53622079066303081310409199779963535117997475674801702670649253314258639177195,
2815976014827110489026899227085751067228569669674380545422790815579479978003
]
}
Using my sage code to how to write correct data.json format, please the anyone write correct json format, update my code please..
And the
red is that I know a bit how to set
Edit:
python lattice_attack.py -f data.json
----- Lattice ECDSA Attack -----
Loading data from file data.json
Traceback (most recent call last):
File "/workspaces/cbmbhpeedwfpy/lattice-attack/lattice_attack.py", line 259, in
lattice_attack_cli(arg.f, arg.l)
File "/workspaces/cbmbhpeedwfpy/lattice-attack/lattice_attack.py", line 222, in lattice_attack_cli
hash_int = ecdsa_lib.sha2_int(bytes(message))
ValueError: bytes must be in range(0, 256)
k = 29083908611578474659724835409188629928450877954572645032653586874541462703237
i know MSB red how to set kp: 29083908611578474659724835409188629 # this is correct or not
and "known_bits": ? # how to correct know bit
and "message": [
29046398954207568557559337812845639802158825374855453790727264507720942269061,
61490995350470215933587146879107314396788082593003439971438111557980048214364,
53622079066303081310409199779963535117997475674801702670649253314258639177195,
2815976014827110489026899227085751067228569669674380545422790815579479978003
]
message is correct format or not ??