-
Notifications
You must be signed in to change notification settings - Fork 0
/
F2_poly.rb
executable file
·351 lines (301 loc) · 6.36 KB
/
F2_poly.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/ruby -Wall
# ================================================================
# Please see LICENSE.txt in the same directory as this file.
# John Kerl
# kerl.john.r@gmail.com
# Copyright (c) 2004
# Ported to Ruby 2011-02-10
# ================================================================
require 'Bit_arith.rb'
class F2_poly
# ------------------------------------------------------------
def initialize(arg)
if String === arg
@bits = Integer("0x" + arg)
elsif F2_poly === arg
@bits = arg.bits
else # Nominally expecting integer or quacks-like-integer.
@bits = arg
end
end
# Allow reading and writing of bits.
attr_accessor :bits
# ------------------------------------------------------------
@@format = '%x'
def F2_poly.set_hex_output
@@format = "%x"
end
def F2_poly.set_binary_output
@@format = "%b"
end
def to_s()
@@format % @bits
end
def inspect()
@@format % @bits
end
# ----------------------------------------------------------------
def ==(other)
@bits == other.bits
end
def zero?
return @bits == 0
end
def nonzero?
return @bits != 0
end
def one?
return @bits == 1
end
def degree
F2_poly.bit_degree(@bits)
end
# Getter for bit j
def [](j)
if ((@bits >> j) & 1) == 1
return 1
else
return 0
end
end
# Setter for bit j
def []=(j, v)
if (v & 1) == 1
@bits |= 1 << j
else
@bits &= ~(1 << j)
end
end
# ------------------------------------------------------------
def +(other)
F2_poly.new(@bits ^ other.bits)
end
def -(other)
F2_poly.new(@bits ^ other.bits)
end
def -@
F2_poly.new(@bits)
end
def *(other)
F2_poly.new(F2_poly.bit_mul(@bits, other.bits))
end
def /(other)
quot_bits, rem_bits = F2_poly.iquot_and_rem(@bits, other.bits)
F2_poly.new(quot_bits)
end
def %(other)
quot_bits, rem_bits = F2_poly.iquot_and_rem(@bits, other.bits)
F2_poly.new(rem_bits)
end
def **(e)
if @bits == 0
if e == 0
raise "F2_poly: 0**0 undefined."
elsif e < 0
raise "F2_poly **: division by zero.."
else
return F2_poly.new(0)
end
end
# The only element that can be reciprocated is 1.
# Why bother?
if e < 0
raise "F2_poly **: negative exponents disallowed."
end
rv = F2_poly.new(1)
xp = F2_poly.new(@bits)
if e < 0
xp = xp.recip
e = -e
end
# Repeated squaring
while (e != 0)
if (e & 1) == 1
rv *= xp;
end
e = e >> 1;
xp *= xp;
end
return rv
end
# ----------------------------------------------------------------
# For sorting, which makes factorization output unique.
def <=>(other)
@bits <=> other.bits
end
def <(other)
@bits < other.bits
end
def <=(other)
@bits <= other.bits
end
def >(other)
@bits > other.bits
end
def >=(other)
@bits >= other.bits
end
# ------------------------------------------------------------
# These are class methods, where arguments and return values are integers.
# They help cut down on unnecessary object creation (and thus garbage
# collection), in particular during gcd and ext_gcd.
def F2_poly.bit_degree(bits)
d = Bit_arith.msb_pos(bits)
if d == -1
return 0
else
return d
end
#d = 0
#if bits == 0 # I define the zero polynomial to have zero degree.
# return 0
#end
#while (bits >>= 1) > 0
# d += 1
#end
#return d
end
def F2_poly.bit_mul(this, that)
prod = 0
this_deg = F2_poly.bit_degree(this)
that_deg = F2_poly.bit_degree(that)
prod_deg = this_deg + that_deg
a = this
b = that
c = 0
ashift = a
for j in 0..that_deg
if ((b >> j) & 1) == 1
c ^= ashift
end
ashift <<= 1
end
return c
end
# Returns quotient, remainder
def F2_poly.iquot_and_rem(this, that)
if that == 0
raise "F2_poly iquot_and_rem: division by zero."
end
divisor_l1_pos = F2_poly.bit_degree(that)
if this == 0
return 0, 0
end
dividend_l1_pos = F2_poly.bit_degree(this)
l1_diff = dividend_l1_pos - divisor_l1_pos
if (l1_diff < 0) # Dividend has lower degree than divisor.
return 0, this
end
shift_divisor = that << l1_diff
quot = 0
rem = this
check_pos = dividend_l1_pos
quot_pos = l1_diff
while check_pos >= divisor_l1_pos
if ((rem >> check_pos) & 1) == 1
rem ^= shift_divisor
quot |= 1 << quot_pos
end
shift_divisor >>= 1
check_pos -= 1
quot_pos -= 1
end
return quot, rem
end
# ----------------------------------------------------------------
def gcd(other)
if @bits == 0
return F2_poly.new(other.bits)
end
if other.bits == 0
return F2_poly.new(@bits)
end
c = @bits
d = other.bits
q = 0
r = 0
while true
q, r = F2_poly.iquot_and_rem(c, d)
if r == 0
break
end
c = d
d = r
end
return F2_poly.new(d)
end
def lcm(other)
return self * other / self.gcd(other)
end
# Blankinship's algorithm.
# Returns the gcd, s, and t, where the latter are such that
# self * s + other * t = gcd.
def ext_gcd(other)
if @bits == 0
return F2_poly.new(other.bits), F2_poly.new(0), F2_poly.new(1)
end
if other.bits == 0
return F2_poly.new(@bits), F2_poly.new(1), F2_poly.new(0)
end
sprime = 1
t = 1
s = 0
tprime = 0
c = @bits
d = other.bits
while true
q, r = F2_poly.iquot_and_rem(c, d)
if r == 0
break
end
c = d
d = r
sprime, s = s, sprime ^ F2_poly.bit_mul(q, s)
tprime, t = t, tprime ^ F2_poly.bit_mul(q, t)
end
return F2_poly.new(d), F2_poly.new(s), F2_poly.new(t)
end
# ----------------------------------------------------------------
# Used by the Berlekamp factorization algorithm.
def deriv
mask = 0x55555555
while (mask < bits)
mask <<= 32
mask |= 0x55555555
end
bits = @bits >> 1
bits &= mask
F2_poly.new(bits)
end
# ----------------------------------------------------------------
# Relies on the fact that f(x^p) = f^p(x) over Fp[x]:
#
# in = a4 x^4 + a2 x^2 + a0
# out = a4 x^2 + a2 x + a0
# Returns [true, the square root] or [false, nil].
def square_root
deg = self.degree
sqroot_bits = 0
inbit = 1
outbit = 1
si = 0 # source index
while si <= deg
if (@bits & inbit) != 0
sqroot_bits |= outbit
end
inbit <<= 1
if (@bits & inbit) != 0
return false, nil
end
inbit <<= 1
outbit <<= 1
si += 2
end
return [true, F2_poly.new(sqroot_bits)]
end
# ----------------------------------------------------------------
def F2_poly.random(degree)
msb = 1 << degree
F2_poly.new(msb | rand(msb))
end
end