privacy-scaling-explorations / maci

Minimal Anti-Collusion Infrastructure (MACI)
https://maci.pse.dev/
Other
502 stars 126 forks source link

Error: Constraint doesn't match main.new_state_tree[0].sufficient_voice_credits.lt.n2b #155

Closed xuhcc closed 3 years ago

xuhcc commented 3 years ago

MACI CLI fails to process messages if the number of voice credits is large (16000000000000000000):

(node:15712) UnhandledPromiseRejectionWarning: Error: Constraint doesn't match main.new_state_tree[0].sufficient_voice_credits.lt.n2b: ./node_modules/maci-circuits/node_modules/circomlib/circuits/bitify.circom:36:4 -> 7338204765041068580 != 21888242871839275222246405745257275088548364400416034343682204186581103413796
    at RTCtx.assert (./node_modules/snarkjs/src/calculateWitness.js:230:19)
    at Object.__f [as Num2Bits] (eval at Circuit (./node_modules/snarkjs/src/circuit.js:46:33), <anonymous>:11:9)
    at RTCtx.triggerComponent (./node_modules/snarkjs/src/calculateWitness.js:126:41)
    at callComponents.map (./node_modules/snarkjs/src/calculateWitness.js:167:51)
    at Array.map (<anonymous>)
    at RTCtx.setSignalFullName (./node_modules/snarkjs/src/calculateWitness.js:166:24)
    at RTCtx.setPin (./node_modules/snarkjs/src/calculateWitness.js:98:14)
    at Object.__f [as LessThan] (eval at Circuit (./node_modules/snarkjs/src/circuit.js:46:33), <anonymous>:2:9)
    at RTCtx.triggerComponent (./node_modules/snarkjs/src/calculateWitness.js:126:41)
    at callComponents.map (./node_modules/snarkjs/src/calculateWitness.js:167:51)
(node:15712) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15712) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Works fine with smaller numbers (e.g. 16000000)

weijiekoh commented 3 years ago

Hi! Currently, the upper limit of voice credits is 2 ^ 32 = 4294967296, hardcoded in the UpdateStateTree circuit.

I can raise the limit but it would not solve the underlying issue - that we may hit the limit anyway, so we might as well scale down their max voice credits on the client side.

e.g. even if i raise the limit to 2 ^ 64 = 18446744073709551616, that's only 18 tokens with precision 18.

And there are tokens with precision 0 etc. So MACI cannot accommodate them all.

MACI's real limit is somewhere less than 2 ^ 126, since the circuit does the following:

    // Calculate the new voice credit balance
    signal vote_options_leaf_squared;
    vote_options_leaf_squared <== vote_options_leaf_raw *
                                  vote_options_leaf_raw;

    signal user_vote_weight_squared;
    user_vote_weight_squared <== decrypted_command_out[CMD_VOTE_WEIGHT_IDX] *
                                 decrypted_command_out[CMD_VOTE_WEIGHT_IDX];

    signal new_voice_credit_balance;
    new_voice_credit_balance <== state_tree_data_raw[STATE_TREE_VOICE_CREDIT_BALANCE_IDX] +
                                 vote_options_leaf_squared -
                                 user_vote_weight_squared;

It can't be more than 126 bits or vote_options_leaf_squared will overflow when we square vote_options_leaf_raw.

I doubt that new_voice_credit_balance would overflow but the safe option is to have the limit as it currently is.

Is 4294967296 credits a sufficient maximum value for client apps? If so, there's no need to increase the limit anyway.

Hope this helps!