Ecdsa

如何將兩個 secp256k1 密鑰加在一起?

  • December 5, 2017

我有兩個公鑰/私鑰對,A 和 B。我想將它們加在一起以獲得一個新的密鑰對 AB。

我還希望能夠添加 A 和 B 的公鑰以獲得 AB 的公鑰。

我該怎麼做呢?

這是一些你可以逆向工程的python程式碼

def add(p, q):
   if p[0] % P == 0 and p[1] % P == 0:
       return q
   if q[0] % P == 0 and q[1] % P == 0:
       return p

   if p[0] == q[0] and p[1] == q[1]:
       if p[1] == 0:
           return [0, 0]
       l = (3 * p[0]**2) * modInv((2 * p[1]), P)
   elif p[0] == q[0]:
       return [0, 0]
   else:
       l = (p[1] - q[1]) * modInv((p[0] - q[0]), P)

   x = l**2 - (p[0] + q[0])
   y = l * (p[0] - x) - p[1]
   return [x % P, y % P]


def modInv(n, p):
   return pow(n, p - 2, p)


#some constants
P = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
x = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8


#example usage of the add function
g1 = [x, y]
print "g1 = " + hex(g1[0]) + " : " + hex(g1[1])

g2 = add([x, y], [x, y])
print "g2 = " + hex(g2[0]) + " : " + hex(g2[1])

g3 = add([x, y], g2)
print "g3 = " + hex(g3[0]) + " : " + hex(g3[1])

g4 = add(g2, g2)
print "g4 = " + hex(g4[0]) + " : " + hex(g4[1])

引用自:https://bitcoin.stackexchange.com/questions/40799