kzg 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: 4ef0ee512428138b4158c95a1ff67254af2926668a45e812df640725108a2d9b
4
+ data.tar.gz: e1673fafdefa436f0c10aa7639ba68431831777718a38ecd5374101847a41fea
5
+ SHA512:
6
+ metadata.gz: 7a78076f208b2397f6c2dddd271dbe3ed47ba0ae1794b06451647639b70989f10bc5f52d6cd3ef80fe9fd1fe49535bfa3fb4b168402c4a2c87be9ade0cebfb6b
7
+ data.tar.gz: dc12b60adf07dd975c05cf44c0b3c01002714d7d11e9f2cee340b1b89097b253386ae9109f4859d1eba28e267928f90f7afe9ea17449ccf8cfa25da293d4b281
@@ -0,0 +1,27 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ name: Ruby ${{ matrix.ruby }}
9
+ strategy:
10
+ matrix:
11
+ ruby:
12
+ - '3.0.5'
13
+ - '3.1.3'
14
+ - '3.2.0'
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby }}
21
+ bundler-cache: true
22
+ - name: prettier
23
+ run: bundle exec rbprettier --check '**/*.rb'
24
+ - name: rubocop
25
+ run: bundle exec rubocop .
26
+ - name: Run the default task
27
+ run: bundle exec rake
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
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.prettierignore ADDED
@@ -0,0 +1,2 @@
1
+ /bin
2
+ /vendor
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ inherit_gem:
5
+ prettier: rubocop.yml
6
+ AllCops:
7
+ TargetRubyVersion: 2.7
8
+ RSpec/ExampleLength:
9
+ Enabled: false
10
+ Metrics:
11
+ Enabled: false
12
+ RSpec/MultipleExpectations:
13
+ Max: 5
14
+ Style/IfUnlessModifier:
15
+ Enabled: false
16
+ Style/WhileUntilModifier:
17
+ Enabled: false
18
+ Naming/MethodParameterName:
19
+ Enabled: false
20
+ RSpec/FilePath:
21
+ Enabled: false
22
+ Layout/LineLength:
23
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ kzg
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-04-11
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at azuchi@chaintope.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in kzg.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'prettier'
13
+
14
+ gem 'rubocop-rake'
15
+ gem 'rubocop-rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 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,97 @@
1
+ # KZG for Ruby
2
+
3
+ A library for [KZG commitment](http://cacr.uwaterloo.ca/techreports/2010/cacr2010-10.pdf) over BLS12-381 in Ruby.
4
+
5
+ Note: This library has not been security audited and tested widely, so should not be used in production.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kzg'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kzg
22
+
23
+ ## Usage
24
+
25
+ ### Setup
26
+
27
+ The first step is to generate public parameters via Trusted Setup.
28
+ The following method specifies the secret value for development purposes,
29
+ but essentially you need to create these parameters in a way that nobody knows the secret.
30
+
31
+ `KZG.setup_params` takes the secret value and the maximum degree + 1 of the polynomial to be generated as input and outputs the public parameters.
32
+ The public parameters consist of an array of point in `BLS::PointG1` and `BLS::PointG2`.
33
+
34
+ ```ruby
35
+ require 'kzg'
36
+
37
+ secret = xxx # secret number
38
+ n = 10
39
+ setting = KZG.setup_params(secret, n)
40
+ ```
41
+
42
+ The above public parameters can support up to a polynomial of degree 9.
43
+
44
+ ### Commitment
45
+
46
+ With a public parameter, a commitment to a polynomial can be made.
47
+
48
+ `KZG::Commitment#from_coeffs` creates the corresponding polynomial commitment from the public parameters and the coefficients of the polynomial.
49
+
50
+ ```ruby
51
+ coefficients = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
52
+ commitment = KZG::Commitment.from_coeffs(setting, coefficients)
53
+ commitment.value
54
+ ```
55
+
56
+ `KZG::Commitment#value` returns the committed value, i.e. the point in the `BLS::PointG1`.
57
+
58
+ The committer can compute proof that the value of the polynomial (f(x)) for any value (x) is correct.
59
+
60
+ ```ruby
61
+ proof = commitment.compute_proof(35)
62
+ ```
63
+
64
+ This proof is point in the `BLS::PointG1`.
65
+
66
+ ### Verify
67
+
68
+ Verifiers can use committed value and proof to verify that the value of f(x) for the value of x is correct.
69
+
70
+ ```ruby
71
+ x = 35
72
+ y = 808951170278371
73
+ setting.valid_proof?(commitment.value, proof, x, y)
74
+ ```
75
+
76
+ ### Use as vector commitment
77
+
78
+ When used as a Vector commitment, the value to be committed is encoded in a polynomial expression as the evaluated value of the polynomial.
79
+ For example, if commit to the vector [3, 2, 9], compute the polynomial pass the points (1, 3), (2, 2), (3, 9).
80
+ This can be computed by polynomial interpolation.
81
+
82
+ `KZG::Polynomial#lagrange_interpolate` method recovers a polynomial from several points using Lagrangian interpolation.
83
+ Then commitment is created using the restored polynomial.
84
+
85
+ ```ruby
86
+ x = [1, 2, 3]
87
+ y = [3, 2, 9]
88
+
89
+ polynomial = KZG::Polynomial.lagrange_interpolate(x, y)
90
+
91
+ commitment = KZG::Commitment.from_coeffs(setting, polynomial.coeffs)
92
+
93
+ # compute proof
94
+ proof = commitment.compute_proof(3)
95
+ # verify
96
+ setting.valid_proof?(commitment.value, proof, 3, 9)
97
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'kzg'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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/kzg.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/kzg/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'kzg'
7
+ spec.version = KZG::VERSION
8
+ spec.authors = ['azuchi']
9
+ spec.email = ['azuchi@chaintope.com']
10
+
11
+ spec.summary = 'KZG polynomial commitment library for Ruby.'
12
+ spec.description = 'KZG polynomial commitment library for Ruby.'
13
+ spec.homepage = 'https://github.com/azuchi/kzg'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+ spec.add_dependency 'bls12-381', '>= 0.2.1'
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ spec.add_development_dependency 'bundler'
37
+ spec.add_development_dependency 'rake', '>= 12.3.3'
38
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KZG
4
+ # KZG commitment
5
+ class Commitment
6
+ attr_reader :setting, :polynomial, :value
7
+
8
+ # Create commitment
9
+ # @param [KZG::Setting] setting
10
+ # @param [Array(Integer | BLS::Fr)] coeffs Coefficients of polynomial equation.
11
+ def initialize(setting, polynomial, value)
12
+ @setting = setting
13
+ @polynomial = polynomial
14
+ @value = value
15
+ end
16
+
17
+ # Create commitment using coefficients.
18
+ # @param [KZG::Setting] setting
19
+ # @param [Array(Integer | BLS::Fr)] coeffs Coefficients of polynomial equation.
20
+ def self.from_coeffs(setting, coeffs)
21
+ if coeffs.length > setting.g1_points.length
22
+ raise KZG::Error,
23
+ "coeffs length is greater than the number of secret parameters."
24
+ end
25
+ value =
26
+ coeffs
27
+ .map
28
+ .with_index do |c, i|
29
+ setting.g1_points[i] * (c.is_a?(BLS::Fr) ? c : BLS::Fr.new(c))
30
+ end
31
+ .inject(&:+)
32
+ Commitment.new(setting, KZG::Polynomial.new(coeffs), value)
33
+ end
34
+
35
+ # Compute KZG proof for polynomial in coefficient form at position x.
36
+ # @param [Integer] x Position
37
+ # @return [BLS::PointG1] Proof.
38
+ def compute_proof(x)
39
+ divisor = Array.new(2)
40
+ divisor[0] = BLS::Fr.new(x).negate
41
+ divisor[1] = BLS::Fr::ONE
42
+ quotient_poly = polynomial.poly_long_div(divisor)
43
+ Commitment.from_coeffs(setting, quotient_poly).value
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KZG
4
+ # Polynomial
5
+ class Polynomial
6
+ attr_reader :coeffs
7
+
8
+ # Create new polynomial
9
+ # @param [Array(Integer|BLS::Fr)]
10
+ def initialize(coeffs)
11
+ @coeffs = coeffs.map { |c| c.is_a?(BLS::Fr) ? c : BLS::Fr.new(c) }
12
+ end
13
+
14
+ # Create polynomial using lagrange interpolation using (x, y) list.
15
+ # @param [Array(Integer)] x The array of x coordinate.
16
+ # @param [Array(Integer)] y The array of y coordinate.
17
+ # @return [KZG::Polynomial]
18
+ def self.lagrange_interpolate(x, y)
19
+ n = x.length
20
+ x = x.map { |i| i.is_a?(BLS::Fr) ? i : BLS::Fr.new(i) }
21
+ y = y.map { |i| i.is_a?(BLS::Fr) ? i : BLS::Fr.new(i) }
22
+ coeffs = Array.new(n, BLS::Fr::ZERO)
23
+ n.times do |i|
24
+ prod = BLS::Fr::ONE
25
+ n.times { |j| prod *= (x[i] - x[j]) unless i == j }
26
+ prod = y[i] / prod
27
+ term = [prod] + Array.new(n - 1, BLS::Fr::ZERO)
28
+ n.times do |j|
29
+ next if i == j
30
+ (n - 1).step(1, -1) do |k|
31
+ term[k] += term[k - 1]
32
+ term[k - 1] *= x[j].negate
33
+ end
34
+ end
35
+ n.times { |j| coeffs[j] += term[j] }
36
+ end
37
+ Polynomial.new(coeffs)
38
+ end
39
+
40
+ # Evaluates a polynomial expression with the specified value +x+.
41
+ # @param [Integer|BLS::Fr] x
42
+ # @return [BLS::Fr]
43
+ def eval_at(x)
44
+ power = x.is_a?(BLS::Fr) ? x : BLS::Fr.new(x)
45
+ sum = coeffs.first
46
+ coeffs[1..].each do |c|
47
+ sum += c * power
48
+ power *= power
49
+ end
50
+ sum
51
+ end
52
+
53
+ # Long polynomial division for two polynomials in coefficient form
54
+ # @param [Array(BLS::Fr)] divisor Array of divisor.
55
+ # @return [Array(BLS::Fr)]
56
+ def poly_long_div(divisor)
57
+ a = coeffs
58
+ a_pos = a.length - 1
59
+ b_pos = divisor.length - 1
60
+ diff = a_pos - b_pos
61
+ quotient_poly = []
62
+
63
+ while diff >= 0
64
+ quot = a[a_pos] / divisor[b_pos]
65
+ i = b_pos
66
+ while i >= 0
67
+ tmp = quot * divisor[i]
68
+ tmp2 = a[diff + i] - tmp
69
+ a[diff + i] = tmp2
70
+ i -= 1
71
+ end
72
+ quotient_poly[diff] = quot
73
+ a_pos -= 1
74
+ diff -= 1
75
+ end
76
+ quotient_poly
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KZG
4
+ #
5
+ class Setting
6
+ attr_reader :g1_points, :g2_points
7
+
8
+ # @param [Array[BLS::PointG1]] g1s
9
+ # @param [Array[BLS::PointG2]] g2s
10
+ def initialize(g1_points, g2_points)
11
+ if !g1_points.is_a?(Array) || !g2_points.is_a?(Array)
12
+ raise KZG::Error, "g1_points and g2_points must be array."
13
+ end
14
+ unless g1_points.all? { |g| g.is_a?(BLS::PointG1) }
15
+ raise KZG::Error, "All elements of g1_points must be BLS::PointG1."
16
+ end
17
+ unless g2_points.all? { |g| g.is_a?(BLS::PointG2) }
18
+ raise KZG::Error, "All elements of g2_points must be BLS::PointG2."
19
+ end
20
+
21
+ @g1_points = g1_points
22
+ @g2_points = g2_points
23
+ end
24
+
25
+ def ==(other)
26
+ g1_points == other.g1_points && g2_points == other.g2_points
27
+ end
28
+
29
+ # Check a proof for a KZG commitment for an evaluation f(x) = y
30
+ # @param [BLS::PointG1] commit_point
31
+ # @param [BLS::PointG1] proof
32
+ # @param [Integer|BLS::Fr] x
33
+ # @param [Integer|BLS::Fr] y
34
+ def valid_proof?(commit_point, proof, x, y)
35
+ x = x.is_a?(BLS::Fr) ? x : BLS::Fr.new(x)
36
+ y = y.is_a?(BLS::Fr) ? y : BLS::Fr.new(y)
37
+ xg2 = BLS::PointG2::BASE * x
38
+ yg = BLS::PointG1::BASE * y
39
+
40
+ # e([commitment - y]^(-1), [1]) * e([proof], [s - x]) = 1
41
+ lhs =
42
+ BLS.pairing(
43
+ (commit_point - yg).negate,
44
+ BLS::PointG2::BASE,
45
+ with_final_exp: false
46
+ )
47
+ rhs = BLS.pairing(proof, g2_points[1] - xg2, with_final_exp: false)
48
+ exp = (lhs * rhs).final_exponentiate
49
+ exp == BLS::Fq12::ONE
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KZG
4
+ VERSION = "0.1.0"
5
+ end
data/lib/kzg.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bls"
4
+ require_relative "kzg/version"
5
+
6
+ # KZG Commitment library.
7
+ module KZG
8
+ class Error < StandardError
9
+ end
10
+
11
+ autoload :Setting, "kzg/setting"
12
+ autoload :Polynomial, "kzg/polynomial"
13
+ autoload :Commitment, "kzg/commitment"
14
+
15
+ module_function
16
+
17
+ # Setup elements of elliptic curve from +secret+.
18
+ # Note: Since the random secret must not be known to anyone,
19
+ # this Trusted Setup usually needs to be performed using an MPC or similar.
20
+ # @param [Integer] secret random secret.
21
+ # @param [Integer] n number of parameters.
22
+ # @return [KZG::Setting]
23
+ def setup_params(secret, n)
24
+ s1 = Array.new(n)
25
+ s2 = Array.new(n)
26
+ s = BLS::Fr.new(secret)
27
+ s_pow = BLS::Fr::ONE
28
+ n.times do |i|
29
+ s1[i] = BLS::PointG1::BASE * s_pow
30
+ s2[i] = BLS::PointG2::BASE * s_pow
31
+ tmp = s_pow
32
+ s_pow = tmp * s
33
+ end
34
+ Setting.new(s1, s2)
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kzg
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: 2023-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bls12-381
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.1
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: 12.3.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.3
55
+ description: KZG polynomial commitment library for Ruby.
56
+ email:
57
+ - azuchi@chaintope.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/main.yml"
63
+ - ".gitignore"
64
+ - ".prettierignore"
65
+ - ".rspec"
66
+ - ".rubocop.yml"
67
+ - ".ruby-gemset"
68
+ - ".ruby-version"
69
+ - CHANGELOG.md
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - kzg.gemspec
78
+ - lib/kzg.rb
79
+ - lib/kzg/commitment.rb
80
+ - lib/kzg/polynomial.rb
81
+ - lib/kzg/setting.rb
82
+ - lib/kzg/version.rb
83
+ homepage: https://github.com/azuchi/kzg
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ homepage_uri: https://github.com/azuchi/kzg
88
+ source_code_uri: https://github.com/azuchi/kzg
89
+ changelog_uri: https://github.com/azuchi/kzg
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 2.7.0
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.4.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: KZG polynomial commitment library for Ruby.
109
+ test_files: []