Skip to content

Commit

Permalink
improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
0x471 committed Aug 31, 2024
1 parent 6c69981 commit 4757611
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/chacha20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,6 @@ class ChaChaState {
}


static chacha20Block(key: UInt32[], nonce: UInt32[], counter: number): UInt32[] {
const state = new ChaChaState(key, nonce, counter);
const workingState = new ChaChaState(key, nonce, counter);

for (let i = 0; i < 10; i++) {
ChaChaState.innerBlock(workingState.state);
}

workingState.add(state);
return workingState.toLe4Bytes();
}


static innerBlock(state: UInt32[]) {
// Column rounds
this.quarterRound(state, 0, 4, 8, 12);
this.quarterRound(state, 1, 5, 9, 13);
this.quarterRound(state, 2, 6, 10, 14);
this.quarterRound(state, 3, 7, 11, 15);

// Diagonal rounds
this.quarterRound(state, 0, 5, 10, 15);
this.quarterRound(state, 1, 6, 11, 12);
this.quarterRound(state, 2, 7, 8, 13);
this.quarterRound(state, 3, 4, 9, 14);
}


static quarterRound(state: UInt32[], aIndex: number, bIndex: number, cIndex: number, dIndex: number) {
const rotate = (value: UInt32, bits: number) =>
UInt32.fromFields([Gadgets.rotate32(value.toFields()[0], bits, 'left')]);
Expand All @@ -106,4 +78,31 @@ class ChaChaState {

[state[aIndex], state[bIndex], state[cIndex], state[dIndex]] = [a, b, c, d];
}

static innerBlock(state: UInt32[]) {
// Column rounds
this.quarterRound(state, 0, 4, 8, 12);
this.quarterRound(state, 1, 5, 9, 13);
this.quarterRound(state, 2, 6, 10, 14);
this.quarterRound(state, 3, 7, 11, 15);

// Diagonal rounds
this.quarterRound(state, 0, 5, 10, 15);
this.quarterRound(state, 1, 6, 11, 12);
this.quarterRound(state, 2, 7, 8, 13);
this.quarterRound(state, 3, 4, 9, 14);
}

static chacha20Block(key: UInt32[], nonce: UInt32[], counter: number): UInt32[] {
const state = new ChaChaState(key, nonce, counter);
const workingState = new ChaChaState(key, nonce, counter);

for (let i = 0; i < 10; i++) {
ChaChaState.innerBlock(workingState.state);
}

workingState.add(state);
return workingState.toLe4Bytes();
}

}

0 comments on commit 4757611

Please sign in to comment.