1
2 #include <NTL/vec_ZZ_pE.h>
3
4
5 NTL_START_IMPL
6
InnerProduct(ZZ_pE & x,const vec_ZZ_pE & a,const vec_ZZ_pE & b)7 void InnerProduct(ZZ_pE& x, const vec_ZZ_pE& a, const vec_ZZ_pE& b)
8 {
9 long n = min(a.length(), b.length());
10 long i;
11 ZZ_pX accum, t;
12
13 clear(accum);
14 for (i = 0; i < n; i++) {
15 mul(t, rep(a[i]), rep(b[i]));
16 add(accum, accum, t);
17 }
18
19 conv(x, accum);
20 }
21
InnerProduct(ZZ_pE & x,const vec_ZZ_pE & a,const vec_ZZ_pE & b,long offset)22 void InnerProduct(ZZ_pE& x, const vec_ZZ_pE& a, const vec_ZZ_pE& b,
23 long offset)
24 {
25 if (offset < 0) LogicError("InnerProduct: negative offset");
26 if (NTL_OVERFLOW(offset, 1, 0)) ResourceError("InnerProduct: offset too big");
27
28 long n = min(a.length(), b.length()+offset);
29 long i;
30 ZZ_pX accum, t;
31
32 clear(accum);
33 for (i = offset; i < n; i++) {
34 mul(t, rep(a[i]), rep(b[i-offset]));
35 add(accum, accum, t);
36 }
37
38 conv(x, accum);
39 }
40
mul(vec_ZZ_pE & x,const vec_ZZ_pE & a,const ZZ_pE & b_in)41 void mul(vec_ZZ_pE& x, const vec_ZZ_pE& a, const ZZ_pE& b_in)
42 {
43 ZZ_pE b = b_in;
44 long n = a.length();
45 x.SetLength(n);
46 long i;
47 for (i = 0; i < n; i++)
48 mul(x[i], a[i], b);
49 }
50
mul(vec_ZZ_pE & x,const vec_ZZ_pE & a,const ZZ_p & b_in)51 void mul(vec_ZZ_pE& x, const vec_ZZ_pE& a, const ZZ_p& b_in)
52 {
53 NTL_ZZ_pRegister(b);
54 b = b_in;
55 long n = a.length();
56 x.SetLength(n);
57 long i;
58 for (i = 0; i < n; i++)
59 mul(x[i], a[i], b);
60 }
61
mul(vec_ZZ_pE & x,const vec_ZZ_pE & a,long b_in)62 void mul(vec_ZZ_pE& x, const vec_ZZ_pE& a, long b_in)
63 {
64 NTL_ZZ_pRegister(b);
65 b = b_in;
66 long n = a.length();
67 x.SetLength(n);
68 long i;
69 for (i = 0; i < n; i++)
70 mul(x[i], a[i], b);
71 }
72
73
add(vec_ZZ_pE & x,const vec_ZZ_pE & a,const vec_ZZ_pE & b)74 void add(vec_ZZ_pE& x, const vec_ZZ_pE& a, const vec_ZZ_pE& b)
75 {
76 long n = a.length();
77 if (b.length() != n) LogicError("vector add: dimension mismatch");
78
79 x.SetLength(n);
80 long i;
81 for (i = 0; i < n; i++)
82 add(x[i], a[i], b[i]);
83 }
84
sub(vec_ZZ_pE & x,const vec_ZZ_pE & a,const vec_ZZ_pE & b)85 void sub(vec_ZZ_pE& x, const vec_ZZ_pE& a, const vec_ZZ_pE& b)
86 {
87 long n = a.length();
88 if (b.length() != n) LogicError("vector sub: dimension mismatch");
89
90 x.SetLength(n);
91 long i;
92 for (i = 0; i < n; i++)
93 sub(x[i], a[i], b[i]);
94 }
95
negate(vec_ZZ_pE & x,const vec_ZZ_pE & a)96 void negate(vec_ZZ_pE& x, const vec_ZZ_pE& a)
97 {
98 long n = a.length();
99
100 x.SetLength(n);
101 long i;
102 for (i = 0; i < n; i++)
103 negate(x[i], a[i]);
104 }
105
106
clear(vec_ZZ_pE & x)107 void clear(vec_ZZ_pE& x)
108 {
109 long n = x.length();
110 long i;
111 for (i = 0; i < n; i++)
112 clear(x[i]);
113 }
114
115
116
IsZero(const vec_ZZ_pE & a)117 long IsZero(const vec_ZZ_pE& a)
118 {
119 long n = a.length();
120 long i;
121
122 for (i = 0; i < n; i++)
123 if (!IsZero(a[i]))
124 return 0;
125
126 return 1;
127 }
128
operator +(const vec_ZZ_pE & a,const vec_ZZ_pE & b)129 vec_ZZ_pE operator+(const vec_ZZ_pE& a, const vec_ZZ_pE& b)
130 {
131 vec_ZZ_pE res;
132 add(res, a, b);
133 NTL_OPT_RETURN(vec_ZZ_pE, res);
134 }
135
operator -(const vec_ZZ_pE & a,const vec_ZZ_pE & b)136 vec_ZZ_pE operator-(const vec_ZZ_pE& a, const vec_ZZ_pE& b)
137 {
138 vec_ZZ_pE res;
139 sub(res, a, b);
140 NTL_OPT_RETURN(vec_ZZ_pE, res);
141 }
142
143
operator -(const vec_ZZ_pE & a)144 vec_ZZ_pE operator-(const vec_ZZ_pE& a)
145 {
146 vec_ZZ_pE res;
147 negate(res, a);
148 NTL_OPT_RETURN(vec_ZZ_pE, res);
149 }
150
151
operator *(const vec_ZZ_pE & a,const vec_ZZ_pE & b)152 ZZ_pE operator*(const vec_ZZ_pE& a, const vec_ZZ_pE& b)
153 {
154 ZZ_pE res;
155 InnerProduct(res, a, b);
156 return res;
157 }
158
VectorCopy(vec_ZZ_pE & x,const vec_ZZ_pE & a,long n)159 void VectorCopy(vec_ZZ_pE& x, const vec_ZZ_pE& a, long n)
160 {
161 if (n < 0) LogicError("VectorCopy: negative length");
162 if (NTL_OVERFLOW(n, 1, 0)) ResourceError("overflow in VectorCopy");
163
164 long m = min(n, a.length());
165
166 x.SetLength(n);
167
168 long i;
169
170 for (i = 0; i < m; i++)
171 x[i] = a[i];
172
173 for (i = m; i < n; i++)
174 clear(x[i]);
175 }
176
random(vec_ZZ_pE & x,long n)177 void random(vec_ZZ_pE& x, long n)
178 {
179 x.SetLength(n);
180 for (long i = 0; i < n; i++) random(x[i]);
181 }
182
183
184 NTL_END_IMPL
185