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

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

r""" 

Linkage for arithmetic with NTL's GF2X elements. 

  

This file provides the backend for \class{Polynomial_GF2X} via 

templating. 

  

AUTHOR: 

-- Martin Albrecht (2008-10): initial version 

""" 

  

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

# Copyright (C) 2008 Martin Albrecht <M.R.Albrecht@rhul.ac.uk> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

  

from cysignals.signals cimport sig_on, sig_off 

  

from sage.libs.ntl.GF2 cimport * 

from sage.libs.ntl.GF2X cimport * 

  

  

cdef GF2X_c *celement_new(long parent): 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

""" 

return new GF2X_c() 

  

cdef int celement_delete(GF2X_c *e, long parent): 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: del x 

""" 

del e 

  

cdef int celement_construct(GF2X_c *e, long parent): 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

""" 

pass 

  

cdef int celement_destruct(GF2X_c *e, long parent): 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: del x 

""" 

pass 

  

cdef int celement_gen(GF2X_c *e, long i, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

""" 

cdef unsigned char g = 2 

GF2XFromBytes(e[0], <unsigned char *>(&g), 1) 

  

cdef object celement_repr(GF2X_c *e, long parent): 

""" 

We ignore NTL's printing. 

  

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x 

x 

""" 

raise NotImplementedError 

  

cdef inline int celement_set(GF2X_c* res, GF2X_c* a, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: y = x; y 

x 

""" 

res[0] = a[0] 

  

cdef inline int celement_set_si(GF2X_c* res, long i, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: P(0) 

0 

sage: P(2) 

0 

sage: P(1) 

1 

""" 

GF2X_conv_long(res[0], i) 

  

cdef inline long celement_get_si(GF2X_c* res, long parent) except -2: 

raise NotImplementedError 

  

cdef inline bint celement_is_zero(GF2X_c* a, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: bool(x), x.is_zero() 

(True, False) 

sage: bool(P(0)), P(0).is_zero() 

(False, True) 

""" 

return GF2X_IsZero(a[0]) 

  

cdef inline bint celement_is_one(GF2X_c *a, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x.is_one() 

False 

sage: P(1).is_one() 

True 

""" 

return GF2X_IsOne(a[0]) 

  

cdef inline bint celement_equal(GF2X_c *a, GF2X_c *b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x == x 

True 

sage: y = x; x == y 

True 

sage: x^2 + 1 == x^2 + x 

False 

""" 

return a[0] == b[0] 

  

cdef inline int celement_cmp(GF2X_c *a, GF2X_c *b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x != 1 

True 

sage: x < 1 

False 

sage: x > 1 

True 

  

sage: f = x^64 + x^20 + 1 

sage: g = x^63 + x^20 + 1 

sage: f > g 

True 

  

sage: f = x^64 + x^10 + 1 

sage: g = x^64 + x^20 + 1 

sage: f < g 

True 

  

sage: f = x^64 + x^20 

sage: g = x^64 + x^20 + 1 

sage: f < g 

True 

""" 

cdef bint t 

cdef long diff 

cdef long ca, cb 

diff = GF2X_NumBits(a[0]) - GF2X_NumBits(b[0]) 

if diff > 0: 

return 1 

elif diff < 0: 

return -1 

else: 

for i in xrange(GF2X_NumBits(a[0])-1, -1, -1): 

ca = GF2_conv_to_long(GF2X_coeff(a[0], i)) 

cb = GF2_conv_to_long(GF2X_coeff(b[0], i)) 

if ca < cb: 

return -1 

elif ca > cb: 

return 1 

return 0 

  

cdef long celement_len(GF2X_c *a, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x.degree() 

1 

sage: (x+1).degree() 

1 

""" 

return int(GF2X_NumBits(a[0])) 

  

cdef inline int celement_add(GF2X_c *res, GF2X_c *a, GF2X_c *b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x + 1 

x + 1 

""" 

GF2X_add(res[0], a[0], b[0]) 

  

cdef inline int celement_sub(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x - 1 

x + 1 

""" 

GF2X_sub(res[0], a[0], b[0]) 

  

cdef inline int celement_neg(GF2X_c* res, GF2X_c* a, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: -x 

x 

""" 

res[0] = a[0] 

  

cdef inline int celement_mul_scalar(GF2X_c* res, GF2X_c* p, object c, 

long parent) except -1: 

""" 

TESTS:: 

  

sage: P.<x> = GF(2)[] 

sage: p = P.random_element() 

sage: 0*p 

0 

sage: 1*p == p 

True 

sage: (3^97)*p == p 

True 

""" 

if int(c) == 0: 

GF2X_conv_long(res[0], 0) 

else: 

res[0] = p[0] 

  

cdef inline int celement_mul(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x*(x+1) 

x^2 + x 

""" 

GF2X_mul(res[0], a[0], b[0]) 

  

cdef inline int celement_div(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

""" 

return GF2X_divide(res[0], a[0], b[0]) 

  

cdef inline int celement_floordiv(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x//(x + 1) 

1 

sage: (x + 1)//x 

1 

""" 

GF2X_div(res[0], a[0], b[0]) 

  

cdef inline int celement_mod(GF2X_c* res, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: (x^2 + 1) % x^2 

1 

""" 

GF2X_rem(res[0], a[0], b[0]) 

  

cdef inline int celement_quorem(GF2X_c* q, GF2X_c* r, GF2X_c* a, GF2X_c* b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: f = x^2 + x + 1 

sage: f.quo_rem(x + 1) 

(x, 1) 

""" 

GF2X_DivRem(q[0], r[0], a[0], b[0]) 

  

cdef inline int celement_inv(GF2X_c* res, GF2X_c* a, long parent) except -2: 

""" 

We ignore NTL here and use the fraction field constructor. 

  

EXAMPLES: 

sage: P.<x> = GF(2)[] 

""" 

raise NotImplementedError 

  

cdef inline int celement_pow(GF2X_c* res, GF2X_c* x, long e, GF2X_c *modulus, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: x^1000 

x^1000 

sage: (x+1)^2 

x^2 + 1 

sage: (x+1)^(-2) 

1/(x^2 + 1) 

sage: f = x^9 + x^7 + x^6 + x^5 + x^4 + x^2 + x 

sage: h = x^10 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + 1 

sage: (f^2) % h 

x^9 + x^8 + x^7 + x^5 + x^3 

sage: pow(f, 2, h) 

x^9 + x^8 + x^7 + x^5 + x^3 

""" 

cdef GF2XModulus_c mod 

  

if modulus == NULL: 

if GF2X_IsX(x[0]): 

GF2X_LeftShift(res[0], x[0], e - 1) 

else: 

do_sig = GF2X_deg(x[0]) > 1e5 

if do_sig: sig_on() 

GF2X_power(res[0], x[0], e) 

if do_sig: sig_off() 

else: 

GF2XModulus_build(mod, modulus[0]) 

  

do_sig = GF2X_deg(x[0]) > 1e5 

if do_sig: sig_on() 

GF2X_PowerMod_long_pre(res[0], x[0], e, mod) 

if do_sig: sig_off() 

  

cdef inline int celement_gcd(GF2X_c* res, GF2X_c* a, GF2X_c *b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: f = x*(x+1) 

sage: f.gcd(x+1) 

x + 1 

sage: f.gcd(x^2) 

x 

""" 

GF2X_GCD(res[0], a[0], b[0]) 

  

cdef inline int celement_xgcd(GF2X_c* res, GF2X_c* s, GF2X_c *t, GF2X_c* a, GF2X_c *b, long parent) except -2: 

""" 

EXAMPLES: 

sage: P.<x> = GF(2)[] 

sage: f = x*(x+1) 

sage: f.xgcd(x+1) 

(x + 1, 0, 1) 

sage: f.xgcd(x^2) 

(x, 1, 1) 

""" 

GF2X_XGCD(res[0], s[0], t[0], a[0], b[0])