Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #35804 by computing the DDT of non-square sboxes properly. #36020

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/sage/crypto/sbox.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,54 @@ cdef class SBox(SageObject):
[0 0 2 2 2 2 0 0]
[0 2 2 0 0 2 2 0]
[0 0 0 0 2 2 2 2]
sage: S = SBox(7,4,8,6)
sage: S.difference_distribution_table()
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0]
[0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2]
[0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0]

TESTS::

Testing square SBoxes::

sage: from sage.crypto.sbox import SBox
sage: S = SBox(7,6,0,4,2,5,1,3)
sage: S.difference_distribution_table()
[8 0 0 0 0 0 0 0]
[0 2 2 0 2 0 0 2]
[0 0 2 2 0 0 2 2]
[0 2 0 2 2 0 2 0]
[0 2 0 2 0 2 0 2]
[0 0 2 2 2 2 0 0]
[0 2 2 0 0 2 2 0]
[0 0 0 0 2 2 2 2]

Testing non-square SBoxes::

sage: from sage.crypto.sbox import SBox
sage: S = SBox(8,8,8,8)
sage: S.difference_distribution_table()
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
sage: S = SBox(7,4,8,6)
sage: S.difference_distribution_table()
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0]
[0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2]
[0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0]
sage: S = SBox(0,0,0,1,0,0,1,3)
sage: S.difference_distribution_table()
[8 0 0 0]
[4 2 2 0]
[2 4 0 2]
[2 4 0 2]
[4 2 2 0]
[6 0 0 2]
[2 4 0 2]
[2 4 0 2]
"""
cdef Py_ssize_t nrows = 1 << self.m
cdef Py_ssize_t ncols = 1 << self.n
Expand All @@ -649,7 +697,7 @@ cdef class SBox(SageObject):
for i in range(nrows):
si = self._S_list[i]
for di in range(nrows):
L[di*nrows + si ^ self._S_list[i ^ di]] += 1
L[di*ncols + si ^ self._S_list[i ^ di]] += 1

A = matrix(ZZ, nrows, ncols, L)
A.set_immutable()
Expand Down