module PathgraphEncoding::PublicKey

Represents a pathgraph public key.

Public Class Methods

from_der(der) click to toggle source

Decode and build a instance of a pathgraph public key from a DER-formatted bytes array.

Parameters:

der

A byte array representing the public key specified.

Returns:

key

The public key dencoded.

# File lib/pathgraph_encoding.rb, line 217
def self.from_der(der)
  # Decode DER bytes
  asn1 = OpenSSL::ASN1.decode(der)

  # Public key build from DER bytes
  {
    version: OpenSSL::ASN1.decode(asn1.value[0]).value,
    n: OpenSSL::ASN1.decode(asn1.value[1].value[0]).value.to_i,
    sigma: OpenSSL::ASN1.decode(asn1.value[1].value[1]).value.map { |e| e.map { |d| d.value.to_i } }
  }
end
is_valid?(key) click to toggle source

Check if is a valid public key.

Implemented validations

  • Hypercube degree (n) is valid if:

    • Is an integer, such that is greater than 4 (‘PathgraphEncoding::MIN_Q_N`).

  • Set (sigma) of pairs of endpoint vertices is valid if:

    • For each pair of vertices in set, each vertex is:

      • A non negative integer, such that is less than 2 to n.

Parameters:

key

A Hash object representing the public key specified.

Returns:

Returns ‘true` if key satisfies implemented validations, otherwise `false`.

# File lib/pathgraph_encoding.rb, line 248
def self.is_valid?(key)
  # Check valid hypercube degree (n)
  return false if !key[:n].is_a? Integer || key[:n]<MIN_Q_N

  # Check vertices valid in hypercube Q_n
  max_vertex = (1<<key[:n])-1
  key[:sigma].flatten.each do |vertex|
    return false if !vertex.is_a? Integer || vertex>max_vertex || vertex<0
  end

  true
end
to_der(key) click to toggle source

Compute DER-formatted bytes array of a pathgraph public key.

Parameters:

key

A public key to encode.

Returns:

A byte array representing the public key specified.

# File lib/pathgraph_encoding.rb, line 190
def self.to_der(key)
  n = OpenSSL::ASN1::Integer.new(key[:n])
  sigma = OpenSSL::ASN1::Sequence.new(
    key[:sigma].map do |sig|
      arr = sig.map { |x| OpenSSL::ASN1::Integer.new(x) }
      OpenSSL::ASN1::Sequence.new(arr)
    end
  )
  publicKey = OpenSSL::ASN1::Sequence.new([n,sigma])

  version = OpenSSL::ASN1::PrintableString.new(PathgraphEncoding::VERSION)
  instance = OpenSSL::ASN1::Sequence.new([version,publicKey])

  instance.to_der
end