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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elliptic.rb', line 50

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?
     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
     hex = '04' + ("%064x" % @x) + ("%064x" % @y)
     bin = [hex].pack( 'H*' )

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

Instance Method Details

#groupObject



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

def group() @pt.group; end

#to_bin(format = :uncompressed) ⇒ Object

formats - :compressed | :uncompressed



82
83
84
# File 'lib/elliptic.rb', line 82

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?



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

def to_ec_point()  @pt; end

#to_s(format = :uncompressed) ⇒ Object



86
87
88
# File 'lib/elliptic.rb', line 86

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

#xObject



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

def x()  @x; end

#yObject



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

def y()  @y; end