def add(self, scalar: bytes, update: bool = False): """ Add a scalar to the public key. :param scalar: The scalar with which to add. :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 :raises ValueError: If the tweak was out of range or the resulting public key was invalid. """ scalar = pad_scalar(scalar) new_key = ffi.new('secp256k1_pubkey *', self.public_key[0]) success = lib.secp256k1_ec_pubkey_tweak_add(self.context.ctx, new_key, scalar) if not success: raise ValueError( 'The tweak was out of range, or the resulting public key is invalid.' ) if update: self.public_key = new_key return self return PublicKey(new_key, self.context)
def add(self, scalar: bytes, update: bool = False): """ Add a scalar to the private key. :param scalar: The scalar with which to add. :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 :raises ValueError: If the tweak was out of range or the resulting private key was invalid. """ scalar = pad_scalar(scalar) secret = ffi.new('unsigned char [32]', self.secret) success = lib.secp256k1_ec_privkey_tweak_add(self.context.ctx, secret, scalar) if not success: raise ValueError( 'The tweak was out of range, or the resulting private key is invalid.' ) secret = bytes(ffi.buffer(secret, 32)) if update: self.secret = secret self._update_public_key() return self return PrivateKey(secret, self.context)
def add(self, scalar, update=False): scalar = pad_scalar(scalar) new_key = ffi.new('secp256k1_pubkey *', self.public_key[0]) success = lib.secp256k1_ec_pubkey_tweak_add(self.context.ctx, new_key, scalar) if not success: raise ValueError('The tweak was out of range, or the resulting public key is invalid.') if update: self.public_key = new_key return self return PublicKey(new_key, self.context)
def add(self, scalar, update=False): scalar = pad_scalar(scalar) secret = ffi.new('unsigned char [32]', self.secret) success = lib.secp256k1_ec_privkey_tweak_add(self.context.ctx, secret, scalar) if not success: raise ValueError('The tweak was out of range, or the resulting private key is invalid.') secret = bytes(ffi.buffer(secret, 32)) if update: self.secret = secret self._update_public_key() return self return PrivateKey(secret, self.context)
def test_bytes_int_conversion(): bytestr = b'\x00' + urandom(31) assert pad_scalar(int_to_bytes(bytes_to_int(bytestr))) == bytestr
def test_empty_scalar(self): assert len(pad_scalar(b'')) == 32
def test_pad_limit(self): n = urandom(32) assert len(pad_scalar(n)) == len(n)
def test_correct(self): assert pad_scalar(b'\x01') == b'\x00' * 31 + b'\x01'