Module: Secp256k1::ECDSA

Included in:
PrivateKey, PublicKey
Defined in:
lib/secp256k1/ecdsa.rb

Constant Summary collapse

SIZE_SERIALIZED =
74
SIZE_COMPACT =
64

Instance Method Summary collapse

Instance Method Details

#ecdsa_deserialize(ser_sig) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
# File 'lib/secp256k1/ecdsa.rb', line 18

def ecdsa_deserialize(ser_sig)
  raw_sig = C::ECDSASignature.new.pointer

  res = C.secp256k1_ecdsa_signature_parse_der(@ctx, raw_sig, ser_sig, ser_sig.size)
  raise AssertError, "raw signature parse failed" unless res == 1

  raw_sig
end

#ecdsa_deserialize_compact(ser_sig) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
# File 'lib/secp256k1/ecdsa.rb', line 36

def ecdsa_deserialize_compact(ser_sig)
  raise ArgumentError, 'invalid signature length' unless ser_sig.size == 64

  raw_sig = C::ECDSASignature.new.pointer

  res = C.secp256k1_ecdsa_signature_parse_compact(@ctx, raw_sig, ser_sig)
  raise AssertError, "failed to deserialize compact signature" unless res == 1

  raw_sig
end

#ecdsa_recover(msg, recover_sig, raw: false, digest: Digest::SHA256) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/secp256k1/ecdsa.rb', line 62

def ecdsa_recover(msg, recover_sig, raw: false, digest: Digest::SHA256)
  raise AssertError, 'instance not configured for ecdsa recover' if (@flags & ALL_FLAGS) != ALL_FLAGS

  msg32 = hash32 msg, raw, digest
  pubkey = C::Pubkey.new.pointer

  res = C.secp256k1_ecdsa_recover(@ctx, pubkey, recover_sig, msg32)
  raise AssertError, 'failed to recover ECDSA public key' unless res == 1

  pubkey
end

#ecdsa_recoverable_convert(recover_sig) ⇒ Object



95
96
97
98
99
# File 'lib/secp256k1/ecdsa.rb', line 95

def ecdsa_recoverable_convert(recover_sig)
  normal_sig = C::ECDSASignature.new.pointer
  C.secp256k1_ecdsa_recoverable_signature_convert(@ctx, normal_sig, recover_sig)
  normal_sig
end

#ecdsa_recoverable_deserialize(ser_sig, rec_id) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/secp256k1/ecdsa.rb', line 83

def ecdsa_recoverable_deserialize(ser_sig, rec_id)
  raise ArgumentError, 'invalid rec_id' if rec_id < 0 || rec_id > 3
  raise ArgumentError, 'invalid signature length' if ser_sig.size != 64

  recover_sig = C::ECDSARecoverableSignature.new.pointer

  res = C.secp256k1_ecdsa_recoverable_signature_parse_compact(@ctx, recover_sig, ser_sig, rec_id)
  raise AssertError, 'failed to parse ECDSA compact sig' unless res == 1

  recover_sig
end

#ecdsa_recoverable_serialize(recover_sig) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/secp256k1/ecdsa.rb', line 74

def ecdsa_recoverable_serialize(recover_sig)
  output = FFI::MemoryPointer.new :uchar, SIZE_COMPACT
  recid = FFI::MemoryPointer.new :int

  C.secp256k1_ecdsa_recoverable_signature_serialize_compact(@ctx, output, recid, recover_sig)

  [output.read_bytes(SIZE_COMPACT), recid.read_int]
end

#ecdsa_serialize(raw_sig) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
# File 'lib/secp256k1/ecdsa.rb', line 8

def ecdsa_serialize(raw_sig)
  output = FFI::MemoryPointer.new(:uchar, SIZE_SERIALIZED)
  outputlen = FFI::MemoryPointer.new(:size_t).put_uint(0, SIZE_SERIALIZED)

  res = C.secp256k1_ecdsa_signature_serialize_der(@ctx, output, outputlen, raw_sig)
  raise AssertError, "failed to seriazlie signature" unless res == 1

  output.read_bytes(outputlen.read_uint)
end

#ecdsa_serialize_compact(raw_sig) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
# File 'lib/secp256k1/ecdsa.rb', line 27

def ecdsa_serialize_compact(raw_sig)
  output = FFI::MemoryPointer.new(:uchar, SIZE_COMPACT)

  res = C.secp256k1_ecdsa_signature_serialize_compact(@ctx, output, raw_sig)
  raise AssertError, "failed to seriazlie compact signature" unless res == 1

  output.read_bytes(SIZE_COMPACT)
end

#ecdsa_signature_normalize(raw_sig, check_only: false) ⇒ Object

Check and optionally convert a signature to a normalized lower-S form. If check_only is ‘true` then the normalized signature is not returned.

This function always return a tuple containing a boolean (‘true` if not previously normalized or `false` if signature was already normalized), and the normalized signature. When check_only is `true`, the normalized signature returned is always `nil`.



56
57
58
59
60
# File 'lib/secp256k1/ecdsa.rb', line 56

def ecdsa_signature_normalize(raw_sig, check_only: false)
  sigout = check_only ? nil : C::ECDSASignature.new.pointer
  res = C.secp256k1_ecdsa_signature_normalize(@ctx, sigout, raw_sig)
  [res == 1, sigout]
end