schnorr 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: 7cd8574f53f2db668bf4faf56f69cd2339b6366dd2063ceb1774ea62e4da534c
4
+ data.tar.gz: 556a46fe20dfc6e733f5eb1a9809b32a097afcaca97834074edccee408ee992c
5
+ SHA512:
6
+ metadata.gz: 8e97068a47a5c3bf6fc1d1e2aa650a073bfbe0ea7cd67b00b30e8051bf6a1773e7d418ee07840c366df84050367fe2ec832d3ef00c655076ec50911eb106255b
7
+ data.tar.gz: '0182ad307f20f2c3752cd23d9b1e9e3c78681601a493f895ea9e07e69102c3d6a9820c50678d11ee0d16ec584e9e2e57bc09f949c712ebe2cf9af6c9a33d92bc'
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
+ schnorrrb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.6.2
4
+ - 2.5.5
5
+
6
+ bundler_args: --jobs=2
7
+
8
+ script:
9
+ - bundle exec rake spec
@@ -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@haw.co.jp. 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 schnorrrb.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018-2019 Chaintope, Inc.
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,85 @@
1
+ # Schnorrrb [![Build Status](https://travis-ci.org/chaintope/schnorrrb.svg?branch=master)](https://travis-ci.org/chaintope/schnorrrb) [![Gem Version](https://badge.fury.io/rb/schnorrrb.svg)](https://badge.fury.io/rb/schnorrrb) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
2
+
3
+ This is a Ruby implementation of the Schnorr signature scheme over the elliptic curve.
4
+ This implementation relies on the [ecdsa gem](https://github.com/DavidEGrayson/ruby_ecdsa) for operate elliptic curves.
5
+
6
+ The code is based upon the initial proposal of Pieter Wuille's [bip-schnorr](https://github.com/sipa/bips/blob/bip-schnorr/bip-schnorr.mediawiki).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'schnorr'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install schnorr
23
+
24
+ ## Usage
25
+
26
+ ### Singing
27
+
28
+ ```ruby
29
+ require 'schnorr'
30
+
31
+ private_key = 0xB7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF
32
+
33
+ message = ['5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C'].pack('H*')
34
+
35
+ # create signature
36
+ signature = Schnorr.sign(message, private_key)
37
+
38
+ # signature r value
39
+ signature.r
40
+
41
+ # signature s value
42
+ signature.s
43
+
44
+ # convert signature to binary
45
+
46
+ signature.encode
47
+
48
+ ```
49
+
50
+ ### Verification
51
+
52
+ ```ruby
53
+ require 'schnorr'
54
+
55
+ public_key = ['03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B'].pack('H*')
56
+
57
+ signature = [`00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BE00880371D01766935B92D2AB4CD5C8A2A5837EC57FED7660773A05F0DE142380`].pack('H*')
58
+
59
+ message = ['5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C'].pack('H*')
60
+
61
+ # verify signature.(result is true or false)
62
+ result = Schnorr.valid_sig?(message, public_key, signature)
63
+
64
+ # signature convert to Signature object
65
+ sig = Schnorr::Signature.decode(signature)
66
+ ```
67
+
68
+ ### Change elliptic curve
69
+
70
+ This library use `secp256k1` curve as default. If you use another curve, you need to specify curve as following:
71
+
72
+ ```ruby
73
+ Schnorr.sign(message, private_key, group: ECDSA::Group::xxx)
74
+
75
+ Schnorr.valid_sig?(message, public_key, signature, group: ECDSA::Group::xxx)
76
+ ```
77
+
78
+ Note: But this library has only been tested with `secp256k1`. So another curve are not tested.
79
+
80
+ ## TODO
81
+
82
+ The following is unimplemented now.
83
+
84
+ * batch verification.
85
+ * MuSig
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 "schnorr"
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/schnorr.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'ecdsa'
2
+ require_relative 'schnorr/signature'
3
+
4
+ module Schnorr
5
+
6
+ module_function
7
+
8
+ # Generate schnorr signature.
9
+ # @param message (String) A message to be signed with binary format.
10
+ # @param private_key (Integer) The private key.
11
+ # (The number of times to add the generator point to itself to get the public key.)
12
+ # @param group (ECDSA::Group) The curve that is being used.
13
+ # @return (Schnorr::Signature)
14
+ def sign(message, private_key, group: ECDSA::Group::Secp256k1)
15
+ secret = ECDSA::Format::IntegerOctetString.encode(private_key, group.byte_length)
16
+ field = ECDSA::PrimeField.new(group.order)
17
+ k = field.mod(ECDSA::Format::IntegerOctetString.decode(Digest::SHA256.digest(secret + message)))
18
+ raise 'Creation of signature failed. k is zero' if k.zero?
19
+
20
+ r_point = group.new_point(k)
21
+ unless ECDSA::PrimeField.jacobi(r_point.y, group.field.prime) == 1
22
+ k = group.order - k
23
+ end
24
+
25
+ e = create_challenge(r_point.x, group.new_point(private_key), message, field, group)
26
+
27
+ Schnorr::Signature.new(r_point.x, field.mod(k + e * private_key))
28
+ end
29
+
30
+ # Verifies the given {Signature} and returns true if it is valid.
31
+ # @param message (String) A message to be signed with binary format.
32
+ # @param public_key (String) The public key with binary format.
33
+ # @param signature (String) The signature with binary format.
34
+ # @param group (ECDSA::Group) The curve that is being used.
35
+ # @return (Boolean) whether signature is valid.
36
+ def valid_sig?(message, public_key, signature, group: ECDSA::Group::Secp256k1)
37
+ check_sig!(message, public_key, signature, group)
38
+ rescue InvalidSignatureError, ECDSA::Format::DecodeError
39
+ false
40
+ end
41
+
42
+ # Verifies the given {Signature} and raises an {InvalidSignatureError} if it is invalid.
43
+ # @param message (String) A message to be signed with binary format.
44
+ # @param public_key (String) The public key with binary format.
45
+ # @param signature (String) The signature with binary format.
46
+ # @param group (ECDSA::Group) The curve that is being used.
47
+ # @return true
48
+ def check_sig!(message, public_key, signature, group)
49
+ sig = Schnorr::Signature.decode(signature)
50
+ pubkey = ECDSA::Format::PointOctetString.decode(public_key, group)
51
+ field = group.field
52
+
53
+ raise Schnorr::InvalidSignatureError, 'Invalid signature: r is not in the field.' unless field.include?(sig.r)
54
+ raise Schnorr::InvalidSignatureError, 'Invalid signature: s is not in the field.' unless field.include?(sig.s)
55
+ raise Schnorr::InvalidSignatureError, 'Invalid signature: r is zero.' if sig.r.zero?
56
+ raise Schnorr::InvalidSignatureError, 'Invalid signature: s is zero.' if sig.s.zero?
57
+
58
+ e = create_challenge(sig.r, pubkey, message, field, group)
59
+
60
+ r = group.new_point(sig.s) + pubkey.multiply_by_scalar(e).negate
61
+
62
+
63
+ if r.infinity? || ECDSA::PrimeField.jacobi(r.y, group.field.prime) != 1 || r.x != sig.r
64
+ raise Schnorr::InvalidSignatureError, 'signature verification failed.'
65
+ end
66
+
67
+ true
68
+ end
69
+
70
+ def create_challenge(x, pubkey, message, field, group)
71
+ r = ECDSA::Format::IntegerOctetString.encode(x, group.byte_length)
72
+ public_key = ECDSA::Format::PointOctetString.encode(pubkey, compression: true)
73
+ field.mod(ECDSA.normalize_digest(Digest::SHA256.digest(r + public_key + message), group.bit_length))
74
+ end
75
+
76
+ private_class_method :create_challenge
77
+
78
+ end
@@ -0,0 +1,38 @@
1
+ module Schnorr
2
+
3
+ class InvalidSignatureError < StandardError; end
4
+
5
+ # Instances of this class represents Schnorr signatures,
6
+ # which are simply a pair of integers named `r` and `s`.
7
+ class Signature
8
+
9
+ attr_reader :r
10
+ attr_reader :s
11
+
12
+ # @param r (Integer) the value of r.
13
+ # @param s (Integer) the value of s.
14
+ def initialize(r, s)
15
+ @r, @s = r, s
16
+ r.is_a?(Integer) or raise ArgumentError, 'r is not an integer.'
17
+ s.is_a?(Integer) or raise ArgumentError, 's is not an integer.'
18
+ end
19
+
20
+ # Parse a string to a {Signature}.
21
+ # @param string (String) signature string with binary format.
22
+ # @return (Signature) signature instance.
23
+ def self.decode(string)
24
+ raise InvalidSignatureError, 'Invalid schnorr signature length.' unless string.bytesize == 64
25
+ r = string[0...32].unpack('H*').first.to_i(16)
26
+ s = string[32..-1].unpack('H*').first.to_i(16)
27
+ new(r, s)
28
+ end
29
+
30
+ # Encode signature to string.
31
+ # @return (String) encoded signature.
32
+ def encode
33
+ ECDSA::Format::IntegerOctetString.encode(r, 32) + ECDSA::Format::IntegerOctetString.encode(s, 32)
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,3 @@
1
+ module Schnorr
2
+ VERSION = "0.1.0"
3
+ end
data/schnorrrb.gemspec ADDED
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "schnorr/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schnorr"
8
+ spec.version = Schnorr::VERSION
9
+ spec.authors = ["azuchi"]
10
+ spec.email = ["azuchi@haw.co.jp"]
11
+
12
+ spec.summary = %q{The ruby implementation of Schnorr signature.}
13
+ spec.description = %q{The ruby implementation of Schnorr signature.}
14
+ spec.homepage = "https://github.com/chaintope/schnorrrb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ spec.add_runtime_dependency "ecdsa", "~> 1.2.0"
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schnorr
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-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ecdsa
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.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 of Schnorr signature.
70
+ email:
71
+ - azuchi@haw.co.jp
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/schnorr.rb
89
+ - lib/schnorr/signature.rb
90
+ - lib/schnorr/version.rb
91
+ - schnorrrb.gemspec
92
+ homepage: https://github.com/chaintope/schnorrrb
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.0.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: The ruby implementation of Schnorr signature.
115
+ test_files: []