secp256k1-ruby 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.gitmodules +3 -0
- data/.ruby-version +1 -0
- data/Gemfile +2 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/Rakefile +11 -0
- data/install_lib.sh +7 -0
- data/lib/secp256k1.rb +20 -0
- data/lib/secp256k1/c.rb +91 -0
- data/lib/secp256k1/ecdsa.rb +102 -0
- data/lib/secp256k1/key.rb +250 -0
- data/lib/secp256k1/utils.rb +22 -0
- data/lib/secp256k1/version.rb +4 -0
- data/secp256k1-ruby.gemspec +25 -0
- data/test/fixtures/ecdsa_sig.json +999 -0
- data/test/fixtures/pubkey.json +1749 -0
- data/test/secp256k1_test.rb +103 -0
- metadata +136 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
3
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'secp256k1'
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class MyECDSA < Secp256k1::BaseKey
|
11
|
+
include Secp256k1::Utils, Secp256k1::ECDSA
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super(nil, Secp256k1::ALL_FLAGS)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Secp256k1Test < Minitest::Test
|
19
|
+
include Secp256k1
|
20
|
+
|
21
|
+
def test_ecdsa
|
22
|
+
vec = ecdsa_sig['vectors']
|
23
|
+
pk = PrivateKey.new
|
24
|
+
|
25
|
+
vec.each do |item|
|
26
|
+
seckey = Utils.decode_hex item['privkey']
|
27
|
+
msg32 = Utils.decode_hex item['msg']
|
28
|
+
sig = Utils.decode_hex(item['sig'])[0...-1]
|
29
|
+
|
30
|
+
pk.set_raw_privkey seckey
|
31
|
+
|
32
|
+
sig_raw = pk.ecdsa_sign msg32, raw: true
|
33
|
+
sig_check = pk.ecdsa_serialize sig_raw
|
34
|
+
|
35
|
+
assert_equal sig, sig_check
|
36
|
+
assert_equal sig_check, pk.ecdsa_serialize(pk.ecdsa_deserialize(sig_check))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_ecdsa_compact
|
41
|
+
pk = PrivateKey.new
|
42
|
+
raw_sig = pk.ecdsa_sign 'test'
|
43
|
+
assert_equal true, pk.pubkey.ecdsa_verify('test', raw_sig)
|
44
|
+
|
45
|
+
compact = pk.ecdsa_serialize_compact raw_sig
|
46
|
+
assert_equal 64, compact.size
|
47
|
+
|
48
|
+
sig_raw = pk.ecdsa_deserialize_compact compact
|
49
|
+
assert_equal compact, pk.ecdsa_serialize_compact(sig_raw)
|
50
|
+
assert_equal true, pk.pubkey.ecdsa_verify('test', sig_raw)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_ecdsa_normalize
|
54
|
+
pk = PrivateKey.new
|
55
|
+
raw_sig = pk.ecdsa_sign 'hi'
|
56
|
+
|
57
|
+
had_to_normalize, normsig = pk.ecdsa_signature_normalize raw_sig
|
58
|
+
assert_equal false, had_to_normalize
|
59
|
+
assert_equal pk.ecdsa_serialize(raw_sig), pk.ecdsa_serialize(normsig)
|
60
|
+
assert_equal pk.ecdsa_serialize_compact(raw_sig), pk.ecdsa_serialize_compact(normsig)
|
61
|
+
|
62
|
+
had_to_normalize, normsig = pk.ecdsa_signature_normalize(raw_sig, check_only: true)
|
63
|
+
assert_equal false, had_to_normalize
|
64
|
+
assert_equal nil, normsig
|
65
|
+
|
66
|
+
sig = "\xAA" + "\xFF"*31 + "\xAA" + "\xFF"*31
|
67
|
+
raw_sig = pk.ecdsa_deserialize_compact sig
|
68
|
+
|
69
|
+
normalized, normsig = pk.ecdsa_signature_normalize raw_sig
|
70
|
+
assert_equal true, normalized
|
71
|
+
assert pk.ecdsa_serialize(raw_sig) != pk.ecdsa_serialize(normsig)
|
72
|
+
|
73
|
+
normalized, normsig = pk.ecdsa_signature_normalize raw_sig, check_only: true
|
74
|
+
assert_equal true, normalized
|
75
|
+
assert_equal nil, normsig
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_ecdsa_recover
|
79
|
+
pk = PrivateKey.new
|
80
|
+
unrelated = MyECDSA.new
|
81
|
+
|
82
|
+
recsig = pk.ecdsa_sign_recoverable 'hello'
|
83
|
+
pubkey = unrelated.ecdsa_recover 'hello', recsig
|
84
|
+
pubser = PublicKey.new(pubkey: pubkey).serialize
|
85
|
+
assert_equal pubser, pk.pubkey.serialize
|
86
|
+
|
87
|
+
recsig_ser = unrelated.ecdsa_recoverable_serialize recsig
|
88
|
+
recsig2 = unrelated.ecdsa_recoverable_deserialize *recsig_ser
|
89
|
+
pubkey2 = unrelated.ecdsa_recover 'hello', recsig2
|
90
|
+
pubser2 = PublicKey.new(pubkey: pubkey2).serialize
|
91
|
+
assert_equal pubser, pubser2
|
92
|
+
|
93
|
+
raw_sig = unrelated.ecdsa_recoverable_convert recsig2
|
94
|
+
unrelated.ecdsa_deserialize(unrelated.ecdsa_serialize(raw_sig))
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def ecdsa_sig
|
100
|
+
@ecdsa_sig = JSON.parse File.read(File.expand_path('../fixtures/ecdsa_sig.json', __FILE__))
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: secp256k1-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hajime Yamaguchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.8.3
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.8.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.7.6
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.7.6
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ffi
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.9.10
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.9.10
|
83
|
+
description: Ruby binding to bitcoin's secp256k1 implementation.
|
84
|
+
email:
|
85
|
+
- gen.yamaguchi0@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".gitmodules"
|
92
|
+
- ".ruby-version"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- install_lib.sh
|
99
|
+
- lib/secp256k1.rb
|
100
|
+
- lib/secp256k1/c.rb
|
101
|
+
- lib/secp256k1/ecdsa.rb
|
102
|
+
- lib/secp256k1/key.rb
|
103
|
+
- lib/secp256k1/utils.rb
|
104
|
+
- lib/secp256k1/version.rb
|
105
|
+
- secp256k1-ruby.gemspec
|
106
|
+
- test/fixtures/ecdsa_sig.json
|
107
|
+
- test/fixtures/pubkey.json
|
108
|
+
- test/secp256k1_test.rb
|
109
|
+
homepage: https://github.com/Yamaguchi/secp256k1-ruby
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.7.3
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Ruby binding to bitcoin's secp256k1 implementation.
|
133
|
+
test_files:
|
134
|
+
- test/fixtures/ecdsa_sig.json
|
135
|
+
- test/fixtures/pubkey.json
|
136
|
+
- test/secp256k1_test.rb
|