Class: BSVShimECPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/primitives/openssl_ec_shim.rb

Overview

Shim Point wrapping BSV::Primitives::Secp256k1::Point.

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, bn = nil) ⇒ BSVShimECPoint

Returns a new instance of BSVShimECPoint.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 48

def initialize(group, bn = nil)
  @group = group
  if bn.nil?
    @secp_point = BSV::Primitives::Secp256k1::Point.infinity
  else
    bytes = bn.to_s(2)
    begin
      @secp_point = BSV::Primitives::Secp256k1::Point.from_bytes(bytes)
    rescue ArgumentError => e
      raise Error, e.message
    end
  end
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



46
47
48
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 46

def group
  @group
end

Class Method Details

.from_secp_point(group, secp_point) ⇒ Object



62
63
64
65
66
67
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 62

def self.from_secp_point(group, secp_point)
  pt = allocate
  pt.instance_variable_set(:@group, group)
  pt.instance_variable_set(:@secp_point, secp_point)
  pt
end

Instance Method Details

#add(other) ⇒ Object



101
102
103
104
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 101

def add(other)
  result = @secp_point.add(other.instance_variable_get(:@secp_point))
  self.class.from_secp_point(@group, result)
end

#infinity?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 114

def infinity?
  @secp_point.infinity?
end

#mul(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 69

def mul(*args)
  if args.length == 1
    scalar = bn_to_int(args[0])
    result = @secp_point.mul(scalar)
    self.class.from_secp_point(@group, result)
  elsif args.length == 2
    bns = args[0]
    points = args[1]
    result = @secp_point.mul(bn_to_int(bns[0]))
    points.each_with_index do |pt, i|
      term = pt.instance_variable_get(:@secp_point).mul(bn_to_int(bns[i + 1]))
      result = result.add(term)
    end
    self.class.from_secp_point(@group, result)
  else
    raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1 or 2)"
  end
end

#mul_ct(scalar_bn) ⇒ BSVShimECPoint

Constant-time scalar multiplication via the Montgomery ladder.

Delegates to BSV::Primitives::Secp256k1::Point#mul_ct to ensure secret-scalar paths execute in constant time.

Parameters:

  • scalar_bn (OpenSSL::BN, Integer)

    the secret scalar

Returns:



95
96
97
98
99
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 95

def mul_ct(scalar_bn)
  scalar = bn_to_int(scalar_bn)
  result = @secp_point.mul_ct(scalar)
  self.class.from_secp_point(@group, result)
end

#set_to_infinity!Object



118
119
120
121
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 118

def set_to_infinity!
  @secp_point = BSV::Primitives::Secp256k1::Point.infinity
  self
end

#to_bn(format = :compressed) ⇒ Object



110
111
112
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 110

def to_bn(format = :compressed)
  OpenSSL::BN.new(to_octet_string(format), 2)
end

#to_octet_string(format = :compressed) ⇒ Object



106
107
108
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 106

def to_octet_string(format = :compressed)
  @secp_point.to_octet_string(format)
end