-
+ DF6064B02A11C90FB20945DAD1ECC4753A231F894D6CF60E133D6196D924442E0FA08C1D38421A97511F4007A9545C6A980960A20817E6EAECF4CE36E920F151mpi/include/mpi-inline.h(0 . 0)(1 . 126)
1637 /* mpi-inline.h - Internal to the Multi Precision Integers
1638 * Copyright (C) 1994, 1996, 1998, 1999 Free Software Foundation, Inc.
1639 *
1640 * This file is part of GnuPG.
1641 *
1642 * GnuPG is free software; you can redistribute it and/or modify
1643 * it under the terms of the GNU General Public License as published by
1644 * the Free Software Foundation; either version 3 of the License, or
1645 * (at your option) any later version.
1646 *
1647 * GnuPG is distributed in the hope that it will be useful,
1648 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1649 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1650 * GNU General Public License for more details.
1651 *
1652 * You should have received a copy of the GNU General Public License
1653 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1654 *
1655 * Note: This code is heavily based on the GNU MP Library.
1656 * Actually it's the same code with only minor changes in the
1657 * way the data is stored; this is to support the abstraction
1658 * of an optional secure memory allocation which may be used
1659 * to avoid revealing of sensitive data due to paging etc.
1660 * The GNU MP Library itself is published under the LGPL;
1661 * however I decided to publish this code under the plain GPL.
1662 */
1663
1664 #ifndef G10_MPI_INLINE_H
1665 #define G10_MPI_INLINE_H
1666
1667 #ifndef G10_MPI_INLINE_DECL
1668 #define G10_MPI_INLINE_DECL extern __inline__
1669 #endif
1670
1671 G10_MPI_INLINE_DECL mpi_limb_t
1672 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1673 mpi_size_t s1_size, mpi_limb_t s2_limb)
1674 {
1675 mpi_limb_t x;
1676
1677 x = *s1_ptr++;
1678 s2_limb += x;
1679 *res_ptr++ = s2_limb;
1680 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
1681 while( --s1_size ) {
1682 x = *s1_ptr++ + 1; /* add carry */
1683 *res_ptr++ = x; /* and store */
1684 if( x ) /* not 0 (no overflow): we can stop */
1685 goto leave;
1686 }
1687 return 1; /* return carry (size of s1 to small) */
1688 }
1689
1690 leave:
1691 if( res_ptr != s1_ptr ) { /* not the same variable */
1692 mpi_size_t i; /* copy the rest */
1693 for( i=0; i < s1_size-1; i++ )
1694 res_ptr[i] = s1_ptr[i];
1695 }
1696 return 0; /* no carry */
1697 }
1698
1699
1700
1701 G10_MPI_INLINE_DECL mpi_limb_t
1702 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1703 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
1704 {
1705 mpi_limb_t cy = 0;
1706
1707 if( s2_size )
1708 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
1709
1710 if( s1_size - s2_size )
1711 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
1712 s1_size - s2_size, cy);
1713 return cy;
1714 }
1715
1716
1717 G10_MPI_INLINE_DECL mpi_limb_t
1718 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1719 mpi_size_t s1_size, mpi_limb_t s2_limb )
1720 {
1721 mpi_limb_t x;
1722
1723 x = *s1_ptr++;
1724 s2_limb = x - s2_limb;
1725 *res_ptr++ = s2_limb;
1726 if( s2_limb > x ) {
1727 while( --s1_size ) {
1728 x = *s1_ptr++;
1729 *res_ptr++ = x - 1;
1730 if( x )
1731 goto leave;
1732 }
1733 return 1;
1734 }
1735
1736 leave:
1737 if( res_ptr != s1_ptr ) {
1738 mpi_size_t i;
1739 for( i=0; i < s1_size-1; i++ )
1740 res_ptr[i] = s1_ptr[i];
1741 }
1742 return 0;
1743 }
1744
1745
1746
1747 G10_MPI_INLINE_DECL mpi_limb_t
1748 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1749 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
1750 {
1751 mpi_limb_t cy = 0;
1752
1753 if( s2_size )
1754 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
1755
1756 if( s1_size - s2_size )
1757 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
1758 s1_size - s2_size, cy);
1759 return cy;
1760 }
1761
1762 #endif /*G10_MPI_INLINE_H*/