Files
crypto/aggregated/managers.go
T

169 lines
3.5 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package aggregated
import (
"errors"
"sync"
"github.com/luxfi/crypto/bls"
"github.com/luxfi/crypto/cggmp21"
"github.com/luxfi/ids"
log "github.com/luxfi/log"
)
// BLSManager manages BLS signature operations
type BLSManager struct {
log log.Logger
mu sync.RWMutex
}
// NewBLSManager creates a new BLS manager
func NewBLSManager(log log.Logger) *BLSManager {
return &BLSManager{
log: log,
}
}
// CreateKeyPair generates a new BLS key pair
func (m *BLSManager) CreateKeyPair() (*bls.SecretKey, *bls.PublicKey, error) {
sk, err := bls.NewSecretKey()
if err != nil {
return nil, nil, err
}
pk := sk.PublicKey()
return sk, pk, nil
}
// Sign creates a BLS signature
func (m *BLSManager) Sign(sk *bls.SecretKey, message []byte) (*bls.Signature, error) {
sig, err := sk.Sign(message)
if err != nil {
return nil, err
}
return sig, nil
}
// ThresholdManager manages threshold signature operations
// Actual threshold operations use github.com/luxfi/threshold
type ThresholdManager struct {
log log.Logger
mu sync.RWMutex
}
// NewThresholdManager creates a new threshold manager
func NewThresholdManager(log log.Logger) *ThresholdManager {
return &ThresholdManager{
log: log,
}
}
// CGGMP21Manager manages CGGMP21 threshold signature operations
type CGGMP21Manager struct {
log log.Logger
parties map[int]*cggmp21.Party
config *cggmp21.Config
mu sync.RWMutex
}
// NewCGGMP21Manager creates a new CGGMP21 manager
func NewCGGMP21Manager(log log.Logger) *CGGMP21Manager {
return &CGGMP21Manager{
log: log,
parties: make(map[int]*cggmp21.Party),
}
}
// InitializeParty creates a new CGGMP21 party
func (m *CGGMP21Manager) InitializeParty(
partyID ids.NodeID,
index int,
config *cggmp21.Config,
) error {
m.mu.Lock()
defer m.mu.Unlock()
party, err := cggmp21.NewParty(partyID, index, config, m.log)
if err != nil {
return err
}
m.parties[index] = party
m.config = config
return nil
}
// GetParty retrieves a CGGMP21 party by index
func (m *CGGMP21Manager) GetParty(index int) (*cggmp21.Party, error) {
m.mu.RLock()
defer m.mu.RUnlock()
party, exists := m.parties[index]
if !exists {
return nil, errors.New("party not found")
}
return party, nil
}
// FeeCollector manages fee collection for signature operations
type FeeCollector struct {
collectedFees map[SignatureType]uint64
mu sync.RWMutex
}
// NewFeeCollector creates a new fee collector
func NewFeeCollector() FeeCollector {
return FeeCollector{
collectedFees: make(map[SignatureType]uint64),
}
}
// CollectFee records a fee collection
func (fc *FeeCollector) CollectFee(sigType SignatureType, amount uint64) {
fc.mu.Lock()
defer fc.mu.Unlock()
fc.collectedFees[sigType] += amount
}
// GetCollectedFees returns total collected fees by type
func (fc *FeeCollector) GetCollectedFees() map[SignatureType]uint64 {
fc.mu.RLock()
defer fc.mu.RUnlock()
fees := make(map[SignatureType]uint64)
for k, v := range fc.collectedFees {
fees[k] = v
}
return fees
}
// GetTotalFees returns the total of all collected fees
func (fc *FeeCollector) GetTotalFees() uint64 {
fc.mu.RLock()
defer fc.mu.RUnlock()
var total uint64
for _, amount := range fc.collectedFees {
total += amount
}
return total
}
// ResetFees resets the fee collection
func (fc *FeeCollector) ResetFees() map[SignatureType]uint64 {
fc.mu.Lock()
defer fc.mu.Unlock()
oldFees := fc.collectedFees
fc.collectedFees = make(map[SignatureType]uint64)
return oldFees
}