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

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

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

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

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

  

from sage.ext.cplusplus cimport ccrepr 

  

include 'misc.pxi' 

include 'decl.pxi' 

  

from cpython.object cimport Py_EQ, Py_NE 

from .ntl_ZZ cimport ntl_ZZ 

from .ntl_GF2 cimport ntl_GF2 

from .ntl_GF2X cimport ntl_GF2X 

from .ntl_GF2EContext cimport ntl_GF2EContext_class 

from .ntl_GF2EContext import ntl_GF2EContext 

from sage.libs.ntl.ntl_ZZ import unpickle_class_args 

from sage.misc.randstate cimport randstate, current_randstate 

  

  

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

# 

# ntl_GF2E: GF(2**x) via NTL 

# 

# AUTHORS: 

# - Martin Albrecht <malb@informatik.uni-bremen.de> 

# 2006-01: initial version (based on code by William Stein) 

# - Martin Albrecht <malb@informatik.uni-bremen.de> 

# 2007-10: adapted to new conventions 

# 

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

  

def ntl_GF2E_random(ntl_GF2EContext_class ctx): 

""" 

Returns a random element from GF2E modulo the current modulus. 

  

INPUT: 

  

- ``ctx`` -- the GF2E context for which an random element should be created 

  

EXAMPLES:: 

  

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

sage: ntl.GF2E_random(ctx) 

[1 1 0 0 1 0 1 1] 

""" 

current_randstate().set_seed_ntl(False) 

  

cdef ntl_GF2E r 

ctx.restore_c() 

r = ntl_GF2E.__new__(ntl_GF2E) 

r.c = ctx 

r.x = GF2E_random() 

return r 

  

cdef class ntl_GF2E(object): 

r""" 

The \\class{GF2E} represents a finite extension field over GF(2) 

using NTL. Elements are represented as polynomials over GF(2) 

modulo a modulus. 

  

This modulus must be set by creating a GF2EContext first and pass 

that context to the constructor of all elements. 

""" 

  

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

""" 

Constructs a new finite field element in GF(2**x). 

  

If you pass a string to the constructor please note that byte 

sequences and the hexadecimal notation are Little Endian in 

NTL. So e.g. '[0 1]' == '0x2' == x. 

  

INPUT: 

x -- value to be assigned to this element. Same types as 

ntl.GF2X() are accepted. 

modulus -- the context/modulus of the field 

  

OUTPUT: 

a new ntl.GF2E element 

  

EXAMPLES: 

sage: k.<a> = GF(2^8) 

sage: e = ntl.GF2E(a,k); e 

[0 1] 

sage: ctx = e.modulus_context() 

sage: ntl.GF2E('0x1c', ctx) 

[1 0 0 0 0 0 1 1] 

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

[1 0 1] 

sage: ntl.GF2E([1,0,1,0], ctx) 

[1 0 1] 

sage: ntl.GF2E(ntl.GF2(1),ctx) 

[1] 

""" 

if modulus is None: 

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

  

cdef ntl_GF2X _x 

  

if isinstance(x, ntl_GF2X): 

GF2E_conv_GF2X(self.x, (<ntl_GF2X>x).x) 

  

elif isinstance(x, int): 

GF2E_conv_long(self.x, x) 

  

elif isinstance(x, ntl_ZZ): 

GF2E_conv_ZZ(self.x, (<ntl_ZZ>x).x) 

  

elif isinstance(x, ntl_GF2): 

GF2E_conv_GF2(self.x, (<ntl_GF2>x).x) 

else: 

_x = ntl_GF2X(x) 

GF2E_conv_GF2X(self.x, _x.x) 

  

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

#################### 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(self): 

cdef ntl_GF2E r 

self.c.restore_c() 

r = ntl_GF2E.__new__(ntl_GF2E) 

r.c = self.c 

return r 

  

def __reduce__(self): 

