1 | |
---|
2 | /**************************************************************************\ |
---|
3 | |
---|
4 | MODULE: vec_GF2 |
---|
5 | |
---|
6 | SUMMARY: |
---|
7 | |
---|
8 | a vec_GF2 is a vector of GF2s that behaves much like generic NTL vectors |
---|
9 | (see vector.txt), but there are some differences. |
---|
10 | |
---|
11 | For efficiency, elements of a vec_GF2 are "packed" into a word. |
---|
12 | One may use subscript notation v[i] or v(i) as an r-value |
---|
13 | in an expression, and as an l-value in the following contexts: |
---|
14 | * on the left-hand side of an assignment operator, |
---|
15 | * on the left-hand side of +=, -=, *=, /=, |
---|
16 | * and as an argument to ++ and --. |
---|
17 | |
---|
18 | One may not use the expression v[i] or v(i) to initialize |
---|
19 | a non-const reference parameter. |
---|
20 | |
---|
21 | For example, if v, w are vec_GF2's, you can write: |
---|
22 | |
---|
23 | v[i] = 0; |
---|
24 | v[i] = v[j] + w[k]; |
---|
25 | v[i] += 1; |
---|
26 | v[i]++; |
---|
27 | v[i] += w[i]; |
---|
28 | |
---|
29 | It is perhaps helpful to describe how this is implemented, |
---|
30 | without going into all the details. |
---|
31 | The type of a subscript-expression is "subscript_GF2" or |
---|
32 | "const_subscript_GF2", the latter chosen if the vector is read-only. |
---|
33 | Both of these "helper" types have automatic conversions |
---|
34 | operators to GF2. Moreover, assignment and increment operators |
---|
35 | are defined for "subscript_GF2" (but not for "const_subscript_GF2"). |
---|
36 | These operators return references to themselves, so one can |
---|
37 | iterate assignment operators as usual (as usual in NTL, |
---|
38 | the return type of post-increment/decrement is void). |
---|
39 | |
---|
40 | As an alternative, one can use the get and put methods below to access |
---|
41 | vector elements. |
---|
42 | |
---|
43 | There is one subtle but important difference in the semantics |
---|
44 | of vec_GF2 and that of generic NTL vectors. With a vec_GF2, whenever its |
---|
45 | length is increased (via SetLength), the "new" bits are always 0. |
---|
46 | For example, if v.length() == 20, then |
---|
47 | |
---|
48 | v.SetLength(10); v.setLength(20); |
---|
49 | |
---|
50 | will effectively clear bits 10..19 of v. |
---|
51 | This is quite different from the semantics of generic NTL vectors, where |
---|
52 | the above sequence would not change the value of v at all. |
---|
53 | One has to be aware of this difference, but it will not matter |
---|
54 | in most ordinary circumstances. |
---|
55 | |
---|
56 | |
---|
57 | \**************************************************************************/ |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | class vec_GF2 { |
---|
62 | |
---|
63 | public: |
---|
64 | |
---|
65 | vec_GF2(); // 0 length vector |
---|
66 | vec_GF2(INIT_SIZE_TYPE, long n); // initialize to length n |
---|
67 | // usage: vec_GF2(INIT_SIZE, n) |
---|
68 | |
---|
69 | vec_GF2(const vec_GF2& a); // copy constructor |
---|
70 | vec_GF2& operator=(const vec_GF2& a); // assignment |
---|
71 | ~vec_GF2(); // destructor |
---|
72 | |
---|
73 | void SetLength(long n); // set length to n bits |
---|
74 | void SetMaxLength(long n); // allocate space for n bits |
---|
75 | |
---|
76 | long length() const; // current length, in bits |
---|
77 | |
---|
78 | long MaxLength() const; // maximum length, i.e., the maximum |
---|
79 | // value passed to either SetLength or SetMaxLength |
---|
80 | // since creation or last kill |
---|
81 | |
---|
82 | long allocated() const; // number of bits for which space is allocated; |
---|
83 | // if n <= v.allocated(), then v.SetLength(n) |
---|
84 | // will not result in any memory re-allocation. |
---|
85 | |
---|
86 | // INVARIANT: |
---|
87 | // length() <= MaxLength() <= allocated() < 2^(NTL_BITS_PER_LONG-4) |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | void FixLength(long n); // fix length to n bits |
---|
92 | // can only be applied after default initialization or kill |
---|
93 | |
---|
94 | long fixed() const; // test if length has been fixed |
---|
95 | |
---|
96 | void kill(); // free space and make length 0 |
---|
97 | |
---|
98 | GF2 get(long i) const; // fetch value at index i (indexing from 0) |
---|
99 | |
---|
100 | void put(long i, GF2 a); // write value a to index i (indexing from 0) |
---|
101 | void put(long i, long a); |
---|
102 | |
---|
103 | // Here are the subscripting operators, defined using the |
---|
104 | // "helper" classes subscript_GF2 and const_subscript_GF2. |
---|
105 | |
---|
106 | subscript_GF2 operator[](long i); |
---|
107 | |
---|
108 | subscript_GF2 operator()(long i); |
---|
109 | |
---|
110 | const_subscript_GF2 operator[](long i) const; |
---|
111 | |
---|
112 | const_subscript_GF2 operator()(long i) const; |
---|
113 | |
---|
114 | }; |
---|
115 | |
---|
116 | |
---|
117 | void swap(vec_GF2& x, vec_GF2& y); |
---|
118 | // swap x and y (fast pointer swap) |
---|
119 | |
---|
120 | void append(vec_GF2& v, GF2 a); |
---|
121 | // append a to v |
---|
122 | |
---|
123 | void append(vec_GF2& v, const vec_GF2& a); |
---|
124 | // append a to v |
---|
125 | |
---|
126 | // equality operators: |
---|
127 | |
---|
128 | long operator==(const vec_GF2& a, const vec_GF2& b); |
---|
129 | long operator!=(const vec_GF2& a, const vec_GF2& b); |
---|
130 | |
---|
131 | |
---|
132 | // I/O operators: |
---|
133 | |
---|
134 | ostream& operator<<(ostream& s, const vec_GF2& a); |
---|
135 | istream& operator>>(istream& s, vec_GF2& a); |
---|
136 | |
---|
137 | // The I/O format is [a_0 a_1 ... a_{n-1}], where each a_i is "0" or "1". |
---|
138 | // On input, the a_i may be arbitrary integers, which are reduced mod 2. |
---|
139 | |
---|
140 | // utility routines: |
---|
141 | |
---|
142 | void clear(vec_GF2& x); // clear all bits--length unchanged |
---|
143 | long IsZero(const vec_GF2& a); // test if all bits are zero |
---|
144 | |
---|
145 | void shift(vec_GF2& x, const vec_GF2& a, long n); |
---|
146 | vec_GF2 shift(const vec_GF2& a, long n); |
---|
147 | // x = a shifted n places, where n may be positive or negative. |
---|
148 | // Generally, x[i] = a[i-n], so positive n shifts to a higher index. |
---|
149 | // The length of x is set to the length of a, and bits |
---|
150 | // are zero-filled or discarded as necessary. |
---|
151 | |
---|
152 | void reverse(vec_GF2& x, const vec_GF2& a); // c = a reversed |
---|
153 | vec_GF2 reverse(const vec_GF2& a); |
---|
154 | |
---|
155 | long weight(const vec_GF2& a); // return number of 1 bits in a |
---|
156 | |
---|
157 | void random(vec_GF2& x, long n); // x = random vector of length n |
---|
158 | vec_GF2 random_vec_GF2(long n); |
---|
159 | |
---|
160 | |
---|
161 | // arithmetic operations over GF(2): |
---|
162 | |
---|
163 | void add(vec_GF2& x, const vec_GF2& a, const vec_GF2& b); |
---|
164 | void sub(vec_GF2& x, const vec_GF2& a, const vec_GF2& b); |
---|
165 | void negate(vec_GF2& x, const vec_GF2& a); |
---|
166 | |
---|
167 | void mul(vec_GF2& x, const vec_GF2& a, GF2 b); |
---|
168 | void mul(vec_GF2& x, const vec_GF2& a, long b); |
---|
169 | |
---|
170 | void mul(vec_GF2& x, GF2 a, const vec_GF2& b); |
---|
171 | void mul(vec_GF2& x, long a, const vec_GF2& b); |
---|
172 | // x = a * b |
---|
173 | |
---|
174 | void InnerProduct(GF2& x, const vec_GF2& a, const vec_GF2& b); |
---|
175 | // vectors may differ in length |
---|
176 | |
---|
177 | void VectorCopy(vec_GF2& x, const vec_GF2& a, long n); |
---|
178 | vec_GF2 VectorCopy(const vec_GF2& a, long n); |
---|
179 | // x = a copy of a of length exactly n. |
---|
180 | // The input is truncated or padded with zeroes, as necessary. |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | // arithmetic operator notation: |
---|
185 | |
---|
186 | vec_GF2 operator+(const vec_GF2& a, const vec_GF2& b); |
---|
187 | vec_GF2 operator-(const vec_GF2& a, const vec_GF2& b); |
---|
188 | vec_GF2 operator-(const vec_GF2& a); |
---|
189 | |
---|
190 | // scalar mul: |
---|
191 | |
---|
192 | vec_GF2 operator*(const vec_GF2& a, GF2 b); |
---|
193 | vec_GF2 operator*(const vec_GF2& a, long b); |
---|
194 | |
---|
195 | vec_GF2 operator*(GF2 a, const vec_GF2& b); |
---|
196 | vec_GF2 operator*(long a, const vec_GF2& b); |
---|
197 | |
---|
198 | // inner product: |
---|
199 | |
---|
200 | inline GF2 operator*(const vec_GF2& a, const vec_GF2& b); |
---|
201 | |
---|
202 | // assignment operator notation: |
---|
203 | |
---|
204 | vec_GF2& operator+=(vec_GF2& x, const vec_GF2& a); |
---|
205 | vec_GF2& operator-=(vec_GF2& x, const vec_GF2& a); |
---|
206 | |
---|
207 | vec_GF2& operator*=(vec_GF2& x, GF2 a); |
---|
208 | vec_GF2& operator*=(vec_GF2& x, long a); |
---|
209 | |
---|