Ejemplo n.º 1
0
int
bench_param_set(PQ_PARAM_SET_ID id)
{
  int i;

  int valid = 0;

  PQ_PARAM_SET *P;
  size_t privkey_blob_len;
  size_t pubkey_blob_len;

  unsigned char *privkey_blob;
  unsigned char *pubkey_blob;

  unsigned char msg[256] = {0};
  uint16_t msg_len = 256;

  unsigned char *sigs;
  size_t packed_sig_len;


  clock_t c0;
  clock_t c1;

  rng_init();
  if(!(P = pq_get_param_set_by_id(id)))
  {
    exit(EXIT_FAILURE);
  }

  fprintf(stderr, "------ Testing parameter set %s. %d trials. ------\n", P->name, TRIALS);
  printf("------ Testing parameter set %s. %d trials. ------\n", P->name, TRIALS);

  pq_gen_key(P, &privkey_blob_len, NULL, &pubkey_blob_len, NULL);

  printf("privkey_blob_len: %d\n", (int) privkey_blob_len);
  printf("pubkey_blob_len: %d\n", (int) pubkey_blob_len);

  privkey_blob = malloc(privkey_blob_len);
  pubkey_blob = malloc(pubkey_blob_len);

  c0 = clock();
  for(i=0; i<TRIALS; i++) {
    msg[(i&0xff)]++; /* Hash a different message each time */
    pq_gen_key(P, &privkey_blob_len, privkey_blob,
               &pubkey_blob_len, pubkey_blob);
  }
  c1 = clock();
  printf("Time/key: %fs\n", (float) (c1 - c0)/(TRIALS*CLOCKS_PER_SEC));

  pq_sign(&packed_sig_len, NULL, privkey_blob_len, privkey_blob, pubkey_blob_len, pubkey_blob, 0, NULL);
//  cuda_pq_sign(&packed_sig_len, NULL, privkey_blob_len, privkey_blob, pubkey_blob_len, pubkey_blob, 0, NULL); // !!!cuda
  printf("packed_sig_len %d\n", (int)packed_sig_len);

  sigs = malloc(TRIALS * packed_sig_len);

  memset(msg, 0, 256);

  valid = 0;
//  attempts = 0;
//  t_prep = 0;
//  t_dowhile = 0;
//  t_writeback = 0;

  cuda_prep(id);

  c0 = clock();
  for(i=0; i<TRIALS; i++) {
    msg[(i&0xff)]++; /* Hash a different message each time */
/*    valid += (PQNTRU_OK == pq_sign(&packed_sig_len, sigs + (i*packed_sig_len),
                                   privkey_blob_len, privkey_blob,
                                   pubkey_blob_len, pubkey_blob,
                                   msg_len, msg));
*/ 
    valid += (PQNTRU_OK == 
        cuda_pq_sign( &packed_sig_len, sigs + (i*packed_sig_len),
                      privkey_blob_len,
                      privkey_blob,
                      pubkey_blob_len,
                      pubkey_blob,
                      msg_len, msg ) ); // !!! cuda
  }
  c1 = clock();

  cuda_clean();

//  printf("Probability of validity: %f\n", (double)valid/attempts);
  printf("Time/signature: %f msec\n", 1000*(double)(c1 - c0)/(TRIALS*CLOCKS_PER_SEC));
//  printf("\tInputTime/signature: %f msec\n", t_prep/TRIALS);
//  printf("\tSigningTime/signature: %f msec\n", t_dowhile/TRIALS);
//  printf("\t\tSigningTime/attempt: %f msec\n", t_dowhile*numBlocks/attempts);
//  printf("\tOutputTime/signature: %f msec\n", t_writeback/TRIALS);
  printf("Good signatures %d/%d\n", valid, TRIALS);

  memset(msg, 0, 256);
  valid = 0;
  c0 = clock();
  for(i=0; i<TRIALS; i++) {
    msg[(i&0xff)]++;
    valid += (PQNTRU_OK == pq_verify(packed_sig_len, sigs + (i*packed_sig_len),
                                     pubkey_blob_len, pubkey_blob,
                                     msg_len, msg));
  }
  c1 = clock();
  printf("Time/verification: %fs\n", (float) (c1 - c0)/(TRIALS*CLOCKS_PER_SEC));
  printf("Verified %d/%d\n\n\n", (int)valid, (int)TRIALS);


  free(sigs);
  free(privkey_blob);
  free(pubkey_blob);

  rng_cleanup();

  return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
static int
testSet(PQ_PARAM_SET_ID id)
{
  uint16_t i;

  PQ_PARAM_SET *P;
  size_t privkey_blob_len;
  size_t pubkey_blob_len;
  unsigned char *privkey_blob;
  unsigned char *pubkey_blob;

  unsigned char *sigs;

  uint16_t msg_len = 256;
  unsigned char *msg;

  int result = 0;

  if(!(P = pq_get_param_set_by_id(id)))
  {
    return -1;
  }
  fprintf(stderr, "Testing parameter set %s", P->name);
  fflush(stderr);

  pq_gen_key(P, &privkey_blob_len, NULL, &pubkey_blob_len, NULL);

  privkey_blob = malloc(TIMES * privkey_blob_len);
  pubkey_blob = malloc(TIMES * pubkey_blob_len);

  msg = malloc(TIMES * msg_len * sizeof(int64_t));
  memset(msg, 0, TIMES*msg_len*sizeof(int64_t));


  for(i=0; i<TIMES; i++)
  {
    if(PQNTRU_ERROR == pq_gen_key(P,
               &privkey_blob_len, privkey_blob + (i*privkey_blob_len),
               &pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len)))
    {
      result = -1;
      fprintf(stderr, "\t fail in keygen\n");
      goto exit_kg;
    }
  }

  size_t packed_sig_len;
  pq_sign(&packed_sig_len, NULL, privkey_blob_len, privkey_blob, pubkey_blob_len, pubkey_blob, 0, NULL);
  sigs = malloc(TIMES * packed_sig_len);

  for(i=0; i<TIMES; i++)
  {
    fastrandombytes(msg+(i*msg_len), msg_len);
    if(PQNTRU_ERROR == pq_sign( &packed_sig_len, sigs + (i*packed_sig_len),
                               privkey_blob_len, privkey_blob + (i*privkey_blob_len),
                                pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len),
                                        msg_len, msg + (i*msg_len)))
    {
      result = -1;
      fprintf(stderr, "\t fail in sign\n");
      goto exit;
    }
  }

  for(i=0; i<TIMES; i++)
  {
    if(PQNTRU_ERROR == pq_verify( packed_sig_len, sigs + (i*packed_sig_len),
                                 pubkey_blob_len, pubkey_blob + (i*pubkey_blob_len),
                                         msg_len, msg + (i*msg_len)))
    {
      result = -1;
      fprintf(stderr, "\t fail in verify\n");
      goto exit;
    }
  }

  fprintf(stderr, "\t good\n");

