Ejemplo n.º 1
0
static void sha1_core(struct sha1_ctx * ctx)
{
	__m128i a, b, c, d, e;
	__m128i k0, k1, k2, k3;
	__m128i v1, w;
	SVAL buf[16];

	a = SET1(AA);
	b = SET1(BB);
	c = SET1(CC);
	d = SET1(DD);
	e = SET1(EE);

	k0 = SET1(K0);
	R20(SHA1R0, 0);

	k1 = SET1(K1);
	R20(SHA1R1, 20);

	k2 = SET1(K2);
	R20(SHA1R2, 40);

	k3 = SET1(K3);
	R16(SHA1R3, 60);
	if (!ssresult_check(ctx->sres, a)) return;
	R4(SHA1R3, 76);

	FINAL(0, a, AA);
	FINAL(1, b, BB);
	FINAL(2, c, CC);
	FINAL(3, d, DD);
	FINAL(4, e, EE);
	check_result(ctx);
}
void sieve() {
    register int i, j, k;
    SET1(1);
    int n = 50000;
    for (i = 2; i <= n; i++) {
        if (!GET1(i)) {
            for (k = n/i, j = i*k; k >= i; k--, j -= i)
                SET1(j);
            P[Pt++] = i;
        }
    }
}
Ejemplo n.º 3
0
/*
 * is_pinned()
 *	Return whether a piece at the specified position
 *	would be pinned to the king to move.
 */
Eximpl Bool
is_pinned(
    register Xconst Board*	bp,
    register rPosition		pos)
{
    register rPosition		kpos;
    register int		dir;
    register PieceSet		enemies;
    register const Field*	p;

    kpos = K_POS(bp, bp->b_tomove);
    dir = att_dir(kpos, pos);
    if( dam_dir(dir) ) {
        enemies = F_IATT(&(bp->b_f[kpos])) & F_DATT(&(bp->b_f[pos]))
                  & COLOUR_MASK(opp_colour(bp->b_tomove));
        if( enemies ) {
            if( (kpos + dam_mov[dir]) != pos ) {	/* space between */
                if( F_IATT(&(bp->b_f[pos - dam_mov[dir]])) & enemies ) {
                    return TRUE;
                }
            } else {					/* no space between */
                p = &( bp->b_f[pos] );
                do {
                    p += dam_mov[dir];
                } while( p->f_c == empty );
                if( (p->f_c == opp_colour(bp->b_tomove))
                        && (enemies & SET1(p->f_idx)) ) {
                    return TRUE;
                }
            }
        }
    }
    return FALSE;
}
Ejemplo n.º 4
0
int Encrypt_Xor(unsigned char *out, unsigned char *in, u64 nonce[], u256 rk[], u64 key[], int numbytes)
{
  u64  x[2],y[2];
  u256 X[5],Y[5];

  if (numbytes==16){
    x[0]=nonce[1]; y[0]=nonce[0]; nonce[0]++;
    Enc(x,y,key,1);
    ((u64 *)out)[1]=x[0]; ((u64 *)out)[0]=y[0];

    return 0;
  }

  if (numbytes==32){
    x[0]=nonce[1]; y[0]=nonce[0]; nonce[0]++;
    x[1]=nonce[1]; y[1]=nonce[0]; nonce[0]++;
    Enc(x,y,key,2);
    ((u64 *)out)[1]=x[0]^((u64 *)in)[1]; ((u64 *)out)[0]=y[0]^((u64 *)in)[0];
    ((u64 *)out)[3]=x[1]^((u64 *)in)[3]; ((u64 *)out)[2]=y[1]^((u64 *)in)[2];

    return 0;
  }
  
  SET1(X[0],nonce[1]);
  SET4(Y[0],nonce[0]);

  if (numbytes==64) Enc(X,Y,rk,4);
  else{
    X[1]=X[0];
    SET4(Y[1],nonce[0]);
    if (numbytes==128) Enc(X,Y,rk,8);
    else{
      X[2]=X[0];
      SET4(Y[2],nonce[0]);
      if (numbytes==192) Enc(X,Y,rk,12);
      else{
	X[3]=X[0];
	SET4(Y[3],nonce[0]);
	if (numbytes==256) Enc(X,Y,rk,16);
	else{
	  X[4]=X[0];
	  SET4(Y[4],nonce[0]);
	  Enc(X,Y,rk,20);
	}
      }
    }
  }

  XOR_STORE(in,out,X[0],Y[0]);

  if (numbytes>=128) XOR_STORE(in+64,out+64,X[1],Y[1]);
  if (numbytes>=192) XOR_STORE(in+128,out+128,X[2],Y[2]);
  if (numbytes>=256) XOR_STORE(in+192,out+192,X[3],Y[3]);
  if (numbytes>=320) XOR_STORE(in+256,out+256,X[4],Y[4]);

  return 0;
}
Ejemplo n.º 5
0
static void step3(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin)
{
	int zerosFound;
	int row, col, cstar;

	zerosFound = 1;
	while (zerosFound) {
		zerosFound = 0;
		for (col = 0; col < ncols; col++) {
			if (GET1(ccol, col))
				continue;
			for (row = 0; row < nrows; row++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(crow, row))
					continue;

				/* prime zero */
				SET2(mprime, row, col);

				/* find starred zero in current row */
				for (cstar = 0; cstar < ncols; cstar++)
					if (GET2(mstar, row, cstar))
						break;

				if (cstar == ncols) { /* no starred zero */
					/* move to step 4 */
					step4(ix, mdist, mstar, nmstar,
					      mprime, ccol, crow, nrows, ncols,
					      dmin, row, col);
					return;
				} else {
					SET1(crow, row);
					CLEAR1(ccol, cstar);
					zerosFound = 1;
					break;
				}
			}
		}
	}

	/* move to step 5 */
	step5(ix, mdist, mstar, nmstar,
	      mprime, ccol, crow, nrows, ncols,
	      dmin);
}
Ejemplo n.º 6
0
    Eximpl void
