1 | |
---|
2 | |
---|
3 | #include <NTL/vec_ZZ_p.h> |
---|
4 | |
---|
5 | #include <NTL/new.h> |
---|
6 | |
---|
7 | NTL_START_IMPL |
---|
8 | |
---|
9 | |
---|
10 | void BlockConstruct(ZZ_p* x, long n) |
---|
11 | { |
---|
12 | if (n <= 0) return; |
---|
13 | |
---|
14 | if (!ZZ_pInfo) |
---|
15 | Error("ZZ_p constructor called while modulus undefined"); |
---|
16 | |
---|
17 | long d = ZZ_p::ModulusSize(); |
---|
18 | |
---|
19 | long m, j; |
---|
20 | |
---|
21 | long i = 0; |
---|
22 | |
---|
23 | while (i < n) { |
---|
24 | m = ZZ_BlockConstructAlloc(x[i]._ZZ_p__rep, d, n-i); |
---|
25 | for (j = 1; j < m; j++) |
---|
26 | ZZ_BlockConstructSet(x[i]._ZZ_p__rep, x[i+j]._ZZ_p__rep, j); |
---|
27 | i += m; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | void BlockDestroy(ZZ_p* x, long n) |
---|
32 | { |
---|
33 | if (n <= 0) return; |
---|
34 | |
---|
35 | long i = 0; |
---|
36 | long m; |
---|
37 | |
---|
38 | while (i < n) { |
---|
39 | m = ZZ_BlockDestroy(x[i]._ZZ_p__rep); |
---|
40 | i += m; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | NTL_vector_impl_plain(ZZ_p,vec_ZZ_p) |
---|
46 | |
---|
47 | NTL_eq_vector_impl(ZZ_p,vec_ZZ_p) |
---|
48 | |
---|
49 | |
---|
50 | void conv(vec_ZZ_p& x, const vec_ZZ& a) |
---|
51 | { |
---|
52 | long i, n; |
---|
53 | |
---|
54 | n = a.length(); |
---|
55 | x.SetLength(n); |
---|
56 | |
---|
57 | ZZ_p* xp = x.elts(); |
---|
58 | const ZZ* ap = a.elts(); |
---|
59 | |
---|
60 | for (i = 0; i < n; i++) |
---|
61 | conv(xp[i], ap[i]); |
---|
62 | } |
---|
63 | |
---|
64 | void conv(vec_ZZ& x, const vec_ZZ_p& a) |
---|
65 | { |
---|
66 | long n = a.length(); |
---|
67 | x.SetLength(n); |
---|
68 | long i; |
---|
69 | for (i = 0; i < n; i++) |
---|
70 | x[i] = rep(a[i]); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | void InnerProduct(ZZ_p& x, const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
76 | { |
---|
77 | long n = min(a.length(), b.length()); |
---|
78 | long i; |
---|
79 | static ZZ accum, t; |
---|
80 | |
---|
81 | clear(accum); |
---|
82 | for (i = 0; i < n; i++) { |
---|
83 | mul(t, rep(a[i]), rep(b[i])); |
---|
84 | add(accum, accum, t); |
---|
85 | } |
---|
86 | |
---|
87 | conv(x, accum); |
---|
88 | } |
---|
89 | |
---|
90 | void InnerProduct(ZZ_p& x, const vec_ZZ_p& a, const vec_ZZ_p& b, |
---|
91 | long offset) |
---|
92 | { |
---|
93 | if (offset < 0) Error("InnerProduct: negative offset"); |
---|
94 | if (NTL_OVERFLOW(offset, 1, 0)) Error("InnerProduct: offset too big"); |
---|
95 | |
---|
96 | long n = min(a.length(), b.length()+offset); |
---|
97 | long i; |
---|
98 | static ZZ accum, t; |
---|
99 | |
---|
100 | clear(accum); |
---|
101 | for (i = offset; i < n; i++) { |
---|
102 | mul(t, rep(a[i]), rep(b[i-offset])); |
---|
103 | add(accum, accum, t); |
---|
104 | } |
---|
105 | |
---|
106 | conv(x, accum); |
---|
107 | } |
---|
108 | |
---|
109 | void mul(vec_ZZ_p& x, const vec_ZZ_p& a, const ZZ_p& b_in) |
---|
110 | { |
---|
111 | NTL_ZZ_pRegister(b); |
---|
112 | b = b_in; |
---|
113 | long n = a.length(); |
---|
114 | x.SetLength(n); |
---|
115 | long i; |
---|
116 | for (i = 0; i < n; i++) |
---|
117 | mul(x[i], a[i], b); |
---|
118 | } |
---|
119 | |
---|
120 | void mul(vec_ZZ_p& x, const vec_ZZ_p& a, long b_in) |
---|
121 | { |
---|
122 | NTL_ZZ_pRegister(b); |
---|
123 | b = b_in; |
---|
124 | long n = a.length(); |
---|
125 | x.SetLength(n); |
---|
126 | long i; |
---|
127 | for (i = 0; i < n; i++) |
---|
128 | mul(x[i], a[i], b); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | void add(vec_ZZ_p& x, const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
133 | { |
---|
134 | long n = a.length(); |
---|
135 | if (b.length() != n) Error("vector add: dimension mismatch"); |
---|
136 | |
---|
137 | x.SetLength(n); |
---|
138 | long i; |
---|
139 | for (i = 0; i < n; i++) |
---|
140 | add(x[i], a[i], b[i]); |
---|
141 | } |
---|
142 | |
---|
143 | void sub(vec_ZZ_p& x, const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
144 | { |
---|
145 | long n = a.length(); |
---|
146 | if (b.length() != n) Error("vector sub: dimension mismatch"); |
---|
147 | x.SetLength(n); |
---|
148 | long i; |
---|
149 | for (i = 0; i < n; i++) |
---|
150 | sub(x[i], a[i], b[i]); |
---|
151 | } |
---|
152 | |
---|
153 | void clear(vec_ZZ_p& x) |
---|
154 | { |
---|
155 | long n = x.length(); |
---|
156 | long i; |
---|
157 | for (i = 0; i < n; i++) |
---|
158 | clear(x[i]); |
---|
159 | } |
---|
160 | |
---|
161 | void negate(vec_ZZ_p& x, const vec_ZZ_p& a) |
---|
162 | { |
---|
163 | long n = a.length(); |
---|
164 | x.SetLength(n); |
---|
165 | long i; |
---|
166 | for (i = 0; i < n; i++) |
---|
167 | negate(x[i], a[i]); |
---|
168 | } |
---|
169 | |
---|
170 | long IsZero(const vec_ZZ_p& a) |
---|
171 | { |
---|
172 | long n = a.length(); |
---|
173 | long i; |
---|
174 | |
---|
175 | for (i = 0; i < n; i++) |
---|
176 | if (!IsZero(a[i])) |
---|
177 | return 0; |
---|
178 | |
---|
179 | return 1; |
---|
180 | } |
---|
181 | |
---|
182 | vec_ZZ_p operator+(const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
183 | { |
---|
184 | vec_ZZ_p res; |
---|
185 | add(res, a, b); |
---|
186 | NTL_OPT_RETURN(vec_ZZ_p, res); |
---|
187 | } |
---|
188 | |
---|
189 | vec_ZZ_p operator-(const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
190 | { |
---|
191 | vec_ZZ_p res; |
---|
192 | sub(res, a, b); |
---|
193 | NTL_OPT_RETURN(vec_ZZ_p, res); |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | vec_ZZ_p operator-(const vec_ZZ_p& a) |
---|
198 | { |
---|
199 | vec_ZZ_p res; |
---|
200 | negate(res, a); |
---|
201 | NTL_OPT_RETURN(vec_ZZ_p, res); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | ZZ_p operator*(const vec_ZZ_p& a, const vec_ZZ_p& b) |
---|
206 | { |
---|
207 | ZZ_p res; |
---|
208 | InnerProduct(res, a, b); |
---|
209 | NTL_OPT_RETURN(ZZ_p, res); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | void VectorCopy(vec_ZZ_p& x, const vec_ZZ_p& a, long n) |
---|
214 | { |
---|
215 | if (n < 0) Error("VectorCopy: negative length"); |
---|
216 | if (NTL_OVERFLOW(n, 1, 0)) Error("overflow in VectorCopy"); |
---|
217 | |
---|
218 | long m = min(n, a.length()); |
---|
219 | |
---|
220 | x.SetLength(n); |
---|
221 | |
---|
222 | long i; |
---|
223 | |
---|
224 | for (i = 0; i < m; i++) |
---|
225 | x[i] = a[i]; |
---|
226 | |
---|
227 | for (i = m; i < n; i++) |
---|
228 | clear(x[i]); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | NTL_END_IMPL |
---|