Skip to content

Commit f9541a4

Browse files
author
Release Manager
committed
gh-36020: Fixes #35804 by computing the DDT of non-square sboxes properly. Fixes #35804. After some investigation, it was found that many non-square sboxes did not work. In cases where the output size is bigger than the input size (e.g. `SBox(16,16,16,16,16,16,16,16)`), an incorrect DDT is produced. In cases where where input size is bigger, it leads to an IndexError such as in #35804. `di` spans from [0, `nrows`) When multiplied by `nrows`, a size of `nrows * (nrows - 1)` can be achieved which can be greater than the maximum size of the table when `nrows > ncols` (leading to the behaviour seen earlier). Changing the multiplication of nrows to ncols fixes the IndexError as well as produce the correct SBox for `SBox(16,16,16,16,16,16,16,16)`. Tested with doctests along with the following script: ```sage import random from sage.crypto.sbox import SBox random.seed(b"sbox") test = lambda x: print(SBox(x).difference_distribution_table()) # Square sbox test([7,6,0,4,2,5,1,3]) for _ in range(10): sbox = random.sample(range(16), 16) print(sbox) test(sbox) # Non-square sboxes for _ in range(10): # input > output sbox = random.sample(list(range(4)) * 4, 16) print(sbox) test(sbox) for _ in range(10): # input < output sbox = random.sample(range(16), 4) print(sbox) test(sbox) print("Passed all cases!") ``` <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes #1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #36020 Reported by: Potato K Reviewer(s): Kwankyu Lee
2 parents 80f6d77 + 1a9f82d commit f9541a4

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/sage/crypto/sbox.pyx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,54 @@ cdef class SBox(SageObject):
639639
[0 0 2 2 2 2 0 0]
640640
[0 2 2 0 0 2 2 0]
641641
[0 0 0 0 2 2 2 2]
642+
sage: S = SBox(7,4,8,6)
643+
sage: S.difference_distribution_table()
644+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
645+
[0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0]
646+
[0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2]
647+
[0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0]
648+
649+
TESTS::
650+
651+
Testing square SBoxes::
652+
653+
sage: from sage.crypto.sbox import SBox
654+
sage: S = SBox(7,6,0,4,2,5,1,3)
655+
sage: S.difference_distribution_table()
656+
[8 0 0 0 0 0 0 0]
657+
[0 2 2 0 2 0 0 2]
658+
[0 0 2 2 0 0 2 2]
659+
[0 2 0 2 2 0 2 0]
660+
[0 2 0 2 0 2 0 2]
661+
[0 0 2 2 2 2 0 0]
662+
[0 2 2 0 0 2 2 0]
663+
[0 0 0 0 2 2 2 2]
664+
665+
Testing non-square SBoxes::
666+
667+
sage: from sage.crypto.sbox import SBox
668+
sage: S = SBox(8,8,8,8)
669+
sage: S.difference_distribution_table()
670+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
671+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
672+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
673+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
674+
sage: S = SBox(7,4,8,6)
675+
sage: S.difference_distribution_table()
676+
[4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
677+
[0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0]
678+
[0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2]
679+
[0 2 0 0 0 0 0 0 0 0 0 0 2 0 0 0]
680+
sage: S = SBox(0,0,0,1,0,0,1,3)
681+
sage: S.difference_distribution_table()
682+
[8 0 0 0]
683+
[4 2 2 0]
684+
[2 4 0 2]
685+
[2 4 0 2]
686+
[4 2 2 0]
687+
[6 0 0 2]
688+
[2 4 0 2]
689+
[2 4 0 2]
642690
"""
643691
cdef Py_ssize_t nrows = 1 << self.m
644692
cdef Py_ssize_t ncols = 1 << self.n
@@ -649,7 +697,7 @@ cdef class SBox(SageObject):
649697
for i in range(nrows):
650698
si = self._S_list[i]
651699
for di in range(nrows):
652-
L[di*nrows + si ^ self._S_list[i ^ di]] += 1
700+
L[di*ncols + si ^ self._S_list[i ^ di]] += 1
653701

654702
A = matrix(ZZ, nrows, ncols, L)
655703
A.set_immutable()

0 commit comments

Comments
 (0)