Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

#***************************************************************************** 

# Copyright (C) 2007 Martin Albrecht <malb@informatik.uni-bremen.de> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from __future__ import division 

  

from cysignals.signals cimport sig_on, sig_off 

from sage.ext.cplusplus cimport ccrepr, ccreadstr 

  

include 'misc.pxi' 

include 'decl.pxi' 

  

from cpython.object cimport Py_EQ, Py_NE 

from sage.rings.integer cimport Integer 

from sage.rings.integer_ring cimport IntegerRing_class 

  

############################################################################## 

# GF2: Bits 

############################################################################## 

  

cdef class ntl_GF2(object): 

r""" 

The \class{GF2} represents the field GF(2). Computationally 

speaking, it is not a particularly useful class. Its main use is 

to make the interfaces to the various finite field classes as 

uniform as possible. 

""" 

def __init__(self, v=None): 

r""" 

Initializes a NTL bit. 

  

EXAMPLES: 

sage: ntl.GF2(1) 

1 

sage: ntl.GF2(int(2)) 

0 

sage: ntl.GF2('1') 

1 

""" 

if isinstance(v, ntl_GF2): 

self.x = (<ntl_GF2>v).x 

elif isinstance(v, int) or isinstance(v, long) or isinstance(v, Integer): 

GF2_conv_long(self.x, int(v) % 2) 

elif v is not None: 

ccreadstr(self.x, str(v)) 

  

def __repr__(self): 

""" 

Return the string representation of self. 

  

EXAMPLES: 

sage: str(ntl.GF2(1)) # indirect doctest 

'1' 

""" 

return ccrepr(self.x) 

  

def __reduce__(self): 

""" 

Serializes self. 

  

EXAMPLES: 

sage: a = ntl.GF2(1) 

sage: loads(dumps(a)) 

1 

""" 

return unpickle_class_value, (ntl_GF2, int(self)) 

  

def __richcmp__(ntl_GF2 self, other, int op): 

""" 

Compare self to other. 

  

EXAMPLES:: 

  

sage: a = ntl.GF2(1) 

sage: b = ntl.GF2(0) 

sage: a == a 

True 

sage: a == b 

False 

sage: a == 1 

True 

sage: a < b 

Traceback (most recent call last): 

... 

TypeError: elements of GF(2) are not ordered 

""" 

if op != Py_EQ and op != Py_NE: 

raise TypeError("elements of GF(2) are not ordered") 

  

cdef ntl_GF2 b 

try: 

b = <ntl_GF2?>other 

except TypeError: 

b = ntl_GF2(other) 

  

return (op == Py_EQ) == (self.x == b.x) 

  

def __mul__(self, other): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: o*o 

1 

sage: o*z 

0 

sage: z*o 

0 

sage: z*z 

0 

""" 

cdef ntl_GF2 r = ntl_GF2.__new__(ntl_GF2) 

if not isinstance(self, ntl_GF2): 

self = ntl_GF2(self) 

if not isinstance(other, ntl_GF2): 

other = ntl_GF2(other) 

GF2_mul(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x) 

return r 

  

def __truediv__(self, other): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: o/o 

1 

sage: o/z 

Traceback (most recent call last): 

... 

ZeroDivisionError 

""" 

cdef ntl_GF2 r 

if not isinstance(self, ntl_GF2): 

self = ntl_GF2(self) 

if not isinstance(other, ntl_GF2): 

other = ntl_GF2(other) 

if GF2_IsZero((<ntl_GF2>other).x): 

raise ZeroDivisionError 

r = ntl_GF2.__new__(ntl_GF2) 

GF2_div(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x) 

return r 

  

def __div__(self, other): 

return self / other 

  

def __sub__(self, other): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: o-o 

0 

sage: o-z 

1 

sage: z-o 

1 

sage: z-z 

0 

""" 

cdef ntl_GF2 r = ntl_GF2.__new__(ntl_GF2) 

if not isinstance(self, ntl_GF2): 

self = ntl_GF2(self) 

if not isinstance(other, ntl_GF2): 

other = ntl_GF2(other) 

GF2_sub(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x) 

return r 

  

def __add__(self, other): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: o+o 

0 

sage: o+z 

1 

sage: z+o 

1 

sage: z+z 

0 

""" 

cdef ntl_GF2 r = ntl_GF2.__new__(ntl_GF2) 

if not isinstance(self, ntl_GF2): 

self = ntl_GF2(self) 

if not isinstance(other, ntl_GF2): 

other = ntl_GF2(other) 

GF2_add(r.x, (<ntl_GF2>self).x, (<ntl_GF2>other).x) 

return r 

  

def __neg__(ntl_GF2 self): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: -z 

0 

sage: -o 

1 

""" 

cdef ntl_GF2 r = ntl_GF2.__new__(ntl_GF2) 

GF2_negate(r.x, self.x) 

return r 

  

def __pow__(ntl_GF2 self, long e, ignored): 

""" 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: z^2 

0 

sage: o^2 

1 

""" 

cdef ntl_GF2 r = ntl_GF2() 

GF2_power(r.x, self.x, e) 

return r 

  

def __int__(self): 

""" 

Return self as an int. 

  

EXAMPLES: 

sage: o = ntl.GF2(1) 

sage: z = ntl.GF2(0) 

sage: int(z) 

0 

sage: int(o) 

1 

""" 

cdef long l = GF2_conv_to_long(self.x) 

return int(l) 

  

def unpickle_class_value(cls, x): 

""" 

Here for unpickling. 

  

EXAMPLES: 

sage: sage.libs.ntl.ntl_GF2.unpickle_class_value(ntl.GF2,1) 

1 

sage: type(sage.libs.ntl.ntl_GF2.unpickle_class_value(ntl.GF2,1)) 

<type 'sage.libs.ntl.ntl_GF2.ntl_GF2'> 

""" 

return cls(x) 

  

def unpickle_class_args(cls, x): 

""" 

Here for unpickling. 

  

EXAMPLES: 

sage: sage.libs.ntl.ntl_GF2.unpickle_class_args(ntl.GF2,[1]) 

1 

sage: type(sage.libs.ntl.ntl_GF2.unpickle_class_args(ntl.GF2,[1])) 

<type 'sage.libs.ntl.ntl_GF2.ntl_GF2'> 

""" 

return cls(*x)