Files
corona/reshare/keyshare_test.go
zeekay 261ad9d290 pulsar: lattice threshold kernel with key-era lifecycle + Pulsar-SHA3 + Nebula binding
Forked from github.com/luxfi/corona @ d09b2c2. Pulsar inherits the
2-round signing math byte-equal but adds blockchain-grade key
lifecycle and a SHA3-based domain-separated hash profile for
permissionless validator-set rotation.

Inherits from upstream Corona (byte-equal):
  - Sign1/Sign2/Combine 2-round signing math
  - Lattice ring R_q = Z_q[X]/(X^256+1), q = 0x1000000004A01
  - Module-LWE / Module-SIS hardness
  - NTT, Discrete-Gaussian Ziggurat, MAC layer

Replaces from upstream:
  - Broken Feldman-style DKG (pseudoinverse-recoverable). Replaced
    with: (a) one-time foundation MPC ceremony / trusted-dealer
    Bootstrap; (b) Pedersen DKG over R_q (research, dkg2/) hiding
    under MLWE on B / binding under MSIS on [A | B].
  - Trusted-dealer-per-epoch lifecycle (wrong shape for permissionless
    rotation). Replaced with Verifiable Secret Resharing (VSR) every
    epoch under the persistent group public key.

Key-era lifecycle (KeyEraID / Generation / RollbackFrom):
  - KeyEraID bumps only at Reanchor (rare governance event)
  - Generation bumps every Refresh / Reshare under the same GroupKey
  - RollbackFrom records prior generation reverted from (0 = forward)
  - Activation cert (QUASAR-PULSAR-ACTIVATE-v1) gates new generations
    under the unchanged GroupKey

Pulsar-SHA3 hash profile (NIST FIPS 202 / SP 800-185):
  - Hc:        cSHAKE256("PULSAR-HC-v1", transcript) → challenge
  - Hu:        cSHAKE256("PULSAR-HU-v1", transcript) → XOF stream
  - TranscriptHash: TupleHash256("PULSAR-TRANSCRIPT-v1", parts...)
  - PRF:       KMAC256(key, msg, "PULSAR-PRF-v1")
  - MAC:       KMAC256(key, msg, "PULSAR-MAC-v1")
  - Pairwise:  KMAC256(kex, encode(...), "PULSAR-PAIRWISE-v1")
  - Default suite is Pulsar-SHA3; Pulsar-BLAKE3 retained as optional
    non-normative fast profile. HashSuiteID is bound into transcripts.
  - Implementation at pulsar/hash/ with HashSuite interface.
  - All KATs regenerated under SHA3 profile; vectors in
    luxcpp/crypto/pulsar/test/kat/.

Nebula root binding (TranscriptInputs + ActivationMessage canonical
bytes, all bound under TupleHash256):
    chain_id, network_id, group_id,
    key_era_id, old_generation, new_generation,
    old_epoch_id, new_epoch_id,
    old_set_hash, new_set_hash,
    threshold_old, threshold_new,
    group_public_key_hash,
    nebula_root,
    hash_suite_id,
    implementation_version,
    variant.

