-
+ DF6064B02A11C90FB20945DAD1ECC4753A231F894D6CF60E133D6196D924442E0FA08C1D38421A97511F4007A9545C6A980960A20817E6EAECF4CE36E920F151
mpi/include/mpi-inline.h
(0 . 0)(1 . 126)
1706 /* mpi-inline.h - Internal to the Multi Precision Integers
1707 * Copyright (C) 1994, 1996, 1998, 1999 Free Software Foundation, Inc.
1708 *
1709 * This file is part of GnuPG.
1710 *
1711 * GnuPG is free software; you can redistribute it and/or modify
1712 * it under the terms of the GNU General Public License as published by
1713 * the Free Software Foundation; either version 3 of the License, or
1714 * (at your option) any later version.
1715 *
1716 * GnuPG is distributed in the hope that it will be useful,
1717 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1718 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1719 * GNU General Public License for more details.
1720 *
1721 * You should have received a copy of the GNU General Public License
1722 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1723 *
1724 * Note: This code is heavily based on the GNU MP Library.
1725 * Actually it's the same code with only minor changes in the
1726 * way the data is stored; this is to support the abstraction
1727 * of an optional secure memory allocation which may be used
1728 * to avoid revealing of sensitive data due to paging etc.
1729 * The GNU MP Library itself is published under the LGPL;
1730 * however I decided to publish this code under the plain GPL.
1731 */
1732
1733 #ifndef G10_MPI_INLINE_H
1734 #define G10_MPI_INLINE_H
1735
1736 #ifndef G10_MPI_INLINE_DECL
1737 #define G10_MPI_INLINE_DECL extern __inline__
1738 #endif
1739
1740 G10_MPI_INLINE_DECL mpi_limb_t
1741 mpihelp_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1742 mpi_size_t s1_size, mpi_limb_t s2_limb)
1743 {
1744 mpi_limb_t x;
1745
1746 x = *s1_ptr++;
1747 s2_limb += x;
1748 *res_ptr++ = s2_limb;
1749 if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
1750 while( --s1_size ) {
1751 x = *s1_ptr++ + 1; /* add carry */
1752 *res_ptr++ = x; /* and store */
1753 if( x ) /* not 0 (no overflow): we can stop */
1754 goto leave;
1755 }
1756 return 1; /* return carry (size of s1 to small) */
1757 }
1758
1759 leave:
1760 if( res_ptr != s1_ptr ) { /* not the same variable */
1761 mpi_size_t i; /* copy the rest */
1762 for( i=0; i < s1_size-1; i++ )
1763 res_ptr[i] = s1_ptr[i];
1764 }
1765 return 0; /* no carry */
1766 }
1767
1768
1769
1770 G10_MPI_INLINE_DECL mpi_limb_t
1771 mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1772 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
1773 {
1774 mpi_limb_t cy = 0;
1775
1776 if( s2_size )
1777 cy = mpihelp_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
1778
1779 if( s1_size - s2_size )
1780 cy = mpihelp_add_1( res_ptr + s2_size, s1_ptr + s2_size,
1781 s1_size - s2_size, cy);
1782 return cy;
1783 }
1784
1785
1786 G10_MPI_INLINE_DECL mpi_limb_t
1787 mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1788 mpi_size_t s1_size, mpi_limb_t s2_limb )
1789 {
1790 mpi_limb_t x;
1791
1792 x = *s1_ptr++;
1793 s2_limb = x - s2_limb;
1794 *res_ptr++ = s2_limb;
1795 if( s2_limb > x ) {
1796 while( --s1_size ) {
1797 x = *s1_ptr++;
1798 *res_ptr++ = x - 1;
1799 if( x )
1800 goto leave;
1801 }
1802 return 1;
1803 }
1804
1805 leave:
1806 if( res_ptr != s1_ptr ) {
1807 mpi_size_t i;
1808 for( i=0; i < s1_size-1; i++ )
1809 res_ptr[i] = s1_ptr[i];
1810 }
1811 return 0;
1812 }
1813
1814
1815
1816 G10_MPI_INLINE_DECL mpi_limb_t
1817 mpihelp_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1818 mpi_ptr_t s2_ptr, mpi_size_t s2_size)
1819 {
1820 mpi_limb_t cy = 0;
1821
1822 if( s2_size )
1823 cy = mpihelp_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
1824
1825 if( s1_size - s2_size )
1826 cy = mpihelp_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
1827 s1_size - s2_size, cy);
1828 return cy;
1829 }
1830
1831 #endif /*G10_MPI_INLINE_H*/