-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmp_int.m
1246 lines (1082 loc) · 33.4 KB
/
mp_int.m
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
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
%
% File: mp_int.m.
% Main author: Matthias Güdemann.
% Stability: low.
%
% This module implements a binding to libtomath.
%
% This library provides a portable ISO C implementation of multi precision
% integers. libtommath is in the public domain and its source code is available
% from https://github.com/libtom/libtommath.
%
% To use the provided binding, one needs the compiled library and the .h
% include files, see README.txt for the details.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module mp_int.
:- interface.
:- type mp_int.
% Addition.
%
:- func '+'(mp_int, mp_int) = mp_int.
% Subtraction.
%
:- func '-'(mp_int, mp_int) = mp_int.
% Unary minus.
%
:- func '-'(mp_int) = mp_int.
% Multiplication.
%
:- func '*'(mp_int, mp_int) = mp_int.
% Truncating integer division, e.g., (-10) // 3 = (-3).
%
:- func '//'(mp_int, mp_int) = mp_int.
% Remainder.
% X rem Y = X - (X // Y) * Y
%
:- func 'rem'(mp_int, mp_int) = mp_int.
% Absolute value.
%
:- func abs(mp_int) = mp_int.
% Squaring.
%
:- func square(mp_int) = mp_int.
% Truncating integer division with remainder.
%
:- pred divide_with_rem(mp_int::in, mp_int::in, mp_int::out, mp_int::out)
is det.
% Multiplication by 2.
%
:- pred multiply_by_2(mp_int::in, mp_int::out) is det.
% Division by 2.
%
:- pred divide_by_2(mp_int::in, mp_int::out) is det.
% Shift Left.
%
:- func '<<'(mp_int, int) = mp_int.
% Shift Right.
%
:- func '>>'(mp_int, int) = mp_int.
% is_zero(X) if X is 0.
%
:- pred is_zero(mp_int::in) is semidet.
% is_even(X) if X is even.
%
:- pred is_even(mp_int::in) is semidet.
% is_odd(X) if X is odd.
%
:- pred is_odd(mp_int::in) is semidet.
% is_negative(X) if X is negative.
%
:- pred is_negative(mp_int::in) is semidet.
% Greater than.
%
:- pred '>'(mp_int::in, mp_int::in) is semidet.
% Less than.
%
:- pred '<'(mp_int::in, mp_int::in) is semidet.
% Greater or equal.
%
:- pred '>='(mp_int::in, mp_int::in) is semidet.
% Less or equal.
%
:- pred '=<'(mp_int::in, mp_int::in) is semidet.
% Equal.
%
:- pred equal(mp_int::in, mp_int::in) is semidet.
% Exponentiation.
% Throws exception `exception.domain_error` if Y is negative.
%
:- func pow(mp_int, mp_int) = mp_int.
% Convert mp_int to int.
% Fails if not inside [min_int, max_int] interval.
%
:- pred to_int(mp_int::in, int::out) is semidet.
% As above, but throws exception if value is outside
% [min_int, max_int] interval.
%
:- func det_to_int(mp_int) = int.
% to_base_string(Mp_Int, Base) = String:
%
% Convert mp_int to a string in given base.
%
% Base must be between 2 and 64, inclusive; if it is not, the predicate
% will throw an exception.
%
:- func to_base_string(mp_int, int) = string.
% Convert mp_int to a string in base 10.
%
:- func to_string(mp_int) = string.
% from_base_string(String, Base, Mp_Int):
%
% Convert string in given base to mp_int.
%
% Base must be between 2 and 64, inclusive; fails if unsuccessful.
%
:- pred from_base_string(string::in, int::in, mp_int::out) is semidet.
% Convert string in base 10 to mp_int. Fails if unsuccessful.
%
:- pred from_string(string::in, mp_int::out) is semidet.
% As above, throws exception instead of failing if unsuccessful.
%
:- func det_from_string(string) = mp_int.
% As above, throws exception instead of failing if unsuccessful.
%
:- func det_from_base_string(string, int) = mp_int.
% Convert an int to an mp_int.
%
:- func mp_int(int) = mp_int.
% Square root of mp_int.
%
% sqrt(X, Sqrt) is true if Sqrt is the positive square root of X.
% Fails if X is negative.
%
:- pred sqrt(mp_int::in, mp_int::out) is semidet.
% As above, but throws error in case of negative value.
%
:- func det_sqrt(mp_int) = mp_int.
% Bitwise or.
%
:- func mp_int \/ mp_int = mp_int.
% Bitwise and.
%
:- func mp_int /\ mp_int = mp_int.
% Bitwise xor.
%
:- func mp_int `xor` mp_int = mp_int.
% Bitwise complement.
%
:- func \ mp_int = mp_int.
% Greatest common divisor.
%
:- func gcd(mp_int, mp_int) = mp_int.
% Least common multiple.
%
:- func lcm(mp_int, mp_int) = mp_int.
% jacobi(A, N) = C:
%
% Computes Jacobi symbol.
%
% C = J(A, N) = L(A, P_1)^(i_1) * ... * L(A, P_k)^(i_k) where
%
% A = P_1^(i_1) * ... * P_k^(i_k) with P_j is prime, and
%
% / 1, if a is a quadratic residue modulo p, and a \= 0 (mod p)
% L(A, P) = | -1, if a is a quadratic non-residue modulo p
% \ 0, if a is a multiple of p
%
:- func jacobi(mp_int, mp_int) = int.
% invmod(A, B) = C:
%
% Modular inverse C = A^(-1) mod B
%
:- func invmod(mp_int, mp_int) = mp_int.
% exptmod(A, B, C, D):
%
% Modular exponentiation D = A^B mod C.
%
:- func exptmod(mp_int, mp_int, mp_int) = mp_int.
% Probabilistic primality test.
%
:- pred is_prime(mp_int::in) is semidet.
% Probabilistic primality test with given number of rounds. Probability of
% reporting composite number for a prime is ca. (1/4)^(-#Rounds)
%
:- pred is_prime(mp_int::in, int::in) is semidet.
% Constant 0.
%
:- func zero = mp_int.
% Constant 1.
%
:- func one = mp_int.
% Constant 2.
%
:- func two = mp_int.
% Constant -1.
%
:- func negative_one = mp_int.
% Constant 10.
%
:- func ten = mp_int.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module exception.
:- import_module int.
:- import_module require.
%---------------------------------------------------------------------------%
% foreign declarations
%---------------------------------------------------------------------------%
% Type declaration for foreign type mp_int*.
%
:- pragma foreign_type("C", mp_int, "mp_int*")
where equality is equal, comparison is mp_cmp.
:- pragma foreign_decl("C",
"#include \"tommath.h\"").
% We assume unsigned long long to be at least as big as MR_Integer.
% This is currently required for the to_int predicates.
%
:- pragma foreign_code("C", "
MR_STATIC_ASSERT(mp_int, sizeof(unsigned long long) >= sizeof(MR_Integer));
").
% Result type to signal success or failure of external functions.
%
:- type mp_result_type --->
mp_result_okay
; mp_result_out_of_mem
; mp_result_invalid_input.
% mapping of libtommath results to Mercury enum.
:- pragma foreign_enum("C", mp_result_type/0,
[
mp_result_okay - "MP_OKAY",
mp_result_out_of_mem - "MP_MEM",
mp_result_invalid_input - "MP_VAL"
]).
%---------------------------------------------------------------------------%
% initialisation code to create static mp_ints for often used constants
%---------------------------------------------------------------------------%
:- initialise mp_initialize/0.
:- impure pred mp_initialize is det.
:- pragma foreign_decl("C",
"
extern mp_int MP_INT_constant_negative_one;
extern mp_int MP_INT_constant_zero;
extern mp_int MP_INT_constant_one;
extern mp_int MP_INT_constant_two;
extern mp_int MP_INT_constant_ten;
").
:- pragma foreign_code("C",
"
mp_int MP_INT_constant_negative_one;
mp_int MP_INT_constant_zero;
mp_int MP_INT_constant_one;
mp_int MP_INT_constant_two;
mp_int MP_INT_constant_ten;
").
:- pragma foreign_proc("C",
mp_initialize,
[will_not_call_mercury, thread_safe],
"
mp_init_set(&MP_INT_constant_negative_one, -1);
mp_init_set(&MP_INT_constant_zero, 0);
mp_init_set(&MP_INT_constant_one, 1);
mp_init_set(&MP_INT_constant_two, 2);
mp_init_set(&MP_INT_constant_ten, 10);
").
%---------------------------------------------------------------------------%
% module internal predicate declarations
%---------------------------------------------------------------------------%
:- pred mp_init(int::in, mp_int::out) is det.
mp_init(N, Res) :-
mp_init(N, Result, Res0),
( Result = mp_result_okay ->
Res = Res0
;
error("could not initialize mp_int")
).
:- pred mp_init(int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_init(Value::in, Result::out, Mp_Int::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Mp_Int = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(Mp_Int);
if (Result == MP_OKAY)
Result = mp_set_long_long(Mp_Int, Value);
").
%---------------------------------------------------------------------------%
% basic arithmetic
%---------------------------------------------------------------------------%
:- pred mp_add(mp_int::in, mp_int::in, mp_int::out) is det.
mp_add(A, B, C) :-
mp_add(A, B, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.mp_add: could not add"))
).
:- pred mp_add(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_add(A::in, B::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_add(A, B, C);
").
:- pred mp_sub(mp_int::in, mp_int::in, mp_int::out) is det.
mp_sub(A, B, C) :-
mp_sub(A, B, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.mp_sub: could not subtract"))
).
:- pred mp_sub(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_sub(A::in, B::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_sub(A, B, C);
").
:- pred mp_neg(mp_int::in, mp_int::out) is det.
mp_neg(A, C) :-
mp_neg(A, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.mp_neg: could not negate value"))
).
:- pred mp_neg(mp_int::in, mp_result_type::out, mp_int::out)is det.
:- pragma foreign_proc("C",
mp_neg(A::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_neg(A, C);
").
:- pred mp_abs(mp_int::in, mp_int::out) is det.
mp_abs(A, C) :-
mp_abs(A, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error(
"mp_int.mp_abs: could not compute absolute value"))
).
:- pred mp_abs(mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_abs(A::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_abs(A, C);
").
abs(A) = Res :- mp_abs(A, Res).
:- pred mp_mul(mp_int::in, mp_int::in, mp_int::out) is det.
mp_mul(A, B, C) :-
mp_mul(A, B, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.mp_mul: could not multiply"))
).
:- pred mp_mul(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_mul(A::in, B::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_mul(A, B, C);
").
multiply_by_2(A, C) :-
mp_mul_2(A, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.multiply_by_2: could not double value"))
).
:- pred mp_mul_2(mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_mul_2(A::in, Result::out, B::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
B = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(B);
if (Result == MP_OKAY)
Result = mp_mul_2(A, B);
").
divide_by_2(A, C) :-
mp_div_2(A, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.divide_by_2: could not halve value"))
).
:- pred mp_div_2(mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_div_2(A::in, Result::out, B::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
B = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(B);
if (Result == MP_OKAY)
Result = mp_div_2(A, B);
").
divide_with_rem(A, B, Quot, Rem) :-
( is_zero(B) ->
throw(domain_error("mp_int.quot_with_rem: division by zero"))
;
mp_quot_rem(A, B, Result, Quot0, Rem0),
(
Result = mp_result_okay,
Quot = Quot0,
Rem = Rem0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error(
"mp_int.quot_with_rem: could not compute quotient and remainder"))
)
).
:- pred mp_quot_rem(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out,
mp_int::out) is det.
:- pragma foreign_proc("C",
mp_quot_rem(A::in, B::in, Result::out,
Quot::out, Rem::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Quot = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Rem = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(Quot);
if (Result == MP_OKAY) {
Result = mp_init(Rem);
if (Result == MP_OKAY) {
Result = mp_div(A, B, Quot, Rem);
}
}
").
rem(A, B) = Res :-
( is_zero(B) ->
throw(domain_error("mp_int.rem: division by zero"))
;
mp_rem(A, B, Result, Rem0),
(
Result = mp_result_okay,
Res = Rem0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error(
"mp_int.rem: could not compute remainder"))
)
).
:- pred mp_rem(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_rem(A::in, B::in, Result::out, Rem::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Rem = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(Rem);
if (Result == MP_OKAY)
Result = mp_div(A, B, NULL, Rem);
").
:- func quotient(mp_int, mp_int) = mp_int.
quotient(A, B) = Res :-
( is_zero(B) ->
throw(domain_error("mp_int.quotient: division by zero"))
;
mp_quot(A, B, Result, Quot0),
(
Result = mp_result_okay,
Res = Quot0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error(
"mp_int.quotient: could not compute quotient"))
)
).
:- pred mp_quot(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out)
is det.
:- pragma foreign_proc("C",
mp_quot(A::in, B::in, Result::out, Quot::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Quot = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(Quot);
if (Result == MP_OKAY)
Result = mp_div(A, B, Quot, NULL);
").
:- pred mp_square(mp_int::in, mp_int::out) is det.
mp_square(A, C) :-
mp_square(A, Result, C0),
(
Result = mp_result_okay,
C = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.mp_square: could not square"))
).
:- pred mp_square(mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_square(A::in, Result::out, A_SQ::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
A_SQ = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(A_SQ);
if (Result == MP_OKAY)
Result = mp_sqr(A, A_SQ);
").
%---------------------------------------------------------------------------%
% conversion predicates
%---------------------------------------------------------------------------%
:- func min_int = mp_int.
min_int = mp_int(int.min_int).
:- func max_int = mp_int.
max_int = mp_int(int.max_int).
to_int(A, N) :-
( ( A >= min_int, A =< max_int) ->
mp_to_long(A, N)
;
fail
).
:- pred mp_to_long(mp_int::in, int::out)
is det.
:- pragma foreign_proc("C",
mp_to_long(A::in, N::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
N = mp_get_long_long(A);
").
det_to_int(A) = Res :-
( to_int(A, Res0) ->
Res0 = Res
;
throw(domain_error("mp_int.det_to_int: not in int range"))
).
to_base_string(A, Radix) = S :-
mp_to_string(A, Radix, Result, S0),
( Result = mp_result_okay ->
S = S0
;
error("could not convert mp_int to string")
).
:- pred mp_to_string(mp_int::in, int::in, mp_result_type::out, string::out)
is det.
:- pragma foreign_proc("C",
mp_to_string(A::in, Radix::in, Result::out, S::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
int length;
Result = mp_radix_size(A, Radix, &length);
if (Result == MP_OKAY)
{
MR_allocate_aligned_string_msg(S, length, MR_ALLOC_ID);
Result = mp_toradix(A, S, Radix);
}
").
to_string(A) = to_base_string(A, 10).
from_base_string(S, Radix, A) :-
mp_from_string(S, Radix, Result, A0),
(
Result = mp_result_okay,
A = A0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
fail
).
:- pred mp_from_string(string::in, int::in, mp_result_type::out, mp_int::out)
is det.
:- pragma foreign_proc("C",
mp_from_string(S::in, Radix::in, Result::out, A::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
A = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(A);
if (Result == MP_OKAY)
Result = mp_read_radix(A, S, Radix);
").
from_string(S, Res) :- from_base_string(S, 10, Res).
det_from_string(S) = Res :-
( from_base_string(S, 10, Res0) ->
Res = Res0
;
error("could not create mp_int from string")
).
det_from_base_string(S, Base) = Res :-
( from_base_string(S, Base, Res0) ->
Res = Res0
;
error("could not create mp_int from string")
).
mp_int(N) = Res :-
( N < 0 ->
( N = min_int ->
% Avoid `-min_int' as it overflows.
mp_init(-(min_int + 1), M),
Res = -(M + one)
;
mp_init(-N, M),
Res = -M
)
;
mp_init(N, Res)
).
%---------------------------------------------------------------------------%
% bit shifting
%---------------------------------------------------------------------------%
A << N = Res :-
mp_shift_left(A, N, Result, A0),
(
Result = mp_result_okay,
Res = A0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.shift_left: could not shift"))
).
:- pred mp_shift_left(mp_int::in, int::in, mp_result_type::out, mp_int::out)
is det.
:- pragma foreign_proc("C",
mp_shift_left(A::in, N::in, Result::out, B::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
B = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init_copy(B, A);
if (Result == MP_OKAY)
Result = mp_lshd(B, N);
").
A >> N = Res :-
mp_shift_right(A, N, Result, A0),
(
Result = mp_result_okay,
Res = A0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.shift_right: could not shift"))
).
:- pred mp_shift_right(mp_int::in, int::in, mp_result_type::out, mp_int::out)
is det.
:- pragma foreign_proc("C",
mp_shift_right(A::in, N::in, Result::out, B::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
B = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init_copy(B, A);
if (Result == MP_OKAY)
mp_rshd(B, N);
").
%---------------------------------------------------------------------------%
% test predicates
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
is_zero(A::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
SUCCESS_INDICATOR = mp_iszero(A) ? MR_TRUE : MR_FALSE;
").
:- pragma foreign_proc("C",
is_even(A::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
SUCCESS_INDICATOR = mp_iseven(A) ? MR_TRUE : MR_FALSE;
").
:- pragma foreign_proc("C",
is_odd(A::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
SUCCESS_INDICATOR = mp_isodd(A) ? MR_TRUE : MR_FALSE;
").
:- pragma foreign_proc("C",
is_negative(A::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
SUCCESS_INDICATOR = mp_isneg(A) ? MR_TRUE : MR_FALSE;
").
%---------------------------------------------------------------------------%
% comparison predicates
%---------------------------------------------------------------------------%
:- pred mp_cmp(comparison_result::uo, mp_int::in, mp_int::in) is det.
:- pragma foreign_proc("C",
mp_cmp(C::uo, A::in, B::in),
[will_not_call_mercury, promise_pure, thread_safe],
"
int result;
result = mp_cmp(A, B);
if (result == MP_LT)
C = MR_COMPARE_LESS;
else
{
if (result == MP_GT)
C = MR_COMPARE_GREATER;
else
C = MR_COMPARE_EQUAL;
}
").
A > B :- mp_cmp((>), A, B).
A < B :- mp_cmp((<), A, B).
A >= B :-
mp_cmp(C, A, B),
( C = (>); C = (=)).
A =< B :-
mp_cmp(C, A, B),
( C = (<); C = (=)).
equal(A, B) :- mp_cmp((=), A, B).
%---------------------------------------------------------------------------%
A + B = C :- mp_add(A, B, C).
A - B = C :- mp_sub(A, B, C).
-A = C :- mp_neg(A, C).
A * B = C :- mp_mul(A, B, C).
A // B = quotient(A, B).
square(X) = Res :- mp_square(X, Res).
%---------------------------------------------------------------------------%
% higher level functions
%---------------------------------------------------------------------------%
pow(A, N) = Res :-
( is_zero(N) ->
Res = one
; is_odd(N) ->
Res = A * pow(A, N - one)
;
divide_by_2(N, N0),
SQ = pow(A, N0),
mp_square(SQ, Res)
).
%---------------------------------------------------------------------------%
% number theoretic functions
%---------------------------------------------------------------------------%
gcd(A, B) = Res :-
mp_gcd(A, B, Result, C0),
(
Result = mp_result_okay ,
Res = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.gcd: could not compute gcd"))
).
:- pred mp_gcd(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_gcd(A::in, B::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_gcd(A, B, C);
").
lcm(A, B) = Res :-
mp_lcm(A, B, Result, C0),
(
Result = mp_result_okay ,
Res = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")
;
Result = mp_result_invalid_input,
throw(domain_error("mp_int.lcm: could not compute lcm"))
).
:- pred mp_lcm(mp_int::in, mp_int::in, mp_result_type::out, mp_int::out) is det.
:- pragma foreign_proc("C",
mp_lcm(A::in, B::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
C = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);
Result = mp_init(C);
if (Result == MP_OKAY)
Result = mp_lcm(A, B, C);
").
jacobi(A, P) = Res :-
mp_jacobi(A, P, Result, C0),
( Result = mp_result_okay ->
Res = C0
;
throw(domain_error(
"mp_int.jacobi: could not compute Jacobi symbol of mp_int"))
).
:- pred mp_jacobi(mp_int::in, mp_int::in, mp_result_type::out, int::out) is det.
:- pragma foreign_proc("C",
mp_jacobi(A::in, P::in, Result::out, C::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
int res;
Result = mp_jacobi(A, P, &res);
C = res;
").
invmod(A, B) = Res :-
mp_invmod(A, B, Result, C0),
(
Result = mp_result_okay ,
Res = C0
;
Result = mp_result_out_of_mem,
error("could not initialize mp_int")