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

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

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# 

# 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 absolute_import 

  

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 .ntl_ZZ import unpickle_class_args 

from .ntl_GF2EContext import ntl_GF2EContext 

from .ntl_GF2EContext cimport ntl_GF2EContext_class 

from .ntl_GF2E cimport ntl_GF2E 

  

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

# 

# ntl_GF2EX: Polynomials over GF(2) via NTL 

# 

# AUTHORS: 

# - Martin Albrecht <malb@informatik.uni-bremen.de> 2006-01: initial version 

# 

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

  

cdef class ntl_GF2EX(object): 

r""" 

Minimal wrapper of NTL's GF2EX class. 

""" 

def __init__(self, modulus=None, x=[]): 

""" 

Minimal wrapper of NTL's GF2EX class. 

  

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

[[1] [0 1]] 

""" 

if modulus is None: 

raise ValueError("You must specify a modulus when creating a GF2E.") 

  

ccreadstr(self.x, str(x)) 

  

def __cinit__(self, modulus=None, x=[]): 

#################### WARNING ################### 

## Before creating a GF2E, you must create a ## 

## GF2EContext, and restore it. In Python, ## 

## the error checking in __init__ will prevent## 

## you from constructing an ntl_GF2E ## 

## inappropriately. However, from Cython, you## 

## could do r = ntl_GF2E.__new__(ntl_GF2E) without 

## first restoring a GF2EContext, which could ## 

## have unfortunate consequences. See _new ## 

## defined below for an example of the right ## 

## way to short-circuit __init__ (or just call## 

## _new in your own code). ## 

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

if modulus is None: 

return 

if isinstance(modulus, ntl_GF2EContext_class): 

self.c = <ntl_GF2EContext_class>modulus 

self.c.restore_c() 

else: 

self.c = <ntl_GF2EContext_class>ntl_GF2EContext(modulus) 

self.c.restore_c() 

  

cdef ntl_GF2E _new_element(self): 

cdef ntl_GF2E r 

self.c.restore_c() 

r = ntl_GF2E.__new__(ntl_GF2E) 

r.c = self.c 

return r 

  

cdef ntl_GF2EX _new(self): 

cdef ntl_GF2EX r 

self.c.restore_c() 

r = ntl_GF2EX.__new__(ntl_GF2EX) 

r.c = self.c 

return r 

  

def modulus_context(self): 

return self.c 

  

def __dealloc__(self): 

if <object>self.c is not None: 

self.c.restore_c() 

  

def __reduce__(self): 

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0 1] [1 0 0 1] [1]]') 

sage: f == loads(dumps(f)) 

True 

""" 

return unpickle_class_args, (ntl_GF2EX, (self.c, self.__repr__())) 

  

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

""" 

Compare self to other. 

  

EXAMPLES:: 

  

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0 1] [1 0 0 1] [1]]') 

sage: g = ntl.GF2EX(ctx, '[[1 0 1] [1 1] [1] [0 0 1]]') 

sage: f == f 

True 

sage: f == g 

False 

sage: f == "??" 

False 

""" 

self.c.restore_c() 

  

if op != Py_EQ and op != Py_NE: 

raise TypeError("elements of GF(2^e)[X] are not ordered") 

  

cdef ntl_GF2EX b 

try: 

b = <ntl_GF2EX?>other 

except TypeError: 

return NotImplemented 

  

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

  

def __repr__(self): 

""" 

Return the string representation of self. 

  

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: ntl.GF2EX(ctx, '[[1 0] [2 1]]').__repr__() 

'[[1] [0 1]]' 

""" 

return ccrepr(self.x) 

  

def __mul__(ntl_GF2EX self, other): 

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]') 

sage: f*g ## indirect doctest 

[[1 0 1 1] [0 0 1 1] [1 0 0 1 0 1] [0 1 0 1]] 

""" 

cdef ntl_GF2EX y 

cdef ntl_GF2EX r = self._new() 

if not isinstance(other, ntl_GF2EX): 

other = ntl_GF2EX(self.c, other) 

y = other 

sig_on() 

GF2EX_mul(r.x, self.x, y.x) 

sig_off() 

return r 

  

def __sub__(ntl_GF2EX self, other): 

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]') 

sage: f-g ## indirect doctest 

[[0 0 1 1] [0 0 1 0 1] [1 0 1]] 

""" 

cdef ntl_GF2EX y 

cdef ntl_GF2EX r = self._new() 

if not isinstance(other, ntl_GF2EX): 

other = ntl_GF2EX(self.c, other) 

y = other 

sig_on() 

GF2EX_sub(r.x, self.x, y.x) 

sig_off() 

return r 

  

def __add__(ntl_GF2EX self, other): 

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

sage: g = ntl.GF2EX(ctx, '[[1 0 1 1] [0 1 1 0 1] [1 0 1]]') 

sage: f+g ## indirect doctest 

[[0 0 1 1] [0 0 1 0 1] [1 0 1]] 

""" 

cdef ntl_GF2EX y 

cdef ntl_GF2EX r = self._new() 

if not isinstance(other, ntl_GF2EX): 

other = ntl_GF2EX(self.c, other) 

y = other 

sig_on() 

GF2EX_add(r.x, self.x, y.x) 

sig_off() 

return r 

  

def __neg__(ntl_GF2EX self): 

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

sage: -f ## indirect doctest 

[[1] [0 1]] 

""" 

cdef ntl_GF2EX r = self._new() 

sig_on() 

GF2EX_negate(r.x, self.x) 

sig_off() 

return r 

  

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

""" 

EXAMPLES: 

sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1,1,0,1])) 

sage: f = ntl.GF2EX(ctx, '[[1 0] [2 1]]') 

sage: f**2 ## indirect doctest 

[[1] [] [0 0 1]] 

""" 

cdef ntl_GF2EX r = self._new() 

sig_on() 

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

sig_off() 

return r