Skip to content

Commit 72a9f16

Browse files
committed
Add encoding and decoding for finite fields
1 parent 1a32b36 commit 72a9f16

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

src/sage/rings/finite_rings/element_base.pyx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ cdef class FiniteRingElement(CommutativeRingElement):
142142
else:
143143
raise ValueError("unknown algorithm")
144144

145+
def to_bytes(self, byteorder="big"):
146+
"""
147+
Return an array of bytes representing an integer. Internally relies
148+
on the python ``int.to_bytes()`` method. Length of byte array is
149+
determined from the field's order.
150+
151+
INPUT:
152+
153+
- ``byteorder`` -- str (default: ``"big"``); determines the byte order of
154+
``input_bytes``; can only be ``"big"`` or ``"little"``
155+
156+
OUTPUT:
157+
158+
- Bytes representing ``self``
159+
160+
EXAMPLES::
161+
162+
sage: F = GF(65537)
163+
sage: a = F(8726)
164+
sage: a.to_bytes()
165+
b'\x00"\x16'
166+
sage: a.to_bytes(byteorder="little")
167+
b'\x16"\x00'
168+
"""
169+
length = (self.parent().order().nbits() + 7) // 8
170+
return int(self).to_bytes(length=length, byteorder=byteorder)
145171

146172
cdef class FinitePolyExtElement(FiniteRingElement):
147173
"""
@@ -1083,6 +1109,38 @@ cdef class FinitePolyExtElement(FiniteRingElement):
10831109

10841110
integer_representation = deprecated_function_alias(33941, to_integer)
10851111

1112+
def to_bytes(self, byteorder="big"):
1113+
"""
1114+
Return an array of bytes representing an integer. Internally relies
1115+
on the python ``int.to_bytes()`` method. Length of byte array is
1116+
determined from the field's order.
1117+
1118+
INPUT:
1119+
1120+
- ``byteorder`` -- str (default: ``"big"``); determines the byte order of
1121+
the output; can only be ``"big"`` or ``"little"``
1122+
1123+
OUTPUT:
1124+
1125+
- Bytes representing ``self``
1126+
1127+
EXAMPLES::
1128+
1129+
sage: F.<z5> = GF(3^5)
1130+
sage: a = z5^4 + 2*z5^3 + 1
1131+
sage: a.to_bytes()
1132+
b'\x88'
1133+
1134+
::
1135+
1136+
sage: F.<z3> = GF(163^3)
1137+
sage: a = 136*z3^2 + 10*z3 + 125
1138+
sage: a.to_bytes()
1139+
b'7)\xa3'
1140+
"""
1141+
length = (self.parent().order().nbits() + 7) // 8
1142+
return self.to_integer().to_bytes(length=length, byteorder=byteorder)
1143+
10861144
cdef class Cache_base(SageObject):
10871145
cpdef FinitePolyExtElement fetch_int(self, number) noexcept:
10881146
r"""

src/sage/rings/finite_rings/finite_field_base.pyx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,52 @@ cdef class FiniteField(Field):
21172117
return [sum(x * y for x, y in zip(col, basis))
21182118
for col in B.columns()]
21192119

2120+
def from_bytes(self, input_bytes, byteorder="big"):
2121+
"""
2122+
Return the integer represented by the given array of bytes. Internally relies
2123+
on the python ``int.from_bytes()`` method.
2124+
2125+
INPUT:
2126+
2127+
- ``input_bytes`` -- a bytes-like object or iterable producing bytes
2128+
2129+
- ``byteorder`` -- str (default: ``"big"``); determines the byte order of
2130+
``input_bytes``; can only be ``"big"`` or ``"little"``
2131+
2132+
OUTPUT:
2133+
2134+
- A field element represented by the ``input_bytes``
2135+
2136+
EXAMPLES::
2137+
2138+
sage: input_bytes = b"some_bytes"
2139+
sage: F = GF(2**127 - 1)
2140+
sage: F.from_bytes(input_bytes)
2141+
545127616933790290830707
2142+
sage: a = F.from_bytes(input_bytes, byteorder="little"); a
2143+
544943659528996309004147
2144+
sage: type(a)
2145+
<class 'sage.rings.finite_rings.integer_mod.IntegerMod_gmp'>
2146+
2147+
::
2148+
2149+
sage: input_bytes = b"some_bytes"
2150+
sage: F_ext = GF(65537**5)
2151+
sage: F_ext.from_bytes(input_bytes)
2152+
29549*z5^4 + 40876*z5^3 + 52171*z5^2 + 13604*z5 + 20843
2153+
sage: F_ext.from_bytes(input_bytes, byteorder="little")
2154+
29539*z5^4 + 42728*z5^3 + 47440*z5^2 + 12423*z5 + 27473
2155+
2156+
TESTS::
2157+
2158+
sage: fields = [GF(2), GF(3), GF(65537), GF(2^10), GF(163^5)]
2159+
sage: for F in fields:
2160+
....: for _ in range(1000):
2161+
....: a = F.random_element()
2162+
....: assert F.from_bytes(a.to_bytes()) == a
2163+
"""
2164+
python_int = int.from_bytes(input_bytes, byteorder=byteorder)
2165+
return self.from_integer(python_int)
21202166

21212167
def unpickle_FiniteField_ext(_type, order, variable_name, modulus, kwargs):
21222168
r"""

src/sage/rings/integer.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7171,11 +7171,11 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
71717171
INPUT:
71727172
71737173
- ``length`` -- positive integer (default: ``1``); integer is represented in
7174-
``length`` bytes. ``OverflowError`` is raised if the integer is not representable
7175-
with the given length.
7174+
``length`` bytes. ``OverflowError`` is raised if the integer is not representable
7175+
with the given length.
71767176
71777177
- ``byteorder`` -- str (default: ``"big"``); determines the byte order of
7178-
``input_bytes``; can only be ``"big"`` or ``"little"``
7178+
the output; can only be ``"big"`` or ``"little"``
71797179
71807180
- ``is_signed`` -- boolean (default: ``False``); determines whether to use two's
71817181
compliment to represent the integer

0 commit comments

Comments
 (0)