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