From f6a0698197973c9f7175e5fc6049a94f0a34de58 Mon Sep 17 00:00:00 2001 From: shreyas-londhe Date: Fri, 2 Aug 2024 09:51:33 +0530 Subject: [PATCH] chore: minor refactoring --- packages/circuits/email-verifier.circom | 10 ++--- packages/circuits/helpers/body-masker.circom | 37 ------------------- packages/circuits/tests/body-masker.test.ts | 6 +-- .../test-circuits/body-masker-test.circom | 4 +- packages/circuits/utils/bytes.circom | 36 ++++++++++++++++++ 5 files changed, 46 insertions(+), 47 deletions(-) delete mode 100644 packages/circuits/helpers/body-masker.circom diff --git a/packages/circuits/email-verifier.circom b/packages/circuits/email-verifier.circom index e67bbb99..51a495b9 100644 --- a/packages/circuits/email-verifier.circom +++ b/packages/circuits/email-verifier.circom @@ -9,8 +9,8 @@ include "./lib/sha.circom"; include "./utils/array.circom"; include "./utils/regex.circom"; include "./utils/hash.circom"; +include "./utils/bytes.circom"; include "./helpers/remove-soft-line-breaks.circom"; -include "./helpers/body-masker.circom"; /// @title EmailVerifier @@ -147,11 +147,11 @@ template EmailVerifier(maxHeadersLength, maxBodyLength, n, k, ignoreBodyHashChec if (enableBodyMasking == 1) { signal input mask[maxBodyLength]; signal output maskedBody[maxBodyLength]; - component bodyMasker = BodyMasker(maxBodyLength); + component byteMask = ByteMask(maxBodyLength); - bodyMasker.body <== emailBody; - bodyMasker.mask <== mask; - maskedBody <== bodyMasker.masked_body; + byteMask.body <== emailBody; + byteMask.mask <== mask; + maskedBody <== byteMask.maskedBody; } } diff --git a/packages/circuits/helpers/body-masker.circom b/packages/circuits/helpers/body-masker.circom deleted file mode 100644 index 474aa237..00000000 --- a/packages/circuits/helpers/body-masker.circom +++ /dev/null @@ -1,37 +0,0 @@ -pragma circom 2.1.6; - -// Asserts that a given input is binary. -// -// Inputs: -// - in: an input signal, expected to be 0 or 1. -template AssertBit() { - signal input in; - in * (in - 1) === 0; -} - -// The BodyMasker template masks an input body array using a binary mask array. -// Each element in the body array is multiplied by the corresponding element in the mask array. -// The mask array is validated to ensure all elements are binary (0 or 1). -// -// Parameters: -// - maxBodyLength: The maximum length of the body and mask arrays. -// -// Inputs: -// - body: An array of signals representing the body to be masked. -// - mask: An array of signals representing the binary mask. -// -// Outputs: -// - masked_body: An array of signals representing the masked body. -template BodyMasker(maxBodyLength) { - signal input body[maxBodyLength]; - signal input mask[maxBodyLength]; - signal output masked_body[maxBodyLength]; - - component bit_check[maxBodyLength]; - - for (var i = 0; i < maxBodyLength; i++) { - bit_check[i] = AssertBit(); - bit_check[i].in <== mask[i]; - masked_body[i] <== body[i] * mask[i]; - } -} \ No newline at end of file diff --git a/packages/circuits/tests/body-masker.test.ts b/packages/circuits/tests/body-masker.test.ts index aea8f3a7..c1fd513b 100644 --- a/packages/circuits/tests/body-masker.test.ts +++ b/packages/circuits/tests/body-masker.test.ts @@ -1,7 +1,7 @@ import { wasm as wasm_tester } from "circom_tester"; import path from "path"; -describe("BodyMasker Circuit", () => { +describe("ByteMask Circuit", () => { let circuit: any; beforeAll(async () => { @@ -24,7 +24,7 @@ describe("BodyMasker Circuit", () => { const witness = await circuit.calculateWitness(input); await circuit.checkConstraints(witness); await circuit.assertOut(witness, { - masked_body: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0], + maskedBody: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0], }); }); @@ -38,7 +38,7 @@ describe("BodyMasker Circuit", () => { const witness = await circuit.calculateWitness(input); await circuit.checkConstraints(witness); await circuit.assertOut(witness, { - masked_body: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0], + maskedBody: [1, 0, 3, 0, 5, 0, 7, 0, 9, 0], }); } catch (error) { expect(error).toBeTruthy(); diff --git a/packages/circuits/tests/test-circuits/body-masker-test.circom b/packages/circuits/tests/test-circuits/body-masker-test.circom index 7066b248..217241eb 100644 --- a/packages/circuits/tests/test-circuits/body-masker-test.circom +++ b/packages/circuits/tests/test-circuits/body-masker-test.circom @@ -1,5 +1,5 @@ pragma circom 2.1.6; -include "../../helpers/body-masker.circom"; +include "../../utils/bytes.circom"; -component main = BodyMasker(10); \ No newline at end of file +component main = ByteMask(10); \ No newline at end of file diff --git a/packages/circuits/utils/bytes.circom b/packages/circuits/utils/bytes.circom index d4d96027..a394ce2e 100644 --- a/packages/circuits/utils/bytes.circom +++ b/packages/circuits/utils/bytes.circom @@ -147,3 +147,39 @@ template SplitBytesToWords (l,n,k) { out[i] <== bits2num[i].out; } } + +// Asserts that a given input is binary. +// +// Inputs: +// - in: an input signal, expected to be 0 or 1. +template AssertBit() { + signal input in; + in * (in - 1) === 0; +} + +// The ByteMask template masks an input body array using a binary mask array. +// Each element in the body array is multiplied by the corresponding element in the mask array. +// The mask array is validated to ensure all elements are binary (0 or 1). +// +// Parameters: +// - maxBodyLength: The maximum length of the body and mask arrays. +// +// Inputs: +// - body: An array of signals representing the body to be masked. +// - mask: An array of signals representing the binary mask. +// +// Outputs: +// - maskedBody: An array of signals representing the masked body. +template ByteMask(maxBodyLength) { + signal input body[maxBodyLength]; + signal input mask[maxBodyLength]; + signal output maskedBody[maxBodyLength]; + + component bit_check[maxBodyLength]; + + for (var i = 0; i < maxBodyLength; i++) { + bit_check[i] = AssertBit(); + bit_check[i].in <== mask[i]; + maskedBody[i] <== body[i] * mask[i]; + } +} \ No newline at end of file