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
-
.from_der(der) ⇒ Object
Decode and build a instance of a pathgraph private key from a DER-formatted bytes array.
-
.gen_public_key(key) ⇒ Object
Generates a public key from a private key.
-
.is_adyacent_in_hp?(a, b) ⇒ Boolean
Check if two vertices are adjacent in a hypercube.
-
.is_valid?(key) ⇒ Boolean
Check if is a valid private key.
-
.is_valid_hypercube_degree?(n) ⇒ Boolean
Check if a number is a valid hypercube degree is an integer between 4 and 8.
-
.is_valid_hypercube_vertex?(v, n) ⇒ Boolean
Check if a number is a valid hypercube vertex.
-
.to_der(key) ⇒ Object
Compute DER-formatted bytes array of a pathgraph private key.
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.
76 77 78 79 80 81 82 83 |
# File 'lib/pathgraph_encoding.rb', line 76 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
Generates a public key from a private key.
Parameters:
- key
-
Public key.
Returns:
Public key related with private one parameterized.
170 171 172 173 174 |
# File 'lib/pathgraph_encoding.rb', line 170 def self.gen_public_key(key) sigma = [] key[:pi].each { |p| sigma << [p.first,p.last] } {n: key[:n], sigma: sigma} 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`.
157 158 159 |
# File 'lib/pathgraph_encoding.rb', line 157 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`.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pathgraph_encoding.rb', line 95 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`.
125 126 127 |
# File 'lib/pathgraph_encoding.rb', line 125 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`.
141 142 143 |
# File 'lib/pathgraph_encoding.rb', line 141 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pathgraph_encoding.rb', line 49 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 |