Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from coincurve import PrivateKey, PublicKey
- import hashlib
- from Crypto.Util.number import inverse
- #secpk256k1 group order
- _n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
- hpk = hashlib.sha256("CoinbaesRulez".encode()).digest()
- h = PrivateKey(hpk).public_key
- g = PrivateKey(b"\x01").public_key
- #verification the given data values are correct
- pubin = PublicKey.combine_keys([PublicKey.multiply(g, (1337).to_bytes(32, "big")), PublicKey.multiply(h, (9876).to_bytes(32, "big"))])
- print("in value correct :", pubin.format(compressed = True).hex() == "025b0d4d0ad70054b53c16d6b3269b03e7b8582aa091317cab4d755508062c6f43")
- pubout = PublicKey.combine_keys([PublicKey.multiply(g, (_n-2674).to_bytes(32, "big")), PublicKey.multiply(h, (3456).to_bytes(32, "big"))])
- print("out value correct :", pubout.format(compressed = True).hex() == "035e3bdfa735f413f2213aa61ae4f7622596feddb96ecc0f263892cb35ca460182")
- y = PublicKey.combine_keys([PublicKey.multiply(g, (4011).to_bytes(32, "big")), PublicKey.multiply(h, (6420).to_bytes(32, "big"))])
- print("y value correct :", y.format(compressed = True).hex() == "020ab37bbcc43b8e96714aae06fdc1bbfc386d0165afb69500c9df1553e6c94ed1")
- #computation of x with h private key
- x = (4011 * inverse(int.from_bytes(hpk,"big"), _n) + 6420) % _n
- #verification x is correct
- y2 = PublicKey.multiply(h, x.to_bytes(32, "big"))
- print("x value correct :", y2 == y)
- #signature computation
- r = 0x19
- t = PublicKey.multiply(h, r.to_bytes(32, "big"))
- c = hashlib.sha256(h.format(compressed = True)+t.format(compressed = True)+y.format(compressed = True)).digest()
- s = (r + int.from_bytes(c, 'big') * x) % _n
- print("Signature:")
- print("(0x" + t.format(compressed = True).hex()[1:] + ", " + hex(s) + ")")
- #signature verification:
- c = hashlib.sha256(h.format(compressed = True)+t.format(compressed = True)+y.format(compressed = True)).digest()
- hs = PublicKey.multiply(h, s.to_bytes(32, "big"))
- tyc = PublicKey.combine_keys([t,PublicKey.multiply(y, c)])
- print("Signature verifies:",hs == tyc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement