Class: SyMath::Poly::Galois

Inherits:
SyMath::Poly show all
Defined in:
lib/symath/poly/galois.rb

Overview

Class representing a galois field, i.e finite field

Instance Attribute Summary collapse

Attributes inherited from SyMath::Poly

#arr, #var

Instance Method Summary collapse

Methods inherited from SyMath::Poly

#%, #*, #**, #+, #-, #-@, #/, #==, #[], #degree, #lc, #sort_factors, #sort_factors_multiple, #strip!, #to_m, #zero?

Constructor Details

#initialize(args) ⇒ Galois

Create gl instance from dup or a gl of the same order.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/symath/poly/galois.rb', line 9

def initialize(args)
  if args.key?(:dup)
    init_from_dup(args[:dup], args[:p])
    return
  end

  if args.key?(:arr)
    init_from_array(args[:arr], args[:p], args[:var])
    return
  end
  
  raise 'Bad arguments for Poly::Galois constructor'
end

Instance Attribute Details

#pObject (readonly)

Returns the value of attribute p.



6
7
8
# File 'lib/symath/poly/galois.rb', line 6

def p
  @p
end

Instance Method Details

#add(g) ⇒ Object

Sum two polynomials



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/symath/poly/galois.rb', line 447

def add(g)
  ret = @arr.clone

  rd = degree
  gd = g.degree
  
  if gd > rd
    (gd - rd).times { ret.unshift(0) }
    rd += gd - rd
  end

  (0..gd).each do |i|
    ret[rd - i] = (ret[rd - i] + g[gd - i]) % @p
  end
  
  return new_gl(ret).strip!
end

#add_ground(a) ⇒ Object



484
485
486
# File 'lib/symath/poly/galois.rb', line 484

def add_ground(a)
  return add(new_gl([a]))
end

#assert_order(g) ⇒ Object

Assert that two fields have the same order



54
55
56
57
58
# File 'lib/symath/poly/galois.rb', line 54

def assert_order(g)
  if @p != g.p
    raise 'Fields do not have the same order'
  end
end

#ddf_zassenhausObject

Deterministic distinct degree factorization



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
# File 'lib/symath/poly/galois.rb', line 251

def ddf_zassenhaus()
  x = gl_x
  
  i = 1
  g = x
  factors = []

  f = self
  b = f.frobenius_monomial_base

  while 2*i <= f.degree
    g = g.frobenius_map(f, b)
    h = f.gcd(g - x)

    if h.arr != [1]
      factors << [h, i]

      f = f / h
      g = g % f
      b = f.frobenius_monomial_base
    end

    i += 1
  end

  if f.arr != [1]
    return factors + [[f, f.degree]]
  else
    return factors
  end
end

#diffObject



396
397
398
399
400
401
402
# File 'lib/symath/poly/galois.rb', line 396

def diff()
  d = degree
  res = @arr.each_with_index.map { |e, i| e*(d - i) % @p }
  res.pop

  return new_gl(res).strip!
end

#div(g) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/symath/poly/galois.rb', line 572

def div(g)
  assert_order(g)

  df = degree
  dg = g.degree

  if g.zero?
    raise 'Division by zero'
  elsif df < dg
    return [zero, self.clone]
  end

  inv = invert(g[0])

  h = @arr.clone
  dq = df - dg
  dr = dg - 1

  (0..df).each do |i|
    coeff = h[i]

    a = [0, dg - i].max
    b = [df - i, dr].min
    (a..b).each do |j|
      coeff -= h[i + j - dg]*g[dg - j]
    end

    if i <= dq
      coeff *= inv
    end

    h[i] = coeff % @p
  end

  return [new_gl(h[0..dq]), new_gl(h[dq + 1..-1]).strip!]
end

#edf_zassenhaus(n) ⇒ Object

Equal degree factorization



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
# File 'lib/symath/poly/galois.rb', line 291

def edf_zassenhaus(n)
  factors = [self.clone]

  if degree <= n
    return factors
  end

  nn = degree / n

  if @p != 2
    b = frobenius_monomial_base
  end

  while factors.size < nn
    r = random_gl(2*n - 1)
  
    if @p == 2
      h = r
      
      (2**(n*nn - 1)).times do
        r = r.pow_mod(2, self)
        h += r
      end

      g = gcd(h)
    else
      h = r.pow_pnm1d2(n, self, b)
      g = gcd(h - 1)
    end
    
    if g.arr != [1] and g != self
      factors = g.edf_zassenhaus(n) + (self / g).edf_zassenhaus(n)
    end
  end

  return sort_factors(factors)
end

#factorObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/symath/poly/galois.rb', line 170

def factor()
  (lc, f) = monic

  if f.degree < 1
    return [lc, []]
  end

  factors = []

  f.sqf_list[1].each do |g, n|
    g.factor_sqf[1].each do |h|
      factors << [h, n]
    end
  end

  return [lc, sort_factors_multiple(factors)]
end

#factor_sqfObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/symath/poly/galois.rb', line 91

