source: git/ntl/include/NTL/GF2.h @ 77902f

spielwiese
Last change on this file since 77902f was 77902f, checked in by Hans Schönemann <hannes@…>, 21 years ago
*hannes: iostream adaption git-svn-id: file:///usr/local/Singular/svn/trunk@6321 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.5 KB
Line 
1
2#ifndef NTL_GF2__H
3#define NTL_GF2__H
4
5#include <NTL/ZZ.h>
6
7NTL_OPEN_NNS
8
9class GF2 {
10public:
11
12long _GF2__rep;
13
14
15GF2() { _GF2__rep = 0; }
16
17GF2(const GF2& a) : _GF2__rep(a._GF2__rep) { }
18
19~GF2() { }
20
21GF2& operator=(const GF2& a) { _GF2__rep = a._GF2__rep; return *this; }
22
23GF2& operator=(long a) { _GF2__rep = a & 1; return *this; }
24
25};
26
27inline void conv(GF2& x, long a) { x._GF2__rep = a & 1; }
28inline GF2 to_GF2(long a) 
29   { GF2 x; conv(x, a); return x; }
30
31inline void conv(GF2& x, const ZZ& a) { x._GF2__rep = IsOdd(a); }
32inline GF2 to_GF2(const ZZ& a) 
33   { GF2 x; conv(x, a); return x; }
34
35inline long rep(GF2 a) { return a._GF2__rep; }
36
37static long modulus() { return 2; }
38static GF2 zero() { return GF2(); }
39
40inline void clear(GF2& x) { x._GF2__rep = 0; }
41inline void set(GF2& x) { x._GF2__rep = 1; }
42
43inline void swap(GF2& x, GF2& y)
44   { long t; t = x._GF2__rep; x._GF2__rep = y._GF2__rep; y._GF2__rep = t; }
45
46inline void add(GF2& x, GF2 a, GF2 b)
47   { x._GF2__rep = a._GF2__rep ^ b._GF2__rep; }
48
49inline void sub(GF2& x, GF2 a, GF2 b)
50   { x._GF2__rep = a._GF2__rep ^ b._GF2__rep; }
51
52inline void negate(GF2& x, GF2 a)
53   { x._GF2__rep = a._GF2__rep; }
54
55inline void add(GF2& x, GF2 a, long b)
56   { x._GF2__rep = a._GF2__rep ^ (b & 1); }
57
58inline void add(GF2& x, long a, GF2 b)
59   { x._GF2__rep = (a & 1) ^ b._GF2__rep; }
60
61inline void sub(GF2& x, GF2 a, long b)
62   { x._GF2__rep = a._GF2__rep ^ (b & 1); }
63
64inline void sub(GF2& x, long a, GF2 b)
65   { x._GF2__rep = (a & 1) ^ b._GF2__rep; }
66
67inline GF2 operator+(GF2 a, GF2 b)
68    { GF2 x; add(x, a, b); return x; }
69
70inline GF2 operator+(GF2 a, long b)
71    { GF2 x; add(x, a, b); return x; }
72
73inline GF2 operator+(long a, GF2 b)
74    { GF2 x; add(x, a, b); return x; }
75
76inline GF2 operator-(GF2 a, GF2 b)
77    { GF2 x; sub(x, a, b); return x; }
78
79inline GF2 operator-(GF2 a, long b)
80    { GF2 x; sub(x, a, b); return x; }
81
82inline GF2 operator-(long a, GF2 b)
83    { GF2 x; sub(x, a, b); return x; }
84
85inline GF2 operator-(GF2 a)
86   { GF2 x; negate(x, a); return x; }
87
88inline GF2& operator+=(GF2& x, GF2 b)
89   { add(x, x, b); return x; }
90
91inline GF2& operator+=(GF2& x, long b)
92   { add(x, x, b); return x; }
93
94inline GF2& operator-=(GF2& x, GF2 b)
95   { sub(x, x, b); return x; }
96
97inline GF2& operator-=(GF2& x, long b)
98   { sub(x, x, b); return x; }
99
100inline GF2& operator++(GF2& x) { add(x, x, 1); return x; }
101inline void operator++(GF2& x, int) { add(x, x, 1); }
102inline GF2& operator--(GF2& x) { sub(x, x, 1); return x; }
103inline void operator--(GF2& x, int) { sub(x, x, 1); }
104
105
106inline void mul(GF2& x, GF2 a, GF2 b)
107   { x._GF2__rep = a._GF2__rep & b._GF2__rep; }
108
109inline void mul(GF2& x, GF2 a, long b)
110   { x._GF2__rep = a._GF2__rep & b; }
111
112inline void mul(GF2& x, long a, GF2 b)
113   { x._GF2__rep = a & b._GF2__rep; }
114
115inline void sqr(GF2& x, GF2 a)
116   { x = a; }
117
118inline GF2 sqr(GF2 a)
119   { return a; }
120
121inline GF2 operator*(GF2 a, GF2 b)
122    { GF2 x; mul(x, a, b); return x; }
123
124inline GF2 operator*(GF2 a, long b)
125    { GF2 x; mul(x, a, b); return x; }
126
127inline GF2 operator*(long a, GF2 b)
128    { GF2 x; mul(x, a, b); return x; }
129
130
131inline GF2& operator*=(GF2& x, GF2 b)
132   { mul(x, x, b); return x; }
133
134inline GF2& operator*=(GF2& x, long b)
135   { mul(x, x, b); return x; }
136
137
138void div(GF2& x, GF2 a, GF2 b);
139void div(GF2& x, long a, GF2 b);
140void div(GF2& x, GF2 a, long b);
141
142void inv(GF2& x, GF2 a);
143
144inline GF2 inv(GF2 a)
145   { GF2 x; inv(x, a); return x; }
146
147inline GF2 operator/(GF2 a, GF2 b)
148    { GF2 x; div(x, a, b); return x; }
149
150inline GF2 operator/(GF2 a, long b)
151    { GF2 x; div(x, a, b); return x; }
152
153inline GF2 operator/(long a, GF2 b)
154    { GF2 x; div(x, a, b); return x; }
155
156
157inline GF2& operator/=(GF2& x, GF2 b)
158   { div(x, x, b); return x; }
159
160inline GF2& operator/=(GF2& x, long b)
161   { div(x, x, b); return x; }
162
163
164void power(GF2& x, GF2 a, long e);
165inline GF2 power(GF2 a, long e)
166   { GF2 x; power(x, a, e); return x; }
167
168
169inline long IsZero(GF2 a)
170   { return a._GF2__rep == 0; }
171
172inline long IsOne(GF2 a)
173   { return a._GF2__rep == 1; }
174
175inline long operator==(GF2 a, GF2 b)
176   { return a._GF2__rep == b._GF2__rep; }
177
178inline long operator!=(GF2 a, GF2 b)
179   { return !(a == b); }
180
181inline long operator==(GF2 a, long b) { return a._GF2__rep == (b & 1); }
182inline long operator==(long a, GF2 b) { return (a & 1) == b._GF2__rep; }
183
184inline long operator!=(GF2 a, long b) { return !(a == b); }
185inline long operator!=(long a, GF2 b) { return !(a == b); }
186
187inline void random(GF2& x)
188   { x._GF2__rep = RandomBnd(2); }
189
190inline GF2 random_GF2()
191   { GF2 x; random(x); return x; }
192
193NTL_CLOSE_NNS
194
195#endif
196
Note: See TracBrowser for help on using the repository browser.