⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpilib.h

📁 各种加密算法的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
#define mp_modmult	merritt_modmult
#define modmult_burn	merritt_burn
#define SLOP_BITS	MERRITT_SLOP_BITS

#else	/* not PEASANT, MERRITT */
#ifdef UPTON
/* Define C names for Upton's modmult primitives. */
#define stage_modulus	stage_upton_modulus
#define mp_modmult	upton_modmult
#define modmult_burn	upton_burn
#define SLOP_BITS	UPTON_SLOP_BITS

#else	/* not PEASANT, MERRITT, UPTON */
#ifdef SMITH
/* Define C names for Smith's modmult primitives. */
#define stage_modulus	stage_smith_modulus
#define mp_modmult	smith_modmult
#define modmult_burn	smith_burn
#define SLOP_BITS	SMITH_SLOP_BITS

#endif	/* SMITH */
#endif	/* UPTON */
#endif	/* MERRITT */
#endif	/* PEASANT */


#define mp_shift_left(r1) mp_rotate_left(r1,(boolean)0)
	/* multiprecision shift left 1 bit */

#define mp_add(r1,r2) mp_addc(r1,r2,(boolean)0)
	/* multiprecision add with no carry */

#define mp_sub(r1,r2) mp_subb(r1,r2,(boolean)0)
	/* multiprecision subtract with no borrow */

#define mp_abs(r) (mp_tstminus(r) ? (mp_neg(r),TRUE) : FALSE)

#define msub(r,m) if (mp_compare(r,m) >= 0) mp_sub(r,m)
	/* Prevents r from getting bigger than modulus m */

#define testeq(r,i)	\
	( (lsunit(r)==(i)) && (significance(r)<=1) )

#define testne(r,i)	\
	( (lsunit(r)!=(i)) || (significance(r)>1) )

#define testge(r,i)	\
	( (lsunit(r)>=(i)) || (significance(r)>1) )

#define testle(r,i)	\
	( (lsunit(r)<=(i)) && (significance(r)<=1) )

#define mp_square(r1,r2) mp_mult(r1,r2,r2)
	/* Square r2, returning product in r1 */

#define mp_modsquare(r1,r2) mp_modmult(r1,r2,r2)
	/* Square r2, returning modulo'ed product in r1 */

#define countbytes(r) ((countbits(r)+7)>>3)

/*	SLOP_BITS is how many "carry bits" to allow for intermediate
	calculation results to exceed the size of the modulus.
	It is used by modexp to give some overflow elbow room for
	modmult to use to perform modulo operations with the modulus.
	The number of slop bits required is determined by the modmult
	algorithm.  The Russian peasant modmult algorithm only requires
	1 slop bit, for example.  Note that if we use an external assembly
	modmult routine, SLOP_BITS may be meaningless or may be defined in a
	non-constant manner.
*/
#define PEASANT_SLOP_BITS	1
#define MERRITT_SLOP_BITS	UNITSIZE
#define UPTON_SLOP_BITS	(UNITSIZE/2)
#ifdef  mp_smul /* old version requires MS word = 0 */
#define SMITH_SLOP_BITS	UNITSIZE
#else           /* mp_smula or C version of mp_smul */
#define SMITH_SLOP_BITS	0
#endif /* mp_smul */

/*	MAX_BIT_PRECISION is upper limit that assembly primitives can handle.
	It must be less than 32704 bits, or 4088 bytes.  It should be an
	integer multiple of UNITSIZE*2.
*/
#if (SLOP_BITS > 0)
#define MAX_BIT_PRECISION (2048+(2*UNITSIZE))
#else
#define MAX_BIT_PRECISION 2048
#endif
#define MAX_BYTE_PRECISION (MAX_BIT_PRECISION/8)
#define MAX_UNIT_PRECISION (MAX_BIT_PRECISION/UNITSIZE)


/* global_precision is the unit precision last set by set_precision */
extern short global_precision;


/*	The "bit sniffer" macros all begin sniffing at the MSB.
	They are used internally by all the various multiply, divide, 
	modulo, exponentiation, and square root functions.
*/
#define sniff_bit(bptr,bitmask)	(*(bptr) & bitmask)

#define init_bitsniffer(bptr,bitmask,prec,bits) \
{ normalize(bptr,prec); \
  if (!prec) \
    return(0); \
  bits = units2bits(prec); \
  make_msbptr(bptr,prec); bitmask = uppermostbit; \
  while (!sniff_bit(bptr,bitmask)) \
  { bitmask >>= 1; bits--; \
  } \
}

