Sorularını cevaplamak için aynı örnek üzerinden başka bir aritmetik kütüphanesini ekledim. pythonda gmp2 kullanıyor.
İlk mesajımdaki aritmetik kodu için biraz uğraşırım ama onuda bu örnek üzerinde yazarım. Gelelim sorularını
Gx ve Gy ile x doğrandan alakalı değil
x bir değişken Gx,Gy ise sabit
Gx ve Gy ile doğrudan ilişkili sayı 1
1*G = G
dir. senin bahsettiğin " G H ve N " için referans verir misin ? anlamadım.
Örnek Kod :
IN
"""
Created on Wed Dec 23 09:43:04 2020
"""
import gmpy2
modulo = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 -1
order = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
Z = Point(0,0) # zero-point, infinite in real x,y - plane
# return (g, x, y) a*x + b*y = gcd(x, y)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def modinv(m, n = modulo):
while m < 0:
m += n
g, x, _ = egcd(m, n)
if g == 1:
return x % n
else: print (' no inverse exist')
def mul2(Pmul2, p = modulo):
R = Point(0,0)
#c = 3*Pmul2.x*Pmul2.x*modinv(2*Pmul2.y, p) % p
c = 3*Pmul2.x*Pmul2.x*gmpy2.invert(2*Pmul2.y, p) % p
R.x = (c*c-2*Pmul2.x) % p
R.y = (c*(Pmul2.x - R.x)-Pmul2.y) % p
return R
def add(Padd, Q, p = modulo):
if Padd.x == Padd.y == 0: return Q
if Q.x == Q.y == 0: return Padd
if Padd == Q: return mul2(Q)
R = Point()
dx = (Q.x - Padd.x) % p
dy = (Q.y - Padd.y) % p
c = dy * gmpy2.invert(dx, p) % p
#c = dy * modinv(dx, p) % p
R.x = (c*c - Padd.x - Q.x) % p
R.y = (c*(Padd.x - R.x) - Padd.y) % p
return R # 6 sub, 3 mul, 1 inv
def mulk(k, Pmulk, p = modulo):
if k == 0: return Z
if k == 1: return Pmulk
if (k % 2 == 0): return mulk(k//2, mul2(Pmulk, p), p)
return add(Pmulk, mulk((k-1)//2, mul2(Pmulk, p), p), p)
x = 8723493475893459873498759834758934759837458973497593847598347598734985798982374987234 % order
G = Point(Gx,Gy)
GeneratorPoint = G
PrivateKey = x
print ('PrivateKey -> ' + str(PrivateKey))
print ('\n')
#PublicPoint = PrivateKey * GeneratorPoint
PublicPoint = mulk (PrivateKey , GeneratorPoint, order )
print ('PublicPoint.x -> ' + str(PublicPoint.x))
print ('PublicPoint.y -> ' + str(PublicPoint.y))
print ('\n')
xx = hex(PublicPoint.x)
xy = hex(PublicPoint.y)
print ('hex(PrivateKey.x) -> ' + str(hex(PublicPoint.x)))
print ('hex(PrivateKey.y) -> ' + str(hex(PublicPoint.y)))
print ('\n')
xx = xx.lstrip('0x')
xy = xy.lstrip('0x')
print ("xx.lstrip('0x') -> " + xx)
print ("xy.lstrip('0x') -> " + xy)
print ('\n')
UnComporessedPublicKey = str('04') + xx + xy
print ("UnComporessedPublicKey -> " + UnComporessedPublicKey)
print ('\n')
if PublicPoint.y %2 == 0 :
ComporessedPublicKey = str('02') + xx
if PublicPoint.y %2 == 1 :
ComporessedPublicKey = str('03') + xx
print ('ComporessedPublicKey-> ' + ComporessedPublicKey)
print ('\n')
print ('Publicpoint ->',PublicPoint)
OUT:
PublicPoint.x -> 88592974801524563600300001436396082991077345600545970471017048342658174170961
PublicPoint.y -> 107521544534117035401926583528158785822472831725006965096095309778371330834318
hex(PrivateKey.x) -> 0xc3ddd8c66a446ac611140bdf0681877fc07849a5ffacff64d9bc9d3cdffc3751
hex(PrivateKey.y) -> 0xedb7099f2c70008aee7c51984c185b4997e033c55e9893d5d4c73832c561ff8e
xx.lstrip('0x') -> c3ddd8c66a446ac611140bdf0681877fc07849a5ffacff64d9bc9d3cdffc3751
xy.lstrip('0x') -> edb7099f2c70008aee7c51984c185b4997e033c55e9893d5d4c73832c561ff8e
UnComporessedPublicKey -> 04c3ddd8c66a446ac611140bdf0681877fc07849a5ffacff64d9bc9d3cdffc3751edb7099f2c70008aee7c51984c185b4997e033c55e9893d5d4c73832c561ff8e
ComporessedPublicKey-> 02c3ddd8c66a446ac611140bdf0681877fc07849a5ffacff64d9bc9d3cdffc3751
Publicpoint -> <__main__.Point object at 0x0000016B1D4E1610>