ecdsa_ext 0.3.3 → 0.4.0

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: 14a1e1194f60d4d1bbd5d82d77c0d628b76fe3e9ed2efd9dccd5b213fd0506c1
4
- data.tar.gz: 5f832b9a184acb52c59dece9f2db7fef06ba1535e118957100c36ba0895718b0
3
+ metadata.gz: 84f032825c120367ced4c69a3faf2f25c519b0591a6fe2e6a9ebe21e1456a66b
4
+ data.tar.gz: af328ff04a086fabaf93652cab94400c95a1dfba85daff6667defca0c112b2a1
5
5
  SHA512:
6
- metadata.gz: 8ea53f7ce5d08ce1766ec5ed7b9030416fb4bb70b9de4db9918656d66869fd1f7c36cff4f06e35bb5aece48e8e0f2ca8a292346b43a30c82c1115196a0d37e7b
7
- data.tar.gz: c7fd59188a641436c3878254299487bd8369565f72625aeb376ae058d9c728b091109ac6b6c87c830ae2f743dba1edcb02a193a29e6ff93c1d37b1d3b084f53f
6
+ metadata.gz: c1c83beba1a26a89ba958f0b1f3f75ea48ad4777f252c1430294997cf44385c205559876354fe62d728f714fc162606af4975535c813a18595623d8ed6944d09
7
+ data.tar.gz: e32b28574ec1c21cc336039e9a66a780784a7a5d3ea2a1fa20796799c2077f24b6db0dcb8853d0b7067bba4b6b41d7196214df10b7175037ab118828a74934ba
data/README.md CHANGED
@@ -82,4 +82,13 @@ affine_point = projective_point4.to_affine
82
82
  Jacobian coordinates have been supported since 0.3.0.
83
83
 
84
84
  When using Jacobian coordinates, use `ECDSA::Ext::JacobianPoint` instead of `ECDSA::Ext::ProjectivePoint`.
85
- In addition, `ECDSA::Point` now has a `to_jacobian` method that convert affine coordinates to jacobian coordinates.
85
+ In addition, `ECDSA::Point` now has a `to_jacobian` method that convert affine coordinates to jacobian coordinates.
86
+
87
+ ### Apply jacobian coordinates to existing ECDSA sign/verify
88
+
89
+ If you want the existing ECDSA gem to generate and verify signatures in Jacobian coordinates,
90
+ add the following code. This code is a monkey patch to do the existing process in Jacobian coordinates.
91
+
92
+ ```ruby
93
+ require 'ecdsa/ext/sign_verify'
94
+ ```
@@ -41,9 +41,8 @@ module ECDSA
41
41
  end
42
42
 
43
43
  # Create infinity point
44
- # @return [ECDSA::Ext::JacobianPoint]
44
+ # @return [ECDSA::Ext::AbstractPoint]
45
45
  def self.infinity_point(group)
46
- # new(group, :infinity)
47
46
  new(group, :infinity)
48
47
  end
49
48
 
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A monkey patch to allow signature generation and verification of existing ECDSA with Jacobian coordinates.
4
+ module ECDSA
5
+ def self.sign(group, private_key, digest, temporary_key)
6
+ # Second part of step 1: Select ephemeral elliptic curve key pair
7
+ # temporary_key was already selected for us by the caller
8
+ r_point = (group.generator.to_jacobian * temporary_key).to_affine
9
+
10
+ # Steps 2 and 3
11
+ point_field = PrimeField.new(group.order)
12
+ r = point_field.mod(r_point.x)
13
+ return nil if r.zero?
14
+
15
+ # Step 4, calculating the hash, was already performed by the caller.
16
+
17
+ # Step 5
18
+ e = normalize_digest(digest, group.bit_length)
19
+
20
+ # Step 6
21
+ s =
22
+ point_field.mod(
23
+ point_field.inverse(temporary_key) * (e + r * private_key)
24
+ )
25
+ return nil if s.zero?
26
+
27
+ Signature.new r, s
28
+ end
29
+
30
+ def self.check_signature!(public_key, digest, signature)
31
+ group = public_key.group
32
+ field = group.field
33
+
34
+ # Step 1: r and s must be in the field and non-zero
35
+ unless field.include?(signature.r)
36
+ raise InvalidSignatureError, "Invalid signature: r is not in the field."
37
+ end
38
+ unless field.include?(signature.s)
39
+ raise InvalidSignatureError, "Invalid signature: s is not in the field."
40
+ end
41
+ if signature.r.zero?
42
+ raise InvalidSignatureError, "Invalid signature: r is zero."
43
+ end
44
+ if signature.s.zero?
45
+ raise InvalidSignatureError, "Invalid signature: s is zero."
46
+ end
47
+
48
+ # Step 2 was already performed when the digest of the message was computed.
49
+
50
+ # Step 3: Convert octet string to number and take leftmost bits.
51
+ e = normalize_digest(digest, group.bit_length)
52
+
53
+ # Step 4
54
+ point_field = PrimeField.new(group.order)
55
+ s_inverted = point_field.inverse(signature.s)
56
+ u1 = point_field.mod(e * s_inverted)
57
+ u2 = point_field.mod(signature.r * s_inverted)
58
+
59
+ # Step 5
60
+ r =
61
+ (group.generator.to_jacobian * u1 + public_key.to_jacobian * u2).to_affine
62
+ if r.infinity?
63
+ raise InvalidSignatureError, "Invalid signature: r is infinity in step 5."
64
+ end
65
+
66
+ # Steps 6 and 7
67
+ v = point_field.mod r.x
68
+
69
+ # Step 8
70
+ if v != signature.r
71
+ raise InvalidSignatureError, "Invalid signature: v does not equal r."
72
+ end
73
+
74
+ true
75
+ end
76
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ECDSA
4
4
  module Ext
5
- VERSION = "0.3.3"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
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.3
4
+ version: 0.4.0
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-07 00:00:00.000000000 Z
11
+ date: 2023-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecdsa
@@ -52,6 +52,7 @@ files:
52
52
  - lib/ecdsa/ext/point.rb
53
53
  - lib/ecdsa/ext/projective_arithmetic.rb
54
54
  - lib/ecdsa/ext/projective_point.rb
55
+ - lib/ecdsa/ext/sign_verify.rb
55
56
  - lib/ecdsa/ext/version.rb
56
57
  - lib/ecdsa_ext.rb
57
58
  - sig/ecdsa_ext.rbs