Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/libs/ntl/ntl_ZZ_pContext.pyx
4107 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
from sage.rings.integer_ring import IntegerRing
23
24
ZZ_sage = IntegerRing()
25
26
27
cdef class ntl_ZZ_pContext_class:
28
def __init__(self, ntl_ZZ v):
29
"""
30
EXAMPLES:
31
# You can construct contexts manually.
32
sage: c = ntl.ZZ_pContext(11)
33
sage: n1 = ntl.ZZ_p(12,c)
34
sage: n1
35
1
36
37
# or You can construct contexts implicitly.
38
sage: n2 = ntl.ZZ_p(12, 7)
39
sage: n2
40
5
41
sage: ntl.ZZ_p(2,3)+ntl.ZZ_p(1,3)
42
0
43
sage: n2+n1 # Mismatched moduli: It will go BOOM!
44
Traceback (most recent call last):
45
...
46
ValueError: You can not perform arithmetic with elements of different moduli.
47
"""
48
pass
49
50
def __cinit__(self, ntl_ZZ v):
51
ZZ_pContext_construct_ZZ(&self.x, &(<ntl_ZZ>v).x)
52
self.p = v
53
self.p_bits = self.p._integer_().nbits()
54
55
def __dealloc__(self):
56
ZZ_pContext_destruct(&self.x)
57
58
def __reduce__(self):
59
"""
60
EXAMPLES:
61
sage: c = ntl.ZZ_pContext(13)
62
sage: loads(dumps(c)) is c
63
True
64
"""
65
return ntl_ZZ_pContext, (self.p,)
66
67
def __repr__(self):
68
"""
69
Returns a print representation of self.
70
71
EXAMPLES:
72
sage: c = ntl.ZZ_pContext(7)
73
sage: c
74
NTL modulus 7
75
"""
76
return "NTL modulus %s"%(self.p)
77
78
def __hash__(self):
79
return hash(self.p)
80
81
def modulus(self):
82
"""
83
Return the current modulus associated to this
84
context.
85
86
EXAMPLES:
87
sage: c = ntl.ZZ_pContext(7)
88
sage: c.modulus()
89
7
90
91
sage: c = ntl.ZZ_pContext(10^30)
92
sage: type(c.modulus())
93
<type 'sage.rings.integer.Integer'>
94
sage: c.modulus() == 10^30
95
True
96
"""
97
return ZZ_sage(self.p)
98
99
100
def restore(self):
101
"""
102
EXAMPLES:
103
sage: c1 = ntl.ZZ_p(5,92) ; c2 = ntl.ZZ_p(7,92)
104
sage: c1+c2
105
12
106
sage: d1 = ntl.ZZ_p(38,91) ; d2 = ntl.ZZ_p(3,91)
107
sage: d1*d2 ## indirect doctest
108
23
109
"""
110
self.restore_c()
111
112
cdef void restore_c(self):
113
self.x.restore()
114
115
cdef class ntl_ZZ_pContext_factory:
116
def __init__(self):
117
self.context_dict = {}
118
119
cdef ntl_ZZ_pContext_class make_c(self, ntl_ZZ v):
120
"""
121
Creates a new ZZ_pContext.
122
123
INPUT:
124
v -- an ntl_ZZ
125
"""
126
cdef ntl_ZZ_pContext_class context
127
if self.context_dict.has_key(v):
128
context = <ntl_ZZ_pContext_class> self.context_dict[v]()
129
if context is not None:
130
return context
131
context = ntl_ZZ_pContext_class(v)
132
self.context_dict[v] = weakref.ref(context)
133
return context
134
135
ZZ_pContext_factory = ntl_ZZ_pContext_factory()
136
137
def ntl_ZZ_pContext( v ):
138
"""
139
Create a new ZZ_pContext.
140
EXAMPLES:
141
sage: c = ntl.ZZ_pContext(178)
142
sage: n1 = ntl.ZZ_p(212,c)
143
sage: n1
144
34
145
"""
146
v = ntl_ZZ(v)
147
if (v < ntl_ZZ(2)):
148
raise ValueError, "%s is not a valid modulus."%v
149
return (<ntl_ZZ_pContext_factory>ZZ_pContext_factory).make_c(v)
150
151