The secp256k1 curve uses a form of \(y^2=x^3+ax+b\) and specifically as \(y^2 = x^3+7\) and a finite field of \(p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f\). The base point (\(G\) is at and the base point is at (0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) [secp256k1 barebones][P256 barebones][P521 barebones][Curve 25519 barebones]:
Barebones secp256k1: Adding and Scalar Multiply on the curve (Python)TheoryThe secp256k1 curve uses a form of \(y^2=x^3+ax+b\) and specifically as: \(y^2 = x^3+7\) and a finite field of: \(p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f\) The simplest operations we have is to take a base point (\(G\)) and then perform point addition and where \(2G\) is equal to \(G+G\) and where we get a new point on the elliptic curve. For \(3G\) we can have \(G+2G\), and so on. We can also perform a scalar multiplication, such as taking a scalar of \(3\) and finding \(3G\). In the following code we have three scalar values of 1, 2 and 3, and then use point addition and scalar multiplication to find \(2G\) and \(3G\), and where we should get the same values as \(G+G\) and \(G+2G\), respectively. Note, in secp256k1, we the point on the curve is defined with a \((x,y)\) value. import random from secp256k1 import scalar_mult,point_add,curve import sys val=1 if (len(sys.argv)>1): val=int(sys.argv[1]) def toHex(val,Gv): (x,y) = Gv x=hex(x) y=hex(y) print (f"({val}G) in hex: {x},{y})\n") print ("secp256k1") n=1 G = scalar_mult(n,curve.g) print (f"Point {n}G: {G}") toHex(n,G) n=2 G2 = scalar_mult(n,curve.g) print (f"Point {n}G: {G2}") toHex(n,G2) n=3 G3 = scalar_mult(n,curve.g) print (f"Point {n}G: {G3}") toHex(n,G3) G_G = point_add(G,G) print (f"G+G: {G_G}") G_2G = point_add(G,G2) print (f"\nG+2G: {G_2G}") Gv = scalar_mult(val,curve.g) print (f"\nPoint {val}G: {Gv}") toHex(val,Gv) val=random.randint(0,curve.n) Gv = scalar_mult(val,curve.g) print (f"\nPoint {val}G: {Gv}") toHex(val,Gv) And secp256k1 code: import collections import random import libnum EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h') curve = EllipticCurve( 'secp256k1', # Field characteristic. p=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f, # Curve coefficients. a=0, b=7, # Base point. g=(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798, 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8), # Subgroup order. n=0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141, # Subgroup cofactor. h=1, ) # Modular arithmetic ########################################################## def inverse_mod(k, p): """Returns the inverse of k modulo p. This function returns the only integer x such that (x * k) % p == 1. k must be non-zero and p must be a prime. """ if k == 0: raise ZeroDivisionError('division by zero') if k < 0: # k ** -1 = p - (-k) ** -1 (mod p) return p - inverse_mod(-k, p) # Extended Euclidean algorithm. s, old_s = 0, 1 t, old_t = 1, 0 r, old_r = p, k while r != 0: quotient = old_r // r old_r, r = r, old_r - quotient * r 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 assert gcd == 1 assert (k * x) % p == 1 return x % p # Functions that work on curve points ######################################### def is_on_curve(point): """Returns True if the given point lies on the elliptic curve.""" if point is None: # None represents the point at infinity. return True x, y = point return (y * y - x * x * x - curve.a * x - curve.b) % curve.p == 0 def point_add(point1, point2): """Returns the result of point1 + point2 according to the group law.""" assert is_on_curve(point1) assert is_on_curve(point2) if point1 is None: # 0 + point2 = point2 return point2 if point2 is None: # point1 + 0 = point1 return point1 x1, y1 = point1 x2, y2 = point2 if x1 == x2 and y1 != y2: # point1 + (-point1) = 0 return None if x1 == x2: # This is the case point1 == point2. m = (3 * x1 * x1 + curve.a) * inverse_mod(2 * y1, curve.p) else: # This is the case point1 != point2. m = (y1 - y2) * inverse_mod(x1 - x2, curve.p) x3 = m * m - x1 - x2 y3 = y1 + m * (x3 - x1) result = (x3 % curve.p, -y3 % curve.p) assert is_on_curve(result) return result def scalar_mult(k, point): """Returns k * point computed using the double and point_add algorithm.""" assert is_on_curve(point) if k % curve.n == 0 or point is None: return None if k < 0: # k * point = -k * (-point) return scalar_mult(-k, point_neg(point)) result = None addend = point while k: if k & 1: # Add. result = point_add(result, addend) # Double. addend = point_add(addend, addend) k >>= 1 assert is_on_curve(result) return result def point_neg(point): """Returns -point.""" assert is_on_curve(point) if point is None: # -0 = 0 return None x, y = point result = (x, -y % curve.p) assert is_on_curve(result) return result A sample run shows that \(2G\) is equal to \(G+G\), and \(3G\) is equal to \(G+2G\), and that \(2G-G=G\) [Test vectors]: secp256k1 secp256k1 Point 1G: (55066263022277343669578718895168534326250603453777594175500187360389116729240, 32670510020758816978083085130507043184471273380659243275938904335757337482424) (1G) in hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) Point 2G: (89565891926547004231252920425935692360644145829622209833684329913297188986597, 12158399299693830322967808612713398636155367887041628176798871954788371653930) (2G) in hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) Point 3G: (112711660439710606056748659173929673102114977341539408544630613555209775888121, 25583027980570883691656905877401976406448868254816295069919888960541586679410) (3G) in hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) G+G: (89565891926547004231252920425935692360644145829622209833684329913297188986597, 12158399299693830322967808612713398636155367887041628176798871954788371653930) G+2G: (112711660439710606056748659173929673102114977341539408544630613555209775888121, 25583027980570883691656905877401976406448868254816295069919888960541586679410) Point 2G: (89565891926547004231252920425935692360644145829622209833684329913297188986597, 12158399299693830322967808612713398636155367887041628176798871954788371653930) (2G) in hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) Point 111761348085794215738137643467639293264493985175375822836116393815933252793503G: (2651890663486341949221862289217270868543753037803011882830028333273483831377, 82036165339864123006014354080577373696204415167555087779548345469730701542574) (111761348085794215738137643467639293264493985175375822836116393815933252793503G) in hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8) |