Skip to content

Commit

Permalink
address todo to reduce redundant constraint and input value
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Oct 29, 2024
1 parent 87d09f7 commit 0c7231a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
28 changes: 17 additions & 11 deletions circuits/aes-gcm/aes-gcm-fold.circom
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ include "./utils.circom";
template AESGCMFOLD(INPUT_LEN) {
assert(INPUT_LEN % 16 == 0);

var DATA_BYTES = (INPUT_LEN * 2) + 5;
var DATA_BYTES = (INPUT_LEN * 2) + 4;
log(DATA_BYTES);

signal input key[16];
signal input iv[12];
Expand All @@ -17,10 +18,22 @@ template AESGCMFOLD(INPUT_LEN) {
// step_in[0..INPUT_LEN] => accumulate plaintext blocks
// step_in[INPUT_LEN..INPUT_LEN*2] => accumulate ciphertext blocks
// step_in[INPUT_LEN*2..INPUT_LEN*2+4] => lastCounter
// step_in[INPUT_LEN*2+5] => foldedBlocks // TODO(WJ 2024-10-24): technically not needed if can read 4 bytes as a 32 bit number, Can do this easy with bits2num
signal input step_in[DATA_BYTES];
signal output step_out[DATA_BYTES];
signal counter <== step_in[INPUT_LEN*2 + 4];
signal counter;

// We extract the number from the 4 byte word counter
component last_counter_bits = BytesToBits(4);
for(var i = 0; i < 4; i ++) {
last_counter_bits.in[i] <== step_in[INPUT_LEN*2 + i];
}
component last_counter_num = Bits2Num(32);
// pass in reverse order
for (var i = 0; i< 32; i++){
last_counter_num.in[i] <== last_counter_bits.out[31 - i];
}

counter <== last_counter_num.out - 1;

// write new plain text block.
signal plainTextAccumulator[DATA_BYTES];
Expand Down Expand Up @@ -55,12 +68,5 @@ template AESGCMFOLD(INPUT_LEN) {
writeCounter.array_to_write_to <== cipherTextAccumulator;
writeCounter.array_to_write_at_index <== aes.counter;
writeCounter.index <== INPUT_LEN*2;
writeCounter.out ==> counterAccumulator;

// accumulate number of folded blocks
component writeNumberOfFoldedBlocks = WriteToIndex(DATA_BYTES, 1);
writeNumberOfFoldedBlocks.array_to_write_to <== counterAccumulator;
writeNumberOfFoldedBlocks.array_to_write_at_index <== [step_in[INPUT_LEN*2 + 4] + 1];
writeNumberOfFoldedBlocks.index <== INPUT_LEN*2 + 4;
writeNumberOfFoldedBlocks.out ==> step_out;
writeCounter.out ==> step_out;
}
24 changes: 9 additions & 15 deletions circuits/test/aes-gcm/aes-gcm-fold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ describe("aes-gcm-fold", () => {
let aad = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let ct = [0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78];

const counter = [0x00, 0x00, 0x00, 0x01];
const foldedBlocks = [0x00];
const step_in = new Array(32).fill(0x00).concat(counter).concat(foldedBlocks);

let expected = plainText.concat(ct).concat([0x00, 0x00, 0x00, 0x02]).concat([0x01]);
const counter = [0x00, 0x00, 0x00, 0x01];
const step_in = new Array(32).fill(0x00).concat(counter);

let expected = plainText.concat(ct).concat([0x00, 0x00, 0x00, 0x02]);
const witness = await circuit_one_block.compute({ key: key, iv: iv, plainText: plainText, aad: aad, step_in: step_in }, ["step_out"])
assert.deepEqual(witness.step_out, expected.map(BigInt));
});
Expand All @@ -34,7 +32,6 @@ describe("aes-gcm-fold", () => {
template: "AESGCMFOLD",
params: [16], // input len is 16 bytes
});


let key = [0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31];
let plainText = [0x74, 0x65, 0x73, 0x74, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30];
Expand All @@ -43,10 +40,9 @@ describe("aes-gcm-fold", () => {
let ct = [0x29, 0x29, 0xd2, 0xbb, 0x1a, 0xe9, 0x48, 0x04, 0x40, 0x2b, 0x8e, 0x77, 0x6e, 0x0d, 0x33, 0x56];

const counter = [0x00, 0x00, 0x00, 0x01];
const foldedBlocks = [0x00];
const step_in = new Array(32).fill(0x00).concat(counter).concat(foldedBlocks);
const step_in = new Array(32).fill(0x00).concat(counter);

let expected = plainText.concat(ct).concat([0x00, 0x00, 0x00, 0x02]).concat([0x01]);
let expected = plainText.concat(ct).concat([0x00, 0x00, 0x00, 0x02]);

const witness = await circuit_one_block.compute({ key: key, iv: iv, plainText: plainText, aad: aad, step_in: step_in }, ["step_out"])
assert.deepEqual(witness.step_out, expected.map(BigInt));
Expand All @@ -71,9 +67,8 @@ describe("aes-gcm-fold", () => {
let ct_part2 = [0x26, 0x75, 0x65, 0x30, 0x71, 0x3e, 0x4c, 0x06, 0x5a, 0xf1, 0xd3, 0xc4, 0xf5, 0x6e, 0x02, 0x04];

const counter = [0x00, 0x00, 0x00, 0x01];
const foldedBlocks = [0x00];
const step_in = new Array(64).fill(0x00).concat(counter).concat(foldedBlocks);
let expected = plainText1.concat(zero_block).concat(ct_part1).concat(zero_block).concat([0x00, 0x00, 0x00, 0x02]).concat([0x01]);
const step_in = new Array(64).fill(0x00).concat(counter);
let expected = plainText1.concat(zero_block).concat(ct_part1).concat(zero_block).concat([0x00, 0x00, 0x00, 0x02]);

const witness = await circuit_one_block.compute({ key: key, iv: iv, plainText: plainText1, aad: aad, step_in: step_in }, ["step_out"])
assert.deepEqual(witness.step_out, expected.map(BigInt));
Expand All @@ -96,9 +91,8 @@ describe("aes-gcm-fold", () => {
let ct_part2 = [0x26, 0x75, 0x65, 0x30, 0x71, 0x3e, 0x4c, 0x06, 0x5a, 0xf1, 0xd3, 0xc4, 0xf5, 0x6e, 0x02, 0x04];

const counter = [0x00, 0x00, 0x00, 0x02];
const foldedBlocks = [0x01];
const step_in = plainText1.concat(zero_block).concat(ct_part1).concat(zero_block).concat(counter).concat(foldedBlocks);
let expected = plainText1.concat(plainText2).concat(ct_part1).concat(ct_part2).concat([0x00, 0x00, 0x00, 0x03]).concat([0x02]);
const step_in = plainText1.concat(zero_block).concat(ct_part1).concat(zero_block).concat(counter);
let expected = plainText1.concat(plainText2).concat(ct_part1).concat(ct_part2).concat([0x00, 0x00, 0x00, 0x03]);

const witness = await circuit_one_block.compute({ key: key, iv: iv, plainText: plainText2, aad: aad, step_in: step_in }, ["step_out"])
assert.deepEqual(witness.step_out, expected.map(BigInt));
Expand Down

0 comments on commit 0c7231a

Please sign in to comment.