def factor_sqf()
  (lc, f) = monic

  if f.degree < 1
    return [lc, zero]
  end

  factors = f.zassenhaus

  return [lc, factors]
end

#frobenius_map(g, b) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/symath/poly/galois.rb', line 228

def frobenius_map(g, b)
  f = self
  m = g.degree

  if f.degree >= m
    f = rem(g)
  end

  if f.zero?
    return zero
  end
  
  n = f.degree
  sf = new_gl([f[-1]])

  (1..n).each do |i|
    sf += b[i]*f[n - i]
  end

  return sf
end

#frobenius_monomial_baseObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/symath/poly/galois.rb', line 203

def frobenius_monomial_base()
  n = degree

  if n == 0
    return []
  end

  b = []
  b << one

  if @p < n
    (1..n - 1).each do |i|
      mon = b[i - 1].lshift(@p)
      b << mon % self
    end
  elsif n > 1
    b << new_gl([1, 0]).pow_mod(@p, self)
    (2..n - 1).each do |i|
      b << (b[i - 1]*b[1]) % self
    end
  end

  return b
end

#gcd(g) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/symath/poly/galois.rb', line 404

def gcd(g)
  assert_order(g)

  f = self
  # Euclidian algorithm for gcd      
  while !g.zero?
    (f, g) = [g, f.rem(g)]
  end

  return f.monic[1]
end

#gcdex(g) ⇒ Object

Extended gcd for two polynomials



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/symath/poly/galois.rb', line 340

def gcdex(g)
  assert_order(g)

  if self.zero? and g.zero?
    return [one, zero, zero]
  end

  (p0, r0) = monic
  (p1, r1) = g.monic

  if zero?
    return [zero, new_gl([invert(p1)]), r1]
  end

  if g.zero?
    return [new_gl([invert(p0)]), zero, r0]
  end

  s0, s1 = new_gl([invert(p0)]), zero
  t0, t1 = zero, new_gl([invert(p1)])

  while true
    (qq, rr) = r0.div(r1)

    if rr.zero?
      break
    end

    r0 = r1
    (c, r1) = rr.monic

    inv = invert(c)

    s = s0 - s1*qq
    t = t0 - t1*qq

    s0 = s1
    s1 = s*inv

    t0 = t1
    t1 = t*inv
  end

  return [s1, t1, r1]
end

#gl_xObject



49
50
51
# File 'lib/symath/poly/galois.rb', line 49

def gl_x()
  new_gl([1, 0])
end

#init_from_array(arr, p, var) ⇒ Object



29
30
31
32
33
# File 'lib/symath/poly/galois.rb', line 29

def init_from_array(arr, p, var)
  @arr = arr
  @p = p
  @var = var
end

#init_from_dup(dup, p) ⇒ Object



23
24
25
26
27
# File 'lib/symath/poly/galois.rb', line 23

def init_from_dup(dup, p)
  @arr = dup.arr.map { |t| t % p }
  @p = p
  @var = dup.var
end

#invert(a) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/symath/poly/galois.rb', line 72

def invert(a)
  p = @p
  raise "No inverse - #{a} and #{p} not coprime" unless a.gcd(p) == 1

  return p if p == 1

  m0, inv, x0 = p, 1, 0

  while a > 1
    inv -= (a / p) * x0
    a, p = p, a % p
    inv, x0 = x0, inv
  end
  
  inv += m0 if inv < 0

  return inv
end

#lshift(n) ⇒ Object



609
610
611
612
613
614
615
# File 'lib/symath/poly/galois.rb', line 609

def lshift(n)
  if zero?
    return zero
  else
    return new_gl(@arr + [0]*n)
  end
end

#monicObject

Divide coefficients by lc



387
388
389
390
391
392
393
394
# File 'lib/symath/poly/galois.rb', line 387

def monic()
  if zero?
    return self.clone
  end

  c = lc
  return [c, mul_ground(invert(c))]
end

#mul(g) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/symath/poly/galois.rb', line 497

def mul(g)
  df = degree
  dg = g.degree

  dh = df + dg
  if dh > 0
    h = [0]*(dh + 1)
  else
    h = []
  end

  (0..dh).each do |i|
    coeff = 0

    a = [0, i - dg].max
    b = [i, df].min
    (a..b).each do |j|
      coeff += self[j]*g[i - j]
    end

    h[i] = coeff % p
  end

  ret = new_gl(h)
  ret.strip!
  return ret
end

#mul_ground(a) ⇒ Object



525
526
527
528
529
530
531
# File 'lib/symath/poly/galois.rb', line 525

def mul_ground(a)
  if a == 0
    return zero
  else
    return new_gl(@arr.map { |e| a*e % @p})
  end
end

#negObject

Return the negative of the polynomial



493
494
495
# File 'lib/symath/poly/galois.rb', line 493

def neg()
  return new_dup(@arr.map { |t| -t % @p })
end

#new_gl(arr) ⇒ Object

Convenience method for creating a new gl of the same order and the same variable



37
38
39
# File 'lib/symath/poly/galois.rb', line 37

