state

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlobTxBytesPerFieldElement = gethparams.BlobTxBytesPerFieldElement
	BlobTxFieldElementsPerBlob = gethparams.BlobTxFieldElementsPerBlob
)

Variables

View Source
var (
	// HashFn is the hash function used in the state tree.
	HashFn = arbo.HashFunctionMultiPoseidon
	// Curve is the curve used for the encryption
	Curve = curves.New(bjj.CurveType)
)
View Source
var (
	KeyProcessID     = types.StateKey(params.StateKeyProcessID)
	KeyBallotMode    = types.StateKey(params.StateKeyBallotMode)
	KeyEncryptionKey = types.StateKey(params.StateKeyEncryptionKey)
	KeyResults       = types.StateKey(params.StateKeyResults)
	KeyCensusOrigin  = types.StateKey(params.StateKeyCensusOrigin)

	ErrStateAlreadyInitialized = fmt.Errorf("state already initialized")
)
View Source
var (

	// ErrNotFound is returned when a key is not found in the state
	ErrKeyNotFound = fmt.Errorf("not found")
)

Functions

func BigIntToBytes

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

BigIntToBytes method converts a big.Int to a byte array. It is a wrapper around the arbo.BigIntToBytes function, which uses the maximum key length for the hash function.

func BytesToBigInt

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

BytesToBigInt method converts a byte array to a big.Int. It is a wrapper around the arbo.BytesToBigInt function.

func EncodeKey

func EncodeKey(key *big.Int) []byte

EncodeKey encodes a key to a byte array using the maximum key length for the current number of levels in the state tree and the hash function length.

func IndexContainsBallot added in v0.0.2

func IndexContainsBallot(db db.Database, processID types.ProcessID, ballotIndex types.BallotIndex) (bool, error)

IndexContainsBallot checks if an address has voted in a given process. It opens the current process state and checks for the address. If found, it returns true, otherwise false. If there's an error opening the state or during the check, it returns the error.

func RootExists

func RootExists(db db.Database, processId types.ProcessID, root *big.Int) error

RootExists checks if the provided root exists in the tree for the given processId. Returns nil if the root exists, or an error if it does not.

Types

type ArboProof

type ArboProof struct {
	// Key+Value hashed through Siblings path, should produce Root hash
	Root     *big.Int
	Siblings []*big.Int
	Key      *big.Int
	Value    *big.Int
}

ArboProof stores the proof in arbo native types

type ArboTransition

type ArboTransition struct {
	// NewKey + NewValue hashed through Siblings path, should produce NewRoot hash
	NewRoot  *big.Int
	Siblings []*big.Int
	NewKey   *big.Int
	NewValue *big.Int

	// OldKey + OldValue hashed through same Siblings should produce OldRoot hash
	OldRoot  *big.Int
	OldKey   *big.Int
	OldValue *big.Int
	IsOld0   int
	Fnc0     int
	Fnc1     int
}

ArboTransition stores a pair of leaves and root hashes, and a single path common to both proofs

func ArboTransitionFromAddOrUpdate

func ArboTransitionFromAddOrUpdate(o *State, k types.StateKey, v ...*big.Int) (*ArboTransition, error)

ArboTransitionFromAddOrUpdate adds or updates a key in the tree, and returns a ArboTransition.

func ArboTransitionFromArboProofPair

func ArboTransitionFromArboProofPair(before, after *arbo.GnarkVerifierProof) *ArboTransition

ArboTransitionFromArboProofPair generates a ArboTransition based on the pair of proofs passed

func ArboTransitionFromNoop

func ArboTransitionFromNoop(o *State) (*ArboTransition, error)

ArboTransitionFromNoop returns a NOOP ArboTransition.

type BallotLeaf added in v0.0.7

type BallotLeaf struct {
	Ballot  *elgamal.Ballot
	Address *big.Int
	Weight  *big.Int
}

type BlobData

type BlobData struct {
	Results     []*big.Int
	VotersCount uint64
	Votes       []*Vote
}