exit:
  free(sigs);
exit_kg:
  free(msg);
  free(privkey_blob);
  free(pubkey_blob);
  return result;
}
Ejemplo n.º 3
0
int
bench_param_set(PQ_PARAM_SET_ID id)
{
  int i,j;

  int valid = 0;

  PQ_PARAM_SET *P;
  size_t privkey_blob_len;
  size_t pubkey_blob_len;

  unsigned char *privkey_blob;
  unsigned char *pubkey_blob;

  unsigned char msg[256] = {0};
  uint16_t msg_len = 256;

  unsigned char *sigs;
  size_t packed_sig_len;

  uint64_t      *pre_processing;
  size_t        no_of_data  =   TRIALS*10;

  clock_t c0;
  clock_t c1;

  rng_init();
  if(!(P = pq_get_param_set_by_id(id)))
  {
    exit(EXIT_FAILURE);
  }

  fprintf(stderr, "Testing parameter set %s. %d trials.\n", P->name, TRIALS);

  pq_gen_key(P, &privkey_blob_len, NULL, &pubkey_blob_len, NULL);

  //printf("privkey_blob_len: %d\n", (int) privkey_blob_len);
  //printf("pubkey_blob_len: %d\n", (int) pubkey_blob_len);

  privkey_blob      = malloc(privkey_blob_len);
  pubkey_blob       = malloc(pubkey_blob_len);
  pre_processing    = malloc(2 * no_of_data * POLYNOMIAL_BYTES(P));

  memset (pre_processing, 0, 2 * no_of_data * POLYNOMIAL_BYTES(P));

  c0 = clock();
  for(i=0; i<TRIALS; i++) {
    msg[(i&0xff)]++; /* Hash a different message each time */
    pq_gen_key(P, &privkey_blob_len, privkey_blob,
               &pubkey_blob_len, pubkey_blob);
  }
  c1 = clock();
  printf("Time/key: %fs\n", (float) (c1 - c0)/(TRIALS*CLOCKS_PER_SEC));

  pq_sign(&packed_sig_len, NULL, privkey_blob_len, privkey_blob, pubkey_blob_len, pubkey_blob, 0, NULL);
  printf("packed_sig_len %d\n", (int)packed_sig_len);

  sigs = malloc(TRIALS * packed_sig_len);

  memset(msg, 0, 256);
  valid = 0;


  /* pre_compute the data for signing */
  c0 = clock();
  pq_pre_process  (pre_processing,   no_of_data,
                   privkey_blob_len, privkey_blob,
                   pubkey_blob_len,  pubkey_blob);
  c1 = clock();
  printf("Time/pre_data: %fs\n", (float) (c1 - c0)/(no_of_data*CLOCKS_PER_SEC));
  c0 = clock();

  for(i=0; i<TRIALS; i++) {


    msg[(i&0xff)]++;    /* Hash a different message each time */
    valid += (PQNTRU_OK == pq_sign_pp(&packed_sig_len, sigs + (i*packed_sig_len),
                                       no_of_data, pre_processing,
                                       privkey_blob_len, privkey_blob,
                                       pubkey_blob_len, pubkey_blob,
                                       msg_len, msg));
  }
  c1 = clock();
  printf("Time/signature: %fs\n", (float) (c1 - c0)/(TRIALS*CLOCKS_PER_SEC));
  printf("Good signatures %d/%d\n", valid, TRIALS);
  printf("avg loop %f\n", ((float)TRIALS)/g_loop);
  printf("max |a*f| %d/%d\n", g_max, (int) P->B_s);
  printf("max |a*g| %d/%d\n", g_max2, (int) P->B_t);


  memset(msg, 0, 256);
  valid = 0;
  c0 = clock();
  for(i=0; i<TRIALS; i++) {
    msg[(i&0xff)]++;
    valid += (PQNTRU_OK == pq_verify(packed_sig_len, sigs + (i*packed_sig_len),
                                     pubkey_blob_len, pubkey_blob,
                                     msg_len, msg));
  }
  c1 = clock();
  printf("Time/verification: %fs\n", (float) (c1 - c0)/(TRIALS*CLOCKS_PER_SEC));
  printf("Verified %d/%d\n\n\n", (int)valid, (int)TRIALS);


  free(sigs);
  free(privkey_blob);
  free(pubkey_blob);

  rng_cleanup();

  return EXIT_SUCCESS;
}