crypto

package
v1.9.7-0...-74d8852 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: LGPL-3.0 Imports: 10 Imported by: 0

README

Context

This is ported from https://github.com/shutter-network/shutter/tree/main/shlib/shcrypto to avoid introducing a go-ethereum transitive dependency in Erigon.

In the long term, we can work with the shutter team to remove their dependency on go-ethereum for their shcrypto package (doable). When that is done we will be able to simply use their library directly.

Documentation

Index

Constants

View Source
const BlockSize = 32

BlockSize is the size in bytes of the blocks into which a message is split up before encryption.

View Source
const HashToG1DST = "SHUTTER_V01_BLS12381G1_XMD:SHA-256_SSWU_RO_"
View Source
const VersionIdentifier byte = 0x03

Variables

View Source
var (
	ErrInputTooLong             = errors.New("input too long")
	ErrInvalidEonSecretKeyShare = errors.New("invalid eon secret key share")
	ErrVersionMismatch          = func(version_got byte) error {
		return fmt.Errorf("version mismatch. want %d got %d", VersionIdentifier, version_got)
	}
)

Functions

func DegreeFromThreshold

func DegreeFromThreshold(threshold uint64) uint64

DegreeFromThreshold returns the degree polynomials should have for the given threshold.

func Hash1

func Hash1(b []byte) *blst.P1Affine

func Hash3

func Hash3(b []byte) *big.Int

func IdentifyVersion

func IdentifyVersion(d []byte) byte

IdentifyVersion reads the version identifier byte from the given (marshaled) EncryptedMessage.

func KeyperX

func KeyperX(keyperIndex int) *big.Int

KeyperX computes the x value assigned to the keyper identified by its index.

func Uint64toBytes

func Uint64toBytes(i uint64) ([]byte, int)

func UnpadMessage

func UnpadMessage(blocks []Block) ([]byte, error)

UnpadMessage returns the message provided in padded form as a sequence of blocks.

func ValidEval

func ValidEval(v *big.Int) bool

ValidEval checks if the given value is a valid polynomial evaluation, i.e., if it is in Z_q.

func VerifyEpochSecretKey

func VerifyEpochSecretKey(epochSecretKey *EpochSecretKey, eonPublicKey *EonPublicKey, epochIDBytes []byte) (bool, error)

VerifyEpochSecretKey checks that an epoch secret key is the correct key for an epoch given the eon public key.

func VerifyEpochSecretKeyDeterministic

func VerifyEpochSecretKeyDeterministic(epochSecretKey *EpochSecretKey, eonPublicKey *EonPublicKey, epochIDBytes []byte, sigma Block, message []byte) (bool, error)

VerifyEpochSecretKeyDeterministic checks that an epoch secret key is the correct key for an epoch given the eon public key and random inputs for sigma and message.

func VerifyEpochSecretKeyShare

func VerifyEpochSecretKeyShare(epochSecretKeyShare *EpochSecretKeyShare, eonPublicKeyShare *EonPublicKeyShare, epochID *EpochID) bool

VerifyEpochSecretKeyShare checks that an epoch sk share published by a keyper is correct.

func VerifyPolyEval

func VerifyPolyEval(keyperIndex int, polyEval *big.Int, gammas *Gammas, threshold uint64) bool

VerifyPolyEval checks that the evaluation of a polynomial is consistent with the public gammas.

Types

type Block

type Block [BlockSize]byte

Block represents a block of data.

func DecryptBlocks

func DecryptBlocks(encryptedBlocks []Block, sigma Block) []Block

func Hash2

func Hash2(gt *blst.Fp12) Block

func Hash4

func Hash4(b []byte) Block

func PadMessage

func PadMessage(m []byte) []Block

PadMessage pads a message and returns it as a sequence of blocks. Implements PKCS #7 according to https://www.ietf.org/rfc/rfc2315.txt

func RandomSigma

func RandomSigma(r io.Reader) (Block, error)

RandomSigma returns a random value to be used during encryption.

func XORBlocks

func XORBlocks(b1 Block, b2 Block) Block

XORBlocks xors the two blocks and returns the result.

func (Block) MarshalText

func (block Block) MarshalText() ([]byte, error)

