From fe1076263d009744cf701ef3827d990684ff78cb Mon Sep 17 00:00:00 2001 From: ctrlc03 <93448202+ctrlc03@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:43:51 +0000 Subject: [PATCH] fix(topup): check valid new state leaf balance --- circuits/circom/processMessages.circom | 18 ++++++++++++++---- core/ts/Poll.ts | 1 + website/versioned_docs/version-v1.x/topup.md | 10 ++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 website/versioned_docs/version-v1.x/topup.md diff --git a/circuits/circom/processMessages.circom b/circuits/circom/processMessages.circom index 3121b46f47..8858a42f4a 100644 --- a/circuits/circom/processMessages.circom +++ b/circuits/circom/processMessages.circom @@ -401,8 +401,8 @@ template ProcessTopup(stateTreeDepth) { // msgType of topup command is 2 amt <== amount * (msgType - 1); index <== stateTreeIndex * (msgType - 1); - component validCreditBalance = LessEqThan(N_BITS); - // check stateIndex, if invalid index, set index and amount to zero + + // check stateIndex, if invalid index, set index to zero component validStateLeafIndex = LessEqThan(N_BITS); validStateLeafIndex.in[0] <== index; validStateLeafIndex.in[1] <== numSignUps; @@ -419,17 +419,27 @@ template ProcessTopup(stateTreeDepth) { amtMux.c[0] <== 0; amtMux.c[1] <== amt; - // check less than field size + // check new balance is valid signal newCreditBalance; + // add either 0 or the actual amount (if the state index is valid) newCreditBalance <== stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] + amtMux.out; + + // we need to ensure it did not overflow (so previous must be <= new) + component validCreditBalance = SafeLessEqThan(N_BITS); validCreditBalance.in[0] <== stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX]; validCreditBalance.in[1] <== newCreditBalance; + // if the new one is <= the old one, then we have a valid topup + component creditBalanceMux = Mux1(); + creditBalanceMux.s <== validCreditBalance.out; + creditBalanceMux.c[0] <== stateLeaf[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX]; + creditBalanceMux.c[1] <== newCreditBalance; + // update credit voice balance component newStateLeafHasher = Hasher4(); newStateLeafHasher.in[STATE_LEAF_PUB_X_IDX] <== stateLeaf[STATE_LEAF_PUB_X_IDX]; newStateLeafHasher.in[STATE_LEAF_PUB_Y_IDX] <== stateLeaf[STATE_LEAF_PUB_Y_IDX]; - newStateLeafHasher.in[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] <== newCreditBalance; + newStateLeafHasher.in[STATE_LEAF_VOICE_CREDIT_BALANCE_IDX] <== creditBalanceMux.out; newStateLeafHasher.in[STATE_LEAF_TIMESTAMP_IDX] <== stateLeaf[STATE_LEAF_TIMESTAMP_IDX]; component stateLeafPathIndices = QuinGeneratePathIndices(stateTreeDepth); diff --git a/core/ts/Poll.ts b/core/ts/Poll.ts index a63aee8998..4b92dac79e 100644 --- a/core/ts/Poll.ts +++ b/core/ts/Poll.ts @@ -657,6 +657,7 @@ export class Poll implements IPoll { const newStateLeaf = this.stateLeaves[stateIndex].copy(); // update the voice credit balance newStateLeaf.voiceCreditBalance += amount; + // save it this.stateLeaves[stateIndex] = newStateLeaf; // update the state tree diff --git a/website/versioned_docs/version-v1.x/topup.md b/website/versioned_docs/version-v1.x/topup.md new file mode 100644 index 0000000000..791b733a65 --- /dev/null +++ b/website/versioned_docs/version-v1.x/topup.md @@ -0,0 +1,10 @@ +--- +title: MACI key change +description: How key change messages work +sidebar_label: Key change +sidebar_position: 16 +--- + +# Topup + +MACI v1.1.1 introduced the topup feature. This was developed by [chaosma](https://github.com/chaosma) and their [article](https://hackmd.io/@chaosma/rkyPfI7Iq) + the initial idea [here](https://hackmd.io/@ef-zkp/rk6uaQBrI).