Class: BSV::Primitives::KeyShares

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

Overview

A set of Shamir’s Secret Sharing shares derived from a private key.

Holds the evaluation points (shares), a reconstruction threshold, and an integrity string for verifying that recombined shares produce the correct key. Supports serialisation to and from a human-readable backup format.

Backup format per share: “Base58(x).Base58(y).threshold.integrity”

This format is compatible with the Go and TypeScript reference SDKs.

Examples:

Round-trip through backup format

shares  = key.to_key_shares(2, 5)
backup  = shares.to_backup_format
rebuilt = KeyShares.from_backup_format(backup[0..1])
key     = PrivateKey.from_key_shares(rebuilt)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points, threshold, integrity) ⇒ KeyShares

Returns a new instance of KeyShares.

Parameters:

  • points (Array<PointInFiniteField>)

    share points

  • threshold (Integer)

    reconstruction threshold

  • integrity (String)

    integrity check string



33
34
35
36
37
# File 'lib/bsv/primitives/key_shares.rb', line 33

def initialize(points, threshold, integrity)
  @points    = points
  @threshold = threshold
  @integrity = integrity
end

Instance Attribute Details

#integrityString (readonly)

Returns first 8 hex characters of the Hash160 of the compressed public key.

Returns:

  • (String)

    first 8 hex characters of the Hash160 of the compressed public key



28
29
30
# File 'lib/bsv/primitives/key_shares.rb', line 28

def integrity
  @integrity
end

#pointsArray<PointInFiniteField> (readonly)

Returns the share points.

Returns:



22
23
24
# File 'lib/bsv/primitives/key_shares.rb', line 22

def points
  @points
end

#thresholdInteger (readonly)

Returns the minimum number of shares required to reconstruct the key.

Returns:

  • (Integer)

    the minimum number of shares required to reconstruct the key



25
26
27
# File 'lib/bsv/primitives/key_shares.rb', line 25

def threshold
  @threshold
end

Class Method Details

.from_backup_format(shares) ⇒ KeyShares

Deserialise shares from backup format strings.

Each string must have the form “Base58(x).Base58(y).threshold.integrity”. All shares must agree on threshold and integrity.

Parameters:

  • shares (Array<String>)

    backup-format share strings

Returns:

Raises:

  • (ArgumentError)

    if any share is malformed or shares are inconsistent



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bsv/primitives/key_shares.rb', line 47

def self.from_backup_format(shares)
  threshold = 0
  integrity = ''
  points    = shares.each_with_index.map do |share, idx|
    parts = share.split('.', -1)
    raise ArgumentError, "invalid share format in share #{idx}: expected 4 dot-separated parts, got #{share.inspect}" unless parts.length == 4

    x_str, y_str, t_str, i_str = parts

    raise ArgumentError, "threshold not found in share #{idx}" if t_str.empty?
    raise ArgumentError, "integrity not found in share #{idx}" if i_str.empty?

    t_int = Integer(t_str)

    if idx != 0
      raise ArgumentError, "threshold mismatch in share #{idx}" unless threshold == t_int
      raise ArgumentError, "integrity mismatch in share #{idx}" unless integrity == i_str
    end

    threshold = t_int
    integrity = i_str

    PointInFiniteField.from_string("#{x_str}.#{y_str}")
  end

  new(points, threshold, integrity)
end

Instance Method Details

#to_backup_formatArray<String>

Serialise shares to backup format strings.

Returns:

  • (Array<String>)

    one backup string per share point



78
79
80
# File 'lib/bsv/primitives/key_shares.rb', line 78

def to_backup_format
  @points.map { |point| "#{point}.#{@threshold}.#{@integrity}" }
end