jest . setTimeout ( 1200000 ) ; import { executeCircuit , getSignalByName , compileAndLoadCircuit , } from '../' ; import { MaciState } from 'maci-core' ; import { Keypair , Command , } from 'maci-domainobjs' ; import { genRandomSalt , } from 'maci-crypto' ; import { config } from 'maci-config' ; const stateTreeDepth = config . maci . merkleTrees . stateTreeDepth ; const messageTreeDepth = config . maci . merkleTrees . messageTreeDepth ; const voteOptionTreeDepth = config . maci . merkleTrees . voteOptionTreeDepth ; const initialVoiceCreditBalance = config . maci . initialVoiceCreditBalance ; const voteOptionsMaxIndex = config . maci . voteOptionsMaxLeafIndex ; const user = new Keypair ( ) ; const voteOptionIndex = BigInt ( 0 ) ; const newVoteWeight = BigInt ( 9 ) ; const nonce = BigInt ( 1 ) ; const salt = genRandomSalt ( ) ; describe ( 'State tree root update verification circuit' , ( ) => { let circuit ; let circuitInputs ; const coordinator = new Keypair ( ) ; const maciState = new MaciState ( coordinator , stateTreeDepth , messageTreeDepth , voteOptionTreeDepth , voteOptionsMaxIndex ) ; it ( 'UpdateStateTree should produce the correct state root' , async ( ) => { const command = new Command ( BigInt ( 1 ) , user . pubKey , voteOptionIndex , newVoteWeight , nonce , salt ) ; const signature = command . sign ( user . privKey ) ; const sharedKey = Keypair . genEcdhSharedKey ( user . privKey , coordinator . pubKey ) ; const message = command . encrypt ( signature , sharedKey ) ; circuit = await compileAndLoadCircuit ( 'test/updateStateTree_test.circom' ) ; maciState . signUp ( user . pubKey , initialVoiceCreditBalance ) ; maciState . publishMessage ( message , user . pubKey ) ; circuitInputs = maciState . genUpdateStateTreeCircuitInputs ( 0 ) ; const witness = await executeCircuit ( circuit , circuitInputs ) ; const circuitNewStateRoot = getSignalByName ( circuit , witness , 'main.root' ) . toString ( ) ; const stateRootBefore = maciState . genStateRoot ( ) . toString ( ) ; expect ( circuitNewStateRoot ) . not . toEqual ( stateRootBefore . toString ( ) ) ; maciState . processMessage ( 0 ) ; const stateRootAfter = maciState . genStateRoot ( ) . toString ( ) ; expect ( stateRootBefore ) . not . toEqual ( stateRootAfter ) ; expect ( circuitNewStateRoot ) . toEqual ( stateRootAfter ) ; } ) ; it ( 'UpdateStateTree should produce the same state root (invalid state index)' , async ( ) => { const command = new Command ( BigInt ( 9 ) , user . pubKey , voteOptionIndex , newVoteWeight , nonce , salt ) ; const signature = command . sign ( user . privKey ) ; const sharedKey = Keypair . genEcdhSharedKey ( user . privKey , coordinator . pubKey ) ; const message = command . encrypt ( signature , sharedKey ) ; maciState . publishMessage ( message , user . pubKey ) ; circuitInputs = maciState . genUpdateStateTreeCircuitInputs ( 1 ) ; const witness = await executeCircuit ( circuit , circuitInputs ) ; const circuitNewStateRoot = getSignalByName ( circuit , witness , 'main.root' ) . toString ( ) ; const stateRootBefore = maciState . genStateRoot ( ) . toString ( ) ; expect ( circuitNewStateRoot ) . toEqual ( stateRootBefore . toString ( ) ) ; maciState . processMessage ( 0 ) ; const stateRootAfter = maciState . genStateRoot ( ) . toString ( ) ; expect ( stateRootBefore ) . toEqual ( stateRootAfter ) ; } ) ; } ) ;