In ECC, if we two private key of \(n\) and \(m\), the public keys will be \(nG\) and \(mG\). If we add these two points, we get \(nG + mG\), and which is the same as \((n+m)G\). The private key will be \(n+m\). In this case we will create private keys for Bob and Alice (\(n\) and \(m\)), and then add their public keys (\(nG+mG\)), and check to see if \(nG+mG\) is the same as \((n+m)G\):
Elliptic Curve Cryptography (ECC) Point Addition |
Theory
In this example we use secp256k1 (as used in Bitcoin) to generate points on the curve. Its format is:
\(y^2 = x^3+7\)
with a prime number (\(p\)) of 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F, and is \( 2^{256} - 2^{32} - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1\). All our operations will be \(\pmod p\)>
Bob will generate a public key and a private key by taking a point on the curve. The private key is a random number (\(n\)) and the Bob's public key (\(Q_B\)) will be:
\(Q_B = n \times G\)
Alice will generate a public key and a private key by taking a point on the curve. The private key is a random number (\(m\)) and the Alice's public key (\(Q_A\)) will be:
\(Q_A = m \times G\)
If we add the points for the public key, we will get:
\(nG + mG\)
and which is equal to:
\((n+m)G\)
and which will have a private key of \(n+m\).
A sample run shows that we start with a point (\(G\)) and then Alice and Bob generate their private key. From this they generate their public keys:
Basepoint G: (55066263022277343669578718895168534326250603453777594175500187360389116729240L, 32670510020758816978083085130507043184471273380659243275938904335757337482424L) Alice's secret key: 43117620402230955476965528669042438660394005541196203406020707489909525604940 Alice's public key: (4019643073730566083617650647291067328215698160799048102792284657940069948802L, 70465223976866971083435294728445479148035854275287211179449856163787341254166L) Bob's secret key: 113946821188667604779240725709383705264602529738288128886334003594195673575512 Bob's public key: (100435347059380861534234278977306370684954979158715388142830524947248770417046L, 94160905864433229108459971809356137950237067657681396576702522474464196460484L) =========Now we compare if same ================= (Alice Private+Bob Private)*G (76993010474774159598056594977435333329474333327636586317573761552043530263605L, 75175255398933451677789825153696420983321174467684797211715435808493620129877L) Alice Public+Bob Public (76993010474774159598056594977435333329474333327636586317573761552043530263605L, 75175255398933451677789825153696420983321174467684797211715435808493620129877L)
Sample coding
The following is some sample code:
import collections import hashlib import random import binascii 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 # Keypair generation and ECDSA ################################################ def make_keypair(): """Generates a random private-public key pair.""" private_key = random.randrange(1, curve.n) public_key = scalar_mult(private_key, curve.g) return private_key, public_key print "Basepoint G:\t",curve.g aliceSecretKey, alicePublicKey = make_keypair() bobSecretKey, bobPublicKey = make_keypair() print "\nAlice\'s secret key:\t", aliceSecretKey print "Alice\'s public key:\t", alicePublicKey print "Bob\'s secret key:\t", bobSecretKey print "Bob\'s public key:\t", bobPublicKey print "\n=========Now we compare if same =================" sharedSecret = scalar_mult(bobSecretKey+aliceSecretKey,curve.g) sharedPublic = point_add(alicePublicKey,bobPublicKey) print "(Alice Private+Bob Private)*G",sharedSecret print "Alice Public+Bob Public",sharedPublic print "\nNow let's compare..." if (sharedSecret[0]==sharedPublic[0]): print "Success!" else: print "Failure!"
Presentation
The following gives an outline presentation on MimbleWimble [slides]: