Skip to content

Commit

Permalink
chore: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Aug 2, 2024
1 parent 52cc15d commit f6a0698
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
10 changes: 5 additions & 5 deletions packages/circuits/email-verifier.circom
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
37 changes: 0 additions & 37 deletions packages/circuits/helpers/body-masker.circom

This file was deleted.

6 changes: 3 additions & 3 deletions packages/circuits/tests/body-masker.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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],
});
});

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/circuits/tests/test-circuits/body-masker-test.circom
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma circom 2.1.6;

include "../../helpers/body-masker.circom";
include "../../utils/bytes.circom";

component main = BodyMasker(10);
component main = ByteMask(10);
36 changes: 36 additions & 0 deletions packages/circuits/utils/bytes.circom
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

0 comments on commit f6a0698

Please sign in to comment.