Class: Secp256k1::MuSig::KeyAggContext

Inherits:
Object
  • Object
show all
Includes:
Secp256k1
Defined in:
lib/secp256k1/musig/key_agg.rb

Overview

Key aggregation context class.

Constant Summary

Constants included from Secp256k1

CONTEXT_SIGN, CONTEXT_VERIFY, EC_COMPRESSED, EC_UNCOMPRESSED, ELL_SWIFT_KEY_SIZE, FLAGS_BIT_COMPRESSION, FLAGS_BIT_CONTEXT_SIGN, FLAGS_BIT_CONTEXT_VERIFY, FLAGS_TYPE_COMPRESSION, FLAGS_TYPE_CONTEXT, FLAGS_TYPE_MASK, VERSION, X_ONLY_PUBKEY_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Secp256k1

#create_keypair, #generate_key_pair, #generate_pubkey, #parse_ec_pubkey?, #sign_ecdsa, #valid_xonly_pubkey?, #verify_ecdsa, #with_context

Methods included from Secp256k1::MuSig

#aggregate_musig_nonce, #aggregate_pubkey, #generate_musig_nonce, #generate_musig_session_id

Methods included from EllSwift

#ellswift_create, #ellswift_decode, #ellswift_ecdh_xonly

Methods included from SchnorrSig

#sign_schnorr, #verify_schnorr

Methods included from Recover

#recover, #sign_recoverable

Methods included from C

ellswift_xdh_hash_function_bip324

Constructor Details

#initialize(key_agg_cache) ⇒ KeyAggContext

Constructor.

Parameters:

Raises:

  • (ArgumentError)

    If invalid arguments specified.



18
19
20
21
# File 'lib/secp256k1/musig/key_agg.rb', line 18

def initialize(key_agg_cache)
  raise ArgumentError, "key_agg_cache must be Secp256k1::KeyAggCache." unless key_agg_cache.is_a?(Secp256k1::KeyAggCache)
  @cache = key_agg_cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/secp256k1/musig/key_agg.rb', line 13

def cache
  @cache
end

Instance Method Details

#aggregate_public_keyString

Get aggregate public key.

Returns:

  • (String)

    An aggregated public key.



25
26
27
28
29
30
31
32
33
# File 'lib/secp256k1/musig/key_agg.rb', line 25

def aggregate_public_key
  with_context do |context|
    agg_pubkey = FFI::MemoryPointer.new(:uchar, 64)
    if secp256k1_musig_pubkey_get(context, agg_pubkey, cache.pointer) == 0
      raise Error, "secp256k1_musig_pubkey_get arguments invalid."
    end
    serialize_pubkey(context, agg_pubkey)
  end
end

#pointerFFI::MemoryPointer

Get KeyAggCache pointer.

Returns:

  • (FFI::MemoryPointer)


61
62
63
# File 'lib/secp256k1/musig/key_agg.rb', line 61

def pointer
  cache.pointer
end

#tweak_add(tweak, xonly: false) ⇒ String

Apply ordinary “EC” tweaking to a public key.

Parameters:

  • tweak (String)

    Tweak value to tweak the aggregated key.

  • xonly (Boolean) (defaults to: false)

    Apply x-only tweaking or not.

Returns:

  • (String)

    Tweaked x-only public key with hex format.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/secp256k1/musig/key_agg.rb', line 41

def tweak_add(tweak, xonly: false)
  validate_string!("tweak", tweak, 32)
  with_context do |context|
    tweak_ptr = FFI::MemoryPointer.new(:uchar, 32).put_bytes(0, hex2bin(tweak))
    pubkey_ptr = FFI::MemoryPointer.new(:uchar, 64)
    if xonly
      if secp256k1_musig_pubkey_xonly_tweak_add(context, pubkey_ptr, cache.pointer, tweak_ptr) == 0
        raise Error, "secp256k1_musig_pubkey_tweak_add arguments invalid."
      end
    else
      if secp256k1_musig_pubkey_ec_tweak_add(context, pubkey_ptr, cache.pointer, tweak_ptr) == 0
        raise Error, "secp256k1_musig_pubkey_tweak_add arguments invalid."
      end
    end
    serialize_pubkey(context, pubkey_ptr)
  end
end