Skip to content

Commit

Permalink
test: added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Jul 11, 2024
1 parent 933a23f commit a156362
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
19 changes: 5 additions & 14 deletions packages/circuits/helpers/remove-soft-line-breaks.circom
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma circom 2.1.6;

include "circomlib/comparators.circom";
include "circomlib/mux1.circom";
include "circomlib/circuits/comparators.circom";
include "circomlib/circuits/mux1.circom";

template QuinSelector(array_length) {
signal input array[array_length];
Expand Down Expand Up @@ -50,9 +50,6 @@ template RemoveSoftLineBreaks(encoded_length, decoded_length) {
signal r_dec[decoded_length];
signal sum_dec[decoded_length];

r_enc[0] <== 1;
r_dec[0] <== 1;

// Helper components
component mux_enc[encoded_length];

Expand Down Expand Up @@ -102,6 +99,7 @@ template RemoveSoftLineBreaks(encoded_length, decoded_length) {
}

// Calculate powers of r for encoded
r_enc[0] <== 1;
for (var i = 1; i < encoded_length; i++) {
mux_enc[i] = Mux1();
mux_enc[i].c[0] <== r_enc[i - 1] * r;
Expand All @@ -111,6 +109,7 @@ template RemoveSoftLineBreaks(encoded_length, decoded_length) {
}

// Calculate powers of r for decoded
r_dec[0] <== 1;
for (var i = 1; i < decoded_length; i++) {
r_dec[i] <== r_dec[i - 1] * r;
}
Expand All @@ -129,12 +128,4 @@ template RemoveSoftLineBreaks(encoded_length, decoded_length) {

// Check if rlc for decoded is equal to rlc for encoded
is_valid <== IsEqual()([ sum_enc[encoded_length - 1], sum_dec[decoded_length - 1]]);
}

component main = RemoveSoftLineBreaks(17, 11);

/* INPUT = {
"encoded": [115, 101, 115, 58, 61, 13, 10, 45, 32, 83, 114, 101, 97, 107, 61, 13, 10],
"decoded": [115, 101, 115, 58, 45, 32, 83, 114, 101, 97, 107],
"r": 69
} */
}
56 changes: 56 additions & 0 deletions packages/circuits/tests/remove-soft-line-breaks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { wasm as wasm_tester } from "circom_tester";
import path from "path";

describe("RemoveSoftLineBreaks", () => {
let circuit: any;

beforeAll(async () => {
circuit = await wasm_tester(
path.join(
__dirname,
"./test-circuits/remove-soft-line-breaks-test.circom"
),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
output: path.join(__dirname, "./compiled-test-circuits"),
}
);
});

it("should correctly remove soft line breaks", async () => {
const input = {
encoded: [
115, 101, 115, 58, 61, 13, 10, 45, 32, 83, 114, 101, 97, 107,
61, 13, 10,
],
decoded: [115, 101, 115, 58, 45, 32, 83, 114, 101, 97, 107],
r: 69,
};

const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);

await circuit.assertOut(witness, {
is_valid: 1,
});
});

it("should fail when decoded input is incorrect", async () => {
const input = {
encoded: [
115, 101, 115, 58, 61, 13, 10, 45, 32, 83, 114, 101, 97, 107,
61, 13, 10,
],
decoded: [115, 101, 115, 58, 45, 32, 83, 114, 101, 97, 108], // Changed last character
r: 69,
};

const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);

await circuit.assertOut(witness, {
is_valid: 0,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.6;

include "../../helpers/remove-soft-line-breaks.circom";

component main = RemoveSoftLineBreaks(17, 11);

0 comments on commit a156362

Please sign in to comment.