1 | |
---|
2 | |
---|
3 | #include <NTL/lzz_pE.h> |
---|
4 | |
---|
5 | #include <NTL/new.h> |
---|
6 | |
---|
7 | NTL_START_IMPL |
---|
8 | |
---|
9 | zz_pEInfoT::zz_pEInfoT(const zz_pX& NewP) |
---|
10 | { |
---|
11 | ref_count = 1; |
---|
12 | |
---|
13 | build(p, NewP); |
---|
14 | |
---|
15 | _card_init = 0; |
---|
16 | _card_base = zz_p::modulus(); |
---|
17 | _card_exp = deg(NewP); |
---|
18 | } |
---|
19 | |
---|
20 | const ZZ& zz_pE::cardinality() |
---|
21 | { |
---|
22 | if (!zz_pEInfo) Error("zz_pE::cardinality: undefined modulus"); |
---|
23 | |
---|
24 | if (!zz_pEInfo->_card_init) { |
---|
25 | power(zz_pEInfo->_card, zz_pEInfo->_card_base, zz_pEInfo->_card_exp); |
---|
26 | zz_pEInfo->_card_init = 1; |
---|
27 | } |
---|
28 | |
---|
29 | return zz_pEInfo->_card; |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | zz_pEInfoT *zz_pEInfo = 0; |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | typedef zz_pEInfoT *zz_pEInfoPtr; |
---|
41 | |
---|
42 | |
---|
43 | static |
---|
44 | void CopyPointer(zz_pEInfoPtr& dst, zz_pEInfoPtr src) |
---|
45 | { |
---|
46 | if (src == dst) return; |
---|
47 | |
---|
48 | if (dst) { |
---|
49 | dst->ref_count--; |
---|
50 | |
---|
51 | if (dst->ref_count < 0) |
---|
52 | Error("internal error: negative zz_pEContext ref_count"); |
---|
53 | |
---|
54 | if (dst->ref_count == 0) delete dst; |
---|
55 | } |
---|
56 | |
---|
57 | if (src) { |
---|
58 | if (src->ref_count == NTL_MAX_LONG) |
---|
59 | Error("internal error: zz_pEContext ref_count overflow"); |
---|
60 | |
---|
61 | src->ref_count++; |
---|
62 | } |
---|
63 | |
---|
64 | dst = src; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | void zz_pE::init(const zz_pX& p) |
---|
71 | { |
---|
72 | zz_pEContext c(p); |
---|
73 | c.restore(); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | zz_pEContext::zz_pEContext(const zz_pX& p) |
---|
78 | { |
---|
79 | ptr = NTL_NEW_OP zz_pEInfoT(p); |
---|
80 | } |
---|
81 | |
---|
82 | zz_pEContext::zz_pEContext(const zz_pEContext& a) |
---|
83 | { |
---|
84 | ptr = 0; |
---|
85 | CopyPointer(ptr, a.ptr); |
---|
86 | } |
---|
87 | |
---|
88 | zz_pEContext& zz_pEContext::operator=(const zz_pEContext& a) |
---|
89 | { |
---|
90 | CopyPointer(ptr, a.ptr); |
---|
91 | return *this; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | zz_pEContext::~zz_pEContext() |
---|
96 | { |
---|
97 | CopyPointer(ptr, 0); |
---|
98 | } |
---|
99 | |
---|
100 | void zz_pEContext::save() |
---|
101 | { |
---|
102 | CopyPointer(ptr, zz_pEInfo); |
---|
103 | } |
---|
104 | |
---|
105 | void zz_pEContext::restore() const |
---|
106 | { |
---|
107 | CopyPointer(zz_pEInfo, ptr); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | zz_pEBak::~zz_pEBak() |
---|
113 | { |
---|
114 | if (MustRestore) |
---|
115 | CopyPointer(zz_pEInfo, ptr); |
---|
116 | |
---|
117 | CopyPointer(ptr, 0); |
---|
118 | } |
---|
119 | |
---|
120 | void zz_pEBak::save() |
---|
121 | { |
---|
122 | MustRestore = 1; |
---|
123 | CopyPointer(ptr, zz_pEInfo); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | void zz_pEBak::restore() |
---|
129 | { |
---|
130 | MustRestore = 0; |
---|
131 | CopyPointer(zz_pEInfo, ptr); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | const zz_pE& zz_pE::zero() |
---|
137 | { |
---|
138 | static zz_pE z(zz_pE_NoAlloc); |
---|
139 | return z; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | zz_pE::zz_pE() |
---|
144 | { |
---|
145 | _zz_pE__rep.rep.SetMaxLength(zz_pE::degree()); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | void div(zz_pE& x, const zz_pE& a, const zz_pE& b) |
---|
151 | { |
---|
152 | zz_pE t; |
---|
153 | |
---|
154 | inv(t, b); |
---|
155 | mul(x, a, t); |
---|
156 | } |
---|
157 | |
---|
158 | void div(zz_pE& x, const zz_pE& a, long b) |
---|
159 | { |
---|
160 | NTL_zz_pRegister(B); |
---|
161 | B = b; |
---|
162 | inv(B, B); |
---|
163 | mul(x, a, B); |
---|
164 | } |
---|
165 | |
---|
166 | void div(zz_pE& x, const zz_pE& a, const zz_p& b) |
---|
167 | { |
---|
168 | NTL_zz_pRegister(B); |
---|
169 | B = b; |
---|
170 | inv(B, B); |
---|
171 | mul(x, a, B); |
---|
172 | } |
---|
173 | |
---|
174 | void div(zz_pE& x, long a, const zz_pE& b) |
---|
175 | { |
---|
176 | zz_pE t; |
---|
177 | inv(t, b); |
---|
178 | mul(x, a, t); |
---|
179 | } |
---|
180 | |
---|
181 | void div(zz_pE& x, const zz_p& a, const zz_pE& b) |
---|
182 | { |
---|
183 | zz_pE t; |
---|
184 | inv(t, b); |
---|
185 | mul(x, a, t); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | void inv(zz_pE& x, const zz_pE& a) |
---|
191 | { |
---|
192 | InvMod(x._zz_pE__rep, a._zz_pE__rep, zz_pE::modulus()); |
---|
193 | } |
---|
194 | |
---|
195 | NTL_END_IMPL |
---|