MarshalText serializes the block to hex.

func (*Block) UnmarshalText

func (block *Block) UnmarshalText(b []byte) error

UnmarshalText deserializes the block from hex.

type EncryptedMessage

type EncryptedMessage struct {
	C1 *blst.P2Affine
	C2 Block
	C3 []Block
}

EncryptedMessage represents the full output of the encryption procedure.

func Encrypt

func Encrypt(message []byte, eonPublicKey *EonPublicKey, epochID *EpochID, sigma Block) *EncryptedMessage

Encrypt encrypts a message for the epoch given by its id. It uses the eon public key and randomness provided in sigma.

func (*EncryptedMessage) Decrypt

func (m *EncryptedMessage) Decrypt(epochSecretKey *EpochSecretKey) ([]byte, error)

Decrypt decrypts the given message using the given epoch secret key.

func (*EncryptedMessage) Marshal

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

Marshal serializes the EncryptedMessage object. It panics, if C1 is nil.

func (EncryptedMessage) MarshalText

func (m EncryptedMessage) MarshalText() ([]byte, error)

MarshalText serializes the encrypted message to hex.

func (*EncryptedMessage) Sigma

func (m *EncryptedMessage) Sigma(epochSecretKey *EpochSecretKey) Block

Sigma computes the sigma value of the encrypted message given the epoch secret key.

func (*EncryptedMessage) Unmarshal

func (m *EncryptedMessage) Unmarshal(d []byte) error

Unmarshal deserializes an EncryptedMessage from the given byte slice.

func (*EncryptedMessage) UnmarshalText

func (m *EncryptedMessage) UnmarshalText(b []byte) error

UnmarshalText deserializes the encrypted message from hex.

type EonPublicKey

type EonPublicKey blst.P2Affine

EonPublicKey represents the combined eon public key.

func ComputeEonPublicKey

func ComputeEonPublicKey(gammas []*Gammas) *EonPublicKey

ComputeEonPublicKey computes the combined eon public key from the set of eon public key shares.

func (*EonPublicKey) Equal

func (eonPublicKey *EonPublicKey) Equal(pk2 *EonPublicKey) bool

func (*EonPublicKey) GobDecode

func (eonPublicKey *EonPublicKey) GobDecode(data []byte) error

func (*EonPublicKey) GobEncode

func (eonPublicKey *EonPublicKey) GobEncode() ([]byte, error)

func (*EonPublicKey) Marshal

func (eonPublicKey *EonPublicKey) Marshal() []byte

Marshal serializes the eon public key.

func (EonPublicKey) MarshalText

func (eonPublicKey EonPublicKey) MarshalText() ([]byte, error)

MarshalText serializes the eon public key to hex.

func (*EonPublicKey) Unmarshal

func (eonPublicKey *EonPublicKey) Unmarshal(m []byte) error

Unmarshal deserializes an eon public key from the given byte slice.

func (*EonPublicKey) UnmarshalText

func (eonPublicKey *EonPublicKey) UnmarshalText(input []byte) error

UnmarshalText deserializes the eon public key from hex.

type EonPublicKeyShare

type EonPublicKeyShare blst.P2Affine

EonPublicKeyShare represents a share of the eon public key.

func ComputeEonPublicKeyShare

func ComputeEonPublicKeyShare(keyperIndex int, gammas []*Gammas) *EonPublicKeyShare

ComputeEonPublicKeyShare computes the eon public key share of the given keyper.

func (*EonPublicKeyShare) Equal

func (eonPublicKeyShare *EonPublicKeyShare) Equal(pk2 *EonPublicKeyShare) bool

func (*EonPublicKeyShare) GobDecode

func (eonPublicKeyShare *EonPublicKeyShare) GobDecode(data []byte) error

func (*EonPublicKeyShare) GobEncode

func (eonPublicKeyShare *EonPublicKeyShare) GobEncode() ([]byte, error)

func (*EonPublicKeyShare) Marshal

func (eonPublicKeyShare *EonPublicKeyShare) Marshal() []byte

Marshal serializes the eon public key share.

func (*EonPublicKeyShare) Unmarshal

func (eonPublicKeyShare *EonPublicKeyShare) Unmarshal(m []byte) error

