1 | |
---|
2 | |
---|
3 | /**************************************************************************\ |
---|
4 | |
---|
5 | MODULE: GF2 |
---|
6 | |
---|
7 | SUMMARY: |
---|
8 | |
---|
9 | The class GF2 represents the field GF(2). |
---|
10 | Computationally speaking, it is not a particularly useful class. |
---|
11 | Its main use is to make the interfaces to the various finite |
---|
12 | field classes as uniform as possible. |
---|
13 | |
---|
14 | \**************************************************************************/ |
---|
15 | |
---|
16 | #include <NTL/ZZ.h> |
---|
17 | |
---|
18 | |
---|
19 | class GF2 { |
---|
20 | public: |
---|
21 | |
---|
22 | GF2(); // initial value 0 |
---|
23 | |
---|
24 | GF2(const GF2& a); // copy constructor |
---|
25 | |
---|
26 | GF2& operator=(const GF2& a); // assignment |
---|
27 | GF2& operator=(long a); // assignment |
---|
28 | |
---|
29 | }; |
---|
30 | |
---|
31 | |
---|
32 | long rep(GF2 a); // read-only access to representation of a |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | /**************************************************************************\ |
---|
39 | |
---|
40 | Comparison |
---|
41 | |
---|
42 | \**************************************************************************/ |
---|
43 | |
---|
44 | |
---|
45 | long operator==(GF2 a, GF2 b); |
---|
46 | long operator!=(GF2 a, GF2 b); |
---|
47 | |
---|
48 | long IsZero(GF2 a); // test for 0 |
---|
49 | long IsOne(GF2 a); // test for 1 |
---|
50 | |
---|
51 | // PROMOTIONS: operators ==, != promote long to GF2 on (a, b). |
---|
52 | |
---|
53 | |
---|
54 | /**************************************************************************\ |
---|
55 | |
---|
56 | Addition |
---|
57 | |
---|
58 | \**************************************************************************/ |
---|
59 | |
---|
60 | // operator notation: |
---|
61 | |
---|
62 | GF2 operator+(GF2 a, GF2 b); |
---|
63 | GF2 operator-(GF2 a, GF2 b); |
---|
64 | |
---|
65 | GF2 operator-(GF2 a); // unary - |
---|
66 | |
---|
67 | GF2& operator+=(GF2& x, GF2 a); |
---|
68 | GF2& operator+=(GF2& x, long a); |
---|
69 | |
---|
70 | GF2& operator-=(GF2& x, GF2 a); |
---|
71 | GF2& operator-=(GF2& x, long a); |
---|
72 | |
---|
73 | GF2& operator++(GF2& x); // prefix |
---|
74 | void operator++(GF2& x, int); // postfix |
---|
75 | |
---|
76 | GF2& operator--(GF2& x); // prefix |
---|
77 | void operator--(GF2& x, int); // postfix |
---|
78 | |
---|
79 | // procedural versions: |
---|
80 | |
---|
81 | |
---|
82 | void add(GF2& x, GF2 a, GF2 b); // x = a + b |
---|
83 | void sub(GF2& x, GF2 a, GF2 b); // x = a - b |
---|
84 | void negate(GF2& x, GF2 a); // x = -a |
---|
85 | |
---|
86 | // PROMOTIONS: binary +, -, and procedures add, sub promote |
---|
87 | // from long to GF2 on (a, b). |
---|
88 | |
---|
89 | |
---|
90 | /**************************************************************************\ |
---|
91 | |
---|
92 | Multiplication |
---|
93 | |
---|
94 | \**************************************************************************/ |
---|
95 | |
---|
96 | // operator notation: |
---|
97 | |
---|
98 | GF2 operator*(GF2 a, GF2 b); |
---|
99 | |
---|
100 | GF2& operator*=(GF2& x, GF2 a); |
---|
101 | GF2& operator*=(GF2& x, long a); |
---|
102 | |
---|
103 | // procedural versions: |
---|
104 | |
---|
105 | void mul(GF2& x, GF2 a, GF2 b); // x = a * b |
---|
106 | |
---|
107 | void sqr(GF2& x, GF2 a); // x = a^2 |
---|
108 | GF2 sqr(GF2 a); |
---|
109 | |
---|
110 | // PROMOTIONS: operator * and procedure mul promote from long to GF2 |
---|
111 | // on (a, b). |
---|
112 | |
---|
113 | |
---|
114 | /**************************************************************************\ |
---|
115 | |
---|
116 | Division |
---|
117 | |
---|
118 | \**************************************************************************/ |
---|
119 | |
---|
120 | operator notation: |
---|
121 | |
---|
122 | GF2 operator/(z_p a, GF2 b); |
---|
123 | |
---|
124 | GF2& operator/=(GF2& x, GF2 a); |
---|
125 | GF2& operator/=(GF2& x, long a); |
---|
126 | |
---|
127 | procedural versions: |
---|
128 | |
---|
129 | void div(GF2& x, GF2 a, GF2 b); |
---|
130 | // x = a/b |
---|
131 | |
---|
132 | void inv(GF2& x, GF2 a); |
---|
133 | GF2 inv(GF2 a); |
---|
134 | // x = 1/a |
---|
135 | |
---|
136 | // PROMOTIONS: operator / and procedure div promote from long to GF2 |
---|
137 | // on (a, b). |
---|
138 | |
---|
139 | |
---|
140 | /**************************************************************************\ |
---|
141 | |
---|
142 | Exponentiation |
---|
143 | |
---|
144 | \**************************************************************************/ |
---|
145 | |
---|
146 | |
---|
147 | void power(GF2& x, GF2 a, long e); // x = a^e (e may be negative) |
---|
148 | GF2 power(GF2 a, long e); |
---|
149 | |
---|
150 | |
---|
151 | /**************************************************************************\ |
---|
152 | |
---|
153 | Random Elements |
---|
154 | |
---|
155 | \**************************************************************************/ |
---|
156 | |
---|
157 | |
---|
158 | void random(GF2& x); |
---|
159 | GF2 random_GF2(); |
---|
160 | // x = random element in GF2. Uses RandomBnd from ZZ. |
---|
161 | |
---|
162 | |
---|
163 | /**************************************************************************\ |
---|
164 | |
---|
165 | Input/Output |
---|
166 | |
---|
167 | \**************************************************************************/ |
---|
168 | |
---|
169 | |
---|
170 | ostream& operator<<(ostream& s, GF2 a); |
---|
171 | |
---|
172 | istream& operator>>(istream& s, GF2& x); |
---|
173 | // a ZZ is read and reduced mod 2 |
---|
174 | |
---|
175 | |
---|
176 | /**************************************************************************\ |
---|
177 | |
---|
178 | Miscellany |
---|
179 | |
---|
180 | \**************************************************************************/ |
---|
181 | |
---|
182 | |
---|
183 | void clear(GF2& x); // x = 0 |
---|
184 | void set(GF2& x); // x = 1 |
---|
185 | |
---|
186 | static GF2 GF2::zero(); |
---|
187 | // GF2::zero() yields a read-only reference to zero |
---|
188 | |
---|
189 | void swap(GF2& x, GF2& y); |
---|
190 | // swap x and y |
---|
191 | |
---|