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

""" 

Finite Fields of Characteristic 2 

""" 

 

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

# Copyright (C) 2011 David Roe 

# Copyright (C) 2012 Travis Scrimshaw 

# Copyright (C) 2013 Peter Bruin 

# Copyright (C) 2014 Jeroen Demeyer 

# 

# 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 sage.rings.finite_rings.finite_field_base import FiniteField 

from sage.libs.pari.all import pari 

from sage.rings.integer_ring import ZZ 

from sage.rings.integer import Integer 

 

def late_import(): 

""" 

Imports various modules after startup. 

 

EXAMPLES:: 

 

sage: sage.rings.finite_rings.finite_field_ntl_gf2e.late_import() 

sage: sage.rings.finite_rings.finite_field_ntl_gf2e.GF2 is None # indirect doctest 

False 

""" 

if "GF2" in globals(): 

return 

global is_FiniteField, exists_conway_polynomial, conway_polynomial, Cache_ntl_gf2e, GF, GF2, is_Polynomial 

 

import sage.rings.finite_rings.finite_field_base 

is_FiniteField = sage.rings.finite_rings.finite_field_base.is_FiniteField 

 

import sage.rings.finite_rings.conway_polynomials 

exists_conway_polynomial = sage.rings.finite_rings.conway_polynomials.exists_conway_polynomial 

conway_polynomial = sage.rings.finite_rings.conway_polynomials.conway_polynomial 

 

import sage.rings.finite_rings.element_ntl_gf2e 

Cache_ntl_gf2e = sage.rings.finite_rings.element_ntl_gf2e.Cache_ntl_gf2e 

 

import sage.rings.finite_rings.finite_field_constructor 

GF = sage.rings.finite_rings.finite_field_constructor.GF 

GF2 = GF(2) 

 

import sage.rings.polynomial.polynomial_element 

is_Polynomial = sage.rings.polynomial.polynomial_element.is_Polynomial 

 

class FiniteField_ntl_gf2e(FiniteField): 

""" 

Finite Field of characteristic 2 and order `2^n`. 

 

INPUT: 

 

- ``q`` -- `2^n` (must be 2 power) 

 

- ``names`` -- variable used for poly_repr (default: ``'a'``) 

 

- ``modulus`` -- A minimal polynomial to use for reduction. 

 

- ``repr`` -- controls the way elements are printed to the user: 

(default: ``'poly'``) 

 

- ``'poly'``: polynomial representation 

 

OUTPUT: 

 

Finite field with characteristic 2 and cardinality `2^n`. 

 

EXAMPLES:: 

 

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

sage: type(k) 

<class 'sage.rings.finite_rings.finite_field_ntl_gf2e.FiniteField_ntl_gf2e_with_category'> 

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

sage: k.modulus() 

x^1024 + x^19 + x^6 + x + 1 

sage: set_random_seed(6397) 

sage: k.<a> = GF(2^17, modulus='random') 

sage: k.modulus() 

x^17 + x^16 + x^15 + x^10 + x^8 + x^6 + x^4 + x^3 + x^2 + x + 1 

sage: k.modulus().is_irreducible() 

True 

sage: k.<a> = GF(2^211, modulus='minimal_weight') 

sage: k.modulus() 

x^211 + x^11 + x^10 + x^8 + 1 

sage: k.<a> = GF(2^211, modulus='conway') 

sage: k.modulus() 

x^211 + x^9 + x^6 + x^5 + x^3 + x + 1 

sage: k.<a> = GF(2^23, modulus='conway') 

sage: a.multiplicative_order() == k.order() - 1 

True 

""" 

 

def __init__(self, q, names="a", modulus=None, repr="poly"): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: k.<a> = GF(2^100, modulus='strangeinput') 

Traceback (most recent call last): 

... 

ValueError: no such algorithm for finding an irreducible polynomial: strangeinput 

sage: k.<a> = GF(2^20) ; type(k) 

<class 'sage.rings.finite_rings.finite_field_ntl_gf2e.FiniteField_ntl_gf2e_with_category'> 

sage: loads(dumps(k)) is k 

True 

sage: k1.<a> = GF(2^16) 

sage: k2.<a> = GF(2^17) 

sage: k1 == k2 

False 

sage: k3.<a> = GF(2^16, impl="pari_ffelt") 

sage: k1 == k3 

False 

 

sage: TestSuite(k).run() 

 

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

sage: k._repr_option('element_is_atomic') 

False 

sage: P.<x> = PolynomialRing(k) 

sage: (a+1)*x # indirect doctest 

(a + 1)*x 