dump_Pset( PieceSet pset )
{
    short	i;
    Flag	first;

    printf("{" /*}*/);
    first = TRUE;
    for( i=0 ; i < 2*MAX_PIECE ; ++i ) {
	if( pset & SET1(i) ) {
	    if( ! first ) {
		printf(",");
	    }
	    printf("%d", i);
	    first = FALSE;
	}
    }
    printf(/*{*/ "}");
}
Ejemplo n.º 7
0
static void step2a(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		   mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		   int dmin)
{
	int col, row;

	/* cover every column containing a starred zero */
	for (col = 0; col < ncols; col++) {
		for (row = 0; row < nrows; row++) {
			if (!GET2(mstar, row, col))
				continue;
			SET1(ccol, col);
			break;
		}
	}

	/* move to step 3 */
	step2b(ix, mdist, mstar, nmstar,
	       mprime, ccol, crow, nrows, ncols,
	       dmin);
}
Ejemplo n.º 8
0
int Encrypt(unsigned char *out, u64 nonce[], u128 rk[], u64 key[], int numbytes)
{
  u64  x[2],y[2];
  u128 X[4],Y[4],Z[4];

  if (numbytes==16){
    x[0]=nonce[1]; y[0]=nonce[0]; nonce[0]++;
    Enc(x,y,key,1);
    ((u64 *)out)[1]=x[0]; ((u64 *)out)[0]=y[0];

    return 0;
  }

  SET1(X[0],nonce[1]);
  SET2(Y[0],nonce[0]);

  if (numbytes==32) Enc(X,Y,rk,2); 
  else{
    X[1]=X[0];
    SET2(Y[1],nonce[0]);
    if (numbytes==64) Enc(X,Y,rk,4); 
    else{
      X[2]=X[0];
      SET2(Y[2],nonce[0]);
      if (numbytes==96) Enc(X,Y,rk,6); 
      else{
	X[3]=X[0];
	SET2(Y[3],nonce[0]);
	Enc(X,Y,rk,8);
      }
    }
  }
  
  STORE(out,X[0],Y[0]);
  if (numbytes>=64)  STORE(out+32,X[1],Y[1]);
  if (numbytes>=96)  STORE(out+64,X[2],Y[2]);
  if (numbytes>=128) STORE(out+96,X[3],Y[3]);

  return 0;
}
Ejemplo n.º 9
0
#define CMP(read1, read2) SMP_OP_ALU_CMP_DECL(read1, read2)

