Skip to content

Commit

Permalink
Refactor: more compact LowMC Sbox and linear layers
Browse files Browse the repository at this point in the history
  • Loading branch information
p-huynh committed Feb 3, 2025
1 parent 44e80cd commit 3f9c4ca
Showing 1 changed file with 5 additions and 29 deletions.
34 changes: 5 additions & 29 deletions claasp/ciphers/block_ciphers/lowmc_block_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
# ****************************************************************************


from os.path import exists
from os.path import dirname
from os.path import exists
from os.path import realpath

from claasp.cipher import Cipher
from claasp.name_mappings import INPUT_PLAINTEXT, INPUT_KEY
from claasp.ciphers.block_ciphers import lowmc_generate_matrices
from claasp.name_mappings import INPUT_PLAINTEXT, INPUT_KEY

PARAMETERS_CONFIGURATION_LIST = [
# See https://tches.iacr.org/index.php/TCHES/article/view/8680/8239 Table 6
Expand Down Expand Up @@ -221,7 +221,7 @@ def __init__(self, block_bit_size=128, key_bit_size=128, number_of_rounds=0, num
self.block_bit_size = block_bit_size
self.key_bit_size = key_bit_size
self.WORD_SIZE = self.block_bit_size // 2
self.sbox = [0x00, 0x01, 0x03, 0x06, 0x07, 0x04, 0x05, 0x02]
self.sbox = [0x0, 0x7, 0x6, 0x5, 0x4, 0x1, 0x3, 0x2]
self.matrices_for_linear_layer = []
self.ROUND_CONSTANTS = []
# Round key derivation matrices
Expand Down Expand Up @@ -251,10 +251,10 @@ def __init__(self, block_bit_size=128, key_bit_size=128, number_of_rounds=0, num

for r in range(number_of_rounds):
# Nonlinear layer
sbox_layer_picnic = self.sbox_layer_picnic(plaintext_id)
sbox_layer = self.sbox_layer(plaintext_id)

# Affine layer
linear_layer = self.linear_layer(sbox_layer_picnic, r)
linear_layer = self.linear_layer(sbox_layer, r)
round_constant = self.add_round_constant(linear_layer, r)

# Generate round key and add to the state
Expand Down Expand Up @@ -421,30 +421,6 @@ def sbox_layer(self, plaintext_id):
[list(range(3 * self.N_SBOX, self.block_bit_size))],
self.block_bit_size).id

def sbox_layer_picnic(self, plaintext_id):
"""
In the Picnic-Ref-Implementation, each 3-bit chunk is first reversed before applying the Sbox.
The output is also reversed when added back to the state
e.g.
state[0:3] = '110' becomes '011', then is mapped to '110' via the
Sbox finally, it is reversed to '011' for the state-update.
"""

sbox_output = [''] * self.N_SBOX

# m computations of 3 - bit sbox
# remaining n - 3m bits remain the same
for i in range(self.N_SBOX):
sbox_output[i] = self.add_SBOX_component([plaintext_id], [list(range(3 * i, 3 * (i + 1)))[::-1]],
3, self.sbox).id

return self.add_concatenate_component(sbox_output + [plaintext_id],
[list(range(3))[::-1]] * self.N_SBOX +
[list(range(3 * self.N_SBOX, self.block_bit_size))],
self.block_bit_size).id

def update_key_register(self, key_id, round):
rk_id = self.add_linear_layer_component([key_id],
[list(range(self.key_bit_size))],
Expand Down

0 comments on commit 3f9c4ca

Please sign in to comment.