Class: Noise::Functions::DH::Secp256k1

Inherits:
Object
  • Object
show all
Defined in:
lib/noise/functions/dh/secp256k1.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSecp256k1

Returns a new instance of Secp256k1.



9
10
11
# File 'lib/noise/functions/dh/secp256k1.rb', line 9

def initialize
  Noise.optional_dependency!('secp256k1')
end

Class Method Details

.from_private(private_key) ⇒ Object



34
35
36
37
38
39
# File 'lib/noise/functions/dh/secp256k1.rb', line 34

def self.from_private(private_key)
  group = ECDSA::Group::Secp256k1
  scalar = ECDSA::Format::IntegerOctetString.decode(private_key)
  point = group.generator.multiply_by_scalar(scalar)
  Noise::Key.new(private_key, ECDSA::Format::PointOctetString.encode(point, compression: true))
end

Instance Method Details

#dh(private_key, public_key) ⇒ Object



23
24
25
26
27
28
# File 'lib/noise/functions/dh/secp256k1.rb', line 23

def dh(private_key, public_key)
  key = ::Secp256k1::PublicKey.new(pubkey: public_key, raw: true)
  key.ecdh(private_key)
rescue ::Secp256k1::AssertError
  raise Noise::Exceptions::InvalidPublicKeyError, public_key
end

#dhlenObject



30
31
32
# File 'lib/noise/functions/dh/secp256k1.rb', line 30

def dhlen
  33
end

#generate_keypairObject



13
14
15
16
17
18
19
20
21
# File 'lib/noise/functions/dh/secp256k1.rb', line 13

def generate_keypair
  group = ECDSA::Group::Secp256k1
  private_key = 1 + SecureRandom.random_number(group.order - 1)
  public_key = group.generator.multiply_by_scalar(private_key)
  Noise::Key.new(
    ECDSA::Format::IntegerOctetString.encode(private_key, 32),
    ECDSA::Format::PointOctetString.encode(public_key, compression: true)
  )
end