Class: BSV::Primitives::PointInFiniteField

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

Overview

A point (x, y) in a finite field over the secp256k1 field prime P.

Used as a share in Shamir’s Secret Sharing Scheme. Both coordinates are reduced modulo P on construction so they always lie in [0, P).

Serialisation uses Base58 for each coordinate, joined by a dot, matching the format used by the Go and TypeScript reference SDKs.

Examples:

point = PointInFiniteField.new(OpenSSL::BN.new(10), OpenSSL::BN.new(20))
str   = point.to_s   #=> "C.N"
back  = PointInFiniteField.from_string(str)

Constant Summary collapse

P =

The secp256k1 field prime P.

OpenSSL::BN.new('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ PointInFiniteField

Returns a new instance of PointInFiniteField.

Parameters:

  • x (OpenSSL::BN)

    the x-coordinate

  • y (OpenSSL::BN)

    the y-coordinate



31
32
33
34
# File 'lib/bsv/primitives/point_in_finite_field.rb', line 31

def initialize(x, y)
  @x = umod(x, P)
  @y = umod(y, P)
end

Instance Attribute Details

#xOpenSSL::BN (readonly)

Returns the x-coordinate, reduced mod P.

Returns:

  • (OpenSSL::BN)

    the x-coordinate, reduced mod P



24
25
26
# File 'lib/bsv/primitives/point_in_finite_field.rb', line 24

def x
  @x
end

#yOpenSSL::BN (readonly)

Returns the y-coordinate, reduced mod P.

Returns:

  • (OpenSSL::BN)

    the y-coordinate, reduced mod P



27
28
29
# File 'lib/bsv/primitives/point_in_finite_field.rb', line 27

def y
  @y
end

Class Method Details

.from_string(str) ⇒ PointInFiniteField

Deserialise from a “Base58(x).Base58(y)” string.

Parameters:

  • str (String)

    dot-separated Base58-encoded coordinates

Returns:

Raises:

  • (ArgumentError)

    if the string does not contain exactly one dot



48
49
50
51
52
53
54
55
# File 'lib/bsv/primitives/point_in_finite_field.rb', line 48

def self.from_string(str)
  parts = str.split('.')
  raise ArgumentError, "invalid point string: expected 'x.y', got #{str.inspect}" unless parts.length == 2

  x = OpenSSL::BN.new(Base58.decode(parts[0]), 2)
  y = OpenSSL::BN.new(Base58.decode(parts[1]), 2)
  new(x, y)
end

Instance Method Details

#to_sString

Serialise to Base58(x) + “.” + Base58(y).

Returns:

  • (String)

    dot-separated Base58-encoded coordinates



39
40
41
# File 'lib/bsv/primitives/point_in_finite_field.rb', line 39

def to_s
  "#{Base58.encode(bn_to_bytes(@x))}.#{Base58.encode(bn_to_bytes(@y))}"
end