utreexo 0.1.0

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
+ SHA256:
3
+ metadata.gz: 2db0a1b0bb2ecdc1be9477aee62cce16fc9ea24f1febd7c877f819a986ce6250
4
+ data.tar.gz: c35c3beafff28204c419f650236856b53e89a87e56270886976742718f399fd3
5
+ SHA512:
6
+ metadata.gz: 183873eaba066f361498edd47c1e94edc9d6d3bc891babb1443775dc45de345759c20de657e22eea7d2fcab1554228462675bc7cc58ab5939006c805772a1db0
7
+ data.tar.gz: 885eeb50daefb5b4cea34fc8463c17bed69e51577e92b2eaec2b24111ed3cf51cbc6606ef763d7e501b3a92455b0b2c00ec82bcc6eded81663116211837cf091
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ utreexorb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.5
4
+ - 2.5.5
5
+ - 2.6.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at azuchi@chaintope.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in utreexo.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 azuchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Utreexorb [![Build Status](https://travis-ci.org/chaintope/utreexorb.svg?branch=master)](https://travis-ci.org/chaintope/utreexorb) [![Gem Version](https://badge.fury.io/rb/utreexorb.svg)](https://badge.fury.io/rb/utreexorb) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
2
+
3
+ This library is a Ruby implementation of [Utreexo](https://github.com/mit-dci/utreexo/blob/master/utreexo.pdf).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'utreexo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install utreexo
20
+
21
+ ## Usage
22
+
23
+ ### Add element to forest.
24
+
25
+ ```ruby
26
+ require 'utreexo'
27
+
28
+ # initialize forest.
29
+
30
+ f = Utreexo::Forest.new
31
+
32
+ # add element to forest.
33
+ f.add('a00000aa00000000000000000000000000000000000000000000000000000000')
34
+ f.add('a00100aa00000000000000000000000000000000000000000000000000000000')
35
+ f.add('a00200aa00000000000000000000000000000000000000000000000000000000')
36
+ f.add('a00300aa00000000000000000000000000000000000000000000000000000000')
37
+ f.add('a00400aa00000000000000000000000000000000000000000000000000000000')
38
+
39
+ # forest has 2 tree, height 2, height 0
40
+ # accumulator root for height 2 tree
41
+ f.acc[2]
42
+ => '2d043522d1fc5adfa966a889492acc8b4f924869e18192ad6f4bcb30db6d56c0'
43
+ # accumulator root for height 0 tree
44
+ f.acc[0]
45
+ => 'a00400aa00000000000000000000000000000000000000000000000000000000'
46
+ ```
47
+
48
+ ### Verify element.
49
+
50
+ ```ruby
51
+ # proof for 3rd element
52
+ proof = Utreexo::Proof.new(2, 'a00200aa00000000000000000000000000000000000000000000000000000000',
53
+ ['a00300aa00000000000000000000000000000000000000000000000000000000', '736b3e12120637186a0a8eef8ce45ed69b39119182cc749b793f05de3996f464'])
54
+
55
+ f.include?(proof)
56
+ => true
57
+ ```
58
+
59
+ ### Remove element from forest.
60
+
61
+ ```ruby
62
+ proof = Utreexo::Proof.new(2, 'a00200aa00000000000000000000000000000000000000000000000000000000',
63
+ ['a00300aa00000000000000000000000000000000000000000000000000000000', '736b3e12120637186a0a8eef8ce45ed69b39119182cc749b793f05de3996f464'])
64
+ f.remove(proof)
65
+
66
+ # If delete 3rd element, last item move to 3rd element position, and root hash changed.
67
+ f.acc[2]
68
+ => '5fd725b67d4651a8d5153bfea9242322f2d96f152ba3cf9cbce2a7ba694ca0e6'
69
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "utreexo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,86 @@
1
+ module Utreexo
2
+
3
+ class Forest
4
+
5
+ # number of leaves in the forest (bottom row)
6
+ attr_accessor :num_leaves
7
+
8
+ # accumulator
9
+ attr_accessor :acc
10
+
11
+ def initialize
12
+ @height = 0
13
+ @num_leaves = 0
14
+ @forest = []
15
+ @acc = []
16
+ end
17
+
18
+ # Add element to forest.
19
+ # @param [String] leaf an element hash to be added with hex format.
20
+ def add(leaf)
21
+ n = leaf
22
+ h = 0
23
+ r = acc[h]
24
+ until r.nil? do
25
+ n = parent(r, n)
26
+ acc[h] = nil
27
+ h += 1
28
+ r = acc[h]
29
+ end
30
+ acc[h] = n
31
+ @num_leaves += 1
32
+ end
33
+
34
+ # Delete element from forest.
35
+ # @param [Utreexo::Proof] proof the proof of element to be removed.
36
+ def remove(proof)
37
+ raise Utreexo::Error, 'The target element does not exist in the forest.' unless include?(proof)
38
+ n = nil
39
+ h = 0
40
+ while h < proof.siblings.length do
41
+ p = proof.siblings[h]
42
+ if !n.nil?
43
+ n = parent(p, n)
44
+ elsif acc[h].nil?
45
+ acc[h] = p
46
+ else
47
+ n = proof.right? ? parent(p, acc[h]) : parent(acc[h], p)
48
+ acc[h] = nil
49
+ end
50
+ h += 1
51
+ end
52
+ acc[h] = n
53
+ end
54
+
55
+ # Whether the element exists in the forest
56
+ # @param [Utreexo::Proof] proof the proof of element
57
+ # @return [Boolean]
58
+ def include?(proof)
59
+ root = acc[proof.siblings.length]
60
+ n = proof.payload
61
+ proof.siblings.each_with_index do |sibling, height|
62
+ if ((1<<height) & proof.position) == 0
63
+ n = parent(n, sibling)
64
+ else
65
+ n = parent(sibling, n)
66
+ end
67
+ end
68
+ n == root
69
+ end
70
+
71
+ # get current height of the highest tree
72
+ # @return [Integer] current height of the highest tree
73
+ def height
74
+ i = acc.reverse.find_index{|i|!i.nil?}
75
+ i ||= 0
76
+ acc.length - i
77
+ end
78
+
79
+ private
80
+
81
+ def parent(left, right)
82
+ Blake2b.hex([left + right].pack('H*'))
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,31 @@
1
+ module Utreexo
2
+ class Proof
3
+
4
+ attr_reader :position # where at the bottom of the tree it sits
5
+ attr_reader :payload # hash of the thing itself (what's getting proved)
6
+ attr_reader :siblings # hash of siblings up to a root
7
+
8
+ # initialize
9
+ # @param [Integer] position Where at the bottom of the tree it sits
10
+ # @param [String] payload Target element
11
+ # @param [Array[String]] siblings proofs
12
+ def initialize(position, payload, siblings = [])
13
+ @position = position
14
+ @payload = payload
15
+ @siblings = siblings
16
+ end
17
+
18
+ # Whether the element is a node on the right
19
+ # @return [Boolean]
20
+ def right?
21
+ position.odd?
22
+ end
23
+
24
+ # Whether the element is a node on the left
25
+ # @return [Boolean]
26
+ def left?
27
+ position.even?
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Utreexo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/utreexo.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "utreexo/version"
2
+ require 'digest/sha2'
3
+ require 'blake2b'
4
+
5
+ module Utreexo
6
+ class Error < StandardError; end
7
+
8
+ autoload :Forest, 'utreexo/forest'
9
+ autoload :Proof, 'utreexo/proof'
10
+
11
+ end
data/utreexorb.gemspec ADDED
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "utreexo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "utreexo"
8
+ spec.version = Utreexo::VERSION
9
+ spec.authors = ["azuchi"]
10
+ spec.email = ["azuchi@chaintope.com"]
11
+
12
+ spec.summary = %q{The ruby implementation for Utreexo.}
13
+ spec.description = %q{The ruby implementation for Utreexo.}
14
+ spec.homepage = "https://github.com/chaintope/utreexorb"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency 'blake2b'
27
+
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utreexo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - azuchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: blake2b
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: The ruby implementation for Utreexo.
70
+ email:
71
+ - azuchi@chaintope.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-gemset"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/utreexo.rb
89
+ - lib/utreexo/forest.rb
90
+ - lib/utreexo/proof.rb
91
+ - lib/utreexo/version.rb
92
+ - utreexorb.gemspec
93
+ homepage: https://github.com/chaintope/utreexorb
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: The ruby implementation for Utreexo.
116
+ test_files: []