ruby-bitcoin-secp256k1 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.gitmodules +3 -0
- data/.travis.yml +61 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +25 -0
- data/LICENSE +21 -0
- data/README.md +54 -0
- data/Rakefile +9 -0
- data/install_lib.sh +7 -0
- data/lib/bitcoin_secp256k1/c.rb +107 -0
- data/lib/bitcoin_secp256k1/ecdsa.rb +102 -0
- data/lib/bitcoin_secp256k1/key.rb +252 -0
- data/lib/bitcoin_secp256k1/utils.rb +22 -0
- data/lib/bitcoin_secp256k1/version.rb +4 -0
- data/lib/bitcoin_secp256k1.rb +21 -0
- data/ruby-bitcoin-secp256k1.gemspec +23 -0
- data/test/fixtures/ecdsa_sig.json +999 -0
- data/test/fixtures/pubkey.json +1749 -0
- data/test/secp256k1_test.rb +112 -0
- metadata +121 -0
@@ -0,0 +1,112 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
3
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'bitcoin_secp256k1'
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class MyECDSA < BitcoinSecp256k1::BaseKey
|
11
|
+
include BitcoinSecp256k1::Utils, BitcoinSecp256k1::ECDSA
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super(nil, BitcoinSecp256k1::ALL_FLAGS)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class BitcoinSecp256k1Test < Minitest::Test
|
19
|
+
include BitcoinSecp256k1
|
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_verity
|
41
|
+
pk = PrivateKey.new
|
42
|
+
raw_sig = pk.ecdsa_sign 'test'
|
43
|
+
assert pk.pubkey.ecdsa_verify('test', raw_sig)
|
44
|
+
assert !pk.pubkey.ecdsa_verify('testtest', raw_sig)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_ecdsa_compact
|
48
|
+
pk = PrivateKey.new
|
49
|
+
raw_sig = pk.ecdsa_sign 'test'
|
50
|
+
assert_equal true, pk.pubkey.ecdsa_verify('test', raw_sig)
|
51
|
+
|
52
|
+
compact = pk.ecdsa_serialize_compact raw_sig
|
53
|
+
assert_equal 64, compact.size
|
54
|
+
|
55
|
+
sig_raw = pk.ecdsa_deserialize_compact compact
|
56
|
+
assert_equal compact, pk.ecdsa_serialize_compact(sig_raw)
|
57
|
+
assert_equal true, pk.pubkey.ecdsa_verify('test', sig_raw)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_ecdsa_normalize
|
61
|
+
pk = PrivateKey.new
|
62
|
+
raw_sig = pk.ecdsa_sign 'hi'
|
63
|
+
|
64
|
+
had_to_normalize, normsig = pk.ecdsa_signature_normalize raw_sig
|
65
|
+
assert_equal false, had_to_normalize
|
66
|
+
assert_equal pk.ecdsa_serialize(raw_sig), pk.ecdsa_serialize(normsig)
|
67
|
+
assert_equal pk.ecdsa_serialize_compact(raw_sig), pk.ecdsa_serialize_compact(normsig)
|
68
|
+
|
69
|
+
had_to_normalize, normsig = pk.ecdsa_signature_normalize(raw_sig, check_only: true)
|
70
|
+
assert_equal false, had_to_normalize
|
71
|
+
assert_nil normsig
|
72
|
+
|
73
|
+
sig = "\xAA" + "\xFF"*31 + "\xAA" + "\xFF"*31
|
74
|
+
raw_sig = pk.ecdsa_deserialize_compact sig
|
75
|
+
|
76
|
+
normalized, normsig = pk.ecdsa_signature_normalize raw_sig
|
77
|
+
assert_equal true, normalized
|
78
|
+
assert pk.ecdsa_serialize(raw_sig) != pk.ecdsa_serialize(normsig)
|
79
|
+
|
80
|
+
normalized, normsig = pk.ecdsa_signature_normalize raw_sig, check_only: true
|
81
|
+
assert_equal true, normalized
|
82
|
+
assert_nil normsig
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_ecdsa_recover
|
86
|
+
return unless C.module_recovery_enabled?
|
87
|
+
|
88
|
+
pk = PrivateKey.new
|
89
|
+
unrelated = MyECDSA.new
|
90
|
+
|
91
|
+
recsig = pk.ecdsa_sign_recoverable 'hello'
|
92
|
+
pubkey = unrelated.ecdsa_recover 'hello', recsig
|
93
|
+
pubser = PublicKey.new(pubkey: pubkey).serialize
|
94
|
+
assert_equal pubser, pk.pubkey.serialize
|
95
|
+
|
96
|
+
recsig_ser = unrelated.ecdsa_recoverable_serialize recsig
|
97
|
+
recsig2 = unrelated.ecdsa_recoverable_deserialize(*recsig_ser)
|
98
|
+
pubkey2 = unrelated.ecdsa_recover 'hello', recsig2
|
99
|
+
pubser2 = PublicKey.new(pubkey: pubkey2).serialize
|
100
|
+
assert_equal pubser, pubser2
|
101
|
+
|
102
|
+
raw_sig = unrelated.ecdsa_recoverable_convert recsig2
|
103
|
+
unrelated.ecdsa_deserialize(unrelated.ecdsa_serialize(raw_sig))
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def ecdsa_sig
|
109
|
+
@ecdsa_sig = JSON.parse File.read(File.expand_path('../fixtures/ecdsa_sig.json', __FILE__))
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-bitcoin-secp256k1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Xie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.9.25
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.9.25
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.11.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.11.3
|
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.9.20
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.20
|
69
|
+
description: Ruby binding to bitcoin's secp256k1 implementation.
|
70
|
+
email:
|
71
|
+
- jan.h.xie@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".gitmodules"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- install_lib.sh
|
85
|
+
- lib/bitcoin_secp256k1.rb
|
86
|
+
- lib/bitcoin_secp256k1/c.rb
|
87
|
+
- lib/bitcoin_secp256k1/ecdsa.rb
|
88
|
+
- lib/bitcoin_secp256k1/key.rb
|
89
|
+
- lib/bitcoin_secp256k1/utils.rb
|
90
|
+
- lib/bitcoin_secp256k1/version.rb
|
91
|
+
- ruby-bitcoin-secp256k1.gemspec
|
92
|
+
- test/fixtures/ecdsa_sig.json
|
93
|
+
- test/fixtures/pubkey.json
|
94
|
+
- test/secp256k1_test.rb
|
95
|
+
homepage: https://github.com/mechanizm/ruby-bitcoin-secp256k1
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.1.4
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Ruby binding to bitcoin's secp256k1 implementation.
|
118
|
+
test_files:
|
119
|
+
- test/fixtures/ecdsa_sig.json
|
120
|
+
- test/fixtures/pubkey.json
|
121
|
+
- test/secp256k1_test.rb
|