Codebase list libcryptx-perl / upstream/0.063
New upstream version 0.063 gregor herrmann 5 years ago
114 changed file(s) with 878 addition(s) and 353 deletion(s). Raw diff Collapse all Expand all
00 Changes for CryptX
1
2 0.063 2018-11-28
3 - proper patch for #46 (related to Math::BigInt::LTM)
14
25 0.062 2018-10-30
36 - bundled libtommath update branch:develop (commit:8b9f98ba 2018-09-23) + stdint.h workaround
47 - bundled libtomcrypt update branch:develop (commit:f413335b 2018-10-29)
58 - fix #45 doc only - sign_message_rfc7518 / sign_message_rfc7518
6 - fix $46 tests only - t/mbi_ltm_bigintpm.t etc. started to fail with latest Math::BigInt
9 - fix #46 tests only - t/mbi_ltm_bigintpm.t etc. started to fail with latest Math::BigInt
710 - fix #47 gcm_decrypt_verify + chacha20poly1305_decrypt_verify don't verify the tag - SERIOUS SECURITY BUG!
811 - improved CBC/ECB padding (using libtomcrypt's functions: padding_depad + padding_pad)
912 - enable pkcs#8 encrypted RSA keys (supported by the latest libtomcrypt)
4444 "url" : "https://github.com/DCIT/perl-CryptX"
4545 }
4646 },
47 "version" : "0.062",
47 "version" : "0.063",
4848 "x_serialization_backend" : "JSON::PP version 2.94"
4949 }
2121 resources:
2222 bugtracker: https://github.com/DCIT/perl-CryptX/issues
2323 repository: https://github.com/DCIT/perl-CryptX
24 version: '0.062'
24 version: '0.063'
2525 x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
5959 Newz(0, RETVAL, 1, mp_int);
6060 mp_init(RETVAL);
6161 mp_read_radix(RETVAL, SvPV_nolen(x), 8);
62 OUTPUT:
63 RETVAL
64
65 ##############################################################################
66 # _from_base()
67
68 Math::BigInt::LTM
69 _from_base(Class, SV *x, int base)
70 CODE:
71 Newz(0, RETVAL, 1, mp_int);
72 mp_init(RETVAL);
73 mp_read_radix(RETVAL, SvPV_nolen(x), base);
74 OUTPUT:
75 RETVAL
76
77 ##############################################################################
78 # _from_bytes()
79
80 Math::BigInt::LTM
81 _from_bytes(Class, SV *x)
82 PREINIT:
83 STRLEN buf_len;
84 unsigned char *buf_ptr;
85 CODE:
86 Newz(0, RETVAL, 1, mp_int);
87 mp_init(RETVAL);
88 buf_ptr = (unsigned char *)SvPVbyte(x, buf_len);
89 mp_read_unsigned_bin(RETVAL, buf_ptr, buf_len);
6290 OUTPUT:
6391 RETVAL
6492
189217 ##############################################################################
190218 # _alen() - return the approx. length of the number in base 10 (fast)
191219 # _alen() might underestimate, but never overestimate the true value
220
192221 int
193222 _alen(Class, Math::BigInt::LTM n)
194223 PREINIT:
233262 RETVAL
234263
235264 ##############################################################################
236 # _as_hex() - return ref to hexadecimal string (prefixed with 0x)
265 # _to_hex() - return ref to hexadecimal string (no prefix)
237266
238267 SV *
239 _as_hex(Class, Math::BigInt::LTM n)
268 _to_hex(Class, Math::BigInt::LTM n)
240269 PREINIT:
241270 int i, len;
242271 char *buf;
243272 CODE:
244 len = mp_unsigned_bin_size(n) * 2 + 3;
245 RETVAL = newSV(len);
246 SvPOK_on(RETVAL);
247 buf = SvPVX(RETVAL); /* get ptr to storage */
248 *buf++ = '0'; *buf++ = 'x'; /* prepend '0x' */
249 mp_tohex(n, buf);
250 for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]);
251 SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */
252 OUTPUT:
253 RETVAL
254
255 ##############################################################################
256 # _as_bin() - return ref to binary string (prefixed with 0b)
257
258 SV *
259 _as_bin(Class, Math::BigInt::LTM n)
260 PREINIT:
261 int len;
262 char *buf;
263 CODE:
264 len = mp_unsigned_bin_size(n) * 8 + 3;
265 RETVAL = newSV(len);
266 SvPOK_on(RETVAL);
267 buf = SvPVX(RETVAL); /* get ptr to storage */
268 *buf++ = '0'; *buf++ = 'b'; /* prepend '0b' */
269 mp_tobinary(n, buf);
270 SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */
271 OUTPUT:
272 RETVAL
273
274 ##############################################################################
275 # _as_oct() - return ref to octal string (prefixed with 0)
276
277 SV *
278 _as_oct(Class, Math::BigInt::LTM n)
279 PREINIT:
280 int len;
281 char *buf;
282 CODE:
283 len = mp_unsigned_bin_size(n) * 3 + 3;
273 len = mp_unsigned_bin_size(n) * 2 + 1;
284274 RETVAL = newSV(len);
285275 SvPOK_on(RETVAL);
286276 buf = SvPVX(RETVAL);
287 *buf++ = '0'; /* prepend '0' */
277 mp_tohex(n, buf);
278 for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]);
279 SvCUR_set(RETVAL, strlen(buf));
280 OUTPUT:
281 RETVAL
282
283 ##############################################################################
284 # _to_bin() - return ref to binary string (no prefix)
285
286 SV *
287 _to_bin(Class, Math::BigInt::LTM n)
288 PREINIT:
289 int len;
290 char *buf;
291 CODE:
292 len = mp_unsigned_bin_size(n) * 8 + 1;
293 RETVAL = newSV(len);
294 SvPOK_on(RETVAL);
295 buf = SvPVX(RETVAL);
296 mp_tobinary(n, buf);
297 SvCUR_set(RETVAL, strlen(buf));
298 OUTPUT:
299 RETVAL
300
301 ##############################################################################
302 # _to_oct() - return ref to octal string (no prefix)
303
304 SV *
305 _to_oct(Class, Math::BigInt::LTM n)
306 PREINIT:
307 int len;
308 char *buf;
309 CODE:
310 len = mp_unsigned_bin_size(n) * 3 + 1;
311 RETVAL = newSV(len);
312 SvPOK_on(RETVAL);
313 buf = SvPVX(RETVAL);
288314 mp_tooctal(n, buf);
289 SvCUR_set(RETVAL, strlen(buf)+1); /* set real length */
315 SvCUR_set(RETVAL, strlen(buf));
316 OUTPUT:
317 RETVAL
318
319 ##############################################################################
320 # _to_base() - raw bytes
321
322 SV *
323 _to_base(Class, Math::BigInt::LTM n, int base)
324 PREINIT:
325 int len;
326 char *buf;
327 CODE:
328 len = mp_unsigned_bin_size(n) * 8; /* the worst case for base == 2 */
329 RETVAL = newSV(len + 1);
330 SvPOK_on(RETVAL);
331 buf = SvPVX(RETVAL);
332 if (len > 0) {
333 mp_toradix_n(n, buf, base, len);
334 SvCUR_set(RETVAL, strlen(buf));
335 }
336 else {
337 buf[0] = '0';
338 SvCUR_set(RETVAL, 1);
339 }
340 OUTPUT:
341 RETVAL
342
343 ##############################################################################
344 # _to_bytes() - raw bytes
345 # _as_bytes() - raw bytes
346
347 SV *
348 _to_bytes(Class, Math::BigInt::LTM n)
349 ALIAS:
350 _as_bytes = 1
351 PREINIT:
352 int len;
353 unsigned char *buf;
354 CODE:
355 PERL_UNUSED_VAR(ix);
356 len = mp_unsigned_bin_size(n);
357 RETVAL = newSV(len + 1);
358 SvPOK_on(RETVAL);
359 buf = (unsigned char*)SvPVX(RETVAL);
360 if (len > 0) {
361 mp_to_unsigned_bin(n, buf);
362 SvCUR_set(RETVAL, len);
363 }
364 else {
365 buf[0] = 0;
366 SvCUR_set(RETVAL, 1);
367 }
290368 OUTPUT:
291369 RETVAL
292370
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ccm_encrypt_authenticate ccm_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( eax_encrypt_authenticate eax_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( gcm_encrypt_authenticate gcm_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ocb_encrypt_authenticate ocb_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 ### not used
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::Checksum Exporter);
77 our %EXPORT_TAGS = ( all => [qw( adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::Checksum Exporter);
77 our %EXPORT_TAGS = ( all => [qw( crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw/ adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Cipher);
99
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_160 blake2b_160_hex blake2b_160_b64 blake2b_160_b64u blake2b_160_file blake2b_160_file_hex blake2b_160_file_b64 blake2b_160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_256 blake2b_256_hex blake2b_256_b64 blake2b_256_b64u blake2b_256_file blake2b_256_file_hex blake2b_256_file_b64 blake2b_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_384 blake2b_384_hex blake2b_384_b64 blake2b_384_b64u blake2b_384_file blake2b_384_file_hex blake2b_384_file_b64 blake2b_384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_512 blake2b_512_hex blake2b_512_b64 blake2b_512_b64u blake2b_512_file blake2b_512_file_hex blake2b_512_file_b64 blake2b_512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_128 blake2s_128_hex blake2s_128_b64 blake2s_128_b64u blake2s_128_file blake2s_128_file_hex blake2s_128_file_b64 blake2s_128_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_160 blake2s_160_hex blake2s_160_b64 blake2s_160_b64u blake2s_160_file blake2s_160_file_hex blake2s_160_file_b64 blake2s_160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_224 blake2s_224_hex blake2s_224_b64 blake2s_224_b64u blake2s_224_file blake2s_224_file_hex blake2s_224_file_b64 blake2s_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_256 blake2s_256_hex blake2s_256_b64 blake2s_256_b64u blake2s_256_file blake2s_256_file_hex blake2s_256_file_b64 blake2s_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( chaes chaes_hex chaes_b64 chaes_b64u chaes_file chaes_file_hex chaes_file_b64 chaes_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak224 keccak224_hex keccak224_b64 keccak224_b64u keccak224_file keccak224_file_hex keccak224_file_b64 keccak224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak256 keccak256_hex keccak256_b64 keccak256_b64u keccak256_file keccak256_file_hex keccak256_file_b64 keccak256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak384 keccak384_hex keccak384_b64 keccak384_b64u keccak384_file keccak384_file_hex keccak384_file_b64 keccak384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak512 keccak512_hex keccak512_b64 keccak512_b64u keccak512_file keccak512_file_hex keccak512_file_b64 keccak512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md2 md2_hex md2_b64 md2_b64u md2_file md2_file_hex md2_file_b64 md2_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md4 md4_hex md4_b64 md4_b64u md4_file md4_file_hex md4_file_b64 md4_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md5 md5_hex md5_b64 md5_b64u md5_file md5_file_hex md5_file_b64 md5_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd128 ripemd128_hex ripemd128_b64 ripemd128_b64u ripemd128_file ripemd128_file_hex ripemd128_file_b64 ripemd128_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd160 ripemd160_hex ripemd160_b64 ripemd160_b64u ripemd160_file ripemd160_file_hex ripemd160_file_b64 ripemd160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd256 ripemd256_hex ripemd256_b64 ripemd256_b64u ripemd256_file ripemd256_file_hex ripemd256_file_b64 ripemd256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd320 ripemd320_hex ripemd320_b64 ripemd320_b64u ripemd320_file ripemd320_file_hex ripemd320_file_b64 ripemd320_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha1 sha1_hex sha1_b64 sha1_b64u sha1_file sha1_file_hex sha1_file_b64 sha1_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha224 sha224_hex sha224_b64 sha224_b64u sha224_file sha224_file_hex sha224_file_b64 sha224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha256 sha256_hex sha256_b64 sha256_b64u sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha384 sha384_hex sha384_b64 sha384_b64u sha384_file sha384_file_hex sha384_file_b64 sha384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_224 sha3_224_hex sha3_224_b64 sha3_224_b64u sha3_224_file sha3_224_file_hex sha3_224_file_b64 sha3_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_256 sha3_256_hex sha3_256_b64 sha3_256_b64u sha3_256_file sha3_256_file_hex sha3_256_file_b64 sha3_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_384 sha3_384_hex sha3_384_b64 sha3_384_b64u sha3_384_file sha3_384_file_hex sha3_384_file_b64 sha3_384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_512 sha3_512_hex sha3_512_b64 sha3_512_b64u sha3_512_file sha3_512_file_hex sha3_512_file_b64 sha3_512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512 sha512_hex sha512_b64 sha512_b64u sha512_file sha512_file_hex sha512_file_b64 sha512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512_224 sha512_224_hex sha512_224_b64 sha512_224_b64u sha512_224_file sha512_224_file_hex sha512_224_file_b64 sha512_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512_256 sha512_256_hex sha512_256_b64 sha512_256_b64u sha512_256_file sha512_256_file_hex sha512_256_file_b64 sha512_256_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( tiger192 tiger192_hex tiger192_b64 tiger192_b64u tiger192_file tiger192_file_hex tiger192_file_b64 tiger192_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( whirlpool whirlpool_hex whirlpool_b64 whirlpool_b64u whirlpool_file whirlpool_file_hex whirlpool_file_b64 whirlpool_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( digest_data digest_data_hex digest_data_b64 digest_data_b64u digest_file digest_file_hex digest_file_b64 digest_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(pbkdf1 pbkdf2 hkdf hkdf_expand hkdf_extract)] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b blake2b_hex blake2b_b64 blake2b_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s blake2s_hex blake2s_b64 blake2s_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( f9 f9_hex f9_b64 f9_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( hmac hmac_hex hmac_b64 hmac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( omac omac_hex omac_b64 omac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( pmac pmac_hex pmac_b64 pmac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( pelican pelican_hex pelican_b64 pelican_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( poly1305 poly1305_hex poly1305_b64 poly1305_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( xcbc xcbc_hex xcbc_b64 xcbc_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
77 use Carp 'croak';
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.062';
6 our $VERSION = '0.063';
77
88 use Crypt::Cipher;
99
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 ### not used
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( dh_shared_secret )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( dsa_encrypt dsa_decrypt dsa_sign_message dsa_verify_message dsa_sign_hash dsa_verify_hash )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ecc_encrypt ecc_decrypt ecc_sign_message ecc_verify_message ecc_sign_hash ecc_verify_hash ecc_shared_secret )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(rsa_encrypt rsa_decrypt rsa_sign_message rsa_verify_message rsa_sign_hash rsa_verify_hash)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use Carp;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings ;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 require XSLoader;
77 XSLoader::load('CryptX', $VERSION);
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.062';
4 our $VERSION = '0.063';
55
66 use CryptX;
7
8 sub api_version() { 2 }
7 use Carp;
98
109 sub CLONE_SKIP { 1 } # prevent cloning
10
11 sub api_version() { 2 } # compatible with Math::BigInt v1.83+
12
13 sub import { }
14
15 ### the following functions are implemented in XS
16 # _1ex()
17 # _acmp()
18 # _add()
19 # _alen()
20 # _alen()
21 # _and()
22 # _as_bytes()
23 # _copy()
24 # _dec()
25 # _div()
26 # _from_base()
27 # _from_bin()
28 # _from_bytes()
29 # _from_hex()
30 # _from_oct()
31 # _gcd()
32 # _inc()
33 # _is_even()
34 # _is_odd()
35 # _is_one()
36 # _is_ten()
37 # _is_two()
38 # _is_zero()
39 # _lcm()
40 # _len()
41 # _lsft()
42 # _mod()
43 # _modinv()
44 # _modpow()
45 # _mul()
46 # _new()
47 # _one()
48 # _or()
49 # _pow()
50 # _root()
51 # _rsft()
52 # _set()
53 # _sqrt()
54 # _str()
55 # _sub()
56 # _ten()
57 # _to_base()
58 # _to_bin()
59 # _to_bytes()
60 # _to_hex()
61 # _to_oct()
62 # _two()
63 # _xor()
64 # _zero()
65 # _zeros()
66
1167
1268 ### same as overloading in Math::BigInt::Lib
1369 use overload
70
1471 # overload key: with_assign
1572
1673 '+' => sub {
238295 return $class -> _sqrt($class -> _copy($_[0]));
239296 },
240297
241 'int' => sub { $_[0] -> copy() -> bint(); },
298 'int' => sub { $_[0] },
242299
243300 # overload key: conversion
244301
251308 '=' => sub { ref($_[0]) -> _copy($_[0]); },
252309
253310 ;
254
255 ### same as import() in Math::BigInt::Lib
256 sub import { }
257311
258312 ### same as _check() in Math::BigInt::Lib
259313 sub _check {
260 # used by the test suite
261 my ($class, $x) = @_;
262 return "Input is undefined" unless defined $x;
263 return "$x is not a reference" unless ref($x);
264 return 0;
314 # used by the test suite
315 my ($class, $x) = @_;
316 return "Input is undefined" unless defined $x;
317 return "$x is not a reference" unless ref($x);
318 return 0;
265319 }
266320
267321 ### same as _digit() in Math::BigInt::Lib
268322 sub _digit {
269 my ($class, $x, $n) = @_;
270 substr($class ->_str($x), -($n+1), 1);
323 my ($class, $x, $n) = @_;
324 substr($class ->_str($x), -($n+1), 1);
271325 }
272326
273327 ### same as _num() in Math::BigInt::Lib
274328 sub _num {
275 my ($class, $x) = @_;
276 0 + $class -> _str($x);
277 }
278
279 ### BEWARE!!! NOT THE SAME as _fac() in Math::BigInt::Lib
329 my ($class, $x) = @_;
330 0 + $class -> _str($x);
331 }
332
333 ### PATCHED _fac() from Math::BigInt::Lib
280334 sub _fac {
281 # factorial
282 my ($class, $x) = @_;
283
284 my $two = $class -> _two();
285
286 if ($class -> _acmp($x, $two) < 0) {
287 $class->_set($x, 1);
288 return $x;
289 }
290
291 my $i = $class -> _copy($x);
292 while ($class -> _acmp($i, $two) > 0) {
293 $i = $class -> _dec($i);
294 $x = $class -> _mul($x, $i);
295 }
296
297 return $x;
335 # factorial
336 my ($class, $x) = @_;
337
338 my $two = $class -> _two();
339
340 if ($class -> _acmp($x, $two) < 0) {
341 ###HACK: needed for MBI 1.999715 compatibility
342 ###return $class -> _one();
343 $class->_set($x, 1); return $x
344 }
345
346 my $i = $class -> _copy($x);
347 while ($class -> _acmp($i, $two) > 0) {
348 $i = $class -> _dec($i);
349 $x = $class -> _mul($x, $i);
350 }
351
352 return $x;
353 }
354
355 ### PATCHED _dfac() from Math::BigInt::Lib
356 sub _dfac {
357 # double factorial
358 my ($class, $x) = @_;
359
360 my $two = $class -> _two();
361
362 if ($class -> _acmp($x, $two) < 0) {
363 ###HACK: needed for MBI 1.999715 compatibility
364 ###return $class -> _one();
365 $class->_set($x, 1); return $x
366 }
367
368 my $i = $class -> _copy($x);
369 while ($class -> _acmp($i, $two) > 0) {
370 $i = $class -> _sub($i, $two);
371 $x = $class -> _mul($x, $i);
372 }
373
374 return $x;
298375 }
299376
300377 ### same as _nok() in Math::BigInt::Lib
301378 sub _nok {
302 # Return binomial coefficient (n over k).
303 # Given refs to arrays, return ref to array.
304 # First input argument is modified.
305
306 my ($class, $n, $k) = @_;
307
308 # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
309 # nok(n, n-k), to minimize the number if iterations in the loop.
310
311 {
312 my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
313 if ($class -> _acmp($twok, $n) > 0) {
314 $k = $class -> _sub($class -> _copy($n), $k);
315 }
316 }
317
318 # Example:
319 #
320 # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7
321 # | | = --------- = --------------- = --------- = 5 * - * -
322 # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3
323
324 if ($class -> _is_zero($k)) {
325 return $class -> _one();
326 }
327
328 # Make a copy of the original n, since we'll be modifying n in-place.
329
330 my $n_orig = $class -> _copy($n);
331
332 # n = 5, f = 6, d = 2 (cf. example above)
333
334 $n = $class -> _sub($n, $k);
335 $n = $class -> _inc($n);
336
337 my $f = $class -> _copy($n);
338 $class -> _inc($f);
339
340 my $d = $class -> _two();
341
342 # while f <= n (the original n, that is) ...
343
344 while ($class -> _acmp($f, $n_orig) <= 0) {
345
346 # n = (n * f / d) == 5 * 6 / 2 (cf. example above)
347
348 $n = $class -> _mul($n, $f);
349 $n = $class -> _div($n, $d);
350
351 # f = 7, d = 3 (cf. example above)
352
353 $f = $class -> _inc($f);
354 $d = $class -> _inc($d);
355 }
356
357 return $n;
379 # Return binomial coefficient (n over k).
380 my ($class, $n, $k) = @_;
381
382 # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
383 # nok(n, n-k), to minimize the number if iterations in the loop.
384
385 {
386 my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
387 if ($class -> _acmp($twok, $n) > 0) {
388 $k = $class -> _sub($class -> _copy($n), $k);
389 }
390 }
391
392 # Example:
393 #
394 # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7
395 # | | = --------- = --------------- = --------- = ((5 * 6) / 2 * 7) / 3
396 # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3
397 #
398 # Equivalently, _nok(11, 5) is computed as
399 #
400 # (((((((7 * 8) / 2) * 9) / 3) * 10) / 4) * 11) / 5
401
402 if ($class -> _is_zero($k)) {
403 return $class -> _one();
404 }
405
406 # Make a copy of the original n, in case the subclass modifies n in-place.
407
408 my $n_orig = $class -> _copy($n);
409
410 # n = 5, f = 6, d = 2 (cf. example above)
411
412 $n = $class -> _sub($n, $k);
413 $n = $class -> _inc($n);
414
415 my $f = $class -> _copy($n);
416 $f = $class -> _inc($f);
417
418 my $d = $class -> _two();
419
420 # while f <= n (the original n, that is) ...
421
422 while ($class -> _acmp($f, $n_orig) <= 0) {
423 $n = $class -> _mul($n, $f);
424 $n = $class -> _div($n, $d);
425 $f = $class -> _inc($f);
426 $d = $class -> _inc($d);
427 }
428
429 return $n;
358430 }
359431
360432 ### same as _log_int() in Math::BigInt::Lib
361433 sub _log_int {
362 # calculate integer log of $x to base $base
363 # ref to array, ref to array - return ref to array
364 my ($class, $x, $base) = @_;
365
366 # X == 0 => NaN
367 return if $class -> _is_zero($x);
368
369 $base = $class -> _new(2) unless defined($base);
370 $base = $class -> _new($base) unless ref($base);
371
372 # BASE 0 or 1 => NaN
373 return if $class -> _is_zero($base) || $class -> _is_one($base);
374
375 # X == 1 => 0 (is exact)
376 if ($class -> _is_one($x)) {
377 return $class -> _zero(), 1;
378 }
379
380 my $cmp = $class -> _acmp($x, $base);
381
382 # X == BASE => 1 (is exact)
383 if ($cmp == 0) {
384 return $class -> _one(), 1;
385 }
386
387 # 1 < X < BASE => 0 (is truncated)
388 if ($cmp < 0) {
389 return $class -> _zero(), 0;
390 }
391
392 my $y;
393
394 # log(x) / log(b) = log(xm * 10^xe) / log(bm * 10^be)
395 # = (log(xm) + xe*(log(10))) / (log(bm) + be*log(10))
396
397 {
398 my $x_str = $class -> _str($x);
399 my $b_str = $class -> _str($base);
400 my $xm = "." . $x_str;
401 my $bm = "." . $b_str;
402 my $xe = length($x_str);
403 my $be = length($b_str);
404 my $log10 = log(10);
405 my $guess = int((log($xm) + $xe * $log10) / (log($bm) + $be * $log10));
406 $y = $class -> _new($guess);
407 }
408
409 my $trial = $class -> _pow($class -> _copy($base), $y);
410 my $acmp = $class -> _acmp($trial, $x);
411
412 # Did we get the exact result?
413
414 return $y, 1 if $acmp == 0;
415
416 # Too small?
417
418 while ($acmp < 0) {
419 $trial = $class -> _mul($trial, $base);
420 $y = $class -> _inc($y);
421 $acmp = $class -> _acmp($trial, $x);
422 }
423
424 # Too big?
425
426 while ($acmp > 0) {
427 $trial = $class -> _div($trial, $base);
428 $y = $class -> _dec($y);
429 $acmp = $class -> _acmp($trial, $x);
430 }
431
432 return $y, 1 if $acmp == 0; # result is exact
433 return $y, 0; # result is too small
434 # calculate integer log of $x to base $base
435 # ref to array, ref to array - return ref to array
436 my ($class, $x, $base) = @_;
437
438 # X == 0 => NaN
439 return if $class -> _is_zero($x);
440
441 $base = $class -> _new(2) unless defined($base);
442 $base = $class -> _new($base) unless ref($base);
443
444 # BASE 0 or 1 => NaN
445 return if $class -> _is_zero($base) || $class -> _is_one($base);
446
447 # X == 1 => 0 (is exact)
448 if ($class -> _is_one($x)) {
449 return $class -> _zero(), 1;
450 }
451
452 my $cmp = $class -> _acmp($x, $base);
453
454 # X == BASE => 1 (is exact)
455 if ($cmp == 0) {
456 return $class -> _one(), 1;
457 }
458
459 # 1 < X < BASE => 0 (is truncated)
460 if ($cmp < 0) {
461 return $class -> _zero(), 0;
462 }
463
464 my $y;
465
466 # log(x) / log(b) = log(xm * 10^xe) / log(bm * 10^be)
467 # = (log(xm) + xe*(log(10))) / (log(bm) + be*log(10))
468
469 {
470 my $x_str = $class -> _str($x);
471 my $b_str = $class -> _str($base);
472 my $xm = "." . $x_str;
473 my $bm = "." . $b_str;
474 my $xe = length($x_str);
475 my $be = length($b_str);
476 my $log10 = log(10);
477 my $guess = int((log($xm) + $xe * $log10) / (log($bm) + $be * $log10));
478 $y = $class -> _new($guess);
479 }
480
481 my $trial = $class -> _pow($class -> _copy($base), $y);
482 my $acmp = $class -> _acmp($trial, $x);
483
484 # Did we get the exact result?
485
486 return $y, 1 if $acmp == 0;
487
488 # Too small?
489
490 while ($acmp < 0) {
491 $trial = $class -> _mul($trial, $base);
492 $y = $class -> _inc($y);
493 $acmp = $class -> _acmp($trial, $x);
494 }
495
496 # Too big?
497
498 while ($acmp > 0) {
499 $trial = $class -> _div($trial, $base);
500 $y = $class -> _dec($y);
501 $acmp = $class -> _acmp($trial, $x);
502 }
503
504 return $y, 1 if $acmp == 0; # result is exact
505 return $y, 0; # result is too small
506 }
507
508 ### same as _lucas() in Math::BigInt::Lib
509 sub _lucas {
510 my ($class, $n) = @_;
511
512 $n = $class -> _num($n) if ref $n;
513
514 # In list context, use lucas(n) = lucas(n-1) + lucas(n-2)
515
516 if (wantarray) {
517 my @y;
518
519 push @y, $class -> _two();
520 return @y if $n == 0;
521
522 push @y, $class -> _one();
523 return @y if $n == 1;
524
525 for (my $i = 2 ; $i <= $n ; ++ $i) {
526 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
527 }
528
529 return @y;
530 }
531
532 require Scalar::Util;
533
534 # In scalar context use that lucas(n) = fib(n-1) + fib(n+1).
535 #
536 # Remember that _fib() behaves differently in scalar context and list
537 # context, so we must add scalar() to get the desired behaviour.
538
539 return $class -> _two() if $n == 0;
540
541 return $class -> _add(scalar $class -> _fib($n - 1),
542 scalar $class -> _fib($n + 1));
543 }
544
545 ### same as _fib() in Math::BigInt::Lib
546 sub _fib {
547 my ($class, $n) = @_;
548
549 $n = $class -> _num($n) if ref $n;
550
551 # In list context, use fib(n) = fib(n-1) + fib(n-2)
552
553 if (wantarray) {
554 my @y;
555
556 push @y, $class -> _zero();
557 return @y if $n == 0;
558
559 push @y, $class -> _one();
560 return @y if $n == 1;
561
562 for (my $i = 2 ; $i <= $n ; ++ $i) {
563 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
564 }
565
566 return @y;
567 }
568
569 # In scalar context use a fast algorithm that is much faster than the
570 # recursive algorith used in list context.
571
572 my $cache = {};
573 my $two = $class -> _two();
574 my $fib;
575
576 $fib = sub {
577 my $n = shift;
578 return $class -> _zero() if $n <= 0;
579 return $class -> _one() if $n <= 2;
580 return $cache -> {$n} if exists $cache -> {$n};
581
582 my $k = int($n / 2);
583 my $a = $fib -> ($k + 1);
584 my $b = $fib -> ($k);
585 my $y;
586
587 if ($n % 2 == 1) {
588 # a*a + b*b
589 $y = $class -> _add($class -> _mul($class -> _copy($a), $a),
590 $class -> _mul($class -> _copy($b), $b));
591 } else {
592 # (2*a - b)*b
593 $y = $class -> _mul($class -> _sub($class -> _mul(
594 $class -> _copy($two), $a), $b), $b);
595 }
596
597 $cache -> {$n} = $y;
598 return $y;
599 };
600
601 return $fib -> ($n);
602 }
603
604 ### same as _sand() in Math::BigInt::Lib
605 sub _sand {
606 my ($class, $x, $sx, $y, $sy) = @_;
607
608 return ($class -> _zero(), '+')
609 if $class -> _is_zero($x) || $class -> _is_zero($y);
610
611 my $sign = $sx eq '-' && $sy eq '-' ? '-' : '+';
612
613 my ($bx, $by);
614
615 if ($sx eq '-') { # if x is negative
616 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
617 $bx = $class -> _copy($x);
618 $bx = $class -> _dec($bx);
619 $bx = $class -> _as_hex($bx);
620 $bx =~ s/^-?0x//;
621 $bx =~ tr<0123456789abcdef>
622 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
623 } else { # if x is positive
624 $bx = $class -> _as_hex($x); # get binary representation
625 $bx =~ s/^-?0x//;
626 $bx =~ tr<fedcba9876543210>
627 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
628 }
629
630 if ($sy eq '-') { # if y is negative
631 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
632 $by = $class -> _copy($y);
633 $by = $class -> _dec($by);
634 $by = $class -> _as_hex($by);
635 $by =~ s/^-?0x//;
636 $by =~ tr<0123456789abcdef>
637 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
638 } else {
639 $by = $class -> _as_hex($y); # get binary representation
640 $by =~ s/^-?0x//;
641 $by =~ tr<fedcba9876543210>
642 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
643 }
644
645 # now we have bit-strings from X and Y, reverse them for padding
646 $bx = reverse $bx;
647 $by = reverse $by;
648
649 # padd the shorter string
650 my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
651 my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
652 my $diff = CORE::length($bx) - CORE::length($by);
653 if ($diff > 0) {
654 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
655 $by .= $yy x $diff;
656 } elsif ($diff < 0) {
657 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
658 $bx .= $xx x abs($diff);
659 }
660
661 # and the strings together
662 my $r = $bx & $by;
663
664 # and reverse the result again
665 $bx = reverse $r;
666
667 # One of $bx or $by was negative, so need to flip bits in the result. In both
668 # cases (one or two of them negative, or both positive) we need to get the
669 # characters back.
670 if ($sign eq '-') {
671 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
672 <0123456789abcdef>;
673 } else {
674 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
675 <fedcba9876543210>;
676 }
677
678 # leading zeros will be stripped by _from_hex()
679 $bx = '0x' . $bx;
680 $bx = $class -> _from_hex($bx);
681
682 $bx = $class -> _inc($bx) if $sign eq '-';
683
684 # avoid negative zero
685 $sign = '+' if $class -> _is_zero($bx);
686
687 return $bx, $sign;
688 }
689
690 ### same as _sxor() in Math::BigInt::Lib
691 sub _sxor {
692 my ($class, $x, $sx, $y, $sy) = @_;
693
694 return ($class -> _zero(), '+')
695 if $class -> _is_zero($x) && $class -> _is_zero($y);
696
697 my $sign = $sx ne $sy ? '-' : '+';
698
699 my ($bx, $by);
700
701 if ($sx eq '-') { # if x is negative
702 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
703 $bx = $class -> _copy($x);
704 $bx = $class -> _dec($bx);
705 $bx = $class -> _as_hex($bx);
706 $bx =~ s/^-?0x//;
707 $bx =~ tr<0123456789abcdef>
708 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
709 } else { # if x is positive
710 $bx = $class -> _as_hex($x); # get binary representation
711 $bx =~ s/^-?0x//;
712 $bx =~ tr<fedcba9876543210>
713 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
714 }
715
716 if ($sy eq '-') { # if y is negative
717 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
718 $by = $class -> _copy($y);
719 $by = $class -> _dec($by);
720 $by = $class -> _as_hex($by);
721 $by =~ s/^-?0x//;
722 $by =~ tr<0123456789abcdef>
723 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
724 } else {
725 $by = $class -> _as_hex($y); # get binary representation
726 $by =~ s/^-?0x//;
727 $by =~ tr<fedcba9876543210>
728 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
729 }
730
731 # now we have bit-strings from X and Y, reverse them for padding
732 $bx = reverse $bx;
733 $by = reverse $by;
734
735 # padd the shorter string
736 my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
737 my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
738 my $diff = CORE::length($bx) - CORE::length($by);
739 if ($diff > 0) {
740 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
741 $by .= $yy x $diff;
742 } elsif ($diff < 0) {
743 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
744 $bx .= $xx x abs($diff);
745 }
746
747 # xor the strings together
748 my $r = $bx ^ $by;
749
750 # and reverse the result again
751 $bx = reverse $r;
752
753 # One of $bx or $by was negative, so need to flip bits in the result. In both
754 # cases (one or two of them negative, or both positive) we need to get the
755 # characters back.
756 if ($sign eq '-') {
757 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
758 <0123456789abcdef>;
759 } else {
760 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
761 <fedcba9876543210>;
762 }
763
764 # leading zeros will be stripped by _from_hex()
765 $bx = '0x' . $bx;
766 $bx = $class -> _from_hex($bx);
767
768 $bx = $class -> _inc($bx) if $sign eq '-';
769
770 # avoid negative zero
771 $sign = '+' if $class -> _is_zero($bx);
772
773 return $bx, $sign;
774 }
775
776 ### same as _sor() in Math::BigInt::Lib
777 sub _sor {
778 my ($class, $x, $sx, $y, $sy) = @_;
779
780 return ($class -> _zero(), '+')
781 if $class -> _is_zero($x) && $class -> _is_zero($y);
782
783 my $sign = $sx eq '-' || $sy eq '-' ? '-' : '+';
784
785 my ($bx, $by);
786
787 if ($sx eq '-') { # if x is negative
788 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
789 $bx = $class -> _copy($x);
790 $bx = $class -> _dec($bx);
791 $bx = $class -> _as_hex($bx);
792 $bx =~ s/^-?0x//;
793 $bx =~ tr<0123456789abcdef>
794 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
795 } else { # if x is positive
796 $bx = $class -> _as_hex($x); # get binary representation
797 $bx =~ s/^-?0x//;
798 $bx =~ tr<fedcba9876543210>
799 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
800 }
801
802 if ($sy eq '-') { # if y is negative
803 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
804 $by = $class -> _copy($y);
805 $by = $class -> _dec($by);
806 $by = $class -> _as_hex($by);
807 $by =~ s/^-?0x//;
808 $by =~ tr<0123456789abcdef>
809 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
810 } else {
811 $by = $class -> _as_hex($y); # get binary representation
812 $by =~ s/^-?0x//;
813 $by =~ tr<fedcba9876543210>
814 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
815 }
816
817 # now we have bit-strings from X and Y, reverse them for padding
818 $bx = reverse $bx;
819 $by = reverse $by;
820
821 # padd the shorter string
822 my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
823 my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
824 my $diff = CORE::length($bx) - CORE::length($by);
825 if ($diff > 0) {
826 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
827 $by .= $yy x $diff;
828 } elsif ($diff < 0) {
829 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
830 $bx .= $xx x abs($diff);
831 }
832
833 # or the strings together
834 my $r = $bx | $by;
835
836 # and reverse the result again
837 $bx = reverse $r;
838
839 # One of $bx or $by was negative, so need to flip bits in the result. In both
840 # cases (one or two of them negative, or both positive) we need to get the
841 # characters back.
842 if ($sign eq '-') {
843 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
844 <0123456789abcdef>;
845 } else {
846 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
847 <fedcba9876543210>;
848 }
849
850 # leading zeros will be stripped by _from_hex()
851 $bx = '0x' . $bx;
852 $bx = $class -> _from_hex($bx);
853
854 $bx = $class -> _inc($bx) if $sign eq '-';
855
856 # avoid negative zero
857 $sign = '+' if $class -> _is_zero($bx);
858
859 return $bx, $sign;
860 }
861
862 ### same as _as_bin() in Math::BigInt::Lib
863 sub _as_bin {
864 # convert the number to a string of binary digits with prefix
865 my ($class, $x) = @_;
866 return '0b' . $class -> _to_bin($x);
867 }
868
869 ### same as _as_oct() in Math::BigInt::Lib
870 sub _as_oct {
871 # convert the number to a string of octal digits with prefix
872 my ($class, $x) = @_;
873 return '0' . $class -> _to_oct($x); # yes, 0 becomes "00"
874 }
875
876 ### same as _as_hex() in Math::BigInt::Lib
877 sub _as_hex {
878 # convert the number to a string of hexadecimal digits with prefix
879 my ($class, $x) = @_;
880 return '0x' . $class -> _to_hex($x);
434881 }
435882
436883 1;
24002400 +281474976710656:0:0
24012401 +281474976710656:1:0
24022402 +281474976710656:+281474976710656:281474976710656
2403 #### does not work since Math::BigInt 1.999815
2404 ##281474976710656:-1:281474976710656
2405 ##-2:-3:-4
2406 ##-1:-1:-1
2407 ##-6:-6:-6
2408 ##-7:-4:-8
2409 ##-7:4:0
2410 ##-4:7:4
2411 ### negative argument is bitwise shorter than positive [perl #26559]
2412 ##30:-3:28
2413 ##123:-1:123
2403 281474976710656:-1:281474976710656
2404 -2:-3:-4
2405 -1:-1:-1
2406 -6:-6:-6
2407 -7:-4:-8
2408 -7:4:0
2409 -4:7:4
2410 # negative argument is bitwise shorter than positive [perl #26559]
2411 30:-3:28
2412 123:-1:123
24142413 # equal arguments are treated special, so also do some test with unequal ones
24152414 0xFFFF:0xFFFF:0x0xFFFF
24162415 0xFFFFFF:0xFFFFFF:0x0xFFFFFF
24382437 +281474976710656:0:281474976710656
24392438 +281474976710656:1:281474976710657
24402439 +281474976710656:281474976710656:281474976710656
2441 #### does not work since Math::BigInt 1.999815
2442 ##-2:-3:-1
2443 ##-1:-1:-1
2444 ##-6:-6:-6
2445 ##-7:4:-3
2446 ##-4:7:-1
2447 ##+281474976710656:-1:-1
2448 ##30:-3:-1
2449 ##30:-4:-2
2450 ##300:-76:-68
2451 ##-76:300:-68
2440 -2:-3:-1
2441 -1:-1:-1
2442 -6:-6:-6
2443 -7:4:-3
2444 -4:7:-1
2445 +281474976710656:-1:-1
2446 30:-3:-1
2447 30:-4:-2
2448 300:-76:-68
2449 -76:300:-68
24522450 # equal arguments are treated special, so also do some test with unequal ones
24532451 0xFFFF:0xFFFF:0x0xFFFF
24542452 0xFFFFFF:0xFFFFFF:0x0xFFFFFF
24912489 +281474976710656:0:281474976710656
24922490 +281474976710656:1:281474976710657
24932491 +281474976710656:281474976710656:0
2494 #### does not work since Math::BigInt 1.999815
2495 ##-2:-3:3
2496 ##-1:-1:0
2497 ##-6:-6:0
2498 ##-7:4:-3
2499 ##-4:7:-5
2500 ##4:-7:-3
2501 ##-4:-7:5
2502 ##30:-3:-29
2503 ##30:-4:-30
2504 ##300:-76:-360
2505 ##-76:300:-360
2492 -2:-3:3
2493 -1:-1:0
2494 -6:-6:0
2495 -7:4:-3
2496 -4:7:-5
2497 4:-7:-3
2498 -4:-7:5
2499 30:-3:-29
2500 30:-4:-30
2501 300:-76:-360
2502 -76:300:-360
25062503 # equal arguments are treated special, so also do some test with unequal ones
25072504 0xFFFF:0xFFFF:0
25082505 0xFFFFFF:0xFFFFFF:0
66
77 BEGIN {
88 plan skip_all => "requires Math::BigInt 1.999712+" unless eval { require Math::BigInt && eval($Math::BigInt::VERSION) >= 1.999712 };
9 plan tests => 3652 # tests in require'd file
9 plan tests => 3712 # tests in require'd file
1010 + 6; # tests in this file
1111 }
1212
66
77 BEGIN {
88 plan skip_all => "requires Math::BigInt 1.999712+" unless eval { require Math::BigInt && eval($Math::BigInt::VERSION) >= 1.999712 };
9 plan tests => 3652 # tests in require'd file
9 plan tests => 3712 # tests in require'd file
1010 + 6; # tests in this file
1111 }
1212