murky_waters 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
+ SHA1:
3
+ metadata.gz: de7e1e125358b97341f115548b95ca34ab2ebc55
4
+ data.tar.gz: 840f25e5a769c54375602c0fe4f57963f74136ca
5
+ SHA512:
6
+ metadata.gz: c96dcc782f362b34e515310fffd874e4bf6003f839f8f4e291f438583d3e4c739f7dec22b774475f12d455a8eb03c3c51110e9e63458634628875d24716e4d44
7
+ data.tar.gz: 639cba30e0f6060d756a7a0040fdf6adec676cbbee08f7cdd5e253fd13afd102ef50d44f9c226fc26ecc39f3b3726d1eb4d1ab4bc1d205215304dd72b2897a88
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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 wouter@youdo.co.nz. 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 murky_waters.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # MurkyWaters
2
+
3
+ MurkyWaters is a simple implementation of a merkle tree backed dictionary.
4
+
5
+ You can use this as an you would any ordinary dictionary structure with the added functionality
6
+ that you can generate verifiable proofs of the dictionary structure and presence of leaf nodes in the dictionary. Proofs for individual leaves can exist with zero-knowledge of the contents of other leaves in the dictionary.
7
+
8
+ In short, this can allow you to provide proof of knowledge of some subset of a larger set of data without leaking any information.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'murky_waters'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install murky_waters
25
+
26
+ ## Usage
27
+
28
+ To construct a new Merkle tree backed ordered dictionary
29
+
30
+ ### Basics
31
+ ```ruby
32
+ # A new, empty Merkle tree dictionary
33
+ dict = Murky::Dict()
34
+
35
+ # Specify a custom backing dictionary, default implementation is Hash
36
+ dict = Murky::Dict(data: acts_like_a_dictionary)
37
+
38
+ # Specify a custom digest class, must #respond_to?(:digest)
39
+ dict = Murky::Dict(digest: Digest::SHA256)
40
+ ```
41
+
42
+ To accesss, add and remove data to be indexed in our dict
43
+
44
+ ```ruby
45
+ dict["hello"] = "world" # Add data
46
+ dict.delete("hello") # Delete data
47
+ dict["hello"] # Retrieve data
48
+ dict.siblings("hello") # => [
49
+ # "OLpNd02wZa5e2XMKG/8rFMUKFVUq/yy6F+c3Rd09eKc=\n",
50
+ # "GySoYGQjpOMz62o7F56Al67mVjB9IP5GxpBmbQvp3wc=\n"
51
+ # ]
52
+
53
+ dict.root # "anajKi18I3C9TlVEcU//hZsw9i4sknlYCTspTQXxCr0=\n" # The merkle root/signature of our entire dictionary contents
54
+ ```
55
+
56
+ ### Proofs
57
+ ```ruby
58
+ # Generate a proof that "hello" exists inside of our dictionary and a merkle root/signature for our entire dictionary contents
59
+ dict.proof("hello")
60
+ # => #<Murky::Proof:0x007f9d1ef5c810
61
+ # @digest=
62
+ # #<Digest::SHA256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855>,
63
+ # @root="anajKi18I3C9TlVEcU//hZsw9i4sknlYCTspTQXxCr0=\n",
64
+ # @siblings=
65
+ # ["47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
66
+ # "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
67
+ # "qZDReXdlGqrjRUZy3nSfWXY6y8KYxwb+A7K6/Xg7Nxc=\n",
68
+ # "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
69
+ # "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
70
+ # "jqwjgWe5ZVi9USkRcycxwiXtFtSLbycfKHJRJ8Scm3A=\n",
71
+ # "ujOfV44N03rF/+m6+eONFRPJYmstEnhsh2YcKdIUyVU=\n"],
72
+ # @valid=true>
73
+ ```
74
+ ### Verification
75
+ ```ruby
76
+ # Perform a verification for a root, and a merkle path/sibling list and some value.
77
+ It verifies that this value resides in the dictionary represented by our root signature. From this we can conclude that the size, shape and order of the tree for this merkle root are unchanged from when this proof was generated and that our value does indeed exist within the dictionary.
78
+
79
+ # Murky.verify(root, siblings, value) # => true/false
80
+ Murky.verify(
81
+ "EpU7Zx9tzTCGGyQtNQgC5Iu8IxRlXFjwG7KCjqfuRwI=\n",
82
+ [
83
+ "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
84
+ "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
85
+ "qZDReXdlGqrjRUZy3nSfWXY6y8KYxwb+A7K6/Xg7Nxc=\n",
86
+ "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
87
+ "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n",
88
+ "jqwjgWe5ZVi9USkRcycxwiXtFtSLbycfKHJRJ8Scm3A=\n",
89
+ "ujOfV44N03rF/+m6+eONFRPJYmstEnhsh2YcKdIUyVU=\n"
90
+ ],
91
+ "world"
92
+ ) # => true if verification passes
93
+ ```
94
+ A tree can optionally be backed by any dictionary like data structure to store the real leaf data.
95
+ The prerequisites of this structure are that:
96
+ * It is hash-like (implements #[]= and #[])
97
+ * It is ordered
98
+ * It implements #keys
99
+ * It implements #values
100
+
101
+ You can pass this structure into the constuctor of the Murky::Dict. E.g
102
+
103
+ ```ruby
104
+ Murky::Dict(data: {foo: :bar})
105
+ ```
106
+ ### Saving and restoring proofs
107
+
108
+ Proofs can be saved to, and restored from a file.
109
+ E.g
110
+
111
+ ```ruby
112
+ dict.proof(:my_value).output("/Users/pico/Desktop/proof.txt")
113
+
114
+ proof = Murky::Proof.from_file("/Users/pico/Desktop/proof.txt")
115
+ proof.valid? #=> true
116
+ ```
117
+
118
+ ## Development
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wouterken/murky_waters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
127
+
128
+ ## Code of Conduct
129
+
130
+ Everyone interacting in the MurkyWaters project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/murky_waters/blob/master/CODE_OF_CONDUCT.md).
131
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "murky"
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
data/lib/murky/dict.rb ADDED
@@ -0,0 +1,100 @@
1
+ module Murky
2
+ class Dict
3
+ attr_reader :root
4
+
5
+ def initialize(data: {}, digest: Digest::SHA256.new)
6
+ @root = nil
7
+ @digest = digest
8
+ @changed = !data.size.zero?
9
+ define_singleton_method(:data, ->{data})
10
+ end
11
+
12
+ def keys
13
+ data.keys
14
+ end
15
+
16
+ def values
17
+ data.values
18
+ end
19
+
20
+ def include?(key)
21
+ data.include?(key)
22
+ end
23
+
24
+ def height
25
+ Math.ceil(Math.log(@size, 2))
26
+ end
27
+
28
+ def sign(value)
29
+ @digest.digest(value.to_s)
30
+ end
31
+
32
+ def []=(key, value)
33
+ @changed = true
34
+ data[key] = value
35
+ end
36
+
37
+ def [](key)
38
+ data[key]
39
+ end
40
+
41
+ def delete(key)
42
+ @changed = true
43
+ data.delete(key)
44
+ end
45
+
46
+ def root
47
+ @root = @changed ? compute_root : @root
48
+ end
49
+
50
+ def proof(key)
51
+ Proof.new({
52
+ root: root,
53
+ siblings: siblings(key),
54
+ signature: sign(data[key]),
55
+ digest: @digest
56
+ })
57
+ end
58
+
59
+ def siblings(key)
60
+ return [] if !include?(key)
61
+ signature = sign(data[key])
62
+ siblings = []
63
+ compute_root do |left_sig, right_sig|
64
+ case
65
+ when left_sig === signature then siblings << right_sig
66
+ when right_sig === signature then siblings << left_sig
67
+ else next
68
+ end
69
+ signature = sign(Murky.xor(left_sig,right_sig))
70
+ end
71
+ return siblings
72
+ end
73
+
74
+ def to_s
75
+ "Murky::Dict(#{data.to_s})"
76
+ end
77
+
78
+ def inspect
79
+ "Murky::Dict(#{data.inspect})"
80
+ end
81
+
82
+ private
83
+ def compute_root(values=self.values, from_bottom=0, &block)
84
+ if values.length == 1
85
+ @changed = false
86
+ return sign(values[0])
87
+ end
88
+ compute_root(values.each_slice(2).map do |left, right|
89
+ ls, rs = sign(left),sign(right)
90
+ block[ls, rs] if block_given?
91
+ Murky.xor(ls, rs)
92
+ end, from_bottom + 1, &block)
93
+ end
94
+ end
95
+
96
+ module_function
97
+ def Dict(*args)
98
+ Dict.new(*args)
99
+ end
100
+ end
@@ -0,0 +1,53 @@
1
+ module Murky
2
+ class Proof
3
+ attr_reader :root, :siblings, :signature
4
+
5
+ FILE_HEADER = "#{?= * 20} BEGIN MURKY PROOF #{?= * 20} "
6
+ FILE_FOOTER = "#{?= * 20} BEGIN MURKY PROOF #{?= * 20} "
7
+ INPUT_REGEXP = %r{#{FILE_HEADER}\n(.*?)\n#{FILE_FOOTER}}m
8
+
9
+ def initialize(root:, siblings:, signature:, digest:)
10
+ @digest = digest
11
+ @root = root
12
+ @siblings = siblings
13
+ @signature = signature
14
+ @valid = root == siblings.reduce(signature) do |mem, sibling|
15
+ sign Murky.xor(mem,sibling)
16
+ end
17
+ end
18
+
19
+ def sign(value)
20
+ @digest.digest(value.to_s)
21
+ end
22
+
23
+ def valid?
24
+ @valid
25
+ end
26
+
27
+ def self.base64_encode(value)
28
+ Base64.encode64(value)
29
+ end
30
+
31
+ def self.base64_decode(value)
32
+ Base64.decode64(value)
33
+ end
34
+
35
+ def output(filename)
36
+ IO.write(
37
+ filename,
38
+ "#{FILE_HEADER}\n" <<
39
+ (
40
+ [self.class.base64_encode(root)] +
41
+ siblings.map(&self.class.method(:base64_encode)) +
42
+ [self.class.base64_encode(@signature)]
43
+ ).join <<
44
+ "#{FILE_FOOTER}"
45
+ )
46
+ end
47
+
48
+ def self.from_file(filename, digest: Digest::SHA256)
49
+ root, *siblings, signature = IO.read(filename)[INPUT_REGEXP, 1].split("\n").map(&method(:base64_decode))
50
+ Murky::Proof.new(root: root, siblings: siblings, signature: signature, digest: digest)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Murky
2
+ VERSION = "0.1.0"
3
+ end
data/lib/murky.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'murky/version'
2
+ require 'murky/dict'
3
+ require 'murky/proof'
4
+ require 'base64'
5
+
6
+ module Murky
7
+ module_function
8
+ def verify(root, siblings, value, digest: Digest::SHA256.new)
9
+ Proof.new(
10
+ root: root,
11
+ siblings: siblings,
12
+ signature: digest.digest(value.to_s),
13
+ digest: digest
14
+ ).valid?
15
+ end
16
+
17
+ def xor(s1, s2)
18
+ s2, s1 = [s1, s2].sort_by(&:bytesize)
19
+ s1.bytes.zip(s2.bytes).map do |x,y|
20
+ ((x || 0) ^ (y || 0)).chr
21
+ end.join
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "murky/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "murky_waters"
8
+ spec.version = Murky::VERSION
9
+ spec.authors = ["Wouter Coppieters"]
10
+ spec.email = ["wouter@youdo.co.nz"]
11
+
12
+ spec.summary = "Merkle Tree backed Dictionary"
13
+ spec.description = "A simple implementation of a Merkle Tree backed Dictionary"
14
+ spec.homepage = "https://github.com/wouterken/murky_waters"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # spec.metadata["allowed_push_host"] = "http://mygemserver.com"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ spec.add_runtime_dependency "pry"
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~>5.8.4"
30
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: murky_waters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wouter Coppieters
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
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: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
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: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.8.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.8.4
69
+ description: A simple implementation of a Merkle Tree backed Dictionary
70
+ email:
71
+ - wouter@youdo.co.nz
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/murky.rb
84
+ - lib/murky/dict.rb
85
+ - lib/murky/proof.rb
86
+ - lib/murky/version.rb
87
+ - murky_waters.gemspec
88
+ homepage: https://github.com/wouterken/murky_waters
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Merkle Tree backed Dictionary
111
+ test_files: []