Example #1
0
BLAPI_PROTO
bl_kball(lua_State *L)
{
    // first, sleep by given seconds
    int r = 0, i = 0;

    r = bl_sleep(L);
    if (blrt.abort)
        return r;

    // pop all arguments
    lua_pop(L, lua_gettop(L));


#ifdef _WIN32
    while (i < LUA_MINSTACK && peekch(0))
    {
        bl_k2s(L, vkey());
        i++;
    }
#else
    while (i < LUA_MINSTACK && vkey_is_ready()) {
        bl_k2s(L, vkey());
        i++;
    }
#endif

    return i;
}
Example #2
0
File: stuff.c Project: ptt/pttbbs
// TODO
// move this function to vtuikit.c
int
cursor_key(int row, int column)
{
    int             ch;

    cursor_show(row, column);
    ch = vkey();
    cursor_clear(row, column);
    return ch;
}
Example #3
0
BLAPI_PROTO
bl_getch(lua_State* L)
{
    int c = vkey();
    if (c == BLCONF_BREAK_KEY)
    {
        vkey_purge();
        blrt.abort = 1;
        return lua_yield(L, 0);
    }
    bl_k2s(L, c);
    return 1;
}
Example #4
0
int
pad_view()
{
  int fd, count;
  Pad *pad;

  if ((fd = open(FN_RUN_NOTE_PAD, O_RDONLY)) < 0)
    return XEASY;

  count = 0;
  mgets(-1);

  for (;;)
  {
    pad = mread(fd, sizeof(Pad));
    if (!pad)
    {
      vmsg(NULL);
      break;
    }
    else if (!(count % 5))	/* itoc.020122: 有 pad 才印 */
    {
      clear();
      move(0, 23);
      prints("【 酸 甜 苦 辣 留 言 板 】                第 %d 頁\n\n", count / 5 + 1);
    }

    outs(pad->msg);
    count++;

    if (!(count % 5))
    {
      move(b_lines, 0);
      outs("請按 [SPACE] 繼續觀賞,或按其他鍵結束: ");
      /* itoc.010127: 修正在偵測左右鍵全形下,按左鍵會跳離二層選單的問題 */

      if (vkey() != ' ')
	break;
    }
  }

  close(fd);
  return 0;
}
Example #5
0
//Do a Breadth first search
int 
Vertex::findReachableNodes()
{
	//lazy bum me is going to STL queue of vertices
	queue<Vertex*> reachableQ;
	//Initialize reachableQ with in neighbours
	for(NINFO_MAP_ITER nmIter=outNeighbours.begin();nmIter!=outNeighbours.end();nmIter++)
	{
		reachableQ.push(nmIter->second);
		reachableNeighbours[nmIter->first]=1;
		nmIter->second->addReachableFromNode(vname,1);
	}

	while(!reachableQ.empty())
	{
		Vertex* v=reachableQ.front();
		//get the neighbours of aNode
		NINFO_MAP& vNeigh=v->getImmediateNeighbours();
		//This node, u is reachable to all the neighbours of v by 1 + dist(u,v)
		string vkey(v->getName());
		if(reachableNeighbours.find(vkey)==reachableNeighbours.end())
		{
			cerr <<"Expected a node in reachable list, did not find " << endl;
			exit(-1);
		}
		int dist_uv=reachableNeighbours[vkey];
		for(NINFO_MAP_ITER nmIter=vNeigh.begin();nmIter!=vNeigh.end();nmIter++)
		{
			if(strcmp(nmIter->first.c_str(),vname)==0)
			{
				continue;
			}
			//Check for cycles
			if(reachableNeighbours.find(nmIter->first)==reachableNeighbours.end())
			{
				//Everytime the node v acts as an intermediary, increment the betweenness count
				v->incrBetweenness(vname,nmIter->first.c_str());
				reachableNeighbours[nmIter->first]=dist_uv+1;
				reachableQ.push(nmIter->second);
				shortestPathCnts[nmIter->first]=1;
				nmIter->second->addReachableFromNode(vname,dist_uv+1);
			}
			else 
			{
				//If we have encountered this vertex before and the length is equal
				//to the length of the previously encounterd path then increment betweenness
				int currDist=reachableNeighbours[nmIter->first];
				if(currDist==(dist_uv+1))
				{
					v->incrBetweenness(vname,nmIter->first.c_str());
					shortestPathCnts[nmIter->first]=shortestPathCnts[nmIter->first]+1;
				}
				if(currDist>(dist_uv+1))
				{
					cerr <<"Error! A shorter path found later on!! BFS search is faulty" << endl;
					exit(-1);
				}
			}
		}
		reachableQ.pop();
	}
	return 0;
}
Example #6
0
int fmain_menu()
{
	char c;
	int i, x, y;
	int now_main = site, old_main = 0;

	clear();
	draw_banner(MAIN_MENU_TOP - 6);

	move(MAIN_MENU_TOP - 2, MAIN_MENU_LEFT);
	prints("¢~שששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששש¢¡");
	for (i = 0;i < total_nall;i++)
	{
		x = i % MAX_MAIN_MENU ;
		y = i / MAX_MAIN_MENU ;
		if (y == 0)
		{
			move(MAIN_MENU_TOP + x, MAIN_MENU_LEFT);
			clrtohol();
		}
		move(MAIN_MENU_TOP + x, MAIN_MENU_LEFT + y*37);
		prints("%s%12s  %s", ORG_BAR, main_menu[i], ORG_BAR);
	}
	move(MAIN_MENU_TOP + MAX_MAIN_MENU + 1, MAIN_MENU_LEFT);
	prints("¢¢שששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששששש¢£");
	outz("\033[46;44m ¡i¾�§@»¡©ת¡j \033[47;31m  (¡פ¡ץ¡צ¡ק) \033[30m¿ן¾� \033[31m(Enter) \033[30m½T©w\033[31m (q)\033[30m ֲק¶}                          \033[m");
	draw_main_menu(site, site);

	do
	{
		old_main = now_main;
		c = vkey();
		switch (c)
		{
		case KEY_DOWN:
			now_main ++;
			if (now_main == total_nall)
				now_main = 0;
			draw_main_menu(now_main, old_main);
			break;
		case KEY_UP:
			now_main --;
			if (now_main < 0)
				now_main = total_nall - 1;
			draw_main_menu(now_main, old_main);
			break;
		case KEY_HOME:
		case '0':
			now_main = 0;
			draw_main_menu(now_main, old_main);
			break;
		case KEY_END:
		case '$':
			now_main = total_nall - 1;
			draw_main_menu(now_main, old_main);
			break;
		case Ctrl('U'):
						every_U();
			break;
		case KEY_RIGHT:
			now_main += MAX_MAIN_MENU ;
			if (now_main > total_nall - 1)
				now_main %= MAX_MAIN_MENU ;
			draw_main_menu(now_main, old_main);
			break ;
		case KEY_LEFT:
			if (now_main < MAX_MAIN_MENU)
	{
				if (now_main <= (total_nall - 1) % MAX_MAIN_MENU)
				{
					now_main += MAX_MAIN_MENU * ((total_nall - 1) / MAX_MAIN_MENU);
				}
				else
				{
					now_main = total_nall - 1;
				}
			}
			else
				now_main -= MAX_MAIN_MENU ;
			draw_main_menu(now_main, old_main);
			break;
		case '\n':
		case '\r':
			return now_main;
		}
	}
	while (c != 'q' && c != 'Q' && c != KEY_ESC);
	return -1;
}
Example #7
0
int main_classtable(void)
{
	char c;
	int x = 1, y = 1;
	int fd;

#ifdef	HAVE_CLASSTABLEALERT
	classtable_free();
#endif
	usr_fpath(fpath_classtable, cuser.userid, FN_CLASSTABLE2);

	init_classtable();
	show_icon_classtable(x, y, 1);

	do
	{
		c = vkey();
		switch (c)
		{
		case 'q' :
			break;
		case KEY_DOWN :
			show_icon_classtable(x, y, 0);
			x++;
			if (x == 14) x = 1;
			show_icon_classtable(x, y, 1);
			break;
		case KEY_UP:
			show_icon_classtable(x, y, 0);
			x--;
			if (x == 0) x = 13;
			show_icon_classtable(x, y, 1);
			break;
		case KEY_LEFT:
			show_icon_classtable(x, y, 0);
			y--;
			if (y == 0) y = 6;
			show_icon_classtable(x, y, 1);
			break;
		case KEY_RIGHT :
			show_icon_classtable(x, y, 0);
			y++;
			if (y == 7) y = 1;
			show_icon_classtable(x, y, 1);
			break;
		case 'a' :
			add_classtable(x, y);
			break;
		case 'd' :
			del_classtable(x, y);
			break;
		case 'e' :
			edit_classtable(x, y);
			break;
		case 'i' :
			import_classtable(x, y);
			break;
		case 'c' :
			if (vans("確定刪除嗎?[y/N] ") == 'y')
			{
				memset(tmp_table, 0, sizeof(CLASS_TABLE2)*78);
				show_table();
			}
			break;
		case '\n' :
			if (tmp_table[x+y*13-14].valid == 1)
				edit_classtable(x, y);
			else
				add_classtable(x, y);
			break;
		}
	}
	while (c != 'q');

	fd = open(fpath_classtable, O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (fd >= 0)
	{
		write(fd, tmp_table, sizeof(CLASS_TABLE2)*78);
		close(fd);
	}
#ifdef	HAVE_CLASSTABLEALERT
	classtable_main();
#endif

	vmsg(NULL);
	return 0;
}
Example #8
0
static int
search_key_user(const char *passwdfile, int mode)
{
    userec_t        user;
    int             ch;
    int             unum = 0;
    FILE            *fp1 = fopen(passwdfile, "r");
    char            friendfile[PATHLEN]="", key[22], *keymatch;
    int		    keytype = 0;
    int	            isCurrentPwd;

    assert(fp1);

    isCurrentPwd = (strcmp(passwdfile, FN_PASSWD) == 0);
    clear();
    if (!mode)
    {
	getdata(0, 0, "請輸入id :", key, sizeof(key), DOECHO);
    } else {
	// improved search
	vs_hdr("關鍵字搜尋");
	outs("搜尋欄位: [0]全部 1.ID 2.姓名 3.暱稱 4.地址 5.Mail 6.IP 7.職業 8.認證\n");
	getdata(2, 0, "要搜尋哪種資料?", key, 2, NUMECHO);

	if (isascii(key[0]) && isdigit(key[0]))
	    keytype = key[0] - '0';
	if (keytype < 0 || keytype > 8)
	    keytype = 0;
	getdata(3, 0, "請輸入關鍵字: ", key, sizeof(key), DOECHO);
    }

    if(!key[0]) {
	fclose(fp1);
	return 0;
    }
    vs_hdr(key);

    // <= or < ? I'm not sure...
    while ((fread(&user, sizeof(user), 1, fp1)) > 0 && unum++ < MAX_USERS) {

	// skip empty records
	if (!user.userid[0])
	    continue;

	if (!(unum & 0xFF)) {
	    vs_hdr(key);
	    prints("第 [%d] 筆資料\n", unum);
	    refresh();
	}

	// XXX 這裡會取舊資料,要小心 PWD 的 upgrade
	if (!upgrade_passwd(&user))
	    continue;

        keymatch = NULL;

	if (!mode)
	{
	    // only verify id
	    if (!strcasecmp(user.userid, key))
		keymatch = user.userid;
	} else {
	    // search by keytype
	    if ((!keytype || keytype == 1) &&
		DBCS_strcasestr(user.userid, key))
		keymatch = user.userid;
	    else if ((!keytype || keytype == 2) &&
		DBCS_strcasestr(user.realname, key))
		keymatch = user.realname;
	    else if ((!keytype || keytype == 3) &&
		DBCS_strcasestr(user.nickname, key))
		keymatch = user.nickname;
	    else if ((!keytype || keytype == 4) &&
		DBCS_strcasestr(user.address, key))
		keymatch = user.address;
	    else if ((!keytype || keytype == 5) &&
		strcasestr(user.email, key)) // not DBCS.
		keymatch = user.email;
	    else if ((!keytype || keytype == 6) &&
		strcasestr(user.lasthost, key)) // not DBCS.
		keymatch = user.lasthost;
	    else if ((!keytype || keytype == 7) &&
		DBCS_strcasestr(user.career, key))
		keymatch = user.career;
	    else if ((!keytype || keytype == 8) &&
		DBCS_strcasestr(user.justify, key))
		keymatch = user.justify;
	}

        if(keymatch) {
	    vs_hdr(key);
	    prints("第 [%d] 筆資料\n", unum);
	    refresh();

	    user_display(&user, 1);
	    // user_display does not have linefeed in tail.

	    if (isCurrentPwd && HasUserPerm(PERM_ACCOUNTS))
		uinfo_query(user.userid, 1, unum);
	    else
		outs("\n");

	    // XXX don't trust 'user' variable after here
	    // because uinfo_query may have changed it.

	    outs(ANSI_COLOR(44) "               空白鍵" \
		 ANSI_COLOR(37) ":搜尋下一個          " \
		 ANSI_COLOR(33)" Q" ANSI_COLOR(37)": 離開");
	    outs(mode ?
                 "      A: add to namelist " ANSI_RESET " " :
		 "      S: 取用備份資料    " ANSI_RESET " ");
	    while (1) {
		while ((ch = vkey()) == 0)
		    ;
                if (ch == 'a' || ch=='A' )
                  {
                   if(!friendfile[0])
                    {
                     friend_special();
                     setfriendfile(friendfile, FRIEND_SPECIAL);
                    }
                   friend_add(user.userid, FRIEND_SPECIAL, keymatch);
                   break;
                  }
		if (ch == ' ')
		    break;
		if (ch == 'q' || ch == 'Q') {
		    fclose(fp1);
		    return 0;
		}
		if (ch == 's' && !mode) {
		    if (retrieve_backup(&user) >= 0) {
			fclose(fp1);
                        vmsg("已成功\取用備份資料。");
			return 0;
		    } else {
                        vmsg("錯誤: 取用備份資料失敗。");
                    }
		}
	    }
	}
void run_setup(int num_constraints, int num_inputs,
               int num_outputs, int num_vars, mpz_t p,
               string vkey_file, string pkey_file,
               string unprocessed_vkey_file) {

    std::ifstream Amat("./bin/" + std::string(NAME) + ".qap.matrix_a");
    std::ifstream Bmat("./bin/" + std::string(NAME) + ".qap.matrix_b");
    std::ifstream Cmat("./bin/" + std::string(NAME) + ".qap.matrix_c");

    libsnark::default_r1cs_gg_ppzksnark_pp::init_public_params();
    libsnark::r1cs_constraint_system<FieldT> q;

    int Ai, Aj, Bi, Bj, Ci, Cj;
    mpz_t  Acoef, Bcoef, Ccoef;
    mpz_init(Acoef);
    mpz_init(Bcoef);
    mpz_init(Ccoef);

    Amat >> Ai;
    Amat >> Aj;
    Amat >> Acoef;

    if (mpz_cmpabs(Acoef, p) > 0) {
        gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Acoef, p);
        mpz_mod(Acoef, Acoef, p);
    }
    if (mpz_sgn(Acoef) == -1) {
        mpz_add(Acoef, p, Acoef);
    }

    //    std::cout << Ai << " " << Aj << " " << Acoef << std::std::endl;

    Bmat >> Bi;
    Bmat >> Bj;
    Bmat >> Bcoef;
    if (mpz_cmpabs(Bcoef, p) > 0) {
        gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Bcoef, p);
        mpz_mod(Bcoef, Bcoef, p);
    }
    if (mpz_sgn(Bcoef) == -1) {
        mpz_add(Bcoef, p, Bcoef);
    }

    Cmat >> Ci;
    Cmat >> Cj;
    Cmat >> Ccoef;

    if (mpz_cmpabs(Ccoef, p) > 0) {
        gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Ccoef, p);
        mpz_mod(Ccoef, Ccoef, p);
    }
    if (mpz_sgn(Ccoef) == -1) {
        mpz_mul_si(Ccoef, Ccoef, -1);
    } else if(mpz_sgn(Ccoef) == 1) {
        mpz_mul_si(Ccoef, Ccoef, -1);
        mpz_add(Ccoef, p, Ccoef);
    }

    int num_intermediate_vars = num_vars;
    int num_inputs_outputs = num_inputs + num_outputs;

    q.primary_input_size = num_inputs_outputs;
    q.auxiliary_input_size = num_intermediate_vars;

    for (int currentconstraint = 1; currentconstraint <= num_constraints; currentconstraint++) {
        libsnark::linear_combination<FieldT> A, B, C;

        while(Aj == currentconstraint && Amat) {
            if (Ai <= num_intermediate_vars && Ai != 0) {
                Ai += num_inputs_outputs;
            } else if (Ai > num_intermediate_vars) {
                Ai -= num_intermediate_vars;
            }

            FieldT AcoefT(Acoef);
            A.add_term(Ai, AcoefT);
            if(!Amat) {
                break;
            }
            Amat >> Ai;
            Amat >> Aj;
            Amat >> Acoef;
            if (mpz_cmpabs(Acoef, p) > 0) {
                gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Acoef, p);
                mpz_mod(Acoef, Acoef, p);
            }
            if (mpz_sgn(Acoef) == -1) {
                mpz_add(Acoef, p, Acoef);
            }
        }

        while(Bj == currentconstraint && Bmat) {
            if (Bi <= num_intermediate_vars && Bi != 0) {
                Bi += num_inputs_outputs;
            } else if (Bi > num_intermediate_vars) {
                Bi -= num_intermediate_vars;
            }
            //         std::cout << Bi << " " << Bj << " " << Bcoef << std::std::endl;
            FieldT BcoefT(Bcoef);
            B.add_term(Bi, BcoefT);
            if (!Bmat) {
                break;
            }
            Bmat >> Bi;
            Bmat >> Bj;
            Bmat >> Bcoef;
            if (mpz_cmpabs(Bcoef, p) > 0) {
                gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Bcoef, p);
                mpz_mod(Bcoef, Bcoef, p);
            }
            if (mpz_sgn(Bcoef) == -1) {
                mpz_add(Bcoef, p, Bcoef);
            }
        }

        while(Cj == currentconstraint && Cmat) {
            if (Ci <= num_intermediate_vars && Ci != 0) {
                Ci += num_inputs_outputs;
            } else if (Ci > num_intermediate_vars) {
                Ci -= num_intermediate_vars;
            }
            //Libsnark constraints are A*B = C, vs. A*B - C = 0 for Zaatar.
            //Which is why the C coefficient is negated.

            // std::cout << Ci << " " << Cj << " " << Ccoef << std::std::endl;
            FieldT CcoefT(Ccoef);
            C.add_term(Ci, CcoefT);
            if (!Cmat) {
                break;
            }
            Cmat >> Ci;
            Cmat >> Cj;
            Cmat >> Ccoef;
            if (mpz_cmpabs(Ccoef, p) > 0) {
                gmp_printf("WARNING: Coefficient larger than prime (%Zd > %Zd).\n", Ccoef, p);
                mpz_mod(Ccoef, Ccoef, p);
            }
            if (mpz_sgn(Ccoef) == -1) {
                mpz_mul_si(Ccoef, Ccoef, -1);
            } else if (mpz_sgn(Ccoef) == 1) {
                mpz_mul_si(Ccoef, Ccoef, -1);
                mpz_add(Ccoef, p, Ccoef);
            }
        }

        q.add_constraint(libsnark::r1cs_constraint<FieldT>(A, B, C));

        //dump_constraint(r1cs_constraint<FieldT>(A, B, C), va, variable_annotations);
    }

    Amat.close();
    Bmat.close();
    Cmat.close();

    libff::start_profiling();
    libsnark::r1cs_gg_ppzksnark_keypair<libsnark::default_r1cs_gg_ppzksnark_pp> keypair = libsnark::r1cs_gg_ppzksnark_generator<libsnark::default_r1cs_gg_ppzksnark_pp>(q);
    libsnark::r1cs_gg_ppzksnark_processed_verification_key<libsnark::default_r1cs_gg_ppzksnark_pp> pvk = libsnark::r1cs_gg_ppzksnark_verifier_process_vk<libsnark::default_r1cs_gg_ppzksnark_pp>(keypair.vk);


    std::ofstream vkey(vkey_file);
    std::ofstream pkey(pkey_file);

    vkey << pvk;
    pkey << keypair.pk;
    pkey.close(); vkey.close();

    if (unprocessed_vkey_file.length() > 0) {
        std::ofstream unprocessed_vkey(unprocessed_vkey_file);
        unprocessed_vkey << keypair.vk;
        unprocessed_vkey.close();
    }
}
Example #10
0
void verify (string verification_key_fn, string inputs_fn, string outputs_fn,
             string proof_fn, int num_inputs, int num_outputs, mpz_t prime) {

    libsnark::default_r1cs_gg_ppzksnark_pp::init_public_params();

    libsnark::r1cs_variable_assignment<FieldT> inputvec;
    libsnark::r1cs_gg_ppzksnark_proof<libsnark::default_r1cs_gg_ppzksnark_pp> proof;

    std::cout << "loading proof from file: " << proof_fn << std::endl;
    std::ifstream proof_file(proof_fn);
    if (!proof_file.good()) {
        std::cerr << "ERROR: " << proof_fn << " not found. " << std::endl;
        exit(1);
    }
    proof_file >> proof;
    proof_file.close();

    std::cout << "loading inputs from file: " << inputs_fn << std::endl;
    std::ifstream inputs_file(inputs_fn);

    std::cout << "loading outputs from file: " << outputs_fn << std::endl;
    std::ifstream outputs_file(outputs_fn);

    mpq_t tmp; mpq_init(tmp);
    mpz_t tmp_z; mpz_init(tmp_z);

    for (int i = 0; i < num_inputs; i++) {
        inputs_file >> tmp;
        convert_to_z(tmp_z, tmp, prime);
        FieldT currentVar(tmp_z);
        inputvec.push_back(currentVar);
    }

    for (int i = 0; i < num_outputs; i++) {
        outputs_file >> tmp;
        convert_to_z(tmp_z, tmp, prime);
        FieldT currentVar(tmp_z);
        inputvec.push_back(currentVar);
    }

    mpq_clear(tmp); mpz_clear(tmp_z);

    inputs_file.close();
    outputs_file.close();

    cout << "loading vk from file: " << verification_key_fn << std::endl;
    std::ifstream vkey(verification_key_fn);
    libsnark::r1cs_gg_ppzksnark_processed_verification_key<libsnark::default_r1cs_gg_ppzksnark_pp> pvk;
    vkey >> pvk;
    vkey.close();

    cout << "verifying..." << std::endl;
    libff::start_profiling();
    bool result = libsnark::r1cs_gg_ppzksnark_online_verifier_strong_IC<libsnark::default_r1cs_gg_ppzksnark_pp>(pvk, inputvec, proof);

    if (result) {
        cout << "VERIFICATION SUCCESSFUL" << std::endl;
    }
    else {
        cout << "VERIFICATION FAILED" << std::endl;
    }

}