Files
zeekay 8273105926 The signing curve is derived from the network, not stated by the caller
Solana addresses were correct and receive-only: ConsensusKeyInfoStore.Save was
the only RegisterKey caller and hardcoded "secp256k1", and TriggerSign read the
curve from that field, so no product entry point could reach Ed25519 signing.

The hardcode is not fixed, it is UNREPRESENTABLE. transport.KeyInfo.KeyType is
deleted — the record carries one public key per curve and a PublicKeyFor(curve)
— and RegisterKey lost its keyType parameter, so there is no longer a place to
state a curve wrongly. A scalar KeyType was the wrong shape anyway: one keygen
ceremony produces BOTH a secp256k1 and an Ed25519 key, so "what curve is this
wallet" has no single answer.

The request carries the network instead, and ONE table maps it to a curve, with
NO default arm — KeyTypeForNetwork returns ErrUnknownNetwork rather than
guessing. That is the point: the old default returned secp256k1 for anything
unrecognized, which is how a Solana request would have been signed with the
wrong key.

/sign's 32-byte clamp becomes the SCHEME's rule rather than the endpoint's:
secp256k1 signs a digest and requires exactly 32 bytes, Ed25519 signs the
message at any length. /sign no longer accepts key_type at all — two ways to say
the curve is the misuse being removed — and Network is bound into the
idempotency fingerprint, so the same bytes for SOL and ETH conflict instead of
sharing one cached signature from a different key.

⚠ The trap this nearly shipped with: every pre-existing KeyInfo record has EMPTY
public keys, because the old Save wrote "", "". A naive "does this wallet have
this curve" gate would have refused every existing wallet on every curve. The
check is gated on RecordsCurves() — a record naming no curve denies none, and
the missing share stays the thing that fails closed.

Verified end to end, not by mirroring: a Solana request through
ConsensusMPCBackend.TriggerSign with a real 2-of-3 FROST ring produces a
signature crypto/ed25519.Verify accepts, over a 63-byte message so any
reintroduced clamp or pre-hash fails it; ETH/EVM/BTC/LUX still route to
secp256k1 and must RECOVER to the wallet's key. Four mutations tried, all
caught.

Behavioral risk worth naming: an unrecognized chain string now fails its
transaction closed rather than signing with a default. That is correct, and the
real fix is validating chain at POST /v1/transactions.
2026-08-06 15:59:30 -07:00

120 lines
3.3 KiB
Go

// Package ceremony drives a threshold protocol to completion entirely
// in-process, relaying messages directly between parties.
//
// It exists so that tests can run the REAL cryptographic protocol — the same
// protocol.StartFunc the daemon's sessions hand to protocol.NewHandler — with
// only NATS, the kvstore and the peer registry absent. The failures worth
// catching at this seam are cryptographic rather than structural: a key on the
// wrong curve, a signature encoded the wrong way round, a share that does not
// survive storage. None of those are visible unless the real protocol runs and
// the real bytes come out.
//
// It lives outside _test.go because two packages need the same driver
// (pkg/mpc, which tests the sessions, and cmd/mpcd, which tests the daemon's
// signing entry point), and a protocol driver duplicated across packages is a
// driver that drifts.
package ceremony
import (
"context"
"fmt"
"sync"
"time"
log "github.com/luxfi/log"
"github.com/luxfi/threshold/pkg/party"
"github.com/luxfi/threshold/pkg/protocol"
)
// Timeout bounds a whole ceremony so a stalled protocol fails in seconds
// rather than hanging on the library's 5m default.
const Timeout = 90 * time.Second
// Parties is a 2-of-3 committee: the smallest shape where a signing subset is
// a strict subset of the keygen set, so Lagrange interpolation is genuinely
// exercised rather than degenerating into a single share.
func Parties() []party.ID {
return []party.ID{"node-a", "node-b", "node-c"}
}
// Run drives start(id) for every id to completion and returns each party's
// result. An error from any party fails the whole ceremony, because a protocol
// that half-finished has produced nothing a caller may use.
func Run(
ids []party.ID,
sessionID []byte,
start func(party.ID) protocol.StartFunc,
) (map[party.ID]any, error) {
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
inbox := make(map[party.ID]chan *protocol.Message, len(ids))
for _, id := range ids {
inbox[id] = make(chan *protocol.Message, 256)
}
handlers := make(map[party.ID]*protocol.Handler, len(ids))
for _, id := range ids {
h, err := protocol.NewHandler(
ctx,
log.NewTestLogger(log.ErrorLevel),
nil,
start(id),
sessionID,
protocol.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("party %s failed to start: %w", id, err)
}
handlers[id] = h
}
var (
mu sync.Mutex
results = make(map[party.ID]any, len(ids))
errs = make(map[party.ID]error, len(ids))
wg sync.WaitGroup
)
for id, h := range handlers {
wg.Add(1)
go func(id party.ID, h *protocol.Handler) {
defer wg.Done()
for {
select {
case msg, ok := <-h.Listen():
if !ok {
res, err := h.Result()
mu.Lock()
results[id], errs[id] = res, err
mu.Unlock()
return
}
for other, ch := range inbox {
if other == msg.From {
continue
}
if msg.To != "" && msg.To != other {
continue
}
ch <- msg
}
case msg := <-inbox[id]:
h.Accept(msg)
}
}
}(id, h)
}
wg.Wait()
for _, id := range ids {
if errs[id] != nil {
return nil, fmt.Errorf("party %s did not finish the ceremony: %w", id, errs[id])
}
if results[id] == nil {
return nil, fmt.Errorf("party %s produced no result", id)
}
}
return results, nil
}