#define bump_bitsniffer(bptr,bitmask) \
{ if (!(bitmask >>= 1)) \
  { bitmask = uppermostbit; \
	post_lowerunit(bptr); \
  } \
}

/* bump_2bitsniffers is used internally by mp_udiv. */
#define bump_2bitsniffers(bptr,bptr2,bitmask) \
{ if (!(bitmask >>= 1)) \
  { bitmask = uppermostbit; \
    post_lowerunit(bptr); \
    post_lowerunit(bptr2); \
  } \
}

/* stuff_bit is used internally by mp_udiv and mp_sqrt. */
#define stuff_bit(bptr,bitmask)	*(bptr) |= bitmask


boolean mp_addc
	(register unitptr r1,const unit * r2,register boolean carry);
	/* multiprecision add with carry r2 to r1, result in r1 */

boolean mp_subb
	(register unitptr r1,const unit * r2,register boolean borrow);
	/* multiprecision subtract with borrow, r2 from r1, result in r1 */

boolean mp_rotate_left(register unitptr r1,register boolean carry);
	/* multiprecision rotate left 1 bit with carry, result in r1. */

void mp_shift_right_bits(register unitptr r1,register short bits);
	/* multiprecision shift right bits, result in r1. */

short mp_compare(const unit * r1,const unit * r2);
	/* Compares registers *r1, *r2, and returns -1, 0, or 1 */

boolean mp_inc(register unitptr r);
	/* Increment multiprecision integer r. */

boolean mp_dec(register unitptr r);
	/* Decrement multiprecision integer r. */

void mp_neg(register unitptr r);
	/* Compute 2's complement, the arithmetic negative, of r */

#ifndef mp_move
#define mp_move(d,s)    memcpy((void*)(d), (void*)(s), \
		units2bytes(global_precision))
#endif
#ifndef unitfill0
#define unitfill0(r,ct) memset((void*)(r), 0, units2bytes(ct))
#endif

#ifndef mp_burn
#define mp_burn(r) mp_init(r,0) /* for burning the evidence */
#define mp_init0(r) mp_init(r,0)
#endif

#define empty_array(r)  unitfill0(r, sizeof(r)/sizeof(r[0])/sizeof(unit))

void mp_init(register unitptr r, word16 value);
	/* Init multiprecision register r with short value. */

short significance(const unit * r);
	/* Returns number of significant units in r */

int mp_udiv(register unitptr remainder,register unitptr quotient,
	const unit * dividend,const unit * divisor);
	/* Unsigned divide, treats both operands as positive. */

int mp_recip(register unitptr quotient,const unit * divisor);
	/* Compute reciprocal as 1/divisor.  Used by faster modmult. */

int mp_div(register unitptr remainder,register unitptr quotient,
	unit * dividend, unit * divisor);
	/* Signed divide, either or both operands may be negative. */

word16 mp_shortdiv(register unitptr quotient,
	const unit * dividend,register word16 divisor);
	/* Returns short remainder of unsigned divide. */

int mp_mod(register unitptr remainder,
	const unit * dividend,const unit * divisor);
	/* Unsigned divide, treats both operands as positive. */

word16 mp_shortmod(register unitptr dividend,register word16 divisor);
	/* Just returns short remainder of unsigned divide. */

int mp_mult(register unitptr prod,
	const unit * multiplicand,const unit * multiplier);
	/* Computes multiprecision prod = multiplicand * multiplier */

int countbits(const unit * r);
	/* Returns number of significant bits in r. */

int stage_peasant_modulus(const unit * n);
int stage_merritt_modulus(const unit * n);
int stage_upton_modulus(const unit * n);
int stage_smith_modulus(const unit * n);
	/* Must pass modulus to stage_modulus before calling modmult. */

int peasant_modmult(register unitptr prod,
	const unit * multiplicand,const unit * multiplier);
int merritt_modmult(register unitptr prod,
	const unit * multiplicand,const unit * multiplier);
int upton_modmult(register unitptr prod,
	const unit * multiplicand,const unit * multiplier);
int smith_modmult(register unitptr prod,
	const unit * multiplicand,const unit * multiplier);
	/* Performs combined multiply/modulo operation, with global modulus */

void peasant_burn();
void merritt_burn();
void upton_burn();
void smith_burn();

int mp_modexp(register unitptr expout,const unit * expin,
	const unit * exponent,const unit * modulus);
	/* Combined exponentiation/modulo algorithm. */

int mp_modexp_crt(unitptr expout, const unit * expin,
	const unit * p, const unit * q, const unit * ep, const unit * eq, const unit * u);
	/* exponentiation and modulo using Chinese Remainder Theorem */

/****************** end of MPI library ****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -