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