def test_generate(self): from ecdsa import SigningKey, NIST521p sk = SigningKey.generate(curve=NIST521p) pri = sk.privkey pub = pri.public_key param = dict( crv=sk.curve, x=pub.point.x(), y=pub.point.y(), d=pri.secret_multiplier) # Curve from ecdsa.ellipticcurve import Point, CurveFp from ecdsa.ecdsa import curve_521 self.assertTrue(isinstance(curve_521, CurveFp)) self.assertTrue(isinstance(param['crv'].curve, CurveFp)) self.assertEqual(curve_521, param['crv'].curve) self.assertEqual(pub.point.curve(), curve_521) # Point p_new = Point(curve_521, param['x'], param['y']) self.assertEqual(p_new, pub.point) self.assertTrue(isinstance(pub.point, Point)) # Public Key from ecdsa.ecdsa import Public_key, generator_521 self.assertEqual(generator_521, pub.generator) pub_new = Public_key(generator_521, p_new) # Private Key from ecdsa.ecdsa import Private_key pri_new = Private_key(pub_new, param['d']) # Signature from ecdsa.ecdsa import string_to_int, Signature from hashlib import sha512 from uuid import uuid1 rnd = uuid1().int msg = "hello, it's me.".encode('utf8') digest = string_to_int(sha512(msg).digest()) signature_new = pri_new.sign(digest, rnd) signature_old = pri.sign(digest, rnd) self.assertTrue(isinstance(signature_new, Signature)) self.assertEqual(signature_new.r, signature_old.r) self.assertEqual(signature_new.s, signature_old.s) import six # python3 no long self.assertTrue(type(signature_new.r) in six.integer_types) self.assertTrue(type(signature_new.s) in six.integer_types) # Verify print(pub.verifies(digest, signature_new)) print(pub_new.verifies(digest, signature_old)) # print(dir(pri_new)) print(dir(pub_new)) print(dir(pub_new.curve))
def verify(self, pubkey_buffer, hash_buffer): """ Verify a digital signature. :param pubkey_buffer: Public key as bytes :param hash_buffer: hash of the message :return: True if this signature is valid """ assert isinstance(hash_buffer, bytes) assert isinstance(pubkey_buffer, bytes) hash_int = _hash_to_int(hash_buffer) point = _pubkey_point_from_bytes(pubkey_buffer) pk = Public_key(g, point) obj = Signature(self.r, self.s) return pk.verifies(hash_int, obj)
def generate_pub_address_from_secret(secret): # secp256k1, not included in stock ecdsa _p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL _r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L _b = 0x0000000000000000000000000000000000000000000000000000000000000007L _a = 0x0000000000000000000000000000000000000000000000000000000000000000L _Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L _Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L curve_256 = CurveFp(_p, _a, _b) generator = Point(curve_256, _Gx, _Gy, _r) # secret = randrange(1, generator.order()) pubkey = Public_key(generator, generator * secret) step1 = '\x04' + int_to_string(pubkey.point.x()) + int_to_string( pubkey.point.y()) step2 = hashlib.sha256(step1).digest() ripehash.update(step2) step4 = '\x3f' + ripehash.digest() step5 = hashlib.sha256(step4).digest() step6 = hashlib.sha256(step5).digest() chksum = step6[:4] addr = step4 + chksum addr_58 = b58encode(addr) return (secret, hex(secret)[2:-1], binascii.hexlify(step1), binascii.hexlify(addr), addr_58)
def product(priv, pub): priv = int.from_bytes(priv, 'big') pub_point = Point(curve=NIST256p.curve, x=int.from_bytes(pub['x'], 'big'), y=int.from_bytes(pub['y'], 'big')) pub_key = Public_key(generator=NIST256p.generator, point=pub_point) priv_key = Private_key(public_key=pub_key, secret_multiplier=priv) return (priv_key.secret_multiplier * pub_key.point).x()
def curve_point_from_int(p: int) -> Point: """ Return the elliptic curve point resulting from multiplication of the sec256k1 base point with the integer ``p``. Corresponds directly to the "point(p)" function in BIP32 (https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#conventions). :param p: The integer to multiply with the base point. :return: The point resulting from multiplication of the base point with ``p``. """ return Public_key(SECP256k1_GEN, SECP256k1_GEN * p).point
def sign(cls, secret, hash_buffer): """ Sign a message (hash) with the provided private key and returns the signature. :param secret: private key as 32-byte int :param hash_buffer: Hash of the message as bytes :return: ECSignature object """ assert isinstance(secret, int) assert isinstance(hash_buffer, bytes) hash_int = _hash_to_int(hash_buffer) pubkey = Public_key(g, g * secret) privkey = Private_key(pubkey, secret) random = SystemRandom().randrange(1, ORDER - 1) signature = privkey.sign(hash_int, random) return cls(signature.r, signature.s)
#!/usr/bin/env python3 from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad from ecdsa import SECP256k1 from ecdsa.ecdsa import Public_key, Private_key #from flag import flag import hashlib import random flag = b'sadfasfd' g = SECP256k1.generator order = int(SECP256k1.order) secret = random.randrange(2, order - 1) pubkey = Public_key(g, g * secret) privkey = Private_key(pubkey, secret) print(order) arr = [] for i in range(30): h = random.randrange(2, order - 1) k = random.randrange(2, order - 1) sig = privkey.sign(h, k) lea_k = int("0x" + "{:064x}".format(k)[10:-10], 16) arr.append((h, lea_k, int(sig.r), int(sig.s))) #print(arr) sha256 = hashlib.sha256() sha256.update(str(secret).encode())
from uplink.protocol import * from uplink import * # uplink testPriv d = 72637887363324669071595225655990695893413546682343152974463667925881860469868 # uplink testPub Qx = 79174541683660805620639640382768661759397823295690888507762753093299621916987 Qy = 71065057682299419085325030940046895916159750330092956837115984315857371796477 # uplink testAddr testAddr = 'fwBVDsVh8SYQy98CzYpNPcbyTRczVUZ96HszhNRB8Ve' # Public Key point = ellipticcurve.Point(curve_secp256k1, Qx, Qy) pubk = Public_key(generator_secp256k1, point) vkey = VerifyingKey.from_public_point(point, curve=SECP256k1) skey = SigningKey.from_secret_exponent(d, curve=SECP256k1) # Assets assetAddr = '43WRxMNcnYgZFcE36iohqrXKQdajUdAxeSn9mzE1ZedB' toAddr = '7mR5d7s6cKB4qjuX1kiwwNtygfURhFQ9TKvEd9kmq6QL' testSig = \ (115136800820456833737994126771386015026287095034625623644186278108926690779567, 98245280522003505644797670843107276132602050133082625768706491602875725788467) testTimestamp = 1231006505 nonce = 42
def generatePublicKey(secret): publicKeyPoint = Public_key(generator_secp256k1, generator_secp256k1 * secret).point return '\x04' + int_to_string(publicKeyPoint.x()) + int_to_string( publicKeyPoint.y())
random_bin = os.urandom(bits // 8) random_hex = random_bin.hex() print("Entropy: ", random_hex) # generate private key as the SHA256 hash of the entropy private_key = hashlib.sha256(random_bin).hexdigest() print("Private Key: ", "0x" + private_key) # convert private key to a number k = int(private_key, 16) # get elliptic curve SECP256k1 standard generator point SECP256k1_GEN = SECP256k1.generator # perform elliptic curve multiplication with generator point and private_key number ecc_point = Public_key(SECP256k1_GEN, SECP256k1_GEN * k).point # concat the hex value of elliptic curve x and y coordinates then take Keccak-256 hash to create the public key concat = hex(ecc_point.x())[2:] + hex(ecc_point.y())[2:] public_key = Web3.keccak(hexstr=concat).hex() print("Public Key: ", public_key) # last 20 bytes of private key is public address address = public_key[-40:] print("Address: ", "0x" + address) # convert address to EIP-55 check sum address addr_hash = Web3.keccak(text=address).hex()[2:42] check_summed_addr = [] for i, e in enumerate(addr_hash):
def generatePublicKey(secret): publicKeyPoint = Public_key(generator_secp256k1, generator_secp256k1 * secret).point return '\x04' + int_to_string(publicKeyPoint.x()) + int_to_string(publicKeyPoint.y())
def __init__(self, secret): curve = CurveFp(_p, _a, _b) generator = Point(curve, _Gx, _Gy, _r) self.pubkey = Public_key(generator, generator * secret) self.privkey = Private_key(self.pubkey, secret) self.secret = secret
def genKey(): d = randint(1, order - 1) pubkey = Public_key(G, d * G) privkey = Private_key(pubkey, d) return pubkey, privkey
def curve_point_from_int(k): return Public_key(SECP256k1_GEN, SECP256k1_GEN * k).point