pathgraph_encoding 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61f5140f2543a7cabf91ad37ca4caa617441264c
4
+ data.tar.gz: 1d30966e594ad575883be4d4f4d98f99aa58168a
5
+ SHA512:
6
+ metadata.gz: 60408328d70ee527ff7e9e0d95d46e66f5ccbf0775bcf35494927fd5bcb92c4eb6a97a1161ec204cffcc3448b1f18a1b095b682561a7e46c76b3eb6e61e4b44d
7
+ data.tar.gz: cd58cf35f08d76b96967a52ab52c1cd8c878d5bf1f4b2733c98eb253a22b5d167770e480cf7d1592f19fbb8b65a4ed097be5abe13896f7eb7853b0481c645cdb
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ pathgraph_encoding
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pathgraph_encoding.gemspec
4
+ gemspec
5
+
6
+ gem 'yard', '~> 0.8.7.6'
7
+ gem 'minitest', '~> 5.6.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/PathGraphs.asn ADDED
@@ -0,0 +1,24 @@
1
+ PathGraph DEFINITIONS IMPLICIT TAGS ::= BEGIN
2
+ version INTEGER,
3
+
4
+ PrivateKey ::= SEQUENCE {
5
+ n INTEGER (4..8),
6
+ pi SEQUENCE OF MSequence
7
+ }
8
+
9
+ PublicKey ::= SEQUENCE {
10
+ n INTEGER (4..8),
11
+ k INTEGER,
12
+ m INTEGER,
13
+ phi SEQUENCE OF MSequence
14
+ }
15
+
16
+ MSequence ::= SEQUENCE {
17
+ pgVertex PGVertex
18
+ }
19
+
20
+ PGVertex ::= SEQUENCE {
21
+ qn_vertex INTEGER
22
+ }
23
+
24
+ END
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # PathgraphEncoding
2
+
3
+ PathgraphEnconding is a library to encode and decode Pathgraph keys (public and private) to [ASN.1 DER format](http://luca.ntop.org/Teaching/Appunti/asn1.html "A Layman's Guide to a Subset of ASN.1, BER, and DER").
4
+
5
+ Pathgraph ANS.1 descriptor file is `PathGraphs.asn`.
6
+
7
+ ## Installation
8
+
9
+ ### Prerequisites
10
+
11
+ This a Ruby library packed into a [gem](http://guides.rubygems.org/what-is-a-gem/ "RubyGems Guides"), so you need Ruby interpreter installed, it is strongly recommended to use at least [Ruby 2.1.0](http://ruby-doc.org/core-2.1.0/ "Ruby 2.1.0 core documentation").
12
+
13
+ ### Library installation
14
+
15
+ Add this line to your application's [Gemfile](http://bundler.io/v1.5/gemfile.html "Bundler documentation"):
16
+
17
+ ```ruby
18
+ gem 'pathgraph_encoding'
19
+ ```
20
+
21
+ then execute in shell:
22
+
23
+ ```bash
24
+ $ bundle
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```bash
30
+ $ gem install pathgraph_encoding
31
+ ```
32
+
33
+ ### Testing
34
+
35
+ This library has included a little suite of test cases to verify library algorithmic correctness. So it is suggested to execute tests before install or use this gem.
36
+
37
+ #### Executing tests
38
+
39
+ You can execute test cases with `rake` command:
40
+
41
+ ```bash
42
+ $ rake test
43
+ ```
44
+
45
+ ## Library usage
46
+
47
+ This library has two specific tasks: encoding and decoding keys.
48
+
49
+ ### Include library
50
+
51
+ You must include this library in your source code file, adding at the top a line like this:
52
+
53
+ ```ruby
54
+ require 'pathgraph_encoding'
55
+ ```
56
+
57
+ ### Encoding a private key
58
+
59
+ Given an instance of a private key, like this:
60
+
61
+ ```ruby
62
+ # Your private key
63
+ key = {
64
+ n: 4,
65
+ pi: [
66
+ [0,1,3,2],
67
+ [6,7,5,4],
68
+ [12,13,15,14],
69
+ [10,11,9,8]
70
+ ]
71
+ }
72
+ ```
73
+
74
+ call `PrivateKey::to_der` method:
75
+
76
+ ```ruby
77
+ der = PathgraphEncoding::PrivateKey::to_der(key)
78
+ ```
79
+
80
+ ### Decoding a private key
81
+
82
+ Given an instance of a DER encoded key:
83
+
84
+ ```ruby
85
+ der = "0F\x13\x050.0.10=\x02\x01\x04080\f\x02\x01\x00"\
86
+ "\x02\x01\x01\x02\x01\x03\x02\x01\x020\f\x02\x01"\
87
+ "\x06\x02\x01\a\x02\x01\x05\x02\x01\x040\f\x02\x01"\
88
+ "\f\x02\x01\r\x02\x01\x0F\x02\x01\x0E0\f\x02\x01\n"\
89
+ "\x02\x01\v\x02\x01\t\x02\x01\b"
90
+ ```
91
+
92
+ call `PrivateKey::from_der` method:
93
+
94
+ ```ruby
95
+ key = PathgraphEncoding::PrivateKey::from_der(der)
96
+ ```
97
+
98
+ it will return a key like:
99
+
100
+ ```ruby
101
+ key = {
102
+ version: "0.0.1",
103
+ n: 4,
104
+ pi: [[0,1,3,2],[6,7,5,4],[12,13,15,14],[10,11,9,8]]
105
+ }
106
+ ```
107
+
108
+
109
+ ## Contributing
110
+
111
+ I will be grateful to have your contributions.
112
+ Feel free to make modifications, fix bugs or add new features.
113
+
114
+ 1. Fork the project (https://gitlab.com/israelbuitron/pathgraph_encoding)
115
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
116
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
117
+ 4. Push to the branch (`git push origin my-new-feature`)
118
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/test_*.rb"
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,209 @@
1
+ require "pathgraph_encoding/version"
2
+ require 'openssl'
3
+ require 'pp'
4
+
5
+ # @author Israel Buitron
6
+ module PathgraphEncoding
7
+
8
+ # Represents a pathgraph private key.
9
+ #
10
+ # An instance of a private key can be like this:
11
+ #
12
+ # key = {
13
+ # n: 4,
14
+ # pi: [[0,1,3,2],[6,7,5,4],[12,13,15,14],[10,11,9,8]]
15
+ # }
16
+ module PrivateKey
17
+
18
+ # Compute DER-formatted bytes array of a pathgraph private key.
19
+ #
20
+ # == Parameters:
21
+ # key::
22
+ # A private key to encode.
23
+ #
24
+ # == Returns:
25
+ # A byte array representing the private key specified.
26
+ #
27
+ def self.to_der(key)
28
+ n = OpenSSL::ASN1::Integer.new(key[:n])
29
+ pi = OpenSSL::ASN1::Sequence.new(
30
+ key[:pi].map do |p|
31
+ arr = p.map { |x| OpenSSL::ASN1::Integer.new(x) }
32
+ OpenSSL::ASN1::Sequence.new(arr)
33
+ end
34
+ )
35
+ privateKey = OpenSSL::ASN1::Sequence.new([n,pi])
36
+
37
+ version = OpenSSL::ASN1::PrintableString.new("0.0.1")
38
+ instance = OpenSSL::ASN1::Sequence.new([version,privateKey])
39
+
40
+ instance.to_der
41
+ end
42
+
43
+ # Decode and build a instance of a pathgraph private key from a
44
+ # DER-formatted bytes array.
45
+ #
46
+ # == Parameters:
47
+ # der::
48
+ # A byte array representing the private key specified.
49
+ #
50
+ # == Returns:
51
+ # key::
52
+ # The private key dencoded.
53
+ #
54
+ def self.from_der(der)
55
+ asn1 = OpenSSL::ASN1.decode(der)
56
+ {
57
+ version: OpenSSL::ASN1.decode(asn1.value[0]).value,
58
+ n: OpenSSL::ASN1.decode(asn1.value[1].value[0]).value.to_i,
59
+ pi: OpenSSL::ASN1.decode(asn1.value[1].value[1]).value.map { |e| e.map { |d| d.value.to_i } }
60
+ }
61
+ end
62
+
63
+ # Check if is a valid private key.
64
+ #
65
+ # == Parameters:
66
+ # key::
67
+ # A byte array representing the private key specified.
68
+ #
69
+ # == Returns:
70
+ # Returns `true` if is a valid private key, otherwise
71
+ # `false`.
72
+ #
73
+ def self.is_valid?(key)
74
+ # Check valid hypercube degree
75
+ return false if is_valid_hypercube_degree(key[:n])
76
+
77
+ # Check valid hypercube vertices
78
+ # max_vertex = (1<<key[:n])-1
79
+ key[:pi].each do |pi|
80
+ pi.each do |vertex|
81
+ return false unless is_valid_hypercube_vertex?(vertex,key[:n])
82
+ end
83
+
84
+ pi[0...-1].each_with_index do |i,j|
85
+ return false unless is_adyacent_in_hp?(i,pi[j+1])
86
+ end
87
+ end
88
+
89
+ true
90
+ end
91
+
92
+ # Check if a number is a valid hypercube degree is an integer
93
+ # between 4 and 8.
94
+ #
95
+ # == Parameters:
96
+ # n::
97
+ # Hypercube degree.
98
+ #
99
+ # == Returns:
100
+ # If is a valid hypercube degree, returns `true`, otherwise
101
+ # `false`.
102
+ #
103
+ def self.is_valid_hypercube_degree?(n)
104
+ n >= 4 && n <= 8 && n.is_a?(Integer)
105
+ end
106
+
107
+ # Check if a number is a valid hypercube vertex.
108
+ #
109
+ # == Parameters:
110
+ # v::
111
+ # Hypercube vertex.
112
+ # n::
113
+ # Hypercube degree.
114
+ #
115
+ # == Returns:
116
+ # If is a valid hypercube vertex, returns `true`, otherwise
117
+ # `false`.
118
+ #
119
+ def self.is_valid_hypercube_vertex?(v,n)
120
+ v >= 0 && v <= (1<<n)-1 && v.is_a?(Integer)
121
+ end
122
+
123
+ # Check if two vertices are adjacent in a hypercube.
124
+ #
125
+ # == Parameters:
126
+ # a::
127
+ # First hypercube vertex.
128
+ # b::
129
+ # Second hypercube vertex.
130
+ #
131
+ # == Returns:
132
+ # If both are adjacent vertices, returns `true`, otherwise
133
+ # `false`.
134
+ #
135
+ def self.is_adyacent_in_hp?(a,b)
136
+ (a^b).to_s(2).count(1) == 1
137
+ end
138
+
139
+ def self.gen_public_key(key)
140
+ {
141
+ n: key[:n],
142
+ sigma: key[:pi].map! { |p| [p[0],p[-1]] }
143
+ }
144
+ end
145
+
146
+ end
147
+
148
+ # Represents a pathgraph public key.
149
+ module PublicKey
150
+
151
+ # Compute DER-formatted bytes array of a pathgraph public key.
152
+ #
153
+ # == Parameters:
154
+ # key::
155
+ # A public key to encode.
156
+ #
157
+ # == Returns:
158
+ # A byte array representing the public key specified.
159
+ #
160
+ def self.to_der(key)
161
+ n = OpenSSL::ASN1::Integer.new(key[:n])
162
+ # m = OpenSSL::ASN1::Integer.new(key[:m])
163
+ # k = OpenSSL::ASN1::Integer.new(key[:k])
164
+ sigma = OpenSSL::ASN1::Sequence.new(
165
+ key[:sigma].map do |sig|
166
+ arr = sig.map { |x| OpenSSL::ASN1::Integer.new(x) }
167
+ OpenSSL::ASN1::Sequence.new(arr)
168
+ end
169
+ )
170
+ publicKey = OpenSSL::ASN1::Sequence.new([n,sigma])
171
+
172
+ version = OpenSSL::ASN1::PrintableString.new("0.0.1")
173
+ instance = OpenSSL::ASN1::Sequence.new([version,publicKey])
174
+
175
+ instance.to_der
176
+ end
177
+
178
+ # Decode and build a instance of a pathgraph public key from a
179
+ # DER-formatted bytes array.
180
+ #
181
+ # == Parameters:
182
+ # der::
183
+ # A byte array representing the public key specified.
184
+ #
185
+ # == Returns:
186
+ # key::
187
+ # The public key dencoded.
188
+ #
189
+ def self.from_der(der)
190
+ # TODO: implement this method
191
+ end
192
+
193
+ # Check if is a valid public key.
194
+ #
195
+ # == Parameters:
196
+ # key::
197
+ # A byte array representing the public key specified.
198
+ #
199
+ # == Returns:
200
+ # Returns `true` if is a valid public key, otherwise
201
+ # `false`.
202
+ #
203
+ def self.is_valid?(key)
204
+ # TODO: implement this method
205
+ end
206
+
207
+ end
208
+
209
+ end
@@ -0,0 +1,4 @@
1
+ module PathgraphEncoding
2
+ # Pathgraph encoding library version
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'pathgraph_encoding/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pathgraph_encoding"
7
+ spec.version = PathgraphEncoding::VERSION
8
+ spec.authors = ["Israel Buitron"]
9
+ spec.email = ["ibuitron@ipn.mx"]
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.}
12
+ spec.homepage = "http://computacion.cs.cinvestav.mx/~ibuitron/"
13
+ spec.license = "MIT"
14
+ spec.metadata = { 'issue_tracker' => "https://gitlab.com/israelbuitron/pathgraph_encoding/issues" }
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake", '~> 10.4'
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'minitest/autorun'
2
+ require 'pathgraph_encoding'
3
+
4
+ class TestPathGraphPrivateKey < Minitest::Test
5
+
6
+ def setup
7
+ @private_key = {
8
+ n: 4,
9
+ pi: [[0,1,3,2],[6,7,5,4],[12,13,15,14],[10,11,9,8]]
10
+ }
11
+ @der_private_key = "0F\x13\x050.0.10=\x02\x01\x04080\f\x02\x01\x00"\
12
+ "\x02\x01\x01\x02\x01\x03\x02\x01\x020\f\x02\x01\x06\x02\x01\a\x02"\
13
+ "\x01\x05\x02\x01\x040\f\x02\x01\f\x02\x01\r\x02\x01\x0F\x02\x01"\
14
+ "\x0E0\f\x02\x01\n\x02\x01\v\x02\x01\t\x02\x01\b"
15
+ end
16
+
17
+ def test_to_der
18
+ der = PathgraphEncoding::PrivateKey::to_der(@private_key)
19
+ assert_equal 72, der.size
20
+ assert_equal @der_private_key, der
21
+ end
22
+
23
+ def test_from_der
24
+ der = "0*\x13\x050.0.10!\x02\x01\x040\x1C0\f\x02\x01\x01\x02\x01\x02\x02\x01\x03\x02\x01\x040\f\x02\x01\x05\x02\x01\x06\x02\x01\a\x02\x01\b"
25
+ key = PathgraphEncoding::PrivateKey::from_der(der)
26
+ exp = {version: "0.0.1", n: 4, pi: [[1, 2, 3, 4], [5, 6, 7, 8]]}
27
+ assert_equal key, exp
28
+ end
29
+
30
+ def test_is_valid
31
+ skip "TODO: implement 'is_valid' testcase"
32
+ end
33
+
34
+ def test_is_valid_hypercube_degree
35
+ (4..8).each do |i|
36
+ assert PathgraphEncoding::PrivateKey::is_valid_hypercube_degree?(i)
37
+ end
38
+ end
39
+
40
+ def test_is_adyacent_in_hp
41
+ skip "TODO: implement 'is_adyacent_in_hp' testcase"
42
+ end
43
+
44
+ def test_gen_public_key
45
+ expected = {
46
+ n: 4,
47
+ sigma: [[0,2],[6,4],[12,14],[10,8]]
48
+ }
49
+ key = PathgraphEncoding::PrivateKey::gen_public_key(@private_key)
50
+ assert_equal expected, key
51
+ end
52
+
53
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathgraph_encoding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Israel Buitron
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
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.
43
+ email:
44
+ - ibuitron@ipn.mx
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - PathGraphs.asn
55
+ - README.md
56
+ - Rakefile
57
+ - lib/pathgraph_encoding.rb
58
+ - lib/pathgraph_encoding/version.rb
59
+ - pathgraph_encoding.gemspec
60
+ - test/test_pathgraph_privatekey.rb
61
+ homepage: http://computacion.cs.cinvestav.mx/~ibuitron/
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ issue_tracker: https://gitlab.com/israelbuitron/pathgraph_encoding/issues
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: PathgraphEnconding is a library to encode Pathgraph keys to ASN.1
86
+ test_files:
87
+ - test/test_pathgraph_privatekey.rb
88
+ has_rdoc: