/
gs_decoder.py
840 lines (660 loc) · 34.6 KB
/
gs_decoder.py
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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
524
525
526
527
528
529
530
531
532
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
563
564
565
566
567
568
569
570
571
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
r"""
Guruswami-Sudan decoder for Generalized Reed-Solomon codes
REFERENCES:
.. [GS99] Venkatesan Guruswami and Madhu Sudan, Improved Decoding of
Reed-Solomon Codes and Algebraic-Geometric Codes, 1999
.. [N13] Johan S. R. Nielsen, List Decoding of Algebraic Codes, Ph.D.
Thesis, Technical University of Denmark, 2013
AUTHORS:
- Johan S. R. Nielsen, original implementation (see [Nielsen]_ for details)
- David Lucas, ported the original implementation in Sage
"""
#*****************************************************************************
# Copyright (C) 2015 David Lucas <david.lucas@inria.fr>
# 2015 Johan S. R. Nielsen <jsrn@jsrn.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
from sage.coding.grs import GeneralizedReedSolomonCode
from sage.modules.free_module_element import vector
from sage.rings.integer_ring import ZZ
from sage.coding.decoder import Decoder
from sage.coding.guruswami_sudan.interpolation import gs_interpolation_linalg, gs_interpolation_lee_osullivan
from sage.coding.guruswami_sudan.utils import (johnson_radius,
gilt,
solve_degree2_to_integer_range)
from sage.functions.other import binomial, floor, sqrt
def n_k_params(C, n_k):
r"""
Internal helper function for the GRSGuruswamiSudanDecoder class for allowing to
specify either a GRS code `C` or the length and dimensions `n, k` directly,
in all the static functions.
If neither `C` or `n,k` were specified to those functions, an appropriate
error should be raised. Otherwise, `n, k` of the code or the supplied tuple
directly is returned.
INPUT:
- ``C`` -- A GRS code or `None`
- ``n_k`` -- A tuple `(n,k)` being length and dimension of a GRS code, or `None`.
OUTPUT:
- ``n_k`` -- A tuple `(n,k)` being length and dimension of a GRS code.
EXAMPLES::
sage: from sage.coding.guruswami_sudan.gs_decoder import n_k_params
sage: n_k_params(None, (10, 5))
(10, 5)
sage: C = codes.GeneralizedReedSolomonCode(GF(11).list()[:10], 5)
sage: n_k_params(C,None)
(10, 5)
sage: n_k_params(None,None)
Traceback (most recent call last):
...
ValueError: Please provide either the code or its length and dimension
sage: n_k_params(C,(12, 2))
Traceback (most recent call last):
...
ValueError: Please provide only the code or its length and dimension
"""
if C is not None and n_k is not None:
raise ValueError("Please provide only the code or its length and dimension")
elif C is None and n_k is None:
raise ValueError("Please provide either the code or its length and dimension")
elif C is not None:
return C.length(), C.dimension()
elif n_k is not None and not isinstance(n_k, tuple):
raise ValueError("n_k has to be a tuple")
elif n_k is not None:
return n_k
def roth_ruckenstein_root_finder(p, maxd=None, precision=None):
"""
Wrapper for Roth-Ruckenstein algorithm to compute the roots of a polynomial
with coefficients in ``F[x]``.
TESTS::
sage: from sage.coding.guruswami_sudan.gs_decoder import roth_ruckenstein_root_finder
sage: R.<x> = GF(13)[]
sage: S.<y> = R[]
sage: p = (y - x^2 - x - 1) * (y + x + 1)
sage: roth_ruckenstein_root_finder(p, maxd = 2)
[12*x + 12, x^2 + x + 1]
"""
gens = p.parent().gens()
if len(gens) == 2:
p = p.polynomial(gens[1])
return p.roots(multiplicities=False, degree_bound=maxd, algorithm="Roth-Ruckenstein")
class GRSGuruswamiSudanDecoder(Decoder):
r"""
The Guruswami-Sudan list-decoding algorithm for decoding Generalized
Reed-Solomon codes.
The Guruswami-Sudan algorithm is a polynomial time algorithm to decode
beyond half the minimum distance of the code. It can decode up to the
Johnson radius which is `n - \sqrt(n(n-d))`, where `n, d` is the length,
respectively minimum distance of the RS code. See [GS99] for more details.
It is a list-decoder meaning that it returns a list of all closest codewords
or their corresponding message polynomials. Note that the output of the
``decode_to_code`` and ``decode_to_message`` methods are therefore lists.
The algorithm has two free parameters, the list size and the multiplicity,
and these determine how many errors the method will correct: generally,
higher decoding radius requires larger values of these parameters. To decode
all the way to the Johnson radius, one generally needs values in the order
of `O(n^2)`, while decoding just one error less requires just `O(n)`.
This class has static methods for computing choices of parameters given the
decoding radius or vice versa.
The Guruswami-Sudan consists of two computationally intensive steps:
Interpolation and Root finding, either of which can be completed in multiple
ways. This implementation allows choosing the sub-algorithms among currently
implemented possibilities, or supplying your own.
INPUT:
- ``code`` -- A code associated to this decoder.
- ``tau`` -- (default: ``None``) an integer, the number of errors one wants the
Guruswami-Sudan algorithm to correct.
- ``parameters`` -- (default: ``None``) a pair of integers, where:
- the first integer is the multiplicity parameter, and
- the second integer is the list size parameter.
- ``interpolation_alg`` -- (default: ``None``) the interpolation algorithm
that will be used. The following possibilities are currently available:
* ``"LinearAlgebra"`` -- uses a linear system solver.
* ``"LeeOSullivan"`` -- uses Lee O'Sullivan method based on row reduction of a matrix
* ``None`` -- one of the above will be chosen based on the size of the
code and the parameters.
You can also supply your own function to perform the interpolation. See
NOTE section for details on the signature of this function.
- ``root_finder`` -- (default: ``None``) the rootfinding algorithm that will
be used. The following possibilities are currently available:
* ``"RothRuckenstein"`` -- uses Roth-Ruckenstein algorithm.
* ``None`` -- one of the above will be chosen based on the size of the
code and the parameters.
You can also supply your own function to perform the interpolation. See
NOTE section for details on the signature of this function.
.. NOTE::
One has to provide either ``tau`` or ``parameters``. If neither are given,
an exception will be raised.
If one provides a function as ``root_finder``, its signature has to be:
``my_rootfinder(Q, maxd=default_value, precision=default_value)``. `Q`
will be given as an element of `F[x][y]`. The function must return the
roots as a list of polynomials over a univariate polynomial ring. See
:meth:`roth_ruckenstein_root_finder` for an example.
If one provides a function as ``interpolation_alg``, its signature has
to be: ``my_inter(interpolation_points, tau, s_and_l, wy)``. See
:meth:`sage.coding.guruswami_sudan.interpolation.gs_interpolation_linalg`
for an example.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau = 97)
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
One can specify multiplicity and list size instead of ``tau``::
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, parameters = (1,2))
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
One can pass a method as ``root_finder`` (works also for ``interpolation_alg``)::
sage: from sage.coding.guruswami_sudan.gs_decoder import roth_ruckenstein_root_finder
sage: rf = roth_ruckenstein_root_finder
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, parameters = (1,2), root_finder = rf)
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
If one wants to use the native Sage algorithms for the root finding step,
one can directly pass the string given in the ``Input`` block of this class.
This works for ``interpolation_alg`` as well::
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, parameters = (1,2), root_finder="RothRuckenstein")
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
Actually, we can construct the decoder from ``C`` directly::
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
"""
####################### static methods ###############################
@staticmethod
def parameters_given_tau(tau, C = None, n_k = None):
r"""
Returns the smallest possible multiplicity and list size given the
given parameters of the code and decoding radius.
INPUT:
- ``tau`` -- an integer, number of errors one wants the Guruswami-Sudan
algorithm to correct
- ``C`` -- (default: ``None``) a :class:`GeneralizedReedSolomonCode`
- ``n_k`` -- (default: ``None``) a pair of integers, respectively the
length and the dimension of the :class:`GeneralizedReedSolomonCode`
OUTPUT:
- ``(s, l)`` -- a pair of integers, where:
- ``s`` is the multiplicity parameter, and
- ``l`` is the list size parameter.
.. NOTE::
One should to provide either ``C`` or ``(n, k)``. If neither or both
are given, an exception will be raised.
EXAMPLES::
sage: tau, n, k = 97, 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
(1, 2)
Another example with a bigger decoding radius::
sage: tau, n, k = 118, 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
(47, 89)
Choosing a decoding radius which is too large results in an errors::
sage: tau = 200
sage: codes.decoders.GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
Traceback (most recent call last):
...
ValueError: The decoding radius must be less than the Johnson radius (which is 118.66)
"""
n,k = n_k_params(C, n_k)
johnson = johnson_radius(n, n - k + 1)
if tau >= johnson:
raise ValueError("The decoding radius must be less than the Johnson radius (which is %.2f)"
% float(johnson))
# We start with l=1 and check if a satisfiable s can be chosen. We keep
# increasing l by 1 until this is the case. The governing equation is
# s*(s+1)/2 * n < (l+1)*s*(n-tau) - l*(l+1)/2*(k-1)
# See [GS99]
def try_l(l):
(mins,maxs) = solve_degree2_to_integer_range(n, n-2*(l+1)*(n-tau), (k-1)*l*(l+1))
if maxs > 0 and maxs >= mins:
return max(1, mins)
else:
return None
s, l = None, 0
while s is None:
l += 1
s = try_l(l)
return (s, l)
@staticmethod
def guruswami_sudan_decoding_radius(C = None, n_k = None, l = None, s = None):
r"""
Returns the maximal decoding radius of the Guruswami-Sudan decoder and
the parameter choices needed for this.
If ``s`` is set but ``l`` is not it will return the best decoding radius using this ``s``
alongside with the required ``l``. Vice versa for ``l``. If both are
set, it returns the decoding radius given this parameter choice.
INPUT:
- ``C`` -- (default: ``None``) a :class:`GeneralizedReedSolomonCode`
- ``n_k`` -- (default: ``None``) a pair of integers, respectively the
length and the dimension of the :class:`GeneralizedReedSolomonCode`
- ``s`` -- (default: ``None``) an integer, the multiplicity parameter of Guruswami-Sudan algorithm
- ``l`` -- (default: ``None``) an integer, the list size parameter
.. NOTE::
One has to provide either ``C`` or ``n_k``. If none or both are
given, an exception will be raised.
OUTPUT:
- ``(tau, (s, l))`` -- where
- ``tau`` is the obtained decoding radius, and
- ``s, ell`` are the multiplicity parameter, respectively list size
parameter giving this radius.
EXAMPLES::
sage: n, k = 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder.guruswami_sudan_decoding_radius(n_k = (n, k))
(118, (47, 89))
One parameter can be restricted at a time::
sage: n, k = 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder.guruswami_sudan_decoding_radius(n_k = (n, k), s=3)
(109, (3, 5))
sage: codes.decoders.GRSGuruswamiSudanDecoder.guruswami_sudan_decoding_radius(n_k = (n, k), l=7)
(111, (4, 7))
The function can also just compute the decoding radius given the parameters::
sage: codes.decoders.GRSGuruswamiSudanDecoder.guruswami_sudan_decoding_radius(n_k = (n, k), s=2, l=6)
(92, (2, 6))
"""
n,k = n_k_params(C, n_k)
def get_tau(s,l):
"Return the decoding radius given this s and l"
if s<=0 or l<=0:
return -1
return gilt(n - n/2*(s+1)/(l+1) - (k-1)/2*l/s)
if l ==None and s==None:
tau = gilt(johnson_radius(n, n - k + 1))
return (tau, GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k)))
if l!=None and s!=None:
return (get_tau(s,l), (s,l))
# Either s or l is set, but not both. First a shared local function
def find_integral_max(real_max, f):
"""Given a real (local) maximum of a function `f`, return that of
the integers around `real_max` which gives the (local) integral
maximum, and the value of at that point."""
if real_max in ZZ:
int_max = Integer(real_max)
return (int_max, f(int_max))
else:
x_f = floor(real_max)
x_c = x_f + 1
f_f, f_c = f(x_f), f(x_c)
return (x_f, f_f) if f_f >= f_c else (x_c, f_c)
if s!= None:
# maximising tau under condition
# n*(s+1 choose 2) < (ell+1)*s*(n-tau) - (ell+1 choose 2)*(k-1)
# knowing n and s, we can just minimise
# ( n*(s+1 choose 2) + (ell+1 choose 2)*(k-1) )/(ell+1)
# Differentiating and setting to zero yields ell best choice:
lmax = sqrt(n*s*(s+1.)/(k-1.)) - 1.
#the best integral value will be
(l,tau) = find_integral_max(lmax, lambda l: get_tau(s,l))
#Note that we have not proven that this ell is minimial in integral
#sense! It just seems that this most often happens
return (tau,(s,l))
if l!= None:
# Acquired similarly to when restricting s
smax = sqrt((k-1.)/n*l*(l+1.))
(s,tau) = find_integral_max(smax, lambda s: get_tau(s,l))
return (tau, (s,l))
@staticmethod
def _suitable_parameters_given_tau(tau, C = None, n_k = None):
r"""
Return quite good multiplicity and list size parameters for the code
parameters and the decoding radius.
These parameters are not guaranteed to be the best ones possible
for the provided ``tau``, but arise from easily-evaluated closed
expressions and are very good approximations of the best ones.
See [N13]_ pages 53-54, proposition 3.11 for details.
INPUT:
- ``tau`` -- an integer, number of errors one wants the Guruswami-Sudan
algorithm to correct
- ``C`` -- (default: ``None``) a :class:`GeneralizedReedSolomonCode`
- ``n_k`` -- (default: ``None``) a pair of integers, respectively the
length and the dimension of the :class:`GeneralizedReedSolomonCode`
OUTPUT:
- ``(s, l)`` -- a pair of integers, where:
- ``s`` is the multiplicity parameter, and
- ``l`` is the list size parameter.
..NOTE::
One has to provide either ``C`` or ``(n, k)``. If neither or both
are given, an exception will be raised.
EXAMPLES:
The following is an example where the parameters are optimal::
sage: tau = 98
sage: n, k = 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder._suitable_parameters_given_tau(tau, n_k = (n, k))
(2, 3)
sage: codes.decoders.GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
(2, 3)
This is an example where they are not::
sage: tau = 97
sage: n, k = 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder._suitable_parameters_given_tau(tau, n_k = (n, k))
(2, 3)
sage: codes.decoders.GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
(1, 2)
We can provide a GRS code instead of `n` and `k` directly::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: codes.decoders.GRSGuruswamiSudanDecoder._suitable_parameters_given_tau(tau, C = C)
(2, 3)
Another one with a bigger ``tau``::
sage: codes.decoders.GRSGuruswamiSudanDecoder._suitable_parameters_given_tau(118, C = C)
(47, 89)
"""
n,k = n_k_params(C, n_k)
w = k - 1
atau = n - tau
smin = tau * w / (atau ** 2 - n * w)
s = floor(1 + smin)
D = (s - smin) * (atau ** 2 - n * w) * s + (w**2) /4
l = floor(atau / w * s + 0.5 - sqrt(D)/w)
return (s, l)
@staticmethod
def gs_satisfactory(tau, s, l, C = None, n_k = None):
r"""
Returns whether input parameters satisfy the governing equation of
Guruswami-Sudan.
See [N13]_ page 49, definition 3.3 and proposition 3.4 for details.
INPUT:
- ``tau`` -- an integer, number of errrors one expects Guruswami-Sudan algorithm
to correct
- ``s`` -- an integer, multiplicity parameter of Guruswami-Sudan algorithm
- ``l`` -- an integer, list size parameter
- ``C`` -- (default: ``None``) a :class:`GeneralizedReedSolomonCode`
- ``n_k`` -- (default: ``None``) a tuple of integers, respectively the
length and the dimension of the :class:`GeneralizedReedSolomonCode`
..NOTE::
One has to provide either ``C`` or ``(n, k)``. If none or both are
given, an exception will be raised.
EXAMPLES::
sage: tau, s, l = 97, 1, 2
sage: n, k = 250, 70
sage: codes.decoders.GRSGuruswamiSudanDecoder.gs_satisfactory(tau, s, l, n_k = (n, k))
True
One can also pass a GRS code::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: codes.decoders.GRSGuruswamiSudanDecoder.gs_satisfactory(tau, s, l, C = C)
True
Another example where ``s`` and ``l`` does not satisfy the equation::
sage: tau, s, l = 118, 47, 80
sage: codes.decoders.GRSGuruswamiSudanDecoder.gs_satisfactory(tau, s, l, n_k = (n, k))
False
If one provides both ``C`` and ``n_k`` an exception is returned::
sage: tau, s, l = 97, 1, 2
sage: n, k = 250, 70
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: codes.decoders.GRSGuruswamiSudanDecoder.gs_satisfactory(tau, s, l, C = C, n_k = (n, k))
Traceback (most recent call last):
...
ValueError: Please provide only the code or its length and dimension
Same if one provides none of these::
sage: codes.decoders.GRSGuruswamiSudanDecoder.gs_satisfactory(tau, s, l)
Traceback (most recent call last):
...
ValueError: Please provide either the code or its length and dimension
"""
n,k = n_k_params(C, n_k)
return l > 0 and s > 0 and n * s * (s+1) < (l+1) * (2*s*(n-tau) - (k-1) * l)
####################### decoder itself ###############################
def __init__(self, code, tau = None, parameters = None, interpolation_alg = None, root_finder = None):
r"""
TESTS:
If neither ``tau`` nor ``parameters`` is given, an exception is returned::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C)
Traceback (most recent call last):
...
ValueError: Specify either tau or parameters
If one provides something else than one of the allowed strings or a method as ``interpolation_alg``,
an exception is returned::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau = 97, interpolation_alg = 42)
Traceback (most recent call last):
...
ValueError: Please provide a method or one of the allowed strings for interpolation_alg
Same thing for ``root_finder``::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau = 97, root_finder = "FortyTwo")
Traceback (most recent call last):
...
ValueError: Please provide a method or one of the allowed strings for root_finder
If one provides a full set of parameters (tau, s and l) which are not satisfactory, an
error message is returned::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau = 142, parameters=(1, 2))
Traceback (most recent call last):
...
ValueError: Impossible parameters for the Guruswami-Sudan algorithm
If ``code`` is not a GRS code, an error is raised::
sage: C = codes.random_linear_code(GF(11), 10, 4)
sage: codes.decoders.GRSGuruswamiSudanDecoder(C, tau = 2)
Traceback (most recent call last):
...
ValueError: code has to be a generalized Reed-Solomon code
"""
if not isinstance(code, GeneralizedReedSolomonCode):
raise ValueError("code has to be a generalized Reed-Solomon code")
n, k = code.length(), code.dimension()
if tau and parameters:
if not GRSGuruswamiSudanDecoder.gs_satisfactory(tau, parameters[0], parameters[1], C = code):
raise ValueError("Impossible parameters for the Guruswami-Sudan algorithm")
self._tau, self._s, self._ell = tau, parameters[0], parameters[1]
elif tau:
self._tau = tau
self._s, self._ell = GRSGuruswamiSudanDecoder.parameters_given_tau(tau, n_k = (n, k))
elif parameters:
self._s = parameters[0]
self._ell = parameters[1]
(self._tau,_) = GRSGuruswamiSudanDecoder.guruswami_sudan_decoding_radius(C = code, s=self._s, l=self._ell)
else:
raise ValueError("Specify either tau or parameters")
if hasattr(interpolation_alg, '__call__'):
self._interpolation_alg = interpolation_alg
elif interpolation_alg == None or interpolation_alg == "LeeOSullivan":
self._interpolation_alg = gs_interpolation_lee_osullivan
elif interpolation_alg == "LinearAlgebra":
self._interpolation_alg = gs_interpolation_linalg
else:
raise ValueError("Please provide a method or one of the allowed strings for interpolation_alg")
if hasattr(root_finder, '__call__'):
self._root_finder = root_finder
elif root_finder == None or root_finder == "RothRuckenstein":
self._root_finder = roth_ruckenstein_root_finder
else:
raise ValueError("Please provide a method or one of the allowed strings for root_finder")
super(GRSGuruswamiSudanDecoder, self).__init__(code, code.ambient_space(), "EvaluationPolynomial")
def _repr_(self):
r"""
Returns a string representation of ``self``.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D
Guruswami-Sudan decoder for [250, 70, 181] Generalized Reed-Solomon Code over Finite Field of size 251 decoding 97 errors with parameters (1, 2)
"""
return "Guruswami-Sudan decoder for %s decoding %s errors with parameters %s" % (self.code(), self.decoding_radius(), (self.multiplicity(), self.list_size()))
def _latex_(self):
r"""
Returns a string representation of ``self``.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: latex(D)
\textnormal{Guruswami-Sudan decoder for } [250, 70, 181] \textnormal{ Generalized Reed-Solomon Code over } \Bold{F}_{251}\textnormal{ decoding }97\textnormal{ errors with parameters }(1, 2)
"""
return "\\textnormal{Guruswami-Sudan decoder for } %s\\textnormal{ decoding }%s\\textnormal{ errors with parameters }%s" % (self.code()._latex_(), self.decoding_radius(), (self.multiplicity(), self.list_size()))
def __eq__(self, other):
r"""
Tests equality between GRSGuruswamiSudanDecoder objects.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D1 = C.decoder("GuruswamiSudan", tau = 97)
sage: D2 = C.decoder("GuruswamiSudan", tau = 97)
sage: D1.__eq__(D2)
True
"""
return isinstance(other, GRSGuruswamiSudanDecoder)\
and self.code() == other.code()\
and self.decoding_radius() == other.decoding_radius()\
and self.multiplicity() == other.multiplicity()\
and self.list_size() == other.list_size()\
and self.interpolation_algorithm() == other.interpolation_algorithm()\
and self.rootfinding_algorithm() == other.rootfinding_algorithm()
def interpolation_algorithm(self):
r"""
Returns the interpolation algorithm that will be used.
Remember that its signature has to be:
``my_inter(interpolation_points, tau, s_and_l, wy)``.
See :meth:`sage.coding.guruswami_sudan.interpolation.gs_interpolation_linalg`
for an example.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.interpolation_algorithm()
<function gs_interpolation_lee_osullivan at 0x...>
"""
return self._interpolation_alg
def rootfinding_algorithm(self):
r"""
Returns the rootfinding algorithm that will be used.
Remember that its signature has to be:
``my_rootfinder(Q, maxd=default_value, precision=default_value)``.
See :meth:`roth_ruckenstein_root_finder`
for an example.
EXAMPLES::
sage: from sage.coding.guruswami_sudan.gs_decoder import roth_ruckenstein_root_finder
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.rootfinding_algorithm()
<function roth_ruckenstein_root_finder at 0x...>
"""
return self._root_finder
def parameters(self):
r"""
Returns the multiplicity and list size parameters of ``self``.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.parameters()
(1, 2)
"""
return (self._s, self._ell)
def multiplicity(self):
r"""
Returns the multiplicity parameter of ``self``.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.multiplicity()
1
"""
return self._s
def list_size(self):
r"""
Returns the list size parameter of ``self``.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.list_size()
2
"""
return self._ell
def decode_to_message(self, r):
r"""
Decodes ``r`` to the list of polynomials whose encoding by
:meth:`self.code()` is within Hamming distance
:meth:`self.decoding_radius` of ``r``.
INPUT:
- ``r`` -- a received word, i.e. a vector in `F^n` where `F` and `n` are
the base field respectively length of :meth:`self.code`.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(17).list()[:15], 6)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau=5)
sage: F.<x> = GF(17)[]
sage: m = 13*x^4 + 7*x^3 + 10*x^2 + 14*x + 3
sage: c = D.connected_encoder().encode(m)
sage: r = vector(GF(17), [3,13,12,0,0,7,5,1,8,11,15,12,14,7,10])
sage: (c-r).hamming_weight()
5
sage: messages = D.decode_to_message(r)
sage: len(messages)
2
sage: m in messages
True
TESTS:
If one has provided a method as a ``root_finder`` or a ``interpolation_alg`` which
does not fit the allowed signature, an exception will be raised::
sage: C = codes.GeneralizedReedSolomonCode(GF(17).list()[:15], 6)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau=5, root_finder=next_prime)
sage: F.<x> = GF(17)[]
sage: m = 9*x^5 + 10*x^4 + 9*x^3 + 7*x^2 + 15*x + 2
sage: c = D.connected_encoder().encode(m)
sage: r = vector(GF(17), [3,1,4,2,14,1,0,4,13,12,1,16,1,13,15])
sage: m in D.decode_to_message(r)
Traceback (most recent call last):
...
ValueError: The provided root-finding algorithm has a wrong signature. See the documentation of `codes.decoders.GRSGuruswamiSudanDecoder.rootfinding_algorithm()` for details
"""
return [self.connected_encoder().unencode(c) for c in self.decode_to_code(r)]
def decode_to_code(self, r):
r"""
Return the list of all codeword within radius :meth:`self.decoding_radius` of the received word `r`.
INPUT:
- ``r`` -- a received word, i.e. a vector in `F^n` where `F` and `n` are
the base field respectively length of :meth:`self.code`.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(17).list()[:15], 6)
sage: D = codes.decoders.GRSGuruswamiSudanDecoder(C, tau=5)
sage: c = vector(GF(17), [3,13,12,0,0,7,5,1,8,11,1,9,4,12,14])
sage: c in C
True
sage: r = vector(GF(17), [3,13,12,0,0,7,5,1,8,11,15,12,14,7,10])
sage: r in C
False
sage: codewords = D.decode_to_code(r)
sage: len(codewords)
2
sage: c in codewords
True
"""
C = self.code()
n, k, d, alphas, colmults, s, l = C.length(), C.dimension(), C.minimum_distance(),\
C.evaluation_points(), C.column_multipliers(), self.multiplicity(), self.list_size()
tau = self.decoding_radius()
## SETUP INTERPOLATION PROBLEM
wy = k-1
points = [(alphas[i], r[i]/colmults[i]) for i in range(0,len(alphas))]
## SOLVE INTERPOLATION
try:
Q = self.interpolation_algorithm()(points, tau, (s,l), wy)
except TypeError:
raise ValueError("The provided interpolation algorithm has a wrong signature. See the documentation of `codes.decoders.GRSGuruswamiSudanDecoder.interpolation_algorithm()` for details")
## EXAMINE THE FACTORS AND CONVERT TO CODEWORDS
try:
polynomials = self.rootfinding_algorithm()(Q, maxd = wy)
except TypeError:
raise ValueError("The provided root-finding algorithm has a wrong signature. See the documentation of `codes.decoders.GRSGuruswamiSudanDecoder.rootfinding_algorithm()` for details")
if not polynomials:
return None
E = self.connected_encoder()
codewords = [ E.encode(f) for f in polynomials]
# Root-finding might find spurious roots. Return only the ones which give nearby codewords
return [ c for c in codewords if (r - c).hamming_weight() <= tau ]
def decoding_radius(self):
r"""
Returns the maximal number of errors that ``self`` is able to correct.
EXAMPLES::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", tau = 97)
sage: D.decoding_radius()
97
An example where tau is not one of the inputs to the constructor::
sage: C = codes.GeneralizedReedSolomonCode(GF(251).list()[:250], 70)
sage: D = C.decoder("GuruswamiSudan", parameters = (2,4))
sage: D.decoding_radius()
105
"""
return self._tau
####################### registration ###############################
GeneralizedReedSolomonCode._registered_decoders["GuruswamiSudan"] = GRSGuruswamiSudanDecoder
GRSGuruswamiSudanDecoder._decoder_type = {"list-decoder", "always-succeed", "hard-decision"}