""" 

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

sage: a = ntl.GF2E(ntl.ZZ_pX([1,1,3],2), ctx) 

sage: loads(dumps(a)) == a 

True 

""" 

return unpickle_class_args, (ntl_GF2E, (str(self), self.modulus_context())) 

  

def modulus_context(self): 

""" 

Returns the structure that holds the underlying NTL GF2E modulus. 

  

EXAMPLES: 

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

sage: a = ntl.GF2E(ntl.ZZ_pX([1,1,3],2), ctx) 

sage: cty = a.modulus_context(); cty 

NTL modulus [1 1 0 1 1 0 0 0 1] 

sage: ctx == cty 

True 

""" 

return self.c 

  

def __repr__(self): 

""" 

Return the string representation of self. 

  

EXAMPLES: 

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

sage: ntl.GF2E([1,1,0,1], ctx) # indirect doctest 

[1 1 0 1] 

""" 

self.c.restore_c() 

return ccrepr(self.x) 

  

def __copy__(self): 

""" 

Return a copy of self. 

  

EXAMPLES: 

sage: x = ntl.GF2E([0,1,1],GF(2^4,'a')) 

sage: y = copy(x) 

sage: x == y 

True 

sage: x is y 

False 

""" 

cdef ntl_GF2E r = self._new() 

r.x = self.x 

return r 

  

def __mul__(ntl_GF2E self, other): 

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1], ctx) 

sage: x*y ## indirect doctest 

[0 0 1 1 1 0 1 1] 

""" 

cdef ntl_GF2E r 

if not isinstance(other, ntl_GF2E): 

other = ntl_GF2E(other,self.c) 

elif self.c is not (<ntl_GF2E>other).c: 

raise ValueError("You can not perform arithmetic with elements in different fields.") 

r = self._new() 

GF2E_mul(r.x, self.x, (<ntl_GF2E>other).x) 

return r 

  

def __sub__(ntl_GF2E self, other): 

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1], ctx) 

sage: x - y ## indirect doctest 

[0 1 1 1] 

""" 

cdef ntl_GF2E r 

if not isinstance(other, ntl_GF2E): 

other = ntl_GF2E(other,self.c) 

elif self.c is not (<ntl_GF2E>other).c: 

raise ValueError("You can not perform arithmetic with elements in different fields.") 

r = self._new() 

GF2E_sub(r.x, self.x, (<ntl_GF2E>other).x) 

return r 

  

def __add__(ntl_GF2E self, other): 

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1], ctx) 

sage: x+y ## indirect doctest 

[0 1 1 1] 

""" 

cdef ntl_GF2E r 

if not isinstance(other, ntl_GF2E): 

other = ntl_GF2E(other,self.c) 

elif self.c is not (<ntl_GF2E>other).c: 

raise ValueError("You can not perform arithmetic with elements in different fields.") 

r = self._new() 

GF2E_add(r.x, self.x, (<ntl_GF2E>other).x) 

return r 

  

def __truediv__(ntl_GF2E self, other): 

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1], ctx) 

sage: x/y ## indirect doctest 

[1 0 1 0 0 1 0 1] 

""" 

cdef ntl_GF2E r 

if not isinstance(other, ntl_GF2E): 

other = ntl_GF2E(other,self.c) 

elif self.c is not (<ntl_GF2E>other).c: 

raise ValueError("You can not perform arithmetic with elements in different fields.") 

r = self._new() 

GF2E_div(r.x, self.x, (<ntl_GF2E>other).x) 

return r 

  

def __div__(self, other): 

return self / other 

  

def __neg__(ntl_GF2E self): 

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) 

sage: -x ## indirect doctest 

[1 0 1 0 1] 

""" 

cdef ntl_GF2E r = self._new() 

r.x = self.x 

return r 

  

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

""" 

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) 

sage: x**2 ## indirect doctest 

[0 1 0 1] 

""" 

cdef ntl_GF2E r = self._new() 

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

return r 

  

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

r""" 

Compare self to other. 

  

EXAMPLES:: 

  

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1], ctx) 

