Skip to content

Commit 7023894

Browse files
author
=
committed
Fix line length formatting issues for ruff
1 parent 2222207 commit 7023894

1 file changed

Lines changed: 45 additions & 9 deletions

File tree

ciphers/aes_128.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""
22
Advanced Encryption Standard (AES) - 128 bit
33
4-
AES is a symmetric block cipher chosen by the U.S. government to protect classified information.
5-
AES is implemented in software and hardware throughout the world to encrypt sensitive data.
6-
This implementation provides the core AES-128 block encryption algorithm (operating on 16-byte blocks).
4+
AES is a symmetric block cipher chosen by the U.S. government to
5+
protect classified information. AES is implemented in software
6+
and hardware throughout the world to encrypt sensitive data.
7+
This implementation provides the core AES-128 block encryption
8+
algorithm (operating on 16-byte blocks).
79
810
Reference: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
911
"""
@@ -275,6 +277,11 @@
275277
def sub_bytes(state: list[int]) -> None:
276278
"""
277279
Applies the AES S-Box substitution to each byte in the state.
280+
281+
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
282+
>>> sub_bytes(state)
283+
>>> state[:4]
284+
[99, 124, 119, 123]
278285
"""
279286
for i in range(16):
280287
state[i] = S_BOX[state[i]]
@@ -283,6 +290,11 @@ def sub_bytes(state: list[int]) -> None:
283290
def shift_rows(state: list[int]) -> None:
284291
"""
285292
Shifts the rows of the 4x4 state matrix.
293+
294+
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
295+
>>> shift_rows(state)
296+
>>> state
297+
[0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]
286298
"""
287299
# Row 0: unchanged
288300
# Row 1: shifted left by 1
@@ -293,25 +305,39 @@ def shift_rows(state: list[int]) -> None:
293305
state[3], state[7], state[11], state[15] = state[15], state[3], state[7], state[11]
294306

295307

308+
<<<<<<< HEAD
296309
def galois_multiply(a: int, b: int) -> int:
310+
=======
311+
def galois_multiply(multiplicand: int, multiplier: int) -> int:
312+
>>>>>>> b42bd6a1 (Fix line length formatting issues for ruff)
297313
"""
298314
Multiplies two numbers in the GF(2^8) Galois field.
315+
316+
>>> galois_multiply(2, 3)
317+
6
318+
>>> galois_multiply(87, 19)
319+
254
299320
"""
300321
p = 0
301322
for _ in range(8):
302-
if b & 1:
303-
p ^= a
304-
hi_bit_set = a & 0x80
305-
a <<= 1
323+
if multiplier & 1:
324+
p ^= multiplicand
325+
hi_bit_set = multiplicand & 0x80
326+
multiplicand <<= 1
306327
if hi_bit_set:
307-
a ^= 0x11B # x^8 + x^4 + x^3 + x + 1
308-
b >>= 1
328+
multiplicand ^= 0x11B # x^8 + x^4 + x^3 + x + 1
329+
multiplier >>= 1
309330
return p % 256
310331

311332

312333
def mix_columns(state: list[int]) -> None:
313334
"""
314335
Mixes the columns of the state matrix to provide diffusion.
336+
337+
>>> state = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
338+
>>> mix_columns(state)
339+
>>> state
340+
[2, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
315341
"""
316342
for i in range(4):
317343
col = state[i * 4 : (i + 1) * 4]
@@ -332,6 +358,12 @@ def mix_columns(state: list[int]) -> None:
332358
def add_round_key(state: list[int], round_key: list[int]) -> None:
333359
"""
334360
XORs the state matrix with the current round key.
361+
362+
>>> state = [0] * 16
363+
>>> round_key = [1] * 16
364+
>>> add_round_key(state, round_key)
365+
>>> state
366+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
335367
"""
336368
for i in range(16):
337369
state[i] ^= round_key[i]
@@ -340,6 +372,10 @@ def add_round_key(state: list[int], round_key: list[int]) -> None:
340372
def key_expansion(key: bytes) -> list[int]:
341373
"""
342374
Expands a 16-byte key into 11 round keys (176 bytes total).
375+
376+
>>> key = bytes([0] * 16)
377+
>>> len(key_expansion(key))
378+
176
343379
"""
344380
key_schedule = list(key)
345381
for i in range(4, 44):

0 commit comments

Comments
 (0)