Unmarshal deserializes an eon public key share.

type EonSecretKeyShare

type EonSecretKeyShare big.Int

EonSecretKeyShare represents a share of the eon secret key.

func ComputeEonSecretKeyShare

func ComputeEonSecretKeyShare(polyEvals []*big.Int) *EonSecretKeyShare

ComputeEonSecretKeyShare computes the keyper's secret key share from the set of poly evals received from the other keypers.

func (*EonSecretKeyShare) Equal

func (eonSecretKeyShare *EonSecretKeyShare) Equal(e2 *EonSecretKeyShare) bool

func (*EonSecretKeyShare) GobDecode

func (eonSecretKeyShare *EonSecretKeyShare) GobDecode(data []byte) error

func (*EonSecretKeyShare) GobEncode

func (eonSecretKeyShare *EonSecretKeyShare) GobEncode() ([]byte, error)

func (*EonSecretKeyShare) Marshal

func (eonSecretKeyShare *EonSecretKeyShare) Marshal() []byte

Marshal serializes the eon secret key share.

func (*EonSecretKeyShare) Unmarshal

func (eonSecretKeyShare *EonSecretKeyShare) Unmarshal(m []byte) error

Unarshal deserializes an eon secret key share.

type EpochID

type EpochID blst.P1Affine

EpochID is the identifier of an epoch.

func ComputeEpochID

func ComputeEpochID(epochIDBytes []byte) *EpochID

ComputeEpochID computes the id of the given epoch.

func (*EpochID) Equal

func (epochID *EpochID) Equal(g2 *EpochID) bool

func (*EpochID) GobDecode

func (epochID *EpochID) GobDecode(data []byte) error

func (*EpochID) GobEncode

func (epochID *EpochID) GobEncode() ([]byte, error)

func (*EpochID) Marshal

func (epochID *EpochID) Marshal() []byte

Marshal serializes the epoch id.

func (*EpochID) Unmarshal

func (epochID *EpochID) Unmarshal(m []byte) error

Unmarshal deserializes an epoch id.

type EpochSecretKey

type EpochSecretKey blst.P1Affine

EpochSecretKey represents an epoch secret key.

func ComputeEpochSecretKey

func ComputeEpochSecretKey(keyperIndices []int, epochSecretKeyShares []*EpochSecretKeyShare, threshold uint64) (*EpochSecretKey, error)

ComputeEpochSecretKey computes the epoch secret key from a set of shares.

func (*EpochSecretKey) Equal

func (epochSecretKey *EpochSecretKey) Equal(g2 *EpochSecretKey) bool

func (*EpochSecretKey) GobDecode

func (epochSecretKey *EpochSecretKey) GobDecode(data []byte) error

func (*EpochSecretKey) GobEncode

func (epochSecretKey *EpochSecretKey) GobEncode() ([]byte, error)

func (*EpochSecretKey) Marshal

func (epochSecretKey *EpochSecretKey) Marshal() []byte

Marshal serializes the epoch secret key.

func (EpochSecretKey) MarshalText

func (epochSecretKey EpochSecretKey) MarshalText() ([]byte, error)

MarshalText serializes the epoch secret key to hex.

func (*EpochSecretKey) Unmarshal

func (epochSecretKey *EpochSecretKey) Unmarshal(m []byte) error

Unmarshal deserializes an epoch secret key.

func (*EpochSecretKey) UnmarshalText

func (epochSecretKey *EpochSecretKey) UnmarshalText(input []byte) error

UnmarshalText deserializes the epoch secret key from hex.

type EpochSecretKeyShare

type EpochSecretKeyShare blst.P1Affine

EpochSecretKeyShare represents a keyper's share of the epoch sk key.

func ComputeEpochSecretKeyShare

func ComputeEpochSecretKeyShare(eonSecretKeyShare *EonSecretKeyShare, epochID *EpochID) *EpochSecretKeyShare

ComputeEpochSecretKeyShare computes a keyper's epoch sk share.

func (*EpochSecretKeyShare) Equal

func (epochSecretKeyShare *EpochSecretKeyShare) Equal(g2 *EpochSecretKeyShare) bool

func (*EpochSecretKeyShare) GobDecode

