# libzahl

big integer library
git clone git://git.suckless.org/libzahl

test-generate.py (25817B)

```      1 #!/usr/bin/env python3
3
4 import sys, random
5
6
7 def mod(a, b):
8     r = (abs(a) % abs(b)) * (-1 if a < 0 else 1)
9     q = div(a, b)
10     if a != q * b + r:
11         print('zdivmod does not satisfly n = qd + r', file = sys.stderr)
12         sys.exit(1)
13     return r
14
15 def div(a, b): # Python's division is floored, not truncated.
16     r = abs(a) // abs(b)
17     if a < 0:
18         r = -r
19     if b < 0:
20         r = -r
21     return r
22
23 def gcd(u, v):
24     if u == 0:
25         return v
26     if v == 0:
27         return u
28     uneg = u < 0
29     vneg = v < 0
30     u = abs(u)
31     v = abs(v)
32
33     shift = 0
34     while ((u | v) & 1) == 0:
35         u >>= 1
36         v >>= 1
37         shift += 1
38
39     while (u & 1) == 0:
40         u >>= 1
41
42     while True:
43         while (v & 1) == 0:
44             v >>= 1
45         if u > v:
46             (u, v) = (v, u)
47         v -= u
48         if v == 0:
49             break
50
51     u <<= shift
52     if uneg and vneg:
53         u = -u
54     return u
55
56
57 def zabs():
58     bits = random.randint(0, LIMIT)
59     a = random.randint(-(1 << bits), 1 << bits)
60     print('zsets(a, "%i");' % a)
61     print('zabs(b, a);')
62     print('zabs(a, a);')
63     print('assert(zcmp(a, b), == 0);')
64     print('assert_s(zstr(a, buf, BUF_N), "%i");' % abs(a))
65
67     bits = random.randint(0, LIMIT)
68     a = random.randint(-(1 << bits), 1 << bits)
69     bits = random.randint(0, LIMIT)
70     b = random.randint(-(1 << bits), 1 << bits)
71     c = a + b
72     print('zsets(a, "%i");' % a)
73     print('zsets(b, "%i");' % b)
75     print('zset(d, b);')
78     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
79     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
80     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
81
83     bits = random.randint(0, LIMIT)
84     a = random.randint(-(1 << bits), 1 << bits)
85     bits = random.randint(0, LIMIT)
86     b = random.randint(-(1 << bits), 1 << bits)
87     c = abs(a) + abs(b)
88     print('zsets(a, "%i");' % a)
89     print('zsets(b, "%i");' % b)
91     print('zset(d, b);')
94     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
95     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
96     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
97     c = abs(b) * 2
100     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
101     print('assert(zcmp(b, c), == 0);')
102
103 def zand():
104     bits = random.randint(0, LIMIT)
105     a = random.randint(-(1 << bits), 1 << bits)
106     bits = random.randint(0, LIMIT)
107     b = random.randint(-(1 << bits), 1 << bits)
108     c = abs(a) & abs(b)
109     if a < 0 and b < 0:
110         c = -c
111     print('zsets(a, "%i");' % a)
112     print('zsets(b, "%i");' % b)
113     print('zand(c, a, b);')
114     print('zset(d, b);')
115     print('zand(d, a, d);')
116     print('zand(a, a, b);')
117     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
118     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
119     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
120     print('zsets(a, "%i");' % a)
121     print('zand(d, a, a);')
122     print('zand(a, a, a);')
123     print('assert_s(zstr(d, buf, BUF_N), "%i");' % a)
124     print('assert_s(zstr(a, buf, BUF_N), "%i");' % a)
125
126 def zbits():
127     bits = random.randint(0, LIMIT)
128     a = random.randint(-(1 << bits), 1 << bits)
129     print('zsets(a, "%i");' % a)
130     a = abs(a)
131     if a == 0:
132         b = 1
133     else:
134         b = 0
135         while a > 0:
136             b += 1
137             a >>= 1
138     print('assert_zu(zbits(a), %i);' % b)
139
140 def zbset():
141     bits = random.randint(0, LIMIT)
142     a = random.randint(-(1 << bits), 1 << bits)
143     b = random.randint(0, 2 * LIMIT)
144     cs = (abs(a) |  (1 << b)) * (-1 if a < 0 else 1)
145     cc = (abs(a) & ~(1 << b)) * (-1 if a < 0 else 1)
146     cf = (abs(a) ^  (1 << b)) * (-1 if a < 0 else 1)
147     print('zsets(a, "%i");' % a)
148     print('zset(d, a);')
149     print('zbset(b, a, %i, 1);' % b)
150     print('assert_s(zstr(b, buf, BUF_N), "%i");' % cs)
151     print('zbset(b, a, %i, 0);' % b)
152     print('assert_s(zstr(b, buf, BUF_N), "%i");' % cc)
153     print('zbset(b, a, %i, -1);' % b)
154     print('assert_s(zstr(b, buf, BUF_N), "%i");' % cf)
155     print('zset(a, d);')
156     print('zbset(a, a, %i, 1);' % b)
157     print('assert_s(zstr(a, buf, BUF_N), "%i");' % cs)
158     print('zset(a, d);')
159     print('zbset(a, a, %i, 0);' % b)
160     print('assert_s(zstr(a, buf, BUF_N), "%i");' % cc)
161     print('zset(a, d);')
162     print('zbset(a, a, %i, -1);' % b)
163     print('assert_s(zstr(a, buf, BUF_N), "%i");' % cf)
164
165 def zbtest():
166     bits = random.randint(0, LIMIT)
167     a = random.randint(-(1 << bits), 1 << bits)
168     b = random.randint(0, 2 * LIMIT)
169     c = (abs(a) >> b) & 1
170     print('zsets(a, "%i");' % a)
171     print('assert(zbtest(a, %i), == %i);' % (b, c))
172
173 def zcmp():
174     bits = random.randint(0, LIMIT)
175     a = random.randint(-(1 << bits), 1 << bits)
176     bits = random.randint(0, LIMIT)
177     b = random.randint(-(1 << bits), 1 << bits)
178     c = -1 if a < b else (1 if a > b else 0)
179     print('zsets(a, "%i");' % a)
180     print('zsets(b, "%i");' % b)
181     print('assert(zcmp(a, b), == %i);' % c)
182
183 def zcmpmag():
184     bits = random.randint(0, LIMIT)
185     a = random.randint(-(1 << bits), 1 << bits)
186     bits = random.randint(0, LIMIT)
187     b = random.randint(-(1 << bits), 1 << bits)
188     print('zsets(a, "%i");' % a)
189     print('zsets(b, "%i");' % b)
190     a = abs(a)
191     b = abs(b)
192     c = -1 if a < b else (1 if a > b else 0)
193     print('assert(zcmpmag(a, b), == %i);' % c)
194
195 def zlsb():
196     bits = random.randint(0, LIMIT)
197     a = random.randint(-(1 << bits), 1 << bits)
198     print('zsets(a, "%i");' % a)
199     a = abs(a)
200     if a == 0:
201         b = "SIZE_MAX"
202     else:
203         b = 0
204         while (a & 1) == 0:
205             b += 1
206             a >>= 1
207         b = str(b)
208     print('assert_zu(zlsb(a), %s);' % b)
209
210 def zlsh():
211     bits = random.randint(0, LIMIT)
212     a = random.randint(-(1 << bits), 1 << bits)
213     bits = random.randint(0, 2 * LIMIT)
214     c = a << bits
215     print('zsets(a, "%i");' % a)
216     print('zlsh(b, a, %i);' % bits)
217     print('zlsh(a, a, %i);' % bits)
218     print('assert(zcmp(a, b), == 0);')
219     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
220
221 def zneg():
222     bits = random.randint(0, LIMIT)
223     a = random.randint(-(1 << bits), 1 << bits)
224     print('zsets(a, "%i");' % a)
225     print('zneg(b, a);')
226     print('zneg(a, a);')
227     print('assert(zcmp(a, b), == 0);')
228     print('assert_s(zstr(a, buf, BUF_N), "%i");' % -a)
229
230 def zor():
231     bits = random.randint(0, LIMIT)
232     a = random.randint(-(1 << bits), 1 << bits)
233     bits = random.randint(0, LIMIT)
234     b = random.randint(-(1 << bits), 1 << bits)
235     c = abs(a) | abs(b)
236     if a < 0 or b < 0:
237         c = -c
238     print('zsets(a, "%i");' % a)
239     print('zsets(b, "%i");' % b)
240     print('zor(c, a, b);')
241     print('zset(d, b);')
242     print('zor(d, a, d);')
243     print('zor(a, a, b);')
244     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
245     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
246     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
247     print('zsets(a, "%i");' % a)
248     print('zor(d, a, a);')
249     print('zor(a, a, a);')
250     print('assert_s(zstr(d, buf, BUF_N), "%i");' % a)
251     print('assert_s(zstr(a, buf, BUF_N), "%i");' % a)
252
253 def zrsh():
254     bits = random.randint(0, LIMIT)
255     a = random.randint(-(1 << bits), 1 << bits)
256     bits = random.randint(0, LIMIT)
257     c = (abs(a) >> bits) * (-1 if a < 0 else 1)
258     print('zsets(a, "%i");' % a)
259     print('zrsh(b, a, %i);' % bits)
260     print('zrsh(a, a, %i);' % bits)
261     print('assert(zcmp(a, b), == 0);')
262     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
263
264 def zsplit():
265     bits = random.randint(0, LIMIT)
266     a = random.randint(-(1 << bits), 1 << bits)
267     bits = random.randint(0, 2 * LIMIT)
268     sign = -1 if a < 0 else 1
269     c = (abs(a) >> bits) * sign
270     d = (abs(a) - (abs(c) << bits)) * sign
271     print('zsets(a, "%i");' % a)
272     print('zset(b, a);')
273     print('zsplit(b, d, b, %i);' % bits)
274     print('assert_s(zstr(b, buf, BUF_N), "%i");' % c)
275     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
276     print('zsplit(c, d, a, %i);' % bits)
277     print('assert(zcmp(b, c), == 0);')
278     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
279     print('zsplit(c, a, a, %i);' % bits)
280     print('assert(zcmp(a, d), == 0);')
281     print('assert(zcmp(b, c), == 0);')
282
283 def zstr():
284     bits = random.randint(0, LIMIT)
285     a = random.randint(-(1 << bits), 1 << bits)
286     print('zsets(a, "%i");' % a)
287     print('assert_s(zstr(a, buf, BUF_N), "%i");' % a)
288
289 def zstr_length():
290     bits = random.randint(0, LIMIT)
291     a = random.randint(-(1 << bits), 1 << bits)
292     print('zsets(a, "%i");' % a)
293     print('assert_zu(zstr_length(a, 10), %i);' % len(str(a)))
294
295 def zsub():
296     bits = random.randint(0, LIMIT)
297     a = random.randint(-(1 << bits), 1 << bits)
298     bits = random.randint(0, LIMIT)
299     b = random.randint(-(1 << bits), 1 << bits)
300     c = a - b
301     print('zsets(a, "%i");' % a)
302     print('zsets(b, "%i");' % b)
303     print('zsub(c, a, b);')
304     print('zset(d, b);')
305     print('zsub(d, a, d);')
306     print('zsub(a, a, b);')
307     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
308     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
309     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
310
311 def zsub_unsigned():
312     bits = random.randint(0, LIMIT)
313     a = random.randint(-(1 << bits), 1 << bits)
314     bits = random.randint(0, LIMIT)
315     b = random.randint(-(1 << bits), 1 << bits)
316     c = abs(a) - abs(b)
317     print('zsets(a, "%i");' % a)
318     print('zsets(b, "%i");' % b)
319     print('zsub_unsigned(c, a, b);')
320     print('zset(d, b);')
321     print('zsub_unsigned(d, a, d);')
322     print('zsub_unsigned(a, a, b);')
323     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
324     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
325     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
326     print('zsub_unsigned(a, b, b);')
327     print('assert(zzero(a), == 1);')
328     print('zsub_unsigned(b, b, b);')
329     print('assert(zzero(b), == 1);')
330
331 def ztrunc():
332     bits = random.randint(0, LIMIT)
333     a = random.randint(-(1 << bits), 1 << bits)
334     bits = random.randint(0, 2 * LIMIT)
335     c = (abs(a) & ((1 << bits) - 1)) * (-1 if a < 0 else 1)
336     print('zsets(a, "%i");' % a)
337     print('ztrunc(b, a, %i);' % bits)
338     print('ztrunc(a, a, %i);' % bits)
339     print('assert(zcmp(a, b), == 0);')
340     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
341
342 def zxor():
343     bits = random.randint(0, LIMIT)
344     a = random.randint(-(1 << bits), 1 << bits)
345     bits = random.randint(0, LIMIT)
346     b = random.randint(-(1 << bits), 1 << bits)
347     c = abs(a) ^ abs(b)
348     if (a < 0) != (b < 0):
349         c = -c
350     print('zsets(a, "%i");' % a)
351     print('zsets(b, "%i");' % b)
352     print('zxor(c, a, b);')
353     print('zset(d, b);')
354     print('zxor(d, a, d);')
355     print('zxor(a, a, b);')
356     print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
357     print('assert_s(zstr(d, buf, BUF_N), "%i");' % c)
358     print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
359     print('zsets(a, "%i");' % a)
360     print('zxor(d, a, a);')
361     print('zxor(a, a, a);')
362     print('assert(zzero(d), == 1);')
363     print('assert(zzero(a), == 1);')
364
365 def zeven():
366     bits = random.randint(0, LIMIT)
367     a = random.randint(-(1 << bits), 1 << bits)
368     b = 1 if (abs(a) & 1) == 0 else 0
369     print('zsets(a, "%i");' % a)
370     print('assert(zeven(a), == %i);' % b)
371
372 def zodd():
373     bits = random.randint(0, LIMIT)
374     a = random.randint(-(1 << bits), 1 << bits)
375     b = 1 if (abs(a) & 1) != 0 else 0
376     print('zsets(a, "%i");' % a)
377     print('assert(zodd(a), == %i);' % b)
378
379 def zeven_nonzero():
380     bits = random.randint(0, LIMIT)
381     a = 0
382     while a == 0:
383         a = random.randint(-(1 << bits), 1 << bits)
384     b = 1 if (abs(a) & 1) == 0 else 0
385     print('zsets(a, "%i");' % a)
386     print('assert(zeven_nonzero(a), == %i);' % b)
387
388 def zodd_nonzero():
389     bits = random.randint(0, LIMIT)
390     a = 0
391     while a == 0:
392         a = random.randint(-(1 << bits), 1 << bits)
393     b = 1 if (abs(a) & 1) != 0 else 0
394     print('zsets(a, "%i");' % a)
395     print('assert(zodd_nonzero(a), == %i);' % b)
396
397 def zzero():
398     bits = random.randint(0, LIMIT)
399     a = random.randint(-(1 << bits), 1 << bits)
400     b = 1 if a == 0 else 0
401     print('zsets(a, "%i");' % a)
402     print('assert(zzero(a), == %i);' % b)
403
404 def zsignum():
405     bits = random.randint(0, LIMIT)
406     a = random.randint(-(1 << bits), 1 << bits)
407     b = -1 if a < 0 else (1 if a > 0 else 0)
408     print('zsets(a, "%i");' % a)
409     print('assert(zsignum(a), == %i);' % b)
410
411 def zdiv():
412     bits = random.randint(0, LIMIT)
413     a = random.randint(-(1 << bits), 1 << bits)
414     bits = random.randint(0, LIMIT)
415     b = 0
416     while b == 0:
417         b = random.randint(-(1 << bits), 1 << bits)
418     c = div(a, b)
419     print('zsets(a, "%i");' % a)
420     print('zsets(b, "%i");' % b)
421     print('zsets(d, "%i");' % c)
422     print('zdiv(c, a, b);')
423     print('zdiv(a, a, b);')
424     print('assert(zcmp(c, d), == 0);')
425     print('assert(zcmp(a, d), == 0);')
426     print('zsets(a, "%i");' % a)
427     print('zdiv(b, a, b);')
428     print('assert(zcmp(b, d), == 0);')
429
430 def zmod():
431     bits = random.randint(0, LIMIT)
432     a = random.randint(-(1 << bits), 1 << bits)
433     bits = random.randint(0, LIMIT)
434     b = 0
435     while b == 0:
436         b = random.randint(-(1 << bits), 1 << bits)
437     c = mod(a, b)
438     print('zsets(a, "%i");' % a)
439     print('zsets(b, "%i");' % b)
440     print('zsets(d, "%i");' % c)
441     print('zmod(c, a, b);')
442     print('zmod(a, a, b);')
443     print('assert(zcmp(c, d), == 0);')
444     print('assert(zcmp(a, d), == 0);')
445     print('zsets(a, "%i");' % a)
446     print('zmod(b, a, b);')
447     print('assert(zcmp(b, d), == 0);')
448
449 def zdivmod():
450     bits = random.randint(0, LIMIT)
451     ap = random.randint(0, 1 << bits)
452     bits = random.randint(0, LIMIT)
453     bp = 0
454     while bp == 0:
455         bp = random.randint(0, 1 << bits)
456     for (a_sign, b_sign) in ((1, 1), (1, -1), (-1, 1), (-1, -1)):
457         a = ap * a_sign
458         b = bp * b_sign
459         (c, d) = (div(a, b), mod(a, b))
460         print('zsets(a, "%i");' % a)
461         print('zsets(b, "%i");' % b)
462         print('zdivmod(c, d, a, b);')
463         print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
464         print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
465         print('zdivmod(a, b, a, b);')
466         print('assert(zcmp(a, c), == 0);')
467         print('assert(zcmp(b, d), == 0);')
468         print('zsets(a, "%i");' % a)
469         print('zsets(b, "%i");' % b)
470         print('zdivmod(b, a, a, b);')
471         print('assert(zcmp(b, c), == 0);')
472         print('assert(zcmp(a, d), == 0);')
473         print('zsets(b, "%i");' % b)
474         print('zdivmod(b, a, b, b);')
475         print('assert(zcmpu(b, 1), == 0);')
476         print('assert(zcmpu(a, 0), == 0);')
477         print('zsets(b, "%i");' % b)
478         print('zdivmod(a, b, b, b);')
479         print('assert(zcmpu(a, 1), == 0);')
480         print('assert(zcmpu(b, 0), == 0);')
481         print('zsets(a, "%i");' % a)
482         print('zsets(b, "%i");' % b)
483         print('zdivmod(a, d, a, b);')
484         print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
485         print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
486         print('zsets(a, "%i");' % a)
487         print('zdivmod(c, b, a, b);')
488         print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
489         print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
490         a = bp * a_sign
491         b = bp * b_sign
492         (c, d) = (div(a, b), mod(a, b))
493         print('zsets(a, "%i");' % a)
494         print('zsets(b, "%i");' % b)
495         print('zdivmod(c, d, a, b);')
496         print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
497         print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
498         print('zdivmod(a, b, a, b);')
499         print('assert(zcmp(a, c), == 0);')
500         print('assert(zcmp(b, d), == 0);')
501         print('zsets(a, "%i");' % a)
502         print('zsets(b, "%i");' % b)
503         print('zdivmod(b, a, a, b);')
504         print('assert(zcmp(b, c), == 0);')
505         print('assert(zcmp(a, d), == 0);')
506         print('zsets(b, "%i");' % b)
507         print('zdivmod(b, a, b, b);')
508         print('assert(zcmpu(b, 1), == 0);')
509         print('assert(zcmpu(a, 0), == 0);')
510         print('zsets(b, "%i");' % b)
511         print('zdivmod(a, b, b, b);')
512         print('assert(zcmpu(a, 1), == 0);')
513         print('assert(zcmpu(b, 0), == 0);')
514         print('zsets(a, "%i");' % a)
515         print('zsets(b, "%i");' % b)
516         print('zdivmod(a, d, a, b);')
517         print('assert_s(zstr(a, buf, BUF_N), "%i");' % c)
518         print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
519         print('zsets(a, "%i");' % a)
520         print('zdivmod(c, b, a, b);')
521         print('assert_s(zstr(c, buf, BUF_N), "%i");' % c)
522         print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
523
524 def zmul():
525     bits = random.randint(0, LIMIT)
526     a = random.randint(-(1 << bits), 1 << bits)
527     bits = random.randint(0, LIMIT)
528     b = random.randint(-(1 << bits), 1 << bits)
529     c = a * b
530     print('zsets(a, "%i");' % a)
531     print('zsets(b, "%i");' % b)
532     print('zsets(d, "%i");' % c)
533     print('zmul(c, a, b);')
534     print('assert(zcmp(c, d), == 0);')
535     print('zmul(c, b, a);')
536     print('assert(zcmp(c, d), == 0);')
537     print('zmul(a, a, b);')
538     print('assert(zcmp(a, d), == 0);')
539     print('zsets(a, "%i");' % a)
540     print('zmul(b, a, b);')
541     print('assert(zcmp(b, d), == 0);')
542     c = a * a
543     print('zsets(d, "%i");' % c)
544     print('zmul(c, a, a);')
545     print('assert(zcmp(c, d), == 0);')
546     print('zmul(a, a, a);')
547     print('assert(zcmp(a, d), == 0);')
548
549 def zsqr():
550     bits = random.randint(0, LIMIT)
551     a = random.randint(-(1 << bits), 1 << bits)
552     c = a * a
553     print('zsets(a, "%i");' % a)
554     print('zsets(d, "%i");' % c)
555     print('zsqr(c, a);')
556     print('assert(zcmp(c, d), == 0);')
557     print('zsqr(a, a);')
558     print('assert(zcmp(a, d), == 0);')
559
560 def zmodmul():
561     bits = random.randint(0, LIMIT)
562     a = random.randint(-(1 << bits), 1 << bits)
563     bits = random.randint(0, LIMIT)
564     b = random.randint(-(1 << bits), 1 << bits)
565     bits = random.randint(0, LIMIT)
566     c = 0
567     while c == 0:
568         c = random.randint(-(1 << bits), 1 << bits)
569     d = mod(a * b, c)
570     print('zsets(a, "%i");' % a)
571     print('zsets(b, "%i");' % b)
572     print('zsets(c, "%i");' % c)
573     print('zmodmul(d, a, b, c);')
574     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
575     print('zmodmul(a, a, b, c);')
576     print('assert_s(zstr(a, buf, BUF_N), "%i");' % d)
577     print('zsets(a, "%i");' % a)
578     print('zmodmul(b, a, b, c);')
579     print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
580     print('zsets(b, "%i");' % b)
581     print('zmodmul(c, a, b, c);')
582     print('assert_s(zstr(c, buf, BUF_N), "%i");' % d)
583     print('zsets(c, "%i");' % c)
584     print('zmodmul(d, b, a, c);')
585     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
586     print('zmodmul(a, b, a, c);')
587     print('assert_s(zstr(a, buf, BUF_N), "%i");' % d)
588     print('zsets(a, "%i");' % a)
589     print('zmodmul(b, b, a, c);')
590     print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
591     print('zsets(b, "%i");' % b)
592     print('zmodmul(c, b, a, c);')
593     print('assert_s(zstr(c, buf, BUF_N), "%i");' % d)
594     print('zsets(c, "%i");' % c)
595     d = mod(a * a, c)
596     print('zmodmul(d, a, a, c);')
597     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
598     print('zmodmul(a, a, a, c);')
599     print('assert_s(zstr(a, buf, BUF_N), "%i");' % d)
600     print('zsets(a, "%i");' % a)
601     print('zmodmul(c, a, a, c);')
602     print('assert_s(zstr(c, buf, BUF_N), "%i");' % d)
603     if a != 0:
604         d = mod(a * b, a)
605         print('zsets(d, "%i");' % d)
606         print('zmodmul(c, a, b, a);')
607         print('assert_s(zstr(c, buf, BUF_N), "%i");' % d)
608         print('zmodmul(a, a, b, a);')
609         print('assert_s(zstr(a, buf, BUF_N), "%i");' % d)
610         print('zsets(a, "%i");' % a)
611         print('zmodmul(b, a, b, a);')
612         print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
613         print('zsets(b, "%i");' % b)
614         print('zmodmul(c, b, a, a);')
615         print('assert_s(zstr(c, buf, BUF_N), "%i");' % d)
616         print('zmodmul(a, b, a, a);')
617         print('assert_s(zstr(a, buf, BUF_N), "%i");' % d)
618         print('zsets(a, "%i");' % a)
619         print('zmodmul(b, b, a, a);')
620         print('assert_s(zstr(b, buf, BUF_N), "%i");' % d)
621         print('zmodmul(b, a, a, a);')
622         print('assert(zzero(b), == 1);')
623         print('zmodmul(a, a, a, a);')
624         print('assert(zzero(a), == 1);')
625
626 def zmodsqr():
627     bits = random.randint(0, LIMIT)
628     a = random.randint(-(1 << bits), 1 << bits)
629     bits = random.randint(0, LIMIT)
630     b = 0
631     while b == 0:
632         b = random.randint(-(1 << bits), 1 << bits)
633     c = mod(a ** 2, b)
634     print('zsets(a, "%i");' % a)
635     print('zsets(b, "%i");' % b)
636     print('zsets(d, "%i");' % c)
637     print('zmodsqr(c, a, b);')
638     print('assert(zcmp(c, d), == 0);')
639     print('zset(c, a);')
640     print('zmodsqr(a, a, b);')
641     print('assert(zcmp(a, d), == 0);')
642     print('zset(a, c);')
643     print('zset(c, b);')
644     print('zmodsqr(b, a, b);')
645     print('assert(zcmp(b, d), == 0);')
646     if a != 0:
647         c = mod(a ** 2, a)
648         print('zmodsqr(b, a, a);')
649         print('assert(zzero(b), == 1);')
650         print('zmodsqr(a, a, a);')
651         print('assert(zzero(a), == 1);')
652
653 def zcmpi():
654     bits = random.randint(0, LIMIT)
655     a = random.randint(-(1 << bits), 1 << bits)
656     b = random.randint(-(1 << 63), (1 << 63) - 1)
657     c = -1 if a < b else (1 if a > b else 0)
658     print('zsets(a, "%i");' % a)
659     if b >= 0:
660         print('assert(zcmpi(a, %iLL), == %i);' % (b, c))
661     else:
662         print('assert(zcmpi(a, %iLL - 1LL), == %i);' % (b + 1, c))
663
664 def zcmpu():
665     bits = random.randint(0, LIMIT)
666     a = random.randint(-(1 << bits), 1 << bits)
667     b = random.randint(0, (1 << 64) - 1)
668     c = -1 if a < b else (1 if a > b else 0)
669     print('zsets(a, "%i");' % a)
670     print('assert(zcmpu(a, %iULL), == %i);' % (b, c))
671
672 def zgcd():
673     bits = random.randint(0, LIMIT)
674     a = random.randint(-(1 << bits), 1 << bits)
675     bits = random.randint(0, LIMIT)
676     b = random.randint(-(1 << bits), 1 << bits)
677     c = gcd(a, b)
678     print('zsets(a, "%i");' % a)
679     print('zsets(b, "%i");' % b)
680     print('zsets(d, "%i");' % c)
681     print('zgcd(c, a, b);')
682     print('assert(zcmp(c, d), == 0);')
683
684 def zpow():
685     bits = random.randint(0, LIMIT)
686     a = random.randint(-(1 << bits), 1 << bits)
687     b = random.randint(1, 16)
688     c = a ** b
689     print('zsets(a, "%i");' % a)
690     print('zsetu(b, %i);' % b)
691     print('zsets(d, "%i");' % c)
692     print('zpow(c, a, b);')
693     print('zpow(a, a, b);')
694     print('assert(zcmp(c, d), == 0);')
695     print('assert(zcmp(a, d), == 0);')
696     print('zsets(a, "%i");' % a)
697     print('zpow(b, a, b);')
698     print('assert(zcmp(b, d), == 0);')
699
700 def zpowu():
701     bits = random.randint(0, LIMIT)
702     a = random.randint(-(1 << bits), 1 << bits)
703     b = random.randint(1, 16)
704     c = a ** b
705     print('zsets(a, "%i");' % a)
706     print('zsets(d, "%i");' % c)
707     print('zpowu(c, a, %i);' % b)
708     print('zpowu(a, a, %i);' % b)
709     print('assert(zcmp(c, d), == 0);')
710     print('assert(zcmp(a, d), == 0);')
711
712 def zmodpowu():
713     bits = random.randint(0, LIMIT)
714     a = random.randint(-(1 << bits), 1 << bits)
715     b = random.randint(1, 16)
716     bits = random.randint(0, LIMIT)
717     c = 0
718     while c == 0:
719         c = random.randint(-(1 << bits), 1 << bits)
720     d = mod(a ** b, c)
721     print('zsets(a, "%i");' % a)
722     print('zsets(c, "%i");' % c)
723     print('zsets(d, "%i");' % d)
724     print('zmodpowu(b, a, %i, c);' % b)
725     print('zmodpowu(a, a, %i, c);' % b)
726     print('assert(zcmp(b, d), == 0);')
727     print('assert(zcmp(a, d), == 0);')
728     print('zsets(a, "%i");' % a)
729     print('zmodpowu(c, a, %i, c);' % b)
730     print('assert(zcmp(c, d), == 0);')
731
732 def zmodpow():
733     bits = random.randint(0, LIMIT)
734     a = random.randint(-(1 << bits), 1 << bits)
735     b = random.randint(1, 16)
736     bits = random.randint(0, LIMIT)
737     c = 0
738     while c == 0:
739         c = random.randint(-(1 << bits), 1 << bits)
740     d = mod(a ** b, c)
741     print('zsets(a, "%i");' % a)
742     print('zsets(b, "%i");' % b)
743     print('zsets(c, "%i");' % c)
744     print('zsets(d, "%i");' % d)
745     print('zmodpow(d, a, b, c);')
746     print('zmodpow(a, a, b, c);')
747     print('assert_s(zstr(d, buf, BUF_N), "%i");' % d)
748     print('assert(zcmp(a, d), == 0);')
749     print('zsets(a, "%i");' % a)
750     print('zmodpow(b, a, b, c);')
751     print('assert(zcmp(b, d), == 0);')
752     print('zsets(b, "%i");' % b)
753     print('zmodpow(c, a, b, c);')
754     print('assert(zcmp(c, d), == 0);')
755
756 def znot():
757     bits = random.randint(0, LIMIT)
758     a = random.randint(-(1 << bits), 1 << bits)
759     sign = -(-1 if a < 0 else 1)
760     b = abs(a)
761     bits = 0
762     x = b
763     while x > 0:
764         bits += 1
765         x >>= 1
766     b = ~b
767     b &= (1 << bits) - 1
768     b *= sign
769     print('zsets(a, "%i");' % a)
770     print('zsets(c, "%i");' % b)
771     print('znot(b, a);')
772     print('znot(a, a);')
773     print('assert(zcmp(b, c), == 0);')
774     print('assert(zcmp(a, c), == 0);')
775
777     bits = random.randint(0, LIMIT)
778     a = random.randint(-(1 << bits), 1 << bits)
779     print('zsets(a, "%i");' % a)
780     print('n = zsave(a, 0);')
781     print('assert_zu(zsave(a, buf), n);')
783     print('assert(zcmp(a, b), == 0);')
784
785
786
787 functions = [zzero, zsignum, zeven_nonzero, zodd_nonzero, zeven, zcmp, zcmpi, zcmpu, zcmpmag,
788              zodd, zabs, zneg, zlsh, zrsh, ztrunc, zsplit, zand, zor, zxor, zbits, zlsb, znot,
790              zdiv, zmod, zmodmul, zmodsqr, zsave_zload, zgcd, zpow, zpowu, zmodpow, zmodpowu,
791              zstr_length, zstr] # untested: zptest, zrand
792
793 limits = [200]
794
795 for LIMIT in limits:
796     for function in functions:
797         print('/* %s */' % function.__qualname__)
798         for i in range(100):
799             function()
800             print()
801         print()
802
```