@yuetloo/maci-core v0.10.1
maci-core
This submodule assists with handling key business logic functions and processes.
Overview
One may conceive of MACI as a state machine with 1) data and 2) functions which transform said data. This makes it easier to reason about the system, write tests, and implement functionality. It also allows us to implement the smart contracts in discrete components which are easy to test.
To this end, we this submodule exposes a MaciState class and a User class.
User
Each `User object has the following attributes:
pubKey: PubKey: The user's public key.
votes: SnarkBigInt[]: The voice credits assigned to each vote option.
voiceCreditBalance: SnarkBigInt: The user's remaining voice credit balance.
Functions
genStateLeaf
Function signature:
(_voteOptionTreeDepth: number): StateLeafGenerates and returns an equivalent StateLeaf domain
object. This function helps MaciState to generate a state
tree.
copy
Function signature:
(): UserDeep-copies and returns this object.
MaciState
We denote all state data as attributes of a MaciState object.
Only the coordinator should have access to state data. Each user can only access their own keypair, commands, and on-chain state and message tree roots.
MaciState contains the following attributes:
coordinatorKeypair: Keypair: The coordinator's keypair.
users: User[]: An array of User objects, each of which represents the current state of a user.
stateTreeDepth: SnarkBigInt: The depth of the state tree.
messageTreeDepth: SnarkBigInt: The depth of the message tree.
voteOptionTreeDepth: SnarkBigInt: The depth of each user's vote option tree.
messages: Message[]: An array of all published messages.
zerothStateLeaf: StateLeaf: The leaf of the state tree at index 0. This means
that the zeroth user in users has index 1 in the state tree.
maxVoteOptionIndex: SnarkBigInt: The maximum allowed vote options. For
instance, even if the vote option tree supports up to 16 vote options, this
value can be set to 12 so as to enforce the fact that there are only 12 options
to choose from.
encPubKeys: PubKey[]: An array of public keys used to generate ephermeral
ECDH shared keys with which to encrypt commands to messages. For each PubKey
in encPubKey, its corresponding Message in messages shares the same array
index.
Functions
The following functions modify the state:
signUp()publishMessage()processMessage()batchProcessMessage()
The following functions do not modify the state:
copy()genStateTree()genStateRoot()genMessageTree()genMessageRoot()computeCumulativeVoteTally()genUpdateStateTreeCircuitInputs()genBatchUpdateStateTreeCircuitInputs()
signUp
Function signature:
(
_pubKey: PubKey,
_initialVoiceCreditBalance: SnarkBigInt,
): voidAppends a User with the specified public key and initial voice credit balance
to the users array.
publishMessage
Function signature:
(
_message: Message,
_encPubKey: PubKey,
): voidAppends a Message to the messages array. It also appends the public key
used to generate the ECDH shared key which encrypts _message to the
encPubKeys array.
processMessage
Function signature:
(_index: number): voidThis function:
- Generates a shared key using
encPubKeys[index]andcoordinatorKeyPair.pubKey - Decrypts
messages[_index]to derive aCommand - If the message is invalid, do nothing and return
- If the message is valid, update the user's public key and vote at
_index.
batchProcessMessage
Function signature:
(
_index: number,
_batchSize: number,
_randomStateLeaf: StateLeaf,
): voidThis function runs processMessage() on a batch of _batchSize leaves
starting from index _index, and then replaces the zeroth leaf with
_randomStateLeaf.
genStateTree()
Function signature:
(): IncrementalMerkleTreeGenerates and returns the state tree as an incremental Merkle tree.
genStateRoot()
Function signature:
(): SnarkBigIntThis function computes the state root given the data stored in users and
zerothStateLeaf.
genMessageTree()
Function signature:
(): IncrementalMerkleTreeGenerates and returns the message tree as an incremental Merkle tree.
genMessageRoot()
Function signature:
(): SnarkBigIntThis function computes the state root given the data stored in messsages.
genUpdateStateTreeCircuitInputs
Function signature:
genUpdateStateTreeCircuitInputs = (_index: number): objectGenerates the circuit inputs (both public and private) for the
UpdateStateTree circuit, as an object with the following attributes:
coordinator_public_keyecdh_private_keyecdh_public_keymessagemsg_tree_rootmsg_tree_path_elementsmsg_tree_path_indexvote_options_leaf_rawvote_options_tree_rootvote_options_tree_path_elementsvote_options_tree_path_indexvote_options_max_leaf_indexstate_tree_data_rawstate_tree_max_leaf_indexstate_tree_rootstate_tree_path_elementsstate_tree_path_index
genBatchUpdateStateTreeCircuitInputs
Function signature:
genBatchUpdateStateTreeCircuitInputs = (
_index: number,
_batchSize: number,
_randomStateLeaf: StateLeaf,
) => objectGenerates the circuit inputs (both public and private) for the
BatchUpdateStateTree circuit, as an object with the following attributes:
coordinator_public_keymessageecdh_private_keyecdh_public_keymsg_tree_rootmsg_tree_path_elementsmsg_tree_batch_start_indexrandom_leafstate_tree_rootstate_tree_path_elementsstate_tree_path_indexrandom_leaf_rootrandom_leaf_path_elementsvote_options_leaf_rawstate_tree_data_rawstate_tree_max_leaf_indexvote_options_max_leaf_indexvote_options_tree_rootvote_options_tree_path_elementsvote_options_tree_path_index
genQuadVoteTallyCircuitInputs
Function signature:
genQuadVoteTallyCircuitInputs = (
_startIndex: SnarkBigInt,
_batchSize: SnarkBigInt,
_currentResultsSalt: SnarkBigInt,
_newResultsSalt: SnarkBigInt,
): objectGenerates the circuit inputs (both public and private) for the
QuadVoteTally circuit, as an object with the following attributes:
voteLeavesstateLeavescurrentResultsfullStateRootcurrentResultsSaltnewResultsSaltcurrentResultsCommitmentintermediatePathElementsintermediatePathIndexintermediateStateRoot
copy()
Function signature:
(): MaciStateThis function returns a deep-copied MaciState object.
3 years ago