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

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

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

# 

# 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.libs.gmp.mpz cimport mpz_get_si 

include 'misc.pxi' 

include 'decl.pxi' 

  

from sage.rings.integer cimport Integer 

  

zz_pContextDict = {} 

  

cdef class ntl_zz_pContext_class(object): 

def __init__(self, long v): 

""" 

EXAMPLES: 

# You can construct contexts manually. 

sage: c = ntl.zz_pContext(11) 

sage: n1 = ntl.zz_p(12,c) 

sage: n1 

1 

  

# or You can construct contexts implicitly. 

sage: n2=ntl.zz_p(12, 7) 

sage: n2 

5 

sage: ntl.zz_p(2,3)+ntl.zz_p(1,3) 

0 

sage: n2+n1 # Mismatched moduli: It will go BOOM! 

Traceback (most recent call last): 

... 

ValueError: arithmetic operands must have the same modulus. 

""" 

pass 

  

def __cinit__(self, long v): 

if v > NTL_SP_BOUND: 

raise ValueError("Modulus (=%s) is too big" % v) 

self.x = zz_pContext_c(v) 

zz_pContextDict[repr(v)] = self 

self.p = v 

  

def __reduce__(self): 

""" 

sage: c=ntl.zz_pContext(13) 

sage: loads(dumps(c)) is c 

True 

""" 

return ntl_zz_pContext, (self.p,) 

  

def modulus(self): 

""" 

Print the modulus for self. 

  

EXAMPLES: 

sage: c1 = ntl.zz_pContext(36) 

sage: c1.modulus() 

36 

""" 

return self.p 

  

def restore(self): 

""" 

Restore a zz_pContext. 

  

EXAMPLES: 

sage: c = ntl.zz_pContext(5) 

sage: m = ntl.zz_p(4,7) 

sage: c.restore() 

""" 

self.restore_c() 

  

cdef void restore_c(self): 

""" 

Actual code for the above. 

  

EXAMPLES: 

sage: n = ntl.zz_p(3,5) 

sage: m = ntl.zz_p(4,7) 

sage: n*n ## indirect doctest 

4 

""" 

self.x.restore() 

  

  

def ntl_zz_pContext( v ): 

""" 

Creation function for a zz_p context. 

  

EXAMPLES: 

sage: f = ntl.zz_pContext(26) 

sage: f = ntl.zz_pContext(10^100) 

Traceback (most recent call last): 

... 

ValueError: Modulus (=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) is too big 

""" 

if v > NTL_SP_BOUND: 

raise ValueError("Modulus (=%s) is too big" % v) 

if isinstance(v, Integer): 

v = mpz_get_si((<Integer>v).value) 

try: 

return zz_pContextDict[repr(v)] 

except KeyError: 

return ntl_zz_pContext_class(v)