Esempio n. 1
0
//removes the children of any node with less than limit work
void SolverPNS2::garbage_collect(PNSNode * node){
	PNSNode * child = node->children.begin();
	PNSNode * end = node->children.end();

	for( ; child != end; child++){
		if(child->terminal()){ //solved
			//log heavy nodes?
			PLUS(nodes, -child->dealloc(ctmem));
		}else if(child->work < gclimit){ //low work, ignore solvedness since it's trivial to re-solve
			PLUS(nodes, -child->dealloc(ctmem));
		}else if(child->children.num() > 0){
			garbage_collect(child);
		}
	}
}
Esempio n. 2
0
void main(void)
{
  int delay;
  char ch;

  delay = DEF_TIME;

  m.load(word_list);

  if (setup())
    return;

  while(kbhit())
   getch();

  do
   {
    while (! kbhit())
      message(delay);
    ch = getch();
    if (MINUS(ch) &&
        (delay >= MIN_TIME))
      delay -= INC_TIME;
    if (PLUS(ch) &&
        (delay <= MAX_TIME))
      delay += INC_TIME;
   }
  while (ch == '-' || ch == '+');

  cleanup();

  cputs("\n\n\n(C) 1995   N.A.A. MATHEWSON");

}
Esempio n. 3
0
void AgentMCTS::create_children_simple(const Board & board, Node * node){
	assert(node->children.empty());

	node->children.alloc(board.moves_avail(), ctmem);

	Node * child = node->children.begin(),
		 * end   = node->children.end();
	MoveIterator moveit(board, prunesymmetry);
	int nummoves = 0;
	for(; !moveit.done() && child != end; ++moveit, ++child){
		*child = Node(*moveit);
		nummoves++;
	}

	if(prunesymmetry)
		node->children.shrink(nummoves); //shrink the node to ignore the extra moves
	else //both end conditions should happen in parallel
		assert(moveit.done() && child == end);

	PLUS(nodes, node->children.num());
}
Esempio n. 4
0
bool AgentPNS::PNSThread::pns(const Board & board, Node * node, int depth, uint32_t tp, uint32_t td){
	iters++;
	if(agent->maxdepth < depth)
		agent->maxdepth = depth;

	if(node->children.empty()){
		if(node->terminal())
			return true;

		if(agent->ctmem.memalloced() >= agent->memlimit)
			return false;

		if(!node->children.lock())
			return false;

		int numnodes = board.moves_avail();
		CompactTree<Node>::Children temp;
		temp.alloc(numnodes, agent->ctmem);

		unsigned int i = 0;
		unsigned int seen = 0;
		for(MoveIterator move(board); !move.done(); ++move){
			int outcome = solve1ply(move.board(), seen);
			unsigned int pd = 1;
			temp[i] = Node(*move).outcome(outcome, board.toplay(), agent->ties, pd);
			i++;
		}
		PLUS(agent->nodes, i);
		temp.shrink(i); //if symmetry, there may be extra moves to ignore
		node->children.swap(temp);
		assert(temp.unlock());

		PLUS(agent->nodes_seen, seen);

		updatePDnum(node);

		return true;
	}

	bool mem;
	do{
		Node * child = node->children.begin(),
		     * child2 = node->children.begin(),
		     * childend = node->children.end();

		uint32_t tpc, tdc;

		if(agent->df){
			for(Node * i = node->children.begin(); i != childend; i++){
				if(i->refdelta() <= child->refdelta()){
					child2 = child;
					child = i;
				}else if(i->refdelta() < child2->refdelta()){
					child2 = i;
				}
			}

			tpc = min(INF32/2, (td + child->phi - node->delta));
			tdc = min(tp, (uint32_t)(child2->delta*(1.0 + agent->epsilon) + 1));
		}else{
			tpc = tdc = 0;
			for(Node * i = node->children.begin(); i != childend; i++)
				if(child->refdelta() > i->refdelta())
					child = i;
		}

		Board next = board;
		next.move(child->move);

		child->ref();
		uint64_t itersbefore = iters;
		mem = pns(next, child, depth + 1, tpc, tdc);
		child->deref();
		PLUS(child->work, iters - itersbefore);

		if(updatePDnum(node) && !agent->df)
			break;

	}while(!agent->timeout && mem && (!agent->df || (node->phi < tp && node->delta < td)));

	return mem;
}
Esempio n. 5
0
static void salsa20_wordtobyte(u8 output[64],const u32 input[16])
{
  u32 x[16];
  unsigned int i;

  for (i = 0;i < 16;++i) x[i] = input[i];
  for (i = 20;i > 0;i -= 2) {
    x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 0],x[12]), 7));
    x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[ 4],x[ 0]), 9));
    x[12] = XOR(x[12],ROTATE(PLUS(x[ 8],x[ 4]),13));
    x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[12],x[ 8]),18));
    x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 5],x[ 1]), 7));
    x[13] = XOR(x[13],ROTATE(PLUS(x[ 9],x[ 5]), 9));
    x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[13],x[ 9]),13));
    x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 1],x[13]),18));
    x[14] = XOR(x[14],ROTATE(PLUS(x[10],x[ 6]), 7));
    x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[14],x[10]), 9));
    x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 2],x[14]),13));
    x[10] = XOR(x[10],ROTATE(PLUS(x[ 6],x[ 2]),18));
    x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[15],x[11]), 7));
    x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 3],x[15]), 9));
    x[11] = XOR(x[11],ROTATE(PLUS(x[ 7],x[ 3]),13));
    x[15] = XOR(x[15],ROTATE(PLUS(x[11],x[ 7]),18));
    x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[ 0],x[ 3]), 7));
    x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[ 1],x[ 0]), 9));
    x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[ 2],x[ 1]),13));
    x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[ 3],x[ 2]),18));
    x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 5],x[ 4]), 7));
    x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 6],x[ 5]), 9));
    x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 7],x[ 6]),13));
    x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 4],x[ 7]),18));
    x[11] = XOR(x[11],ROTATE(PLUS(x[10],x[ 9]), 7));
    x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[11],x[10]), 9));
    x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 8],x[11]),13));
    x[10] = XOR(x[10],ROTATE(PLUS(x[ 9],x[ 8]),18));
    x[12] = XOR(x[12],ROTATE(PLUS(x[15],x[14]), 7));
    x[13] = XOR(x[13],ROTATE(PLUS(x[12],x[15]), 9));
    x[14] = XOR(x[14],ROTATE(PLUS(x[13],x[12]),13));
    x[15] = XOR(x[15],ROTATE(PLUS(x[14],x[13]),18));
  }
  for (i = 0;i < 16;++i) x[i] = PLUS(x[i],input[i]);
  for (i = 0;i < 16;++i) U32TO8_LITTLE(output + 4 * i,x[i]);
}
Esempio n. 6
0
PEG_FUNC(parse_peg_open);
PEG_FUNC(parse_peg_close);
PEG_FUNC(parse_peg_dot);
PEG_FUNC(parse_peg_spacing);
PEG_FUNC(parse_peg_comment);
PEG_FUNC(parse_peg_space);
PEG_FUNC(parse_peg_eol);
PEG_FUNC(parse_peg_eof);
PEG_FUNC(parse_peg_hide);