BlobData represents the structured data extracted from a blob

func ParseBlobData

func ParseBlobData(blob []byte) (*BlobData, error)

ParseBlobData extracts vote and results data from a blob.

type ProcessProofs

type ProcessProofs struct {
	ID            *ArboProof
	CensusOrigin  *ArboProof
	BallotMode    *ArboProof
	EncryptionKey *ArboProof
}

ProcessProofs stores the Merkle proofs for the process, including the ID census root, ballot mode, and encryption key proofs.

type State

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

State represents a state tree

func LoadOnRoot

func LoadOnRoot(db db.Database, processId types.ProcessID, root *big.Int) (*State, error)

LoadOnRoot loads a State from the database using the provided processId and root. It creates a new State with the given processId and sets the root of the tree to the provided root. It returns an error if the processId is not found in the database or if the root cannot be set. The root provided is formatted to the arbo format before being set in the state tree.

func New

func New(db db.Database, processID types.ProcessID) (*State, error)

New creates or opens a State stored in the passed database. The processID is used as a prefix for the keys in the database.

func (*State) AddVotesBatch added in v0.0.2

func (o *State) AddVotesBatch(votes []*Vote) error

func (*State) ApplyBlobToState

func (st *State) ApplyBlobToState(blob *types.Blob) error

ApplyBlobToState applies the data from a blob to restore state

func (*State) ArboProofsFromAddOrUpdate

func (o *State) ArboProofsFromAddOrUpdate(key types.StateKey, v []*big.Int) (*arbo.GnarkVerifierProof, *arbo.GnarkVerifierProof, error)

ArboProofsFromAddOrUpdate generates an ArboProof before adding (or updating) the given leaf, and another ArboProof after updating, and returns both.

func (*State) BallotLeaf added in v0.0.7

func (o *State) BallotLeaf(ballotIndex types.BallotIndex) (*BallotLeaf, error)

BallotLeaf returns the stored ballot leaf associated with a ballot index.

func (*State) BallotMode

func (o *State) BallotMode() *big.Int

BallotMode returns the packed ballot mode of the state as a *big.Int.

func (*State) BlobEvalData added in v0.0.4

func (st *State) BlobEvalData() (*blobs.BlobEvalData, error)

BlobEvalData returns the cached blob evaluation data for the current batch.

blob layout:

  1. Results (params.FieldsPerBallot * 4 coordinates)
  2. VotersCount
  3. Votes sequentially for exactly VotersCount entries: Each vote: voteID + address + ballotIndex + weight + reencryptedBallot coordinates

func (*State) CensusOrigin

func (o *State) CensusOrigin() *big.Int

CensusOrigin returns the census origin of the state as a *big.Int.

func (*State) Close

func (o *State) Close() error

Close the database, no more operations can be done after this.

func (*State) ContainsBallot added in v0.0.2

func (o *State) ContainsBallot(ballotIndex types.BallotIndex) bool

ContainsBallot checks if the state contains an address

func (*State) ContainsVoteID

func (o *State) ContainsVoteID(voteID types.VoteID) bool

ContainsVoteID checks if the state contains a vote ID

func (*State) EncryptedBallot

func (o *State) EncryptedBallot(ballotIndex types.BallotIndex) (*elgamal.Ballot, error)

EncryptedBallot returns the ballot associated with a address

func (*State) EncryptionKey

func (o *State) EncryptionKey() circuits.EncryptionKey[*big.Int]

EncryptionKey returns the encryption key of the state as a circuits.EncryptionKey[*big.Int].

func (*State) GenArboProof

func (o *State) GenArboProof(key types.StateKey) (*ArboProof, error)

GenArboProof generates a ArboProof for the given key

func (*State) Initialize

func (o *State) Initialize(
	censusOrigin *big.Int,
	ballotMode *big.Int,
	encryptionKey types.EncryptionKey,
) error