#define RMW(type, op) SMP_OP_RMW_##type##_DECL(op)
#define RMWW(op) SMP_OP_RMWW_DP_DECL(op)

#define MOVE(dst, src) SMP_OP_MOVE_##dst##_DECL(src)
#define MOVE_(dst, src) SMP_OP_MOVE_DECL(dst, src)

#define BRANCH(flag) SMP_OP_BRANCH_FLAG_DECL(flag)
#define BRANCH_N(flag) SMP_OP_BRANCH_N_FLAG_DECL(flag)

smp_op_t ssnes_smp_optable[256] = {
   NOP,                    // 0x00
   TCALL(0),               // 0x01
   SET1(0),                // 0x02
   BBS(0),                 // 0x03
   ALU(DP, or),            // 0x04
   ALU(ADDR, or),          // 0x05
   ALU(DPX, or),           // 0x06
   ALU(IDPX, or),          // 0x07
   ALU(IMM, or),           // 0x08
   ALU(DP_DP, or),         // 0x09
   ALU(BIT, or1),          // 0x0a
   RMW(DP, asl),           // 0x0b
   RMW(ADDR, asl),         // 0x0c
   smp_op_push_p,          // 0x0d
   smp_op_rmw_tset,        // 0x0e
   smp_op_brk,             // 0x0f

   BRANCH_N(n),            // 0x10
Ejemplo n.º 10
0
/*
 - slow - step through the string more deliberately
 == static const char *slow(struct match *m, const char *start, \
 ==	const char *stop, sopno startst, sopno stopst);
 */
static const char *		/* where it ended */
slow(	struct match *m,
	const char *start,
	const char *stop,
	sopno startst,
	sopno stopst)
{
	states st = m->st;
	states empty = m->empty;
	states tmp = m->tmp;
	const char *p = start;
	wint_t c;
	wint_t lastc;		/* previous c */
	wint_t flagch;
	int i;
	const char *matchp;	/* last p at which a match ended */
	size_t clen;

	AT("slow", start, stop, startst, stopst);
	CLEAR(st);
	SET1(st, startst);
	SP("sstart", st, *p);
	st = step(m->g, startst, stopst, st, NOTHING, st);
	matchp = NULL;
	if (start == m->beginp)
		c = OUT;
	else {
		/*
		 * XXX Wrong if the previous character was multi-byte.
		 * Newline never is (in supported encodings),
		 * so this only breaks the ISWORD tests below.
		 */
		c = (uch)*(start - 1);
	}
	for (;;) {
		/* next character */
		lastc = c;
		if (p == m->endp) {
			c = OUT;
			clen = 0;
		} else
			clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);

		/* is there an EOL and/or BOL between lastc and c? */
		flagch = '\0';
		i = 0;
		if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
				(lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
			flagch = BOL;
			i = m->g->nbol;
		}
		if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
				(c == OUT && !(m->eflags&REG_NOTEOL)) ) {
			flagch = (flagch == BOL) ? BOLEOL : EOL;
			i += m->g->neol;
		}
		if (i != 0) {
			for (; i > 0; i--)
				st = step(m->g, startst, stopst, st, flagch, st);
			SP("sboleol", st, c);
		}

		/* how about a word boundary? */
		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
					(c != OUT && ISWORD(c)) ) {
			flagch = BOW;
		}
		if ( (lastc != OUT && ISWORD(lastc)) &&
				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
			flagch = EOW;
		}
		if (flagch == BOW || flagch == EOW) {
			st = step(m->g, startst, stopst, st, flagch, st);
			SP("sboweow", st, c);
		}

		/* are we done? */
		if (ISSET(st, stopst))
			matchp = p;
		if (EQ(st, empty) || p == stop || clen > stop - p)
			break;		/* NOTE BREAK OUT */

		/* no, we must deal with this character */
		ASSIGN(tmp, st);
		ASSIGN(st, empty);
		assert(c != OUT);
		st = step(m->g, startst, stopst, tmp, c, st);
		SP("saft", st, c);
		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
		p += clen;
	}

	return(matchp);
}
Ejemplo n.º 11
0
void
check_various (void)
{
  mpz_t  x, y;

  mpz_init (x);
  mpz_init (y);

  mpz_realloc (x, (mp_size_t) 20);
  mpz_realloc (y, (mp_size_t) 20);

  /* 0 cmp 0, junk in low limbs */
  SET1 (x,0, 123);
  SET1 (y,0, 456);
  check_all (x, y, 0, 0);


  /* 123 cmp 0 */
  SET1 (x,1, 123);
  SET1 (y,0, 456);
  check_all (x, y, 1, 1);

  /* 123:456 cmp 0 */
  SET2 (x,2, 456,123);
  SET1 (y,0, 9999);
  check_all (x, y, 1, 1);


  /* 123 cmp 123 */
  SET1(x,1, 123);
  SET1(y,1, 123);
  check_all (x, y, 0, 0);

  /* -123 cmp 123 */
  SET1(x,-1, 123);
  SET1(y,1,  123);
  check_all (x, y, -1, 0);


  /* 123 cmp 456 */
  SET1(x,1, 123);
  SET1(y,1, 456);
  check_all (x, y, -1, -1);

  /* -123 cmp 456 */
  SET1(x,-1, 123);
  SET1(y,1,  456);
  check_all (x, y, -1, -1);

  /* 123 cmp -456 */
  SET1(x,1,  123);
  SET1(y,-1, 456);
  check_all (x, y, 1, -1);


  /* 1:0 cmp 1:0 */
  SET2 (x,2, 1,0);
  SET2 (y,2, 1,0);
  check_all (x, y, 0, 0);

  /* -1:0 cmp 1:0 */
  SET2 (x,-2, 1,0);
  SET2 (y,2,  1,0);
  check_all (x, y, -1, 0);


  /* 2:0 cmp 1:0 */
  SET2 (x,2, 2,0);
  SET2 (y,2, 1,0);
  check_all (x, y, 1, 1);


  /* 4:3:2:1 cmp 2:1 */
  SET4 (x,4, 4,3,2,1);
  SET2 (y,2, 2,1);
  check_all (x, y, 1, 1);

  /* -4:3:2:1 cmp 2:1 */
  SET4 (x,-4, 4,3,2,1);
  SET2 (y,2,  2,1);
  check_all (x, y, -1, 1);


  mpz_clear (x);
  mpz_clear (y);
}
Ejemplo n.º 12
0
/*
 - slow - step through the string more deliberately
 */
