From ad04340972f3d35a8d4915227d15c5502af7fe63 Mon Sep 17 00:00:00 2001 From: Anton <14254374+0xmad@users.noreply.github.com> Date: Fri, 19 Jul 2024 14:37:12 -0500 Subject: [PATCH] chore(circuits): optimize message to command (#1682) --- .../circom/trees/incrementalQuinaryTree.circom | 17 +++++++++++++++-- circuits/circom/utils/messageToCommand.circom | 6 +----- circuits/circom/utils/verifySignature.circom | 5 +---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/circuits/circom/trees/incrementalQuinaryTree.circom b/circuits/circom/trees/incrementalQuinaryTree.circom index 377fe95c95..a3b903017d 100644 --- a/circuits/circom/trees/incrementalQuinaryTree.circom +++ b/circuits/circom/trees/incrementalQuinaryTree.circom @@ -93,6 +93,13 @@ template Splicer(numItems) { // The output signal from the QuinSelector is and gets // wired to Mux1 (as above). + var inputs[NUM_OUTPUT_ITEMS]; + + for (var i = 0; i < numItems; i++) { + inputs[i] = in[i]; + } + inputs[NUM_OUTPUT_ITEMS - 1] = 0; + for (var i = 0; i < NUM_OUTPUT_ITEMS; i++) { // Determines if current index is greater than the insertion index. var computedIsIndexAfterInsertPoint = SafeGreaterThan(3)([i, index]); @@ -101,7 +108,7 @@ template Splicer(numItems) { var computedAdjustedIndex = i - computedIsIndexAfterInsertPoint; // Selects item from the original array or the leaf for insertion. - var computedQuinSelected = QuinSelector(NUM_OUTPUT_ITEMS)([in[0], in[1], in[2], in[3], 0], computedAdjustedIndex); + var computedQuinSelected = QuinSelector(NUM_OUTPUT_ITEMS)(inputs, computedAdjustedIndex); var computedIsIndexEqual = IsEqual()([index, i]); var mux = Mux1()([computedQuinSelected, leaf], computedIsIndexEqual); @@ -129,8 +136,14 @@ template QuinTreeInclusionProof(levels) { // Iteratively hash each level of path_elements with the leaf or previous hash for (var i = 0; i < levels; i++) { + var elements[LEAVES_PER_PATH_LEVEL]; + + for (var j = 0; j < LEAVES_PER_PATH_LEVEL; j++) { + elements[j] = path_elements[i][j]; + } + var computedSplicedLeaf[LEAVES_PER_NODE] = Splicer(LEAVES_PER_PATH_LEVEL)( - [path_elements[i][0], path_elements[i][1], path_elements[i][2], path_elements[i][3]], + elements, currentLeaf, path_index[i] ); diff --git a/circuits/circom/utils/messageToCommand.circom b/circuits/circom/utils/messageToCommand.circom index 70af685387..a9113269e6 100644 --- a/circuits/circom/utils/messageToCommand.circom +++ b/circuits/circom/utils/messageToCommand.circom @@ -46,11 +46,7 @@ template MessageToCommand() { // Decrypt the message using Poseidon decryption. var computedDecryptor[DECRYPTED_LENGTH] = PoseidonDecryptWithoutCheck(MSG_LENGTH)( - [ - message[0], message[1], message[2], message[3], - message[4], message[5], message[6], message[7], - message[8], message[9] - ], + message, 0, computedEcdh ); diff --git a/circuits/circom/utils/verifySignature.circom b/circuits/circom/utils/verifySignature.circom index dcc42c08aa..83deef7ba7 100644 --- a/circuits/circom/utils/verifySignature.circom +++ b/circuits/circom/utils/verifySignature.circom @@ -32,10 +32,7 @@ template EdDSAPoseidonVerifier_patched() { // convert the signature scalar S into its binary representation. var computedNum2Bits[254] = Num2Bits(254)(S); - var computedCompConstantIn[254]; - for (var i=0; i<253; i++) { - computedCompConstantIn[i] = computedNum2Bits[i]; - } + var computedCompConstantIn[254] = computedNum2Bits; computedCompConstantIn[253] = 0; // A component that ensures S is within a valid range,