Files
crypto/utils/beacon/set.go
T
Hanzo Dev 0be2fe8f6c crypto: implement post-quantum primitives with circl
- Implement ML-DSA-65 (FIPS 204) using cloudflare/circl
  * Single implementation with automatic CGO optimization
  * Sign ~440μs, Verify ~130μs, KeyGen ~165μs on M1 Max
  * All 11 tests passing

- Simplify ML-KEM implementation
  * Remove redundant optimized versions
  * Use circl ML-KEM-768 directly

- Simplify SLH-DSA implementation
  * Remove premature optimizations
  * Clean stub for future circl support (FIPS 205)

- Add comprehensive cache package
  * LRU cache from luxfi/node
  * Metercacher for metrics integration
  * Test utilities

- Add crypto utils
  * Atomic operations
  * Bytes utilities
  * Complete utils package from luxfi/node

- Update secp256k1 and BLS
  * All BLS tests passing (23 tests)
  * secp256k1 fuzz test added

All post-quantum implementations now use cloudflare/circl as single source
of truth, following DRY principle and ensuring FIPS compliance.
2025-11-22 16:37:21 -08:00

133 lines
2.5 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package beacon
import (
"errors"
"net/netip"
"strings"
"github.com/luxfi/ids"
)
var (
_ Set = (*set)(nil)
errDuplicateID = errors.New("duplicated ID")
errDuplicateIP = errors.New("duplicated IP")
errUnknownID = errors.New("unknown ID")
errUnknownIP = errors.New("unknown IP")
)
type Set interface {
Add(Beacon) error
RemoveByID(ids.NodeID) error
RemoveByIP(netip.AddrPort) error
Len() int
IDsArg() string
IPsArg() string
}
type set struct {
ids map[ids.NodeID]int
ips map[netip.AddrPort]int
beacons []Beacon
}
func NewSet() Set {
return &set{
ids: make(map[ids.NodeID]int),
ips: make(map[netip.AddrPort]int),
}
}
func (s *set) Add(b Beacon) error {
id := b.ID()
_, duplicateID := s.ids[id]
if duplicateID {
return errDuplicateID
}
ip := b.IP()
_, duplicateIP := s.ips[ip]
if duplicateIP {
return errDuplicateIP
}
s.ids[id] = len(s.beacons)
s.ips[ip] = len(s.beacons)
s.beacons = append(s.beacons, b)
return nil
}
func (s *set) RemoveByID(idToRemove ids.NodeID) error {
indexToRemove, exists := s.ids[idToRemove]
if !exists {
return errUnknownID
}
toRemove := s.beacons[indexToRemove]
ipToRemove := toRemove.IP()
indexToMove := len(s.beacons) - 1
toMove := s.beacons[indexToMove]
idToMove := toMove.ID()
ipToMove := toMove.IP()
s.ids[idToMove] = indexToRemove
s.ips[ipToMove] = indexToRemove
s.beacons[indexToRemove] = toMove
delete(s.ids, idToRemove)
delete(s.ips, ipToRemove)
s.beacons[indexToMove] = nil
s.beacons = s.beacons[:indexToMove]
return nil
}
func (s *set) RemoveByIP(ip netip.AddrPort) error {
indexToRemove, exists := s.ips[ip]
if !exists {
return errUnknownIP
}
toRemove := s.beacons[indexToRemove]
idToRemove := toRemove.ID()
return s.RemoveByID(idToRemove)
}
func (s *set) Len() int {
return len(s.beacons)
}
func (s *set) IDsArg() string {
sb := strings.Builder{}
if len(s.beacons) == 0 {
return ""
}
b := s.beacons[0]
_, _ = sb.WriteString(b.ID().String())
for _, b := range s.beacons[1:] {
_, _ = sb.WriteString(",")
_, _ = sb.WriteString(b.ID().String())
}
return sb.String()
}
func (s *set) IPsArg() string {
sb := strings.Builder{}
if len(s.beacons) == 0 {
return ""
}
b := s.beacons[0]
_, _ = sb.WriteString(b.IP().String())
for _, b := range s.beacons[1:] {
_, _ = sb.WriteString(",")
_, _ = sb.WriteString(b.IP().String())
}
return sb.String()
}