Ejemplo n.º 1
0
void load_vector(int size, mpq_t *q, const char *vec_name, const char *folder_name) {
  FILE *fp;
  open_file(&fp, vec_name, (char *)"rb", folder_name);
  for (int i=0; i<size; i++) {
    mpz_inp_raw(mpq_numref(q[i]), fp);
    mpz_inp_raw(mpq_denref(q[i]), fp);
  }
  fclose(fp);
}
Ejemplo n.º 2
0
void load_scalar(mpz_t q, const char *scalar_name, const char *folder_name) {
  FILE *fp;
  open_file(&fp, scalar_name, (char *)"rb", folder_name);
  if (fp == NULL) return;
  mpz_inp_raw(q, fp);
  fclose(fp);
}
Ejemplo n.º 3
0
void
read_s_from_file (mpz_t s, char *fn) 
{
  FILE *file;
  int ret = 0;

#ifdef DEBUG
  if (fn == NULL)
    {
      fprintf (stderr, "read_s_from_file: fn == NULL\n");
      exit (EXIT_FAILURE);
    }
#endif
  
  file = fopen (fn, "r");
  if (file == NULL)
    {
      fprintf (stderr, "Could not open file %s for reading\n", fn);
      exit (EXIT_FAILURE);
    }
 
  ret = mpz_inp_raw (s, file);
  if (ret == 0)
    {
      fprintf (stderr, "read_s_from_file: 0 bytes read from %s\n", fn);
      exit (EXIT_FAILURE);
    }

  fclose (file);
}
Ejemplo n.º 4
0
void mpz_inp_wrapper(mpz_t x, FILE * stream)
{
    if (mpz_inp_raw(x, stream) == 0)
    {
        fprintf(stderr, "mpz_imp_raw error.\n");
        exit(EXIT_FAILURE);
    }
}
Ejemplo n.º 5
0
void load_vector(int size, mpz_t *q, const char *vec_name, const char *folder_name) {
  FILE *fp;
  open_file(&fp, vec_name, (char *)"rb", folder_name);
  if (fp == NULL) return;
  for (int i=0; i<size; i++)
    mpz_inp_raw(q[i], fp);
  fclose(fp);
}
Ejemplo n.º 6
0
/* Read all entries in list from stream. 
   Return 0 on success, ECM_ERROR on error */
