You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
0 commit comments