Beispiel #1
0
void SteeringDataTlv::ComputeBloomFilter(const otExtAddress &aJoinerId)
{
    Crc16 ccitt(Crc16::kCcitt);
    Crc16 ansi(Crc16::kAnsi);

    for (size_t j = 0; j < sizeof(otExtAddress); j++)
    {
        uint8_t byte = aJoinerId.m8[j];
        ccitt.Update(byte);
        ansi.Update(byte);
    }

    SetBit(ccitt.Get() % GetNumBits());
    SetBit(ansi.Get() % GetNumBits());
}
Beispiel #2
0
M4Err BM_ParseRouteInsert(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	M4Err e;
	u8 flag;
	SGCommand *com;
	SFNode *InNode, *OutNode;
	u32 RouteID, outField, inField, numBits, ind, node_id;
	char name[1000];

	RouteID = 0;

	flag = BS_ReadInt(bs, 1);
	/*def'ed route*/
	if (flag) {
		RouteID = 1 + BS_ReadInt(bs, codec->info->config.RouteIDBits);
		if (codec->info->UseName) BD_GetName(bs, name);
	}
	/*origin*/
	node_id = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	OutNode = BIFS_FindNode(codec, node_id);
	if (!OutNode) return M4InvalidNode;

	numBits = Node_GetNumFields(OutNode, FCM_OUT) - 1;
	numBits = GetNumBits(numBits);
	ind = BS_ReadInt(bs, numBits);
	e = Node_GetFieldIndex(OutNode, ind, FCM_OUT, &outField);

	/*target*/
	node_id = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	InNode = BIFS_FindNode(codec, node_id);
	if (!InNode) return M4InvalidNode;

	numBits = Node_GetNumFields(InNode, FCM_IN) - 1;
	numBits = GetNumBits(numBits);
	ind = BS_ReadInt(bs, numBits);
	e = Node_GetFieldIndex(InNode, ind, FCM_IN, &inField);
	if (e) return e;

	com = SG_NewCommand(codec->current_graph, SG_RouteInsert);
	com->RouteID = RouteID;
	if (codec->info->UseName) com->def_name = strdup( name);
	com->fromNodeID = Node_GetID(OutNode);
	com->fromFieldIndex = outField;
	com->toNodeID = Node_GetID(InNode);
	com->toFieldIndex = inField;
	ChainAddEntry(com_list, com);
	return codec->LastError;
}
Beispiel #3
0
u32 RunHTKDec(void *par)
{
	BitStream *bs;
	char *szWord;
	s32 word_index;
	u32 len, val, i;
	Float word_score;
	SLHeader slh;
	unsigned char *buf;
	u32 buf_size;


	ISPriv *is_dec = (ISPriv *)par;
//	while (is_dec->htk_running)

	HTK_DoDetection();
	szWord = HTK_GetWord();
	word_index = HTK_GetWordIndex();
	word_score = HTK_GetWordScore();

	fprintf(stdout, "HTK result: %s %d %f\n", szWord, word_index, word_score);

	bs = NewBitStream(NULL, 0, BS_WRITE);
	
	/*HTK sensor buffer format: SFString - SFInt32 - SFFloat*/
	BS_WriteInt(bs, 1, 1); 
	len = strlen(szWord);
	val = GetNumBits(len);
	BS_WriteInt(bs, val, 5);
	BS_WriteInt(bs, len, val);
	for (i=0; i<len; i++) BS_WriteInt(bs, szWord[i], 8);

	BS_WriteInt(bs, 1, 1); 
	BS_WriteInt(bs, word_index, 32);
	BS_WriteInt(bs, 1, 1); 
	BS_WriteFloat(bs, word_score);

	BS_Align(bs);
	BS_GetContent(bs, &buf, &buf_size);
	DeleteBitStream(bs);

	memset(&slh, 0, sizeof(SLHeader));
	slh.accessUnitStartFlag = slh.accessUnitEndFlag = 1;
	slh.compositionTimeStamp = 0;

	/*get all IS keySensor decoders and send frame*/
	for (i=0; i<ChainGetCount(is_dec->scene->root_od->term->input_streams); i++) {
		GenericCodec *cod = ChainGetEntry(is_dec->scene->root_od->term->input_streams, i);
		ISPriv *is = cod->decio->privateStack;
		if (is != is_dec) continue;
		if (is->type==IS_HTKSensor) {
			Channel *ch = ChainGetEntry(cod->inChannels, 0);
			Channel_RecieveSLP(ch->service, ch, buf, buf_size, &slh, M4OK);
		}
	}
	free(buf);

	is_dec->htk_running = 0;
	return 0;
}
Beispiel #4
0
M4Err BM_ParseIndexValueReplace(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	u32 NodeID, ind, field_ind, NumBits;
	s32 type, pos;
	SGCommand *com;
	SFNode *node;
	M4Err e;
	FieldInfo field, sffield;
	CommandFieldInfo *inf;

	/*get the node*/
	NodeID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);

	node = BIFS_FindNode(codec, NodeID);
	if (!node) return M4NonCompliantBitStream;
	NumBits = GetNumBits(Node_GetNumFields(node, FCM_IN)-1);
	ind = BS_ReadInt(bs, NumBits);
	e = Node_GetFieldIndex(node, ind, FCM_IN, &field_ind);
	if (e) return e;

	e = Node_GetField(node, field_ind, &field);
	if (VRML_IsSFField(field.fieldType)) return M4NonCompliantBitStream;

	type = BS_ReadInt(bs, 2);
	switch (type) {
	case 0:
		pos = BS_ReadInt(bs, 16);
		break;
	case 2:
		pos = 0;
		break;
	case 3:
		pos = ((GenMFField *) field.far_ptr)->count - 1;
		break;
	default:
		return M4NonCompliantBitStream;
	}
	
	com = SG_NewCommand(codec->current_graph, SG_IndexedReplace);
	BM_SetCommandNode(com, node);
	inf = SG_NewFieldCommand(com);
	inf->fieldIndex = field.fieldIndex;
	inf->pos = pos;

	if (field.fieldType == FT_MFNode) {
		inf->fieldType = FT_SFNode;
		inf->new_node = BD_DecSFNode(codec, bs, field.NDTtype);
		inf->field_ptr = &inf->new_node;
		Node_Register(inf->new_node, com->node);
	} else {
		memcpy(&sffield, &field, sizeof(FieldInfo));
		sffield.fieldType = VRML_GetSFType(field.fieldType);
		inf->fieldType = sffield.fieldType;
		sffield.far_ptr = inf->field_ptr = VRML_NewFieldPointer(sffield.fieldType);
		codec->LastError = BD_DecSFField(codec, bs, node, &sffield);
	}
	ChainAddEntry(com_list, com);
	return codec->LastError;
}
Beispiel #5
0
u32 BM_ParseRouteReplace(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	M4Err e;
	SGCommand *com;
	u32 RouteID, numBits, ind, node_id, fromID, toID;
	LPROUTE r;
	SFNode *OutNode, *InNode;

	RouteID = 1+BS_ReadInt(bs, codec->info->config.RouteIDBits);
	
	r = SG_FindRoute(codec->current_graph, RouteID);

	/*origin*/
	node_id = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	OutNode = BIFS_FindNode(codec, node_id);
	if (!OutNode) return M4NonCompliantBitStream;
	numBits = GetNumBits(Node_GetNumFields(OutNode, FCM_OUT) - 1);
	ind = BS_ReadInt(bs, numBits);
	e = Node_GetFieldIndex(OutNode, ind, FCM_OUT, &fromID);
	if (e) return e;

	/*target*/
	node_id = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	InNode = BIFS_FindNode(codec, node_id);
	if (!InNode) return M4NonCompliantBitStream;
	numBits = GetNumBits(Node_GetNumFields(InNode, FCM_IN) - 1);
	ind = BS_ReadInt(bs, numBits);
	e = Node_GetFieldIndex(InNode, ind, FCM_IN, &toID);
	if (e) return e;

	com = SG_NewCommand(codec->current_graph, SG_RouteReplace);
	com->RouteID = RouteID;
	com->fromNodeID = Node_GetID(OutNode);
	com->fromFieldIndex = fromID;
	com->toNodeID = Node_GetID(InNode);
	com->toFieldIndex = toID;
	ChainAddEntry(com_list, com);
	return codec->LastError;
}
Beispiel #6
0
static M4Err BM_ParseMultipleIndexedReplace(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	u32 ID, ind, field_ind, NumBits, lenpos, lennum, count;
	SFNode *node;
	M4Err e;
	SGCommand *com;
	CommandFieldInfo *inf;
	FieldInfo field;
	
	ID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	node = SG_FindNode(codec->current_graph, ID);
	if (!node) return M4NonCompliantBitStream;
	NumBits = GetNumBits(Node_GetNumFields(node, FCM_IN)-1);
	ind = BS_ReadInt(bs, NumBits);
	e = Node_GetFieldIndex(node, ind, FCM_IN, &field_ind);
	if (e) return e;
	e = Node_GetField(node, field_ind, &field);
	if (VRML_IsSFField(field.fieldType)) return M4NonCompliantBitStream;

	lenpos = BS_ReadInt(bs, 5);
	lennum = BS_ReadInt(bs, 5);
	count = BS_ReadInt(bs, lennum);
	
	com = SG_NewCommand(codec->current_graph, SG_MultipleIndexedReplace);
	BM_SetCommandNode(com, node);
	field.fieldType = VRML_GetSFType(field.fieldType);

	while (count) {
		inf = SG_NewFieldCommand(com);
		inf->pos = BS_ReadInt(bs, lenpos);
		inf->fieldIndex = field.fieldIndex;
		inf->fieldType = field.fieldType;
		
		if (field.fieldType==FT_SFNode) {
			inf->new_node = BD_DecSFNode(codec, bs, field.NDTtype);
			if (codec->LastError) goto err;
			inf->field_ptr = &inf->new_node;
			Node_Register(inf->new_node, node);
		} else {
			field.far_ptr = inf->field_ptr = VRML_NewFieldPointer(inf->fieldType);
			e = BD_DecSFField(codec, bs, node, &field);
			if (e) goto err;
		}
		count--;
	}
err:
	if (e) SG_DeleteCommand(com);
	else ChainAddEntry(com_list, com);
	return e;
}
Beispiel #7
0
M4Err BM_ParseFieldReplace(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	M4Err e;
	SGCommand *com;
	u32 NodeID, ind, field_ind, NumBits;
	SFNode *node;
	FieldInfo field;
	CommandFieldInfo *inf;

	NodeID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	node = BIFS_FindNode(codec, NodeID);
	if (!node) return M4NonCompliantBitStream;
	NumBits = GetNumBits(Node_GetNumFields(node, FCM_IN)-1);
	ind = BS_ReadInt(bs, NumBits);
	e = Node_GetFieldIndex(node, ind, FCM_IN, &field_ind);
	if (e) return e;

	e = Node_GetField(node, field_ind, &field);

	com = SG_NewCommand(codec->current_graph, SG_FieldReplace);
	BM_SetCommandNode(com, node);
	inf = SG_NewFieldCommand(com);
	inf->fieldIndex = field_ind;
	inf->fieldType = field.fieldType;
	if (inf->fieldType == FT_SFNode) {
		field.far_ptr = inf->field_ptr = &inf->new_node;
	} else if (inf->fieldType == FT_MFNode) {
		inf->node_list = NewChain();
		field.far_ptr = inf->field_ptr = &inf->node_list;
	} else {
		field.far_ptr = inf->field_ptr = VRML_NewFieldPointer(field.fieldType);
	}
	/*parse the field*/
	codec->LastError = BD_DecField(codec, bs, node, &field);

	/*register nodes*/
	if (inf->fieldType == FT_SFNode) {
		Node_Register(inf->new_node, com->node);
	} else if (inf->fieldType == FT_MFNode) {
		u32 i;
		for (i=0; i<ChainGetCount(inf->node_list); i++) {
			SFNode *p = ChainGetEntry(inf->node_list, i);
			Node_Register(p, com->node);
		}
	}

	ChainAddEntry(com_list, com);
	return codec->LastError;
}
Beispiel #8
0
M4Err BM_ParseIndexDelete(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	u32 NodeID, NumBits, ind, field_ind;
	s32 pos;
	SGCommand *com;
	u8 type;
	SFNode *node;
	M4Err e;
	CommandFieldInfo *inf;
	FieldInfo field;
	
	NodeID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	node = BIFS_FindNode(codec, NodeID);
	if (!node) return M4NonCompliantBitStream;

	NumBits = GetNumBits(Node_GetNumFields(node, FCM_IN) - 1);
	ind = BS_ReadInt(bs, NumBits);

	type = BS_ReadInt(bs, 2);
	switch (type) {
	case 0:
		pos = (u32) BS_ReadInt(bs, 16);
		break;
	case 2:
		pos = 0;
		break;
	case 3:
		pos = -1;
		break;
	default:
		return M4NonCompliantBitStream;
	}
	e = Node_GetFieldIndex(node, ind, FCM_IN, &field_ind);
	if (e) return e;
	e = Node_GetField(node, field_ind, &field);
	if (e) return e;
	if (VRML_IsSFField(field.fieldType)) return M4NonCompliantBitStream;
	com = SG_NewCommand(codec->current_graph, SG_IndexedDelete);
	BM_SetCommandNode(com, node);
	inf = SG_NewFieldCommand(com);
	inf->pos = pos;
	inf->fieldIndex = field.fieldIndex;
	inf->fieldType = VRML_GetSFType(field.fieldType);
	ChainAddEntry(com_list, com);
	return codec->LastError;
}
Beispiel #9
0
static M4Err BM_ParseMultipleReplace(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	u32 i, numFields, index, flag, nbBits, field_ref, fieldind;
	M4Err e;
	FieldInfo field;
	u32 NodeID;
	SFNode *node;
	SGCommand *com;
	CommandFieldInfo *inf;

	NodeID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	node = BIFS_FindNode(codec, NodeID);
	if (!node) return M4NonCompliantBitStream;
	
	e = M4OK;
	com = SG_NewCommand(codec->current_graph, SG_MultipleReplace);
	BM_SetCommandNode(com, node);
	flag = BS_ReadInt(bs, 1);
	if (flag) {
		numFields = Node_GetNumFields(node, FCM_DEF);
		for (i=0; i<numFields; i++) {
			flag = BS_ReadInt(bs, 1);
			if (!flag) continue;
			Node_GetFieldIndex(node, i, FCM_DEF, &index);
			e = Node_GetField(node, index, &field);
			if (e) goto exit;
			inf = SG_NewFieldCommand(com);
			inf->fieldType = field.fieldType;
			inf->fieldIndex = field.fieldIndex;
			if (inf->fieldType==FT_SFNode) {
				field.far_ptr = inf->field_ptr = &inf->new_node;
			} else if (inf->fieldType==FT_MFNode) {
				inf->node_list = NewChain();
				field.far_ptr = inf->field_ptr = &inf->node_list;
			} else {
				field.far_ptr = inf->field_ptr = VRML_NewFieldPointer(inf->fieldType);
			}
			e = BD_DecField(codec, bs, node, &field);
			if (e) goto exit;
		}
	} else {
		flag = BS_ReadInt(bs, 1);
		nbBits = GetNumBits(Node_GetNumFields(node, FCM_DEF)-1);
		while (!flag) {
			field_ref = BS_ReadInt(bs, nbBits);
			e = Node_GetFieldIndex(node, field_ref, FCM_DEF, &fieldind);
			if (e) goto exit;
			e = Node_GetField(node, fieldind, &field);
			if (e) goto exit;
			inf = SG_NewFieldCommand(com);
			inf->fieldType = field.fieldType;
			inf->fieldIndex = field.fieldIndex;
			if (inf->fieldType==FT_SFNode) {
				field.far_ptr = inf->field_ptr = &inf->new_node;
			} else if (inf->fieldType==FT_MFNode) {
				inf->node_list = NewChain();
				field.far_ptr = inf->field_ptr = &inf->node_list;
			} else {
				field.far_ptr = inf->field_ptr = VRML_NewFieldPointer(inf->fieldType);
			}
			e = BD_DecField(codec, bs, node, &field);
			if (e) goto exit;
			flag = BS_ReadInt(bs, 1);
		}
	}

	
exit:
	if (e) SG_DeleteCommand(com);
	else ChainAddEntry(com_list, com);
	return e;
}
Beispiel #10
0
/*NB This can insert a node as well (but usually not in the .children field)*/
M4Err BM_ParseIndexInsert(LPBIFSDEC codec, BitStream *bs, Chain *com_list)
{
	M4Err e;
	u32 NodeID;
	u32 NumBits, ind, field_ind;
	u8 type;
	SGCommand *com;
	CommandFieldInfo *inf;
	s32 pos;
	SFNode *def, *node;
	FieldInfo field, sffield;

	NodeID = 1 + BS_ReadInt(bs, codec->info->config.NodeIDBits);
	def = BIFS_FindNode(codec, NodeID);
	if (!def) return M4NonCompliantBitStream;
	/*index insertion uses IN mode for field index*/
	NumBits = GetNumBits(Node_GetNumFields(def, FCM_IN)-1);
	ind = BS_ReadInt(bs, NumBits);

	e = Node_GetFieldIndex(def, ind, FCM_IN, &field_ind);
	if (e) return e;

	type = BS_ReadInt(bs, 2);
	switch (type) {
	case 0:
		pos = BS_ReadInt(bs, 16);
		break;
	case 2:
		pos = 0;
		break;
	case 3:
		pos = -1;
		break;
	default:
		return M4NonCompliantBitStream;
	}

	e = Node_GetField(def, field_ind, &field);
	if (e) return e;
	if (VRML_IsSFField(field.fieldType)) return M4NonCompliantBitStream;

	memcpy(&sffield, &field, sizeof(FieldInfo));
	sffield.fieldType = VRML_GetSFType(field.fieldType);

	/*rescale the MFField and parse the SFField*/
	if (field.fieldType==FT_MFNode) {
		node = BD_DecSFNode(codec, bs, field.NDTtype);
		if (!codec->LastError) {
			com = SG_NewCommand(codec->current_graph, SG_IndexedInsert);
			BM_SetCommandNode(com, def);
			inf = SG_NewFieldCommand(com);
			inf->pos = pos;
			inf->fieldIndex = field_ind;
			inf->fieldType = sffield.fieldType;
			inf->new_node = node;
			inf->field_ptr = &inf->new_node;
			ChainAddEntry(com_list, com);
			/*register*/
			Node_Register(node, def);
		}
	} else {
		com = SG_NewCommand(codec->current_graph, SG_IndexedInsert);
		BM_SetCommandNode(com, def);
		inf = SG_NewFieldCommand(com);
		inf->pos = pos;
		inf->fieldIndex = field_ind;
		inf->fieldType = sffield.fieldType;
		sffield.far_ptr = inf->field_ptr = VRML_NewFieldPointer(sffield.fieldType);
		codec->LastError = BD_DecSFField(codec, bs, def, &sffield);
		ChainAddEntry(com_list, com);
	}
	return codec->LastError;
}
Beispiel #11
0
 // num_bit16_table_[]を初期化する。
 void Util::InitNumBit16Table() {
   for (unsigned int index = 0; index <= 0xffff; index++) {
     num_bit16_table_[index] = GetNumBits(index);
   }
 }
