Class: BitcoinCigs::PrivateKey

Inherits:
Object
  • Object
show all
Includes:
CryptoHelper
Defined in:
lib/bitcoin_cigs/private_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CryptoHelper

#decode58, #decode64, #decode_hex, #encode58, #encode64, #inverse_mod, #leftmost_bit, #ripemd160, #sha256, #sqrt_mod, #str_to_num

Constructor Details

#initialize(public_key, secret_multiplier) ⇒ PrivateKey

Returns a new instance of PrivateKey.



7
8
9
10
# File 'lib/bitcoin_cigs/private_key.rb', line 7

def initialize(public_key, secret_multiplier)
  self.public_key = public_key
  self.secret_multiplier = secret_multiplier
end

Instance Attribute Details

#public_keyObject

Returns the value of attribute public_key.



5
6
7
# File 'lib/bitcoin_cigs/private_key.rb', line 5

def public_key
  @public_key
end

#secret_multiplierObject

Returns the value of attribute secret_multiplier.



5
6
7
# File 'lib/bitcoin_cigs/private_key.rb', line 5

def secret_multiplier
  @secret_multiplier
end

Instance Method Details

#sign(hash, random_k) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bitcoin_cigs/private_key.rb', line 12

def sign(hash, random_k)
  hash = str_to_num(hash) if hash.is_a?(String)

  g = public_key.generator
  n = g.order
  k = random_k % n
  p1 = g * k
  r = p1.x
  raise raise ::BitcoinCigs::Error, "Random number r is 0" if r == 0
  
  s = (inverse_mod(k, n) * (hash + (secret_multiplier * r) % n)) % n
  raise raise ::BitcoinCigs::Error, "Random number s is 0" if s == 0
  
  Signature.new(r, s)
end