Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Dec 5, 2024
1 parent 4ca28f8 commit 4b4d333
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
38 changes: 14 additions & 24 deletions circuits/chacha20/nivc/chacha20_nivc.circom
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,13 @@ template ChaCha20_NIVC(N) {

// the below can be both ciphertext or plaintext depending on the direction
// in => N 32-bit words => N 4 byte words
signal input plainText[N*4];
signal input plainText[N][32];
// out => N 32-bit words => N 4 byte words
signal input cipherText[N*4];

signal input step_in[1];
signal output step_out[1];

signal plaintextBits[N][32];
component toBits[N];
for (var i = 0 ; i < N ; i++) {
toBits[i] = fromWords32ToLittleEndian();
for (var j = 0 ; j < 4 ; j++) {
toBits[i].words[j] <== plainText[i*4 + j];
}
plaintextBits[i] <== toBits[i].data;
}

var tmp[16][32] = [
[
// constant 0x61707865
Expand Down Expand Up @@ -110,7 +100,7 @@ template ChaCha20_NIVC(N) {
// XOR block with input
for(j = 0; j < 16; j++) {
xors[i*16 + j] = XorBits(32);
xors[i*16 + j].a <== plaintextBits[i*16 + j];
xors[i*16 + j].a <== plainText[i*16 + j];
xors[i*16 + j].b <== rounds[i].out[j];
computedCipherText[i*16 + j] <== xors[i*16 + j].out;
}
Expand Down Expand Up @@ -140,17 +130,17 @@ template ChaCha20_NIVC(N) {
signal paddedCiphertextCheck <== IsEqualArrayPaddedLHS(N*4)([cipherText, bigEndianCiphertext]);
paddedCiphertextCheck === 1;

// component toBytes[N];
// signal bigEndianPlaintext[N*4];
// for(var i = 0 ; i < N; i++) {
// toBytes[i] = fromLittleEndianToWords32();
// for(var j = 0 ; j < 32 ; j++) {
// toBytes[i].data[j] <== plainText[i][j];
// }
// for(var j = 0; j < 4; j++) {
// bigEndianPlaintext[i*4 + j] <== toBytes[i].words[j];
// }
// }
signal data_hash <== DataHasher(N*4)(plainText);
component toBytes[N];
signal bigEndianPlaintext[N*4];
for(var i = 0 ; i < N; i++) {
toBytes[i] = fromLittleEndianToWords32();
for(var j = 0 ; j < 32 ; j++) {
toBytes[i].data[j] <== plainText[i][j];
}
for(var j = 0; j < 4; j++) {
bigEndianPlaintext[i*4 + j] <== toBytes[i].words[j];
}
}
signal data_hash <== DataHasher(N*4)(bigEndianPlaintext);
step_out[0] <== data_hash;
}
3 changes: 1 addition & 2 deletions circuits/test/chacha20/chacha20-nivc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ describe("chacha20-nivc", () => {
0xf9, 0x1b, 0x65, 0xc5, 0x52, 0x47, 0x33, 0xab, 0x8f, 0x59, 0x3d, 0xab, 0xcd, 0x62, 0xb3, 0x57,
0x16, 0x39, 0xd6, 0x24, 0xe6, 0x51, 0x52, 0xab, 0x8f, 0x53, 0x0c, 0x35, 0x9f, 0x08, 0x61, 0xd8
];
const ciphertextBits = toInput(Buffer.from(ciphertextBytes))
const plaintextBits = toInput(Buffer.from(plaintextBytes))
const counterBits = uintArray32ToBits([1])[0]
let w = await circuit.compute({
key: toInput(Buffer.from(keyBytes)),
nonce: toInput(Buffer.from(nonceBytes)),
counter: counterBits,
cipherText: ciphertextBits,
cipherText: ciphertextBytes,
plainText: plaintextBits,
step_in: 0
}, (["step_out"]));
Expand Down
3 changes: 1 addition & 2 deletions circuits/test/full/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,9 @@ describe("NIVC_FULL_CHACHA", async () => {
// Run ChaCha20
const counterBits = uintArray32ToBits([1])[0]
const ptIn = toInput(Buffer.from(http_response_plaintext));
const ctIn = toInput(Buffer.from(chacha20_http_response_ciphertext));
const keyIn = toInput(Buffer.from(Array(32).fill(0)));
const nonceIn = toInput(Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00]));
let chacha20 = await chacha20Circuit.compute({ key: keyIn, nonce: nonceIn, counter: counterBits, plainText: ptIn, cipherText: ctIn, length: http_response_plaintext.length, step_in: init_nivc_input }, ["step_out"]);
let chacha20 = await chacha20Circuit.compute({ key: keyIn, nonce: nonceIn, counter: counterBits, plainText: ptIn, cipherText: chacha20_http_response_ciphertext, length: http_response_plaintext.length, step_in: init_nivc_input }, ["step_out"]);
console.log("ChaCha20 `step_out`:", chacha20.step_out);
assert.deepEqual(http_response_hash, chacha20.step_out);

Expand Down

0 comments on commit 4b4d333

Please sign in to comment.