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



128
129
130
131
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 128

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)


141
142
143
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 141

def infinity?
  @secp_point.infinity?
end

#mul(scalar_bn) ⇒ BSVShimECPoint #mul(bns, points) ⇒ BSVShimECPoint

Scalar multiplication: self * scalar (constant-time, Montgomery ladder).

Matches OpenSSL convention where EC_POINT_mul is always constant-time. Safe for both secret and public scalars.

Also supports the multi-scalar form: mul(bns, points) computes bns[0]*self + bns[1]*points[0] + ... where bns.length == points.length 1+.

Overloads:

  • #mul(scalar_bn) ⇒ BSVShimECPoint

    Parameters:

    • scalar_bn (OpenSSL::BN, Integer)

      the scalar multiplier

  • #mul(bns, points) ⇒ BSVShimECPoint

    Parameters:

    • bns (Array<OpenSSL::BN>)

      scalars; must have points.length 1+ elements

    • points (Array<BSVShimECPoint>)

      additional points

    Raises:

    • (NoMethodError)

      if bns and points lengths are mismatched

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 85

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 (alias for #mul).

Retained for backward compatibility and expressiveness. Delegates to #mul, which is constant-time by default.

Parameters:

  • scalar_bn (OpenSSL::BN, Integer)

    the scalar multiplier

Returns:



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

def mul_ct(scalar_bn)
  mul(scalar_bn)
end

#mul_vt(scalar_bn) ⇒ BSVShimECPoint

Variable-time scalar multiplication (wNAF).

Faster than #mul but leaks timing information about the scalar. Use only for public scalars (e.g. signature verification).

Parameters:

  • scalar_bn (OpenSSL::BN, Integer)

    the public scalar multiplier

Returns:



122
123
124
125
126
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 122

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

#set_to_infinity!Object



145
146
147
148
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 145

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

#to_bn(format = :compressed) ⇒ Object



137
138
139
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 137

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

#to_octet_string(format = :compressed) ⇒ Object



133
134
135
# File 'lib/bsv/primitives/openssl_ec_shim.rb', line 133

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