forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finfield.c
1640 lines (1392 loc) · 47.8 KB
/
finfield.c
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the functions to compute with elements from small
** finite fields.
**
** The concepts of this kernel module are documented in finfield.h
*/
#include "finfield.h"
#include "ariths.h"
#include "bool.h"
#include "calls.h"
#include "error.h"
#include "finfield_conway.h"
#include "gvars.h"
#include "io.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#ifdef HPCGAP
#include "hpc/aobjects.h"
#include "hpc/thread.h"
#endif
/****************************************************************************
**
*V SuccFF . . . . . Tables for finite fields which are computed on demand
*V TypeFF
*V TypeFF0
**
** SuccFF holds a plain list of successor lists.
** TypeFF holds the types of typical elements of the finite fields.
** TypeFF0 holds the types of the zero elements of the finite fields.
*/
Obj SuccFF;
static Obj TypeFF;
static Obj TypeFF0;
/****************************************************************************
**
*V TYPE_FFE . . . . . kernel copy of GAP function TYPE_FFE
*V TYPE_FFE0 . . . . . kernel copy of GAP function TYPE_FFE0
**
** These GAP functions are called to compute types of finite field elemnents
*/
static Obj TYPE_FFE;
static Obj TYPE_FFE0;
/****************************************************************************
**
*V PrimitiveRootMod
**
** Local copy of GAP function PrimitiveRootMod, used when initializing new
** fields.
*/
static Obj PrimitiveRootMod;
/****************************************************************************
**
*F LookupPrimePower(<q>) . . . . . . . . search for a prime power in tables
**
** Searches the tables of prime powers from ffdata.c for q, returns the
** index of q in SizeFF if it is present and 0 if not (the 0 position of
** SizeFF is unused).
*/
static FF LookupPrimePower(UInt q)
{
UInt l, n;
FF ff;
UInt e;
// search through the finite field table
l = 1;
n = NUM_SHORT_FINITE_FIELDS;
ff = 0;
while (l <= n && SizeFF[l] <= q && q <= SizeFF[n]) {
// interpolation search
/* cuts iterations roughly in half compared to binary search at
* the expense of additional divisions. */
e = (q - SizeFF[l] + 1) * (n - l) / (SizeFF[n] - SizeFF[l] + 1);
ff = l + e;
if (SizeFF[ff] == q)
break;
if (SizeFF[ff] < q)
l = ff + 1;
else
n = ff - 1;
}
if (ff < 1 || ff > NUM_SHORT_FINITE_FIELDS)
return 0;
if (SizeFF[ff] != q)
return 0;
return ff;
}
/****************************************************************************
**
*F FiniteField(<p>,<d>) . make the small finite field with <p>^<d> elements
*F FiniteFieldBySize(<q>) . . make the small finite field with <q> elements
**
** The work is done in the Lookup function above, and in FiniteFieldBySize
** where the successor tables are computed.
*/
FF FiniteFieldBySize(UInt q)
{
FF ff; // finite field, result
Obj tmp; // temporary bag
Obj succBag; // successor table bag
FFV * succ; // successor table
FFV * indx; // index table
UInt p; // characteristic of the field
UInt poly; // Conway polynomial of extension
UInt i, l, f, n, e; // loop variables
Obj root; // will be a primitive root mod p
ff = LookupPrimePower(q);
if (!ff)
return 0;
#ifdef HPCGAP
/* Important correctness concern here:
*
* The values of SuccFF, TypeFF, and TypeFF0 are set in that
* order, separated by write barriers. This can happen concurrently
* in a different thread.
*
* Thus, after observing that TypeFF0 has been set, we can be sure
* that the thread also sees the values of SuccFF and TypeFF.
* This is ensured by the read barrier in ATOMIC_ELM_PLIST().
*
* In the worst case, we may do the following calculations once per
* thread and throw them away for all but one thread. Correctness
* is still ensured through the use of ATOMIC_SET_ELM_PLIST_ONCE(),
* which results in all threads sharing the same types and successor
* tables.
*/
if (ATOMIC_ELM_PLIST(TypeFF0, ff))
return ff;
#else
if (ELM_PLIST(TypeFF0, ff))
return ff;
#endif
// determine the characteristic of the field
p = CHAR_FF(ff);
// allocate a bag for the successor table and one for a temporary
tmp = NewKernelBuffer(sizeof(Obj) + q * sizeof(FFV));
succBag = NewKernelBuffer(sizeof(Obj) + q * sizeof(FFV));
indx = (FFV *)(1 + ADDR_OBJ(tmp));
succ = (FFV *)(1 + ADDR_OBJ(succBag));
// if q is a prime find the smallest primitive root $e$, use $x - e$
if (DEGR_FF(ff) == 1) {
if (p < 65537) {
/* for smaller primes we do this in the kernel for performance and
bootstrapping reasons
TODO -- review the threshold */
for (e = 1, i = 1; i != p - 1; ++e) {
for (f = e, i = 1; f != 1; ++i)
f = (f * e) % p;
}
}
else {
// Otherwise we ask the library
root = CALL_1ARGS(PrimitiveRootMod, INTOBJ_INT(p));
e = INT_INTOBJ(root) + 1;
}
poly = p - (e - 1);
}
// otherwise look up the polynomial used to construct this field
else {
for (i = 0; PolsFF[i] != q; i += 2)
;
poly = PolsFF[i + 1];
}
// construct 'indx' such that 'e = x^(indx[e]-1) % poly' for every e
indx[0] = 0;
for (e = 1, n = 0; n < q - 1; ++n) {
indx[e] = n + 1;
// e =p*e mod poly =x*e mod poly =x*x^n mod poly =x^{n+1} mod poly
if (p != 2) {
f = p * (e % (q / p));
l = ((p - 1) * (e / (q / p))) % p;
e = 0;
for (i = 1; i < q; i *= p)
e = e + i * ((f / i + l * (poly / i)) % p);
}
else {
if (2 * e & q)
e = 2 * e ^ poly ^ q;
else
e = 2 * e;
}
}
// construct 'succ' such that 'x^(n-1)+1 = x^(succ[n]-1)' for every n
succ[0] = q - 1;
for (e = 1, f = p - 1; e < q; e++) {
if (e < f) {
succ[indx[e]] = indx[e + 1];
}
else {
succ[indx[e]] = indx[e + 1 - p];
f += p;
}
}
// enter the finite field in the tables
#ifdef HPCGAP
MakeBagReadOnly(succBag);
ATOMIC_SET_ELM_PLIST_ONCE(SuccFF, ff, succBag);
CHANGED_BAG(SuccFF);
tmp = CALL_1ARGS(TYPE_FFE, INTOBJ_INT(p));
ATOMIC_SET_ELM_PLIST_ONCE(TypeFF, ff, tmp);
CHANGED_BAG(TypeFF);
tmp = CALL_1ARGS(TYPE_FFE0, INTOBJ_INT(p));
ATOMIC_SET_ELM_PLIST_ONCE(TypeFF0, ff, tmp);
CHANGED_BAG(TypeFF0);
#else
ASS_LIST(SuccFF, ff, succBag);
CHANGED_BAG(SuccFF);
tmp = CALL_1ARGS(TYPE_FFE, INTOBJ_INT(p));
ASS_LIST(TypeFF, ff, tmp);
CHANGED_BAG(TypeFF);
tmp = CALL_1ARGS(TYPE_FFE0, INTOBJ_INT(p));
ASS_LIST(TypeFF0, ff, tmp);
CHANGED_BAG(TypeFF0);
#endif
// return the finite field
return ff;
}
FF FiniteField(UInt p, UInt d)
{
UInt q, i;
FF ff;
q = 1;
for (i = 1; i <= d; i++)
q *= p;
ff = FiniteFieldBySize(q);
if (ff != 0 && CHAR_FF(ff) != p)
return 0;
return ff;
}
/****************************************************************************
**
*F CommonFF(<f1>,<d1>,<f2>,<d2>) . . . . . . . . . . . . find a common field
**
** 'CommonFF' returns a small finite field that can represent elements of
** degree <d1> from the small finite field <f1> and elements of degree <d2>
** from the small finite field <f2>. Note that this is not guaranteed to be
** the smallest such field. If <f1> and <f2> have different characteristic
** or the smallest common field, is too large, 'CommonFF' returns 0.
*/
FF CommonFF (
FF f1,
UInt d1,
FF f2,
UInt d2 )
{
UInt p; // characteristic
UInt d; // degree
// trivial case first
if ( f1 == f2 ) {
return f1;
}
// get and check the characteristics
p = CHAR_FF( f1 );
if ( p != CHAR_FF( f2 ) ) {
return 0;
}
// check whether one of the fields will do
if ( DEGR_FF(f1) % d2 == 0 ) {
return f1;
}
if ( DEGR_FF(f2) % d1 == 0 ) {
return f2;
}
// compute the necessary degree
d = d1;
while ( d % d2 != 0 ) {
d += d1;
}
// try to build the field
return FiniteField( p, d );
}
/****************************************************************************
**
*F CharFFE(<ffe>) . . . . . . . . . characteristic of a small finite field
**
** 'CharFFE' returns the characteristic of the small finite field in which
** the element <ffe> lies.
*/
UInt CharFFE (
Obj ffe )
{
return CHAR_FF( FLD_FFE(ffe) );
}
static Obj FuncCHAR_FFE_DEFAULT(Obj self, Obj ffe)
{
return INTOBJ_INT( CHAR_FF( FLD_FFE(ffe) ) );
}
/****************************************************************************
**
*F DegreeFFE(<ffe>) . . . . . . . . . . . . degree of a small finite field
**
** 'DegreeFFE' returns the degree of the smallest finite field in which the
** element <ffe> lies.
*/
UInt DegreeFFE (
Obj ffe )
{
UInt d; // degree, result
FFV val; // value of element
FF fld; // field of element
UInt q; // size of field
UInt p; // char. of field
UInt m; // size of minimal field
// get the value, the field, the size, and the characteristic
val = VAL_FFE( ffe );
fld = FLD_FFE( ffe );
q = SIZE_FF( fld );
p = CHAR_FF( fld );
// the zero element has a degree of one
if ( val == 0 ) {
return 1;
}
// compute the degree
m = p;
d = 1;
while ( (q-1) % (m-1) != 0 || (val-1) % ((q-1)/(m-1)) != 0 ) {
m *= p;
d += 1;
}
// return the degree
return d;
}
static Obj FuncDEGREE_FFE_DEFAULT(Obj self, Obj ffe)
{
return INTOBJ_INT( DegreeFFE( ffe ) );
}
/****************************************************************************
**
*F TypeFFE(<ffe>) . . . . . . . . . . type of element of small finite field
**
** 'TypeFFE' returns the type of the element <ffe> of a small finite field.
**
** 'TypeFFE' is the function in 'TypeObjFuncs' for elements in small finite
** fields.
*/
Obj TypeFFE(Obj ffe)
{
Obj types = (VAL_FFE(ffe) == 0) ? TypeFF0 : TypeFF;
#ifdef HPCGAP
return ATOMIC_ELM_PLIST(types, FLD_FFE(ffe));
#else
return ELM_PLIST(types, FLD_FFE(ffe));
#endif
}
/****************************************************************************
**
*F EqFFE(<opL>,<opR>) . . . . . . . test if finite field elements are equal
**
** 'EqFFE' returns 'True' if the two finite field elements <opL> and <opR>
** are equal and 'False' othwise.
**
** This is complicated because it must account for the following situation.
** Suppose 'a' is 'Z(3)', 'b' is 'Z(3^2)^4' and finally 'c' is 'Z(3^3)^13'.
** Mathematically 'a' is equal to 'b', so we want 'a = b' to be 'true' and
** since 'a' is represented over a subfield of 'b' this is no big problem.
** Again 'a' is equal to 'c', and again we want 'a = c' to be 'true' and
** again this is no problem since 'a' is represented over a subfield of 'c'.
** Since '=' ought to be transitive we also want 'b = c' to be 'true' and
** this is a problem, because they are represented over incompatible fields.
*/
static Int EqFFE(Obj opL, Obj opR)
{
FFV vL, vR; // value of left and right
FF fL, fR; // field of left and right
UInt pL, pR; // char. of left and right
UInt qL, qR; // size of left and right
UInt mL, mR; // size of minimal field
// get the values and the fields over which they are represented
vL = VAL_FFE( opL );
vR = VAL_FFE( opR );
fL = FLD_FFE( opL );
fR = FLD_FFE( opR );
// if the elements are represented over the same field, it is easy
if ( fL == fR ) {
return (vL == vR);
}
// elements in fields of different characteristic are different too
pL = CHAR_FF( fL );
pR = CHAR_FF( fR );
if ( pL != pR ) {
return 0;
}
// the zero element is not equal to any other element
if ( vL == 0 || vR == 0 ) {
return (vL == 0 && vR == 0);
}
// compute the sizes of the minimal fields in which the elements lie
qL = SIZE_FF( fL );
mL = pL;
while ( (qL-1) % (mL-1) != 0 || (vL-1) % ((qL-1)/(mL-1)) != 0 ) mL *= pL;
qR = SIZE_FF( fR );
mR = pR;
while ( (qR-1) % (mR-1) != 0 || (vR-1) % ((qR-1)/(mR-1)) != 0 ) mR *= pR;
// elements in different fields are different too
if ( mL != mR ) {
return 0;
}
// otherwise compare the elements in the common minimal field
return ((vL-1)/((qL-1)/(mL-1)) == (vR-1)/((qR-1)/(mR-1)));
}
/****************************************************************************
**
*F LtFFE(<opL>,<opR>) . . . . . . test if finite field elements is smaller
**
** 'LtFFEFFE' returns 'True' if the finite field element <opL> is strictly
** less than the finite field element <opR> and 'False' otherwise.
*/
static Int LtFFE(Obj opL, Obj opR)
{
FFV vL, vR; // value of left and right
FF fL, fR; // field of left and right
UInt pL, pR; // char. of left and right
UInt qL, qR; // size of left and right
UInt mL, mR; // size of minimal field
// get the values and the fields over which they are represented
vL = VAL_FFE( opL );
vR = VAL_FFE( opR );
fL = FLD_FFE( opL );
fR = FLD_FFE( opR );
// elements in fields of different characteristic are not comparable
pL = CHAR_FF( fL );
pR = CHAR_FF( fR );
if ( pL != pR ) {
return (DoOperation2Args( LtOper, opL, opR ) == True);
}
// the zero element is smaller than any other element
if ( vL == 0 || vR == 0 ) {
return (vL == 0 && vR != 0);
}
// get the sizes of the fields over which the elements are written
qL = SIZE_FF( fL );
qR = SIZE_FF( fR );
// Deal quickly with the case where both elements are written over the ground field
if (qL ==pL && qR == pR)
return vL < vR;
// compute the sizes of the minimal fields in which the elements lie
mL = pL;
while ( (qL-1) % (mL-1) != 0 || (vL-1) % ((qL-1)/(mL-1)) != 0 ) mL *= pL;
mR = pR;
while ( (qR-1) % (mR-1) != 0 || (vR-1) % ((qR-1)/(mR-1)) != 0 ) mR *= pR;
// elements in smaller fields are smaller too
if ( mL != mR ) {
return (mL < mR);
}
// otherwise compare the elements in the common minimal field
return ((vL-1)/((qL-1)/(mL-1)) < (vR-1)/((qR-1)/(mR-1)));
}
/****************************************************************************
**
*F PrFFV(<fld>,<val>) . . . . . . . . . . . . . print a finite field value
**
** 'PrFFV' prints the value <val> from the finite field <fld>.
**
*/
static void PrFFV(FF fld, FFV val)
{
UInt q; // size of finite field
UInt p; // char. of finite field
UInt m; // size of minimal field
UInt d; // degree of minimal field
// get the characteristic, order of the minimal field and the degree
q = SIZE_FF( fld );
p = CHAR_FF( fld );
// print the zero
if ( val == 0 ) {
Pr("%>0*Z(%>%d%2<)", (Int)p, 0);
}
// print a nonzero element as power of the primitive root
else {
// find the degree of the minimal field in that the element lies
d = 1; m = p;
while ( (q-1) % (m-1) != 0 || (val-1) % ((q-1)/(m-1)) != 0 ) {
d++; m *= p;
}
val = (val-1) / ((q-1)/(m-1)) + 1;
// print the element
Pr("%>Z(%>%d%<", (Int)p, 0);
if ( d == 1 ) {
Pr("%<)", 0, 0);
}
else {
Pr("^%>%d%2<)", (Int)d, 0);
}
if ( val != 2 ) {
Pr("^%>%d%<", (Int)(val-1), 0);
}
}
}
/****************************************************************************
**
*F PrFFE(<ffe>) . . . . . . . . . . . . . . . print a finite field element
**
** 'PrFFE' prints the finite field element <ffe>.
*/
static void PrFFE(Obj ffe)
{
PrFFV( FLD_FFE(ffe), VAL_FFE(ffe) );
}
/****************************************************************************
**
*F SumFFEFFE(<opL>,<opR>) . . . . . . . . . . sum of finite field elements
**
** 'SumFFEFFE' returns the sum of the two finite field elements <opL> and
** <opR>. The sum is represented over the field over which the operands are
** represented, even if it lies in a much smaller field.
**
** If one of the elements is represented over a subfield of the field over
** which the other element is represented, it is lifted into the larger
** field before the addition.
**
** 'SumFFEFFE' just does the conversions mentioned above and then calls the
** macro 'SUM_FFV' to do the actual addition.
*/
static Obj SUM_FFE_LARGE;
static Obj SumFFEFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fL, fR, fX; // field of left, right, result
UInt qL, qR, qX; // size of left, right, result
// get the values, handle trivial cases
vL = VAL_FFE( opL );
vR = VAL_FFE( opR );
// bring the two operands into a common field <fX>
fL = FLD_FFE( opL );
qL = SIZE_FF( fL );
fR = FLD_FFE( opR );
qR = SIZE_FF( fR );
if ( qL == qR ) {
fX = fL;
}
else if ( qL % qR == 0 && (qL-1) % (qR-1) == 0 ) {
fX = fL;
if ( vR != 0 ) vR = (qL-1) / (qR-1) * (vR-1) + 1;
}
else if ( qR % qL == 0 && (qR-1) % (qL-1) == 0 ) {
fX = fR;
if ( vL != 0 ) vL = (qR-1) / (qL-1) * (vL-1) + 1;
}
else {
fX = CommonFF( fL, DegreeFFE(opL), fR, DegreeFFE(opR) );
if ( fX == 0 ) return CALL_2ARGS( SUM_FFE_LARGE, opL, opR );
qX = SIZE_FF( fX );
// if ( vL != 0 ) vL = (qX-1) / (qL-1) * (vL-1) + 1;
if ( vL != 0 ) vL = ((qX-1) * (vL-1)) / (qL-1) + 1;
// if ( vR != 0 ) vR = (qX-1) / (qR-1) * (vR-1) + 1;
if ( vR != 0 ) vR = ((qX-1) * (vR-1)) / (qR-1) + 1;
}
vX = SUM_FFV( vL, vR, SUCC_FF(fX) );
return NEW_FFE( fX, vX );
}
static Obj SumFFEInt(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opL );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the right operand
vX = ((INT_INTOBJ( opR ) % pX) + pX) % pX;
if ( vX == 0 ) {
vR = 0;
}
else {
vR = 1;
for ( ; 1 < vX; vX-- ) vR = sX[vR];
}
// get the left operand
vL = VAL_FFE( opL );
vX = SUM_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
static Obj SumIntFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opR );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the left operand
vX = ((INT_INTOBJ( opL ) % pX) + pX) % pX;
if ( vX == 0 ) {
vL = 0;
}
else {
vL = 1;
for ( ; 1 < vX; vX-- ) vL = sX[vL];
}
// get the right operand
vR = VAL_FFE( opR );
vX = SUM_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
/****************************************************************************
**
*F ZeroFFE(<op>) . . . . . . . . . . . . . . zero of a finite field element
*/
static Obj ZeroFFE(Obj op)
{
FF fX; // field of result
// get the field for the result
fX = FLD_FFE( op );
return NEW_FFE( fX, 0 );
}
/****************************************************************************
**
*F AInvFFE(<op>) . . . . . . . . . . additive inverse of finite field element
*/
static Obj AInvFFE(Obj op)
{
FFV v, vX; // value of operand, result
FF fX; // field of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( op );
sX = SUCC_FF( fX );
// get the operand
v = VAL_FFE( op );
vX = NEG_FFV( v, sX );
return NEW_FFE( fX, vX );
}
/****************************************************************************
**
*F DiffFFEFFE(<opL>,<opR>) . . . . . . . difference of finite field elements
**
** 'DiffFFEFFE' returns the difference of the two finite field elements
** <opL> and <opR>. The difference is represented over the field over which
** the operands are represented, even if it lies in a much smaller field.
**
** If one of the elements is represented over a subfield of the field over
** which the other element is represented, it is lifted into the larger
** field before the subtraction.
**
** 'DiffFFEFFE' just does the conversions mentioned above and then calls the
** macros 'NEG_FFV' and 'SUM_FFV' to do the actual subtraction.
*/
static Obj DIFF_FFE_LARGE;
static Obj DiffFFEFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fL, fR, fX; // field of left, right, result
UInt qL, qR, qX; // size of left, right, result
// get the values, handle trivial cases
vL = VAL_FFE( opL );
vR = VAL_FFE( opR );
// bring the two operands into a common field <fX>
fL = FLD_FFE( opL );
qL = SIZE_FF( fL );
fR = FLD_FFE( opR );
qR = SIZE_FF( fR );
if ( qL == qR ) {
fX = fL;
}
else if ( qL % qR == 0 && (qL-1) % (qR-1) == 0 ) {
fX = fL;
if ( vR != 0 ) vR = (qL-1) / (qR-1) * (vR-1) + 1;
}
else if ( qR % qL == 0 && (qR-1) % (qL-1) == 0 ) {
fX = fR;
if ( vL != 0 ) vL = (qR-1) / (qL-1) * (vL-1) + 1;
}
else {
fX = CommonFF( fL, DegreeFFE(opL), fR, DegreeFFE(opR) );
if ( fX == 0 ) return CALL_2ARGS( DIFF_FFE_LARGE, opL, opR );
qX = SIZE_FF( fX );
// if ( vL != 0 ) vL = (qX-1) / (qL-1) * (vL-1) + 1;
if ( vL != 0 ) vL = ((qX-1) * (vL-1)) / (qL-1) + 1;
// if ( vR != 0 ) vR = (qX-1) / (qR-1) * (vR-1) + 1;
if ( vR != 0 ) vR = ((qX-1) * (vR-1)) / (qR-1) + 1;
}
vR = NEG_FFV( vR, SUCC_FF(fX) );
vX = SUM_FFV( vL, vR, SUCC_FF(fX) );
return NEW_FFE( fX, vX );
}
static Obj DiffFFEInt(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opL );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the right operand
vX = ((INT_INTOBJ( opR ) % pX) + pX) % pX;
if ( vX == 0 ) {
vR = 0;
}
else {
vR = 1;
for ( ; 1 < vX; vX-- ) vR = sX[vR];
}
// get the left operand
vL = VAL_FFE( opL );
vR = NEG_FFV( vR, sX );
vX = SUM_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
static Obj DiffIntFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opR );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the left operand
vX = ((INT_INTOBJ( opL ) % pX) + pX) % pX;
if ( vX == 0 ) {
vL = 0;
}
else {
vL = 1;
for ( ; 1 < vX; vX-- ) vL = sX[vL];
}
// get the right operand
vR = VAL_FFE( opR );
vR = NEG_FFV( vR, sX );
vX = SUM_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
/****************************************************************************
**
*F ProdFFEFFE(<opL>,<opR>) . . . . . . . . product of finite field elements
**
** 'ProdFFEFFE' returns the product of the two finite field elements <opL>
** and <opR>. The product is represented over the field over which the
** operands are represented, even if it lies in a much smaller field.
**
** If one of the elements is represented over a subfield of the field over
** which the other element is represented, it is lifted into the larger
** field before the multiplication.
**
** 'ProdFFEFFE' just does the conversions mentioned above and then calls the
** macro 'PROD_FFV' to do the actual multiplication.
*/
static Obj PROD_FFE_LARGE;
static Obj ProdFFEFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fL, fR, fX; // field of left, right, result
UInt qL, qR, qX; // size of left, right, result
// get the values, handle trivial cases
vL = VAL_FFE( opL );
vR = VAL_FFE( opR );
// bring the two operands into a common field <fX>
fL = FLD_FFE( opL );
qL = SIZE_FF( fL );
fR = FLD_FFE( opR );
qR = SIZE_FF( fR );
if ( qL == qR ) {
fX = fL;
}
else if ( qL % qR == 0 && (qL-1) % (qR-1) == 0 ) {
fX = fL;
if ( vR != 0 ) vR = (qL-1) / (qR-1) * (vR-1) + 1;
}
else if ( qR % qL == 0 && (qR-1) % (qL-1) == 0 ) {
fX = fR;
if ( vL != 0 ) vL = (qR-1) / (qL-1) * (vL-1) + 1;
}
else {
fX = CommonFF( fL, DegreeFFE(opL), fR, DegreeFFE(opR) );
if ( fX == 0 ) return CALL_2ARGS( PROD_FFE_LARGE, opL, opR );
qX = SIZE_FF( fX );
// if ( vL != 0 ) vL = (qX-1) / (qL-1) * (vL-1) + 1;
if ( vL != 0 ) vL = ((qX-1) * (vL-1)) / (qL-1) + 1;
// if ( vR != 0 ) vR = (qX-1) / (qR-1) * (vR-1) + 1;
if ( vR != 0 ) vR = ((qX-1) * (vR-1)) / (qR-1) + 1;
}
vX = PROD_FFV( vL, vR, SUCC_FF(fX) );
return NEW_FFE( fX, vX );
}
static Obj ProdFFEInt(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opL );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the right operand
vX = ((INT_INTOBJ( opR ) % pX) + pX) % pX;
if ( vX == 0 ) {
vR = 0;
}
else {
vR = 1;
for ( ; 1 < vX; vX-- ) vR = sX[vR];
}
// get the left operand
vL = VAL_FFE( opL );
vX = PROD_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
static Obj ProdIntFFE(Obj opL, Obj opR)
{
FFV vL, vR, vX; // value of left, right, result
FF fX; // field of result
Int pX; // char. of result
const FFV* sX; // successor table of result field
// get the field for the result
fX = FLD_FFE( opR );
pX = CHAR_FF( fX );
sX = SUCC_FF( fX );
// get the left operand
vX = ((INT_INTOBJ( opL ) % pX) + pX) % pX;
if ( vX == 0 ) {
vL = 0;
}
else {
vL = 1;
for ( ; 1 < vX; vX-- ) vL = sX[vL];
}
// get the right operand
vR = VAL_FFE( opR );
vX = PROD_FFV( vL, vR, sX );
return NEW_FFE( fX, vX );
}
/****************************************************************************
**
*F OneFFE(<op>) . . . . . . . . . . . . . . . one of a finite field element
*/
static Obj OneFFE(Obj op)
{
FF fX; // field of result
// get the field for the result
fX = FLD_FFE( op );
return NEW_FFE( fX, 1 );
}
/****************************************************************************
**
*F InvFFE(<op>) . . . . . . . . . . . . . . inverse of finite field element
*/
static Obj InvFFE(Obj op)
{
FFV v, vX; // value of operand, result
FF fX; // field of result