-
Notifications
You must be signed in to change notification settings - Fork 3
/
lip.h
2990 lines (2604 loc) · 118 KB
/
lip.h
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
#if 0
{
Copyright Arjen K. Lenstra, 1989-1997
free L I P
l o n g i n t e g e r p a c k a g e
Arjen K. Lenstra
version 1.1
Introduction
============
This very long int package is supposed to be easy to use, portable, and
not too slow. It should also not be hard to make it fast by converting
a few macros to your favorite assembly code.
This version has an include file lip.h that defines all functions
and their arguments for ANSI C and contains:
typedef long * verylong;
Older programs need not be changed to use this type. It is added for
enhancing program readability.
As an example, the following program reads the decimal representation
of two arbitrary length signed integers a and b from stdin, computes
their product in c, and prints c in decimal on stdout, followed by a
newline:
#include "lip.h"
main()
{
/* declare verylong ints a, b, c */
verylong a = 0;
verylong b = 0;
verylong c = 0;
/*********************************\
* or declare them by
* long *a = 0, *b = 0, *c = 0
\*********************************/
zread(&a); /* read a from stdin */
zread(&b); /* read b from stdin */
zmul(a, b, &c); /* multiply a anb b, put result in c */
zwriteln(c); /* print c, followed by a newline */
}
Sample input:
7419043440758059956596 -60967068778579064460287972
with output:
-452317331723962514217511611516823866219980863312
(To run this example, first produce lip.o:
gcc -O -c lip.c
where gcc is assumed to be your ansi-C compiler, and next
gcc -O -o example1 example1.c lip.o -lm
to get the executable example1.)
As you can see, very long ints a, b, and c are declared simply by
verylong a = 0;
verylong b = 0;
verylong c = 0;
If a very long int is input to a function (like a and b in zmul(a, b, &c)),
just give its name as argument. Long ints that get new values in
a function (like a in zread(&a), b in zread(&b), c in zmul(a, b, &c))
are given by their address, which means that they are preceded by a &
in the function call.
As a slightly more challenging example, the following program starts
the random generator with a seed read from stdin, and attempts to
generate and print 5 probable primes of binary lengths 64, 80, 96, 112,
128:
#include "lip.h"
main()
{
verylong seed = 0;
long bl;
verylong p = 0;
zread(&seed); /* get seed from stdin */
zrstart(seed); /* start the random generator */
for (bl = 64; bl <= 128; bl += 16)
{
/* find prime of bl bits */
if (zrandomprime(bl, 5, &p, zrandomb))
{
/* refer to description below for the 5 */
printf("%3d bits: ", bl);
zwriteln(p);
}
else
printf("couldn`t find a %3d bit prime\n", bl);
}
}
Sample input:
742434390077967024401578982882
with output:
64 bits: 10513989034112217947
80 bits: 775541835531627786320957
96 bits: 58185536691780358241291462137
112 bits: 3464241483388970526627866839605371
128 bits: 198790427312192931901507582677867621703
WARNING for old users: some names have been changed.
======= If zxxx is a function operating on very long ints, then there
may be two variants of zxxx: zsxxx where one op the operands
is an ordinary long, and zxxxs where all operands are ordinary
longs. Compared to the previous version, the following
functions are affected (with some other name changes as well):
Old name New name
-------- --------
ztimes2 z2mul
zdivide2 z2div
zsmulmod zmulmods (there`s a new zsmulmod)
zsinv zinvs
zsexp zexpmods (there`s a new zsexp)
zexps zsexp
zexpsmod zsexpmod
zexp2mod z2expmod
zssqrt zsqrts
zsodinv zinvodds
zsjacobi zjacobis
zrstart zrstarts (there`s a new zrstart)
zmont zmontmul
zmexp zmontexp
zmexp_m_ary zmontexp_m_ary
Some arguments of some functions have changed:
zmulin switched order
zmakeodd *verylong (**long) instead of verylong (*long)
znegate *verylong (**long) instead of verylong (*long)
zcomposite *verylong (**long) instead of verylong (*long)
Overview of available functions
===============================
Basic arithmetic
----------------
zstart, zsadd, zadd, zsub, zsubpos,
zsmul, zmul, zmulin, zmul_plain, zsq, zsqin, zsq_plain,
zsdiv, zdiv, zsmod, zmod
Shifting and bit manipulation
-----------------------------
z2mul, z2div, z2mod, zlshift, zrshift, zmakeodd,
zodd, znot, zand, zor, zxor, zslowbits, zlowbits,
zshighbits, zhighbits, zweights, zweight, zcat,
zbit, zgetbits, zsetbit, zswitchbit, zreverses,
zreverse
Comparison, signs, copying, logarithms
--------------------------------------
zscompare, zcompare, ziszero, zsign, zabs, znegate, zcopy, zswap
z2logs, z2log, zln, zslog, zlog, zdlog
Conversion
----------
zzero, zone, zintoz, zuintoz, zultoz, ztoint, ztouint, ztoul,
sztrtozbas, zstrtoz, zdoub, zsbastoz, zbastoz, zstobas, ztobas,
zstosymbas, ztosymbas
Non-modular exponentiation
--------------------------
zsexp, zexp, zsqrts, zsqrt, zroot, zispower
Modular arithmetic
------------------
zaddmod, zsubmod, zmulmods, zsmulmod, zmulmod, zsqmod, zdivmod,
zinvmod, zexpmods, z2expmod, zsexpmod, zexpmod, zexpmod_m_ary,
zdefault_m, zexpmod_doub1, zexpmod_doub2, zexpmod_doub3, zexpmod_doub,
zmulmod26
Montgomery modular arithmetic
-----------------------------
zmstart, zmfree, ztom, zmtoz, zmontadd, zmontsub, zsmontmul, zmontmul,
zmontsq, zmontdiv, zmontinv, zmontexp, zmontexp_m_ary,
zmontexp_doub1, zmontexp_doub2, zmontexp_doub3, zmontexp_doub
Euclidean algorithms
--------------------
zgcd, zgcdeucl, zexteucl, zinvs, zinvodds, zinv, zchirem,
zjacobis, zjacobi
Random number generation
------------------------
zrstarts, zrstart, zrseed, zrandom, zrandomb, zrandoml,
zrandomprime, zrandomqprime, zrandomfprime, zrandomgprime
Small prime generation
----------------------
zpstart, zpstart2, zpnext, zpnextb, zp
Compositeness testing and factorization
---------------------------------------
zcomposite, zmcomposite, zprime, zprobprime,
ztridiv, zpollardrho, zecm_trial, zecm, zfecm, zsquf
Allocation
----------
zsetlength, zfree
Timing
------
gettime, getutime, getstime, starttime, printtime
Input and output
----------------
from to to from to to from to
file file file stdin stdout stdout string string
------------------------------------------------------------------------
decimal| zfread zfwrite zfwriteln zread zwrite zwriteln zsread zswrite
hex | zhfread zhfwrite zhfwriteln zhread zhwrite zhwriteln
nits | zbfread zbfwrite
anybase| zfread_b zfwrite_b zfwriteln_b
Remarks
=======
- Unless stated otherwise, output can be input, but it`s not advised
to make output arguments identical (if there are more output
parameters).
- Very long integers are represented by arrays of longs, in blocks
of NBITS bits (these blocks will be referred to as "nits").
A very long int (declared as verylong a=0 ) either satisfies a==0,
in which case it is treated as zero, or it satisfies the following:
- |a[0]| is the significant length, say n, with n>0,
even for a with value zero.
- a[1], a[2], ..., a[n] are the nits, most significant nit
is a[n], with always 0<a[n]<RADIX and 0<=a[i]<RADIX for
i=1,2,...,n-1; here RADIX is (1<<NBITS).
Exception: a[n] can be zero if n=1, in which case a has
value zero.
- the sign of a[0] is the sign of a.
- a[-1] gives the amount of space currently allocated
for a. The functions check this location to see if
reallocation is needed.
- the values of a[n+1],...,a[a[-1]] are undefined, and
cannot be assumed to be zero.
- except a==0, the only other correct representation
of an a with value zero is: a[0]==1, a[1]==0. Negative
zero is not recognized.
Unless you know what you`re doing, don`t play with any of the a[i].
- Because of the way verylongs are represented, local change (in
a routine) of a verylong parameter can affect the global value
of that parameter (and even destroy it entirely). As an example,
consider the following program:
#include "lip.h"
change_verylong(
verylong verylong_a
)
{
zwriteln(verylong_a);
zsadd(verylong_a,1,&verylong_a);
zwriteln(verylong_a);
}
change_long(
long long_a
)
{
printf("%ld\n",long_a);
long_a ++;
printf("%ld\n",long_a);
}
main () {
long a = 5;
verylong b = 0;
zintoz(5,&b);
change_long(a);
printf("%ld\n",a);
change_verylong(b);
zwriteln(b);
}
Ouput:
5
6
5
5
6
6
Although the parameter long_a gets a new value in change_long, this does
not affect the value of the argument a upon call: it is 5 before and after
the call. In change_verylong, however, the change made to the parameter
verylong_a affects the value of the argument b: it is 5 before, but 6
after the call. More dramatic things might happen too, if for instance
verylong_a gets reallocated in change_verylong and its original space
gets freed up.... To avoid these possible side-effects, either make
verylong_a a *verylong parameter (which should and does keep the local
changes made to it), or copy verylong_a to a local variable, and work
with the local variable:
better_change_verylong(
verylong verylong_a
)
{
static local_verylong_a = 0;
zcopy(verylong_a,&local_verylong_a);
zwriteln(local_verylong_a);
zsadd(local_verylong_a,1,&local_verylong_a);
zwriteln(local_verylong_a);
}
- For those who know what the sizes of input and output
arguments are going to be, you can allocate variables by hand using
zsetlength, and make things slightly faster by using the
-DNO_ALLOCATE flag. Internal local variables will always be allocated
to the proper length, no matter what flags you use, and output
variables will also be reallocated if they didn`t get enough
space, irrespective of the -DNO_ALLOCATE flag.
- To get an indication of what (re)allocations take place,
you can use the -DPRT_REALLOC flag. The indications will be
printed on stderr.
- If an error is detected (division by zero, undefined Montgomery
modulus, undefined results, etc) a message is printed on stderr
and the program exits. If the -DNO_HALT flag is used, the
program won`t exit, but values of variables might be undefined.
In the function descriptions below the possible error messages are
described. They are supposed to be self-explanatory, if not
a short explanation follows between (). If the message implies a bug
in LIP, please report it to [email protected] as soon as possible.
There is one message (`wrong call to zsmexp...BUG`) which can only be
generated by a wrong call to the internal (and undocumented) function
zsmexp; if you get this message you`re not using the original code.
- If you don`t want to think about -DNO_ALLOCATE or -DPRT_REALLOC
flags, allocations, or other unpleasant matters, everything
should work fine, as long as you make sure that you declare
the very long ints as indicated above (i.e., verylong a=0, b=0, etc...),
and give them a & in a function call if they are output.
So, as you can see below, zadd(a, a, &a) adds a to a and puts
the result in a. On the other hand, zmul(a, b, &a) leads to
trouble, because input cannot be output in zmul; use
zmulin(b, &a) instead.
- If you`re writing your own functions with local very long ints,
then it`s a good idea to declare the very long ints in frequently
called non-recursive functions as statics:
static verylong a = 0;
static verylong b = 0;
etc...
instead of
verylong a = 0;
verylong b = 0;
etc...
If static is used, reallocation of space for the local
very long ints is avoided in later calls to that same function,
unless one of the local very long ints needs more space than
in any of the previous calls: they always keep their longest
length. If you don`t use static, new space for the local
very long ints will be allocated for each new call to the
function, which is less efficient; also, you should use zfree
in that case at the end of the function, see below.
Acknowledgments
===============
Acknowledgments are due to many users for reporting bugs,
to Achim Flammenkamp for initializing the process of writing
this documentation, and to Bob Cain for converting to ansi.
Before you compile
==================
You should make sure that some constants get the right value
for your environment. Here`s a short description of what you
need. If in doubt, don`t change it, and just try how it works.
Include Files Needed
--------------------
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <sys/resource.h>
#include "lip.h"
Machine dependent constants
---------------------------
Have a look at the constants with <------, and set them
to the right value for your environment
#define CHARL 8 <------ Set this to the number of
bits in a byte. Usually it`s 8.
#define SIZEOFLONG 4 <------ Set this to the number of
bytes in a long, equals
sizeof(long).
#define NBITS 30 <------ Set this even, and as large as
possible such that
0 < NBITS < CHARL*SIZEOFLONG.
(Addition: use -DSINGLE_MUL flag to get NBITS=26 and faster macros
(at least, on most machines); assumes that words
in doubles are ordered high-low. Use -DDOUBLE_LOW_HIGH if it`s
the other way around)
#define RADIX (1<<NBITS) Don`t touch this, but it`s
good to know what the radix is.
#define KAR_MUL_CROV 30 If in a call zmul(a, b, &c) the
#define KAR_SQU_CROV 30 number of nits of a or b is
#define KAR_DEPTH 20 less than KAR_MUL_CROV, then a
and b are multiplied using the
plain quadratic multiplication function; if that`s not the
case Karatsuba will be applied, recursively, but to a
maximum of at most KAR_DEPTH recursions. Same for zsq and
ZKAR_SQU_CROV. The optimal values of these two cross-over
values depend on the machine you are going to use. The
choices above are not too far from optimal on a DEC5000;
on Sparcs the optimal values are somewhat smaller. You
can make KAR_DEPTH as large as you like, as long as you
have enough memory.
#define SIZE 20 <------ Set this to anything such that
SIZE*NBITS>=CHARL*SIZEOFLONG
SIZE is the default and minimum allocation size for very
long ints. Any value >= 2 should work. Depending on your
application smaller or larger values than 20 might lead
to more efficient code, because it either uses less space
(for a smaller SIZE), or it uses fewer allocations (for
a larger SIZE). If you`re not sure what SIZE to pick,
compile the package and your program with the -DPRT_REALLOC
flag, and run a representative example: the output will give
you an impression of the actual sizes that will be used, and
SIZE can be set accordingly before you compile again without
the -DPRT_REALLOC flag. If you don`t change it, it should work
fine.
#define OUT_LINE 68 <------ An approximate bound for
the maximal number of digits
per line of output. You might want to change this to 40
if you have an unusually narrow screen, or to 132 if you`re
still using one of these nice old lineprinters.
#define IN_LINE 2048 Input accepts at most
IN_LINE characters per line.
This should not be too restrictive, because long lines
can easily be split into smaller lines, see below.
#define PRIM_BND 16500 This enables you to
generate the primes
less than (2*PRIM_BND+1)^2 using zpnext, see below.
For 16500 the last prime that can thus be generated is
1089065981.
}
#endif
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#ifndef WIN32
#include <netinet/in.h>
#endif
#include "lippar.h"
/*The type of very long ints.*/
typedef long * verylong;
#ifdef FREE
#define STATIC
#define FREESPACE(x) zfree(&x);
#define FREE2SPACE(x,y) zfree(&x); zfree(&y);
#define FREE3SPACE(x,y,z) zfree(&x); zfree(&y); zfree(&z);
#else
#define STATIC static
#define FREESPACE(x)
#define FREE2SPACE(x,y)
#define FREE3SPACE(x,y,z)
#endif
#define ILLEGAL 0
#ifdef NO_ALLOCATE
# define ALLOCATE 0
#else
# define ALLOCATE 1
#endif
#ifdef PRT_REALLOC
# undef PRT_REALLOC
# define PRT_REALLOC 1
#else
# define PRT_REALLOC 0
#endif
#ifndef CHARL
# define CHARL 8 /* 8 bits per char */
#endif
#ifndef SIZEOFLONG
# define SIZEOFLONG 4 /* set this to sizeof(long) */
#endif
#define BITSOFLONG (CHARL*SIZEOFLONG)
#ifdef ALPHA
# ifdef ALPHA50
# undef ILLEGAL
# define ILLEGAL 1
# endif
# ifndef PLAIN
# undef KARAT
# define KARAT 1
# endif
# ifdef SINGLE_MUL
# undef ILLEGAL
# define ILLEGAL 1
# endif
# define NBITS 62
# undef BITSOFLONG
# define BITSOFLONG 64
# undef SIZEOFLONG
# define SIZEOFLONG 8
# define PRIM_BND (1L<<14)
# define ALPHA_OR_ALPHA50 1
#endif
#ifdef ALPHA50
# ifndef PLAIN
# undef KARAT
# define KARAT 1
# endif
# ifdef SINGLE_MUL
# undef ILLEGAL
# define ILLEGAL 1
# endif
# define NBITS 62
# undef BITSOFLONG
# define BITSOFLONG 64
# undef SIZEOFLONG
# define SIZEOFLONG 8
# define PRIM_BND (1L<<14)
# define ALPHA50NBITS 50
# define ALPHA50NBITSH (ALPHA50NBITS>>1)
# define ALPHA50RADIX (1L<<ALPHA50NBITS)
# define ALPHA50RADIXM (ALPHA50RADIX-1)
# define ALPHA50RADIXROOT (1L<< ALPHA50NBITSH)
# define ALPHA50RADIXROOTM (ALPHA50RADIXROOT-1)
# define ALPHA_OR_ALPHA50 1
#endif
#ifdef PLAIN
# define PLAIN_OR_KARAT 1
#endif
#ifdef KARAT
# define PLAIN_OR_KARAT 1
#endif
#ifndef NBITS
# ifdef SINGLE_MUL
# define NBITS 26
# else
# define NBITS 30
# endif
#endif
#define NBITSH (NBITS>>1)
#define RADIX (1L<<NBITS)
#define RADIXM (RADIX-1)
#define RADIXROOT (1L<<NBITSH)
#define RADIXROOTM (RADIXROOT-1)
#ifndef SIZE
# define SIZE 20 /* SIZE*NBITS must be >= BITSOFLONG */
#endif
#ifndef OUT_LINE
# define OUT_LINE 68 /* approximate bound # digits per line */
#endif
#define OUT_LINE_BREAK '\\'
#ifndef IN_LINE
# define IN_LINE 2048 /* at most 2048 characters per line */
#endif
#define IN_LINE_BREAK '\\'
#ifndef HEX_BLOCK
# define HEX_BLOCK 8
#endif
#ifndef HEX_BLOCKS_PER_LINE
# define HEX_BLOCKS_PER_LINE 7
#endif
#define HEX_SEP_CHAR ' '
#ifndef PRIM_BND
# ifdef SINGLE_MUL
# define PRIM_BND (1L<<14)
# else
# define PRIM_BND (1L<<(NBITSH-1))
# endif
/* to generate primes <= (2*PRIM_BND+1)^2 */
/* last prime for NBITS == 30 is 1073807359 */
/* same for SINGLE_MUL */
#elif (PRIM_BND>(1L<<(NBITSH-1)))
# undef PRIM_BND
# define PRIM_BND (1L<<(NBITSH-1))
#endif
#define PRIM_UP ((((PRIM_BND<<1)+1)*((PRIM_BND<<1)+1))-(NBITS<<2))
#if (NBITS&1)
# undef ILLEGAL
# define ILLEGAL 1
#endif
#if (NBITS <= 0)
# undef ILLEGAL
# define ILLEGAL 1
#endif
#if (NBITS >= BITSOFLONG)
# undef ILLEGAL
# define ILLEGAL 1
#endif
#if (SIZE*NBITS<=BITSOFLONG)
# undef ILLEGAL
# define ILLEGAL 1
#endif
#ifdef SINGLE_MUL
# if (NBITS != 26)
# undef ILLEGAL
# define ILLEGAL 1
# endif
# ifdef PLAIN
# undef ILLEGAL
# define ILLEGAL 1
# endif
# ifdef KARAT
# undef ILLEGAL
# define ILLEGAL 1
# endif
#endif
#ifdef PLAIN
# ifdef KARAT
# undef ILLEGAL
# define ILLEGAL 1
# endif
#endif
/******************************************************************************\
* Internal macros
*
* Although the package is supposed to be portable, you might want to
* fine tune it to your particular machine to get better performance.
* The easiest way to do this is to replace the following macros, which
* you will find in the source code, by appropriate assembly language
* versions:
*
* Also the C-code for zsubmul, zdiv21 and zmulmods can be made
* much faster in a similar way. I always simply used the macros
* or the C-code, and I get acceptable performance.
\******************************************************************************/
static void zaddmulp(long *a, long b, long d, long *t);
/******************************************************************\
* a = (a + t + b * d) % RADIX; and simultaneously
* t = (a + t + b * d) / RADIX;
\******************************************************************/
static void zaddmulpsq(long *a, long b, long *t);
/******************************************************************\
* a = (a + b * b) % RADIX; and simultaneously
* t = (a + b * b) / RADIX;
\******************************************************************/
static void zaddmulone(verylong a, verylong b);
/******************************************************************\
* a += b;
* a and b not at overlapping addresses
\******************************************************************/
static void zaddmul(long d, verylong a, verylong b);
/******************************************************************\
* a += d * b;
* a and b not at overlapping addresses (except if d=1)
\******************************************************************/
static void zaddmulsq(long d, verylong a, verylong b);
/******************************************************************\
* a += b[0] * b[1:d];
* a and b not at overlapping addresses (except if d=1)
\******************************************************************/
static void zmmulp(verylong a);
/******************************************************************\
* a[s:] += (a[s] * zminv) * zn;
* to make a[s] == 0, where s and zminv are clear from
* the context, only in Montgomery multiplication
\******************************************************************/
/******************************************************************************\
* Basic functions
*
* Addition, subtraction, multiplication, squaring, and
* division with remainder on signed arbitrary length integers.
* Multiplication and squaring use Karatsuba, if inputs large enough;
* see KAR_MUL_CROV, KAR_SQU_CROV, and KAR_DEPTH as explained above.
\******************************************************************************/
void zstart(void);
/******************************************************************\
* To initialize some global machine dependent values
* that have to be computed only once, call this only once per run.
* Everything still works fine if you forget to call zstart.
* If you`re sure that you`re not going to forget it, you may
* compile the package with the -DSTART flag and get slightly
* faster code.
*
* possible error message:
* recompile with smaller NBITS
* result undefined if error occurs
\******************************************************************/
void zsadd(verylong a, long d, verylong *b);
/******************************************************************\
* *b = a + d;
*
\******************************************************************/
void zadd(verylong a, verylong b, verylong *c);
/******************************************************************\
* *c = a + b;
*
\******************************************************************/
void zsub(verylong a, verylong b, verylong *c);
/******************************************************************\
* *c = a - b;
*
\******************************************************************/
void zsubpos(verylong a, verylong b, verylong *c);
/******************************************************************\
* *c = a - b;
*
* only for a >= b >= 0
\******************************************************************/
void zsmul(verylong a, long d, verylong *b);
/******************************************************************\
* *b = d * a;
*
\******************************************************************/
void zmul(verylong a, verylong b, verylong *c);
/******************************************************************\
* *c = a * b;
*
* output cannot be input
\******************************************************************/
void zmulin(verylong a, verylong *b);
/******************************************************************\
* *b = a * b;
*
* output cannot be input
\******************************************************************/
void zmul_plain(verylong a, verylong b, verylong *c);
/******************************************************************\
* *c = a * b;
*
* output cannot be input, uses ordinary multiplication
\******************************************************************/
void zsq(verylong a, verylong *c);
/******************************************************************\
* *c = a * a;
*
* output cannot be input
\******************************************************************/
void zsqin(verylong *a);
/******************************************************************\
* *a = a ^ 2;
*
\******************************************************************/
void zsq_plain(verylong a, verylong *b);
/******************************************************************\
* *b = a ^ 2;
*
* output cannot be input, uses ordinary squaring
\******************************************************************/
long zsdiv(verylong a, long d, verylong *b);
/******************************************************************\
* *b = a / d;
* return (a % d);
*
* d != 0,
* always b * q + (a % d) == a and,
* unless b divides a, sign(a % d) == sign(d),
* calls zdiv if |d| >= RADIX
*
* possible error message:
* division by zero in zsdiv
* result undefined if error occurs
\******************************************************************/
void zdiv(verylong a, verylong b, verylong *q, verylong *r);
/******************************************************************\
* *q = a / b;
* *r = a % b;
*
* b != 0,
* always b * q + r == a,
* unless b divides a, sign(r) == sign(b)
*
* possible error message:
* division by zero in zdiv
* result undefined if error occurs
\******************************************************************/
long zsmod(verylong a, long d);
/******************************************************************\
* return (a % d);
*
* calls zsdiv
\******************************************************************/
void zmod(verylong a, verylong b, verylong *r);
/******************************************************************\
* *r = a % b;
*
* unless b divides a, sign(r) == sign(b),
* slightly faster than zdiv
*
* possible error message:
* division by zero in zmod
* result undefined if error occurs
\******************************************************************/
/******************************************************************************\
* Shifting and bit manipulation
*
* Left and right shifting, removal of all factors 2,
* parity test, logical and/(x)or, bit selections, weight,
* concatenation, bit-reverse
*
* WARNING: The bit manipulation routines need to be debugged carefully
\******************************************************************************/
void z2mul(verylong n, verylong *a);
/******************************************************************\
* *a = 2 * n;
*
\******************************************************************/
long z2div(verylong n, verylong *a);
/******************************************************************\
* *a = n / 2; return (n % 2);
*
* warning: for n == -2 * k + 1 (k > 0), z2div(n, &a)
* gives a = -k + 1, but zsdiv(a, 2, &b) gives b = -k,
* both return 1
\******************************************************************/
long z2mod(verylong n);
/******************************************************************\
* return (n % 2);
\******************************************************************/
void zlshift(verylong n, long k, verylong *a);
/******************************************************************\
* *a = 2 ^ k * n;
*
* i.e., shifts left over k positions,
* calls zrshift(n, -k, a) if k < 0
\******************************************************************/
void zrshift(verylong n, long k, verylong *a);
/******************************************************************\
* *a = n / (2 ^ k);
*
* i.e., shifts right over k positions,
* calls zlshift(n, -k, a) if k<0
\******************************************************************/
long zmakeodd(verylong *n);
/******************************************************************\
* if (n != 0)
* *n = m;
* return (k such that n == 2 ^ k * m with m odd);
* else
* return (-1);
\******************************************************************/
long zodd(verylong a);
/******************************************************************\
* returns 1 if a is odd, returns 0 if a is even
\******************************************************************/
void znot(verylong a, verylong *b);
/******************************************************************\
* if (a==0) then b gets 1,
* else b gets the negated bit pattern of the first z2log(|a|)
* bits of a, and b gets the same sign as a (unless b is zero)
\******************************************************************/
void zand(verylong a, verylong b, verylong *c);
/******************************************************************\
* c gets bit pattern `bits of |a|` and `bits of |b|`
\******************************************************************/
void zor(verylong a, verylong b, verylong *c);
/******************************************************************\
* c gets bit pattern `bits of |a|` inclusive or `bits of |b|`
\******************************************************************/
void zxor(verylong a, verylong b, verylong *c);
/******************************************************************\
* c gets bit pattern `bits of |a|` exclusive or `bits of |b|`
\******************************************************************/
long zslowbits(verylong a, long b);
/******************************************************************\
* returns b (or NBITS if b>NBITS) lowest order bits of |a|,
* 0 if b <= 0
\******************************************************************/
void zlowbits(verylong a, long b, verylong *c);
/******************************************************************\
* c gets b lowest order bits of |a|, c gets 0 if b <= 0
\******************************************************************/
long zshighbits(verylong a, long b);
/******************************************************************\
* returns b (or NBITS if b>NBITS) highest order bits of |a|,
* 0 if b <= 0
\******************************************************************/
void zhighbits(verylong a, long b, verylong *c);
/******************************************************************\
* c gets b highest order bits of |a|, c gets 0 if b <= 0
\******************************************************************/
long zweights(long a);
/******************************************************************\
* returns the number of one bits in |a|
\******************************************************************/
long zweight(verylong a);
/******************************************************************\
* returns the number of one bits in |a|