static int param_set_tab(pbc_param_t par, symtab_t tab) { const char *s = lookup(tab, "type"); static struct { char *s; int (*fun)(pbc_param_ptr, symtab_t tab); } funtab[] = { { "a", pbc_param_init_a }, { "d", pbc_param_init_d }, { "e", pbc_param_init_e }, { "f", pbc_param_init_f }, { "g", pbc_param_init_g }, { "a1", pbc_param_init_a1 }, { "i", pbc_param_init_i }, }; int res = 1; if (s) { unsigned int i; for(i = 0; i < sizeof(funtab)/sizeof(*funtab); i++) { if (!strcmp(s, funtab[i].s)) { res = funtab[i].fun(par, tab); if (res) pbc_error("bad pairing parameters"); return res; } } } pbc_error("unknown pairing type"); return res; }
/* lightuserdata env */ static int _last_error(lua_State *L) { struct pbc_env * env = (struct pbc_env *)checkuserdata(L, 1); const char * err = pbc_error(env); lua_pushstring(L,err); return 1; }
static const char *lookup(symtab_t tab, const char *key) { if (!symtab_has(tab, key)) { pbc_error("missing param: `%s'", key); return NULL; } return (const char*)symtab_at(tab, key); }
static void test_rmessage(struct pbc_env *env, struct pbc_slice *slice) { struct pbc_rmessage * m = pbc_rmessage_new(env, "tutorial.Person", slice); if (m==NULL) { printf("Error : %s",pbc_error(env)); return; } printf("name = %s\n", pbc_rmessage_string(m , "name" , 0 , NULL)); printf("id = %d\n", pbc_rmessage_integer(m , "id" , 0 , NULL)); printf("email = %s\n", pbc_rmessage_string(m , "email" , 0 , NULL)); int phone_n = pbc_rmessage_size(m, "phone"); int i; const char * field_name; pbc_type(env, "tutorial.Person", "phone", &field_name); printf("phone type [%s]\n",field_name); for (i=0;i<phone_n;i++) { struct pbc_rmessage * p = pbc_rmessage_message(m , "phone", i); printf("\tnumber[%d] = %s\n",i,pbc_rmessage_string(p , "number", i ,NULL)); printf("\ttype[%d] = %s\n",i,pbc_rmessage_string(p, "type", i, NULL)); } int n = pbc_rmessage_size(m , "test"); for (i=0;i<n;i++) { printf("test[%d] = %d\n",i, pbc_rmessage_integer(m , "test" , i , NULL)); } printf("tutorial.Ext.test = %d\n", pbc_rmessage_integer(m,"tutorial.Ext.test",0,NULL)); pbc_rmessage_delete(m); }
void testNewRead() { struct pbc_slice slice; read_file("addressbooknew.pb", &slice); //ClientSvrMsg //read_file("ClientSvrMsg.pb", &slice); //ClientSvrMsg if (slice.buffer == NULL) return 1; struct pbc_env * env = pbc_new(); int r = pbc_register(env, &slice); if (r) { printf("Error : %s", pbc_error(env)); return 1; } free(slice.buffer); size_t* size = malloc(sizeof(size_t)); FILE* fp = fopen("test.bin", "rb"); fseek(fp, 0, SEEK_END); *size = ftell(fp); *((size_t *)&slice.len) = ftell(fp); fseek(fp, 0, SEEK_SET); slice.buffer = (unsigned char*)malloc(*size); fread(slice.buffer, slice.len, 1, fp); test_rmessage(env, &slice); pbc_delete(env); }
void testOldWrite() { struct pbc_slice slice; read_file("addressbooknew.pb", &slice); //ClientSvrMsg //read_file("ClientSvrMsg.pb", &slice); //ClientSvrMsg if (slice.buffer == NULL) return 1; struct pbc_env * env = pbc_new(); int r = pbc_register(env, &slice); if (r) { printf("Error : %s", pbc_error(env)); return 1; } free(slice.buffer); struct pbc_wmessage *msg = test_wmessage(env); //struct pbc_wmessage *msg = test_ClientSevermessage(env); pbc_wmessage_buffer(msg, &slice); dump(slice.buffer, slice.len); FILE* f = fopen("test.bin", "wb"); fwrite(slice.buffer, slice.len, 1, f); fclose(f); f = NULL; }
int main() { struct pbc_slice slice; read_file("addressbook.pb", &slice); if (slice.buffer == NULL) return 1; struct pbc_env * env = pbc_new(); int r = pbc_register(env, &slice); if (r) { printf("Error : %s", pbc_error(env)); return 1; } free(slice.buffer); struct pbc_wmessage *msg = test_wmessage(env); pbc_wmessage_buffer(msg, &slice); dump(slice.buffer, slice.len); test_rmessage(env, &slice); pbc_wmessage_delete(msg); pbc_delete(env); return 0; }
int lookup_mpz(mpz_t z, symtab_t tab, const char *key) { const char *data = lookup(tab, key); if (!data) { pbc_error("missing param: `%s'", key); return 1; } mpz_set_str(z, data, 0); return 0; }
int pairing_init_set_buf(pairing_t pairing, const char *input, size_t len) { pbc_param_t par; int res = pbc_param_init_set_buf(par, input, len); if (res) { pbc_error("error initializing pairing"); return 1; } pairing_init_pbc_param(pairing, par); pbc_param_clear(par); return 0; }
void pbc_tweak_use_fp(char *s) { if (!strcmp(s, "naive")) { option_fpinit = field_init_naive_fp; } else if (!strcmp(s, "fast")) { option_fpinit = field_init_fast_fp; } else if (!strcmp(s, "faster")) { option_fpinit = field_init_faster_fp; } else if (!strcmp(s, "mont")) { option_fpinit = field_init_mont_fp; } else { pbc_error("no such Fp implementation: %s", s); } }
static void win32_mpz_random(mpz_t r, mpz_t limit, void *data) { UNUSED_VAR (data); HCRYPTPROV phProv; unsigned int error; if (!CryptAcquireContext(&phProv,NULL,NULL,PROV_RSA_FULL,0)) { error = GetLastError(); if (error == 0x80090016) { //need to create a new keyset if (!CryptAcquireContext(&phProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET)) { pbc_error("Couldn't create CryptContext: %x", (int)GetLastError()); return; } } else { pbc_error("Couldn't create CryptContext: %x", error); return; } } int n, bytecount, leftover; unsigned char *bytes; mpz_t z; mpz_init(z); n = mpz_sizeinbase(limit, 2); bytecount = (n + 7) / 8; leftover = n % 8; bytes = (unsigned char *) pbc_malloc(bytecount); for (;;) { CryptGenRandom(phProv,bytecount,(byte *)bytes); if (leftover) { *bytes = *bytes % (1 << leftover); } mpz_import(z, bytecount, 1, 1, 0, 0, bytes); if (mpz_cmp(z, limit) < 0) break; } CryptReleaseContext(phProv,0); mpz_set(r, z); mpz_clear(z); pbc_free(bytes); }
int lookup_int(int *n, symtab_t tab, const char *key) { mpz_t z; const char *data = lookup(tab, key); if (!data) { pbc_error("missing param: `%s'", key); return 1; } mpz_init(z); mpz_set_str(z, data, 0); *n = mpz_get_si(z); mpz_clear(z); return 0; }
static void test_rmessage(struct pbc_env *env, struct pbc_slice *slice) { struct pbc_rmessage * m = pbc_rmessage_new(env, "test", slice); if (m==NULL) { printf("Error : %s",pbc_error(env)); return; } int phone_n = pbc_rmessage_size(m, "el"); int i; for (i=0;i<phone_n;i++) { struct pbc_rmessage * p = pbc_rmessage_message(m , "el", i); printf("\tint16_min[%d] = %d\n",i,pbc_rmessage_integer(p , "int16_min", i ,NULL)); printf("\tdouble_max[%d] = %f\n",i,pbc_rmessage_real(p, "double_max", i)); printf("\tstring[%d] = %s\n",i,pbc_rmessage_string(p, "str", i, NULL)); } pbc_rmessage_delete(m); }
int main() { struct pbc_slice slice; read_file("performance.pb", &slice); if (slice.buffer == NULL) return 1; struct pbc_env * env = pbc_new(); int r = pbc_register(env, &slice); if (r) { printf("Error : %s", pbc_error(env)); return 1; } free(slice.buffer); clock_t start = clock(); int i; for ( i = 0;i < 1000000;i ++ ) { struct pbc_wmessage *msg = test_wmessage(env); pbc_wmessage_buffer(msg, &slice); //dump(slice.buffer, slice.len); //test_rmessage(env, &slice); //if ( i == 100000 - 1) test_rmessage(env, &slice); pbc_wmessage_delete(msg); } clock_t t = clock() - start; printf( "time cost %f\n", ((float)t)/CLOCKS_PER_SEC ); pbc_delete(env); return 0; }