func (epochSecretKeyShare *EpochSecretKeyShare) GobDecode(data []byte) error

func (*EpochSecretKeyShare) GobEncode

func (epochSecretKeyShare *EpochSecretKeyShare) GobEncode() ([]byte, error)

func (*EpochSecretKeyShare) Marshal

func (epochSecretKeyShare *EpochSecretKeyShare) Marshal() []byte

Marshal serializes the epoch secret key share.

func (*EpochSecretKeyShare) Unmarshal

func (epochSecretKeyShare *EpochSecretKeyShare) Unmarshal(m []byte) error

Unmarshal deserializes an epoch secret key share.

type Gammas

type Gammas []*blst.P2Affine

Gammas is a sequence of G2 points based on a polynomial.

func ZeroGammas

func ZeroGammas(degree uint64) *Gammas

ZeroGammas returns the zero value for gammas.

func (*Gammas) Degree

func (g *Gammas) Degree() uint64

Degree returns the degree of the underlying polynomial.

func (Gammas) Equal

func (g Gammas) Equal(otherG Gammas) bool

func (*Gammas) GobDecode

func (g *Gammas) GobDecode(data []byte) error

GobDecode decodes a Gammas value. See https://golang.org/pkg/encoding/gob/#GobDecoder

func (*Gammas) GobEncode

func (g *Gammas) GobEncode() ([]byte, error)

GobEncode encodes a Gammas value. See https://golang.org/pkg/encoding/gob/#GobEncoder

func (*Gammas) Marshal

func (g *Gammas) Marshal() []byte

Marshal serializes the gammas value.

Serialization format: [n:4][gamma1:96]...[gamman:96]

func (*Gammas) MarshalText

func (g *Gammas) MarshalText() ([]byte, error)

MarshalText serializes the gammas as hex.

func (*Gammas) Pi

func (g *Gammas) Pi(xi *big.Int) *blst.P2Affine

Pi computes the pi value at the given x coordinate.

func (*Gammas) Unmarshal

func (g *Gammas) Unmarshal(m []byte) error

Unmarshal deserializes a gammas value.

func (*Gammas) UnmarshalText

func (g *Gammas) UnmarshalText(b []byte) error

UnmarshalText deserializes the gammas from hex.

type LagrangeCoeffs

type LagrangeCoeffs struct {
	// contains filtered or unexported fields
}

LagrangeCoeffs stores the lagrange coefficients that are needed to compute an epoch secret key for a certain array of keypers. We use this to speedup epoch secret key generation.

func NewLagrangeCoeffs

func NewLagrangeCoeffs(keyperIndices []int) *LagrangeCoeffs

NewLagrangeCoeffs computes the lagrange coefficients for the given array of keypers.

func (*LagrangeCoeffs) ComputeEpochSecretKey

func (lc *LagrangeCoeffs) ComputeEpochSecretKey(epochSecretKeyShares []*EpochSecretKeyShare) (*EpochSecretKey, error)

ComputeEpochSecretKey computes the epoch secret key given the secret key shares of the keypers. The caller has to ensure that the secret shares match the keyperIndices used during initialisation.

type Polynomial

type Polynomial []*big.Int

Polynomial represents a polynomial over Z_q.

func NewPolynomial

func NewPolynomial(coefficients []*big.Int) (*Polynomial, error)

NewPolynomial creates a new polynomial from the given coefficients. It verifies the number and range of them.

func RandomPolynomial

func RandomPolynomial(r io.Reader, degree uint64) (*Polynomial, error)

RandomPolynomial generates a random polynomial of given degree.

func (*Polynomial) Degree

func (p *Polynomial) Degree() uint64

Degree returns the degree of the polynomial.

func (*Polynomial) Eval

func (p *Polynomial) Eval(x *big.Int) *big.Int

Eval evaluates the polynomial at the given coordinate.

func (*Polynomial) EvalForKeyper

func (p *Polynomial) EvalForKeyper(keyperIndex int) *big.Int

EvalForKeyper evaluates the polynomial at the position designated for the given keyper.

func (*Polynomial) Gammas

func (p *Polynomial) Gammas() *Gammas

Gammas computes the gamma values for a given polynomial.

Jump to

Keyboard shortcuts

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