elliptic 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff30ac78fc3b9f3528bec550afdfffc4bdc7c4f7049edf67280ba48150d6ce95
4
- data.tar.gz: bb13fa5c43a30149dab2457a7ef78ec3c6478122e80c8fd450b99ed8e6998f31
3
+ metadata.gz: ef99a20d778ba1cd9c3ea51cfb6869ffe3d120de2be75715805d186014ebf38a
4
+ data.tar.gz: ac4037e5b01cf28cd46bfcf1d7278382d0d15157da228ed37b2cdf2e55610fc2
5
5
  SHA512:
6
- metadata.gz: 48c3b6ffda0ee6f94aa43fe31fc963be9228aba19adff661ad63bbfc8737be084d8db2c412cee9fa85e7cb9d8fee97fb6e46eb0d752dfdc2bbf6ba0dcf6d322b
7
- data.tar.gz: fb14d75d44756fe762f86b81425d6e13796bca980a3b8a5bbecef824fe363085366d3af51a6fffbd5de28d113af304960de1b6a06289cf3138c89a2e7915dc3c
6
+ metadata.gz: cca7de548087b2bc5f713f26fa76e405802ad1125d8a22b92577324b13f32086322938993ce3f6c45596f307af9abe835afd713870c87d7acdcfcfe404094576
7
+ data.tar.gz: 45ff2ebc47de2509e73bdb6c0623e63419a15d0b8294ef12e5f69bc568a0ab99b6afe0adf538e1c6d56a49f53c7da2343087827059d36b3a45e9e2adb641715b
data/README.md CHANGED
@@ -28,7 +28,7 @@ Maximum value of 2^256-1 =
28
28
  ``` ruby
29
29
  2**256-1
30
30
  #=> 115792089237316195423570985008687907853269984665640564039457584007913129639935
31
- (2**256-1).to_s.length
31
+ (2**256-1).digits.size # or to_s.length
32
32
  #=> 78
33
33
  ```
34
34
 
@@ -41,12 +41,12 @@ in hexadecimal (base 16) and binary (base 2) format?
41
41
  ``` ruby
42
42
  (2**256-1).to_s(16)
43
43
  #=> "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
44
- (2**256-1).to_s(16).length
44
+ (2**256-1).digits(16).size # or to_s(16).length
45
45
  #=> 64
46
46
 
47
47
  (2**256-1).to_s(2)
48
48
  #=> "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
49
- (2**256-1).to_s(2).length
49
+ (2**256-1).digits(2).size # or to_s(2).length
50
50
  #=> 256
51
51
  ```
52
52
 
@@ -77,15 +77,17 @@ require 'elliptic'
77
77
  private_key = EC::PrivateKey.generate # by default uses Secp256k1 curve (used in Bitcoin and Ethereum)
78
78
 
79
79
  private_key.to_i
80
- #=> 72190737707147846840353520312904745954595478835413056312168022784020322830309
80
+ #=> 29170346885894798724849267297784761178669026868482995474159965944722616190552
81
+ private_key.to_s
82
+ #=> "407dd4ccde53d30f3a9cda74ceccb247f3997466964786b59e4d68e93e8f8658"
81
83
  ```
82
84
 
83
85
 
84
- ### (Auto-)Calculate the Public Key - Enter Elliptic Curve (EC) Cryptography
86
+ ### Derive / (Auto-)Calculate the Public Key - Enter Elliptic Curve (EC) Cryptography
85
87
 
86
- The public key are two numbers (that is, a point with the coordinates x and y) computed by multiplying
87
- the generator point (`G`) of the curve with the private key.
88
- This is equivalent to adding the generator to itself `private_key` times.
88
+ The public key (`K`) are two numbers (that is, a point with the coordinates x and y) computed by multiplying
89
+ the generator point (`G`) of the curve with the private key (`k`) e.g. `K=k*G`.
90
+ This is equivalent to adding the generator to itself `k` times.
89
91
  Magic?
90
92
  Let's try:
91
93
 
@@ -266,7 +268,7 @@ public_key.to_pem
266
268
 
267
269
  To export a private or public key to
268
270
  the (binary) Distinguished Encoding Rules (DER)
269
- in ASN.1 format use `to_der`:
271
+ in Abstract Syntax Notation One (ASN.1) format use `to_der`:
270
272
 
271
273
  ``` ruby