""" 

late_import() 

q = Integer(q) 

if q < 2: 

raise ValueError("q must be a 2-power") 

k = q.exact_log(2) 

if q != 1 << k: 

raise ValueError("q must be a 2-power") 

FiniteField.__init__(self, GF2, names, normalize=True) 

 

from sage.rings.polynomial.polynomial_element import is_Polynomial 

if not is_Polynomial(modulus): 

raise TypeError("modulus must be a polynomial") 

 

self._cache = Cache_ntl_gf2e(self, k, modulus) 

self._modulus = modulus 

 

def characteristic(self): 

""" 

Return the characteristic of ``self`` which is 2. 

 

EXAMPLES:: 

 

sage: k.<a> = GF(2^16,modulus='random') 

sage: k.characteristic() 

2 

""" 

return Integer(2) 

 

def order(self): 

""" 

Return the cardinality of this field. 

 

EXAMPLES:: 

 

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

sage: k.order() 

18446744073709551616 

""" 

return self._cache.order() 

 

def degree(self): 

r""" 

If this field has cardinality `2^n` this method returns `n`. 

 

EXAMPLES:: 

 

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

sage: k.degree() 

64 

""" 

return self._cache.degree() 

 

def _element_constructor_(self, e): 

""" 

Coerces several data types to ``self``. 

 

INPUT: 

 

- ``e`` -- data to coerce 

 

EXAMPLES:: 

 

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

sage: k(1) # indirect doctest 

1 

sage: k(int(2)) 

0 

 

sage: k('a+1') 

a + 1 

sage: k('b+1') 

Traceback (most recent call last): 

... 

NameError: name 'b' is not defined 

 

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

sage: k(1+x+x^10+x^55) 

a^19 + a^17 + a^16 + a^15 + a^12 + a^11 + a^8 + a^6 + a^4 + a^2 + 1 

 

sage: V = k.vector_space() 

sage: v = V.random_element(); v 

(1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1) 

sage: k(v) 

a^19 + a^15 + a^14 + a^13 + a^11 + a^10 + a^9 + a^6 + a^5 + a^4 + 1 

sage: vector(k(v)) == v 

True 

 

sage: k(pari('Mod(1,2)*a^20')) 

a^10 + a^9 + a^7 + a^6 + a^5 + a^4 + a + 1 

""" 

return self._cache.import_data(e) 

 

def gen(self, n=0): 

r""" 

Return a generator of ``self`` over its prime field, which is a 

root of ``self.modulus()``. 

 

INPUT: 

 

- ``n`` -- must be 0 

 

OUTPUT: 

 

An element `a` of ``self`` such that ``self.modulus()(a) == 0``. 

 

.. WARNING:: 

 

This generator is not guaranteed to be a generator for the 

multiplicative group. To obtain the latter, use 

:meth:`~sage.rings.finite_rings.finite_field_base.FiniteFields.multiplicative_generator()` 

or use the ``modulus="primitive"`` option when constructing 

the field. 

 

EXAMPLES:: 

 

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

sage: k.gen() == a 

True 

sage: a 

a 

 

TESTS:: 

 

sage: GF(2, impl='ntl').gen() 

1 

sage: GF(2, impl='ntl', modulus=polygen(GF(2)) ).gen() 

0 

sage: GF(2^19, 'a').gen(1) 

Traceback (most recent call last): 

... 

IndexError: only one generator 

""" 

if n: 

raise IndexError("only one generator") 

return self._cache._gen 

 

def prime_subfield(self): 

r""" 

Return the prime subfield `\GF{p}` of ``self`` if ``self`` is 

`\GF{p^n}`. 

 

EXAMPLES:: 

 

sage: F.<a> = GF(2^16) 

sage: F.prime_subfield() 

Finite Field of size 2 

""" 

return GF2 

 

def fetch_int(self, number): 

r""" 

Given an integer `n` less than :meth:`cardinality` with base `2` 

representation `a_0 + 2 \cdot a_1 + \cdots + 2^k a_k`, returns 

`a_0 + a_1 \cdot x + \cdots + a_k x^k`, where `x` is the 

generator of this finite field. 

 

INPUT: 

 

- ``number`` -- an integer 

 

EXAMPLES:: 

 

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

sage: k.fetch_int(2^43 + 2^15 + 1) 

a^43 + a^15 + 1 

sage: k.fetch_int(33793) 

a^15 + a^10 + 1 

sage: 33793.digits(2) # little endian 

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

""" 

return self._cache.fetch_int(number) 

 

def _pari_modulus(self): 

""" 

Return PARI object which is equivalent to the 

polynomial/modulus of ``self``. 

 

EXAMPLES:: 

 

sage: k1.<a> = GF(2^16) 

sage: k1._pari_modulus() 

Mod(1, 2)*a^16 + Mod(1, 2)*a^5 + Mod(1, 2)*a^3 + Mod(1, 2)*a^2 + Mod(1, 2) 

""" 

f = pari(str(self.modulus())) 

return f.subst('x', 'a') * pari("Mod(1,%s)"%self.characteristic())