Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yash1io committed Aug 2, 2024
1 parent d93ae94 commit d52625a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 22 deletions.
5 changes: 3 additions & 2 deletions circuits/ctr.circom
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template EncryptCTR(l,nk){
signal cipherBlocks[n][4][4];
component AddCipher[n];

for(var i=0;i<n;i++){
for(var i = 0 ; i < n; i++){
aes[i] = Cipher(nk);
ivBlock[3][3] = (ivBlock[3][3] + i)%256;
aes[i].key <== key;
Expand Down Expand Up @@ -58,7 +58,7 @@ template ToBlocks(l){

var i, j, k;

for (var idx = 0; idx < l; idx++) {
for (var idx = 0; idx < l; idx++) {
blocks[i][k][j] <== stream[idx];
k = k + 1;
if (k == 4){
Expand All @@ -77,6 +77,7 @@ template ToBlocks(l){
}
}

// convert blocks of 16 bytes to stream of bytes
template ToStream(n,l){
signal input blocks[n][4][4];

Expand Down
124 changes: 104 additions & 20 deletions tests/ctr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe("ToBlocks", () => {
{
blocks: [
[
[0x32, 0x88, 0x31, 0xe0],
[0x43, 0x5a, 0x31, 0x37],
[0xf6, 0x30, 0x98, 0x07],
[0xa8, 0x8d, 0xa2, 0x34],
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x34],
],
],
}
Expand All @@ -43,10 +43,10 @@ describe("ToBlocks", () => {
{
blocks: [
[
[0x32, 0x88, 0x31, 0xe0],
[0x43, 0x5a, 0x31, 0x37],
[0xf6, 0x30, 0x98, 0x07],
[0xa8, 0x8d, 0xa2, 0x01],
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x01],
],
],
}
Expand All @@ -67,10 +67,10 @@ describe("ToBlocks", () => {
{
blocks: [
[
[0x32, 0x88, 0x31, 0xe0],
[0x43, 0x5a, 0x31, 0x37],
[0xf6, 0x30, 0x98, 0x07],
[0xa8, 0x8d, 0x01, 0x00],
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0x01],
[0xe0, 0x37, 0x07, 0x00],
],
],
}
Expand All @@ -86,19 +86,19 @@ describe("ToBlocks", () => {

await circuit.expectPass(
{
stream: [0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x34, 0x12],
stream: [0x32, 0x88, 0x31, 0xe0, 0x42, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x34, 0x12],
},
{
blocks: [
[
[0x32, 0x88, 0x31, 0xe0],
[0x43, 0x5a, 0x31, 0x37],
[0xf6, 0x30, 0x98, 0x07],
[0xa8, 0x8d, 0xa2, 0x34],
[0x32, 0x42, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x34],
],
[
[0x12, 0x01, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x00],
[0x12, 0x00, 0x00, 0x00],
[0x01, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x00],
[0x00, 0x00, 0x00, 0x00],
],
Expand Down Expand Up @@ -145,7 +145,7 @@ describe("EncryptCTR", () => {
// 5AE4DF3E DBD5D35E 5B4F0902 0DB03EAB
// 1E031DDA 2FBE03D1 792170A0 F3009CEE

it.skip("should encrypt multiple blocks correctly", async () => {
it("should encrypt multiple blocks correctly", async () => {
circuit = await circomkit.WitnessTester(`EncryptCTR`, {
file: "ctr",
template: "EncryptCTR",
Expand Down Expand Up @@ -175,3 +175,87 @@ describe("EncryptCTR", () => {
);
});
});

describe("ToStream", () => {
let circuit: WitnessTester<["blocks"], ["stream"]>;
it("should convert blocks to stream#1", async () => {
circuit = await circomkit.WitnessTester(`ToStream`, {
file: "ctr",
template: "ToStream",
params: [1, 16],
});
console.log("@ToStream #constraints:", await circuit.getConstraintCount());

await circuit.expectPass(
{
blocks: [
[
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x34],
],
],
},
{
stream: [0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x34],
}
);
});
it("should convert blocks to stream#2", async () => {
circuit = await circomkit.WitnessTester(`ToStream`, {
file: "ctr",
template: "ToStream",
params: [1, 15],
});
console.log("@ToStream #constraints:", await circuit.getConstraintCount());

await circuit.expectPass(
{
blocks: [
[
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x01],
],
],
},
{
stream: [0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2],
}
);
});
it("should convert multiple blocks to stream", async () => {
circuit = await circomkit.WitnessTester(`ToStream`, {
file: "ctr",
template: "ToStream",
params: [2, 18],
});
console.log("@ToStream #constraints:", await circuit.getConstraintCount());

await circuit.expectPass(
{
blocks: [
[
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x01],
],
[
[0x32, 0x43, 0xf6, 0xa8],
[0x88, 0x5a, 0x30, 0x8d],
[0x31, 0x31, 0x98, 0xa2],
[0xe0, 0x37, 0x07, 0x01],
],
],
},
{
stream: [
0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x01, 0x32, 0x88,
],
}
);
});
});

0 comments on commit d52625a

Please sign in to comment.