ecdsa_ext 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2a5e0f4a0a8de441e3d2f1374666b345b3b5576c4d3b455b1e138ee9c032c88
4
- data.tar.gz: 844a766b56bf77ce971788fa414194500f9e9ee25c9549b003cded0437fb2ff4
3
+ metadata.gz: 14a1e1194f60d4d1bbd5d82d77c0d628b76fe3e9ed2efd9dccd5b213fd0506c1
4
+ data.tar.gz: 5f832b9a184acb52c59dece9f2db7fef06ba1535e118957100c36ba0895718b0
5
5
  SHA512:
6
- metadata.gz: 794aaac69e3cad831d943aa488d0b3a5f9169d7660dd3c37edb284332550be3112b39455090af2efc0229ac828d72280ad827c383e6c29300ad2d29febc28bfc
7
- data.tar.gz: 5ee76f7e334da633becd188573089f00e3c326726217da25ceb7fb3a9e04279e5548a4085f64366bb8e530b986fdd8f691360a88690dfbc889f90333aea45314
6
+ metadata.gz: 8ea53f7ce5d08ce1766ec5ed7b9030416fb4bb70b9de4db9918656d66869fd1f7c36cff4f06e35bb5aece48e8e0f2ca8a292346b43a30c82c1115196a0d37e7b
7
+ data.tar.gz: c7fd59188a641436c3878254299487bd8369565f72625aeb376ae058d9c728b091109ac6b6c87c830ae2f743dba1edcb02a193a29e6ff93c1d37b1d3b084f53f
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ECDSA
4
+ module Ext
5
+ # Abstract class of point
6
+ class AbstractPoint
7
+ attr_reader :group, :x, :y, :z, :infinity
8
+
9
+ # Create new instance.
10
+ # @param [ECDSA::Group] group
11
+ # @param [Array] args [x, y, z]
12
+ # @return [ECDSA::Ext::AbstractPoint]
13
+ def initialize(group, *args)
14
+ @group = group
15
+ if args == [:infinity]
16
+ @infinity = true
17
+ else
18
+ @infinity = false
19
+ @x, @y, @z = args
20
+ raise ArgumentError, "Invalid x: #{x.inspect}" unless x.is_a?(Integer)
21
+ raise ArgumentError, "Invalid y: #{y.inspect}" unless y.is_a?(Integer)
22
+ raise ArgumentError, "Invalid z: #{z.inspect}" unless z.is_a?(Integer)
23
+ end
24
+ end
25
+
26
+ # Get filed of this group.
27
+ # @return [ECDSA::PrimeField]
28
+ def field
29
+ group.field
30
+ end
31
+
32
+ # Convert coordinates from affine.
33
+ # @param [ECDSA::Point] point
34
+ # @return [ECDSA::Ext::AbstractPoint]
35
+ def self.from_affine(point)
36
+ if point.infinity?
37
+ infinity_point(point.group)
38
+ else
39
+ new(point.group, point.x, point.y, 1)
40
+ end
41
+ end
42
+
43
+ # Create infinity point
44
+ # @return [ECDSA::Ext::JacobianPoint]
45
+ def self.infinity_point(group)
46
+ # new(group, :infinity)
47
+ new(group, :infinity)
48
+ end
49
+
50
+ # Check whether infinity point or not.
51
+ # @return [Boolean]
52
+ def infinity?
53
+ @infinity
54
+ end
55
+
56
+ # Return additive inverse of the point.
57
+ # @return [ECDSA::Ext::AbstractPoint]
58
+ def negate
59
+ return self if infinity?
60
+ self.class.new(group, x, field.mod(-y), z)
61
+ end
62
+
63
+ # Return coordinates.
64
+ # @return [Array] (x, y , z)
65
+ def coords
66
+ [x, y, z]
67
+ end
68
+
69
+ # Return the point multiplied by a non-negative integer.
70
+ # @param [Integer] x
71
+ # @return [ECDSA::Ext::ProjectivePoint]
72
+ def multiply_by_scalar(x)
73
+ raise ArgumentError, "Scalar is not an integer." unless x.is_a?(Integer)
74
+ raise ArgumentError, "Scalar is negative." if x.negative?
75
+
76
+ q = self.class.infinity_point(group)
77
+ v = self
78
+ i = x
79
+ while i.positive?
80
+ q = q.add_to_point(v) if i.odd?
81
+ v = v.double
82
+ i >>= 1
83
+ end
84
+ q
85
+ end
86
+ alias * multiply_by_scalar
87
+
88
+ def add_to_point(other)
89
+ raise NotImplementedError
90
+ end
91
+
92
+ def double
93
+ raise NotImplementedError
94
+ end
95
+
96
+ def to_affine
97
+ raise NotImplementedError
98
+ end
99
+
100
+ def ==(other)
101
+ raise NotImplementedError
102
+ end
103
+ end
104
+ end
105
+ end
@@ -3,53 +3,9 @@
3
3
  module ECDSA
4
4
  module Ext
5
5
  # Point of Jacobian coordinates
6
- class JacobianPoint
6
+ class JacobianPoint < AbstractPoint
7
7
  include JacobianArithmetic
8
8
 
9
- attr_reader :group, :x, :y, :z
10
-
11
- # Create new instance of jacobian
12
- # @param [ECDSA::Group] group
13
- # @param [Array] args [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::JacobianPoint]
32
- def self.from_affine(point)
33
- if point.infinity?
34
- JacobianPoint.infinity(point.group)
35
- else
36
- new(point.group, point.x, point.y, 1)
37
- end
38
- end
39
-
40
- # Create infinity point
41
- # @return [ECDSA::Ext::JacobianPoint]
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
9
  # Add this point to another point on the same curve.
