def tweak_add(self, scalar): if USE_SECP: self.obj = self.obj.tweak_add(scalar) else: scalar = int.from_bytes(scalar, 'big') privKey = ECPrivateKey(scalar, CURVE_SECP256K1) self.obj = ECPublicKey(self.obj.W + privKey.get_public_key().W)
def tweak_add(self, scalar): if USE_SECP: self.obj = self.obj.tweak_add(scalar) else: scalar = int.from_bytes(scalar, 'big') privKey = ECPrivateKey(scalar, CURVE_SECP256K1) self.obj = ECPublicKey(self.obj.W + privKey.get_public_key().W)
def init_key(): cv = Curve.get_curve('secp256k1') pv_key = ECPrivateKey( int(hashlib.md5(settings.PRIVATE_KEY.encode()).hexdigest(), 16), cv) settings.PUBLIC_KEY = hex( int.from_bytes(cv.encode_point(pv_key.get_public_key().W), "big")) print(settings.PUBLIC_KEY)
def verify(api_key, api_secret, params, signature, timestamp): payload = __composePayload(api_key, params, timestamp) pv_key = ECPrivateKey( int(binascii.hexlify(base64.b64decode(api_secret)), 16), cv) hashed_payload = hashlib.sha256(payload.encode("UTF-8")).hexdigest() return ECDSA().verify(bytearray.fromhex(hashed_payload), bytearray.fromhex(signature), pv_key.get_public_key())
class PrivateKey(object): def __init__(self, privkey=None, raw=True, flags=None, ctx=None): if USE_SECP: if flags == None: flags = secp256k1.ALL_FLAGS self.obj = secp256k1.PrivateKey(privkey, raw, flags, ctx) self.pubkey = self.obj.pubkey else: if not raw: raise Exception("Non raw init unsupported") if privkey == None: privkey = ecpy.ecrand.rnd(CURVE_SECP256K1.order) else: privkey = int.from_bytes(privkey, 'big') self.obj = ECPrivateKey(privkey, CURVE_SECP256K1) pubkey = self.obj.get_public_key().W out = b"\x04" out += pubkey.x.to_bytes(32, 'big') out += pubkey.y.to_bytes(32, 'big') self.pubkey = PublicKey(out, raw=True) def serialize(self): if USE_SECP: return self.obj.serialize() else: return "%.64x" % self.obj.d def ecdsa_serialize(self, raw_sig): if USE_SECP: return self.obj.ecdsa_serialize(raw_sig) else: return raw_sig def ecdsa_sign(self, msg, raw=False, digest=hashlib.sha256, rfc6979=False): if USE_SECP: return self.obj.ecdsa_sign(msg, raw, digest) else: if not raw: h = digest() h.update(msg) msg = h.digest() if rfc6979: signature = SIGNER.sign_rfc6979(msg, self.obj, digest, True) else: signature = SIGNER.sign(msg, self.obj, True) return signature
class PrivateKey(object): def __init__(self, privkey=None, raw=True, flags=None, ctx=None): if USE_SECP: if flags == None: flags = secp256k1.ALL_FLAGS self.obj = secp256k1.PrivateKey(privkey, raw, flags, ctx) self.pubkey = self.obj.pubkey else: if not raw: raise Exception("Non raw init unsupported") if privkey == None: privkey = ecpy.ecrand.rnd(CURVE_SECP256K1.order) else: privkey = int.from_bytes(privkey,'big') self.obj = ECPrivateKey(privkey, CURVE_SECP256K1) pubkey = self.obj.get_public_key().W out = b"\x04" out += pubkey.x.to_bytes(32, 'big') out += pubkey.y.to_bytes(32, 'big') self.pubkey = PublicKey(out, raw=True) def serialize(self): if USE_SECP: return self.obj.serialize() else: return "%.64x"%self.obj.d def ecdsa_serialize(self, raw_sig): if USE_SECP: return self.obj.ecdsa_serialize(raw_sig) else: return raw_sig def ecdsa_sign(self, msg, raw=False, digest=hashlib.sha256): if USE_SECP: return self.obj.ecdsa_sign(msg, raw, digest) else: if not raw: h = digest() h.update(msg) msg = h.digest() signature = SIGNER.sign(msg, self.obj) return bytearray(signature)
def _do_derive_part( cls: Type[SECP256K1], bytes_input: bytes, phase: Literal["root", "mid"]) -> Tuple[ECPublicKey, ECPrivateKey]: """ Given bytes_input determine public/private keypair for a given phase of this algorithm. The difference between generating the root and intermediate keypairs is just what bytes are input by the caller and that the intermediate keypair needs to inject _INTERMEDIATE_KEYPAIR_PADDING into the value to hash to get the raw private key. """ def _candidate_merger(candidate: bytes) -> bytes: if phase == "root": return bytes_input + candidate return bytes_input + _INTERMEDIATE_KEYPAIR_PADDING + candidate raw_private = cls._get_secret(_candidate_merger) wrapped_private = ECPrivateKey(int.from_bytes(raw_private, "big"), _CURVE) return wrapped_private.get_public_key(), wrapped_private
# 8e3f35e4d7fb27a56a3f35d34c8c2b27cd1d266d5294df131bf3c1cbc39f5a91 #App signature: # 304402203a329589dbc6f3bb88bf90b45b5d4935a18e13e2cb8fcee0b94b3102ec19645702202f61af55df0e56e71d40a9f5f111faeb2f831c1fd314c55227ac44110fb33049 ### ECS # test key cv = Curve.get_curve('secp256k1') pv_key = ECPrivateKey(0xf028458b39af92fea938486ecc49562d0e7731b53d9b25e2701183e4f2adc991,cv) pu_key = ECPublicKey(Point(0x81bc1f9486564d3d57a305e8f9067df2a7e1f007d4af4fed085aca139c6b9c7a, 0x8e3f35e4d7fb27a56a3f35d34c8c2b27cd1d266d5294df131bf3c1cbc39f5a91, cv)) k = pv_key.get_public_key() assert(k.W.x == pu_key.W.x) assert(k.W.y == pu_key.W.y) print("Public key ok") msg = 0x8c7632afe967e2e16ae7f39dc32c252b3d751fa6e01daa0efc3c174e230f4617 msg = msg.to_bytes(32,'big') sig = 0x304402203a329589dbc6f3bb88bf90b45b5d4935a18e13e2cb8fcee0b94b3102ec19645702202f61af55df0e56e71d40a9f5f111faeb2f831c1fd314c55227ac44110fb33049 sig = sig.to_bytes(70,'big') ## verify signer = ECDSA() while True:
sig = signer.sign_k(msg,pv_key,k) assert(R==sig[0]) assert(S==sig[1]) assert(signer.verify(msg,sig,pu_key)) ##BSI signer = ECSchnorr(hashlib.sha256,"BSI","ITUPLE") sig = signer.sign_k(msg,pv_key,k) assert(signer.verify(msg,sig,pu_key)) ##Z k = int(0xde7e0e5e663f24183414b7c72f24546b81e9e5f410bebf26f3ca5fa82f5192c8) cv = Curve.get_curve('secp256r1') pv_key = ECPrivateKey(0x2eef7823f82ed254524fad3d11cc17e897e582a0cd52b93f07cc030370d170bd, cv) pu_key = pv_key.get_public_key() msg = int(0xb46d1525379e02e232d97928265b7254ea2ed97813454388c1a08f62dccd70b3) msg = msg.to_bytes(32,'big') signer = ECSchnorr(hashlib.sha256,"Z","ITUPLE") sig = signer.sign_k(msg,pv_key,k) assert(signer.verify(msg,sig,pu_key)) ##LIBSECP cv = Curve.get_curve('secp256k1') pu_key = ECPublicKey(Point(0x65d5b8bf9ab1801c9f168d4815994ad35f1dcb6ae6c7a1a303966b677b813b00, 0xe6b865e529b8ecbf71cf966e900477d49ced5846d7662dd2dd11ccd55c0aff7f, cv)) pv_key = ECPrivateKey(0xfb26a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv)
seckey2 = ECPrivateKey( 0xf226a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey3 = ECPrivateKey( 0xf326a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey4 = ECPrivateKey( 0xf426a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey5 = ECPrivateKey( 0xf526a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey6 = ECPrivateKey( 0xf626a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey7 = ECPrivateKey( 0xf726a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) seckey8 = ECPrivateKey( 0xf826a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) pubkey0 = seckey0.get_public_key() pubkey1 = seckey1.get_public_key() pubkey2 = seckey2.get_public_key() pubkey3 = seckey3.get_public_key() pubkey4 = seckey4.get_public_key() pubkey5 = seckey5.get_public_key() pubkey6 = seckey6.get_public_key() pubkey7 = seckey7.get_public_key() pubkey8 = seckey8.get_public_key() pubs = [ pubkey0, pubkey1, pubkey2, pubkey3, pubkey4, pubkey5, pubkey6, pubkey7 ] secs = [ seckey0, seckey1, seckey2, seckey3, seckey4, seckey5, seckey6, seckey7 ]
pprint("The signature is valid") else: pprint("Invalid signature") ### Létrehozok 4 privát-public keypárt és a tömbökbe teszem publicKeys = [] privateKeys = [] signer = EDDSA(hashlib.sha512) #privKey = ECPrivateKey(secrets.randbits(32*8), curve) #privKey2 = ECPrivateKey(secrets.randbits(32*8), curve) #pubKey = signer.get_public_key(privKey, hashlib.sha512) for x in range(4): privKey = ECPrivateKey(secrets.randbits(32 * 8), curve) pubKey = signer.get_public_key(privKey, hashlib.sha512) publicKeys.append(privKey.get_public_key()) privateKeys.append(privKey) print("Priv - pubkey párok:") print(privKey) print(pubKey) print() ####TESZTELÉS##### ###Kiválasztom a 2-es indexen levő embert a signernek(de lehetne véletlenszerü is) #key_index = random.randint(0,3) key_index = 2 message = "This is a ring signature" verify(curve, *ring_sign(curve, message, publicKeys, privateKeys, key_index))
def generate_key_with_peerid(self, curve, peerid): cv = Curve.get_curve(curve) pv_key = ECPrivateKey(peerid, cv) pu_key = pv_key.get_public_key() return (pu_key, pv_key, cv, ECDSA())
def generate_key(self, curve): # curve : curve 이름 cv = Curve.get_curve(curve) pv_key = ECPrivateKey(rnd(cv.order), cv) pu_key = pv_key.get_public_key() return (pu_key, pv_key, cv, ECDSA())
from ecpy.curves import Curve, Point from ecpy.keys import ECPublicKey, ECPrivateKey from ecpy.ecdsa import ECDSA import hashlib import binascii import settings # blind signature cv = Curve.get_curve('secp256k1') signer = ECDSA() pv_key = ECPrivateKey( int(hashlib.md5(settings.PRIVATE_KEY.encode()).hexdigest(), 16), cv) pu_key = pv_key.get_public_key() d = pv_key.d P = pu_key.W n = cv.order G = cv.generator r = 1 R = r * G hasher = hashlib.sha256() def int_to_bytes(x): return x.to_bytes((x.bit_length() + 7) // 8, byteorder='big') def hex_to_bytes(x): x = int(x, 16) return int_to_bytes(x)