Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Nov 18, 2024
1 parent 0bde22c commit e8cc32f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
38 changes: 16 additions & 22 deletions circuits/test/chacha20/chacha20-nivc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WitnessTester } from "circomkit";
import { circomkit, toUint32Array, uintArray32ToBits } from "../common";
import { chacha20_packed_hash } from "../common/poseidon";
import { DataHasher } from "../common/poseidon";
import { assert } from "chai";


Expand All @@ -18,9 +18,7 @@ describe("chacha20-nivc", () => {
// i.e. "e4e7f110" is serialized as "10 f1 e7 e4". So the way i am reading in inputs is
// to ensure that every 32 bit word is byte reversed before being turned into bits.
// i think this should be easy when we compute witness in rust.
let test = {
keyBytes: Buffer.from(
[
let keyBytes = [
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b,
Expand All @@ -29,43 +27,40 @@ describe("chacha20-nivc", () => {
0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f
]
),
nonceBytes: Buffer.from(
];

let nonceBytes =
[
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x4a,
0x00, 0x00, 0x00, 0x00
]
),
plaintextBytes: Buffer.from(
];
let plaintextBytes =
[
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
]
),
ciphertextBytes: Buffer.from(
];
let ciphertextBytes =
[
0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80, 0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d, 0x69, 0x81,
0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2, 0x0a, 0x27, 0xaf, 0xcc, 0xfd, 0x9f, 0xae, 0x0b,
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 = uintArray32ToBits(toUint32Array(test.ciphertextBytes))
const plaintextBits = uintArray32ToBits(toUint32Array(test.plaintextBytes))
];
const ciphertextBits = toInput(Buffer.from(ciphertextBytes))
const plaintextBits = toInput(Buffer.from(plaintextBytes))
const counterBits = uintArray32ToBits([1])[0]
let w = await circuit.compute({
key: uintArray32ToBits(toUint32Array(test.keyBytes)),
nonce: uintArray32ToBits(toUint32Array(test.nonceBytes)),
key: toInput(Buffer.from(keyBytes)),
nonce: toInput(Buffer.from(nonceBytes)),
counter: counterBits,
cipherText: ciphertextBits,
plainText: plaintextBits,
step_in: 0
}, (["step_out"]));
assert.deepEqual(w.step_out, chacha20_packed_hash(uintArray32ToBits(toUint32Array(test.plaintextBytes))));
assert.deepEqual(w.step_out, DataHasher(plaintextBytes));
});
});
});
Expand All @@ -85,5 +80,4 @@ export function fromInput(bits: number[]) {
buffer.writeUInt32LE(uint32Array[i], i * 4);
}
return buffer;
}

}
21 changes: 0 additions & 21 deletions circuits/test/common/poseidon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,4 @@ export function DataHasher(input: number[]): bigint {

// Return the last hash
return hashes[Math.floor(input.length / 16)];
}

// takes in plaintext[n][32], packs each 32 bit word and hashes it.
export function chacha20_packed_hash(Bytes: number[][]): bigint {
let hashes: bigint[] = [BigInt(0)]; // Initialize first hash as 0

for (let i = 0; i < Bytes.length; i++) {
let packedInput = BigInt(0);
for (let j = 0; j < 32; j++) {
packedInput += BigInt(Bytes[i][j]) * BigInt(Math.pow(2, j));
}
// Compute next hash using previous hash and packed input, but if packed input is zero, don't hash it.
if (packedInput == BigInt(0)) {
hashes.push(hashes[i]);
} else {
let hash = PoseidonModular([hashes[i], packedInput]);
hashes.push(hash);
}
}
// Return the last hash
return hashes[Bytes.length];
}

0 comments on commit e8cc32f

Please sign in to comment.