54
10
  # @param [ECDSA::Ext::JacobianPoint] other
55
11
  # @return [ECDSA::Ext::JacobianPoint]
@@ -65,7 +21,7 @@ module ECDSA
65
21
  return self if other.infinity?
66
22
 
67
23
  if x == other.x && y == field.mod(-other.y) && z == other.z
68
- return JacobianPoint.infinity(group)
24
+ return JacobianPoint.infinity_point(group)
69
25
  end
70
26
 
71
27
  return other if y.zero? || z.zero?
@@ -106,25 +62,6 @@ module ECDSA
106
62
  JacobianPoint.new(group, t, y3, z3)
107
63
  end
108
64
 
109
- # Return the point multiplied by a non-negative integer.
110
- # @param [Integer] x
111
- # @return [ECDSA::Ext::JacobianPoint]
112
- def multiply_by_scalar(x)
113
- raise ArgumentError, "Scalar is not an integer." unless x.is_a?(Integer)
114
- raise ArgumentError, "Scalar is negative." if x.negative?
115
-
116
- q = JacobianPoint.infinity(group)
117
- v = self
118
- i = x
119
- while i.positive?
120
- q = q.add_to_point(v) if i.odd?
121
- v = v.double
122
- i >>= 1
123
- end
124
- q
125
- end
126
- alias * multiply_by_scalar
127
-
128
65
  # Convert this coordinates to affine coordinates.
129
66
  # @return [ECDSA::Point]
130
67
  def to_affine
@@ -139,19 +76,6 @@ module ECDSA
139
76
  end
140
77
  end
141
78
 
142
- # Return additive inverse of the point.
143
- # @return [ECDSA::Ext::ProjectivePoint]
144
- def negate
145
- return self if infinity?
146
- ProjectivePoint.new(group, x, field.mod(-y), z)
147
- end
148
-
149
- # Return coordinates.
150
- # @return [Array] (x, y , z)
151
- def coords
152
- [x, y, z]
153
- end
154
-
155
79
  # Check whether same jacobian point or not.
156
80
  # @param [ECDSA::Ext::JacobianPoint] other
157
81
  # @return [Boolean]
@@ -3,53 +3,9 @@
3
3
  module ECDSA
4
4
  module Ext
5
5
  # Representing a point on elliptic curves using projective coordinates.
6
- class ProjectivePoint
6
+ class ProjectivePoint < AbstractPoint
7
7
  include ProjectiveArithmetic
8
8
 
9
- attr_reader :group, :x, :y, :z
10
-
11
- # Create new instance of projective
12
- # @param [ECDSA::Group] group
13
- # @param [Array] args [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
9
  # Add this point to another point on the same curve.
54
10
  # @param [ECDSA::Ext::ProjectivePoint] other
55
11
  # @return [ECDSA::Ext::ProjectivePoint]
@@ -65,7 +21,7 @@ module ECDSA
65
21
  return self if other.infinity?
66
22
 
67
23
  if x == other.x && y == field.mod(-other.y) && z == other.z
68
- return ProjectivePoint.infinity(group)
24
+ return ProjectivePoint.infinity_point(group)
69
25
  end
70
26
 
71
27
  unless x == other.x
@@ -108,38 +64,6 @@ module ECDSA
108
64
  end
109
65
  end
110
66
 
111
- # Return the point multiplied by a non-negative integer.
112
- # @param [Integer] x
113
- # @return [ECDSA::Ext::ProjectivePoint]
114
- def multiply_by_scalar(x)
115
- raise ArgumentError, "Scalar is not an integer." unless x.is_a?(Integer)
116
- raise ArgumentError, "Scalar is negative." if x.negative?
117
-
118
- q = ProjectivePoint.infinity(group)
119
- v = self
120
- i = x
121
- while i.positive?
122
- q = q.add_to_point(v) if i.odd?
123
- v = v.double
124
- i >>= 1
125
- end
126
- q
127
- end
128
- alias * multiply_by_scalar
129
-
130
- # Return additive inverse of the point.
131
- # @return [ECDSA::Ext::ProjectivePoint]
132
- def negate
133
- return self if infinity?
134
- ProjectivePoint.new(group, x, field.mod(-y), z)
135
- end
136
-
137
- # Return coordinates.
138
- # @return [Array] (x, y , z)
139
- def coords
140
- [x, y, z]
141
- end
142
-
143
67
  def ==(other)
144
68
  return false unless other.is_a?(ProjectivePoint)
145
69
  return true if infinity? && other.infinity?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ECDSA
4
4
  module Ext
5
- VERSION = "0.3.1"
5
+ VERSION = "0.3.3"
6
6
  end
7
7
  end
data/lib/ecdsa/ext.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "ext/point"
4
4
  module ECDSA
5
5
  # Extension for ecdsa gem.
6
6
  module Ext
7
+ autoload :AbstractPoint, "ecdsa/ext/abstract_point"
7
8
  autoload :ProjectiveArithmetic, "ecdsa/ext/projective_arithmetic"
8
9
  autoload :ProjectivePoint, "ecdsa/ext/projective_point"
9
10
  autoload :JacobianArithmetic, "ecdsa/ext/jacobian_arithmetic"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecdsa_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-06 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecdsa
@@ -46,6 +46,7 @@ files:
46
46
  - bin/setup
47
47
  - ecdsa_ext.gemspec
48
48
  - lib/ecdsa/ext.rb
49
+ - lib/ecdsa/ext/abstract_point.rb
49
50
  - lib/ecdsa/ext/jacobian_arithmetic.rb
50
51
  - lib/ecdsa/ext/jacobian_point.rb
51
52
  - lib/ecdsa/ext/point.rb