272
274
  private_key.to_der
@@ -286,7 +288,7 @@ public_key.to_der
286
288
 
287
289
  To export a private or public key to
288
290
  the Base64-encoded Distinguished Encoding Rules (DER)
289
- in ASN.1 format use `to_base64`:
291
+ in Abstract Syntax Notation One (ASN.1) format use `to_base64`:
290
292
 
291
293
  ``` ruby
292
294
  private_key.to_base64
@@ -2,6 +2,8 @@ require 'pp'
2
2
  require 'digest'
3
3
  require 'base64'
4
4
  require 'openssl'
5
+ require 'securerandom'
6
+
5
7
 
6
8
  ## our own code
7
9
  require 'elliptic/version' # note: let version always go first
@@ -13,10 +15,11 @@ require 'elliptic/signature'
13
15
 
14
16
  module EC
15
17
 
18
+ SECP256K1 = Secp256k1 = OpenSSL::PKey::EC::Group.new( 'secp256k1' )
16
19
  ## "cached" / available groups for now include:
20
+ ## todo/check: change group to curve - why? why not?
17
21
  GROUP = {
18
- ## todo/check: is there a more direct way to get a group object?
19
- 'secp256k1' => OpenSSL::PKey::EC.new( 'secp256k1' ).group
22
+ 'secp256k1' => SECP256K1,
20
23
  }
21
24
 
22
25
 
@@ -28,6 +31,8 @@ module EC
28
31
  @pt = args[0]
29
32
 
30
33
  ## todo/check: is there a "better" way to get the x/y numbers?
34
+ ## note: octet_string is just a fancy name (technial term) for
35
+ ## byte/binary string (where a byte is 8-bit, thus octet)
31
36
  hex = @pt.to_octet_string( :uncompressed ).unpack( 'H*' )[0]
32
37
 
33
38
  ## todo/fix: check for infinity / 0 !!!!
@@ -38,14 +43,21 @@ module EC
38
43
 
39
44
  @x = args[0]
40
45
  @y = args[1]
41
-
42
46
  ## encoded_point is the octet string representation of the point.
43
47
  ## This must be either a String or an OpenSSL::BN
44
- hex = '04' + ("%064x" % @x) + ("%064x" % @y)
48
+ ## was: ("%064x" % @x) + ("%064x" % @y)
49
+ hex = '04' +
50
+ @x.to_s(16).rjust(64, '0') +
51
+ @y.to_s(16).rjust(64, '0')
45
52
  bin = [hex].pack( 'H*' )
46
53
 
47
54
  ec_group = GROUP[ group || 'secp256k1' ]
48
55
  @pt = OpenSSL::PKey::EC::Point.new( ec_group, bin )
56
+
57
+ ### or use hex e.g.
58
+ ## hex = '04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284'
59
+ ## bn = OpenSSL::BN.new(hex, 16) # note: 16=Hexadecimal string encoding
60
+ ## OpenSSL::PKey::EC::Point.new( ec_group, bn )
49
61
  end
50
62
  end
51
63
 
@@ -47,9 +47,14 @@ class PrivateKey
47
47
 
48
48
 
49
49
  def to_i() @pkey.private_key.to_i; end
50
+
50
51
  ## todo/check/fix: make it always a 32 byte (64 hex chars) string
51
52
  ## even with leading zeros !!! - why? why not?
52
- def to_s() @pkey.private_key.to_i.to_s(16); end
53
+ ## todo/check - add hex alias - why? why not?
54
+ def to_s
55
+ ## todo/fix: use number of bytes depending on curve (e.g. secp256k1 = 32-byte/256-bit)
56
+ @pkey.private_key.to_i.to_s(16).rjust(64, '0'); # convert to hex and make sure it's 32 bytes (64 characters)
57
+ end
53
58
 
54
59
 
55
60
  def to_pem() @pkey.to_pem; end
@@ -5,7 +5,7 @@ module EC
5
5
 
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- PATCH = 1
8
+ PATCH = 2
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
11
11
  def self.version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elliptic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-24 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc