crypto

package
v0.0.0-...-d489140 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package crypto provides cryptographic primitives for secure anonymous communication.

This package implements the core cryptographic operations required for anonymous broadcast protocols, including:

  • Field arithmetic for finite field operations (auction data)
  • Key management (Ed25519 signing, P-256 key exchange)
  • XOR-based blinding vector generation for privacy-preserving message aggregation
  • Field-based blinding for auction data aggregation

Field Operations

The package uses a 384-bit prime field (AuctionFieldOrder) for auction-related operations where homomorphic addition is required.

Blinding

Two blinding mechanisms are provided:

  • XOR-based blinding for message vectors (DeriveXorBlindingVector)
  • Field arithmetic blinding for auction IBLT vectors (DeriveBlindingVector)

Security Note

Not all operations are constant-time (particularly field arithmetic). This may leak timing information about field element values.

Index

Constants

This section is empty.

Variables

View Source
var AuctionFieldOrder *big.Int

AuctionFieldOrder defines the finite field order for auction operations

Functions

func Decrypt

func Decrypt(recipientPrivKey *ecdh.PrivateKey, msg *EncryptedMessage) ([]byte, error)

Decrypt decrypts an ECIES-encrypted message using the recipient's private key.

func DeriveBlindingVector

func DeriveBlindingVector(sharedSecrets []SharedKey, round uint32, nEls int32, fieldOrder *big.Int) []*big.Int

DeriveBlindingVector generates field element blinding factors from shared secrets. Used for auction IBLT blinding where field arithmetic is required.

func DeriveXorBlindingVector

func DeriveXorBlindingVector(sharedSecrets []SharedKey, round uint32, nBytes int) []byte

DeriveXorBlindingVector generates XOR blinding bytes from shared secrets. Used for message vector blinding where XOR-based privacy is sufficient.

func FieldAddInplace

func FieldAddInplace(l *big.Int, r *big.Int, fieldOrder *big.Int) *big.Int

FieldAddInplace performs modular addition in-place: l = (l + r) mod fieldOrder. The result is stored in l and also returned.

func FieldSubInplace

func FieldSubInplace(l *big.Int, r *big.Int, fieldOrder *big.Int) *big.Int

FieldSubInplace performs modular subtraction in-place: l = (l - r) mod fieldOrder. The result is stored in l and also returned.

func GenerateKeyPair

func GenerateKeyPair() (PublicKey, PrivateKey, error)

GenerateKeyPair generates a new Ed25519 key pair for signing and verification.

func ServerIDsToXEvals

func ServerIDsToXEvals(roundSIds []ServerID, availableSIds []ServerID) []*big.Int

ServerIDsToXEvals converts server IDs to x-coordinates for polynomial evaluation. Maps server IDs to sequential integers 1..n based on sorted order.

func XorInplace

func XorInplace(l, r []byte) []byte

XorInplace performs byte-wise XOR: l[i] ^= r[i] for all i. Returns l for chaining.

Types

type EncryptedMessage

type EncryptedMessage struct {
	EphemeralPubKey []byte // P-256 uncompressed public key
	Nonce           []byte // AES-GCM nonce
	Ciphertext      []byte // Encrypted data with auth tag
}

EncryptedMessage contains ECIES-encrypted data. Format: ephemeral pubkey (65 bytes) || nonce (12 bytes) || ciphertext+tag

func Encrypt

func Encrypt(recipientPubKey *ecdh.PublicKey, plaintext []byte) (*EncryptedMessage, error)

Encrypt encrypts plaintext to a recipient's ECDH public key using ECIES. Uses ephemeral ECDH key agreement and AES-256-GCM for authenticated encryption.

func ParseEncryptedMessage

func ParseEncryptedMessage(data []byte) (*EncryptedMessage, error)

ParseEncryptedMessage deserializes an encrypted message.

func (*EncryptedMessage) Bytes

func (m *EncryptedMessage) Bytes() []byte

Bytes serializes an encrypted message.

type PrivateKey

type PrivateKey []byte

PrivateKey represents a private key used for signing and key exchange. The implementation uses Ed25519 private keys.

func NewPrivateKeyFromBytes

func NewPrivateKeyFromBytes(data []byte) PrivateKey

NewPrivateKeyFromBytes creates a PrivateKey from a byte slice.

func (PrivateKey) Bytes

func (sk PrivateKey) Bytes() []byte

Bytes returns the private key as a byte slice.

func (PrivateKey) PublicKey

func (sk PrivateKey) PublicKey() (PublicKey, error)

PublicKey derives the public key corresponding to this private key.

type PublicKey

type PublicKey []byte

PublicKey represents a public key used for authentication and encryption. In ADCNet, public keys are used to verify signatures and as client/server identifiers. The implementation uses Ed25519 public keys.

func NewPublicKeyFromBytes

func NewPublicKeyFromBytes(data []byte) PublicKey

NewPublicKeyFromBytes creates a PublicKey from a byte slice.

func NewPublicKeyFromString

func NewPublicKeyFromString(data string) (PublicKey, error)

NewPublicKeyFromString creates a PublicKey from a hex-encoded string.

func (PublicKey) Bytes

func (pk PublicKey) Bytes() []byte

Bytes returns the public key as a byte slice.

func (PublicKey) Equal

func (pk PublicKey) Equal(other PublicKey) bool

Equal compares two public keys for equality.

func (PublicKey) String

func (pk PublicKey) String() string

String returns a hex-encoded string representation of the public key.

type ServerID

type ServerID uint32

ServerID is a unique identifier for a server derived from its public key.

func PublicKeyToServerID

func PublicKeyToServerID(pubKey PublicKey) ServerID

PublicKeyToServerID derives a server ID from a public key using SHA256.

type SharedKey

type SharedKey []byte

SharedKey represents a Diffie-Hellman shared secret used for deriving blinding vectors.

func NewSharedKey

func NewSharedKey(data []byte) SharedKey

NewSharedKey creates a SharedKey from a byte slice.

func (SharedKey) Bytes

func (sk SharedKey) Bytes() []byte

Bytes returns the shared key as a byte slice.

type Signature

type Signature []byte

Signature represents a digital signature produced with a private key.

func NewSignature

func NewSignature(data []byte) Signature

NewSignature creates a Signature from a byte slice.

func Sign

func Sign(privateKey PrivateKey, data []byte) (Signature, error)

Sign signs data with the given private key using Ed25519.

func (Signature) Bytes

func (s Signature) Bytes() []byte

Bytes returns the signature as a byte slice.

func (Signature) String

func (s Signature) String() string

String returns a hex-encoded string representation of the signature.

func (Signature) Verify

func (s Signature) Verify(publicKey PublicKey, data []byte) bool

Verify checks if this signature is valid for the given data and public key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL