Class: Confium::TC::ShareFile

Inherits:
Object
  • Object
show all
Defined in:
lib/confium/tc/share_file.rb

Overview

Filesystem-backed share persistence.

Share blobs produced by Confium::TC::Cmp20.keygen / Confium::TC::Gg18.keygen are 71-byte binary strings. This class wraps them in a JSON envelope so they can be saved to disk, transferred between hosts, and loaded back without encoding ambiguity.

The envelope format:

{
  "scheme":      "CMP20-ECDSA-P256",
  "threshold":   3,
  "party_count": 5,
  "public_key":  "<33-byte hex>",
  "shares":      ["<71-byte hex>", ...]
}

The format is identical to what the Python binding's confium.tc.ShareFile produces, so shares saved from one binding can be loaded by the other.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheme:, threshold:, party_count:, public_key:, shares:) ⇒ ShareFile

Returns a new instance of ShareFile.



32
33
34
35
36
37
38
# File 'lib/confium/tc/share_file.rb', line 32

def initialize(scheme:, threshold:, party_count:, public_key:, shares:)
  @scheme = scheme
  @threshold = threshold
  @party_count = party_count
  @public_key = public_key
  @shares = shares
end

Instance Attribute Details

#party_countObject (readonly)

Returns the value of attribute party_count.



30
31
32
# File 'lib/confium/tc/share_file.rb', line 30

def party_count
  @party_count
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



30
31
32
# File 'lib/confium/tc/share_file.rb', line 30

def public_key
  @public_key
end

#schemeObject (readonly)

Returns the value of attribute scheme.



30
31
32
# File 'lib/confium/tc/share_file.rb', line 30

def scheme
  @scheme
end

#sharesObject (readonly)

Returns the value of attribute shares.



30
31
32
# File 'lib/confium/tc/share_file.rb', line 30

def shares
  @shares
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



30
31
32
# File 'lib/confium/tc/share_file.rb', line 30

def threshold
  @threshold
end

Class Method Details

.from_json(json) ⇒ Object

Parse a ShareFile from a JSON string.



46
47
48
49
50
51
52
53
54
55
# File 'lib/confium/tc/share_file.rb', line 46

def self.from_json(json)
  d = JSON.parse(json)
  new(
    scheme: d.fetch("scheme"),
    threshold: d.fetch("threshold"),
    party_count: d.fetch("party_count"),
    public_key: [d.fetch("public_key")].pack("H*"),
    shares: d.fetch("shares").map { |h| [h].pack("H*") },
  )
end

.from_keygen(scheme_name, keygen_result) ⇒ Object

Build a ShareFile from a CMP20 / GG18 keygen result Hash.



76
77
78
79
80
81
82
83
84
# File 'lib/confium/tc/share_file.rb', line 76

def self.from_keygen(scheme_name, keygen_result)
  new(
    scheme: scheme_name,
    threshold: nil,        # not carried by the keygen Hash; caller knows
    party_count: keygen_result["shares"].length,
    public_key: keygen_result["public_key"],
    shares: keygen_result["shares"],
  )
end

.load(path) ⇒ Object

Load a ShareFile from a JSON file at path.



41
42
43
# File 'lib/confium/tc/share_file.rb', line 41

def self.load(path)
  from_json(File.read(path))
end

Instance Method Details

#save(path) ⇒ Object

Save to path as JSON. Creates parent directories if missing.



58
59
60
61
62
# File 'lib/confium/tc/share_file.rb', line 58

def save(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_json)
  self
end

#to_json(*_args) ⇒ Object

Serialize to a JSON string.



65
66
67
68
69
70
71
72
73
# File 'lib/confium/tc/share_file.rb', line 65

def to_json(*_args)
  JSON.generate(
    scheme: scheme,
    threshold: threshold,
    party_count: party_count,
    public_key: public_key.unpack1("H*"),
    shares: shares.map { |s| s.unpack1("H*") },
  )
end