ecdsa_ext 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/lib/ecdsa/ext/abstract_point.rb +1 -2
- data/lib/ecdsa/ext/sign_verify.rb +76 -0
- data/lib/ecdsa/ext/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84f032825c120367ced4c69a3faf2f25c519b0591a6fe2e6a9ebe21e1456a66b
|
4
|
+
data.tar.gz: af328ff04a086fabaf93652cab94400c95a1dfba85daff6667defca0c112b2a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+
```
|
@@ -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
|
data/lib/ecdsa/ext/version.rb
CHANGED
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.
|
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-
|
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
|