/*************************************************/

/* grammar */

PEG_PARSE(parse_peg_grammar,        PEG_GRAMMAR_NODE,               L"PEG grammar",             SEQ(T(parse_peg_spacing),
                                                                                                    SEQ(PLUS(T(parse_peg_rule)),
                                                                                                        T(parse_peg_eof))));

PEG_PARSE(parse_peg_rule,           PEG_RULE_NODE,                  L"PEG rule",                SEQ(T(parse_peg_identifier),
                                                                                                    SEQ(QUES(T(parse_peg_literal)),
                                                                                                        SEQ(T(parse_peg_left_arrow),
                                                                                                            T(parse_peg_disjunction)))));

PEG_PARSE(parse_peg_disjunction,    PEG_DISJ_NODE,                  L"expression",              SEQ(T(parse_peg_conjunction), STAR(SEQ(T(parse_peg_slash), T(parse_peg_conjunction)))));

PEG_PARSE(parse_peg_conjunction,    PEG_CONJ_NODE,                  L"term",                    PLUS(T(parse_peg_prefix_exp)));

PEG_PARSE(parse_peg_prefix_exp,     PEG_PREFIX_EXP_NODE,            L"term",                    SEQ(QUES(DISJ(T(parse_peg_and), DISJ(T(parse_peg_not), T(parse_peg_hide)))), T(parse_peg_suffix_exp)));

