Skip to content

Commit

Permalink
[ADD] Inverse Key method
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Oct 31, 2023
1 parent 20594e8 commit e37fa6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions bitcoinlib/config/secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

# Parameters secp256k1
# from http://www.secg.org/sec2-v2.pdf, par 2.4.1
secp256k1_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
secp256k1_n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
secp256k1_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F # field size
secp256k1_n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 # order
secp256k1_b = 7
secp256k1_a = 0
secp256k1_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
secp256k1_Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
secp256k1_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 # generator point x
secp256k1_Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8 # generator point y

# secp256k1_curve = ecdsa.ellipticcurve.CurveFp(secp256k1_p, secp256k1_a, secp256k1_b)
# secp256k1_generator = ecdsa.ellipticcurve.Point(secp256k1_curve, secp256k1_Gx, secp256k1_Gy, secp256k1_n)
4 changes: 4 additions & 0 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ def __mul__(self, other):
def __rmul__(self, other):
return self * other

def __neg__(self):
assert self.secret
return HDKey(secp256k1_n - self.secret)

def __len__(self):
return len(self.public_byte)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ def test_keys_proof_distributivity_of_scalar_operations(self):
kc = HDKey()
self.assertTrue(((ka - kb) * kc) == ((ka * kc) - (kb * kc)))

def test_keys_inverse(self):
secret = 95695802915573022935630358993164660366922511389187789518108651759801046161623
inv_x = 18153291153288219155018628681705413538294494009875615719062204619491226452658
inv_y = 67935514921393906349711087930011707333238709725906400058836382320969451605430
k = Key(secret)
k_inv = -k
self.assertEqual(k_inv.x, inv_x)
self.assertEqual(k_inv.y, inv_y)

def test_dict_and_json_outputs(self):
k = HDKey()
k.address(script_type='p2wsh', encoding='bech32')
Expand Down

0 comments on commit e37fa6f

Please sign in to comment.