ecdsa_ext 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: 3b62285517cc90d7dcb953cc669c69c22883e2804f729bf28e937922b43439aa
4
+ data.tar.gz: 99f865acb675894ad30c868ddd7ce421c9a84fd063c4e57c5abe022419abf85c
5
+ SHA512:
6
+ metadata.gz: 932217994dec3b19d5896eae554b94a98180d37836af0c921aaf08bc0667787a871cfc19d3745ffd538c3b6adca86a02ace890e5fd746ab406e97e6c7d8fbbac
7
+ data.tar.gz: 35a8165ff84e644ad6866208d3bacc930112b37e999e1bfafc898b73825175983fb4a0875d8e2fcde1a60450b8a8d98bca0e533f204a65da7736a946a88124c3
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,21 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ inherit_gem:
5
+ prettier: rubocop.yml
6
+ AllCops:
7
+ TargetRubyVersion: 3.0
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
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ ecdsa_ext
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] - 2023-02-23
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 ecdsa_ext.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) 2023 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,43 @@
1
+ # EcdsaExt
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ecdsa_ext`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ecdsa_ext'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ecdsa_ext
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ecdsa_ext. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ecdsa_ext/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the EcdsaExt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ecdsa_ext/blob/master/CODE_OF_CONDUCT.md).
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 "ecdsa_ext"
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/ecdsa_ext.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ecdsa/ext/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ecdsa_ext"
7
+ spec.version = ECDSA::Ext::VERSION
8
+ spec.authors = ["azuchi"]
9
+ spec.email = ["techmedia.think@gmail.com"]
10
+
11
+ spec.summary = "Extension of the ecdsa gem."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/azuchi/ruby_ecdsa_ext"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.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 do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+ spec.add_dependency "ecdsa", "~> 1.2.0"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+ module ECDSA
3
+ module Ext
4
+ # Point arithmetic implementation
5
+ module PointArithmetic
6
+ def addition_negative3(a, b)
7
+ field = a.field
8
+ xx = field.mod(a.x * b.x)
9
+ yy = field.mod(a.y * b.y)
10
+ zz = field.mod(a.z * b.z)
11
+ xy_pairs = field.mod((a.x + a.y) * (b.x + b.y) - (xx + yy))
12
+ yz_pairs = field.mod((a.y + a.z) * (b.y + b.z) - (yy + zz))
13
+ xz_pairs = field.mod((a.x + a.z) * (b.x + b.z) - (xx + zz))
14
+
15
+ bzz_part = field.mod(xz_pairs - a.group.param_b * zz)
16
+ bzz3_part = field.mod(bzz_part * 2 + bzz_part)
17
+ yy_m_bzz3 = field.mod(yy - bzz3_part)
18
+ yy_p_bzz3 = field.mod(yy + bzz3_part)
19
+
20
+ zz3 = field.mod(zz * 3)
21
+ bxz_part = field.mod(a.group.param_b * xz_pairs - (zz3 + xx))
22
+ bxz3_part = field.mod(bxz_part * 3)
23
+ xx3_m_zz3 = field.mod(xx * 3 - zz3)
24
+
25
+ x = field.mod(yy_p_bzz3 * xy_pairs - yz_pairs * bxz3_part)
26
+ y = field.mod(yy_p_bzz3 * yy_m_bzz3 + xx3_m_zz3 * bxz3_part)
27
+ z = field.mod(yy_m_bzz3 * yz_pairs + xy_pairs * xx3_m_zz3)
28
+ ProjectivePoint.new(a.group, x, y, z)
29
+ end
30
+
31
+ def addition_any(a, b)
32
+ field = a.field
33
+ b3 = field.mod(3 * group.param_b)
34
+ t0 = field.mod(a.x * b.x)
35
+ t1 = field.mod(a.y * b.y)
36
+ t2 = field.mod(a.z * b.z)
37
+ t3 = field.mod(a.x + a.y)
38
+ t4 = field.mod(b.x + b.y)
39
+ t3 = field.mod(t3 * t4)
40
+ t4 = field.mod(t0 + t1)
41
+ t3 = field.mod(t3 - t4)
42
+ t4 = field.mod(a.x + a.z)
43
+ t5 = field.mod(b.x + b.z)
44
+ t4 = field.mod(t4 * t5)
45
+ t5 = field.mod(t0 + t2)
46
+ t4 = field.mod(t4 - t5)
47
+ t5 = field.mod(a.y + a.z)
48
+ x3 = field.mod(b.y + b.z)
49
+ t5 = field.mod(t5 * x3)
50
+ x3 = field.mod(t1 + t2)
51
+ t5 = field.mod(t5 - x3)
52
+ z3 = field.mod(a.group.param_a * t4)
53
+ x3 = field.mod(b3 * t2)
54
+ z3 = field.mod(x3 + z3)
55
+ x3 = field.mod(t1 - z3)
56
+ z3 = field.mod(t1 + z3)
57
+ y3 = field.mod(x3 * z3)
58
+ t1 = field.mod(t0 + t0)
59
+ t1 = field.mod(t1 + t0)
60
+ t2 = field.mod(a.group.param_a * t2)
61
+ t4 = field.mod(b3 * t4)
62
+ t1 = field.mod(t1 + t2)
63
+ t2 = field.mod(t0 - t2)
64
+ t2 = field.mod(a.group.param_a * t2)
65
+ t4 = field.mod(t4 + t2)
66
+ t0 = field.mod(t1 * t4)
67
+ y3 = field.mod(y3 + t0)
68
+ t0 = field.mod(t5 * t4)
69
+ x3 = field.mod(t3 * x3)
70
+ x3 = field.mod(x3 - t0)
71
+ t0 = field.mod(t3 * t1)
72
+ z3 = field.mod(t5 * z3)
73
+ z3 = field.mod(z3 + t0)
74
+ ProjectivePoint.new(a.group, x3, y3, z3)
75
+ end
76
+
77
+ def double_negative3(point)
78
+ field = point.field
79
+ xx = field.power(point.x, 2)
80
+ yy = field.power(point.y, 2)
81
+ zz = field.power(point.z, 2)
82
+ xy2 = field.mod(point.x * point.y * 2)
83
+ xz2 = field.mod(point.x * point.z * 2)
84
+
85
+ bzz_part = field.mod(point.group.param_b * zz - xz2)
86
+ bzz3_part = field.mod(bzz_part + bzz_part + bzz_part)
87
+ yy_m_bzz3 = field.mod(yy - bzz3_part)
88
+ yy_p_bzz3 = field.mod(yy + bzz3_part)
89
+ y_frag = field.mod(yy_p_bzz3 * yy_m_bzz3)
90
+ x_frag = field.mod(yy_m_bzz3 * xy2)
91
+
92
+ zz3 = field.mod(zz * 3)
93
+ bxz2_part = field.mod(point.group.param_b * xz2 - (zz3 + xx))
94
+ bxz6_part = field.mod(bxz2_part * 3)
95
+ xx3_m_zz3 = field.mod(xx * 3 - zz3)
96
+
97
+ y = field.mod(y_frag + xx3_m_zz3 * bxz6_part)
98
+ yz2 = field.mod(point.y * point.z * 2)
99
+ x = field.mod(x_frag - bxz6_part * yz2)
100
+ z = field.mod(yz2 * yy * 4)
101
+ ProjectivePoint.new(point.group, x, y, z)
102
+ end
103
+
104
+ def double_any(point)
105
+ field = point.field
106
+ b3 = field.mod(point.group.param_b * 3)
107
+
108
+ t0 = field.mod(point.x * point.x)
109
+ t1 = field.mod(point.y * point.y)
110
+ t2 = field.mod(point.z * point.z)
111
+ t3 = field.mod(point.x * point.y)
112
+ t3 = field.mod(t3 + t3)
113
+ z3 = field.mod(point.x * point.z)
114
+ z3 = field.mod(z3 + z3)
115
+ x3 = field.mod(point.group.param_a * z3)
116
+ y3 = field.mod(b3 * t2)
117
+ y3 = field.mod(x3 + y3)
118
+ x3 = field.mod(t1 - y3)
119
+ y3 = field.mod(t1 + y3)
120
+ y3 = field.mod(x3 * y3)
121
+ x3 = field.mod(t3 * x3)
122
+ z3 = field.mod(b3 * z3)
123
+ t2 = field.mod(point.group.param_a * t2)
124
+ t3 = field.mod(t0 - t2)
125
+ t3 = field.mod(point.group.param_a * t3)
126
+ t3 = field.mod(t3 + z3)
127
+ z3 = field.mod(t0 + t0)
128
+ t0 = field.mod(z3 + t0)
129
+ t0 = field.mod(t0 + t2)
130
+ t0 = field.mod(t0 * t3)
131
+ y3 = field.mod(y3 + t0)
132
+ t2 = field.mod(point.y * point.z)
133
+ t2 = field.mod(t2 + t2)
134
+ t0 = field.mod(t2 * t3)
135
+ x3 = field.mod(x3 - t0)
136
+ z3 = field.mod(t2 * t1)
137
+ z3 = field.mod(z3 + z3)
138
+ z3 = field.mod(z3 + z3)
139
+ ProjectivePoint.new(point.group, x3, y3, z3)
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ECDSA
4
+ module Ext
5
+ # Representing a point on elliptic curves using projective coordinates.
6
+ class ProjectivePoint
7
+ include PointArithmetic
8
+
9
+ attr_reader :group, :x, :y, :z
10
+
11
+ # Create new instance of projective
12
+ # @param [ECDSA::Group] group
13
+ # @param [Array] args [:infinity] or [x, y, z]
14
+ # @return [ECDSA::Ext::ProjectivePoint]
15
+ def initialize(group, *args)
16
+ @group = group
17
+ @x, @y, @z = args
18
+ raise ArgumentError, "Invalid x: #{x.inspect}" unless x.is_a?(Integer)
19
+ raise ArgumentError, "Invalid y: #{y.inspect}" unless y.is_a?(Integer)
20
+ raise ArgumentError, "Invalid z: #{z.inspect}" unless z.is_a?(Integer)
21
+ end
22
+
23
+ # Get filed of this group.
24
+ # @return [ECDSA::PrimeField]
25
+ def field
26
+ group.field
27
+ end
28
+
29
+ # Convert coordinates from affine to projective.
30
+ # @param [ECDSA::Point] point
31
+ # @return [ECDSA::Ext::ProjectivePoint]
32
+ def self.from_affine(point)
33
+ if point.infinity?
34
+ ProjectivePoint.infinity(point.group)
35
+ else
36
+ new(point.group, point.x, point.y, 1)
37
+ end
38
+ end
39
+
40
+ # Create infinity
41
+ # @return [ECDSA::Ext::ProjectivePoint]
42
+ def self.infinity(group)
43
+ # new(group, :infinity)
44
+ new(group, 0, 1, 0)
45
+ end
46
+
47
+ # Check whether infinity point or not.
48
+ # @return [Boolean]
49
+ def infinity?
50
+ x.zero? && y == 1 && z.zero?
51
+ end
52
+
53
+ # Add this point to another point on the same curve.
54
+ # @param [ECDSA::Ext::ProjectivePoint] other
55
+ # @return [ECDSA::Ext::ProjectivePoint]
56
+ def add_to_point(other)
57
+ unless other.is_a?(ProjectivePoint)
58
+ raise ArgumentError, "other point must be instance of ProjectivePoint"
59
+ end
60
+ unless other.group == group
61
+ raise ArgumentError, "other group must be same group of this point"
62
+ end
63
+
64
+ return other if infinity?
65
+ return self if other.infinity?
66
+
67
+ if x == other.x && y == field.mod(-other.y) && z == other.z
68
+ return ProjectivePoint.infinity(group)
69
+ end
70
+
71
+ unless x == other.x
72
+ return(
73
+ (
74
+ if group.param_a == field.mod(-3)
75
+ addition_negative3(self, other)
76
+ else
77
+ addition_any(self, other)
78
+ end
79
+ )
80
+ )
81
+ end
82
+
83
+ return double if self == other
84
+ raise "Failed to add #{inspect} to #{other.inspect}: No addition rules matched."
85
+ end
86
+ alias + add_to_point
87
+
88
+ # Return the point added to itself.
89
+ # @return [ECDSA::Ext::ProjectivePoint]
90
+ def double
91
+ return self if infinity?
92
+
93
+ if group.param_a == field.mod(-3)
94
+ double_negative3(self)
95
+ else
96
+ double_any(self)
97
+ end
98
+ end
99
+
100
+ # Convert this coordinates to affine coordinates.
101
+ # @return [ECDSA::Point]
102
+ def to_affine
103
+ if infinity?
104
+ group.infinity
105
+ else
106
+ ECDSA::Point.new(
107
+ group,
108
+ field.mod(x * field.inverse(z)),
109
+ field.mod(y * field.inverse(z))
110
+ )
111
+ end
112
+ end
113
+
114
+ # Return the point multiplied by a non-negative integer.
115
+ # @param [Integer] x
116
+ # @return [ECDSA::Ext::ProjectivePoint]
117
+ def multiply_by_scalar(x)
118
+ raise ArgumentError, "Scalar is not an integer." unless x.is_a?(Integer)
119
+ raise ArgumentError, "Scalar is negative." if x.negative?
120
+
121
+ q = ProjectivePoint.infinity(group)
122
+ v = self
123
+ i = x
124
+ while i.positive?
125
+ q = q.add_to_point(v) if i.odd?
126
+ v = v.double
127
+ i >>= 1
128
+ end
129
+ q
130
+ end
131
+ alias * multiply_by_scalar
132
+
133
+ # Return additive inverse of the point.
134
+ # @return [ECDSA::Ext::ProjectivePoint]
135
+ def negate
136
+ return self if infinity?
137
+ ProjectivePoint.new(group, x, field.mod(-y), z)
138
+ end
139
+
140
+ # Return coordinates.
141
+ # @return [Array] (x, y , z)
142
+ def coords
143
+ [x, y, z]
144
+ end
145
+
146
+ def ==(other)
147
+ return false unless other.is_a?(ProjectivePoint)
148
+ group == other.group && x == other.x && y == other.y && z == other.z
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ECDSA
4
+ module Ext
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/ecdsa/ext.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ require_relative "ext/version"
3
+ module ECDSA
4
+ # Extension for ecdsa gem.
5
+ module Ext
6
+ autoload :PointArithmetic, "ecdsa/ext/point_arithmetic"
7
+ autoload :ProjectivePoint, "ecdsa/ext/projective_point"
8
+ end
9
+ end
data/lib/ecdsa_ext.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ecdsa"
4
+ require "ecdsa/ext"
data/sig/ecdsa_ext.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module EcdsaExt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecdsa_ext
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-02-25 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
+ description: Extension of the ecdsa gem.
28
+ email:
29
+ - techmedia.think@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".prettierignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - ".ruby-gemset"
38
+ - ".ruby-version"
39
+ - CHANGELOG.md
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - ecdsa_ext.gemspec
48
+ - lib/ecdsa/ext.rb
49
+ - lib/ecdsa/ext/point_arithmetic.rb
50
+ - lib/ecdsa/ext/projective_point.rb
51
+ - lib/ecdsa/ext/version.rb
52
+ - lib/ecdsa_ext.rb
53
+ - sig/ecdsa_ext.rbs
54
+ homepage: https://github.com/azuchi/ruby_ecdsa_ext
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/azuchi/ruby_ecdsa_ext
59
+ source_code_uri: https://github.com/azuchi/ruby_ecdsa_ext
60
+ changelog_uri: https://github.com/azuchi/ruby_ecdsa_ext
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.0.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.4.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Extension of the ecdsa gem.
80
+ test_files: []