PEG_PARSE(parse_peg_suffix_exp,     PEG_SUFFIX_EXP_NODE,            L"term",                    SEQ(T(parse_peg_term),
                                                                                                    QUES(DISJ(DISJ(T(parse_peg_question), 
Esempio n. 7
0
static void salsa20_wordtobyte(u8 output[64], const u32 input[16])
{
    u32 x0, x1, x2,  x3,  x4,  x5,  x6,  x7,
        x8, x9, x10, x11, x12, x13, x14, x15;
    int i;

    x0  = input[0];
    x1  = input[1];
    x2  = input[2];
    x3  = input[3];
    x4  = input[4];
    x5  = input[5];
    x6  = input[6];
    x7  = input[7];
    x8  = input[8];
    x9  = input[9];
    x10 = input[10];
    x11 = input[11];
    x12 = input[12];
    x13 = input[13];
    x14 = input[14];
    x15 = input[15];

    for( i=20; i>0; i-=2 ) {
        QUARTERROUND( x0, x4, x8,x12);
        QUARTERROUND( x1, x5, x9,x13);
        QUARTERROUND( x2, x6,x10,x14);
        QUARTERROUND( x3, x7,x11,x15);
        QUARTERROUND( x0, x5,x10,x15);
        QUARTERROUND( x1, x6,x11,x12);
        QUARTERROUND( x2, x7, x8,x13);
        QUARTERROUND( x3, x4, x9,x14);
    }

    x0  = PLUS(x0,  input[0]);
    x1  = PLUS(x1,  input[1]);
    x2  = PLUS(x2,  input[2]);
    x3  = PLUS(x3,  input[3]);
    x4  = PLUS(x4,  input[4]);
    x5  = PLUS(x5,  input[5]);
    x6  = PLUS(x6,  input[6]);
    x7  = PLUS(x7,  input[7]);
    x8  = PLUS(x8,  input[8]);
    x9  = PLUS(x9,  input[9]);
    x10 = PLUS(x10, input[10]);
    x11 = PLUS(x11, input[11]);
    x12 = PLUS(x12, input[12]);
    x13 = PLUS(x13, input[13]);
    x14 = PLUS(x14, input[14]);
    x15 = PLUS(x15, input[15]);

    U32TO8_LITTLE(output + 0,x0);
    U32TO8_LITTLE(output + 4,x1);
    U32TO8_LITTLE(output + 8,x2);
    U32TO8_LITTLE(output + 12,x3);
    U32TO8_LITTLE(output + 16,x4);
    U32TO8_LITTLE(output + 20,x5);
    U32TO8_LITTLE(output + 24,x6);
    U32TO8_LITTLE(output + 28,x7);
    U32TO8_LITTLE(output + 32,x8);
    U32TO8_LITTLE(output + 36,x9);
    U32TO8_LITTLE(output + 40,x10);
    U32TO8_LITTLE(output + 44,x11);
    U32TO8_LITTLE(output + 48,x12);
    U32TO8_LITTLE(output + 52,x13);
    U32TO8_LITTLE(output + 56,x14);
    U32TO8_LITTLE(output + 60,x15);
}
Esempio n. 8
0
bool SolverPNS2::SolverThread::pns(const Board & board, PNSNode * node, int depth, uint32_t tp, uint32_t td){
	iters++;
	if(solver->maxdepth < depth)
		solver->maxdepth = depth;

	if(node->children.empty()){
		if(node->terminal())
			return true;

		if(solver->ctmem.memalloced() >= solver->memlimit)
			return false;

		if(!node->children.lock())
			return false;

		int numnodes = board.movesremain();
		CompactTree<PNSNode>::Children temp;
		temp.alloc(numnodes, solver->ctmem);
		PLUS(solver->nodes, numnodes);

		if(solver->lbdist)
			dists.run(&board);

		int i = 0;
		for(Board::MoveIterator move = board.moveit(true); !move.done(); ++move){
			int outcome, pd;

			if(solver->ab){
				Board next = board;
				next.move(*move);

				pd = 0;
				outcome = (solver->ab == 1 ? solve1ply(next, pd) : solve2ply(next, pd));
				PLUS(solver->nodes_seen, pd);
			}else{
				outcome = board.test_win(*move);
				pd = 1;
			}

			if(solver->lbdist && outcome < 0)
				pd = dists.get(*move);

			temp[i] = PNSNode(*move).outcome(outcome, board.toplay(), solver->ties, pd);

			i++;
		}
		temp.shrink(i); //if symmetry, there may be extra moves to ignore
		node->children.swap(temp);
		assert(temp.unlock());

		PLUS(solver->nodes_seen, i);

		updatePDnum(node);

		return true;
	}

	bool mem;
	do{
		PNSNode * child = node->children.begin(),
		        * child2 = node->children.begin(),
		        * childend = node->children.end();

		uint32_t tpc, tdc;

		if(solver->df){
			for(PNSNode * i = node->children.begin(); i != childend; i++){
				if(i->refdelta() <= child->refdelta()){
					child2 = child;
					child = i;
				}else if(i->refdelta() < child2->refdelta()){
					child2 = i;
				}
			}

			tpc = min(INF32/2, (td + child->phi - node->delta));
			tdc = min(tp, (uint32_t)(child2->delta*(1.0 + solver->epsilon) + 1));
		}else{
			tpc = tdc = 0;
			for(PNSNode * i = node->children.begin(); i != childend; i++)
				if(child->refdelta() > i->refdelta())
					child = i;
		}

		Board next = board;
		next.move(child->move);

		child->ref();
		uint64_t itersbefore = iters;
		mem = pns(next, child, depth + 1, tpc, tdc);
		child->deref();
		PLUS(child->work, iters - itersbefore);

		if(updatePDnum(node) && !solver->df)
			break;

	}while(!solver->timeout && mem && (!solver->df || (node->phi < tp && node->delta < td)));

	return mem;
}
Esempio n. 9
0
#define X() 1
#define PLUS() +
#define MINUS() -
#define DOT() .
#define GREATER() >
#define LESS() <

//R #line 23 "t_9_003.cpp"
X()2                          //R 1 2 
STRINGIZE( X()2 )             //R "12" 
//R 
X() 2                         //R 1 2 
STRINGIZE( X() 2 )            //R "1 2" 
//R 
PLUS()MINUS()                 //R +- 
STRINGIZE( PLUS()MINUS() )    //R "+-" 
//R 
PLUS()PLUS()                  //R + + 
STRINGIZE( PLUS()PLUS() )     //R "++" 
//R 
MINUS()MINUS()                //R - - 
STRINGIZE( MINUS()MINUS() )   //R "--" 
//R 
DOT()DOT()DOT()               //R .. . 
STRINGIZE( DOT()DOT()DOT() )  //R "..." 

// the following are regressions reported by Stefan Seefeld
//R #line 43 "t_9_003.cpp"
GREATER()GREATER()            //R > > 
STRINGIZE( GREATER()GREATER() ) //R ">>" 
//R
Esempio n. 10
0
/* SIMD format-compatible scalar code. This should be replaced by
   more efficient code, but so far this is a proof of concept. */
static void salsa20_wordtobyte_tr(u8 output[64],const u32 input[16])
{
  u32 x[16];
  int i;

  for (i = 0;i < 16;++i) x[tr[i]] = input[tr[i]];
  for (i = 20;i > 0;i -= 2) {
    x[tr[ 4]] = XOR(x[tr[ 4]],ROTATE(PLUS(x[tr[ 0]],x[tr[12]]), 7));
    x[tr[ 8]] = XOR(x[tr[ 8]],ROTATE(PLUS(x[tr[ 4]],x[tr[ 0]]), 9));
    x[tr[12]] = XOR(x[tr[12]],ROTATE(PLUS(x[tr[ 8]],x[tr[ 4]]),13));
    x[tr[ 0]] = XOR(x[tr[ 0]],ROTATE(PLUS(x[tr[12]],x[tr[ 8]]),18));
    x[tr[ 9]] = XOR(x[tr[ 9]],ROTATE(PLUS(x[tr[ 5]],x[tr[ 1]]), 7));
    x[tr[13]] = XOR(x[tr[13]],ROTATE(PLUS(x[tr[ 9]],x[tr[ 5]]), 9));
    x[tr[ 1]] = XOR(x[tr[ 1]],ROTATE(PLUS(x[tr[13]],x[tr[ 9]]),13));
    x[tr[ 5]] = XOR(x[tr[ 5]],ROTATE(PLUS(x[tr[ 1]],x[tr[13]]),18));
    x[tr[14]] = XOR(x[tr[14]],ROTATE(PLUS(x[tr[10]],x[tr[ 6]]), 7));
    x[tr[ 2]] = XOR(x[tr[ 2]],ROTATE(PLUS(x[tr[14]],x[tr[10]]), 9));
    x[tr[ 6]] = XOR(x[tr[ 6]],ROTATE(PLUS(x[tr[ 2]],x[tr[14]]),13));
    x[tr[10]] = XOR(x[tr[10]],ROTATE(PLUS(x[tr[ 6]],x[tr[ 2]]),18));
    x[tr[ 3]] = XOR(x[tr[ 3]],ROTATE(PLUS(x[tr[15]],x[tr[11]]), 7));
    x[tr[ 7]] = XOR(x[tr[ 7]],ROTATE(PLUS(x[tr[ 3]],x[tr[15]]), 9));
    x[tr[11]] = XOR(x[tr[11]],ROTATE(PLUS(x[tr[ 7]],x[tr[ 3]]),13));
    x[tr[15]] = XOR(x[tr[15]],ROTATE(PLUS(x[tr[11]],x[tr[ 7]]),18));
    x[tr[ 1]] = XOR(x[tr[ 1]],ROTATE(PLUS(x[tr[ 0]],x[tr[ 3]]), 7));
    x[tr[ 2]] = XOR(x[tr[ 2]],ROTATE(PLUS(x[tr[ 1]],x[tr[ 0]]), 9));
    x[tr[ 3]] = XOR(x[tr[ 3]],ROTATE(PLUS(x[tr[ 2]],x[tr[ 1]]),13));
    x[tr[ 0]] = XOR(x[tr[ 0]],ROTATE(PLUS(x[tr[ 3]],x[tr[ 2]]),18));
    x[tr[ 6]] = XOR(x[tr[ 6]],ROTATE(PLUS(x[tr[ 5]],x[tr[ 4]]), 7));
    x[tr[ 7]] = XOR(x[tr[ 7]],ROTATE(PLUS(x[tr[ 6]],x[tr[ 5]]), 9));
    x[tr[ 4]] = XOR(x[tr[ 4]],ROTATE(PLUS(x[tr[ 7]],x[tr[ 6]]),13));
    x[tr[ 5]] = XOR(x[tr[ 5]],ROTATE(PLUS(x[tr[ 4]],x[tr[ 7]]),18));
    x[tr[11]] = XOR(x[tr[11]],ROTATE(PLUS(x[tr[10]],x[tr[ 9]]), 7));
    x[tr[ 8]] = XOR(x[tr[ 8]],ROTATE(PLUS(x[tr[11]],x[tr[10]]), 9));
    x[tr[ 9]] = XOR(x[tr[ 9]],ROTATE(PLUS(x[tr[ 8]],x[tr[11]]),13));
    x[tr[10]] = XOR(x[tr[10]],ROTATE(PLUS(x[tr[ 9]],x[tr[ 8]]),18));
    x[tr[12]] = XOR(x[tr[12]],ROTATE(PLUS(x[tr[15]],x[tr[14]]), 7));
    x[tr[13]] = XOR(x[tr[13]],ROTATE(PLUS(x[tr[12]],x[tr[15]]), 9));
    x[tr[14]] = XOR(x[tr[14]],ROTATE(PLUS(x[tr[13]],x[tr[12]]),13));
    x[tr[15]] = XOR(x[tr[15]],ROTATE(PLUS(x[tr[14]],x[tr[13]]),18));
  }
  for (i = 0;i < 16;++i) x[tr[i]] = PLUS(x[tr[i]],input[tr[i]]);
  for (i = 0;i < 16;++i) U32TO8_LITTLE(output + 4 * i,x[tr[i]]);
}
Esempio n. 11
0
void ECRYPT_encrypt_bytes(ECRYPT_ctx *x,const u8 *m,u8 *c,u32 bytes)
{
  u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
  u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
  u8 *ctarget;
  u8 tmp[64];
  int i;

  if (!bytes) return;

  j0 = x->input[0];
  j1 = x->input[1];
  j2 = x->input[2];
  j3 = x->input[3];
  j4 = x->input[4];
  j5 = x->input[5];
  j6 = x->input[6];
  j7 = x->input[7];
  j8 = x->input[8];
  j9 = x->input[9];
  j10 = x->input[10];
  j11 = x->input[11];
  j12 = x->input[12];
  j13 = x->input[13];
  j14 = x->input[14];
  j15 = x->input[15];

  for (;;) {
    if (bytes < 64) {
      for (i = 0;i < bytes;++i) tmp[i] = m[i];
      m = tmp;
      ctarget = c;
      c = tmp;
    }
    x0 = j0;
    x1 = j1;
    x2 = j2;
    x3 = j3;
    x4 = j4;
    x5 = j5;
    x6 = j6;
    x7 = j7;
    x8 = j8;
    x9 = j9;
    x10 = j10;
    x11 = j11;
    x12 = j12;
    x13 = j13;
    x14 = j14;
    x15 = j15;
    for (i = 8;i > 0;i -= 2) {
       x4 = XOR( x4,ROTATE(PLUS( x0,x12), 7));
       x8 = XOR( x8,ROTATE(PLUS( x4, x0), 9));
      x12 = XOR(x12,ROTATE(PLUS( x8, x4),13));
       x0 = XOR( x0,ROTATE(PLUS(x12, x8),18));
       x9 = XOR( x9,ROTATE(PLUS( x5, x1), 7));
      x13 = XOR(x13,ROTATE(PLUS( x9, x5), 9));
       x1 = XOR( x1,ROTATE(PLUS(x13, x9),13));
       x5 = XOR( x5,ROTATE(PLUS( x1,x13),18));
      x14 = XOR(x14,ROTATE(PLUS(x10, x6), 7));
       x2 = XOR( x2,ROTATE(PLUS(x14,x10), 9));
       x6 = XOR( x6,ROTATE(PLUS( x2,x14),13));
      x10 = XOR(x10,ROTATE(PLUS( x6, x2),18));
       x3 = XOR( x3,ROTATE(PLUS(x15,x11), 7));
       x7 = XOR( x7,ROTATE(PLUS( x3,x15), 9));
      x11 = XOR(x11,ROTATE(PLUS( x7, x3),13));
      x15 = XOR(x15,ROTATE(PLUS(x11, x7),18));
       x1 = XOR( x1,ROTATE(PLUS( x0, x3), 7));
       x2 = XOR( x2,ROTATE(PLUS( x1, x0), 9));
       x3 = XOR( x3,ROTATE(PLUS( x2, x1),13));
       x0 = XOR( x0,ROTATE(PLUS( x3, x2),18));
       x6 = XOR( x6,ROTATE(PLUS( x5, x4), 7));
       x7 = XOR( x7,ROTATE(PLUS( x6, x5), 9));
       x4 = XOR( x4,ROTATE(PLUS( x7, x6),13));
       x5 = XOR( x5,ROTATE(PLUS( x4, x7),18));
      x11 = XOR(x11,ROTATE(PLUS(x10, x9), 7));
       x8 = XOR( x8,ROTATE(PLUS(x11,x10), 9));
       x9 = XOR( x9,ROTATE(PLUS( x8,x11),13));
      x10 = XOR(x10,ROTATE(PLUS( x9, x8),18));
      x12 = XOR(x12,ROTATE(PLUS(x15,x14), 7));
      x13 = XOR(x13,ROTATE(PLUS(x12,x15), 9));
      x14 = XOR(x14,ROTATE(PLUS(x13,x12),13));
      x15 = XOR(x15,ROTATE(PLUS(x14,x13),18));
    }
    x0 = PLUS(x0,j0);
    x1 = PLUS(x1,j1);
    x2 = PLUS(x2,j2);
    x3 = PLUS(x3,j3);
    x4 = PLUS(x4,j4);
    x5 = PLUS(x5,j5);
    x6 = PLUS(x6,j6);
    x7 = PLUS(x7,j7);
    x8 = PLUS(x8,j8);
    x9 = PLUS(x9,j9);
    x10 = PLUS(x10,j10);
    x11 = PLUS(x11,j11);
    x12 = PLUS(x12,j12);
    x13 = PLUS(x13,j13);
    x14 = PLUS(x14,j14);
    x15 = PLUS(x15,j15);

    x0 = XOR(x0,U8TO32_LITTLE(m + 0));
    x1 = XOR(x1,U8TO32_LITTLE(m + 4));
    x2 = XOR(x2,U8TO32_LITTLE(m + 8));
    x3 = XOR(x3,U8TO32_LITTLE(m + 12));
    x4 = XOR(x4,U8TO32_LITTLE(m + 16));
    x5 = XOR(x5,U8TO32_LITTLE(m + 20));
    x6 = XOR(x6,U8TO32_LITTLE(m + 24));
    x7 = XOR(x7,U8TO32_LITTLE(m + 28));
    x8 = XOR(x8,U8TO32_LITTLE(m + 32));
    x9 = XOR(x9,U8TO32_LITTLE(m + 36));
    x10 = XOR(x10,U8TO32_LITTLE(m + 40));
    x11 = XOR(x11,U8TO32_LITTLE(m + 44));
    x12 = XOR(x12,U8TO32_LITTLE(m + 48));
    x13 = XOR(x13,U8TO32_LITTLE(m + 52));
    x14 = XOR(x14,U8TO32_LITTLE(m + 56));
    x15 = XOR(x15,U8TO32_LITTLE(m + 60));

    j8 = PLUSONE(j8);
    if (!j8) {
      j9 = PLUSONE(j9);
      /* stopping at 2^70 bytes per nonce is user's responsibility */
    }

    U32TO8_LITTLE(c + 0,x0);
    U32TO8_LITTLE(c + 4,x1);
    U32TO8_LITTLE(c + 8,x2);
    U32TO8_LITTLE(c + 12,x3);
    U32TO8_LITTLE(c + 16,x4);
    U32TO8_LITTLE(c + 20,x5);
    U32TO8_LITTLE(c + 24,x6);
    U32TO8_LITTLE(c + 28,x7);
    U32TO8_LITTLE(c + 32,x8);
    U32TO8_LITTLE(c + 36,x9);
    U32TO8_LITTLE(c + 40,x10);
    U32TO8_LITTLE(c + 44,x11);
    U32TO8_LITTLE(c + 48,x12);
    U32TO8_LITTLE(c + 52,x13);
    U32TO8_LITTLE(c + 56,x14);
    U32TO8_LITTLE(c + 60,x15);

    if (bytes <= 64) {
      if (bytes < 64) {
        for (i = 0;i < bytes;++i) ctarget[i] = c[i];
      }
      x->input[8] = j8;
      x->input[9] = j9;
      return;
    }
    bytes -= 64;
    c += 64;
    m += 64;
  }
}
Esempio n. 12
0
File: salsa20.c Progetto: adh/dfsch
void dfsch_salsa20_get_keystream_block(dfsch_salsa20_state_t* st,
                                       uint8_t output[64]){
  uint32_t x[16];
  int i;

  for (i = 0;i < 16;++i) x[i] = st->input[i];
  for (i = 20;i > 0;i -= 2) {
    x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 0],x[12]), 7));
    x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[ 4],x[ 0]), 9));
    x[12] = XOR(x[12],ROTATE(PLUS(x[ 8],x[ 4]),13));
    x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[12],x[ 8]),18));
    x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 5],x[ 1]), 7));
    x[13] = XOR(x[13],ROTATE(PLUS(x[ 9],x[ 5]), 9));
    x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[13],x[ 9]),13));
    x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 1],x[13]),18));
    x[14] = XOR(x[14],ROTATE(PLUS(x[10],x[ 6]), 7));
    x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[14],x[10]), 9));
    x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 2],x[14]),13));
    x[10] = XOR(x[10],ROTATE(PLUS(x[ 6],x[ 2]),18));
    x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[15],x[11]), 7));
    x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 3],x[15]), 9));
    x[11] = XOR(x[11],ROTATE(PLUS(x[ 7],x[ 3]),13));
    x[15] = XOR(x[15],ROTATE(PLUS(x[11],x[ 7]),18));
    x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[ 0],x[ 3]), 7));
    x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[ 1],x[ 0]), 9));
    x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[ 2],x[ 1]),13));
    x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[ 3],x[ 2]),18));
    x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 5],x[ 4]), 7));
    x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 6],x[ 5]), 9));
    x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 7],x[ 6]),13));
    x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 4],x[ 7]),18));
    x[11] = XOR(x[11],ROTATE(PLUS(x[10],x[ 9]), 7));
    x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[11],x[10]), 9));
    x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 8],x[11]),13));
    x[10] = XOR(x[10],ROTATE(PLUS(x[ 9],x[ 8]),18));
    x[12] = XOR(x[12],ROTATE(PLUS(x[15],x[14]), 7));
    x[13] = XOR(x[13],ROTATE(PLUS(x[12],x[15]), 9));
    x[14] = XOR(x[14],ROTATE(PLUS(x[13],x[12]),13));
    x[15] = XOR(x[15],ROTATE(PLUS(x[14],x[13]),18));
  }
  for (i = 0;i < 16;++i) x[i] = PLUS(x[i],st->input[i]);
  for (i = 0;i < 16;++i) U32TO8_LITTLE(output + 4 * i,x[i]);
  st->input[8]++;
  if (!st->input[8]){
    st->input[9]++;
  }
}
Esempio n. 13
0
bool Player::PlayerUCT::create_children(Board & board, Node * node, int toplay){
	if(!node->children.lock())
		return false;

	if(player->dists || player->detectdraw){
		dists.run(&board, (player->dists > 0), (player->detectdraw ? 0 : toplay));

		if(player->detectdraw){
//			assert(node->outcome == -3);
			node->outcome = dists.isdraw(); //could be winnable by only one side

			if(node->outcome == 0){ //proven draw, neither side can influence the outcome
				node->bestmove = *(board.moveit()); //just choose the first move since all are equal at this point
				node->children.unlock();
				return true;
			}
		}
	}

	CompactTree<Node>::Children temp;
	temp.alloc(board.movesremain(), player->ctmem);

	int losses = 0;

	Node * child = temp.begin(),
	     * end   = temp.end(),
	     * loss  = NULL;
	Board::MoveIterator move = board.moveit(player->prunesymmetry);
	int nummoves = 0;
	for(; !move.done() && child != end; ++move, ++child){
		*child = Node(*move);

		if(player->minimax){
			child->outcome = board.test_win(*move);

			if(player->minimax >= 2 && board.test_win(*move, 3 - board.toplay()) > 0){
				losses++;
				loss = child;
			}

			if(child->outcome == toplay){ //proven win from here, don't need children
				node->outcome = child->outcome;
				node->proofdepth = 1;
				node->bestmove = *move;
				node->children.unlock();
				temp.dealloc(player->ctmem);
				return true;
			}
		}

		if(player->knowledge)
			add_knowledge(board, node, child);
		nummoves++;
	}

	if(player->prunesymmetry)
		temp.shrink(nummoves); //shrink the node to ignore the extra moves
	else //both end conditions should happen in parallel
		assert(move.done() && child == end);

	//Make a macro move, add experience to the move so the current simulation continues past this move
	if(losses == 1){
		Node macro = *loss;
		temp.dealloc(player->ctmem);
		temp.alloc(1, player->ctmem);
		macro.exp.addwins(player->visitexpand);
		*(temp.begin()) = macro;
	}else if(losses >= 2){ //proven loss, but at least try to block one of them
		node->outcome = 3 - toplay;
		node->proofdepth = 2;
		node->bestmove = loss->move;
		node->children.unlock();
		temp.dealloc(player->ctmem);
		return true;
	}

	if(player->dynwiden > 0) //sort in decreasing order by knowledge
		sort(temp.begin(), temp.end(), sort_node_know);

	PLUS(player->nodes, temp.num());
	node->children.swap(temp);
	assert(temp.unlock());

	return true;
}
Esempio n. 14
0
static void
_salsa20_block(int rounds, uint32_t *input, uint8_t *output)
{
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
    uint32_t x8, x9, x10, x11, x12, x13, x14, x15;
    uint8_t  i;

    x0  = input[0];
    x1  = input[1];
    x2  = input[2];
    x3  = input[3];
    x4  = input[4];
    x5  = input[5];
    x6  = input[6];
    x7  = input[7];
    x8  = input[8];
    x9  = input[9];
    x10 = input[10];
    x11 = input[11];
    x12 = input[12];
    x13 = input[13];
    x14 = input[14];
    x15 = input[15];
    
    for (i = rounds; i > 0; i -= 2) {
        /* Column round */
        x4  = XOR ( x4, ROTATE (PLUS ( x0,x12), 7));
        x8  = XOR ( x8, ROTATE (PLUS ( x4, x0), 9));
        x12 = XOR (x12, ROTATE (PLUS ( x8, x4),13));
        x0  = XOR ( x0, ROTATE (PLUS (x12, x8),18));
        x9  = XOR ( x9, ROTATE (PLUS ( x5, x1), 7));
        x13 = XOR (x13, ROTATE (PLUS ( x9, x5), 9));
        x1  = XOR ( x1, ROTATE (PLUS (x13, x9),13));
        x5  = XOR ( x5, ROTATE (PLUS ( x1,x13),18));
        x14 = XOR (x14, ROTATE (PLUS (x10, x6), 7));
        x2  = XOR ( x2, ROTATE (PLUS (x14,x10), 9));
        x6  = XOR ( x6, ROTATE (PLUS ( x2,x14),13));
        x10 = XOR (x10, ROTATE (PLUS ( x6, x2),18));
        x3  = XOR ( x3, ROTATE (PLUS (x15,x11), 7));
        x7  = XOR ( x7, ROTATE (PLUS ( x3,x15), 9));
        x11 = XOR (x11, ROTATE (PLUS ( x7, x3),13));
        x15 = XOR (x15, ROTATE (PLUS (x11, x7),18));
        
        /* Row round */
        x1  = XOR ( x1, ROTATE (PLUS ( x0, x3), 7));
        x2  = XOR ( x2, ROTATE (PLUS ( x1, x0), 9));
        x3  = XOR ( x3, ROTATE (PLUS ( x2, x1),13));
        x0  = XOR ( x0, ROTATE (PLUS ( x3, x2),18));
        x6  = XOR ( x6, ROTATE (PLUS ( x5, x4), 7));
        x7  = XOR ( x7, ROTATE (PLUS ( x6, x5), 9));
        x4  = XOR ( x4, ROTATE (PLUS ( x7, x6),13));
        x5  = XOR ( x5, ROTATE (PLUS ( x4, x7),18));
        x11 = XOR (x11, ROTATE (PLUS (x10, x9), 7));
        x8  = XOR ( x8, ROTATE (PLUS (x11,x10), 9));
        x9  = XOR ( x9, ROTATE (PLUS ( x8,x11),13));
        x10 = XOR (x10, ROTATE (PLUS ( x9, x8),18));
        x12 = XOR (x12, ROTATE (PLUS (x15,x14), 7));
        x13 = XOR (x13, ROTATE (PLUS (x12,x15), 9));
        x14 = XOR (x14, ROTATE (PLUS (x13,x12),13));
        x15 = XOR (x15, ROTATE (PLUS (x14,x13),18));
    }
    
    x0  = PLUS (x0, input[0]);
    x1  = PLUS (x1, input[1]);
    x2  = PLUS (x2, input[2]);
    x3  = PLUS (x3, input[3]);
    x4  = PLUS (x4, input[4]);
    x5  = PLUS (x5, input[5]);
    x6  = PLUS (x6, input[6]);
    x7  = PLUS (x7, input[7]);
    x8  = PLUS (x8, input[8]);
    x9  = PLUS (x9, input[9]);
    x10 = PLUS (x10, input[10]);
    x11 = PLUS (x11, input[11]);
    x12 = PLUS (x12, input[12]);
    x13 = PLUS (x13, input[13]);
    x14 = PLUS (x14, input[14]);
    x15 = PLUS (x15, input[15]);
    
    U32TO8_LITTLE (output + 0, x0);
    U32TO8_LITTLE (output + 4, x1);
    U32TO8_LITTLE (output + 8, x2);
    U32TO8_LITTLE (output + 12, x3);
    U32TO8_LITTLE (output + 16, x4);
    U32TO8_LITTLE (output + 20, x5);
    U32TO8_LITTLE (output + 24, x6);
    U32TO8_LITTLE (output + 28, x7);
    U32TO8_LITTLE (output + 32, x8);
    U32TO8_LITTLE (output + 36, x9);
    U32TO8_LITTLE (output + 40, x10);
    U32TO8_LITTLE (output + 44, x11);
    U32TO8_LITTLE (output + 48, x12);
    U32TO8_LITTLE (output + 52, x13);
    U32TO8_LITTLE (output + 56, x14);
    U32TO8_LITTLE (output + 60, x15);
    
    /* Increment block counter */
    input[8] = PLUSONE (input[8]);
    if (!input[8]) {
        input[9] = PLUSONE (input[9]);
        /* stopping at 2^70 bytes per nonce is user's responsibility */
    }
}