state

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

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     = big.NewInt(circuits.KeyProcessID)
	KeyBallotMode    = big.NewInt(circuits.KeyBallotMode)
	KeyEncryptionKey = big.NewInt(circuits.KeyEncryptionKey)
	KeyResultsAdd    = big.NewInt(circuits.KeyResultsAdd)
	KeyResultsSub    = big.NewInt(circuits.KeyResultsSub)
	KeyCensusOrigin  = big.NewInt(circuits.KeyCensusOrigin)

	ReservedKeysOffset = big.NewInt(circuits.ReservedKeysOffset)

	ErrStateAlreadyInitialized = fmt.Errorf("state already initialized")
)
View Source
var (
	// VoteIDKeyValue is a constant key used to store vote IDs in the state
	VoteIDKeyValue = big.NewInt(0)

	// 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 CalculateInitialRoot

func CalculateInitialRoot(
	processID types.ProcessID,
	censusOrigin *big.Int,
	ballotMode *types.BallotMode,
	publicKey ecc.Point,
) (*big.Int, error)

CalculateInitialRoot returns the root of the tree that would result from initializing a state with the passed parameters. It uses an ephemereal tree, nothing is written down to storage.

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 HasAddressVoted

func HasAddressVoted(db db.Database, processID types.ProcessID, address *types.BigInt) (bool, error)

HasAddressVoted 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 *big.Int, 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 BlobData

type BlobData struct {
	Votes      []*Vote
	ResultsAdd []*big.Int
	ResultsSub []*big.Int
}

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) AddVote

func (o *State) AddVote(v *Vote) error

AddVote adds a vote to the state. If v.Address exists already in the tree, it counts as vote overwrite. Note that this method modifies passed v, sets v.OverwrittenBallot

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(k *big.Int, 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) BallotMode

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

BallotMode returns the ballot mode of the state as a circuits.BallotMode[*big.Int].

func (*State) BuildKZGCommitment

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

BuildKZGCommitment collects the raw batch-data, packs it into one blob and produces (blob, commitment, proof, z, y, versionedHash).

blob layout:

  1. ResultsAdd (params.FieldsPerBallot * 4 coordinates)
  2. ResultsSub (params.FieldsPerBallot * 4 coordinates)
  3. Votes sequentially until voteID = 0x0 (sentinel): Each vote: voteID + address + 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) ContainsAddress

func (o *State) ContainsAddress(address *types.BigInt) bool

ContainsAddress checks if the state contains an address

func (*State) ContainsVoteID

func (o *State) ContainsVoteID(voteID *big.Int) bool

ContainsVoteID checks if the state contains a vote ID

func (*State) EncryptedBallot

func (o *State) EncryptedBallot(address *big.Int) (*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) EndBatch

func (o *State) EndBatch() error

EndBatch commits the current batch to the database and generates the Merkle proofs for the current batch. It also updates the results of the state tree with the new results. The results are calculated by adding the old results with the new results. The function returns an error if the commit fails or if the Merkle proofs cannot be generated.

func (*State) GenArboProof

func (o *State) GenArboProof(k *big.Int) (*ArboProof, error)

GenArboProof generates a ArboProof for the given key

func (*State) Initialize

func (o *State) Initialize(
	censusOrigin *big.Int,
	ballotMode circuits.BallotMode[*big.Int],
	encryptionKey circuits.EncryptionKey[*big.Int],
) error

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

func (*State) NewResultsAdd

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

NewResultsAdd returns the new results add ballot of the current batch.

func (*State) NewResultsSub

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

NewResultsSub returns the new results sub ballot of the current batch.

func (*State) OldResultsAdd

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

OldResultsAdd returns the old results add ballot of the current batch.

func (*State) OldResultsSub

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

OldResultsSub returns the old results sub 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) ResultsAdd

func (o *State) ResultsAdd() (*elgamal.Ballot, bool)

ResultsAdd returns the resultsAdd of the state as a elgamal.Ballot

func (*State) ResultsSub

func (o *State) ResultsSub() (*elgamal.Ballot, bool)

ResultsSub returns the resultsSub of the state as a 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) SetResultsAdd

func (o *State) SetResultsAdd(resultsAdd *elgamal.Ballot)

SetResultsAdd sets the resultsAdd directly in the state tree

func (*State) SetResultsSub

func (o *State) SetResultsSub(resultsSub *elgamal.Ballot)

SetResultsSub sets the resultsSub 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) StartBatch

func (o *State) StartBatch() error

StartBatch resets counters and sums to zero, and creates a new write transaction in the db

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
	VoteID            types.HexBytes
	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) SerializeBigInts

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

SerializeBigInts returns

  • vote.Address
  • vote.VoteID
  • vote.UserWeight
  • vote.Ballot

type VotesProofs

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

VotesProofs stores the Merkle proofs for the votes, including the results add and sub proofs, as well as the ballot and commitment proofs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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