Files
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

378 lines
7.6 KiB
Go

package utils
import (
"testing"
"github.com/luxfi/lattice/v7/ring"
"github.com/luxfi/lattice/v7/utils/sampling"
"github.com/luxfi/lattice/v7/utils/structs"
)
func TestMatrixVectorMul(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
rows int
cols int
}{
{
name: "square matrix",
rows: 3,
cols: 3,
},
{
name: "rectangular matrix",
rows: 4,
cols: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create random matrix and vector
A := createTestMatrix(r, sampler, tt.rows, tt.cols)
v := createTestVector(r, sampler, tt.cols)
result := make(structs.Vector[ring.Poly], tt.rows)
for i := range result {
result[i] = r.NewPoly()
}
// Multiply
MatrixVectorMul(r, A, v, result)
// Check dimensions
if len(result) != tt.rows {
t.Errorf("MatrixVectorMul() result has length %d, want %d", len(result), tt.rows)
}
// Verify result is non-nil
for i, poly := range result {
if poly.N() == 0 {
t.Errorf("Result contains invalid polynomial at index %d", i)
}
}
})
}
}
func TestVectorAdd(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
size int
}{
{
name: "small vectors",
size: 3,
},
{
name: "large vectors",
size: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v1 := createTestVector(r, sampler, tt.size)
v2 := createTestVector(r, sampler, tt.size)
result := make(structs.Vector[ring.Poly], tt.size)
for i := range result {
result[i] = r.NewPoly()
}
VectorAdd(r, v1, v2, result)
// Check dimensions
if len(result) != tt.size {
t.Errorf("VectorAdd() result has length %d, want %d", len(result), tt.size)
}
})
}
}
func TestVectorSub(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
size int
}{
{
name: "small vectors",
size: 3,
},
{
name: "large vectors",
size: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v1 := createTestVector(r, sampler, tt.size)
v2 := createTestVector(r, sampler, tt.size)
result := make(structs.Vector[ring.Poly], tt.size)
for i := range result {
result[i] = r.NewPoly()
}
VectorSub(r, v1, v2, result)
// Check dimensions
if len(result) != tt.size {
t.Errorf("VectorSub() result has length %d, want %d", len(result), tt.size)
}
})
}
}
func TestNTTConversions(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
rows int
cols int
}{
{
name: "small matrix",
rows: 2,
cols: 2,
},
{
name: "large matrix",
rows: 3,
cols: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test matrix conversions
matrix := createTestMatrix(r, sampler, tt.rows, tt.cols)
// Store original values for comparison
original := make(structs.Matrix[ring.Poly], tt.rows)
for i := range original {
original[i] = make(structs.Vector[ring.Poly], tt.cols)
for j := range original[i] {
original[i][j] = *matrix[i][j].CopyNew()
}
}
// Convert to NTT and back
ConvertMatrixToNTT(r, matrix)
ConvertMatrixFromNTT(r, matrix)
// Verify round-trip (approximately - NTT may introduce small numerical differences)
for i := range matrix {
for j := range matrix[i] {
// Just verify they're still valid polynomials
if matrix[i][j].N() == 0 {
t.Errorf("Matrix NTT round-trip produced invalid polynomial at [%d][%d]", i, j)
}
}
}
// Test vector conversions
vector := createTestVector(r, sampler, tt.cols)
// Convert to NTT and back
ConvertVectorToNTT(r, vector)
ConvertVectorFromNTT(r, vector)
// Verify round-trip
for i := range vector {
if vector[i].N() == 0 {
t.Errorf("Vector NTT round-trip produced invalid polynomial at index %d", i)
}
}
})
}
}
func TestSamplePolyVector(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
size int
}{
{
name: "small vector",
size: 3,
},
{
name: "large vector",
size: 20,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := SamplePolyVector(r, tt.size, sampler, false, false)
// Check dimensions
if len(v) != tt.size {
t.Errorf("SamplePolyVector() returned vector of length %d, want %d", len(v), tt.size)
}
// Verify all polynomials are valid
for i, poly := range v {
if poly.N() == 0 {
t.Errorf("SamplePolyVector() returned invalid polynomial at index %d", i)
}
}
})
}
}
func TestSamplePolyMatrix(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
prng, _ := sampling.NewPRNG()
sampler := ring.NewUniformSampler(prng, r)
tests := []struct {
name string
rows int
cols int
}{
{
name: "small matrix",
rows: 2,
cols: 3,
},
{
name: "square matrix",
rows: 5,
cols: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := SamplePolyMatrix(r, tt.rows, tt.cols, sampler, false, false)
// Check dimensions
if len(m) != tt.rows {
t.Errorf("SamplePolyMatrix() returned %d rows, want %d", len(m), tt.rows)
}
if len(m) > 0 && len(m[0]) != tt.cols {
t.Errorf("SamplePolyMatrix() returned %d columns, want %d", len(m[0]), tt.cols)
}
// Verify all polynomials are valid
for i := range m {
for j := range m[i] {
if m[i][j].N() == 0 {
t.Errorf("SamplePolyMatrix() returned invalid polynomial at [%d][%d]", i, j)
}
}
}
})
}
}
func TestInitializeMatrix(t *testing.T) {
r, err := ring.NewRing(256, []uint64{8380417})
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
rows int
cols int
}{
{
name: "small matrix",
rows: 2,
cols: 3,
},
{
name: "square matrix",
rows: 4,
cols: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := InitializeMatrix(r, tt.rows, tt.cols)
// Check dimensions
if len(m) != tt.rows {
t.Errorf("InitializeMatrix() returned %d rows, want %d", len(m), tt.rows)
}
if len(m) > 0 && len(m[0]) != tt.cols {
t.Errorf("InitializeMatrix() returned %d columns, want %d", len(m[0]), tt.cols)
}
// Verify all polynomials are initialized
for i := range m {
for j := range m[i] {
if m[i][j].N() == 0 {
t.Errorf("InitializeMatrix() returned uninitialized polynomial at [%d][%d]", i, j)
}
}
}
})
}
}
// Helper functions for testing
func createTestVector(r *ring.Ring, sampler ring.Sampler, size int) structs.Vector[ring.Poly] {
v := make(structs.Vector[ring.Poly], size)
for i := range v {
v[i] = sampler.ReadNew()
}
return v
}
func createTestMatrix(r *ring.Ring, sampler ring.Sampler, rows, cols int) structs.Matrix[ring.Poly] {
m := make(structs.Matrix[ring.Poly], rows)
for i := range m {
m[i] = make(structs.Vector[ring.Poly], cols)
for j := range m[i] {
m[i][j] = sampler.ReadNew()
}
}
return m
}