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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/elliptic.rb', line 25

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



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

def group() @pt.group; end

#to_bin(format = :uncompressed) ⇒ Object

formats - :compressed | :uncompressed



57
58
59
# File 'lib/elliptic.rb', line 57

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?



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

def to_ec_point()  @pt; end

#to_s(format = :uncompressed) ⇒ Object



61
62
63
# File 'lib/elliptic.rb', line 61

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

#xObject



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

def x()  @x; end

#yObject



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

def y()  @y; end