-
+ 98C4253D69C7531F96DC7631D3D26C5733D5104A3DDBD9EAFD622F8AACD8619E71ABB55D0C951B148E9D33887DAB0EDF2CBD9C9B38CA908BC8E04ED930943FDEmpi/include/mpi-internal.h(0 . 0)(1 . 290)
1767 /* mpi-internal.h - Internal to the Multi Precision Integers
1768 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
1769 * Copyright (C) 1998, 2000 Free Software Foundation, Inc.
1770 *
1771 * This file is part of GnuPG.
1772 *
1773 * GnuPG is free software; you can redistribute it and/or modify
1774 * it under the terms of the GNU General Public License as published by
1775 * the Free Software Foundation; either version 3 of the License, or
1776 * (at your option) any later version.
1777 *
1778 * GnuPG is distributed in the hope that it will be useful,
1779 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1780 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1781 * GNU General Public License for more details.
1782 *
1783 * You should have received a copy of the GNU General Public License
1784 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1785 *
1786 * Note: This code is heavily based on the GNU MP Library.
1787 * Actually it's the same code with only minor changes in the
1788 * way the data is stored; this is to support the abstraction
1789 * of an optional secure memory allocation which may be used
1790 * to avoid revealing of sensitive data due to paging etc.
1791 * The GNU MP Library itself is published under the LGPL;
1792 * however I decided to publish this code under the plain GPL.
1793 */
1794
1795 #ifndef G10_MPI_INTERNAL_H
1796 #define G10_MPI_INTERNAL_H
1797
1798 #include "mpi.h"
1799 #include "mpi-asm-defs.h"
1800
1801 #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
1802 typedef unsigned int mpi_limb_t;
1803 typedef signed int mpi_limb_signed_t;
1804 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
1805 typedef unsigned long int mpi_limb_t;
1806 typedef signed long int mpi_limb_signed_t;
1807 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
1808 typedef unsigned long long int mpi_limb_t;
1809 typedef signed long long int mpi_limb_signed_t;
1810 #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
1811 typedef unsigned short int mpi_limb_t;
1812 typedef signed short int mpi_limb_signed_t;
1813 #else
1814 #error BYTES_PER_MPI_LIMB does not match any C type
1815 #endif
1816 #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
1817
1818
1819 struct gcry_mpi {
1820 int alloced; /* array size (# of allocated limbs) */
1821 int nlimbs; /* number of valid limbs */
1822 unsigned int nbits; /* the real number of valid bits (info only) */
1823 int sign; /* indicates a negative number */
1824 unsigned flags; /* bit 0: array must be allocated in secure memory space */
1825 /* bit 1: not used */
1826 /* bit 2: the limb is a pointer to some xmalloced data */
1827 mpi_limb_t *d; /* array with the limbs */
1828 };
1829
1830
1831
1832 /* If KARATSUBA_THRESHOLD is not already defined, define it to a
1833 * value which is good on most machines. */
1834
1835 /* tested 4, 16, 32 and 64, where 16 gave the best performance when
1836 * checking a 768 and a 1024 bit ElGamal signature.
1837 * (wk 22.12.97) */
1838 #ifndef KARATSUBA_THRESHOLD
1839 #define KARATSUBA_THRESHOLD 16
1840 #endif
1841
1842 /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
1843 #if KARATSUBA_THRESHOLD < 2
1844 #undef KARATSUBA_THRESHOLD
1845 #define KARATSUBA_THRESHOLD 2
1846 #endif
1847
1848
1849 typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
1850 typedef int mpi_size_t; /* (must be a signed type) */
1851
1852 #define ABS(x) (x >= 0 ? x : -x)
1853 #define MIN(l,o) ((l) < (o) ? (l) : (o))
1854 #define MAX(h,i) ((h) > (i) ? (h) : (i))
1855 #define RESIZE_IF_NEEDED(a,b) \
1856 do { \
1857 if( (a)->alloced < (b) ) \
1858 mpi_resize((a), (b)); \
1859 } while(0)
1860
1861 /* Copy N limbs from S to D. */
1862 #define MPN_COPY( d, s, n) \
1863 do { \
1864 mpi_size_t _i; \
1865 for( _i = 0; _i < (n); _i++ ) \
1866 (d)[_i] = (s)[_i]; \
1867 } while(0)
1868
1869 #define MPN_COPY_INCR( d, s, n) \
1870 do { \
1871 mpi_size_t _i; \
1872 for( _i = 0; _i < (n); _i++ ) \
1873 (d)[_i] = (d)[_i]; \
1874 } while (0)
1875
1876 #define MPN_COPY_DECR( d, s, n ) \
1877 do { \
1878 mpi_size_t _i; \
1879 for( _i = (n)-1; _i >= 0; _i--) \
1880 (d)[_i] = (s)[_i]; \
1881 } while(0)
1882
1883 /* Zero N limbs at D */
1884 #define MPN_ZERO(d, n) \
1885 do { \
1886 int _i; \
1887 for( _i = 0; _i < (n); _i++ ) \
1888 (d)[_i] = 0; \
1889 } while (0)
1890
1891 #define MPN_NORMALIZE(d, n) \
1892 do { \
1893 while( (n) > 0 ) { \
1894 if( (d)[(n)-1] ) \
1895 break; \
1896 (n)--; \
1897 } \
1898 } while(0)
1899
1900 #define MPN_NORMALIZE_NOT_ZERO(d, n) \
1901 do { \
1902 for(;;) { \
1903 if( (d)[(n)-1] ) \
1904 break; \
1905 (n)--; \
1906 } \
1907 } while(0)
1908
1909 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
1910 do { \
1911 if( (size) < KARATSUBA_THRESHOLD ) \
1912 mul_n_basecase (prodp, up, vp, size); \
1913 else \
1914 mul_n (prodp, up, vp, size, tspace); \
1915 } while (0);
1916
1917
1918 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
1919 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
1920 * If this would yield overflow, DI should be the largest possible number
1921 * (i.e., only ones). For correct operation, the most significant bit of D
1922 * has to be set. Put the quotient in Q and the remainder in R.
1923 */
1924 #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
1925 do { \
1926 mpi_limb_t _q, _ql, _r; \
1927 mpi_limb_t _xh, _xl; \
1928 umul_ppmm (_q, _ql, (nh), (di)); \
1929 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
1930 umul_ppmm (_xh, _xl, _q, (d)); \
1931 sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl); \
1932 if( _xh ) { \
1933 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
1934 _q++; \
1935 if( _xh) { \
1936 sub_ddmmss (_xh, _r, _xh, _r, 0, (d)); \
1937 _q++; \
1938 } \
1939 } \
1940 if( _r >= (d) ) { \
1941 _r -= (d); \
1942 _q++; \
1943 } \
1944 (r) = _r; \
1945 (q) = _q; \
1946 } while (0)
1947
1948
1949 /*-- mpiutil.c --*/
1950 #ifdef M_DEBUG
1951 #define mpi_alloc_limb_space(n,f) mpi_debug_alloc_limb_space((n),(f), M_DBGINFO( __LINE__ ) )
1952 #define mpi_free_limb_space(n) mpi_debug_free_limb_space((n), M_DBGINFO( __LINE__ ) )
1953 mpi_ptr_t mpi_debug_alloc_limb_space( unsigned nlimbs, int sec, const char *info );
1954 void mpi_debug_free_limb_space( mpi_ptr_t a, const char *info );
1955 #else
1956 mpi_ptr_t mpi_alloc_limb_space( unsigned nlimbs, int sec );
1957 void mpi_free_limb_space( mpi_ptr_t a );
1958 #endif
1959 void mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs );
1960
1961 /*-- mpi-bit.c --*/
1962 void mpi_rshift_limbs( MPI a, unsigned int count );
1963 void mpi_lshift_limbs( MPI a, unsigned int count );
1964
1965
1966 /*-- mpihelp-add.c --*/
1967 mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1968 mpi_size_t s1_size, mpi_limb_t s2_limb );
1969 mpi_limb_t mpihelp_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1970 mpi_ptr_t s2_ptr, mpi_size_t size);
1971 mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1972 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
1973
1974 /*-- mpihelp-sub.c --*/
1975 mpi_limb_t mpihelp_sub_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1976 mpi_size_t s1_size, mpi_limb_t s2_limb );
1977 mpi_limb_t mpihelp_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1978 mpi_ptr_t s2_ptr, mpi_size_t size);
1979 mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
1980 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
1981
1982 /*-- mpihelp-cmp.c --*/
1983 int mpihelp_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
1984
1985 /*-- mpihelp-mul.c --*/
1986
1987 struct karatsuba_ctx {
1988 struct karatsuba_ctx *next;
1989 mpi_ptr_t tspace;
1990 mpi_size_t tspace_size;
1991 mpi_ptr_t tp;
1992 mpi_size_t tp_size;
1993 };
1994
1995 void mpihelp_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
1996
1997 mpi_limb_t mpihelp_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
1998 mpi_size_t s1_size, mpi_limb_t s2_limb);
1999 mpi_limb_t mpihelp_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2000 mpi_size_t s1_size, mpi_limb_t s2_limb);
2001 void mpihelp_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
2002 mpi_size_t size);
2003 mpi_limb_t mpihelp_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
2004 mpi_ptr_t vp, mpi_size_t vsize);
2005 void mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
2006 void mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
2007 mpi_ptr_t tspace);
2008
2009 void mpihelp_mul_karatsuba_case( mpi_ptr_t prodp,
2010 mpi_ptr_t up, mpi_size_t usize,
2011 mpi_ptr_t vp, mpi_size_t vsize,
2012 struct karatsuba_ctx *ctx );
2013
2014
2015 /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
2016 mpi_limb_t mpihelp_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
2017 mpi_size_t s1_size, mpi_limb_t s2_limb);
2018
2019 /*-- mpihelp-div.c --*/
2020 mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
2021 mpi_limb_t divisor_limb);
2022 mpi_limb_t mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
2023 mpi_ptr_t np, mpi_size_t nsize,
2024 mpi_ptr_t dp, mpi_size_t dsize);
2025 mpi_limb_t mpihelp_divmod_1( mpi_ptr_t quot_ptr,
2026 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
2027 mpi_limb_t divisor_limb);
2028
2029 /*-- mpihelp-shift.c --*/
2030 mpi_limb_t mpihelp_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
2031 unsigned cnt);
2032 mpi_limb_t mpihelp_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
2033 unsigned cnt);
2034
2035
2036 /* Define stuff for longlong.h. */
2037 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
2038 typedef mpi_limb_t UWtype;
2039 typedef unsigned int UHWtype;
2040 #if defined (__GNUC__)
2041 typedef unsigned int UQItype __attribute__ ((mode (QI)));
2042 typedef int SItype __attribute__ ((mode (SI)));
2043 typedef unsigned int USItype __attribute__ ((mode (SI)));
2044 typedef int DItype __attribute__ ((mode (DI)));
2045 typedef unsigned int UDItype __attribute__ ((mode (DI)));
2046 #else
2047 typedef unsigned char UQItype;
2048 typedef long SItype;
2049 typedef unsigned long USItype;
2050 #endif
2051
2052 #ifdef __GNUC__
2053 #include "mpi-inline.h"
2054 #endif
2055
2056 #endif /*G10_MPI_INTERNAL_H*/