Path: blob/master/src/sage/libs/ntl/ntl_ZZ_pContext.pyx
8815 views
#*****************************************************************************1# Copyright (C) 2005 William Stein <[email protected]>2#3# Distributed under the terms of the GNU General Public License (GPL)4#5# This code is distributed in the hope that it will be useful,6# but WITHOUT ANY WARRANTY; without even the implied warranty of7# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU8# General Public License for more details.9#10# The full text of the GPL is available at:11#12# http://www.gnu.org/licenses/13#*****************************************************************************1415include "sage/ext/interrupt.pxi"16include "sage/ext/stdsage.pxi"17include 'misc.pxi'18include 'decl.pxi'19import weakref2021from sage.rings.integer_ring import IntegerRing2223ZZ_sage = IntegerRing()242526cdef class ntl_ZZ_pContext_class:27def __init__(self, ntl_ZZ v):28"""29EXAMPLES:30# You can construct contexts manually.31sage: c = ntl.ZZ_pContext(11)32sage: n1 = ntl.ZZ_p(12,c)33sage: n13413536# or You can construct contexts implicitly.37sage: n2 = ntl.ZZ_p(12, 7)38sage: n239540sage: ntl.ZZ_p(2,3)+ntl.ZZ_p(1,3)41042sage: n2+n1 # Mismatched moduli: It will go BOOM!43Traceback (most recent call last):44...45ValueError: You can not perform arithmetic with elements of different moduli.46"""47pass4849def __cinit__(self, ntl_ZZ v):50ZZ_pContext_construct_ZZ(&self.x, &(<ntl_ZZ>v).x)51self.p = v52self.p_bits = self.p._integer_().nbits()5354def __dealloc__(self):55ZZ_pContext_destruct(&self.x)5657def __reduce__(self):58"""59EXAMPLES:60sage: c = ntl.ZZ_pContext(13)61sage: loads(dumps(c)) is c62True63"""64return ntl_ZZ_pContext, (self.p,)6566def __repr__(self):67"""68Returns a print representation of self.6970EXAMPLES:71sage: c = ntl.ZZ_pContext(7)72sage: c73NTL modulus 774"""75return "NTL modulus %s"%(self.p)7677def __hash__(self):78return hash(self.p)7980def modulus(self):81"""82Return the current modulus associated to this83context.8485EXAMPLES:86sage: c = ntl.ZZ_pContext(7)87sage: c.modulus()8878990sage: c = ntl.ZZ_pContext(10^30)91sage: type(c.modulus())92<type 'sage.rings.integer.Integer'>93sage: c.modulus() == 10^3094True95"""96return ZZ_sage(self.p)979899def restore(self):100"""101EXAMPLES:102sage: c1 = ntl.ZZ_p(5,92) ; c2 = ntl.ZZ_p(7,92)103sage: c1+c210412105sage: d1 = ntl.ZZ_p(38,91) ; d2 = ntl.ZZ_p(3,91)106sage: d1*d2 ## indirect doctest10723108"""109self.restore_c()110111cdef void restore_c(self):112self.x.restore()113114cdef class ntl_ZZ_pContext_factory:115def __init__(self):116self.context_dict = {}117118cdef ntl_ZZ_pContext_class make_c(self, ntl_ZZ v):119"""120Creates a new ZZ_pContext.121122INPUT:123v -- an ntl_ZZ124"""125cdef ntl_ZZ_pContext_class context126if self.context_dict.has_key(v):127context = <ntl_ZZ_pContext_class> self.context_dict[v]()128if context is not None:129return context130context = ntl_ZZ_pContext_class(v)131self.context_dict[v] = weakref.ref(context)132return context133134ZZ_pContext_factory = ntl_ZZ_pContext_factory()135136def ntl_ZZ_pContext( v ):137"""138Create a new ZZ_pContext.139EXAMPLES:140sage: c = ntl.ZZ_pContext(178)141sage: n1 = ntl.ZZ_p(212,c)142sage: n114334144"""145v = ntl_ZZ(v)146if (v < ntl_ZZ(2)):147raise ValueError, "%s is not a valid modulus."%v148return (<ntl_ZZ_pContext_factory>ZZ_pContext_factory).make_c(v)149150151