class Secp256k1::PublicKey

Attributes

public_key[RW]

Public Class Methods

new(pubkey: nil, raw: false, flags: FLAG_VERIFY, ctx: nil) click to toggle source
Calls superclass method Secp256k1::BaseKey::new
# File lib/secp256k1/key.rb, line 26
def initialize(pubkey: nil, raw: false, flags: FLAG_VERIFY, ctx: nil)
  super(ctx, flags)

  if pubkey
    if raw
      raise ArgumentError, 'raw pubkey must be bytes' unless pubkey.instance_of?(String)
      @public_key = deserialize pubkey
    else
      #raise ArgumentError, 'pubkey must be an internal object' unless pubkey.instance_of?(String)
      @public_key = pubkey
    end
  else
    @public_key = nil
  end
end

Public Instance Methods

combine(pubkeys) click to toggle source

Add a number of public keys together.

# File lib/secp256k1/key.rb, line 71
def combine(pubkeys)
  raise ArgumentError, 'must give at least 1 pubkey' if pubkeys.empty?

  outpub = FFI::Pubkey.new.pointer
  #pubkeys.each {|item| }

  res = C.secp256k1_ec_pubkey_combine(@ctx, outpub, pubkeys, pubkeys.size)
  raise AssertError, 'failed to combine public keys' unless res == 1

  @public_key = outpub
  outpub
end
deserialize(pubkey_ser) click to toggle source
# File lib/secp256k1/key.rb, line 56
def deserialize(pubkey_ser)
  raise ArgumentError, 'unknown public key size (expected 33 or 65)' unless [33,65].include?(pubkey_ser.size)

  pubkey = C::Pubkey.new.pointer

  res = C.secp256k1_ec_pubkey_parse(@ctx, pubkey, pubkey_ser, pubkey_ser.size)
  raise AssertError, 'invalid public key' unless res == 1

  @public_key = pubkey
  pubkey
end
ecdh(scalar) click to toggle source
# File lib/secp256k1/key.rb, line 109
def ecdh(scalar)
  raise AssertError, 'No public key defined' unless @public_key
  raise ArgumentError, 'scalar must be composed of 32 bytes' unless scalar.instance_of?(String) && scalar.size == 32

  result = FFI::MemoryPointer.new :char, 32

  res = C.secp256k1_ecdh @ctx, result, @public_key, scalar
  raise AssertError, "invalid scalar (#{scalar})" unless res == 1

  result.read_bytes(32)
end
ecdsa_verify(msg, raw_sig, raw: false, digest: Digest::SHA256) click to toggle source
# File lib/secp256k1/key.rb, line 100
def ecdsa_verify(msg, raw_sig, raw: false, digest: Digest::SHA256)
  raise AssertError, 'No public key defined' unless @public_key
  raise AssertError, 'instance not configured for sig verification' if (@flags & FLAG_VERIFY) != FLAG_VERIFY

  msg32 = hash32 msg, raw, digest

  C.secp256k1_ecdsa_verify(@ctx, raw_sig, msg32, @public_key) == 1
end
serialize(compressed: true) click to toggle source
# File lib/secp256k1/key.rb, line 42
def serialize(compressed: true)
  raise AssertError, 'No public key defined' unless @public_key

  len_compressed = compressed ? 33 : 65
  res_compressed = FFI::MemoryPointer.new :char, len_compressed
  outlen = FFI::MemoryPointer.new(:size_t).write_uint(len_compressed)
  compflag = compressed ? EC_COMPRESSED : EC_UNCOMPRESSED

  res = C.secp256k1_ec_pubkey_serialize(@ctx, res_compressed, outlen, @public_key, compflag)
  raise AssertError, 'pubkey serialization failed' unless res == 1

  res_compressed.read_bytes(len_compressed)
end
tweak_add(scalar) click to toggle source

Tweak the current public key by adding a 32 byte scalar times the generator to it and return a new PublicKey instance.

# File lib/secp256k1/key.rb, line 88
def tweak_add(scalar)
  tweak_public :secp256k1_ec_pubkey_tweak_add, scalar
end
tweak_mul(scalar) click to toggle source

Tweak the current public key by multiplying it by a 32 byte scalar and return a new PublicKey instance.

# File lib/secp256k1/key.rb, line 96
def tweak_mul(scalar)
  tweak_public :secp256k1_ec_pubkey_tweak_mul, scalar
end

Private Instance Methods

tweak_public(meth, scalar) click to toggle source
# File lib/secp256k1/key.rb, line 123
def tweak_public(meth, scalar)
  raise ArgumentError, 'scalar must be composed of 32 bytes' unless scalar.instance_of?(String) && scalar.size == 32
  raise AssertError, 'No public key defined.' unless @public_key

  newpub = self.class.new serialize, raw: true

  res = C.send meth, newpub.public_key, scalar
  raise AssertError, 'Tweak is out of range' unless res == 1

  newpub
end