pathgraph_encoding 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 539b9944e969b623e99638226f2147a40616cd2d
4
- data.tar.gz: e948a204f88dbd3d3c6748e8907fb6bbb6c50f01
3
+ metadata.gz: 34c1f39199c77a438522e9223ad23f6f76443e1b
4
+ data.tar.gz: 4983a1ca55c12384d73fd67d72afbfcbd58bd128
5
5
  SHA512:
6
- metadata.gz: 142e6d3e73febbc817ddd745575182bba62f69be6a35795caafb09dbe1d854b8a9ed4b5bf94ec98895c0889a0e03ac98b42687ace35da963b9620f49ad4e7bd6
7
- data.tar.gz: ce33bd72517c1177df080f7b5d092be896606116d977b49457f664823f4441eefed417d149b302c2bdf73a94ddbea47e1572dca9efd198f0a504ba2ab9bdf1bd
6
+ metadata.gz: 1daf7a42f5318483a8e205297f3fad5c82e016b21d993e5628abe00579bf406c62689f90bbcf7a2fb4c574120d0148ac4ce482cead5968551485b68bc660c9a6
7
+ data.tar.gz: 6274c878da0a5380a1b7ea973f90d73106b48a965a7fe5389b1756cba1cf77dbfcfce7f0fdd377ecb8e88ef55eeffbab4aa2e3d495ffd88aa5fe1e0f66ea756d
@@ -1,4 +1,7 @@
1
1
  module PathgraphEncoding
2
2
  # Pathgraph encoding library version
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
+
5
+ # Minimum hypercube degree
6
+ MIN_Q_N = 4
4
7
  end
@@ -1,10 +1,32 @@
1
- require "pathgraph_encoding/version"
1
+ require 'pathgraph_encoding/version'
2
2
  require 'openssl'
3
- require 'pp'
4
3
 
5
4
  # @author Israel Buitron
6
5
  module PathgraphEncoding
7
6
 
7
+ def self.pack(k, m, set)
8
+ OpenSSL::ASN1::Sequence.new([
9
+ OpenSSL::ASN1::Integer.new(k),
10
+ OpenSSL::ASN1::Integer.new(m),
11
+ OpenSSL::ASN1::Sequence.new(
12
+ set.map { |path|
13
+ OpenSSL::ASN1::Sequence.new(
14
+ path.map { |x| OpenSSL::ASN1::Integer.new(x) }
15
+ )
16
+ }
17
+ )
18
+ ]).to_der
19
+ end
20
+
21
+ def self.unpack(der)
22
+ asn1 = OpenSSL::ASN1.decode(der)
23
+ [
24
+ OpenSSL::ASN1.decode(asn1.value[0]).value.to_i, # k
25
+ OpenSSL::ASN1.decode(asn1.value[1]).value.to_i, # m
26
+ OpenSSL::ASN1.decode(asn1.value[2]).value.map { |e| e.map { |d| d.value.to_i } }
27
+ ]
28
+ end
29
+
8
30
  # Represents a pathgraph private key.
9
31
  #
10
32
  # An instance of a private key can be like this:
@@ -136,11 +158,19 @@ module PathgraphEncoding
136
158
  (a^b).to_s(2).count(1) == 1
137
159
  end
138
160
 
161
+ # Generates a public key from a private key.
162
+ #
163
+ # == Parameters:
164
+ # key::
165
+ # Public key.
166
+ #
167
+ # == Returns:
168
+ # Public key related with private one parameterized.
169
+ #
139
170
  def self.gen_public_key(key)
140
- {
141
- n: key[:n],
142
- sigma: key[:pi].map! { |p| [p[0],p[-1]] }
143
- }
171
+ sigma = []
172
+ key[:pi].each { |p| sigma << [p.first,p.last] }
173
+ {n: key[:n], sigma: sigma}
144
174
  end
145
175
 
146
176
  end
@@ -159,8 +189,6 @@ module PathgraphEncoding
159
189
  #
160
190
  def self.to_der(key)
161
191
  n = OpenSSL::ASN1::Integer.new(key[:n])
