def test_transform(self): x = urandom(32) k = urandom(32) point = G.multiply(x) assert point.add(k) == G.multiply( int_to_bytes_padded((bytes_to_int(x) + bytes_to_int(k)) % n))
def getPubkey(new_prvkey, flag_compress): if flag_gmpy2: Ptmp = mul_ka(new_prvkey) Xcoord = Ptmp.x Ycoord = Ptmp.y elif flag_coincurve: Ptmp = PublicKey.from_valid_secret(int_to_bytes_padded(new_prvkey)) if flag_cffi: tmp_pubkey = ffi.buffer(Ptmp.public_key, 64)[:] Xcoord = bytes_to_int(tmp_pubkey[31::-1]) Ycoord = bytes_to_int(tmp_pubkey[:31:-1]) else: Xcoord, Ycoord = Ptmp.point() else: Ptmp = mul_ka(new_prvkey) Xcoord = Ptmp.x Ycoord = Ptmp.y if flag_compress: if (Ycoord % 2) == 0: new_pubkey = '02%064x' % int(hex(Xcoord)[2:66], 16) else: new_pubkey = '03%064x' % int(hex(Xcoord)[2:66], 16) else: new_pubkey = '04%064x%064x' % (int(hex(Xcoord)[2:66], 16), int(hex(Ycoord)[2:66], 16)) return new_pubkey
def test_bytes_greater_than_group_order(self): secret = (b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff' b'\xff\xff\xfe\xba\xae\xdc\xe6\xafH\xa0;\xbf\xd2^\x8d') assert secret > GROUP_ORDER and bytes_to_int(secret) < GROUP_ORDER_INT secret_from_int = validate_secret(bytes_to_int(secret)) secret = validate_secret(secret) assert secret_from_int == secret and len(secret) == 32 assert ZERO < secret < GROUP_ORDER
def test_out_of_range(self): with pytest.raises(ValueError): validate_secret(ZERO) validate_secret(bytes_to_int(ZERO)) with pytest.raises(ValueError): validate_secret(GROUP_ORDER) validate_secret(bytes_to_int(GROUP_ORDER)) validate_secret(GROUP_ORDER_INT) validate_secret(2**256)
def getXcoord(itpoint): if flag_gmpy2: Xcoord = itpoint.x #Ycoord = itpoint.y elif flag_coincurve: if flag_cffi: tmp_pubkey = ffi.buffer(itpoint.public_key, 64)[:] Xcoord = bytes_to_int(tmp_pubkey[31::-1]) #Ycoord = bytes_to_int(tmp_pubkey[:31:-1]) else: Xcoord, Ycoord = itpoint.point() else: Xcoord = itpoint.x #Ycoord = itpoint.y return Xcoord #,Ycoord
def deserialize_recoverable(serialized, context=GLOBAL_CONTEXT): if len(serialized) != 65: raise ValueError("Serialized signature must be 65 bytes long.") ser_sig, rec_id = serialized[:64], bytes_to_int(serialized[64:]) if not 0 <= rec_id <= 3: raise ValueError("Invalid recovery id.") recover_sig = ffi.new('secp256k1_ecdsa_recoverable_signature *') parsed = lib.secp256k1_ecdsa_recoverable_signature_parse_compact( context.ctx, recover_sig, ser_sig, rec_id) if not parsed: raise ValueError('Failed to parse recoverable signature.') return recover_sig
def point(self) -> Tuple[int, int]: """ :return: The public key as a coordinate point. """ public_key = self.format(compressed=False) return bytes_to_int(public_key[1:33]), bytes_to_int(public_key[33:])
def to_int(self) -> int: """ :return: The private key as an integer. """ return bytes_to_int(self.secret)
def point(self): public_key = self.format(compressed=False) return bytes_to_int(public_key[1:33]), bytes_to_int(public_key[33:])
def to_int(self): return bytes_to_int(self.secret)
def test_bytes_int_conversion_padded(): bytestr = b'\x00' + urandom(31) assert int_to_bytes_padded(bytes_to_int(bytestr)) == bytestr
def test_bytes_int_conversion(): bytestr = b'\x00' + urandom(31) assert pad_scalar(int_to_bytes(bytes_to_int(bytestr))) == bytestr