Bug in BooleanFunction: truth_table(format="hex") is wrong

26 views
Skip to first unread message

Paul Leopardi

unread,
Nov 23, 2017, 10:04:24 AM11/23/17
to sage-support
Hi all,
Sometimes, especially with functions of 8 variables, the BooleanFunction.truth_table() method with format='hex" gives wrong values that are the wrong length.
I don't yet have a login to Trac. Should I obtain one and report the bug there?
All the best, Paul

Bug in BooleanFunction.truth_table(format='hex")

To reproduce:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 7.6, Release Date: 2017-03-25                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: from sage.rings.integer_ring import ZZ
sage: from sage.crypto.boolean_function import BooleanFunction
sage: hex_str = "0123456789112345678921234567893123456789412345678951234567896123"
sage: len(hex_str)
64
sage: bf = BooleanFunction(hex_str)
sage: bf.nvariables()
8
sage: bf_tt_hex = bf.truth_table(format="hex")
sage: bf_tt_hex
'123456789112345678921234567893123456789412345670123456789112345678921234567893123456789412345678951234567896123'
sage: len(bf_tt_hex)
111
sage: S = ZZ(bf.truth_table(), 2).str(16)
sage: padding = "0" * ((1 << (bf.nvariables() - 2)) - len(S))
sage: tt_hex = padding + S
sage: tt_hex
'0123456789112345678921234567893123456789412345678951234567896123'
sage: len(tt_hex)
64

Suspect source code: https://github.com/sagemath/sage/blob/develop/src/sage/crypto/boolean_function.pyx lines 566 to 576:

if format == 'hex':
S = ""
S = ZZ(self.truth_table(),2).str(16)
S = "0"*((1<<(self._nvariables-2)) - len(S)) + S
for 1 <= i < self._truth_table.limbs:
if sizeof(long)==4:
t = "%04x"%self._truth_table.bits[i]
if sizeof(long)==8:
t = "%08x"%self._truth_table.bits[i]
S = t + S
return S

This could simply be replaced by:

if format == 'hex':
S = ZZ(self.truth_table(),2).str(16)
S = "0"*((1<<(self._nvariables-2)) - len(S)) + S
return S


Paul Leopardi

unread,
Nov 26, 2017, 6:17:32 AM11/26/17
to sage-support
I have now reported this suspected bug as https://trac.sagemath.org/ticket/24282

Reply all
Reply to author
Forward
0 new messages