Skip to content

Commit d09850e

Browse files
author
Release Manager
committed
sagemathgh-39322: Implement the chi function in sage.crypto.sboxes <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fix sagemath#39321 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [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 and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39322 Reported by: Rusydi H. Makarim Reviewer(s): Kwankyu Lee, Rusydi H. Makarim
2 parents 3503f4c + 03bb288 commit d09850e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/sage/crypto/sboxes.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,43 @@ def monomial_function(n, e):
399399
return SBox(X**e)
400400

401401

402+
def chi(n):
403+
r"""
404+
Return the `\chi` function defined over `\GF{2^n}` used in the nonlinear
405+
layer of Keccak and Xoodyak.
406+
407+
INPUT:
408+
409+
- ``n`` -- size of the S-Box
410+
411+
EXAMPLES::
412+
413+
sage: from sage.crypto.sboxes import chi
414+
sage: chi(3)
415+
(0, 3, 6, 1, 5, 4, 2, 7)
416+
sage: chi(3).is_permutation()
417+
True
418+
sage: chi(4).is_permutation()
419+
False
420+
sage: chi(5)
421+
(0, 9, 18, 11, 5, 12, 22, 15, 10, 3, 24, 1, 13, 4, 30, 7, 20, 21, 6,
422+
23, 17, 16, 2, 19, 26, 27, 8, 25, 29, 28, 14, 31)
423+
"""
424+
from sage.rings.integer_ring import ZZ
425+
from sage.rings.finite_rings.finite_field_constructor import GF
426+
from sage.modules.free_module_element import vector
427+
428+
table = [0]*(1 << n)
429+
430+
for x in range(1 << n):
431+
vx = vector(GF(2), ZZ(x).digits(base=2, padto=n))
432+
vy = [vx[i] + (vx[(i+1) % n] + 1)*vx[(i+2) % n] for i in range(n)]
433+
y = ZZ(vy, base=2)
434+
table[x] = y
435+
436+
return SBox(table)
437+
438+
402439
# Bijective S-Boxes mapping 9 bits to 9
403440
# =====================================
404441

0 commit comments

Comments
 (0)