static char *			/* where it ended */
slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst)
{
	states st = m->st;
	states empty = m->empty;
	states tmp = m->tmp;
	char *p = start;
	int c = (start == m->beginp) ? OUT : *(start-1);
	int lastc;	/* previous c */
	int flagch;
	int i;
	char *matchp;	/* last p at which a match ended */

	AT("slow", start, stop, startst, stopst);
	CLEAR(st);
	SET1(st, startst);
	SP("sstart", st, *p);
	st = step(m->g, startst, stopst, st, NOTHING, st);
	matchp = NULL;
	for (;;) {
		/* next character */
		lastc = c;
		c = (p == m->endp) ? OUT : *p;

		/* is there an EOL and/or BOL between lastc and c? */
		flagch = '\0';
		i = 0;
		if ( (lastc == '\n' && m->g->cflags&R_REGEX_NEWLINE) ||
				(lastc == OUT && !(m->eflags&R_REGEX_NOTBOL)) ) {
			flagch = BOL;
			i = m->g->nbol;
		}
		if ( (c == '\n' && m->g->cflags&R_REGEX_NEWLINE) ||
				(c == OUT && !(m->eflags&R_REGEX_NOTEOL)) ) {
			flagch = (flagch == BOL) ? BOLEOL : EOL;
			i += m->g->neol;
		}
		if (i != 0) {
			for (; i > 0; i--)
				st = step(m->g, startst, stopst, st, flagch, st);
			SP("sboleol", st, c);
		}

		/* how about a word boundary? */
		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
					(c != OUT && ISWORD(c)) ) {
			flagch = BOW;
		}
		if ( (lastc != OUT && ISWORD(lastc)) &&
				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
			flagch = EOW;
		}
		if (flagch == BOW || flagch == EOW) {
			st = step(m->g, startst, stopst, st, flagch, st);
			SP("sboweow", st, c);
		}

		/* are we done? */
		if (ISSET(st, stopst))
			matchp = p;
		if (EQ(st, empty) || p == stop)
			break;		/* NOTE BREAK OUT */

		/* no, we must deal with this character */
		ASSIGN(tmp, st);
		ASSIGN(st, empty);
		assert(c != OUT);
		st = step(m->g, startst, stopst, tmp, c, st);
		SP("saft", st, c);
		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
		p++;
	}

	return(matchp);
}
Ejemplo n.º 13
0
/*
 - fast - step through the string at top speed
 */
