Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_GF2.pyx
4096 views
1
#*****************************************************************************
2
# Copyright (C) 2007 Martin Albrecht <[email protected]>
3
#
4
# Distributed under the terms of the GNU General Public License (GPL)
5
#
6
# This code is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
# General Public License for more details.
10
#
11
# The full text of the GPL is available at:
12
#
13
# http://www.gnu.org/licenses/
14
#*****************************************************************************
15
16
include "../../ext/interrupt.pxi"
17
include "../../ext/stdsage.pxi"
18
include "../../ext/cdefs.pxi"
19
include 'misc.pxi'
20
include 'decl.pxi'
21
22
from sage.rings.integer cimport Integer
23
from sage.rings.integer_ring cimport IntegerRing_class
24
25
##############################################################################
26
# GF2: Bits
27
##############################################################################
28
29
cdef class ntl_GF2:
30
r"""
31
The \class{GF2} represents the field GF(2). Computationally
32
speaking, it is not a particularly useful class. Its main use is
33
to make the interfaces to the various finite field classes as
34
uniform as possible.
35
"""
36
def __init__(self, v=None):
37
r"""
38
Initializes a NTL bit.
39
40
EXAMPLES:
41
sage: ntl.GF2(1)
42
1
43
sage: ntl.GF2(int(2))
44
0
45
sage: ntl.GF2('1')
46
1
47
"""
48
if PY_TYPE_CHECK(v, ntl_GF2):
49
self.x = (<ntl_GF2>v).x
50
elif PyInt_Check(v) or PyLong_Check(v) or PY_TYPE_CHECK(v, Integer):
51
GF2_conv_long(self.x, int(v) % 2)
52
elif v is not None:
53
v = str(v)
54
sig_on()
55
GF2_from_str(&self.x, v)
56
sig_off()
57
58
def __cinit__(self):
59
GF2_construct(&self.x)
60
61
def __dealloc__(self):
62
GF2_destruct(&self.x)
63
64
def __repr__(self):
65
"""
66
Return the string representation of self.
67
68
EXAMPLES:
69
sage: str(ntl.GF2(1)) # indirect doctest
70
'1'
71
"""
72
return GF2_to_PyString(&self.x)
73
74
def __reduce__(self):
75
"""
76
Serializes self.
77
78
EXAMPLES:
79
sage: a = ntl.GF2(1)
80
sage: loads(dumps(a))
81
1
82
"""
83
return unpickle_class_value, (ntl_GF2, int(self))
84
85
def __richcmp__(self, other, op):
86
"""
87
Compare self to other.
88
89
EXAMPLES:
90
sage: a = ntl.GF2(1)
91
sage: b = ntl.GF2(0)
92
sage: a == b
93
False
94
"""
95
if op != 2 and op != 3:
96
raise TypeError, "elements in GF(2) are not ordered."
97
98
if not PY_TYPE_CHECK(other, ntl_GF2):
99
other = ntl_GF2(other)
100
101
if not PY_TYPE_CHECK(self, ntl_GF2):
102
self = ntl_GF2(self)
103
104
cdef int t
105
t = GF2_equal((<ntl_GF2>self).x, (<ntl_GF2>other).x)
106
if op == 2:
107
return t == 1
108
elif op == 3:
109
return t == 0
110
111
def __mul__(self, other):
112
"""
113
sage: o = ntl.GF2(1)
114
sage: z = ntl.GF2(0)
115
sage: o*o
116
1
117
sage: o*z
118
0
119
sage: z*o
120
0
121
sage: z*z
122
0
123
"""
124
cdef ntl_GF2 r = PY_NEW(ntl_GF2)
125
if not PY_TYPE_CHECK(self, ntl_GF2):
126
self = ntl_GF2(self)
127
if not PY_TYPE_CHECK(other, ntl_GF2):
128
other = ntl_GF2(other)
129
GF2_mul(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x)
130
return r
131
132
def __div__(self, other):
133
"""
134
sage: o = ntl.GF2(1)
135
sage: z = ntl.GF2(0)
136
sage: o/o
137
1
138
sage: o/z
139
Traceback (most recent call last):
140
...
141
ZeroDivisionError
142
"""
143
cdef ntl_GF2 r
144
if not PY_TYPE_CHECK(self, ntl_GF2):
145
self = ntl_GF2(self)
146
if not PY_TYPE_CHECK(other, ntl_GF2):
147
other = ntl_GF2(other)
148
if GF2_IsZero((<ntl_GF2>other).x):
149
raise ZeroDivisionError
150
r = PY_NEW(ntl_GF2)
151
GF2_div(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x)
152
return r
153
154
def __sub__(self, other):
155
"""
156
sage: o = ntl.GF2(1)
157
sage: z = ntl.GF2(0)
158
sage: o-o
159
0
160
sage: o-z
161
1
162
sage: z-o
163
1
164
sage: z-z
165
0
166
"""
167
cdef ntl_GF2 r = PY_NEW(ntl_GF2)
168
if not PY_TYPE_CHECK(self, ntl_GF2):
169
self = ntl_GF2(self)
170
if not PY_TYPE_CHECK(other, ntl_GF2):
171
other = ntl_GF2(other)
172
GF2_sub(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x)
173
return r
174
175
def __add__(self, other):
176
"""
177
sage: o = ntl.GF2(1)
178
sage: z = ntl.GF2(0)
179
sage: o+o
180
0
181
sage: o+z
182
1
183
sage: z+o
184
1
185
sage: z+z
186
0
187
"""
188
cdef ntl_GF2 r = PY_NEW(ntl_GF2)
189
if not PY_TYPE_CHECK(self, ntl_GF2):
190
self = ntl_GF2(self)
191
if not PY_TYPE_CHECK(other, ntl_GF2):
192
other = ntl_GF2(other)
193
GF2_add(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x)
194
return r
195
196
def __neg__(ntl_GF2 self):
197
"""
198
sage: o = ntl.GF2(1)
199
sage: z = ntl.GF2(0)
200
sage: -z
201
0
202
sage: -o
203
1
204
"""
205
cdef ntl_GF2 r = PY_NEW(ntl_GF2)
206
GF2_negate(r.x, self.x)
207
return r
208
209
def __pow__(ntl_GF2 self, long e, ignored):
210
"""
211
sage: o = ntl.GF2(1)
212
sage: z = ntl.GF2(0)
213
sage: z^2
214
0
215
sage: o^2
216
1
217
"""
218
cdef ntl_GF2 r = ntl_GF2()
219
GF2_power(r.x, self.x, e)
220
return r
221
222
def __int__(self):
223
"""
224
Return self as an int.
225
226
EXAMPLES:
227
sage: o = ntl.GF2(1)
228
sage: z = ntl.GF2(0)
229
sage: int(z)
230
0
231
sage: int(o)
232
1
233
"""
234
cdef long l = GF2_conv_to_long(self.x)
235
return int(l)
236
237
def unpickle_class_value(cls, x):
238
"""
239
Here for unpickling.
240
241
EXAMPLES:
242
sage: sage.libs.ntl.ntl_GF2.unpickle_class_value(ntl.GF2,1)
243
1
244
sage: type(sage.libs.ntl.ntl_GF2.unpickle_class_value(ntl.GF2,1))
245
<type 'sage.libs.ntl.ntl_GF2.ntl_GF2'>
246
"""
247
return cls(x)
248
249
def unpickle_class_args(cls, x):
250
"""
251
Here for unpickling.
252
253
EXAMPLES:
254
sage: sage.libs.ntl.ntl_GF2.unpickle_class_args(ntl.GF2,[1])
255
1
256
sage: type(sage.libs.ntl.ntl_GF2.unpickle_class_args(ntl.GF2,[1]))
257
<type 'sage.libs.ntl.ntl_GF2.ntl_GF2'>
258
"""
259
return cls(*x)
260
261
262