1 | |
---|
2 | |
---|
3 | #include <NTL/ZZ_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 |
---|