Module: MoneyTree::OpenSSLExtensions

Extended by:
FFI::Library
Included in:
OpenSSL::PKey::EC::Point
Defined in:
lib/openssl_extensions.rb

Constant Summary collapse

NID_secp256k1 =
714
POINT_CONVERSION_COMPRESSED =
2
POINT_CONVERSION_UNCOMPRESSED =
4

Class Method Summary collapse

Class Method Details

.add(point_0, point_1) ⇒ Object



24
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/openssl_extensions.rb', line 24

def self.add(point_0, point_1)
  validate_points(point_0, point_1)
  eckey = EC_KEY_new_by_curve_name(NID_secp256k1)
  group = EC_KEY_get0_group(eckey)
  
  point_0_hex = point_0.to_bn.to_s(16)
  point_0_pt = EC_POINT_hex2point(group, point_0_hex, nil, nil)
  point_1_hex = point_1.to_bn.to_s(16)
  point_1_pt = EC_POINT_hex2point(group, point_1_hex, nil, nil)

  sum_point = EC_POINT_new(group)
  success = EC_POINT_add(group, sum_point, point_0_pt, point_1_pt, nil)
  hex = EC_POINT_point2hex(group, sum_point, POINT_CONVERSION_UNCOMPRESSED, nil)

  EC_KEY_free(eckey)
  EC_POINT_clear_free(sum_point)
  EC_POINT_clear_free(point_0_pt)
  EC_POINT_clear_free(point_1_pt)

  eckey = nil
  group = nil
  sum_point = nil
  point_0_pt = nil
  point_1_pt = nil

  hex
end

.validate_points(*points) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/openssl_extensions.rb', line 52

def self.validate_points(*points)
  points.each do |point|
    if !point.is_a?(OpenSSL::PKey::EC::Point)
      raise ArgumentError, "point must be an OpenSSL::PKey::EC::Point object" 
    elsif point.infinity?
      raise ArgumentError, "point must not be infinity" 
    end
  end
end