162
- # m = OpenSSL::ASN1::Integer.new(key[:m])
163
- # k = OpenSSL::ASN1::Integer.new(key[:k])
164
192
  sigma = OpenSSL::ASN1::Sequence.new(
165
193
  key[:sigma].map do |sig|
166
194
  arr = sig.map { |x| OpenSSL::ASN1::Integer.new(x) }
@@ -169,7 +197,7 @@ module PathgraphEncoding
169
197
  )
170
198
  publicKey = OpenSSL::ASN1::Sequence.new([n,sigma])
171
199
 
172
- version = OpenSSL::ASN1::PrintableString.new("0.0.1")
200
+ version = OpenSSL::ASN1::PrintableString.new(PathgraphEncoding::VERSION)
173
201
  instance = OpenSSL::ASN1::Sequence.new([version,publicKey])
174
202
 
175
203
  instance.to_der
@@ -187,21 +215,47 @@ module PathgraphEncoding
187
215
  # The public key dencoded.
188
216
  #
189
217
  def self.from_der(der)
190
- # TODO: implement this method
218
+ # Decode DER bytes
219
+ asn1 = OpenSSL::ASN1.decode(der)
220
+
221
+ # Public key build from DER bytes
222
+ {
223
+ version: OpenSSL::ASN1.decode(asn1.value[0]).value,
224
+ n: OpenSSL::ASN1.decode(asn1.value[1].value[0]).value.to_i,
225
+ sigma: OpenSSL::ASN1.decode(asn1.value[1].value[1]).value.map { |e| e.map { |d| d.value.to_i } }
226
+ }
191
227
  end
192
228
 
193
229
  # Check if is a valid public key.
230
+ #
231
+ # === Implemented validations
232
+ #
233
+ # - Hypercube degree (n) is valid if:
234
+ # - Is an integer, such that is greater than 4
235
+ # (`PathgraphEncoding::MIN_Q_N`).
236
+ # - Set (sigma) of pairs of endpoint vertices is valid if:
237
+ # - For each pair of vertices in set, each vertex is:
238
+ # - A non negative integer, such that is less than 2 to n.
194
239
  #
195
240
  # == Parameters:
196
241
  # key::
197
- # A byte array representing the public key specified.
242
+ # A Hash object representing the public key specified.
198
243
  #
199
244
  # == Returns:
200
- # Returns `true` if is a valid public key, otherwise
201
- # `false`.
245
+ # Returns `true` if key satisfies implemented validations,
246
+ # otherwise `false`.
202
247
  #
203
248
  def self.is_valid?(key)
204
- # TODO: implement this method
249
+ # Check valid hypercube degree (n)
250
+ return false if !key[:n].is_a? Integer || key[:n]<MIN_Q_N
251
+
252
+ # Check vertices valid in hypercube Q_n
253
+ max_vertex = (1<<key[:n])-1
254
+ key[:sigma].flatten.each do |vertex|
255
+ return false if !vertex.is_a? Integer || vertex>max_vertex || vertex<0
256
+ end
257
+
258
+ true
205
259
  end
206
260
 
207
261
  end
@@ -8,7 +8,10 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["Israel Buitron"]
9
9
  spec.email = ["ibuitron@ipn.mx"]
10
10
  spec.summary = %q{PathgraphEnconding is a library to encode Pathgraph keys to ASN.1}
11
- spec.description = %q{PathgraphEnconding is a library to encode and decode Pathgraph keys (public and private) to ASN.1 DER format.}
11
+ spec.description = <<-EOF
12
+ PathgraphEnconding is a library to encode and decode Pathgraph
13
+ keys (public and private) to ASN.1 DER format.
14
+ EOF
12
15
  spec.homepage = "http://computacion.cs.cinvestav.mx/~ibuitron/"
13
16
  spec.license = "MIT"
14
17
  spec.metadata = { 'issue_tracker' => "https://gitlab.com/israelbuitron/pathgraph_encoding/issues" }
