Skip to content

Commit c69a13a

Browse files
Add quarter-round function
1 parent 4b663fb commit c69a13a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

crypto/ChaCha20.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@ def __new__(cls, key: bytes, nonce: bytes, counter: int = 0):
2424
instance.key = key
2525
instance.nonce = nonce
2626
instance.counter = counter
27-
return instance
27+
return instance
28+
def _quarter_round(self, state: List[int], a: int, b: int, c: int, d: int):
29+
state[a] = (state[a] + state[b]) % (2**32)
30+
state[d] ^= state[a]
31+
state[d] = ((state[d] << 16) | (state[d] >> 16)) % (2**32)
32+
state[c] = (state[c] + state[d]) % (2**32)
33+
state[b] ^= state[c]
34+
state[b] = ((state[b] << 12) | (state[b] >> 20)) % (2**32)
35+
state[a] = (state[a] + state[b]) % (2**32)
36+
state[d] ^= state[a]
37+
state[d] = ((state[d] << 8) | (state[d] >> 24)) % (2**32)
38+
state[c] = (state[c] + state[d]) % (2**32)
39+
state[b] ^= state[c]
40+
state[b] = ((state[b] << 7) | (state[b] >> 25)) % (2**32)

0 commit comments

Comments
 (0)