Documentation
¶
Index ¶
- Variables
- func BigIntToBytes(b *big.Int) []byte
- func BytesToBigInt(b []byte) *big.Int
- func CalculateInitialRoot(processID types.ProcessID, censusOrigin *big.Int, ballotMode *types.BallotMode, ...) (*big.Int, error)
- func EncodeKey(key *big.Int) []byte
- func HasAddressVoted(db db.Database, processID types.ProcessID, address *types.BigInt) (bool, error)
- func RootExists(db db.Database, processId types.ProcessID, root *big.Int) error
- type ArboProof
- type ArboTransition
- type BlobData
- type ProcessProofs
- type State
- func (o *State) AddVote(v *Vote) error
- func (st *State) ApplyBlobToState(blob *types.Blob) error
- func (o *State) ArboProofsFromAddOrUpdate(k *big.Int, v []*big.Int) (*arbo.GnarkVerifierProof, *arbo.GnarkVerifierProof, error)
- func (o *State) BallotMode() circuits.BallotMode[*big.Int]
- func (st *State) BuildKZGCommitment() (*blobs.BlobEvalData, error)
- func (o *State) CensusOrigin() *big.Int
- func (o *State) Close() error
- func (o *State) ContainsAddress(address *types.BigInt) bool
- func (o *State) ContainsVoteID(voteID *big.Int) bool
- func (o *State) EncryptedBallot(address *big.Int) (*elgamal.Ballot, error)
- func (o *State) EncryptionKey() circuits.EncryptionKey[*big.Int]
- func (o *State) EndBatch() error
- func (o *State) GenArboProof(k *big.Int) (*ArboProof, error)
- func (o *State) Initialize(censusOrigin *big.Int, ballotMode circuits.BallotMode[*big.Int], ...) error
- func (o *State) NewResultsAdd() *elgamal.Ballot
- func (o *State) NewResultsSub() *elgamal.Ballot
- func (o *State) OldResultsAdd() *elgamal.Ballot
- func (o *State) OldResultsSub() *elgamal.Ballot
- func (o *State) OverwrittenVotesCount() int
- func (o *State) PaddedVotes() []*Vote
- func (o *State) Process() circuits.Process[*big.Int]
- func (o *State) ProcessID() *big.Int
- func (o *State) ProcessProofs() ProcessProofs
- func (o *State) ProcessSerializeBigInts() []*big.Int
- func (o *State) ResultsAdd() (*elgamal.Ballot, bool)
- func (o *State) ResultsSub() (*elgamal.Ballot, bool)
- func (o *State) Root() ([]byte, error)
- func (o *State) RootAsBigInt() (*big.Int, error)
- func (o *State) RootExists(root *big.Int) error
- func (o *State) RootHashBefore() *big.Int
- func (o *State) SetResultsAdd(resultsAdd *elgamal.Ballot)
- func (o *State) SetResultsSub(resultsSub *elgamal.Ballot)
- func (o *State) SetRoot(newRoot []byte) error
- func (o *State) SetRootAsBigInt(newRoot *big.Int) error
- func (o *State) StartBatch() error
- func (o *State) VotersCount() int
- func (o *State) Votes() []*Vote
- func (o *State) VotesProofs() VotesProofs
- type Vote
- type VotesProofs
Constants ¶
This section is empty.
Variables ¶
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) )
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") )
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 ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
BlobData represents the structured data extracted from a blob
func ParseBlobData ¶
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 ¶
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 ¶
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 ¶
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 ¶
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:
- ResultsAdd (params.FieldsPerBallot * 4 coordinates)
- ResultsSub (params.FieldsPerBallot * 4 coordinates)
- Votes sequentially until voteID = 0x0 (sentinel): Each vote: voteID + address + reencryptedBallot coordinates
func (*State) CensusOrigin ¶
CensusOrigin returns the census origin of the state as a *big.Int.
func (*State) ContainsAddress ¶
ContainsAddress checks if the state contains an address
func (*State) ContainsVoteID ¶
ContainsVoteID checks if the state contains a vote ID
func (*State) EncryptedBallot ¶
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 ¶
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 ¶
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 ¶
NewResultsAdd returns the new results add ballot of the current batch.
func (*State) NewResultsSub ¶
NewResultsSub returns the new results sub ballot of the current batch.
func (*State) OldResultsAdd ¶
OldResultsAdd returns the old results add ballot of the current batch.
func (*State) OldResultsSub ¶
OldResultsSub returns the old results sub ballot of the current batch.
func (*State) OverwrittenVotesCount ¶
OverwrittenVotesCount returns the number of ballots overwritten in the current batch.
func (*State) PaddedVotes ¶
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) ProcessProofs ¶
func (o *State) ProcessProofs() ProcessProofs
ProcessProofs returns a pointer to the process proofs for the state.
func (*State) ProcessSerializeBigInts ¶
ProcessSerializeBigInts returns
process.ID process.CensusOrigin process.BallotMode process.EncryptionKey
func (*State) ResultsAdd ¶
ResultsAdd returns the resultsAdd of the state as a elgamal.Ballot
func (*State) ResultsSub ¶
ResultsSub returns the resultsSub of the state as a elgamal.Ballot
func (*State) RootAsBigInt ¶
RootAsBigInt method returns the root of the tree as a big.Int.
func (*State) RootExists ¶
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 ¶
RootHashBefore returns the root hash before state transition.
func (*State) SetResultsAdd ¶
SetResultsAdd sets the resultsAdd directly in the state tree
func (*State) SetResultsSub ¶
SetResultsSub sets the resultsSub directly in the state tree
func (*State) SetRootAsBigInt ¶
SetRootAsBigInt method sets the root of the tree to the provided one as a big.Int.
func (*State) StartBatch ¶
StartBatch resets counters and sums to zero, and creates a new write transaction in the db
func (*State) VotersCount ¶
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) 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 ¶
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.