Sha256: 245f305bdc2f3784c5c872018bcd7452a8e34038d9900a499cb530d58dffad3f

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Noise
  module Functions
    module DH
      class Secp256k1
        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)
          [
            ECDSA::Format::IntegerOctetString.encode(private_key, 32),
            ECDSA::Format::PointOctetString.encode(public_key, compression: true)
          ]
        end

        def dh(private_key, public_key)
          key = ::Secp256k1::PublicKey.new(pubkey: public_key, raw: true)
          key.ecdh(private_key)
        end

        def dhlen
          33
        end

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

        def self.from_public(public_key)
          [nil, public_key]
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
noise-ruby-0.6.0 lib/noise/functions/dh/secp256k1.rb
noise-ruby-0.5.3 lib/noise/functions/dh/secp256k1.rb
noise-ruby-0.5.2 lib/noise/functions/dh/secp256k1.rb
noise-ruby-0.5.1 lib/noise/functions/dh/secp256k1.rb
noise-ruby-0.5.0 lib/noise/functions/dh/secp256k1.rb