Skip to content

Commit

Permalink
minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
0x471 committed Aug 29, 2024
1 parent 55fd73a commit 321ab8a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/chacha20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,18 @@ describe('ChaCha', () => {
const nonceArray = octetsToUint32Array(nonce);
const chachaState = new ChaChaState(keyArray, nonceArray, counter);

const expectedValues: number[] = [
0x61707865, 0x3320646e, 0x79622d32, 0x6b206574, // ChaCha constants
0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, // Key
0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, // Key
0x00000001, // Block count
0x09000000, 0x4a000000, 0x00000000 // Nonce
const expectedState: UInt32[] = [
UInt32.fromValue(0x61707865n), UInt32.fromValue(0x3320646en), UInt32.fromValue(0x79622d32n), UInt32.fromValue(0x6b206574n), // ChaCha constants
UInt32.fromValue(0x03020100n), UInt32.fromValue(0x07060504n), UInt32.fromValue(0x0b0a0908n), UInt32.fromValue(0x0f0e0d0cn), // Key
UInt32.fromValue(0x13121110n), UInt32.fromValue(0x17161514n), UInt32.fromValue(0x1b1a1918n), UInt32.fromValue(0x1f1e1d1cn), // Key
UInt32.fromValue(0x00000001n), // Block count
UInt32.fromValue(0x09000000n), UInt32.fromValue(0x4a000000n), UInt32.fromValue(0x00000000n) // Nonce
];

const expectedState = expectedValues.map(value => UInt32.fromValue(BigInt(value)));

for (let i = 0; i < chachaState.state.length; i++) {
const receivedHex = toHexString(chachaState.state[i]);
const expectedHex = toHexString(expectedState[i]);
console.log(`Received: ${receivedHex}, Expected: ${expectedHex}`);
expect(receivedHex).toBe(expectedHex);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/chacha20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ChaChaState {
this.quarterRound(state, 3, 4, 9, 14);
}

add(other: ChaChaState): void {
add(other: ChaChaState) {
for (let i = 0; i < 16; i++) {
this.state[i] = UInt32.fromFields([Field.from((this.state[i].toBigint() + other.state[i].toBigint()) & 0xFFFFFFFFn)]);
}
Expand Down

0 comments on commit 321ab8a

Please sign in to comment.