static char *			/* where tentative match ended, or NULL */
fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst)
{
	states st = m->st;
	states fresh = m->fresh;
	states tmp = m->tmp;
	char *p = start;
	int c = (start == m->beginp) ? OUT : *(start-1);
	int lastc;	/* previous c */
	int flagch;
	int i;
	char *coldp;	/* last p after which no match was underway */

	CLEAR(st);
	SET1(st, startst);
	st = step(m->g, startst, stopst, st, NOTHING, st);
	ASSIGN(fresh, st);
	SP("start", st, *p);
	coldp = NULL;
	for (;;) {
		/* next character */
		lastc = c;
		c = (p == m->endp) ? OUT : *p;
		if (st==fresh)
			coldp = p;

		/* is there an EOL and/or BOL between lastc and c? */
		flagch = '\0';
		i = 0;
		if ( (lastc == '\n' && m->g->cflags&R_REGEX_NEWLINE) ||
				(lastc == OUT && !(m->eflags&R_REGEX_NOTBOL)) ) {
			flagch = BOL;
			i = m->g->nbol;
		}
		if ( (c == '\n' && m->g->cflags&R_REGEX_NEWLINE) ||
				(c == OUT && !(m->eflags&R_REGEX_NOTEOL)) ) {
			flagch = (flagch == BOL) ? BOLEOL : EOL;
			i += m->g->neol;
		}
		if (i != 0) {
			for (; i > 0; i--)
				st = step(m->g, startst, stopst, st, flagch, st);
			SP("boleol", st, c);
		}

		/* how about a word boundary? */
		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
					(c != OUT && ISWORD(c)) ) {
			flagch = BOW;
		}
		if ( (lastc != OUT && ISWORD(lastc)) &&
				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
			flagch = EOW;
		}
		if (flagch == BOW || flagch == EOW) {
			st = step(m->g, startst, stopst, st, flagch, st);
			SP("boweow", st, c);
		}

		/* are we done? */
		if (ISSET(st, stopst) || p == stop)
			break;		/* NOTE BREAK OUT */

		/* no, we must deal with this character */
		ASSIGN(tmp, st);
		ASSIGN(st, fresh);
		assert(c != OUT);
		st = step(m->g, startst, stopst, tmp, c, st);
		SP("aft", st, c);
		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
		p++;
	}

	assert(coldp != NULL);
	m->coldp = coldp;
	if (ISSET(st, stopst))
		return(p+1);
	return NULL;
}
Ejemplo n.º 14
0
/*
 * mg_app_prom()
 *	Append all legal promotion moves to the specified list.
 *	No conditions need be assured.
 *	Optionally there are left out obviously non-checking moves.
 */
