Class: EC::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/elliptic.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, group: nil) ⇒ Point

Returns a new instance of Point.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/elliptic.rb', line 28

def initialize( *args, group: nil )
  ## case 1) assume OpenSSL::PKey::EC::Point
  if args.size == 1 && args[0].is_a?( OpenSSL::PKey::EC::Point )
     @pt = args[0]

     ## todo/check: is there a "better" way to get the x/y numbers?
     ## note: octet_string is just a fancy name (technial term) for
     ##           byte/binary string (where a byte is 8-bit, thus octet)
     hex = @pt.to_octet_string( :uncompressed ).unpack( 'H*' )[0]

     ## todo/fix: check for infinity / 0 !!!!
     @x = hex[2,64].to_i(16)       ## skip leading 0x04 marker
     @y = hex[2+64,64].to_i(16)
  else  ## assume x,y with group
     ## rebuild openssl point from octet

     @x = args[0]
     @y = args[1]
     ## encoded_point is the octet string representation of the point.
     ## This must be either a String or an OpenSSL::BN
     ##   was: ("%064x" % @x) + ("%064x" % @y)
     hex = '04' +
           @x.to_s(16).rjust(64, '0') +
           @y.to_s(16).rjust(64, '0')
     bin = [hex].pack( 'H*' )

     ec_group = GROUP[ group || 'secp256k1' ]
     @pt = OpenSSL::PKey::EC::Point.new( ec_group, bin )

     ### or use hex e.g.
     ## hex = '04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284'
     ## bn = OpenSSL::BN.new(hex, 16)    # note: 16=Hexadecimal string encoding
     ## OpenSSL::PKey::EC::Point.new( ec_group, bn )
  end
end

Instance Method Details

#groupObject



66
# File 'lib/elliptic.rb', line 66

def group() @pt.group; end

#to_bin(format = :uncompressed) ⇒ Object

formats - :compressed | :uncompressed



69
70
71
# File 'lib/elliptic.rb', line 69

def to_bin( format=:uncompressed )  ## todo/check add alias .b too - why? why not?
  @pt.to_octet_string( format )
end

#to_ec_pointObject

return OpenSSL::PKey::EC::Point - find a better name? e.g. to_raw/native or such - why? why not?



78
# File 'lib/elliptic.rb', line 78

def to_ec_point()  @pt; end

#to_s(format = :uncompressed) ⇒ Object



73
74
75
# File 'lib/elliptic.rb', line 73

def to_s( format=:uncompressed )
  to_bin( format ).unpack( 'H*' )[0]
end

#xObject



64
# File 'lib/elliptic.rb', line 64

def x()  @x; end

#yObject



65
# File 'lib/elliptic.rb', line 65

def y()  @y; end