def new_gl(arr)
  return SyMath::Poly::Galois.new({ :arr => arr, :p => @p, :var => @var })
end

#oneObject



45
46
47
# File 'lib/symath/poly/galois.rb', line 45

def one()
  return new_gl([1])
end

#pow_mod(n, g) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/symath/poly/galois.rb', line 416

def pow_mod(n, g)
  if n == 0
    return one
  elsif n == 1
    return self % g
  elsif n == 2
    return self**2 % g
  end

  f = self
  h = one

  while true
    if n.odd?
      h = (h*f) % g
      n -= 1
    end

    n >>= 1

    if n == 0
      break
    end

    f = f**2 % g
  end

  return h
end

#pow_pnm1d2(n, g, b) ⇒ Object

Compute f**(p**n - 1) / 2 in GF(p)/(g) (Utility function for edf_zassenhaus)



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/symath/poly/galois.rb', line 190

def pow_pnm1d2(n, g, b)
  f = rem(g)
  h = f
  r = f

  (n - 1).times do
    h = h.frobenius_map(g, b)
    r = (r*h) % g
  end

  return r.pow_mod((@p - 1)/2, g)
end

#quo(g) ⇒ Object



564
565
566
# File 'lib/symath/poly/galois.rb', line 564

def quo(g)
  return div(g)[0]
end

#random_gl(n) ⇒ Object

Generate random polynomial of degree n



284
285
286
287
288
# File 'lib/symath/poly/galois.rb', line 284

def random_gl(n)
  ret = [1]
  (n).times { ret << rand(@p) }
  return new_gl(ret)
end

#rem(f) ⇒ Object



568
569
570
# File 'lib/symath/poly/galois.rb', line 568

def rem(f)
  return div(f)[1]
end

#sqf_listObject



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
# File 'lib/symath/poly/galois.rb', line 114

def sqf_list()
  n = 1
  sqf = false
  factors = []
  r = @p

  (lc, f) = monic

  if degree < 1
    return [lc, []]
  end

  f = self
  
  while true
    ff = f.diff

    if !ff.zero?
      g = f.gcd(ff)
      h = f / g
    
      i = 1
    
      while h.arr != [1]
        gg = g.gcd(h)
        hh = h / gg

        if hh.degree > 0
          factors << [hh, i*n]
        end

        g = g / gg
        h = gg
        i += 1
      end

      if g.arr == [1]
        sqf = true
      else
        f = g
      end
    end

    if !sqf
      d = f.degree/r

      f = new_gl((0..d).map { |i| f[i*r] })
      n = n*r
    else
      break
    end
  end

  return [lc, factors]
end

#sqf_pObject

Return true if polynomial is square free



104
105
106
107
108
109
110
111
112
# File 'lib/symath/poly/galois.rb', line 104

def sqf_p()
  (lc, f) = monic

  if f.zero?
    return true
  else
    return f.gcd(f.diff).arr == [1]
  end
end

#sqrObject



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/symath/poly/galois.rb', line 533

def sqr()
  d = degree
  dh = 2*d
  h = []

  (0..dh).each do |i|
    coeff = 0

    jmin = [0, i - d].max
    jmax = [i, d].min

    n = jmax - jmin + 1
    jmax = jmin + n/2 - 1

    (jmin..jmax).each do |j|
      coeff += self[j]*self[i - j]
    end

    coeff += coeff

    if n.odd?
      elem = self[jmax + 1]
      coeff += elem**2
    end

    h << coeff % @p
  end

  return new_gl(h).strip!
end

#sub(g) ⇒ Object

Subtract a polynomial from this one



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/symath/poly/galois.rb', line 466

def sub(g)
  ret = @arr.clone

  rd = degree
  gd = g.degree

  if gd > rd
    (gd - rd).times { ret.unshift(0) }
    rd += gd - rd
  end

  (0..gd).each do |i|
    ret[rd - i] = (ret[rd - i] - g[gd - i]) % @p
  end
  
  return new_gl(ret).strip!
end

#sub_ground(a) ⇒ Object



488
489
490
# File 'lib/symath/poly/galois.rb', line 488

def sub_ground(a)
  return sub(new_gl([a]))
end

#to_dupObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/symath/poly/galois.rb', line 60

def to_dup()
  ret = @arr.map do |e|
    if e <= @p / 2
      e
    else
      e - @p
    end
  end
  
  return SyMath::Poly::DUP.new({ :arr => ret, :var => @var })
end

#to_sObject



617
618
619
# File 'lib/symath/poly/galois.rb', line 617

def to_s()
  return @arr.to_s + '/' + @p.to_s
end

#zassenhausObject



329
330
331
332
333
334
335
336
337
# File 'lib/symath/poly/galois.rb', line 329

def zassenhaus()
  factors = []

  ddf_zassenhaus.each do |f|
    factors += f[0].edf_zassenhaus(f[1])
  end
  
  return sort_factors(factors)
end

#zeroObject



41
42
43
# File 'lib/symath/poly/galois.rb', line 41

def zero()
  return new_gl([])
end