Example #1
0
int main(void) {
  mpz_t p, q, N, d;
  mpz_t dmp1, dmq1;
  mpz_t ipmq, iqmp;
  mpz_t adq, adp;

  field_t f;
  element_t a, b;
  double t0, t1, tnaive = 0, tcrt=0;
  int i, n;

  mpz_init(p);
  mpz_init(q);
  mpz_init(N);
  mpz_init(d);
  mpz_init(dmp1);
  mpz_init(dmq1);
  mpz_init(ipmq);
  mpz_init(iqmp);
  mpz_init(adp);
  mpz_init(adq);
  pbc_mpz_randomb(p, 512);
  pbc_mpz_randomb(q, 512);
  mpz_nextprime(p, p);
  mpz_nextprime(q, q);
  mpz_mul(N, p, q);
  mpz_invert(ipmq, p, q);
  mpz_invert(iqmp, q, p);

  field_init_fp(f, N);
  element_init(a, f);
  element_init(b, f);
  n = 10;
  for (i=0; i<n; i++) {
    pbc_mpz_random(d, N);
    element_random(a);
    t0 = pbc_get_time();
    element_pow_mpz(b, a, d);
    t1 = pbc_get_time();
    tnaive += t1 - t0;

    mpz_sub_ui(p, p, 1);
    mpz_sub_ui(q, q, 1);

    mpz_mod(dmp1, d, p);
    mpz_mod(dmq1, d, q);

    mpz_add_ui(p, p, 1);
    mpz_add_ui(q, q, 1);

    element_to_mpz(adq, a);
    element_to_mpz(adp, a);

    t0 = pbc_get_time();
    mpz_powm(adp, adp, d, p);
    mpz_powm(adq, adq, d, q);

    /* textbook CRT
    mpz_mul(adp, adp, q);
    mpz_mul(adp, adp, iqmp);
    mpz_mul(adq, adq, p);
    mpz_mul(adq, adq, ipmq);
    mpz_add(adp, adp, adq);
    */
    // Garner's algorithm
    mpz_sub(adq, adq, adp);
    mpz_mul(adq, adq, ipmq);
    mpz_mod(adq, adq, q);
    mpz_mul(adq, adq, p);
    mpz_add(adp, adp, adq);

    t1 = pbc_get_time();
    tcrt += t1 - t0;
    element_set_mpz(b, adp);
  }
  printf("average RSA exp time = %lf\n", tnaive / n);
  printf("average RSA exp time (CRT) = %lf\n", tcrt / n);
  return 0;
}
Example #2
0
int main(int argc, char **argv) {
  pairing_t pairing;
  double time1, time2;
  element_t P, a, b, c, Ka, Kb, Kc, t1, t2, t3, t4, t5, t6;
  pbc_demo_pairing_init(pairing, argc, argv);
  if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");

  element_init_G1(P, pairing);
  element_init_G1(t1, pairing);
  element_init_G1(t2, pairing);
  element_init_G1(t3, pairing);

  element_init_Zr(a, pairing);
  element_init_Zr(b, pairing);
  element_init_Zr(c, pairing);

  element_init_GT(t4, pairing);
  element_init_GT(t5, pairing);
  element_init_GT(t6, pairing);
  element_init_GT(Ka, pairing);
  element_init_GT(Kb, pairing);
  element_init_GT(Kc, pairing);

  time1 = pbc_get_time();
  printf("Joux key agreement between A, B and C.\n");
  element_random(P);
  element_random(a);
  element_random(b);
  element_random(c);
  element_mul_zn(t1, P, a);
  printf("A sends B and C: aP\n");
  element_printf("aP = %B\n", t1);
  element_mul_zn(t2, P, b);
  printf("B sends A and C: bP\n");
  element_printf("bP = %B\n", t2);
  element_mul_zn(t3, P, c);
  printf("C sends A and B: cP\n");
  element_printf("cP = %B\n", t3);

  element_pairing(t4, t2, t3);
  element_pow_zn(Ka, t4, a);
  element_printf("Ka = %B\n", Ka);
  element_pairing(t5, t1, t3);
  element_pow_zn(Kb, t5, b);
  element_printf("Kb = %B\n", Kb);
  element_pairing(t6, t1, t2);
  element_pow_zn(Kc, t6, c);
  element_printf("Kc = %B\n", Kc);

  printf("Shared key K = Ka = Kb = Kc\n");
  time2 = pbc_get_time();
  printf("All time = %fs\n", time2 - time1);


  element_clear(P);
  element_clear(a);
  element_clear(b);
  element_clear(c);
  element_clear(Ka);
  element_clear(Kb);
  element_clear(Kc);
  element_clear(t1);
  element_clear(t2);
  element_clear(t3);
  element_clear(t4);
  element_clear(t5);
  element_clear(t6);
  pairing_clear(pairing);

  return 0;
}
Example #3
0
int main(int argc, char **argv) {
  pairing_t pairing;
  element_t g1, u1, up1, g2, u2, up2, r;
  mpz_t r_mpz;
  element_pp_t g1_pp, g2_pp;
  double t0, t1;
  int i, n;

  printf("reading pairing from stdin...\n");
  pbc_demo_pairing_init(pairing, argc, argv);

  element_init(r, pairing->Zr);
  element_init(g1, pairing->G1);
  element_init(u1, pairing->G1);
  element_init(up1, pairing->G1);
  element_init(g2, pairing->G2);
  element_init(u2, pairing->G2);
  element_init(up2, pairing->G2);

  element_random(r);
  element_random(g1);
  element_random(g2);

  mpz_init(r_mpz);
  element_to_mpz(r_mpz, r);

  element_pp_init(g1_pp, g1);
  element_pp_init(g2_pp, g2);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pow_mpz(u1, g1, r_mpz);
  }
  t1 = pbc_get_time();
  printf("G1 exp:\t\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pow_mpz(u2, g2, r_mpz);
  }
  t1 = pbc_get_time();
  printf("G2 exp:\t\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pp_pow(up1, r_mpz, g1_pp);
  }
  t1 = pbc_get_time();
  printf("G1 pp exp:\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pp_pow(up2, r_mpz, g2_pp);
  }
  t1 = pbc_get_time();
  printf("G2 pp exp:\t%fs\n", t1 - t0);

  if (element_cmp(u1, up1)) {
    printf("Oops 1!\n");
  }
  if (element_cmp(u2, up2)) {
    printf("Oops 2!\n");
  }

  mpz_clear(r_mpz);
  element_clear(g1);
  element_clear(u1);
  element_clear(up1);
  element_clear(g2);
  element_clear(u2);
  element_clear(up2);
  element_clear(r);
  element_pp_clear(g1_pp);
  element_pp_clear(g2_pp);
  pairing_clear(pairing);

  return 0;
}
Example #4
0
int main(int argc, char **argv) {
  pairing_t pairing;
  pbc_demo_pairing_init(pairing, argc, argv);
  if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");
  double time1, time2;
  element_t P, Ppub, x, S, H, t1, t2, t3, t4;
  element_init_Zr(x, pairing);
  element_init_Zr(H, pairing);
  element_init_Zr(t1, pairing);

  element_init_G1(S, pairing);
  element_init_G1(P, pairing);
  element_init_G1(Ppub, pairing);
  element_init_G1(t2, pairing);

  element_init_GT(t3, pairing);
  element_init_GT(t4, pairing);

  printf("ZSS short signature schema\n");
  printf("KEYGEN\n");
  time1 = pbc_get_time();
  element_random(x);
  element_random(P);
  element_mul_zn(Ppub, P, x);
  element_printf("P = %B\n", P);
  element_printf("x = %B\n", x);
  element_printf("Ppub = %B\n", Ppub);

  printf("SIGN\n");
  element_from_hash(H, "Message", 7);
  element_add(t1, H, x);
  element_invert(t1, t1);
  element_mul_zn(S, P, t1);
  printf("Signature of message \"Message\" is:\n");
  element_printf("S = %B\n", S);

  printf("VERIFY\n");
  element_from_hash(H, "Message", 7);
  element_mul_zn(t2, P, H);
  element_add(t2, t2, Ppub);
  element_pairing(t3, t2, S);
  element_pairing(t4, P, P);
  element_printf("e(H(m)P + Ppub, S) = %B\n", t3);
  element_printf("e(P, P) = %B\n", t4);
  if (!element_cmp(t3, t4)) printf("Signature is valid\n");
  else printf("Signature is invalid\n");
  time2 = pbc_get_time();
  printf("All time = %fs\n", time2 - time1);
  element_clear(P);
  element_clear(Ppub);
  element_clear(x);
  element_clear(S);
  element_clear(H);
  element_clear(t1);
  element_clear(t2);
  element_clear(t3);
  element_clear(t4);
  pairing_clear(pairing);

  return 0;
}
Example #5
0
int main(int argc, char **argv) {
	pairing_t pairing;
	element_t g1;
	element_t s, r;
	element_t id;
	element_t pri_id;
	element_t gID;
	element_t Ppub;
	element_t C1;
	element_t gIDr;
	
	pbc_demo_pairing_init(pairing, argc, argv);
	element_init_G1(g1, pairing);
	element_init_Zr(s, pairing);
	element_init_Zr(r, pairing);
	element_init_G1(id, pairing);
	element_init_G1(pri_id, pairing);
	element_init_GT(gID, pairing);
	element_init_G1(Ppub, pairing);
	element_init_G1(C1, pairing);
	element_init_GT(gIDr, pairing);
	
	double t0, t1, ttotal, ttotalpp;
	element_random(g1);
	element_random(s);
	element_random(r);
	element_random(id);
	element_pow_zn(Ppub, g1, s);
	ttotal = 0.0;
	t0 = pbc_get_time();
	element_pow_zn(pri_id, id, s);
	element_pairing(gID, id, Ppub);
	element_pow_zn(gIDr, gID, r);
	t1 = pbc_get_time();
	
	int i = 0;
	int n = 1000;
	
	for(i; i<n; i++)
	{	
		t0 = pbc_get_time();
		element_pow_zn(C1, g1, r);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("if we only have one user and the user is cached, average enc time = %f\n", ttotal / n);

	i = 0;
	ttotal = 0.0;
	for(i; i<n; i++)
	{	
		element_random(id);
		t0 = pbc_get_time();
		element_pow_zn(pri_id, id, s);
		element_pairing(gID, id, Ppub);
		element_pow_zn(gIDr, gID, r);
		element_pow_zn(C1, g1, r);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("100 diff users, average enc time = %f\n", ttotal / n);
	
	

	//dec
	ttotal = 0.0; 
	element_t gIDr_prime;
	element_init_GT(gIDr_prime, pairing);
		i = 0;
	
	for(i; i<n; i++)
	{	
		element_random(pri_id);
		element_random(C1);
		t0 = pbc_get_time();
		element_pairing(gIDr_prime, pri_id,  C1);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("dec random messages, average dec time = %f\n", ttotal / n);
	

	/*if (!element_cmp(gIDr_prime, gIDr)) {
    	printf("----------Confirm----------------\n");}
  	else{
    	printf("----------damn--------\n");
   	}*/
	
}