Beispiel #12
0
///=================================================================================
// creates the binary circuit that consits of only gate infomation
// input: the number of items of each server,	
//        the number of servers,
//        the number of bits for each item "value" (for multiparty computation)
// output: void
BOOL CP2PCircuit::Create(int nParties, const vector<int>& vParams)
{
	int num_servers = nParties - 1;
	
	int num_items  = vParams[0];
	int input_range = vParams[1];

	if( vParams.size() < 2 )
	{
		cout << "Error! This circuit needs two parameters: #items and input_range." << endl;
		return FALSE;
	}

	int tot_items;		// # of items of all servers
	int num_vbits;		// # of bits to represent each value	
	int num_bits;		// # of bits to identify [0, num_items]
	int num_gates;		// # of gates required to create the circuit
	int num_layers;		// # of layers for the tournament
	int num_andgates;	// # of AND gates
	int num_mux_val;	// # of MUX circuit for values
	int num_mux_idx;	// # of MUX circuit for indexes
	int num_cmp;		// # of comparison (circuit)
	int num_in;			// # of IN gates
	int num_out;		// # of OUT gates
	int num_vs_bottom;	// # of gates for each vs at the bottom of tournament 
	int num_vs_middle;	// # of gates for each vs between the bottom and top of the tournament
	int num_vs_top;		// # of gates for each vs at top of the tournament
	//vector<int> outputs = vector<int>();	// final outputs that will be given to OUT gates
	
	tot_items = num_items * num_servers;

	// collects necessary info first
	num_bits = GetNumBits(tot_items);
	num_vbits = input_range >= 0? GetNumBits(input_range) : num_bits;
	//num_vbits = -1;
	
	num_layers = (int)ceil(Log2(tot_items));
	//num_andgates = tot_items * num_bits;
	num_andgates = tot_items * num_vbits;
	num_cmp = tot_items - 1;
	num_mux_val = num_cmp - 1;
	num_mux_idx = num_cmp;
	//num_in = 2+ tot_items*num_bits + tot_items;
	num_in = 2+ tot_items*num_vbits + tot_items;
	num_out = num_bits;
	//num_gates = num_andgates + (num_mux_val+num_mux_idx)*MUX_GATES*num_bits 
	//	+ num_cmp*CMP_GATES*num_bits + num_in +num_out;
	num_gates = num_andgates + num_mux_val*MUX_GATES*num_vbits
		+ num_mux_idx*MUX_GATES*num_bits + num_cmp*CMP_GATES*num_vbits + num_in +num_out;
	//num_gates = GetNumGates(tot_items, num_bits);

	//num_vs_bottom = num_bits*2 + CMP_GATES*num_bits + 2*MUX_GATES*num_bits;
	num_vs_bottom = num_vbits*2 + CMP_GATES*num_vbits + MUX_GATES*(num_bits+num_vbits);
	//num_vs_middle = num_vs_bottom - 2*num_bits;
	num_vs_middle = num_vs_bottom - 2*num_vbits;
	num_vs_top = num_vs_middle - MUX_GATES*num_bits;
	num_vs_top = num_vs_middle - MUX_GATES*num_vbits;

	// circuit initialization
	cout << " Total # of gates:" << num_gates << endl;
	cout << " Total # of gates for bottom vs:" << num_vs_bottom << endl;
	cout << " Total # of gates for middle vs:" << num_vs_middle << endl;
	cout << " Total # of gates for top vs:" << num_vs_top << endl;
	cout << " Total # of IN gates:" << num_in << endl;

	m_nNumGates = num_gates;
	//m_nNumBits = num_bits;
	m_nNumItems = num_items; 
	//m_nNumVBits = input_range >= 0 ? GetNumBits(input_range) : num_bits;
	m_nNumServers = num_servers;
	m_nNumXORs = 0;
	m_vNumVBits.resize(nParties,num_vbits);
	m_vNumVBits[nParties-1] = 1;

	m_pGates = new GATE[num_gates];  
	if (m_pGates == NULL){
		puts("CP2PCircuit::Create(const int): memory error");
		exit(-1);
	}
	for (int i=0;i<num_gates;i++){
		(m_pGates+i)->left = -1;
		(m_pGates+i)->right = -1;
		(m_pGates+i)->p_num = 0;
	}
	 

	// starts creating the circuit	
	int id = 0;				// gate id
	int is_left = 1;		// left - 1, right - 0	
	int is_final = 0;		// final comparison: yes - 1, no - 0
	int is_bottompair = 0;	// indicates whether the comparison is a pair at the bottom of tournament
	int is_bottomsingle = 0;// indicates whether the comparison is a single at the bottom of tournament
	int is_middle = 0;		// indicates whether the comparison is in the middle of tournament
	int is_alone = 0;		// whether the vs is a pair or single
	int is_moreMUXvalue = 0;// whether the higher layer's vs uses MUX_value
	int num_remainders;		// # of items that remain on the tournament
	int gates_skip;			// # of gates that should be skipped
	int layer_no=0;			// layer number from the bottom of the tournament starting from 0
	int* vs_no;				// # of completed vs from the leftmost (on the current layer) of the tournament starting from 0
	int* layer_waiting;	// # of waitings at each layer
	int* is_left_before;	// whether last vs was left or right
	
	num_remainders = tot_items;
	
	layer_waiting = new int[num_layers+1];
	vs_no = new int[num_layers+1];
	is_left_before = new int[num_layers+1];
	for (int i=0;i<num_layers;i++){
		layer_waiting[i] = 0;
		vs_no[i]=0;
		is_left_before[i] = -1;
	}

	// IN gates for const inputs 0 and 1
	(m_pGates+id)->type = G_XOR;
	(m_pGates+id)->right = 0;
	id++;
	(m_pGates+id)->type = G_XOR;
	(m_pGates+id)->right = 0;
	id++;
	
	// creates gates for server inputs
	int tmp_num_bits;
	if (num_vbits <= 0){
		tmp_num_bits = num_bits;	// for two party computation
	}
	else{
		tmp_num_bits = num_vbits;	// for multiparty computation
	}
	m_serStartGate = id;
	for (int i=0;i<num_servers;i++){
		m_vInputStart.push_back(id);
		for (int j=0;j<num_items;j++){
			for (int k=0;k<tmp_num_bits;k++,id++){
				(m_pGates+id)->type = G_XOR;
				(m_pGates+id)->right = 0;
			}
		}
		m_vInputEnd.push_back(id-1);
	}
	
	// creates gates for client inputs
	m_cliStartGate = id;
	m_vInputStart.push_back(id);
	for(int i=0;i<tot_items;i++,id++){
		(m_pGates+id)->type = G_XOR;
		(m_pGates+id)->right = 0;
	}
	m_vInputEnd.push_back(id-1);

	// creates gates for output of each party
	// servers
	for (int i=0;i<nParties-1;i++){
		// no output for servers
		m_vOutputStart.push_back(0);
		m_vOutputEnd.push_back(-1);
	}

	// output for client
	m_vOutputStart.push_back(num_gates-num_out);
	m_vOutputEnd.push_back(num_gates-1);
	for(int i=0;i<num_out;i++){
		(m_pGates+m_vOutputStart[num_servers]+i)->type = G_XOR;
		(m_pGates+m_vOutputStart[num_servers]+i)->p_num = 0;
		(m_pGates+m_vOutputStart[num_servers]+i)->right = 0;
	}

	// creates gates other than server and client's input gates
	m_othStartGate = id;
	for (int i=0;i<num_cmp;i++){
		if (i== num_cmp-1){
			is_final = 1;	
		}

		int tmp=0;
		int layer_no_tmp=-1;
		// compares with two same layers (bottom layer not included)
		assert(layer_no >= 0);
		if (layer_waiting[layer_no] == 2){
			is_alone = 0;
			//is_left = 1;///////////////////////////////
			if (is_left_before[layer_no] == 1){
				is_left = 0;
				is_left_before[layer_no] = is_left;
			}
			else if (is_left_before[layer_no] == -1){
				is_left = 1;
				is_left_before[layer_no] = is_left;
			}
			else if (is_left_before[layer_no] == 0){
				is_left = IsVSLeft(tot_items,layer_no, vs_no);
				//tmp = tot_items - 2*vs_no[layer_no];
				is_left_before[layer_no] = is_left;
			}
			
			if (is_left){
				if (layer_no >= num_layers-2){
					is_moreMUXvalue = 0;
				}
				else{
					is_moreMUXvalue = 1;
				}
			}
			else {// && num_remainders>0){
				is_moreMUXvalue = 0;
				for (int j=layer_no+1;j<num_layers-1;j++){

					#ifdef _DEBUG
					assert(j >= 0);
					#endif
					
					if (layer_waiting[j] == 1){
						is_moreMUXvalue = 1;
						break;
					}
				}
			}
			
			if (is_left){
				gates_skip = GetGatesSkip(tot_items, num_vbits, layer_no, num_vs_bottom, num_vs_middle, vs_no);
			}

			id = CreateCMP(id, is_final, num_bits, num_vbits);
			if (!is_final){
				id = CreateMUXValue(id, is_final, num_bits, num_vbits,is_left, gates_skip,is_moreMUXvalue);
			}
			id = CreateMUXIndex(id, is_final, num_bits, num_vbits, is_left, layer_no, gates_skip,is_moreMUXvalue, vs_no, is_alone, nParties);
			
			vs_no[layer_no]++;
			layer_waiting[layer_no]=0;
			
			#ifdef _DEBUG
			assert( layer_no >= 0);
			#endif
			
			layer_no++;
			layer_waiting[layer_no]++;

			#ifdef _DEBUG
			assert( layer_no <= num_layers);
			#endif
			
		}
		// compares with different layers (bottom layer not included)
		else if (num_remainders == 0){
			is_alone= 0;
			is_left = 0;

			for (int j=layer_no+1;j<num_layers;j++){

				#ifdef _DEBUG
				assert( j >= 0 );
				#endif
			
				if (layer_waiting[j] == 1){
					layer_waiting[j] = 0;
					vs_no[j]++;
					layer_no_tmp = j+1;
					break;
				}
			}
			if (layer_no_tmp == -1){
				puts("CP2PCircuit::Create(const int): layer_no_tmp is -1");		
				exit(-1);
			}

			//if (layer_no_tmp == num_layers-1){	// higher layer (one above) is the top layer
			//	is_moreMUXvalue=0;
			//}
			//else{
			//	is_moreMUXvalue=1;
			//}
			
			is_moreMUXvalue = 0;
			for (int j=layer_no_tmp;j<num_layers-1;j++){

				#ifdef _DEBUG
				assert( j >= 0 );
				#endif
		

				if (layer_waiting[j] == 1){
					is_moreMUXvalue = 1;
					break;
				}
			}

			id = CreateCMP(id, is_final, num_bits, num_vbits);
			if (!is_final){
				id = CreateMUXValue(id, is_final, num_bits, num_vbits,is_left, gates_skip,is_moreMUXvalue);
			}
			id = CreateMUXIndex(id, is_final, num_bits, num_vbits, is_left, layer_no, gates_skip,is_moreMUXvalue, vs_no, is_alone, nParties);
			
			layer_waiting[layer_no]=0;

			#ifdef _DEBUG
			assert( layer_no >= 0 && layer_no < num_layers);
			#endif
		

			layer_no = layer_no_tmp;
			layer_waiting[layer_no]++;

			//#ifdef _DEBUG
			//assert( layer_no >= 0 && layer_no < num_layers);
			//#endif
		
		}
		// compares with different layers (only one layer is bottom)
		else if (num_remainders == 1){
			layer_no = 0;
			is_alone = 1;
			is_left = 0;
			
			for (int j=layer_no+1;j<num_layers;j++){

				#ifdef _DEBUG
				assert( j >= 0 );  
				#endif
		
				if (layer_waiting[j] == 1){
					layer_waiting[j] = 0;
					vs_no[j]++;
					layer_no_tmp = j+1;
					break;
				}
			}
			if (layer_no_tmp == -1){
				puts("CP2PCircuit::Create(const int): layer_no_tmp is -1");		
				exit(-1);
			}
			/*
			if (layer_no_tmp == num_layers){	// higher layer (one above) is the top layer
				is_moreMUXvalue=0;
			}
			else{
				is_moreMUXvalue=1;
			}
			*/
			is_moreMUXvalue = 0;
			for (int j=layer_no_tmp;j<num_layers-1;j++){

				#ifdef _DEBUG
				assert( j >= 0 );  
				#endif
		
				if (layer_waiting[j] == 1){
					is_moreMUXvalue = 1;
					break;
				}
			}
			
			id = CreateANDRight(id, is_final, num_vbits, vs_no, 1);
			id = CreateCMP(id, is_final, num_bits, num_vbits);
			if (!is_final){
				id = CreateMUXValue(id, is_final, num_bits, num_vbits, is_left, gates_skip,is_moreMUXvalue);
			}
			id = CreateMUXIndex(id, is_final, num_bits, num_vbits, is_left, layer_no, gates_skip,is_moreMUXvalue, vs_no, is_alone, nParties);

			layer_no = layer_no_tmp;
			layer_waiting[layer_no]++;
			num_remainders--;

			#ifdef _DEBUG
			assert( layer_no >= 0 && layer_no < num_layers);
			#endif
		
		}
		// compares with two bottom layers
		else if (num_remainders >= 2){
			layer_no = 0;
			is_alone = 0;
			if (is_left_before[layer_no] == 1){
				is_left = 0;
				is_left_before[layer_no] = is_left;
			}
			else if (is_left_before[layer_no] == -1){
				is_left = 1;
				is_left_before[layer_no] = is_left;
			}
			else if (is_left_before[layer_no] == 0){
				is_left = IsVSLeft(tot_items,layer_no, vs_no);
				//tmp = tot_items - 2*vs_no[layer_no];
				is_left_before[layer_no] = is_left;
			}
			
			if (num_layers<=2){
				is_moreMUXvalue = 0;
			}
			else if(is_left){
				is_moreMUXvalue = 1;
			}
			else {
				is_moreMUXvalue = 0;
				for (int j=layer_no+1;j<num_layers-1;j++){

					#ifdef _DEBUG
					assert( j >= 0 );
					#endif
		
					if (layer_waiting[j] == 1){
						is_moreMUXvalue = 1;
						break;
					}
				}
			}
			if (is_left){						
				gates_skip = GetGatesSkip(tot_items, num_vbits, layer_no, num_vs_bottom, num_vs_middle, vs_no);
			}
			//id = CreateInputLeft(id, is_final, num_bits);
			//id = CreateInputRight(id, is_final, num_bits);
			id = CreateANDLeft(id, is_final, num_vbits, vs_no);
			id = CreateANDRight(id, is_final, num_vbits, vs_no, 0);
			id = CreateCMP(id, is_final, num_bits, num_vbits);
			if (!is_final){
				id = CreateMUXValue(id, is_final, num_bits, num_vbits, is_left, gates_skip, is_moreMUXvalue);
			}
			id = CreateMUXIndex(id, is_final, num_bits, num_vbits, is_left, layer_no, gates_skip,is_moreMUXvalue, vs_no, is_alone, nParties);
			
			vs_no[layer_no]++;
			layer_no = 1;
			layer_waiting[layer_no]++;
			num_remainders -= 2;

			#ifdef _DEBUG
			assert( layer_no >= 0 && layer_no < num_layers);
			#endif
		
		}
	}

	#ifdef _DEBUG
	cout << "final gate id: " << id-1+num_out << endl;
	#endif

	delete [] layer_waiting;
	delete [] vs_no;
	delete [] is_left_before; 

	m_nNumParties = m_nNumServers + 1;
	m_vInputStart.resize(m_nNumParties);
	m_vInputEnd.resize(m_nNumParties);

	//for(int i=0; i<m_nNumParties; i++)
	//{
	//	m_vInputStart[i] = GetInputStartC(i);
	//	m_vInputEnd[i] = GetInputEndC(i);
	//}

	#ifdef _DEBUG
	cout << "input start gate id1: " << m_vInputStart[0] << endl;
	cout << "input end gate id1: " << m_vInputEnd[0] << endl;
	cout << "input start gate id2: " << m_vInputStart[1] << endl;
	cout << "input end gate id2: " << m_vInputEnd[1] << endl;
	cout << "input start gate id3: " << m_vInputStart[2] << endl;
	cout << "input end gate id3: " << m_vInputEnd[2] << endl;
	cout << "output start gate id1: " << m_vOutputStart[0] << endl;
	cout << "output end gate id1: " << m_vOutputEnd[0] << endl;
	cout << "output start gate id2: " << m_vOutputStart[1] << endl;
	cout << "output end gate id2: " << m_vOutputEnd[1] << endl;
	cout << "output start gate id3: " << m_vOutputStart[2] << endl;
	cout << "output end gate id3: " << m_vOutputEnd[2] << endl;
	#endif

	return TRUE;
}