|
@@ -1,28 +1,17 @@
|
|
|
import random, sympy
|
|
|
from point import Point
|
|
|
+from crypt import CipherAES
|
|
|
+from tools import searchPrime
|
|
|
|
|
|
-'''
|
|
|
-'secp256k1',
|
|
|
-p=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
|
|
|
-a=0,
|
|
|
-b=7,
|
|
|
-g=(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8),
|
|
|
-
|
|
|
-n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,
|
|
|
-
|
|
|
-h=1,
|
|
|
-'''
|
|
|
-
|
|
|
-
|
|
|
class Curve:
|
|
|
|
|
|
- def __init__(self, p, a, b, gx, gy, n, h):
|
|
|
+ def __init__(self, p, a, b, gx = None, gy = None, n = None, h = 1):
|
|
|
|
|
|
self.p = p
|
|
|
|
|
|
self.a = a
|
|
|
self.b = b
|
|
|
-
|
|
|
+
|
|
|
self.g = Point(
|
|
|
curve = self,
|
|
|
x = gx,
|
|
@@ -33,30 +22,58 @@ class Curve:
|
|
|
|
|
|
self.h = h
|
|
|
|
|
|
+ def clear(self):
|
|
|
+ self.g = self.n = None
|
|
|
+
|
|
|
def isSingular(self):
|
|
|
res = 4 * self.a ** 3 + 27 * self.b ** 2 == 0
|
|
|
return res
|
|
|
|
|
|
+
|
|
|
def setupN(self):
|
|
|
- for self.n in range(self.p - 1, 1, -1):
|
|
|
- if sympy.isprime(self.n):
|
|
|
+ dx, dy = self.g.x, (-1 * self.g.y) % self.p
|
|
|
+ tmpG = self.g.copy()
|
|
|
+ self.n = 1
|
|
|
+ while True:
|
|
|
+ self.n += 1
|
|
|
+ tmpG = tmpG + self.g
|
|
|
+ if tmpG.x == dx and tmpG.y == dy:
|
|
|
+ self.n += 1
|
|
|
return
|
|
|
|
|
|
- def setupG(self, processCallback = None):
|
|
|
- percent = 0
|
|
|
- for x in range(self.n, 0, -1):
|
|
|
- if processCallback is not None:
|
|
|
- dPercent = round(100 - x * 100 / self.n, 5)
|
|
|
- if dPercent != percent:
|
|
|
- percent = dPercent
|
|
|
- processCallback(percent)
|
|
|
- p1, p2 = self.pointsCurve(x)
|
|
|
- if p1.isInCurve():
|
|
|
- self.g = p1
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ def setupGAuto(self):
|
|
|
+ x = random.randrange(1, self.p - 1)
|
|
|
+ p1, p2 = self.searchClosePoints(x)
|
|
|
+ if p1.isInCurve():
|
|
|
+ self.g = p1
|
|
|
+ elif p2.isInCurve():
|
|
|
+ self.g = p2
|
|
|
+
|
|
|
+
|
|
|
+ def searchClosePoints(self, x):
|
|
|
+ if x >= self.p:
|
|
|
+ x = self.p - 1
|
|
|
+ if x < 0:
|
|
|
+ x = 0
|
|
|
+ kx = x
|
|
|
+ while True:
|
|
|
+ if x >= self.p and kx < 0:
|
|
|
+ return self.point()
|
|
|
+
|
|
|
+ if x <= self.p:
|
|
|
+ p1, p2 = self.pointsCurve(x)
|
|
|
+ if p1.isInCurve():
|
|
|
+ return p1, p2
|
|
|
+ x += 1
|
|
|
+
|
|
|
+ if kx <= 0:
|
|
|
+ p1, p2 = self.pointsCurve(kx)
|
|
|
+ if p1.isInCurve():
|
|
|
+ return p1, p2
|
|
|
+ kx -= 1
|
|
|
+ return self.point()
|
|
|
|
|
|
- return
|
|
|
|
|
|
def point(self, x = None, y = None):
|
|
|
return Point(
|
|
@@ -91,7 +108,7 @@ class Curve:
|
|
|
|
|
|
def inverseMod(self, k, point):
|
|
|
if k == 0:
|
|
|
- raise ZeroDivisionError('division by zero')
|
|
|
+ raise ZeroDivisionError('Деление на 0 невозможно!!!')
|
|
|
if k < 0:
|
|
|
|
|
|
return point - self.inverseMod(-k, point)
|
|
@@ -105,71 +122,127 @@ class Curve:
|
|
|
old_s, s = s, old_s - quotient * s
|
|
|
old_t, t = t, old_t - quotient * t
|
|
|
gcd, x, y = old_r, old_s, old_t
|
|
|
-
|
|
|
-
|
|
|
return x % point
|
|
|
|
|
|
def randomKeypair(self):
|
|
|
- keyPriv = random.randrange(1, self.n)
|
|
|
+ keyPriv = random.randrange(1, self.n - 1)
|
|
|
+ print(keyPriv)
|
|
|
+ keyPub = self.g * keyPriv
|
|
|
+ return keyPriv, keyPub
|
|
|
+
|
|
|
+ def keypair(self, keyPriv):
|
|
|
keyPub = self.g * keyPriv
|
|
|
return keyPriv, keyPub
|
|
|
|
|
|
+
|
|
|
def keyPub(self, keyPriv):
|
|
|
+
|
|
|
return self.g * keyPriv
|
|
|
|
|
|
+
|
|
|
+ def pointSecret(self, myKeyPrivate, friendKeyPublic):
|
|
|
+ return friendKeyPublic * myKeyPrivate
|
|
|
+
|
|
|
+
|
|
|
+ def encodeMessage(self, secretPoint, text):
|
|
|
+ cipher = CipherAES(self, secretPoint)
|
|
|
+ return cipher.encrypt(text)
|
|
|
+
|
|
|
+
|
|
|
+ def decodeMessage(self, secretPoint, encodedText):
|
|
|
+ cipher = CipherAES(self, secretPoint)
|
|
|
+ return cipher.decrypt(encodedText)
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-'''
|
|
|
-c = Curve(
|
|
|
- p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
|
|
|
- a = 0,
|
|
|
- b = 7,
|
|
|
- gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
|
|
|
- gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8,
|
|
|
- n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,
|
|
|
- h = 1
|
|
|
-)
|
|
|
-
|
|
|
-c = Curve(
|
|
|
- p = 6277101735386680763835789423207666416083908700390324961279,
|
|
|
- a = -3,
|
|
|
- b = 2455155546008943817740293915197451784769108058161191238065,
|
|
|
- gx = 602046282375688656758213480587526111916698976636884684818,
|
|
|
- gy = 174050332293622031404857552280219410364023488927386650641,
|
|
|
- n = 6277101735386680763835789423176059013767194773182842284081,
|
|
|
- h = 1
|
|
|
-)
|
|
|
-'''
|
|
|
-
|
|
|
-c = Curve(
|
|
|
- p = 2 ** 20,
|
|
|
- a = 5,
|
|
|
- b = 4,
|
|
|
- gx = 2,
|
|
|
- gy = 2,
|
|
|
- n = 54534432,
|
|
|
- h = 1
|
|
|
-)
|
|
|
-
|
|
|
-if c.isSingular():
|
|
|
- print('Кривая сингулярна')
|
|
|
- exit(0)
|
|
|
-
|
|
|
-def process(percent):
|
|
|
- print(percent)
|
|
|
-
|
|
|
-c.setupN()
|
|
|
-c.setupG(process)
|
|
|
-print(c.n)
|
|
|
-
|
|
|
-aPriv, aPub = c.randomKeypair()
|
|
|
-bPriv, bPub = c.randomKeypair()
|
|
|
-
|
|
|
-print(hex(aPriv))
|
|
|
-print(aPub.show())
|
|
|
-
|
|
|
-
|
|
|
-aS = bPub * aPriv
|
|
|
-bS = aPub * bPriv
|
|
|
-
|
|
|
-print(aS.show())
|
|
|
-print(bS.show())
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+
|
|
|
+
|
|
|
+ '''
|
|
|
+ cur = Curve(
|
|
|
+
|
|
|
+ p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f,
|
|
|
+
|
|
|
+ a = 0,
|
|
|
+ b = 7,
|
|
|
+
|
|
|
+ gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
|
|
|
+ gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8,
|
|
|
+
|
|
|
+ n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,
|
|
|
+
|
|
|
+ h = 1
|
|
|
+ )
|
|
|
+ '''
|
|
|
+ cur = Curve(
|
|
|
+ p = searchPrime(2 ** 18),
|
|
|
+ a = 1550,
|
|
|
+ b = 7840
|
|
|
+ )
|
|
|
+
|
|
|
+ print('setup g')
|
|
|
+ cur.setupGAuto()
|
|
|
+ print('g', cur.g.coords(), cur.g.isInCurve())
|
|
|
+ print('setup n')
|
|
|
+ cur.setupN()
|
|
|
+ print(cur.p, cur.n)
|
|
|
+
|
|
|
+ print(cur.n, cur.g.coords(), cur.g.isInCurve())
|
|
|
+
|
|
|
+ k1, p1 = cur.keypair(10)
|
|
|
+ print(k1, p1.coords())
|
|
|
+
|
|
|
+ k2, p2 = cur.keypair(110)
|
|
|
+ print(k2, p2.coords())
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p1KeyPriv, p1KeyPub = cur.randomKeypair()
|
|
|
+
|
|
|
+ p2KeyPriv, p2KeyPub = cur.randomKeypair()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p1KeySecret = cur.pointSecret(p1KeyPriv, p2KeyPub)
|
|
|
+ print(p1KeySecret.coords())
|
|
|
+
|
|
|
+
|
|
|
+ p2KeySecret = cur.pointSecret(p2KeyPriv, p1KeyPub)
|
|
|
+ print(p2KeySecret.coords())
|
|
|
+
|
|
|
+
|
|
|
+ print(p1KeySecret.isEqual(p2KeySecret))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ p1MessageEncrypted = cur.encodeMessage(p1KeySecret, 'Привет, как дела?')
|
|
|
+
|
|
|
+ print("п1: ", p1MessageEncrypted)
|
|
|
+
|
|
|
+
|
|
|
+ p1MessageDecrypted = cur.decodeMessage(p2KeySecret, p1MessageEncrypted)
|
|
|
+
|
|
|
+ print("п1: ", p1MessageDecrypted)
|
|
|
+
|
|
|
+
|
|
|
+ p2MessageEncrypted = cur.encodeMessage(p2KeySecret, 'Спасибо, у меня всё хорошо :)')
|
|
|
+
|
|
|
+ print("п2: ", p2MessageEncrypted)
|
|
|
+
|
|
|
+
|
|
|
+ p2MessageDecrypted = cur.decodeMessage(p1KeySecret, p2MessageEncrypted)
|
|
|
+
|
|
|
+ print("п2: ", p2MessageDecrypted)
|