yes,it is sage.
it is just part of my app for finding range of privatekey.
import hashlib
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
E = EllipticCurve(GF(p), [0, 7])
G = E.point( (0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)) # Base point
x= 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
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 verify(r, s,z,public_key):
w = int(modinv(s, n))
u1 = int((z * w) % n)
u2 = int((r * w) % n)
D=u1*G + u2*public_key
x,y=D.xy()
x=int(x)
if (r % n) == (x % n):
print( "signature matches")
return 1
else:
print("invalid signature",r,x%n,hex(int(x%n)))
return -1
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)
def calc_u(r,s,z):
mod_s= modinv(s,n)%n
u1=mod_s*z%n
u2=mod_s*r%n
return u1,u2
import random
p1=100#random.randrange(1,119)
pb1=p1*G
msg=2#random.randrange(1,n)
no=10000000000000#random.randrange(1,n)
print("p1",p1)
print("nonce",no)
r,s,z=sign(p1,no,msg)
assert z==msg
print("r=",r)
print("s=",s)
print("z=",z)
x,y=pb1.xy()
print("pb1x=",x)
print("pb1y=",y)
print("pb1= E.point((pb1x,pb1y)")
u1,u2=calc_u(r,s,z)
print("u1",u1)
print("u2",u2)
kat={}
def make_rsz(a,b,pub):
R=a*G + b*pub
r=int(R.xy()[0])
mod_s=int( b*modinv(r,n)%n)
z=int( a*modinv(mod_s,n)%n)
s=int( modinv(mod_s,n)%n)
return r,s,z
a,b=calc_u(r,s,z)
kat={}
diff=0
for i in range(1,2):
res=0
for i in range(1 ,700):
pub=i*G
r2,s2,z2=make_rsz(a,b,pub)
k=(r2*i+z2)*modinv(s2,n)%n
print("private",i,"K :",k)
d=(s2*k-z2)*modinv(r2,n)%n
assert d==i
if diff==0:
diff=k
else:
print("diff",(diff-k)%n)
print()
diff=k
output :
p1 100
nonce 10000000000000
r= 115045167963494515061513744671884131783397561769819471159495798754884242293003
s= 63219244353825002060721550925150673377693765484618230755991360113610911083035
z= 2
pb1x= 107303582290733097924842193972465022053148211775194373671539518313500194639752
pb1y= 103795966108782717446806684023742168462365449272639790795591544606836007446638
pb1= E.point((pb1x,pb1y)
u1 55884778358236413705386203537939314657187400009875545072465900600581599500329
u2 19125807386761389084953205416097551188410511927343978294318218728152271459034
private 1 K : 75010585744997802790339408954036865845597911937219523366784119328733870959363
private 2 K : 94136393131759191875292614370134417034008423864563501661102338056886142418397
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 3 K : 113262200518520580960245819786231968222418935791907479955420556785038413877431
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 4 K : 16595918667965774621628040193641611557991883440176553867133612371672523842128
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 5 K : 35721726054727163706581245609739162746402395367520532161451831099824795301162
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 6 K : 54847533441488552791534451025836713934812907294864510455770049827977066760196
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 7 K : 73973340828249941876487656441934265123223419222208488750088268556129338219230
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 8 K : 93099148215011330961440861858031816311633931149552467044406487284281609678264
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 9 K : 112224955601772720046394067274129367500044443076896445338724706012433881137298
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 10 K : 15558673751217913707776287681539010835617390725165519250437761599067991101995
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 11 K : 34684481137979302792729493097636562024027902652509497544755980327220262561029
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 12 K : 53810288524740691877682698513734113212438414579853475839074199055372534020063
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 13 K : 72936095911502080962635903929831664400848926507197454133392417783524805479097
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 14 K : 92061903298263470047589109345929215589259438434541432427710636511677076938131
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 15 K : 111187710685024859132542314762026766777669950361885410722028855239829348397165
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 16 K : 14521428834470052793924535169436410113242898010154484633741910826463458361862
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 17 K : 33647236221231441878877740585533961301653409937498462928060129554615729820896
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 18 K : 52773043607992830963830946001631512490063921864842441222378348282768001279930
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 19 K : 71898850994754220048784151417729063678474433792186419516696567010920272738964
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 20 K : 91024658381515609133737356833826614866884945719530397811014785739072544197998
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 21 K : 110150465768276998218690562249924166055295457646874376105333004467224815657032
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 22 K : 13484183917722191880072782657333809390868405295143450017046060053858925621729
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 23 K : 32609991304483580965025988073431360579278917222487428311364278782011197080763
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 24 K : 51735798691244970049979193489528911767689429149831406605682497510163468539797
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 25 K : 70861606078006359134932398905626462956099941077175384900000716238315739998831
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 26 K : 89987413464767748219885604321724014144510453004519363194318934966468011457865
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 27 K : 109113220851529137304838809737821565332920964931863341488637153694620282916899
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 28 K : 12446939000974330966221030145231208668493912580132415400350209281254392881596
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 29 K : 31572746387735720051174235561328759856904424507476393694668428009406664340630
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 30 K : 50698553774497109136127440977426311045314936434820371988986646737558935799664
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 31 K : 69824361161258498221080646393523862233725448362164350283304865465711207258698
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 32 K : 88950168548019887306033851809621413422135960289508328577623084193863478717732
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 33 K : 108075975934781276390987057225718964610546472216852306871941302922015750176766
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 34 K : 11409694084226470052369277633128607946119419865121380783654358508649860141463
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 35 K : 30535501470987859137322483049226159134529931792465359077972577236802131600497
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 36 K : 49661308857749248222275688465323710322940443719809337372290795964954403059531
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 37 K : 68787116244510637307228893881421261511350955647153315666609014693106674518565
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 38 K : 87912923631272026392182099297518812699761467574497293960927233421258945977599
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 39 K : 107038731018033415477135304713616363888171979501841272255245452149411217436633
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 40 K : 10372449167478609138517525121026007223744927150110346166958507736045327401330
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 41 K : 29498256554239998223470730537123558412155439077454324461276726464197598860364
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 42 K : 48624063941001387308423935953221109600565951004798302755594945192349870319398
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 43 K : 67749871327762776393377141369318660788976462932142281049913163920502141778432
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 44 K : 86875678714524165478330346785416211977386974859486259344231382648654413237466
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 45 K : 106001486101285554563283552201513763165797486786830237638549601376806684696500
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 46 K : 9335204250730748224665772608923406501370434435099311550262656963440794661197
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 47 K : 28461011637492137309618978025020957689780946362443289844580875691593066120231
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 48 K : 47586819024253526394572183441118508878191458289787268138899094419745337579265
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 49 K : 66712626411014915479525388857216060066601970217131246433217313147897609038299
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 50 K : 85838433797776304564478594273313611255012482144475224727535531876049880497333
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 51 K : 104964241184537693649431799689411162443422994071819203021853750604202151956367
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 52 K : 8297959333982887310814020096820805778995941720088276933566806190836261921064
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 53 K : 27423766720744276395767225512918356967406453647432255227885024918988533380098
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 54 K : 46549574107505665480720430929015908155816965574776233522203243647140804839132
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 55 K : 65675381494267054565673636345113459344227477502120211816521462375293076298166
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 56 K : 84801188881028443650626841761211010532637989429464190110839681103445347757200
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 57 K : 103926996267789832735580047177308561721048501356808168405157899831597619216234
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 58 K : 7260714417235026396962267584718205056621449005077242316870955418231729180931
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 59 K : 26386521803996415481915473000815756245031960932421220611189174146384000639965
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 60 K : 45512329190757804566868678416913307433442472859765198905507392874536272098999
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 61 K : 64638136577519193651821883833010858621852984787109177199825611602688543558033
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 62 K : 83763943964280582736775089249108409810263496714453155494143830330840815017067
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 63 K : 102889751351041971821728294665205960998674008641797133788462049058993086476101
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 64 K : 6223469500487165483110515072615604334246956290066207700175104645627196440798
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 65 K : 25349276887248554568063720488713155522657468217410185994493323373779467899832
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 66 K : 44475084274009943653016925904810706711067980144754164288811542101931739358866
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 67 K : 63600891660771332737970131320908257899478492072098142583129760830084010817900
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 68 K : 82726699047532721822923336737005809087889003999442120877447979558236282276934
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 69 K : 101852506434294110907876542153103360276299515926786099171766198286388553735968
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 70 K : 5186224583739304569258762560513003611872463575055173083479253873022663700665
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 71 K : 24312031970500693654211967976610554800282975502399151377797472601174935159699
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 72 K : 43437839357262082739165173392708105988693487429743129672115691329327206618733
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 73 K : 62563646744023471824118378808805657177103999357087107966433910057479478077767
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 74 K : 81689454130784860909071584224903208365514511284431086260752128785631749536801
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 75 K : 100815261517546249994024789641000759553925023211775064555070347513784020995835
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 76 K : 4148979666991443655407010048410402889497970860044138466783403100418130960532
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 77 K : 23274787053752832740360215464507954077908482787388116761101621828570402419566
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 78 K : 42400594440514221825313420880605505266318994714732095055419840556722673878600
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 79 K : 61526401827275610910266626296703056454729506642076073349738059284874945337634
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 80 K : 80652209214036999995219831712800607643140018569420051644056278013027216796668
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 81 K : 99778016600798389080173037128898158831550530496764029938374496741179488255702
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 82 K : 3111734750243582741555257536307802167123478145033103850087552327813598220399
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 83 K : 22237542137004971826508462952405353355533990072377082144405771055965869679433
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 84 K : 41363349523766360911461668368502904543944501999721060438723989784118141138467
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 85 K : 60489156910527749996414873784600455732355013927065038733042208512270412597501
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 86 K : 79614964297289139081368079200698006920765525854409017027360427240422684056535
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 87 K : 98740771684050528166321284616795558109176037781752995321678645968574955515569
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 88 K : 2074489833495721827703505024205201444748985430022069233391701555209065480266
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 89 K : 21200297220257110912656710440302752633159497357366047527709920283361336939300
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 90 K : 40326104607018499997609915856400303821570009284710025822028139011513608398334
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 91 K : 59451911993779889082563121272497855009980521212054004116346357739665879857368
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 92 K : 78577719380541278167516326688595406198391033139397982410664576467818151316402
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 93 K : 97703526767302667252469532104692957386801545066741960704982795195970422775436
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303
private 94 K : 1037244916747860913851752512102600722374492715011034616695850782604532740133
diff 96666281850554806338617779592590356664427052351730926088286944413365890035303