sage: x == x 

True 

sage: x == y 

False 

sage: ntl.GF2E(0,ctx) == 0 

True 

sage: a = ntl.GF2E([0,1],GF(2^2,'a')) 

sage: a == x 

False 

""" 

self.c.restore_c() 

  

if op != Py_EQ and op != Py_NE: 

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

  

cdef ntl_GF2E b 

try: 

b = <ntl_GF2E?>other 

except TypeError: 

b = ntl_GF2E(other, self.c) 

  

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

  

def IsZero(ntl_GF2E self): 

""" 

Returns True if this element equals zero, False otherwise. 

  

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([1,1,0,1,1,0,0,0,1], ctx) 

sage: x.IsZero() 

False 

sage: y.IsZero() 

True 

""" 

return bool(GF2E_IsZero(self.x)) 

  

def IsOne(ntl_GF2E self): 

""" 

Returns True if this element equals one, False otherwise. 

  

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([0,1,0,1,1,0,0,0,1], ctx) 

sage: x.IsOne() 

False 

sage: y.IsOne() 

True 

""" 

return bool(GF2E_IsOne(self.x)) 

  

def trace(ntl_GF2E self): 

""" 

Returns the trace of this element. 

  

EXAMPLES: 

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

sage: x = ntl.GF2E([1,0,1,0,1], ctx) ; y = ntl.GF2E([0,1,1,0,1,1], ctx) 

sage: x.trace() 

0 

sage: y.trace() 

1 

""" 

cdef ntl_GF2 x = ntl_GF2.__new__(ntl_GF2) 

x.x = GF2E_trace(self.x) 

return x 

  

def rep(ntl_GF2E self): 

""" 

Returns a ntl.GF2X copy of this element. 

  

EXAMPLES: 

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

sage: a = ntl.GF2E('0x1c', ctx) 

sage: a.rep() 

[1 0 0 0 0 0 1 1] 

sage: type(a.rep()) 

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

""" 

cdef ntl_GF2X x = ntl_GF2X.__new__(ntl_GF2X) 

x.x = GF2E_rep(self.x) 

return x 

  

def list(ntl_GF2E self): 

""" 

Represents this element as a list of binary digits. 

  

EXAMPLES: 

sage: e=ntl.GF2E([0,1,1],GF(2^4,'a')) 

sage: e.list() 

[0, 1, 1] 

sage: e=ntl.GF2E('0xff',GF(2^8,'a')) 

sage: e.list() 

[1, 1, 1, 1, 1, 1, 1, 1] 

  

OUTPUT: 

a list of digits representing the coefficients in this element's 

polynomial representation 

""" 

cdef int i 

cdef GF2X_c x = GF2E_rep(self.x) 

cdef ntl_GF2 b 

  

l = [] 

  

for i from 0 <= i <= GF2X_deg(x): 

b = ntl_GF2.__new__(ntl_GF2) 

b.x = GF2X_coeff(x,i) 

l.append(b) 

return l 

  

def _sage_(ntl_GF2E self, k=None): 

""" 

Returns a \class{FiniteFieldElement} representation 

of this element. If a \class{FiniteField} k is provided 

it is constructed in this field if possible. A \class{FiniteField} 

will be constructed if none is provided. 

  

INPUT: 

k -- optional GF(2**deg) 

  

OUTPUT: 

FiniteFieldElement over k 

  

EXAMPLES: 

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

sage: e = ntl.GF2E([0,1], ctx) 

sage: a = e._sage_(); a 

a 

""" 

cdef int i 

cdef int length 

  

self.c.restore_c() 

  

e = GF2E_degree() 

  

if k is None: 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

f = self.c.m._sage_() 

k = FiniteField(2**e, name='a', modulus=f) 

  

a=k.gen() 

l = self.list() 

  

length = len(l) 

ret = 0 

  

for i from 0 <= i < length: 

if l[i]==1: 

ret = ret + a**i 

  

return ret