Eximpl void
mg_app_prom(
    register Xconst Board*	bp,
    Movelist*			lp,
    Bool			needcheck)
{
    register rColour		self;
    register rColour		enemy;
    register Xconst Field*	p;
    register Xconst Field*	tp;
    register rPosition		pos;
    register rPosition		tpos;
    register rPosition		kpos;
    register rPieceSet		ckset;
    register int		pindir;
    register const Field*	ep;
    register int		delta;
    register int		i;
    register Bool		needckdirect;
    register rPosition		ekpos;
    register Xconst Field*	ekp;
    register int		ckdir;
    PieceSet			amask;

    self  = bp->b_tomove;
    enemy = opp_colour(self);
    kpos = K_POS(bp, self);
    ckset = F_DATT(&(bp->b_f[kpos])) & COLOUR_MASK(enemy);	/* those say check */
    if( min2elems(ckset) ) {
        return;						/* double check */
    }
    if( needcheck ) {
        amask = COLOUR_MASK(self);
        ekp = &(bp->b_f[ ekpos = K_POS(bp, enemy) ]);
    }
    /*
     * Scan enemies base line to find own promotion candidates:
     */
    p = &(bp->b_f[ pos = MK_POS(0, BAS_LIN(enemy)) ]);
    for( ; p->f_c != border ; pos += MK_DELTA(1,0), p += MK_DELTA(1,0) ) {
        if( (p->f_c == self) && (p->f_f == bauer) ) {
            pindir = att_dir(kpos, pos);
            needckdirect =
                (  needcheck
                   && ( ! dam_dir(att_dir(pos, ekpos))
                        || ! (F_DATT(p) & F_IATT(ekp) & amask)
                      )
                );
            for( i=0 ; i<2 ; ++i ) {
                tp = &(bp->b_f[ tpos = pos + (i ? bau_left:bau_right)[self] ]);
                if( tp->f_c == enemy ) {		/* pseudo-legal */
                    if( ( !ckset
                            || (ckset == SET1(tp->f_idx))
                        )			/* existing check removed */
                            && ( no_dir(pindir)
                                 || (pindir == att_dir(kpos, tpos))
                                 || ! is_pinned(bp, pos)
                               )			/* no check uncovered */
                      ) {
                        if( ! needckdirect ) {
                            app_4_proms(lp, pos, tpos, p->f_idx);
                        } else if( ! no_dir(ckdir = att_dir(tpos, ekpos)) ) {
                            app_ck_proms(lp, pos, tpos, p->f_idx, ckdir);
                        }
                    }
                }
            }
            tp = &(bp->b_f[ tpos = pos + bau_mov[self] ]);
            if( tp->f_c == empty ) {		/* pseudo-legal */
                if( ( !ckset
                        || ( (ckset & F_DATT(tp))
                             && trm_dir(att_dir(kpos, tpos))
                           )
                    )			/* existing check may be removed */
                        && ( no_dir(pindir)
                             || (pindir == att_dir(kpos, tpos))
                             || ! is_pinned(bp, pos)
                           )			/* no check uncovered */
                  ) {
                    if( ckset ) {
                        delta = dam_mov[att_dir(kpos, tpos)];
                        ep = tp;
                        do {
                            ep += delta;
                        } while( ep->f_c == empty );
                        if( (ep->f_c == enemy)
                                && (ckset == SET1(ep->f_idx)) ) {
                            if( ! needckdirect ) {
                                app_4_proms(lp, pos, tpos, p->f_idx);
                            } else if( ! no_dir(ckdir = att_dir(tpos, ekpos)) ) {
                                app_ck_proms(lp, pos, tpos, p->f_idx, ckdir);
                            }
                        }
                    } else {
                        if( ! needckdirect ) {
                            app_4_proms(lp, pos, tpos, p->f_idx);
                        } else if( ! no_dir(ckdir = att_dir(tpos, ekpos)) ) {
                            app_ck_proms(lp, pos, tpos, p->f_idx, ckdir);
                        }
                    }
                }
            }
        }
    } /* for pos, p */
}
Ejemplo n.º 15
0
//gfd*
static void md5_process(mir_md5_state_t *pms, const mir_md5_byte_t *data /*[64]*/)
{
    mir_md5_word_t
    a = pms->abcd[0], b = pms->abcd[1],
    c = pms->abcd[2], d = pms->abcd[3];
    mir_md5_word_t t;
    /* Define storage for little-endian or both types of CPUs. */
    mir_md5_word_t xbuf[16];
    const mir_md5_word_t *X;

    {
        /*
        * Determine dynamically whether this is a big-endian or
        * little-endian machine, since we can use a more efficient
        * algorithm on the latter.
        */
        static const int w = 1;

        if (*((const mir_md5_byte_t *)&w)) /* dynamic little-endian */
        {
            /*
            * On little-endian machines, we can process properly aligned
            * data without copying it.
            */
            if (!((data - (const mir_md5_byte_t *)0) & 3)) {
                /* data are properly aligned */
                X = (const mir_md5_word_t *)data;
            } else {
                /* not aligned */
                memcpy(xbuf, data, 64);
                X = xbuf;
            }
        }
        else      /* dynamic big-endian */
        {
            /*
            * On big-endian machines, we must arrange the bytes in the
            * right order.
            */
            const mir_md5_byte_t *xp = data;
            int i;

            X = xbuf;    /* (dynamic only) */
            for (i = 0; i < 16; ++i, xp += 4)
                xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
        }
    }

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

    /* Round 1. */
    /* Let [abcd k s i] denote the operation
    a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET1(a, b, c, d, k, s, Ti)\
	t = a + F(b,c,d) + X[k] + Ti;\
	a = ROTATE_LEFT(t, s) + b
    /* Do the following 16 operations. */
    SET1(a, b, c, d,  0,  7,  T1);
    SET1(d, a, b, c,  1, 12,  T2);
    SET1(c, d, a, b,  2, 17,  T3);
    SET1(b, c, d, a,  3, 22,  T4);
    SET1(a, b, c, d,  4,  7,  T5);
    SET1(d, a, b, c,  5, 12,  T6);
    SET1(c, d, a, b,  6, 17,  T7);
    SET1(b, c, d, a,  7, 22,  T8);
    SET1(a, b, c, d,  8,  7,  T9);
    SET1(d, a, b, c,  9, 12, T10);
    SET1(c, d, a, b, 10, 17, T11);
    SET1(b, c, d, a, 11, 22, T12);
    SET1(a, b, c, d, 12,  7, T13);
    SET1(d, a, b, c, 13, 12, T14);
    SET1(c, d, a, b, 14, 17, T15);
    SET1(b, c, d, a, 15, 22, T16);

    /* Round 2. */
    /* Let [abcd k s i] denote the operation
    a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET2(a, b, c, d, k, s, Ti)\
	t = a + G(b,c,d) + X[k] + Ti;\
	a = ROTATE_LEFT(t, s) + b
    /* Do the following 16 operations. */
    SET2(a, b, c, d,  1,  5, T17);
    SET2(d, a, b, c,  6,  9, T18);
    SET2(c, d, a, b, 11, 14, T19);
    SET2(b, c, d, a,  0, 20, T20);
    SET2(a, b, c, d,  5,  5, T21);
    SET2(d, a, b, c, 10,  9, T22);
    SET2(c, d, a, b, 15, 14, T23);
    SET2(b, c, d, a,  4, 20, T24);
    SET2(a, b, c, d,  9,  5, T25);
    SET2(d, a, b, c, 14,  9, T26);
    SET2(c, d, a, b,  3, 14, T27);
    SET2(b, c, d, a,  8, 20, T28);
    SET2(a, b, c, d, 13,  5, T29);
    SET2(d, a, b, c,  2,  9, T30);
    SET2(c, d, a, b,  7, 14, T31);
    SET2(b, c, d, a, 12, 20, T32);

    /* Round 3. */
    /* Let [abcd k s t] denote the operation
    a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define SET3(a, b, c, d, k, s, Ti)\
	t = a + H(b,c,d) + X[k] + Ti;\
	a = ROTATE_LEFT(t, s) + b
    /* Do the following 16 operations. */
    SET3(a, b, c, d,  5,  4, T33);
    SET3(d, a, b, c,  8, 11, T34);
    SET3(c, d, a, b, 11, 16, T35);
    SET3(b, c, d, a, 14, 23, T36);
    SET3(a, b, c, d,  1,  4, T37);
    SET3(d, a, b, c,  4, 11, T38);
    SET3(c, d, a, b,  7, 16, T39);
    SET3(b, c, d, a, 10, 23, T40);
    SET3(a, b, c, d, 13,  4, T41);
    SET3(d, a, b, c,  0, 11, T42);
    SET3(c, d, a, b,  3, 16, T43);
    SET3(b, c, d, a,  6, 23, T44);
    SET3(a, b, c, d,  9,  4, T45);
    SET3(d, a, b, c, 12, 11, T46);
    SET3(c, d, a, b, 15, 16, T47);
    SET3(b, c, d, a,  2, 23, T48);

    /* Round 4. */
    /* Let [abcd k s t] denote the operation
    a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define SET4(a, b, c, d, k, s, Ti)\
	t = a + I(b,c,d) + X[k] + Ti;\
	a = ROTATE_LEFT(t, s) + b
    /* Do the following 16 operations. */
    SET4(a, b, c, d,  0,  6, T49);
    SET4(d, a, b, c,  7, 10, T50);
    SET4(c, d, a, b, 14, 15, T51);
    SET4(b, c, d, a,  5, 21, T52);
    SET4(a, b, c, d, 12,  6, T53);
    SET4(d, a, b, c,  3, 10, T54);
    SET4(c, d, a, b, 10, 15, T55);
    SET4(b, c, d, a,  1, 21, T56);
    SET4(a, b, c, d,  8,  6, T57);
    SET4(d, a, b, c, 15, 10, T58);
    SET4(c, d, a, b,  6, 15, T59);
    SET4(b, c, d, a, 13, 21, T60);
    SET4(a, b, c, d,  4,  6, T61);
    SET4(d, a, b, c, 11, 10, T62);
    SET4(c, d, a, b,  2, 15, T63);
    SET4(b, c, d, a,  9, 21, T64);

    /* Then perform the following additions. (That is increment each
    of the four registers by the value it had before this block
    was started.) */
    pms->abcd[0] += a;
    pms->abcd[1] += b;
    pms->abcd[2] += c;
    pms->abcd[3] += d;
}
Ejemplo n.º 16
0
static void ixoptimal(int *ix, int *mdist, int nrows, int ncols)
{
	int *mdistTemp, *mdistEnd, *columnEnd, value, minValue;
	int dmin, row, col;
	col_t ccol, crow;
	mat_t mstar, mprime, nmstar;

	memset(ccol, 0, sizeof(col_t));
	memset(crow, 0, sizeof(col_t));
	memset(mstar, 0, sizeof(mat_t));
	memset(mprime, 0, sizeof(mat_t));
	memset(nmstar, 0, sizeof(mat_t));

	/* initialization */
	for (row = 0; row < nrows; row++)
		ix[row] = -1;

	mdistEnd = mdist + nrows * ncols;

	/* preliminary steps */
	if (nrows <= ncols) {
		dmin = nrows;

		for (row = 0; row < nrows; row++) {
			/* find the smallest element in the row */
			mdistTemp = mdist + row;
			minValue = *mdistTemp;
			mdistTemp += nrows;
			while (mdistTemp < mdistEnd) {
				value = *mdistTemp;
				if (value < minValue)
					minValue = value;
				mdistTemp += nrows;
			}

			/* subtract the smallest element from each element
			   of the row */
			mdistTemp = mdist + row;
			while (mdistTemp < mdistEnd) {
				*mdistTemp -= minValue;
				mdistTemp += nrows;
			}
		}

		/* Steps 1 and 2a */
		for (row = 0; row < nrows; row++) {
			for (col = 0; col < ncols; col++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(ccol, col))
					continue;
				SET2(mstar, row, col);
				SET1(ccol, col);
				break;
			}
		}
	} else {
		dmin = ncols;

		for (col = 0; col < ncols; col++) {
			/* find the smallest element in the column */
			mdistTemp = mdist + nrows*col;
			columnEnd = mdistTemp + nrows;

			minValue = *mdistTemp++;
			while (mdistTemp < columnEnd) {
				value = *mdistTemp++;
				if (value < minValue)
					minValue = value;
			}

			/* subtract the smallest element from each element
			   of the column */
			mdistTemp = mdist + nrows*col;
			while (mdistTemp < columnEnd)
				*mdistTemp++ -= minValue;
		}

		/* Steps 1 and 2a */
		for (col = 0; col < ncols; col++) {
			for (row = 0; row < nrows; row++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(crow, row))
					continue;
				SET2(mstar, row, col);
				SET1(ccol, col);
				SET1(crow, row);
				break;
			}
		}
		memset(crow, 0, sizeof(col_t));
	}

	/* move to step 2b */
	step2b(ix, mdist, mstar, nmstar,
	       mprime, ccol, crow, nrows, ncols,
	       dmin);
}