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_valid(self): secret = validate_secret(b'\x01') assert len(secret) == 32 and ZERO < secret < GROUP_ORDER secret = validate_secret(1) assert len(secret) == 32 and ZERO < secret < GROUP_ORDER secret = validate_secret(2**255) assert len(secret) == 32 and ZERO < secret < GROUP_ORDER
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 secret = validate_secret(secret) assert len(secret) == 32 and ZERO < secret < GROUP_ORDER
def __init__(self, secret: bytes = None, context: Context = GLOBAL_CONTEXT): """ :param secret: The secret used to initialize the private key. If not provided or `None`, a new key will be generated. """ self.secret: bytes = validate_secret( secret) if secret is not None else get_valid_secret() self.context = context self.public_key: PublicKey = PublicKey.from_valid_secret( self.secret, self.context)
def multiply(self, scalar, update=False): scalar = validate_secret(scalar) new_key = ffi.new('secp256k1_pubkey *', self.public_key[0]) lib.secp256k1_ec_pubkey_tweak_mul(self.context.ctx, new_key, scalar) if update: self.public_key = new_key return self return PublicKey(new_key, self.context)
def from_secret(cls, secret, context=GLOBAL_CONTEXT): public_key = ffi.new('secp256k1_pubkey *') created = lib.secp256k1_ec_pubkey_create(context.ctx, public_key, validate_secret(secret)) if not created: raise ValueError('Somehow an invalid secret was used. Please ' 'submit this as an issue here: ' 'https://github.com/ofek/coincurve/issues/new') return PublicKey(public_key, context)
def multiply(self, scalar, update=False): scalar = validate_secret(scalar) secret = ffi.new('unsigned char [32]', self.secret) lib.secp256k1_ec_privkey_tweak_mul(self.context.ctx, secret, scalar) secret = bytes(ffi.buffer(secret, 32)) if update: self.secret = secret self._update_public_key() return self return PrivateKey(secret, self.context)
def multiply(self, scalar: bytes, update: bool = False): """ Multiply the public key by a scalar. :param scalar: The scalar with which to multiply. :param update: Whether or not to update and return the public key in-place. :return: The new public key, or the modified public key if `update` is `True`. :rtype: PublicKey """ scalar = validate_secret(scalar) new_key = ffi.new('secp256k1_pubkey *', self.public_key[0]) lib.secp256k1_ec_pubkey_tweak_mul(self.context.ctx, new_key, scalar) if update: self.public_key = new_key return self return PublicKey(new_key, self.context)
def from_secret(cls, secret: bytes, context: Context = GLOBAL_CONTEXT): """ Derive a public key from a private key secret. :param secret: The private key secret. :param context: :return: The public key. :rtype: PublicKey """ public_key = ffi.new('secp256k1_pubkey *') created = lib.secp256k1_ec_pubkey_create(context.ctx, public_key, validate_secret(secret)) if not created: # no cov raise ValueError('Somehow an invalid secret was used. Please ' 'submit this as an issue here: ' 'https://github.com/ofek/coincurve/issues/new') return PublicKey(public_key, context)
def multiply(self, scalar: bytes, update: bool = False): """ Multiply the private key by a scalar. :param scalar: The scalar with which to multiply. :param update: Whether or not to update and return the private key in-place. :return: The new private key, or the modified private key if `update` is `True`. :rtype: PrivateKey """ scalar = validate_secret(scalar) secret = ffi.new('unsigned char [32]', self.secret) lib.secp256k1_ec_privkey_tweak_mul(self.context.ctx, secret, scalar) secret = bytes(ffi.buffer(secret, 32)) if update: self.secret = secret self._update_public_key() return self return PrivateKey(secret, self.context)
def __init__(self, secret=None, context=GLOBAL_CONTEXT): self.secret = (validate_secret(secret) if secret is not None else get_valid_secret()) self.context = context self.public_key = PublicKey.from_valid_secret(self.secret, self.context)
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 test_out_of_range(self): with pytest.raises(ValueError): validate_secret(ZERO) with pytest.raises(ValueError): validate_secret(GROUP_ORDER)