Packages:
  primitives/   Shamir over R_q, Lagrange, hash helpers
  sign/         2-round sign math (byte-equal upstream)
  threshold/    GroupKey, KeyShare, Signer types
  reshare/      VSR kernel — Refresh (HJKY97 zero-poly), Reshare
                (Desmedt-Jajodia), commit (Pedersen R_q), complaint,
                transcript, pairwise KEX, activation cert
  hash/         HashSuite interface; PulsarSHA3 (default), PulsarBLAKE3
  dkg2/         Pedersen DKG over R_q (research, reference only)
  keyera/       Lifecycle wrapper: Bootstrap → Reshare → Reanchor
  papers/       LP-073-pulsar paper (sections + bibliography)
  cmd/*_oracle/ KAT generators (Go ↔ C++ byte-equal validation)

DESIGN.md is the single source of truth:
  - The Pulsar metaphor (rotating beam over persistent body)
  - Vocabulary stack per LP-105: Nebula / Photon / Lumen / Beam /
    Pulsar / Pulse / Prism / Horizon / Quasar
  - Pulsar ≠ Lumen (PQ stream is separate, planned)
  - Pulsar / Lens / LSS three-layer architecture
  - Bootstrap Dealer vs Signature Coordinator
  - No-slashing failure ladder (timeout → retry → rollback → reanchor)
  - MVP VSR vs Robust VSR phasing
  - Borrowed-brand terms ("X-Wing" used casually) replaced with
    "hybrid KEM" / "Hybrid Lumen Handshake"; X-Wing cited only as
    the IETF combiner primitive name where exact reference matters.

Tests: full suite green
  hash 5+/5+, keyera 5/5, reshare 45+/45+, threshold, sign, dkg,
  dkg2, primitives, networking, utils, corona_oracle_v2 KAT —
  all pass.

Honest claim: the crypto primitives are not new (HJKY97,
Desmedt-Jajodia97, Wong-Wang-Wing02, Corona, Hermine, Threshold
Raccoon all exist). The novel contribution is the systems
composition: a permissionless-consensus deployment architecture that
turns static two-round PQ threshold signatures into a dynamic,
leaderless, validator-rotation-tolerant finality layer.

Companion docs: papers/lp-073-pulsar/ (academic paper); LP-073 +
LP-103 + LP-105 in github.com/luxfi/lps; threshold/protocols/lss/
lss_pulsar.go (LSS adapter wrapping this kernel).
2026-03-03 12:00:00 -08:00

187 lines
4.8 KiB
Go

// Copyright (C) 2025-2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package reshare
import (
"bytes"
"testing"
"github.com/luxfi/lattice/v7/ring"
)
// TestPartyKeyShareFromShareFullPath — round-trips a Shamir share into
// a complete PartyKeyShare and confirms every field is populated.
func TestPartyKeyShareFromShareFullPath(t *testing.T) {
r := canonicalRing(t)
const tThr, n = 3, 5
secret := pickSecret(r, "keyshare-full", testNVec)
rs := newFakeRand([]byte("keyshare-shamir"))
oldShares, err := makeStandardShamirShares(r, secret, tThr, n, rs)
if err != nil {
t.Fatal(err)
}
// Reshare onto a new committee.
const tNew = 3
newCommittee := []int{10, 11, 12, 13, 14}
reshared, err := Reshare(r, oldShares, tThr, newCommittee, tNew,
newFakeRand([]byte("keyshare-reshare")))
if err != nil {
t.Fatal(err)
}
// Build pairwise material. In the kernel test we use deterministic
// fakes; production wires this through AuthenticatedKex (see
// pairwise.go).
K := len(newCommittee)
authKex := make(map[[2]int][]byte, K*K)
selfSeeds := make(map[int][]byte, K)
for i := 0; i < K; i++ {
selfSeeds[i] = []byte{byte(0xA0 + i), 0x11, 0x22, 0x33}
for j := i + 1; j < K; j++ {
authKex[[2]int{i, j}] = []byte{byte(i), byte(j), 0xCC, 0xDD}
}
}
chainID := []byte("test")
groupID := []byte("g0")
pseudoEpoch := uint64(7)
pairwiseSeeds, err := DeriveSeeds(K, authKex, selfSeeds, chainID, groupID, 0, pseudoEpoch, nil, 32)
if err != nil {
t.Fatal(err)
}
pairwiseMACs, err := DeriveMACKeys(K, authKex, chainID, groupID, 0, pseudoEpoch, nil, 32)
if err != nil {
t.Fatal(err)
}
groupKey := &PartyGroupKey{}
// Build the PartyKeyShare for one new party.
myID := newCommittee[2] // 12
pks, err := PartyKeyShareFromShare(
r, reshared[myID], myID, newCommittee,
pairwiseSeeds, pairwiseMACs, groupKey,
)
if err != nil {
t.Fatal(err)
}
// Sanity checks: every field populated.
if pks.Index != 2 {
t.Errorf("Index: got %d, want 2", pks.Index)
}
if pks.SkShare == nil || len(pks.SkShare) == 0 {
t.Error("SkShare empty")
}
if pks.GroupKey != groupKey {
t.Error("GroupKey not propagated as pointer")
}
if pks.Lambda.Coeffs == nil {
t.Error("Lambda not populated")
}
// Lambda must be in NTT-Mont form (not all-zero, not equal to a
// raw scalar — there's no easy invariant to check beyond
// non-zero-ness without re-running the helper).
if allZeroPoly(pks.Lambda) {
t.Error("Lambda is all zero")
}
if pks.Seeds == nil {
t.Fatal("Seeds map empty")
}
if len(pks.Seeds) != K {
t.Errorf("Seeds map size: got %d, want %d", len(pks.Seeds), K)
}
for i := 0; i < K; i++ {
row := pks.Seeds[i]
if row == nil {
t.Errorf("Seeds[%d] missing", i)
continue
}
if len(row) != K {
t.Errorf("Seeds[%d] length: got %d, want %d", i, len(row), K)
}
}
// MACKeys[i] for i != myIdx should be non-nil and match the
// canonical pairwise derivation.
for k := 0; k < K; k++ {
if k == 2 {
if _, ok := pks.MACKeys[k]; ok {
t.Errorf("MACKeys[self=%d] should not exist", k)
}
continue
}
if mk := pks.MACKeys[k]; len(mk) == 0 {
t.Errorf("MACKeys[%d] missing", k)
}
}
}
// TestEraseShareZeroes — every coefficient of every poly in the share
// is zero after EraseShare.
func TestEraseShareZeroes(t *testing.T) {
r := canonicalRing(t)
secret := pickSecret(r, "erase", testNVec)
rs := newFakeRand([]byte("erase-rs"))
shares, err := makeStandardShamirShares(r, secret, 3, 5, rs)
if err != nil {
t.Fatal(err)
}
for _, sh := range shares {
// Check non-trivial before erase (overwhelming probability).
if allZeroShare(sh) {
t.Fatal("share is all-zero before erase (test setup broken)")
}
EraseShare(sh)
if !allZeroShare(sh) {
t.Fatal("EraseShare did not zero all coefficients")
}
}
}
// TestPartyKeyShareFromShareRejectsMissingParty — a party not in the
// new committee triggers a clear error.
func TestPartyKeyShareFromShareRejectsMissingParty(t *testing.T) {
r := canonicalRing(t)
secret := pickSecret(r, "missing-party", testNVec)
rs := newFakeRand([]byte("mp-shamir"))
old, _ := makeStandardShamirShares(r, secret, 2, 3, rs)
reshared, _ := Reshare(r, old, 2, []int{10, 11, 12}, 2, newFakeRand([]byte("mp-rng")))
_, err := PartyKeyShareFromShare(r, reshared[10], 99, []int{10, 11, 12},
map[[2]int][]byte{}, map[[2]int][]byte{}, nil)
if err == nil {
t.Fatal("expected error for missing party")
}
if !bytes.Contains([]byte(err.Error()), []byte("not in new committee")) {
t.Fatalf("unexpected error: %v", err)
}
}
func allZeroShare(s Share) bool {
for _, p := range s {
for _, level := range p.Coeffs {
for _, c := range level {
if c != 0 {
return false
}
}
}
}
return true
}
func allZeroPoly(p ring.Poly) bool {
for _, level := range p.Coeffs {
for _, c := range level {
if c != 0 {
return false
}
}
}
return true
}