Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_GF2EContext.pyx
4095 views
1
#*****************************************************************************
2
# Copyright (C) 2005 William Stein <[email protected]>
3
#
4
# Distributed under the terms of the GNU General Public License (GPL)
5
#
6
# This code is distributed in the hope that it will be useful,
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
# General Public License for more details.
10
#
11
# The full text of the GPL is available at:
12
#
13
# http://www.gnu.org/licenses/
14
#*****************************************************************************
15
16
include "../../ext/interrupt.pxi"
17
include "../../ext/stdsage.pxi"
18
include 'misc.pxi'
19
include 'decl.pxi'
20
import weakref
21
22
GF2EContextDict = {}
23
24
25
cdef class ntl_GF2EContext_class:
26
def __init__(self, ntl_GF2X v):
27
"""
28
EXAMPLES:
29
# You can construct contexts manually.
30
sage: ctx = ntl.GF2EContext(ntl.GF2X([1,1,0,1]))
31
sage: n1 = ntl.GF2E([1,1],ctx)
32
sage: n1
33
[1 1]
34
35
# or You can construct contexts implicitly.
36
sage: n2 = ntl.GF2E([0,1], ntl.GF2X([1,1,0,1]))
37
sage: n2
38
[0 1]
39
sage: ntl.GF2E(2, GF(2^8,'a'))+ntl.GF2E([0,1],ctx)
40
Traceback (most recent call last):
41
...
42
ValueError: You can not perform arithmetic with elements in different fields.
43
44
sage: n2+n1 # Mismatched moduli: It will go BOOM!
45
[1]
46
"""
47
pass
48
49
def __cinit__(self, ntl_GF2X v):
50
GF2EContext_construct_GF2X(&self.x, &((<ntl_GF2X>v).x))
51
self.m = v
52
53
def __dealloc__(self):
54
GF2EContext_destruct(&self.x)
55
56
def __reduce__(self):
57
"""
58
EXAMPLES:
59
sage: c = ntl.GF2EContext(GF(2^5,'b'))
60
sage: loads(dumps(c)) is c
61
True
62
"""
63
return ntl_GF2EContext, (self.m,)
64
65
def __repr__(self):
66
"""
67
Returns a print representation of self.
68
69
EXAMPLES:
70
sage: c = ntl.GF2EContext(GF(2^16,'a'))
71
sage: c
72
NTL modulus [1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1]
73
"""
74
return "NTL modulus %s"%(self.m)
75
76
def modulus(self):
77
"""
78
Return the current modulus associated to this
79
context.
80
81
EXAMPLES:
82
sage: c = ntl.GF2EContext(GF(2^7,'foo'))
83
sage: c.modulus()
84
[1 1 0 0 0 0 0 1]
85
"""
86
return self.m
87
88
89
def restore(self):
90
"""
91
EXAMPLES:
92
sage: c1 = ntl.GF2E([0,1],GF(2^4,'a')) ; c2 = ntl.GF2E([1,0,1],GF(2^4,'a'))
93
sage: c1+c2
94
[1 1 1]
95
sage: d1 = ntl.GF2E([0,1],GF(2^5,'a')) ; d2 = ntl.GF2E([0,0,1],GF(2^5,'a'))
96
sage: d1*d2 ## indirect doctest
97
[0 0 0 1]
98
"""
99
self.restore_c()
100
101
cdef void restore_c(self):
102
self.x.restore()
103
104
def ntl_GF2EContext( v ):
105
"""
106
Create a new GF2EContext.
107
EXAMPLES:
108
sage: c = ntl.GF2EContext(GF(2^2,'a'))
109
sage: n1 = ntl.GF2E([0,1],c)
110
sage: n1
111
[0 1]
112
"""
113
v = ntl_GF2X(v)
114
if (GF2X_deg((<ntl_GF2X>v).x) < 1):
115
raise ValueError, "%s is not a valid modulus."%v
116
key = hash(v)
117
if GF2EContextDict.has_key(key):
118
context = GF2EContextDict[key]()
119
if context is not None:
120
return context
121
context = ntl_GF2EContext_class(v)
122
GF2EContextDict[key] = weakref.ref(context)
123
return context
124
125