Module: PathgraphEncoding::PrivateKey

Defined in:
lib/pathgraph_encoding.rb

Overview

Represents a pathgraph private key.

An instance of a private key can be like this:

key = {
  n: 4,
  pi: [[0,1,3,2],[6,7,5,4],[12,13,15,14],[10,11,9,8]]
}

Class Method Summary collapse

Class Method Details

.from_der(der) ⇒ Object

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

Parameters:

der

A byte array representing the private key specified.

Returns:

key

The private key dencoded.



54
55
56
57
58
59
60
61
# File 'lib/pathgraph_encoding.rb', line 54

def self.from_der(der)
  asn1 = OpenSSL::ASN1.decode(der)
  {
    version: OpenSSL::ASN1.decode(asn1.value[0]).value,
    n: OpenSSL::ASN1.decode(asn1.value[1].value[0]).value.to_i,
    pi: OpenSSL::ASN1.decode(asn1.value[1].value[1]).value.map { |e| e.map { |d| d.value.to_i } }
  }
end

.gen_public_key(key) ⇒ Object



139
140
141
142
143
144
# File 'lib/pathgraph_encoding.rb', line 139

def self.gen_public_key(key)
  {
    n: key[:n],
    sigma: key[:pi].map! { |p| [p[0],p[-1]] }
  }
end

.is_adyacent_in_hp?(a, b) ⇒ Boolean

Check if two vertices are adjacent in a hypercube.

Parameters:

a

First hypercube vertex.

b

Second hypercube vertex.

Returns:

If both are adjacent vertices, returns ‘true`, otherwise `false`.

Returns:

  • (Boolean)


135
136
137
# File 'lib/pathgraph_encoding.rb', line 135

def self.is_adyacent_in_hp?(a,b)
  (a^b).to_s(2).count(1) == 1
end

.is_valid?(key) ⇒ Boolean

Check if is a valid private key.

Parameters:

key

A byte array representing the private key specified.

Returns:

Returns ‘true` if is a valid private key, otherwise `false`.

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pathgraph_encoding.rb', line 73

def self.is_valid?(key)
  # Check valid hypercube degree
  return false if is_valid_hypercube_degree(key[:n])

  # Check valid hypercube vertices
  # max_vertex = (1<<key[:n])-1
  key[:pi].each do |pi|
    pi.each do |vertex|
      return false unless is_valid_hypercube_vertex?(vertex,key[:n])
    end

    pi[0...-1].each_with_index do |i,j|
      return false unless is_adyacent_in_hp?(i,pi[j+1])
    end
  end

  true
end

.is_valid_hypercube_degree?(n) ⇒ Boolean

Check if a number is a valid hypercube degree is an integer between 4 and 8.

Parameters:

n

Hypercube degree.

Returns:

If is a valid hypercube degree, returns ‘true`, otherwise `false`.

Returns:

  • (Boolean)


103
104
105
# File 'lib/pathgraph_encoding.rb', line 103

def self.is_valid_hypercube_degree?(n)
  n >= 4 && n <= 8 && n.is_a?(Integer)
end

.is_valid_hypercube_vertex?(v, n) ⇒ Boolean

Check if a number is a valid hypercube vertex.

Parameters:

v

Hypercube vertex.

n

Hypercube degree.

Returns:

If is a valid hypercube vertex, returns ‘true`, otherwise `false`.

Returns:

  • (Boolean)


119
120
121
# File 'lib/pathgraph_encoding.rb', line 119

def self.is_valid_hypercube_vertex?(v,n)
  v >= 0 && v <= (1<<n)-1 && v.is_a?(Integer)
end

.to_der(key) ⇒ Object

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

Parameters:

key

A private key to encode.

Returns:

A byte array representing the private key specified.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pathgraph_encoding.rb', line 27

def self.to_der(key)
  n = OpenSSL::ASN1::Integer.new(key[:n])
  pi = OpenSSL::ASN1::Sequence.new(
    key[:pi].map do |p|
      arr = p.map { |x| OpenSSL::ASN1::Integer.new(x) }
      OpenSSL::ASN1::Sequence.new(arr)
    end
  )
  privateKey = OpenSSL::ASN1::Sequence.new([n,pi])

  version = OpenSSL::ASN1::PrintableString.new("0.0.1")
  instance = OpenSSL::ASN1::Sequence.new([version,privateKey])

  instance.to_der
end