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