Class: Cryptos::PublicKey

Inherits:
Object
  • Object
show all
Includes:
Utils::Bytes, Utils::Hexas
Defined in:
lib/cryptos/public_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Bytes

#bignum_to_bytes, #bytes_to_bignum

Methods included from Utils::Hexas

#bignum_to_hex, #bin_to_hex, #byte_to_hex, #bytes_to_hex, #hex_size, #hex_to_little, #int_to_hex, #long_to_hex

Constructor Details

#initialize(private_key) ⇒ PublicKey

Returns a new instance of PublicKey.



7
8
9
10
# File 'lib/cryptos/public_key.rb', line 7

def initialize(private_key)
  @private_key = private_key
  @x, @y = *ec_multiply(private_key.value, EC_Gx, EC_Gy, EC_p)
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



5
6
7
# File 'lib/cryptos/public_key.rb', line 5

def private_key
  @private_key
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/cryptos/public_key.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/cryptos/public_key.rb', line 5

def y
  @y
end

Instance Method Details

#check!Object



12
13
14
# File 'lib/cryptos/public_key.rb', line 12

def check!
  (x**3 + 7 - y**2) % EC_p == 0 || raise('public key point is not on the curve')
end

#coordinatesObject



27
28
29
# File 'lib/cryptos/public_key.rb', line 27

def coordinates
  [x, y]
end

#to_sObject



31
32
33
# File 'lib/cryptos/public_key.rb', line 31

def to_s
  coordinates.to_s
end

#to_sec(compressed = true) ⇒ Object

Serialize public key as SEC (Standards for Efficient Cryptography) format

Parameters:

  • compressed (true, false) (defaults to: true)

    the format to return, either compressed or uncompressed

Returns:

  • address in SEC format



19
20
21
22
23
24
25
# File 'lib/cryptos/public_key.rb', line 19

def to_sec(compressed = true)
  if compressed
    "#{y.even? ? '02' : '03'}#{x_to_sec}"
  else
    "04#{x_to_sec}#{y_to_sec}"
  end
end