int
list_inp_raw (listz_t a, FILE *f, unsigned int n)
{
  unsigned int i;
  
  for (i = 0; i < n; i++)
    if (mpz_inp_raw (a[i], f) == 0)
      return ECM_ERROR;
  
  return 0;
}
Ejemplo n.º 7
0
int
newepoch_load(char *in,epoch_item_t *ep,setup_params_t * setup)
{
      FILE *fp = NULL;
      uint32_t elem_size = 0;
      int rv = 0;
      unsigned char *buf = NULL;

      fp=fopen(in,"rb");
      if(!fp) {
          pbgp_error("newepoch_load :: %s\n",strerror(errno));
          return -1;
      }

      fread(&elem_size,sizeof(uint32_t),1,fp);

      buf = (unsigned char *) malloc(elem_size);
      if(!buf) {
          pbgp_error("newepoch_load :: cannot allocate buf\n");
          rv = -1;
          goto out;
      }
      
      fread(buf,elem_size,1,fp);
      element_from_bytes(ep->acc->elem,buf);
      mpz_inp_raw (ep->s_acc,fp); 

      ids_load_fp(fp,ep->epls.act);
      mpz_inp_raw(ep->s_new,fp);
      
      ids_load_fp(fp,ep->epls.rvk);
      mpz_inp_raw(ep->s_rvk,fp);

out:
	if(buf)
		free(buf);

	fclose(fp);
	return rv;
}
Ejemplo n.º 8
0
void bg_decrypt_block(bg_prikey key, FILE *in, FILE *out, mp_size_t len) {
	mpz_t y, xqmp, xpmq, pp, qq, negone;
	unsigned long int l;
	bbs_state *bbs;

	mpz_init(y);
	mpz_init(xqmp);
	mpz_init(xpmq);
	mpz_init(pp);
	mpz_init(qq);
	mpz_init_set_si(negone, -1);
	bbs = init_bbs(negone, key.n);
	bbs_gen(bbs, NULL, 8*len, 0);
	bbs_iter(bbs);
	bbs_close(bbs);
	l = bbs->len;
	mpz_inp_raw(y, in);

	mpz_add_ui(pp, key.p, 1);
	mpz_tdiv_q_ui(pp, pp, 4);
	mpz_pow_ui(pp, pp, l);
	mpz_powm_sec(pp, y, pp, key.p);

	mpz_add_ui(qq, key.q, 1);
	mpz_tdiv_q_ui(qq, qq, 4);
	mpz_pow_ui(qq, qq, l);
	mpz_powm_sec(qq, y, qq, key.q);

	mpz_mul(xqmp, key.q, pp);
	mpz_powm(pp, key.q, negone, key.p);
	mpz_mul(xqmp, xqmp, pp);

	mpz_mul(xpmq, key.p, qq);
	mpz_powm(qq, key.p, negone, key.q);
	mpz_mul(xpmq, xpmq, qq);

	mpz_add(y, xqmp, xpmq);
	mpz_mod(y, y, key.n);

	bbs = init_bbs(y, key.n);
	while(len && !feof(in)) len -= bg_xor(bbs, in, out, len);
	bbs_close(bbs);
}
Ejemplo n.º 9
0
void mpz_urandomb_crypto(mpz_t rop, cryptorand_t state, mp_bitcnt_t n) {
  unsigned long ctr_iv;
  #pragma omp critical(update_iv_counter)
  {
    // update the internal counter, works at most 2^64 times
    ctr_iv = state->ctr++;
  }

  memcpy(state->iv, &ctr_iv, sizeof(ctr_iv)); 
  mp_bitcnt_t nb = n/8+1; // number of bytes

  if(!(state->ctx = EVP_CIPHER_CTX_new())) {
    perror("Error in initializing new cipher context");
    return;
  }
  if(!EVP_EncryptInit_ex(state->ctx, AES_ALGORITHM, NULL, state->key,
        state->iv)) {
    perror("Error in calling EncryptInit");
    return;
  }

  unsigned char *output;
  if(!(output = malloc(2 * (nb + EVP_MAX_IV_LENGTH)))) {
    perror("Error in initializing output buffer");
    return;
  }
  mp_bitcnt_t outlen = 0;

  int in_size = nb;
  unsigned char in[in_size];
  memset(in, 0, in_size);

  while(outlen < nb) {
    int buflen = 0;
    if(!EVP_EncryptUpdate(state->ctx, output+outlen, &buflen, in, in_size)) {
      perror("Error in calling EncryptUpdate");
      goto output_exit;
    }
    outlen += buflen;
  }
  int final_len = 0;
  if(!EVP_EncryptFinal(state->ctx, output+outlen, &final_len)) {
    perror("Error in calling EncryptFinal");
    goto output_exit;
  }
  outlen += final_len;

  if(outlen > nb) {
    outlen = nb; // we will only use nb bytes
  }

  mp_bitcnt_t true_len = outlen + 4;
  mp_bitcnt_t bytelen = outlen;

  unsigned char *buf;
  if(!(buf = malloc(true_len))) {
    perror("Error in initializing buf");
    goto output_exit;
  }
  memset(buf, 0, true_len);
  memcpy(buf+4, output, outlen);
  buf[4] >>= ((outlen*8) - (unsigned int) n);

  for(int i = 3; i >= 0; i--) {
    buf[i] = (unsigned char) (bytelen % (1 << 8));
    bytelen /= (1 << 8);
  }

  // generate a random n-bit number
  FILE *fp;
  if(!(fp = fmemopen(buf, true_len, "rb"))) {
    perror("Error in calling fmemopen");
    goto buf_exit;
  }

  if(!mpz_inp_raw(rop, fp)) {
    fprintf(stderr, "Error in parsing randomness.\n");
  }

  fclose(fp); 

buf_exit:
  free(buf); 

output_exit:
  free(output);

  EVP_CIPHER_CTX_cleanup(state->ctx);
  free(state->ctx);

}
Ejemplo n.º 10
0
/*!
  Loads the memory.

  \param path the file containing the data dumped by syx_memory_save_image
  \return FALSE if an error occurred
*/
syx_bool
syx_memory_load_image (syx_symbol path)
{
  SyxObject *object;
  FILE *image;
  syx_int32 data;
  syx_int32 i;
  SyxMemoryLazyPointer *lazy;
  
  SYX_START_PROFILE;

  if (!path)
    {
      if (SYX_IS_NIL (syx_globals))
        path = syx_get_image_path ();
      else
        path = SYX_OBJECT_SYMBOL (syx_globals_at ("ImageFileName"));
    }

  if (!path)
    return FALSE;

  image = fopen (path, "rb");
  if (!image)
    return FALSE;

  fread (&data, sizeof (syx_int32), 1, image);
  data = SYX_COMPAT_SWAP_32 (data);
  syx_memory_init (data);

  fread (&data, sizeof (syx_int32), 1, image);
  _syx_freed_memory_top = SYX_COMPAT_SWAP_32 (data);
  _syx_memory_read (_syx_freed_memory, FALSE, _syx_freed_memory_top, image);

  _syx_scheduler_load (image);

  _syx_memory_read (&syx_globals, FALSE, 1, image);
  _syx_memory_read (&syx_symbols, FALSE, 1, image);

  while (!feof (image))
    {
      if (!_syx_memory_read ((SyxOop *)&object, FALSE, 1, image))
        break;

      _syx_memory_read (&object->klass, FALSE, 1, image);
      object->has_refs = fgetc (image);
      object->is_constant = fgetc (image);

      /* fetch instance variables */
      fread (&data, sizeof (syx_varsize), 1, image);
      data = SYX_COMPAT_SWAP_32 (data);
      if (object->vars)
        syx_free (object->vars);
      object->vars = (SyxOop *) syx_calloc (data, sizeof (SyxOop));
      _syx_memory_read (object->vars, TRUE, data, image);

      /* fetch data */
      fread (&data, sizeof (syx_varsize), 1, image);
      object->data_size = SYX_COMPAT_SWAP_32 (data);
      if (object->data_size > 0)
        {
          if (object->data && object->data_size > 0)
            syx_free (object->data);
          
          if (object->has_refs)
            {
              object->data = (SyxOop *) syx_calloc (object->data_size, sizeof (SyxOop));
              _syx_memory_read (object->data, TRUE, object->data_size, image);
            }
          else
            {
              object->data = (SyxOop *) syx_calloc (object->data_size, sizeof (syx_int8));
              if (fgetc (image) == SYX_MEMORY_TYPE_LARGE_INTEGER)
                {
                  fread (&data, sizeof (syx_int32), 1, image);
                  data = SYX_COMPAT_SWAP_32 (data);
#ifdef HAVE_LIBGMP
                  mpz_init (SYX_OBJECT_LARGE_INTEGER ((SyxOop)object));
                  mpz_inp_raw (SYX_OBJECT_LARGE_INTEGER ((SyxOop)object), image);
#else
                  /* skip GMP data since we can't handle it */
                  fseek (image, data, SEEK_CUR);
#endif
                }
              else
                fread (object->data, sizeof (syx_int8), object->data_size, image);
            }
        }
    }
  
  fclose (image);
 
  /* Fix lazy pointers */
  for (i=0; i < _syx_memory_lazy_pointers_top; i++)
    {
      lazy = &_syx_memory_lazy_pointers[i];
      if (lazy->stack != 0)
        *lazy->entry = SYX_POINTER_CAST_OOP (SYX_OBJECT_DATA (lazy->stack) + lazy->offset);
      else
        *lazy->entry = SYX_POINTER_CAST_OOP (NULL);
    }
  syx_free (_syx_memory_lazy_pointers);
  _syx_memory_lazy_pointers_top = 0;

  syx_fetch_basic ();

  SYX_END_PROFILE(load_image);

  syx_initialize_system ();

  return TRUE;
}
Ejemplo n.º 11
0
void load_scalar_from_fp(FILE *fp, mpz_t s) {
  mpz_inp_raw(s, fp);
}
Ejemplo n.º 12
0
void Verifier::prepare_answers(int beta) {
#if NONINTERACTIVE == 1
  prepare_noninteractive_answers(beta);
#else
#if VERBOSE == 1
  cout << endl << "LOG: Batch " << beta << endl;
#endif

  FILE *fp = NULL;
  if (optimize_answers) {
    char f_name[BUFLEN];
    snprintf(f_name, BUFLEN - 1, "answers_%d", beta + 1);
    snprintf(f_name, BUFLEN - 1, "%s/answers_%d",
             FOLDER_STATE, beta + 1);
    fp = fopen(f_name, "rb");
    if (fp == NULL) {
      printf("Failed to open %s file for reading.", f_name);
      exit(1);
    }
  }

  // only one commitment answer and one consistency answer
  string commitment_answer_str = "f_commitment_answer_b_%d";
  snprintf(scratch_str, BUFLEN - 1, commitment_answer_str.c_str(), beta);
  load_vector(expansion_factor, temp_arr_ptrs[0], scratch_str);

  if (!optimize_answers) {
    string consistency_answer_str = "f_consistency_answer_b_%d";
    snprintf(scratch_str, BUFLEN - 1, consistency_answer_str.c_str(), beta);
    load_scalar(a, scratch_str);
  } else {
    size_t bytes = mpz_inp_raw(a, fp);
    fseek(fp, bytes, SEEK_CUR);
  }

  for (uint32_t j=0; j<num_repetitions*num_lin_pcp_queries; j++)
    mpz_set_ui(f_answers[j], 0);

  std::list<string> files = get_files_in_dir((char *)FOLDER_STATE);
  for (int rho = 0; rho < num_repetitions; rho++) {
    if (!optimize_answers) {
      char *file_exp = (char *)"_qquery_answer_b_%d_r_%d";
      snprintf(scratch_str, BUFLEN-1, file_exp, beta, rho);
      std::list<string>::const_iterator it = files.begin();

      for (; it != files.end(); it++) {
        string file_name = *it;
        size_t b_index = file_name.find(scratch_str);
        if (b_index != string::npos) {
          int q_num = atoi(file_name.substr(file_name.find("q") + 1, b_index).c_str());
          load_scalar(f_answers[rho * num_lin_pcp_queries + Q_list[q_num - 1]],
                      (char*)file_name.c_str());
        }
      }
    } else {
      for (uint32_t q = 0; q < Q_list.size(); q++) {
        size_t bytes =
          mpz_inp_raw(f_answers[rho * num_lin_pcp_queries + Q_list[q]], fp);
        fseek(fp, bytes, SEEK_CUR);
      }
    }
    //populate_answers(f_answers, rho, num_repetitions, beta);
  }

  snprintf(scratch_str, BUFLEN-1, "input_b_%d", beta);
  load_vector(size_input, input, scratch_str);

  snprintf(scratch_str, BUFLEN-1, "output_b_%d", beta);
  load_vector(size_output, output, scratch_str);

  if (output_q != NULL) {
    snprintf(scratch_str, BUFLEN-1, "output_q_b_%d", beta);
    load_vector(size_output, output_q, scratch_str);
    if (verify_conversion_to_z(size_output, output, output_q, prime) == false) {
      cout<<"LOG: Prover presented two different versions of output"<<endl;
      exit(1);
    } 
  }

  if (fp != NULL)
    fclose(fp);
#endif
}