-
Notifications
You must be signed in to change notification settings - Fork 0
/
GF_x.pas
465 lines (355 loc) · 10 KB
/
GF_x.pas
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
unit GF_x;
interface
uses G_F, Math,SysUtils;
type
bvec= array of byte;
ivec= array of integer;
GFX=class
private
q, degree : integer;
public
coeffs: array of GF;
Constructor Create; overload;
constructor Create(qvalue: integer); overload;
constructor Create( qvalue, indegree: integer); overload;
constructor Create(qvalue : integer; const invalues : bvec); overload;
constructor Create(qvalue : integer; const invalues : string); overload;
constructor Create(const ingfx: GFX ); overload;
procedure set_field_values(qvalue: integer; const invalues: ivec ); overload;
procedure set_field_values(qvalue: integer; const invalues: string ); overload;
function get_size :integer;
function get_degree:integer ; // Return degree of gf(q)[x]
procedure set_degree(indegree:integer);
function get_true_degree:integer;
procedure clear;
procedure Copy(const ingfx: GFX); //overload;
procedure Add(const ingfx: GFX);
procedure Sub(const ingfx: GFX);
procedure Mul(const ingfx: GFX); overload;
function Count(const ingf:GF):GF;
function toString : string ;
end;
function Add(const op1, op2:GFX):GFX;
function Sub(const op1, op2:GFX):GFX;
function Mul(const op1, op2:GFX):GFX;overload;
function Divv(const c,g: GFX):GFX;overload;
function Modd(const c,g: GFX):GFX;
function Mul(const ingfx: GFX; const ingf:GF):GFX; overload;
function Divv(const ingfx: GFX; const ingf:GF):GFX; overload;
implementation
constructor GFX.Create;
begin
degree:=-1;
q:=0;
end;
constructor GFX.Create(qvalue: integer);
begin
q:=qvalue;
end;
constructor GFX.Create( qvalue, indegree: integer);
var i :integer;
begin
q:=qvalue;
SetLength(coeffs,indegree+1);
degree:=indegree;
for i:=0 to degree do
coeffs[i]:=GF.Create(q,-1);
end;
//Copy
constructor GFX.Create(const ingfx: GFX );
var i,b:integer;
begin
degree:=ingfx.degree;
q:=ingfx.q;
SetLength(coeffs,degree+1);
for i:=0 to degree do
coeffs[i]:=GF.Create(q,ingfx.coeffs[i].get_value);
end;
constructor GFX.Create(qvalue:integer; const invalues: bvec);
var input:ivec;
i,len :integer;
begin
len:= Length(invalues) ;
SetLength(input,len);
for i:=0 to len-1 do
input[i]:= invalues[i] ;
set_field_values(qvalue,input);
end;
constructor GFX.Create( qvalue:integer; const invalues: string);
begin
set_field_values(qvalue,invalues);
end;
procedure GFX.set_field_values(qvalue: integer; const invalues: ivec ) ;
var b,i:integer;
begin
degree:= Length(invalues)-1;
Setlength(coeffs,degree+1);
for i:=0 to degree do
coeffs[i]:=GF.Create(qvalue,invalues[i]);
q:=qvalue;
end;
procedure GFX.set_field_values(qvalue: integer; const invalues: string ) ;
var size:byte;
vec:ivec;
i:integer;
forconvert:GF;
begin
size:=Length(invalues);
Setlength(vec,size);
forconvert:=GF.Create(qvalue);
for i:=1 To size Do
begin
forconvert.set_field_value_by_pol(qvalue,StrToInt(invalues[i]));
vec[i-1]:= forconvert.get_value;
end;
set_field_values(qvalue,vec);
end;
function GFX.get_size :integer;
begin
Result:=q;
end;
function GFX.get_degree:integer ;
begin
Result:=degree;
end;
procedure GFX.set_degree(indegree:integer);
begin
SetLength(coeffs,indegree+1);
degree:=indegree;
end;
function GFX.get_true_degree:integer;
var i:integer;
begin
i:=degree;
while(coeffs[i].get_value=-1) do
begin
i:=i-1;
if (i=-1) then
break;
end;
Result:=i;
end;
procedure GFX.clear;
var i:integer;
begin
for i:=0 to degree do
coeffs[i].set_field_value(q,-1);
end;
procedure GFX.Copy(const ingfx: GFX);
var i,Olddegree:integer;
begin
Olddegree:=degree;
degree:=ingfx.degree;
q:=ingfx.q;
SetLength(coeffs,degree+1);
for i:=0 to degree do
if(i<=Olddegree) then
coeffs[i].set_field_value(q,ingfx.coeffs[i].get_value)
else
coeffs[i]:=GF.Create(q,ingfx.coeffs[i].get_value);
(*for i:=0 to Olddegree-1 do
coeffs[i].set_field_value(q,ingfx.coeffs[i].get_value);
for i:=Olddegree to degree do
coeffs[i]:=GF.Create(q,ingfx.coeffs[i].get_value);
*)
end;
procedure GFX.Add(const ingfx: GFX);
var i,j:integer;
begin
if (q = ingfx.q) then
begin
if (ingfx.degree > degree) then
begin
SetLength(coeffs,ingfx.degree+1);
// set new coefficients to the zeroth element
for j:=degree+1 to ingfx.degree do
coeffs[j]:=GF.create(q,-1);
degree:=ingfx.degree;
end;
for i:=0 to ingfx.degree do
coeffs[i].Add(ingfx.coeffs[i]);
end;
end;
procedure GFX.Sub(const ingfx: GFX);
begin
if (q = ingfx.q) then
Add(ingfx);
end;
procedure GFX.Mul(const ingfx: GFX);
var i,j:integer;
tempcoeffs:array of GF;
begin
if (q = ingfx.q) then
begin
Setlength(tempcoeffs,degree+1);
for i:=0 to degree do
tempcoeffs[i]:=GF.Create(coeffs[i]);
SetLength(coeffs,degree+ingfx.degree+1);
for j:=0 to degree do
coeffs[j].set_field_value(q,-1); // set coefficients to the zeroth element (log(0)=-Inf=-1)
for j:=degree+1 to (degree+ingfx.degree) do // and create new coeffs
coeffs[j]:=GF.Create(q,-1);
for i:=0 to degree do
for j:=0 to ingfx.degree do
coeffs[i+j].Add(G_F.Mul(ingfx.coeffs[j],tempcoeffs[i]));
degree:=degree+ingfx.degree;
end;
end;
function Divv(const c,g: GFX):GFX;
var q_c,q_g,q,tempdegree,gdegree,degreedif,i:integer;
temp,m,divisor:GFX;
Zeroo:GF;
begin
q_c:= c.get_size;
q_g:=g.get_size;
q:=c.get_size;
if (q_c=q_g) then
begin
Zeroo:=GF.Create(q,-1);
temp:=GFX.Create(c);
tempdegree:= temp.get_true_degree;
gdegree:= g.get_true_degree;
if (gdegree=-1) then
Result:=GFX.Create(q_c,0)
else
if (tempdegree>=gdegree) then
begin
degreedif:= tempdegree - gdegree;
m:=GFX.Create(q,degreedif);
divisor:=GFX.Create(q,degreedif);
for i:=0 to c.get_degree-1 do
begin
m.coeffs[degreedif]:= G_F.Divv(temp.coeffs[tempdegree],g.coeffs[gdegree]);
divisor.set_degree(degreedif);
divisor.clear;
divisor.coeffs[degreedif]:= m.coeffs[degreedif];
temp.Sub(Mul(divisor,g));
tempdegree:= temp.get_true_degree;
degreedif:= tempdegree - gdegree;
if ( (degreedif<0) or ((temp.get_true_degree=0) and (Equal(temp.coeffs[0],Zeroo) ) )) then
break;
end;
Result:=GFX.Create(m);
end
else
Result:=GFX.Create(q_c,0);
end
else Result:=GFX.Create;
end;
function Modd(const c,g: GFX):GFX;
var q_c,q_g,q,tempdegree,gdegree,degreedif,i:integer;
temp,m,divisor:GFX;
Zeroo:GF;
begin
q_c:= c.get_size;
q_g:=g.get_size;
q:=q_c;
if (q_c=q_g) then
begin
Zeroo:=GF.Create(q,-1);
gdegree:= g.get_true_degree;
if (gdegree=-1) then
temp:=GFX.Create(q_c,0)
else
begin
temp:=GFX.Create(c);
tempdegree:= temp.get_true_degree;
if (tempdegree>=gdegree) then
begin
degreedif:= tempdegree - gdegree;
m:=GFX.Create(q,degreedif);
divisor:=GFX.Create(q,degreedif);
for i:=0 to c.get_degree-1 do
begin
m.coeffs[degreedif]:= G_F.Divv(temp.coeffs[tempdegree],g.coeffs[gdegree]);
divisor.set_degree(degreedif);
divisor.clear;
divisor.coeffs[degreedif]:= m.coeffs[degreedif];
temp.Sub(Mul(divisor,g));
tempdegree:= temp.get_true_degree;
degreedif:= tempdegree - gdegree;
if ( (degreedif<0) or ((temp.get_true_degree=0) and (Equal(temp.coeffs[0],Zeroo) ) )) then
break;
end;
end;
end;
Result:=GFX.Create(temp);
end
else Result:=GFX.Create;
end;
function Mul(const op1,op2:GFX):GFX;
begin
Result:=GFX.Create(op1);
Result.Mul(op2);
end;
function GFX.Count(const ingf:GF):GF;
var i:integer;
temp,ingfpower:GF;
begin
if (q = ingf.get_size) then
begin
temp:=GF.Create(coeffs[0]);
ingfpower:=GF.Create(ingf);
for i:=1 to degree do
begin
temp.Add(G_F.Mul(coeffs[i],ingfpower));
ingfpower.Mul(ingf);
end;
result:= GF.Create(temp);
end;
//result:=GF.Create;
end;
function GFX.toString : string ;
var i,terms:integer;
Zero0,One:GF;
s:string;
begin
terms:=0;
Zero0:=GF.Create(q,-1);
One:=GF.Create(q,0);
for i:=0 to degree do
if ( NOTEqual(coeffs[i],Zero0) ) then
begin
if (terms<>0) then Result:=Result+'+' else terms:=terms+1;
if (Equal(coeffs[i],One)) then Result:=Result+'x^'+IntToStr(i)
else Result:=Result+coeffs[i].ValueToString+'*x^'+IntToStr(i);
end;
if (terms = 0) then Result:='zero polynom';
end;
function Add(const op1, op2:GFX):GFX;
begin
Result:=GFX.Create(op1);
Result.Add(op2);
end;
function Sub(const op1, op2:GFX):GFX;
begin
Result:=GFX.Create(op1);
Result.Add(op2);
end;
function Mul(const ingfx:GFX; const ingf:GF):GFX;
var temp:GFX;
i:integer;
begin
if (ingf.get_size = ingfx.q) then
begin
temp:=GFX.Create(ingfx);
for i:=0 to ingfx.get_degree do
temp.coeffs[i].Mul(ingf);
Result:=GFX.Create(temp);
end
else Result:=GFX.Create;
end;
function Divv(const ingfx:GFX;const ingf:GF):GFX;
var temp:GFX;
i:integer;
begin
if (ingf.get_size = ingfx.q) then
begin
temp:=GFX.Create(ingfx);
for i:=0 to ingfx.get_degree do
temp.coeffs[i].Divv(ingf);
Result:=GFX.Create(temp);
end
else Result:=GFX.Create;
end;
end.