def secp256k1(): """ create the secp256k1 curve """ GFp= FiniteField(2**256 - 2**32 - 977) ec= EllipticCurve(GFp, 0, 7) return ECDSA(ec, ec.point( 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 ), 2**256 - 432420386565659656852420866394968145599)
def main(): curve = EllipticCurve(EC_A, EC_B, EC_P) base_point = curve.create_point(EC_POINT_COORD) diffie = DiffieHellman(base_point) private_key = random.randint(1, 1000) public_key = diffie.get_public_key(private_key) q = random.randint(1, 100) to_encrypt = base_point * 2 encrypted_points = diffie.encrypt(to_encrypt, public_key, q) decrypted = diffie.decrypt(encrypted_points, private_key) print('Base eliptic curve point: ', to_encrypt) print('Encrypted: ', encrypted_points[0], encrypted_points[1]) print('Decryped: ', decrypted) if decrypted == to_encrypt: print('Decrypted successfully') else: print('Something went wrong')
from ec import EllipticCurve e = EllipticCurve(-11, 11, 593899) print e(5, 9) - e(1, 1)
from ec import EllipticCurve e = EllipticCurve(2,3,19) print e print '(a)', e(1,5) + e(9,3) print '(b)', e(9,3) + e(9,3) print '(c)', e(1,5) - e(9,3) for i in range(100): p = i*e(1,5) if p == e(9,3): print '(d)', i break print '(e)' for i in range(1,100): p = i*e(1,5) print '\t%dP ='%i, p if p == e('inf'): break
from ec import EllipticCurve e = EllipticCurve(-10, 21, 557) p = e(2, 3) print '189P =', 189 * p print ' 63P =', 63 * p print ' 27P =', 27 * p
# print("gamma:", gamma_) # print("m:", m_) # print("f(x) =", ((x - gamma_)**2 + gamma_ + m_).expand()) # print("f^3(x) = p1(x)*p2(x)") # p1, p2 = factor(gamma_, m_, d_) # print("p1(x) =", p1) # print("p2(x) =", p2) # print() # P = P - P0 m_ = -Rational(7, 4) cs = [16 * m_**2 + 16 * m_, 0, 8 * m_, 0, 2] alpha_ = 1 coeffs, xtrans, ytrans = transform(*cs, alpha_) gx, gy = Rational(53, 81), Rational(289, 81) C = EllipticCurve(*coeffs) P0 = Point(0, 0, 1, C) P = P0 while True: d_ = xtrans.subs(x, P.x).subs(y, P.y) for gamma_ in gammas_at(m_, d_): if check_newly(gamma_, m_): print("gamma:", gamma_) print("m:", m_) print("f(x) =", ((x - gamma_)**2 + gamma_ + m_).expand()) print("f^3(x) = p1(x)*p2(x)") p1, p2 = factor(gamma_, m_, d_) print("p1(x) =", p1) print("p2(x) =", p2) print() P = P - P0
def aCurve(x, y, b, n): c = (pow(y, 2, n) - pow(x, 3, n) - b * x) % n return EllipticCurve(b, c, n)
plt.contour(x.ravel(), y.ravel(), pow(y, 2) - pow(x, 3) - x * ec.a - ec.b, [0]) plt.grid() def draw_point(P): plt.plot([P.x], [P.y], marker='o', color='r') def draw_sum(P, Q): R = P + Q plt.plot([P.x, Q.x], [P.y, Q.y], marker='o', color='r') if isinstance(R, O): pass else: plt.plot([R.x], [R.y], marker='o', color='r') if __name__ == '__main__': ec = EllipticCurve(a=frac(-2), b=frac(4)) p = Point(ec, frac(3), frac(5)) q = Point(ec, frac(-2), frac(0)) print p + q print q + p print q + q print 10 * p draw(ec) draw_sum(p, q) draw_sum(q, q) plt.show()
from ec import EllipticCurve p = 593899 e = EllipticCurve(7, 11, p) for i in range(10): x = 123450 + i r = (x**3 + 7 * x + 11) % p y = pow(r, (p + 1) / 4, p) if y**2 % p == r: print e(x, y)