@@ -0,0 +1,27 @@
1
+ require 'minitest/autorun'
2
+ require 'pathgraph_encoding'
3
+
4
+ class TestPathGraph < Minitest::Test
5
+
6
+ def setup
7
+ @k = 4
8
+ @m = 4
9
+ @pi = [[0,1,3,2],[6,7,5,4],[12,13,15,14],[10,11,9,8]]
10
+ @der = "0@\x02\x01\x04\x02\x01\x04080\f\x02\x01\x00\x02\x01"\
11
+ "\x01\x02\x01\x03\x02\x01\x020\f\x02\x01\x06\x02\x01\a\x02\x01"\
12
+ "\x05\x02\x01\x040\f\x02\x01\f\x02\x01\r\x02\x01\x0F\x02\x01"\
13
+ "\x0E0\f\x02\x01\n\x02\x01\v\x02\x01\t\x02\x01\b"
14
+ end
15
+
16
+ def test_pack
17
+ der = PathgraphEncoding.pack(@k, @m, @pi)
18
+ assert_equal 66, der.size
19
+ assert_equal @der, der
20
+ end
21
+
22
+ def test_unpack
23
+ tuple = PathgraphEncoding.unpack(@der)
24
+ assert_equal [@k,@m,@pi], tuple
25
+ end
26
+
27
+ end
@@ -46,8 +46,10 @@ class TestPathGraphPrivateKey < Minitest::Test
46
46
  n: 4,
47
47
  sigma: [[0,2],[6,4],[12,14],[10,8]]
48
48
  }
49
- key = PathgraphEncoding::PrivateKey::gen_public_key(@private_key)
49
+ key = PathgraphEncoding::PrivateKey.gen_public_key(@private_key)
50
50
  assert_equal expected, key
51
+ refute_same @private_key, key
52
+ refute_same @private_key[:pi], key[:sigma]
51
53
  end
52
54
 
53
55
  end
@@ -0,0 +1,36 @@
1
+ require 'minitest/autorun'
2
+ require 'pathgraph_encoding'
3
+
4
+ class TestPathGraphPublicKey < Minitest::Test
5
+
6
+ def setup
7
+ @public_key = {
8
+ n: 4,
9
+ sigma: [[0,2],[6,4],[12,14],[10,8]]
10
+ }
11
+ @der_public_key = "0.\x13\x05#{PathgraphEncoding::VERSION}0%"\
12
+ "\x02\x01\x040 0\x06\x02\x01\x00\x02\x01\x020\x06\x02\x01\x06"\
13
+ "\x02\x01\x040\x06\x02\x01\f\x02\x01\x0E0\x06\x02\x01\n\x02"\
14
+ "\x01\b"
15
+ end
16
+
17
+ def test_to_der
18
+ der = PathgraphEncoding::PublicKey::to_der(@public_key)
19
+
20
+ assert_equal 48, der.size
21
+ assert_equal @der_public_key, der
22
+ end
23
+
24
+ def test_from_der
25
+ key = PathgraphEncoding::PublicKey::from_der(@der_public_key)
26
+ exp = {version: PathgraphEncoding::VERSION,
27
+ n: 4,
28
+ sigma: [[0,2],[6,4],[12,14],[10,8]]}
29
+ assert_equal key, exp
30
+ end
31
+
32
+ def test_is_valid
33
+ assert PathgraphEncoding::PublicKey::is_valid?(@public_key)
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pathgraph_encoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Israel Buitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,8 +38,9 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.4'
41
- description: PathgraphEnconding is a library to encode and decode Pathgraph keys (public
42
- and private) to ASN.1 DER format.
41
+ description: |2
42
+ PathgraphEnconding is a library to encode and decode Pathgraph
43
+ keys (public and private) to ASN.1 DER format.
43
44
  email:
44
45
  - ibuitron@ipn.mx
45
46
  executables: []
@@ -57,7 +58,9 @@ files:
57
58
  - lib/pathgraph_encoding.rb
58
59
  - lib/pathgraph_encoding/version.rb
59
60
  - pathgraph_encoding.gemspec
61
+ - test/test_pathgraph.rb
60
62
  - test/test_pathgraph_privatekey.rb
63
+ - test/test_pathgraph_publickey.rb
61
64
  homepage: http://computacion.cs.cinvestav.mx/~ibuitron/
62
65
  licenses:
63
66
  - MIT
@@ -84,5 +87,7 @@ signing_key:
84
87
  specification_version: 4
85
88
  summary: PathgraphEnconding is a library to encode Pathgraph keys to ASN.1
86
89
  test_files:
90
+ - test/test_pathgraph.rb
87
91
  - test/test_pathgraph_privatekey.rb
92
+ - test/test_pathgraph_publickey.rb
88
93
  has_rdoc: