-
+ 4C06FE251CB2613489112E141996AACC96DF8663971921487AA423F35648F26B8C57EAEBB4C392196EF382778C6150B5D73921AAAFC3ED3CD65216F4EB6C1CF0
eucrypt/mpi/include/mpi-internal.h
(0 . 0)(1 . 286)
1352 /* mpi-internal.h - Internal to the Multi Precision Integers
1353 * Modified by No Such Labs. (C) 2015. See README.
1354 *
1355 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
1356 * SHA256(gnupg-1.4.10.tar.gz):
1357 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
1358 * (C) 1994-2005 Free Software Foundation, Inc.
1359 *
1360 * This program is free software: you can redistribute it and/or modify
1361 * it under the terms of the GNU General Public License as published by
1362 * the Free Software Foundation, either version 3 of the License, or
1363 * (at your option) any later version.
1364 *
1365 * This program is distributed in the hope that it will be useful,
1366 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1367 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1368 * GNU General Public License for more details.
1369 *
1370 * You should have received a copy of the GNU General Public License
1371 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1372 */
1373
1374 #ifndef G10_MPI_INTERNAL_H
1375 #define G10_MPI_INTERNAL_H
1376
1377 #include "mpi.h"
1378
1379 /* from the old mpi-asm-defs.h */
1380 #define BYTES_PER_MPI_LIMB (SIZEOF_UNSIGNED_LONG)
1381
1382 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
1383 typedef unsigned int mpi_limb_t;
1384 typedef signed int mpi_limb_signed_t;
1385 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
1386 typedef unsigned long int mpi_limb_t;
1387 typedef signed long int mpi_limb_signed_t;
1388 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
1389 typedef unsigned long long int mpi_limb_t;
1390 typedef signed long long int mpi_limb_signed_t;
1391 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
1392 typedef unsigned short int mpi_limb_t;
1393 typedef signed short int mpi_limb_signed_t;
1394 #else
1395 #error BYTES_PER_MPI_LIMB does not match any C type
1396 #endif
1397 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
1398
1399
1400 struct gcry_mpi {
1401 int alloced; /* array size (# of allocated limbs) */
1402 int nlimbs; /* number of valid limbs */
1403 unsigned int nbits; /* the real number of valid bits (info only) */
1404 int sign; /* indicates a negative number */
1405 unsigned flags; /* bit 0: array must be allocated in secure memory space */
1406 /* bit 1: not used */
1407 /* bit 2: the limb is a pointer to some xmalloced data */
1408 mpi_limb_t *d; /* array with the limbs */
1409 };
1410
1411
1412
1413 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
1414 * value which is good on most machines. */
1415
1416 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
1417 * checking a 768 and a 1024 bit ElGamal signature.
1418 * (wk 22.12.97) */
1419 #ifndef KARATSUBA_THRESHOLD
1420 #define KARATSUBA_THRESHOLD 16
1421 #endif
1422
1423 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
1424 #if KARATSUBA_THRESHOLD < 2
1425 #undef KARATSUBA_THRESHOLD
1426 #define KARATSUBA_THRESHOLD 2
1427 #endif
1428
1429
1430 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
1431 typedef int mpi_size_t; /* (must be a signed type) */
1432
1433 #define ABS(x) (x >= 0 ? x : -x)
1434 #define MIN(l,o) ((l) < (o) ? (l) : (o))
1435 #define MAX(h,i) ((h) > (i) ? (h) : (i))
1436 #define RESIZE_IF_NEEDED(a,b) \
1437 do { \
1438 if( (a)->alloced < (b) ) \
1439 mpi_resize((a), (b)); \
1440 } while(0)
1441
1442 /* Copy N limbs from S to D. */
1443 #define MPN_COPY( d, s, n) \
1444 do { \
1445 mpi_size_t _i; \
1446 for( _i = 0; _i < (n); _i++ ) \
1447 (d)[_i] = (s)[_i]; \
1448 } while(0)
1449
1450 #define MPN_COPY_INCR( d, s, n) \
1451 do { \
1452 mpi_size_t _i; \
1453 for( _i = 0; _i < (n); _i++ ) \
1454 (d)[_i] = (d)[_i]; \
1455 } while (0)
1456
1457 #define MPN_COPY_DECR( d, s, n ) \
1458 do { \
1459 mpi_size_t _i; \
1460 for( _i = (n)-1; _i >= 0; _i--) \
1461 (d)[_i] = (s)[_i]; \
1462 } while(0)
1463
1464 /* Zero N limbs at D */
1465 #define MPN_ZERO(d, n) \
1466 do { \
1467 int _i; \
1468 for( _i = 0; _i < (n); _i++ ) \
1469 (d)[_i] = 0; \
1470 } while (0)
1471
1472 #define MPN_NORMALIZE(d, n) \
1473 do { \
1474 while( (n) > 0 ) { \
1475 if( (d)[(n)-1] ) \
1476 break; \
1477 (n)--; \
1478 } \
1479 } while(0)
1480
1481 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
1482 do { \
1483 for(;;) { \
1484 if( (d)[(n)-1] ) \
1485 break; \
1486 (n)--; \
1487 } \
1488 } while(0)
1489
1490 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
1491 do { \
1492 if( (size) < KARATSUBA_THRESHOLD ) \
1493 mul_n_basecase (prodp, up, vp, size); \
1494 else \
1495 mul_n (prodp, up, vp, size, tspace); \
1496 } while (0);
1497
1498
1499 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
1500 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
1501 * If this would yield overflow, DI should be the largest possible number
1502 * (i.e., only ones). For correct operation, the most significant bit of D
1503 * has to be set. Put the quotient in Q and the remainder in R.
1504 */
1505 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
1506 do { \
1507 mpi_limb_t _q, _ql, _r; \
1508 mpi_limb_t _xh, _xl; \
1509 umul_ppmm (_q, _ql, (nh), (di)); \
1510 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
1511 umul_ppmm (_xh, _xl, _q, (d)); \
1512 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
1513 if( _xh ) { \
1514 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
1515 _q++; \
1516 if( _xh) { \
1517 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
1518 _q++; \
1519 } \
1520 } \
1521 if( _r >= (d) ) { \
1522 _r -= (d); \
1523 _q++; \
1524 } \
1525 (r) = _r; \
1526 (q) = _q; \
1527 } while (0)
1528
1529
1530 /*-- mpiutil.c --*/
1531 #ifdef M_DEBUG
1532 #define mpi_alloc_limb_space(n,f) mpi_debug_alloc_limb_space((n),(f), M_DBGINFO( __LINE__ ) )
1533 #define mpi_free_limb_space(n) mpi_debug_free_limb_space((n), M_DBGINFO( __LINE__ ) )
1534 mpi_ptr_t mpi_debug_alloc_limb_space( unsigned nlimbs, int sec, const char *info );
1535 void mpi_debug_free_limb_space( mpi_ptr_t a, const char *info );
1536 #else
1537 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs, int sec );
1538 void mpi_free_limb_space( mpi_ptr_t a );
1539 #endif
1540 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
1541
1542 /*-- mpi-bit.c --*/
1543 void mpi_rshift_limbs( MPI a, unsigned int count );
1544 void mpi_lshift_limbs( MPI a, unsigned int count );
1545
1546
1547 /*-- mpihelp-add.c --*/
1548 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1549 mpi_size_t s1_size, mpi_limb_t s2_limb );
1550 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1551 mpi_ptr_t s2_ptr, mpi_size_t size);
1552 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1553 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
1554
1555 /*-- mpihelp-sub.c --*/
1556 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1557 mpi_size_t s1_size, mpi_limb_t s2_limb );
1558 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1559 mpi_ptr_t s2_ptr, mpi_size_t size);
1560 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1561 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
1562
1563 /*-- mpihelp-cmp.c --*/
1564 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
1565
1566 /*-- mpihelp-mul.c --*/
1567
1568 struct karatsuba_ctx {
1569 struct karatsuba_ctx *next;
1570 mpi_ptr_t tspace;
1571 mpi_size_t tspace_size;
1572 mpi_ptr_t tp;
1573 mpi_size_t tp_size;
1574 };
1575
1576 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
1577
1578 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1579 mpi_size_t s1_size, mpi_limb_t s2_limb);
1580 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1581 mpi_size_t s1_size, mpi_limb_t s2_limb);
1582 void mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
1583 mpi_size_t size);
1584 mpi_limb_t mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
1585 mpi_ptr_t vp, mpi_size_t vsize);
1586 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
1587 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
1588 mpi_ptr_t tspace);
1589
1590 void mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
1591 mpi_ptr_t up, mpi_size_t usize,
1592 mpi_ptr_t vp, mpi_size_t vsize,
1593 struct karatsuba_ctx *ctx );
1594
1595
1596 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
1597 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1598 mpi_size_t s1_size, mpi_limb_t s2_limb);
1599
1600 /*-- mpihelp-div.c --*/
1601 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
1602 mpi_limb_t divisor_limb);
1603 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
1604 mpi_ptr_t np, mpi_size_t nsize,
1605 mpi_ptr_t dp, mpi_size_t dsize);
1606 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
1607 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
1608 mpi_limb_t divisor_limb);
1609
1610 /*-- mpihelp-shift.c --*/
1611 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
1612 unsigned cnt);
1613 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
1614 unsigned cnt);
1615
1616
1617 /* Define stuff for longlong.h. */
1618 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
1619 typedef mpi_limb_t UWtype;
1620 typedef unsigned int UHWtype;
1621 #if defined (__GNUC__)
1622 typedef unsigned int UQItype __attribute__ ((mode (QI)));
1623 typedef int SItype __attribute__ ((mode (SI)));
1624 typedef unsigned int USItype __attribute__ ((mode (SI)));
1625 typedef int DItype __attribute__ ((mode (DI)));
1626 typedef unsigned int UDItype __attribute__ ((mode (DI)));
1627 #else
1628 typedef unsigned char UQItype;
1629 typedef long SItype;
1630 typedef unsigned long USItype;
1631 #endif
1632
1633 #ifdef __GNUC__
1634 #include "mpi-inline.h"
1635 #endif
1636
1637 #endif /*G10_MPI_INTERNAL_H*/