Skip to content

Commit 914f81e

Browse files
author
Release Manager
committed
sagemathgh-41546: fix issue in `to_bytes()` for finite fields When a field had order `2^8*n` for `n >= 1` then there was an additional byte of padding to the to_bytes encoding because of the use of `nbits` on the order. In this PR we instead set the length from the size of the largest element represented in the field: `order - 1` Thanks ti @rasti37 for finding this. Fixes sagemath#41545 URL: sagemath#41546 Reported by: Giacomo Pope Reviewer(s): Frédéric Chapoton
2 parents affbd95 + 14b8e96 commit 914f81e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/sage/rings/finite_rings/element_base.pyx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ cdef class FiniteRingElement(CommutativeRingElement):
138138
sage: a.to_bytes(byteorder='little')
139139
b'\x16"\x00'
140140
"""
141-
length = (self.parent().order().nbits() + 7) // 8
141+
order = self.parent().order()
142+
length = ((order - 1).nbits() + 7) // 8
142143
return int(self).to_bytes(length=length, byteorder=byteorder)
143144

144145
def canonical_associate(self):
@@ -1128,8 +1129,18 @@ cdef class FinitePolyExtElement(FiniteRingElement):
11281129
sage: a = 136*z3^2 + 10*z3 + 125
11291130
sage: a.to_bytes()
11301131
b'7)\xa3'
1132+
1133+
TESTS:
1134+
1135+
Check that :issue:`41545` is fixed::
1136+
1137+
sage: F.<z2> = GF(2^8)
1138+
sage: a = F.from_integer(137)
1139+
sage: a.to_bytes()
1140+
b'\x89'
11311141
"""
1132-
length = (self.parent().order().nbits() + 7) // 8
1142+
order = self.parent().order()
1143+
length = ((order - 1).nbits() + 7) // 8
11331144
return self.to_integer().to_bytes(length=length, byteorder=byteorder)
11341145

11351146
cdef class Cache_base(SageObject):

0 commit comments

Comments
 (0)