Initialize creates a new State, initialized with the passed parameters. After Initialize, caller is expected to StartBatch, AddVote, EndBatch, StartBatch...

func (*State) NewResults added in v0.0.7

func (o *State) NewResults() *elgamal.Ballot

NewResults returns the new results ballot of the current batch.

func (*State) OldResults added in v0.0.7

func (o *State) OldResults() *elgamal.Ballot

OldResults returns the old results ballot of the current batch.

func (*State) OverwrittenVotesCount

func (o *State) OverwrittenVotesCount() int

OverwrittenVotesCount returns the number of ballots overwritten in the current batch.

func (*State) PaddedVotes

func (o *State) PaddedVotes() []*Vote

PaddedVotes returns the votes added in the current batch, padded to circuits.VotesPerBatch. The padding is done by adding empty votes with zero values.

func (*State) Process

func (o *State) Process() circuits.Process[*big.Int]

Proccess returns all process details from the state

func (*State) ProcessID

func (o *State) ProcessID() *big.Int

ProccessID returns the process ID of the state as a big.Int.

func (*State) ProcessProofs

func (o *State) ProcessProofs() ProcessProofs

ProcessProofs returns a pointer to the process proofs for the state.

func (*State) ProcessSerializeBigInts

func (o *State) ProcessSerializeBigInts() []*big.Int

ProcessSerializeBigInts returns

process.ID
process.CensusOrigin
process.BallotMode
process.EncryptionKey

func (*State) Results added in v0.0.7

func (o *State) Results() (*elgamal.Ballot, error)

Results returns the results of the state as an elgamal.Ballot.

func (*State) Root

func (o *State) Root() ([]byte, error)

Root method returns the root of the tree as a byte array.

func (*State) RootAsBigInt

func (o *State) RootAsBigInt() (*big.Int, error)

RootAsBigInt method returns the root of the tree as a big.Int.

func (*State) RootExists

func (o *State) RootExists(root *big.Int) error

RootExists checks if the provided root exists in the tree. Returns nil if the root exists, or an error if it does not.

func (*State) RootHashBefore

func (o *State) RootHashBefore() *big.Int

RootHashBefore returns the root hash before state transition.

func (*State) SetResults added in v0.0.7

func (o *State) SetResults(results *elgamal.Ballot)

SetResults sets the results directly in the state tree.

func (*State) SetRoot

func (o *State) SetRoot(newRoot []byte) error

SetRoot method sets the root of the tree to the provided one.

func (*State) SetRootAsBigInt

func (o *State) SetRootAsBigInt(newRoot *big.Int) error

SetRootAsBigInt method sets the root of the tree to the provided one as a big.Int.

func (*State) VotersCount

func (o *State) VotersCount() int

VotersCount returns the number of voters participating in the current batch, i.e. either casting their first vote or overwriting a previous one.

func (*State) Votes

func (o *State) Votes() []*Vote

Votes returns the votes added in the current batch.

func (*State) VotesProofs

func (o *State) VotesProofs() VotesProofs

VotesProofs returns a pointer to the votes proofs for the state.

type Vote

type Vote struct {
	Address           *big.Int
	BallotIndex       types.BallotIndex
	VoteID            types.VoteID
	Ballot            *elgamal.Ballot
	OverwrittenBallot *elgamal.Ballot
	Weight            *big.Int
	ReencryptedBallot *elgamal.Ballot // Reencrypted ballot for the state transition circuit
}

Vote describes a vote with homomorphic ballot

func (*Vote) TreeLeafValues added in v0.0.7

func (v *Vote) TreeLeafValues() []*big.Int

TreeLeafValues returns the ballot leaf values in this order: ballot coordinates, address, weight.

type VotesProofs

type VotesProofs struct {
	Results *ArboTransition
	Ballot  [params.VotesPerBatch]*ArboTransition
	VoteID  [params.VotesPerBatch]*ArboTransition
}

VotesProofs stores the Merkle transitions for the votes: the results transition, as well as the ballot and vote ID transitions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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