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
|
# ===================================================================
#
# Copyright (c) 2014, Legrandin <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ===================================================================
import sys
from Cryptodome.Util.py3compat import tobytes, is_native_int
from Cryptodome.Util._raw_api import (backend, load_lib,
get_raw_buffer, get_c_string,
null_pointer, create_string_buffer,
c_ulong, c_size_t, c_uint8_ptr)
from ._IntegerBase import IntegerBase
gmp_defs = """typedef unsigned long UNIX_ULONG;
typedef struct { int a; int b; void *c; } MPZ;
typedef MPZ mpz_t[1];
typedef UNIX_ULONG mp_bitcnt_t;
void __gmpz_init (mpz_t x);
void __gmpz_init_set (mpz_t rop, const mpz_t op);
void __gmpz_init_set_ui (mpz_t rop, UNIX_ULONG op);
UNIX_ULONG __gmpz_get_ui (const mpz_t op);
void __gmpz_set (mpz_t rop, const mpz_t op);
void __gmpz_set_ui (mpz_t rop, UNIX_ULONG op);
void __gmpz_add (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_add_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2);
void __gmpz_sub_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2);
void __gmpz_addmul (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_addmul_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2);
void __gmpz_submul_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2);
void __gmpz_import (mpz_t rop, size_t count, int order, size_t size,
int endian, size_t nails, const void *op);
void * __gmpz_export (void *rop, size_t *countp, int order,
size_t size,
int endian, size_t nails, const mpz_t op);
size_t __gmpz_sizeinbase (const mpz_t op, int base);
void __gmpz_sub (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_mul (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_mul_ui (mpz_t rop, const mpz_t op1, UNIX_ULONG op2);
int __gmpz_cmp (const mpz_t op1, const mpz_t op2);
void __gmpz_powm (mpz_t rop, const mpz_t base, const mpz_t exp, const
mpz_t mod);
void __gmpz_powm_ui (mpz_t rop, const mpz_t base, UNIX_ULONG exp,
const mpz_t mod);
void __gmpz_pow_ui (mpz_t rop, const mpz_t base, UNIX_ULONG exp);
void __gmpz_sqrt(mpz_t rop, const mpz_t op);
void __gmpz_mod (mpz_t r, const mpz_t n, const mpz_t d);
void __gmpz_neg (mpz_t rop, const mpz_t op);
void __gmpz_abs (mpz_t rop, const mpz_t op);
void __gmpz_and (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_ior (mpz_t rop, const mpz_t op1, const mpz_t op2);
void __gmpz_clear (mpz_t x);
void __gmpz_tdiv_q_2exp (mpz_t q, const mpz_t n, mp_bitcnt_t b);
void __gmpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d);
void __gmpz_mul_2exp (mpz_t rop, const mpz_t op1, mp_bitcnt_t op2);
int __gmpz_tstbit (const mpz_t op, mp_bitcnt_t bit_index);
int __gmpz_perfect_square_p (const mpz_t op);
int __gmpz_jacobi (const mpz_t a, const mpz_t b);
void __gmpz_gcd (mpz_t rop, const mpz_t op1, const mpz_t op2);
UNIX_ULONG __gmpz_gcd_ui (mpz_t rop, const mpz_t op1,
UNIX_ULONG op2);
void __gmpz_lcm (mpz_t rop, const mpz_t op1, const mpz_t op2);
int __gmpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2);
int __gmpz_divisible_p (const mpz_t n, const mpz_t d);
int __gmpz_divisible_ui_p (const mpz_t n, UNIX_ULONG d);
"""
if sys.platform == "win32":
raise ImportError("Not using GMP on Windows")
lib = load_lib("gmp", gmp_defs)
implementation = {"library": "gmp", "api": backend}
if hasattr(lib, "__mpir_version"):
raise ImportError("MPIR library detected")
# In order to create a function that returns a pointer to
# a new MPZ structure, we need to break the abstraction
# and know exactly what ffi backend we have
if implementation["api"] == "ctypes":
from ctypes import Structure, c_int, c_void_p, byref
class _MPZ(Structure):
_fields_ = [('_mp_alloc', c_int),
('_mp_size', c_int),
('_mp_d', c_void_p)]
def new_mpz():
return byref(_MPZ())
else:
# We are using CFFI
from Cryptodome.Util._raw_api import ffi
def new_mpz():
return ffi.new("MPZ*")
# Lazy creation of GMP methods
class _GMP(object):
def __getattr__(self, name):
if name.startswith("mpz_"):
func_name = "__gmpz_" + name[4:]
elif name.startswith("gmp_"):
func_name = "__gmp_" + name[4:]
else:
raise AttributeError("Attribute %s is invalid" % name)
func = getattr(lib, func_name)
setattr(self, name, func)
return func
_gmp = _GMP()
class IntegerGMP(IntegerBase):
"""A fast, arbitrary precision integer"""
_zero_mpz_p = new_mpz()
_gmp.mpz_init_set_ui(_zero_mpz_p, c_ulong(0))
def __init__(self, value):
"""Initialize the integer to the given value."""
self._mpz_p = new_mpz()
self._initialized = False
if isinstance(value, float):
raise ValueError("A floating point type is not a natural number")
if is_native_int(value):
_gmp.mpz_init(self._mpz_p)
self._initialized = True
if value == 0:
return
tmp = new_mpz()
_gmp.mpz_init(tmp)
try:
positive = value >= 0
reduce = abs(value)
slots = (reduce.bit_length() - 1) // 32 + 1
while slots > 0:
slots = slots - 1
_gmp.mpz_set_ui(tmp,
c_ulong(0xFFFFFFFF & (reduce >> (slots * 32))))
_gmp.mpz_mul_2exp(tmp, tmp, c_ulong(slots * 32))
_gmp.mpz_add(self._mpz_p, self._mpz_p, tmp)
finally:
_gmp.mpz_clear(tmp)
if not positive:
_gmp.mpz_neg(self._mpz_p, self._mpz_p)
elif isinstance(value, IntegerGMP):
_gmp.mpz_init_set(self._mpz_p, value._mpz_p)
self._initialized = True
else:
raise NotImplementedError
# Conversions
def __int__(self):
tmp = new_mpz()
_gmp.mpz_init_set(tmp, self._mpz_p)
try:
value = 0
slot = 0
while _gmp.mpz_cmp(tmp, self._zero_mpz_p) != 0:
lsb = _gmp.mpz_get_ui(tmp) & 0xFFFFFFFF
value |= lsb << (slot * 32)
_gmp.mpz_tdiv_q_2exp(tmp, tmp, c_ulong(32))
slot = slot + 1
finally:
_gmp.mpz_clear(tmp)
if self < 0:
value = -value
return int(value)
def __str__(self):
return str(int(self))
def __repr__(self):
return "Integer(%s)" % str(self)
# Only Python 2.x
def __hex__(self):
return hex(int(self))
# Only Python 3.x
def __index__(self):
return int(self)
def to_bytes(self, block_size=0, byteorder='big'):
"""Convert the number into a byte string.
This method encodes the number in network order and prepends
as many zero bytes as required. It only works for non-negative
values.
:Parameters:
block_size : integer
The exact size the output byte string must have.
If zero, the string has the minimal length.
byteorder : string
'big' for big-endian integers (default), 'little' for litte-endian.
:Returns:
A byte string.
:Raise ValueError:
If the value is negative or if ``block_size`` is
provided and the length of the byte string would exceed it.
"""
if self < 0:
raise ValueError("Conversion only valid for non-negative numbers")
buf_len = (_gmp.mpz_sizeinbase(self._mpz_p, 2) + 7) // 8
if buf_len > block_size > 0:
raise ValueError("Number is too big to convert to byte string"
" of prescribed length")
buf = create_string_buffer(buf_len)
_gmp.mpz_export(
buf,
null_pointer, # Ignore countp
1, # Big endian
c_size_t(1), # Each word is 1 byte long
0, # Endianess within a word - not relevant
c_size_t(0), # No nails
self._mpz_p)
result = b'\x00' * max(0, block_size - buf_len) + get_raw_buffer(buf)
if byteorder == 'big':
pass
elif byteorder == 'little':
result = bytearray(result)
result.reverse()
result = bytes(result)
else:
raise ValueError("Incorrect byteorder")
return result
@staticmethod
def from_bytes(byte_string, byteorder='big'):
"""Convert a byte string into a number.
:Parameters:
byte_string : byte string
The input number, encoded in network order.
It can only be non-negative.
byteorder : string
'big' for big-endian integers (default), 'little' for litte-endian.
:Return:
The ``Integer`` object carrying the same value as the input.
"""
result = IntegerGMP(0)
if byteorder == 'big':
pass
elif byteorder == 'little':
byte_string = bytearray(byte_string)
byte_string.reverse()
else:
raise ValueError("Incorrect byteorder")
_gmp.mpz_import(
result._mpz_p,
c_size_t(len(byte_string)), # Amount of words to read
1, # Big endian
c_size_t(1), # Each word is 1 byte long
0, # Endianess within a word - not relevant
c_size_t(0), # No nails
c_uint8_ptr(byte_string))
return result
# Relations
def _apply_and_return(self, func, term):
if not isinstance(term, IntegerGMP):
term = IntegerGMP(term)
return func(self._mpz_p, term._mpz_p)
def __eq__(self, term):
if not (isinstance(term, IntegerGMP) or is_native_int(term)):
return False
return self._apply_and_return(_gmp.mpz_cmp, term) == 0
def __ne__(self, term):
if not (isinstance(term, IntegerGMP) or is_native_int(term)):
return True
return self._apply_and_return(_gmp.mpz_cmp, term) != 0
def __lt__(self, term):
return self._apply_and_return(_gmp.mpz_cmp, term) < 0
def __le__(self, term):
return self._apply_and_return(_gmp.mpz_cmp, term) <= 0
def __gt__(self, term):
return self._apply_and_return(_gmp.mpz_cmp, term) > 0
def __ge__(self, term):
return self._apply_and_return(_gmp.mpz_cmp, term) >= 0
def __nonzero__(self):
return _gmp.mpz_cmp(self._mpz_p, self._zero_mpz_p) != 0
__bool__ = __nonzero__
def is_negative(self):
return _gmp.mpz_cmp(self._mpz_p, self._zero_mpz_p) < 0
# Arithmetic operations
def __add__(self, term):
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
try:
term = IntegerGMP(term)
except NotImplementedError:
return NotImplemented
_gmp.mpz_add(result._mpz_p,
self._mpz_p,
term._mpz_p)
return result
def __sub__(self, term):
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
try:
term = IntegerGMP(term)
except NotImplementedError:
return NotImplemented
_gmp.mpz_sub(result._mpz_p,
self._mpz_p,
term._mpz_p)
return result
def __mul__(self, term):
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
try:
term = IntegerGMP(term)
except NotImplementedError:
return NotImplemented
_gmp.mpz_mul(result._mpz_p,
self._mpz_p,
term._mpz_p)
return result
def __floordiv__(self, divisor):
if not isinstance(divisor, IntegerGMP):
divisor = IntegerGMP(divisor)
if _gmp.mpz_cmp(divisor._mpz_p,
self._zero_mpz_p) == 0:
raise ZeroDivisionError("Division by zero")
result = IntegerGMP(0)
_gmp.mpz_fdiv_q(result._mpz_p,
self._mpz_p,
divisor._mpz_p)
return result
def __mod__(self, divisor):
if not isinstance(divisor, IntegerGMP):
divisor = IntegerGMP(divisor)
comp = _gmp.mpz_cmp(divisor._mpz_p,
self._zero_mpz_p)
if comp == 0:
raise ZeroDivisionError("Division by zero")
if comp < 0:
raise ValueError("Modulus must be positive")
result = IntegerGMP(0)
_gmp.mpz_mod(result._mpz_p,
self._mpz_p,
divisor._mpz_p)
return result
def inplace_pow(self, exponent, modulus=None):
if modulus is None:
if exponent < 0:
raise ValueError("Exponent must not be negative")
# Normal exponentiation
if exponent > 256:
raise ValueError("Exponent is too big")
_gmp.mpz_pow_ui(self._mpz_p,
self._mpz_p, # Base
c_ulong(int(exponent))
)
else:
# Modular exponentiation
if not isinstance(modulus, IntegerGMP):
modulus = IntegerGMP(modulus)
if not modulus:
raise ZeroDivisionError("Division by zero")
if modulus.is_negative():
raise ValueError("Modulus must be positive")
if is_native_int(exponent):
if exponent < 0:
raise ValueError("Exponent must not be negative")
if exponent < 65536:
_gmp.mpz_powm_ui(self._mpz_p,
self._mpz_p,
c_ulong(exponent),
modulus._mpz_p)
return self
exponent = IntegerGMP(exponent)
elif exponent.is_negative():
raise ValueError("Exponent must not be negative")
_gmp.mpz_powm(self._mpz_p,
self._mpz_p,
exponent._mpz_p,
modulus._mpz_p)
return self
def __pow__(self, exponent, modulus=None):
result = IntegerGMP(self)
return result.inplace_pow(exponent, modulus)
def __abs__(self):
result = IntegerGMP(0)
_gmp.mpz_abs(result._mpz_p, self._mpz_p)
return result
def sqrt(self, modulus=None):
"""Return the largest Integer that does not
exceed the square root"""
if modulus is None:
if self < 0:
raise ValueError("Square root of negative value")
result = IntegerGMP(0)
_gmp.mpz_sqrt(result._mpz_p,
self._mpz_p)
else:
if modulus <= 0:
raise ValueError("Modulus must be positive")
modulus = int(modulus)
result = IntegerGMP(self._tonelli_shanks(int(self) % modulus, modulus))
return result
def __iadd__(self, term):
if is_native_int(term):
if 0 <= term < 65536:
_gmp.mpz_add_ui(self._mpz_p,
self._mpz_p,
c_ulong(term))
return self
if -65535 < term < 0:
_gmp.mpz_sub_ui(self._mpz_p,
self._mpz_p,
c_ulong(-term))
return self
term = IntegerGMP(term)
_gmp.mpz_add(self._mpz_p,
self._mpz_p,
term._mpz_p)
return self
def __isub__(self, term):
if is_native_int(term):
if 0 <= term < 65536:
_gmp.mpz_sub_ui(self._mpz_p,
self._mpz_p,
c_ulong(term))
return self
if -65535 < term < 0:
_gmp.mpz_add_ui(self._mpz_p,
self._mpz_p,
c_ulong(-term))
return self
term = IntegerGMP(term)
_gmp.mpz_sub(self._mpz_p,
self._mpz_p,
term._mpz_p)
return self
def __imul__(self, term):
if is_native_int(term):
if 0 <= term < 65536:
_gmp.mpz_mul_ui(self._mpz_p,
self._mpz_p,
c_ulong(term))
return self
if -65535 < term < 0:
_gmp.mpz_mul_ui(self._mpz_p,
self._mpz_p,
c_ulong(-term))
_gmp.mpz_neg(self._mpz_p, self._mpz_p)
return self
term = IntegerGMP(term)
_gmp.mpz_mul(self._mpz_p,
self._mpz_p,
term._mpz_p)
return self
def __imod__(self, divisor):
if not isinstance(divisor, IntegerGMP):
divisor = IntegerGMP(divisor)
comp = _gmp.mpz_cmp(divisor._mpz_p,
divisor._zero_mpz_p)
if comp == 0:
raise ZeroDivisionError("Division by zero")
if comp < 0:
raise ValueError("Modulus must be positive")
_gmp.mpz_mod(self._mpz_p,
self._mpz_p,
divisor._mpz_p)
return self
# Boolean/bit operations
def __and__(self, term):
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
term = IntegerGMP(term)
_gmp.mpz_and(result._mpz_p,
self._mpz_p,
term._mpz_p)
return result
def __or__(self, term):
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
term = IntegerGMP(term)
_gmp.mpz_ior(result._mpz_p,
self._mpz_p,
term._mpz_p)
return result
def __rshift__(self, pos):
result = IntegerGMP(0)
if pos < 0:
raise ValueError("negative shift count")
if pos > 65536:
if self < 0:
return -1
else:
return 0
_gmp.mpz_tdiv_q_2exp(result._mpz_p,
self._mpz_p,
c_ulong(int(pos)))
return result
def __irshift__(self, pos):
if pos < 0:
raise ValueError("negative shift count")
if pos > 65536:
if self < 0:
return -1
else:
return 0
_gmp.mpz_tdiv_q_2exp(self._mpz_p,
self._mpz_p,
c_ulong(int(pos)))
return self
def __lshift__(self, pos):
result = IntegerGMP(0)
if not 0 <= pos < 65536:
raise ValueError("Incorrect shift count")
_gmp.mpz_mul_2exp(result._mpz_p,
self._mpz_p,
c_ulong(int(pos)))
return result
def __ilshift__(self, pos):
if not 0 <= pos < 65536:
raise ValueError("Incorrect shift count")
_gmp.mpz_mul_2exp(self._mpz_p,
self._mpz_p,
c_ulong(int(pos)))
return self
def get_bit(self, n):
"""Return True if the n-th bit is set to 1.
Bit 0 is the least significant."""
if self < 0:
raise ValueError("no bit representation for negative values")
if n < 0:
raise ValueError("negative bit count")
if n > 65536:
return 0
return bool(_gmp.mpz_tstbit(self._mpz_p,
c_ulong(int(n))))
# Extra
def is_odd(self):
return _gmp.mpz_tstbit(self._mpz_p, 0) == 1
def is_even(self):
return _gmp.mpz_tstbit(self._mpz_p, 0) == 0
def size_in_bits(self):
"""Return the minimum number of bits that can encode the number."""
if self < 0:
raise ValueError("Conversion only valid for non-negative numbers")
return _gmp.mpz_sizeinbase(self._mpz_p, 2)
def size_in_bytes(self):
"""Return the minimum number of bytes that can encode the number."""
return (self.size_in_bits() - 1) // 8 + 1
def is_perfect_square(self):
return _gmp.mpz_perfect_square_p(self._mpz_p) != 0
def fail_if_divisible_by(self, small_prime):
"""Raise an exception if the small prime is a divisor."""
if is_native_int(small_prime):
if 0 < small_prime < 65536:
if _gmp.mpz_divisible_ui_p(self._mpz_p,
c_ulong(small_prime)):
raise ValueError("The value is composite")
return
small_prime = IntegerGMP(small_prime)
if _gmp.mpz_divisible_p(self._mpz_p,
small_prime._mpz_p):
raise ValueError("The value is composite")
def multiply_accumulate(self, a, b):
"""Increment the number by the product of a and b."""
if not isinstance(a, IntegerGMP):
a = IntegerGMP(a)
if is_native_int(b):
if 0 < b < 65536:
_gmp.mpz_addmul_ui(self._mpz_p,
a._mpz_p,
c_ulong(b))
return self
if -65535 < b < 0:
_gmp.mpz_submul_ui(self._mpz_p,
a._mpz_p,
c_ulong(-b))
return self
b = IntegerGMP(b)
_gmp.mpz_addmul(self._mpz_p,
a._mpz_p,
b._mpz_p)
return self
def set(self, source):
"""Set the Integer to have the given value"""
if not isinstance(source, IntegerGMP):
source = IntegerGMP(source)
_gmp.mpz_set(self._mpz_p,
source._mpz_p)
return self
def inplace_inverse(self, modulus):
"""Compute the inverse of this number in the ring of
modulo integers.
Raise an exception if no inverse exists.
"""
if not isinstance(modulus, IntegerGMP):
modulus = IntegerGMP(modulus)
comp = _gmp.mpz_cmp(modulus._mpz_p,
self._zero_mpz_p)
if comp == 0:
raise ZeroDivisionError("Modulus cannot be zero")
if comp < 0:
raise ValueError("Modulus must be positive")
result = _gmp.mpz_invert(self._mpz_p,
self._mpz_p,
modulus._mpz_p)
if not result:
raise ValueError("No inverse value can be computed")
return self
def inverse(self, modulus):
result = IntegerGMP(self)
result.inplace_inverse(modulus)
return result
def gcd(self, term):
"""Compute the greatest common denominator between this
number and another term."""
result = IntegerGMP(0)
if is_native_int(term):
if 0 < term < 65535:
_gmp.mpz_gcd_ui(result._mpz_p,
self._mpz_p,
c_ulong(term))
return result
term = IntegerGMP(term)
_gmp.mpz_gcd(result._mpz_p, self._mpz_p, term._mpz_p)
return result
def lcm(self, term):
"""Compute the least common multiplier between this
number and another term."""
result = IntegerGMP(0)
if not isinstance(term, IntegerGMP):
term = IntegerGMP(term)
_gmp.mpz_lcm(result._mpz_p, self._mpz_p, term._mpz_p)
return result
@staticmethod
def jacobi_symbol(a, n):
"""Compute the Jacobi symbol"""
if not isinstance(a, IntegerGMP):
a = IntegerGMP(a)
if not isinstance(n, IntegerGMP):
n = IntegerGMP(n)
if n <= 0 or n.is_even():
raise ValueError("n must be positive odd for the Jacobi symbol")
return _gmp.mpz_jacobi(a._mpz_p, n._mpz_p)
@staticmethod
def _mult_modulo_bytes(term1, term2, modulus):
if not isinstance(term1, IntegerGMP):
term1 = IntegerGMP(term1)
if not isinstance(term2, IntegerGMP):
term2 = IntegerGMP(term2)
if not isinstance(modulus, IntegerGMP):
modulus = IntegerGMP(modulus)
if modulus < 0:
raise ValueError("Modulus must be positive")
if modulus == 0:
raise ZeroDivisionError("Modulus cannot be zero")
if (modulus & 1) == 0:
raise ValueError("Odd modulus is required")
numbers_len = len(modulus.to_bytes())
result = ((term1 * term2) % modulus).to_bytes(numbers_len)
return result
# Clean-up
def __del__(self):
try:
if self._mpz_p is not None:
if self._initialized:
_gmp.mpz_clear(self._mpz_p)
self._mpz_p = None
except AttributeError:
pass
|