void dump_vector_interleaved(int size, const mpz_t *q, const char *vec_name, const char *folder_name) { FILE *fp; open_file(&fp, vec_name, (char *)"wb", folder_name); if (fp == NULL) return; for (int i=0; i<size/2; i++) mpz_out_raw(fp, q[2*i]); for (int i=0; i<size/2; i++) mpz_out_raw(fp, q[2*i+1]); fclose(fp); }
bool dump_vector(int size, const mpq_t *q, const char *vec_name, const char *folder_name) { FILE *fp; open_file(&fp, vec_name, (char *)"wb", folder_name); if (!fp) return false; for (int i=0; i<size; i++) { mpz_out_raw(fp, mpq_numref(q[i])); mpz_out_raw(fp, mpq_denref(q[i])); } fclose(fp); return true; }
void dump_scalar(const mpz_t q, char *scalar_name, const char *folder_name) { FILE *fp; open_file(&fp, scalar_name, (char *)"wb", folder_name); if (fp == NULL) return; mpz_out_raw(fp, q); fclose(fp); }
/* Return the number of bytes written */ int write_s_in_file (char *fn, mpz_t s) { FILE *file; int ret = 0; #ifdef DEBUG if (fn == NULL) { fprintf (stderr, "write_s_in_file: fn == NULL\n"); exit (EXIT_FAILURE); } #endif file = fopen (fn, "w"); if (file == NULL) { fprintf (stderr, "Could not open file %s for writing\n", fn); return 0; } ret = mpz_out_raw (file, s); fclose (file); return ret; }
void mpz_out_wrapper(FILE * stream, mpz_t x) { if (mpz_out_raw(stream, x) == 0) { fprintf(stderr, "mpz_out_raw error.\n"); exit(EXIT_FAILURE); } }
/* Write all entries in list to stream. Return 0 on success, ECM_ERROR on error */ int list_out_raw (FILE *f, listz_t a, unsigned int n) { unsigned int i; for (i = 0; i < n; i++) if (mpz_out_raw (f, a[i]) == 0) return ECM_ERROR; return 0; }
bool dump_vector(int size, const mpz_t *q, const char *vec_name, const char *folder_name) { FILE *fp; open_file(&fp, vec_name, (char *)"wb", folder_name); if (fp == NULL) return false; //cout << "WRITING TO FILE: " << folder_name << vec_name << endl; for (int i=0; i<size; i++) mpz_out_raw(fp, q[i]); fclose(fp); return true; }
int newepoch_save(char *out,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(out,"wb"); if(!fp) { pbgp_error("newepoch_save :: %s\n",strerror(errno)); return -1; } elem_size = element_length_in_bytes(ep->acc->elem); buf = (unsigned char *) malloc(elem_size); if(!buf) { pbgp_error("newepoch_save :: cannot allocate buf\n"); rv = -1; goto out; } element_to_bytes(buf,ep->acc->elem); fwrite(&elem_size,sizeof(uint32_t),1,fp); fwrite(buf,elem_size,1,fp); mpz_out_raw(fp,ep->s_acc); ids_save_fp(fp,ep->epls.act); mpz_out_raw(fp,ep->s_new); ids_save_fp(fp,ep->epls.rvk); mpz_out_raw(fp,ep->s_rvk); out: if(buf) free(buf); fclose(fp); return rv; }
void readline_cb(char* line, unsigned int len) { size_t write_len = 0; unsigned int c = 0; unsigned int c_start = 0; unsigned int c_end = 0; int is_rsa = 0; long int keylength = 0; mpz_t key; // Parse the line. for (c_end = 0; c_end<=len; c_end++) { if (line[c_end] == ',' || c_end == len) { line[c_end] = '\0'; // Check if the second column contains "rsaEncryption". if (c == 1) { if (strncmp(line+c_start, "rsaEncryption", c_end-c_start) == 0) { is_rsa = 1; } // Store the fourth column as `mpz_t`. } else if(is_rsa && c == 4) { mpz_init(key); mpz_set_str(key, line+c_start+2, 16); // Store the tenth column as the key length. } else if(is_rsa && c == 9) { keylength = strtol(line+c_start, NULL, 0); } c++; c_start = c_end+1; } } // If the is an RSA key and the key length condition is meet write the // key in raw gmp format to `out`. if (is_rsa && keylength >= min_length && keylength <= max_length) { write_len = mpz_out_raw(out, key); if (write_len == 0) { fprintf(stderr, "Cannot write to file.\n"); } else { key_count++; } } // Free the memory. if (is_rsa) { mpz_clear(key); } }
int main(void) { FILE* output = fopen("aencoded.bin", "w"); int l = {SIZE}; fwrite(&l, sizeof(int), 1, output); mpz_t number; rand_trit(); char total[SIZE + 1]; total[SIZE] = '\0'; for(int i = 0; i < SIZE; i++) { total[i] = mainarray[i] + '0'; } mpz_init_set_str(number, total, 3); mpz_out_raw (output, number); fclose(output); mpz_clear(number); struct stat st; stat("aencoded.bin", &st); int size = st.st_size; printf("%d\n", size); }
void bg_encrypt_block(bg_pubkey key, FILE *in, FILE *out, mp_size_t len) { mpz_t r; bbs_state *bbs; mp_bitcnt_t blen; mpz_init(r); blen = mpz_sizeinbase(key.n, 2); do { if(rand_bigint(r, blen)) { fprintf(stderr, "Failed to write, can't start block.\n"); return; } } while(mpz_cmp_ui(r, 1) < 0 || mpz_cmp(key.n, r) < 0); bbs = init_bbs(r, key.n); bbs_gen(bbs, NULL, 8*len, 0); bbs_iter(bbs); mpz_out_raw(out, bbs->obj); bbs_close(bbs); bbs = init_bbs(r, key.n); while(len && !feof(in)) len -= bg_xor(bbs, in, out, len); bbs_close(bbs); }
/*! Dumps all the memory. \param path the file path to put all inside \return FALSE if an error occurred */ syx_bool syx_memory_save_image (syx_symbol path) { SyxObject *object; FILE *image; syx_int32 data = 0; SyxObject *stack; SyxOop process; if (!path) path = SYX_OBJECT_SYMBOL (syx_globals_at ("ImageFileName")); if (!path) return FALSE; image = fopen (path, "wb"); if (!image) return FALSE; syx_memory_gc (); data = SYX_COMPAT_SWAP_32 (_syx_memory_size); fwrite (&data, sizeof (syx_int32), 1, image); data = SYX_COMPAT_SWAP_32 (_syx_freed_memory_top); fwrite (&data, sizeof (syx_int32), 1, image); _syx_memory_write (_syx_freed_memory, FALSE, _syx_freed_memory_top, image); _syx_scheduler_save (image); _syx_memory_write (&syx_globals, FALSE, 1, image); _syx_memory_write (&syx_symbols, FALSE, 1, image); /* First store the processes */ process = syx_processor_active_process; if (!SYX_IS_NIL (process)) { do { _syx_memory_write_process_stack (SYX_OBJECT (process), image); process = SYX_PROCESS_NEXT (process); } while (SYX_OOP_NE (process, syx_processor_active_process)); } for (object=syx_memory; object <= SYX_MEMORY_TOP; object++) { /* the mark check is not related to the GC but means the object has been already written */ if (SYX_IS_NIL (object->klass) || object->is_marked) continue; _syx_memory_write_object_with_vars (object, image); /* store data */ data = SYX_COMPAT_SWAP_32 (object->data_size); fwrite (&data, sizeof (syx_varsize), 1, image); if (object->data_size > 0) { if (object->has_refs) _syx_memory_write (object->data, TRUE, object->data_size, image); else { #ifdef HAVE_LIBGMP if (SYX_OBJECT_IS_LARGE_INTEGER ((SyxOop)object)) { /* This algorithm is used to store large integers. We need to specify how many bytes GMP wrote to the image, for systems that don't support GMP */ syx_int32 offset = 0; syx_nint start, end; /* specify that's a large integer */ fputc (SYX_MEMORY_TYPE_LARGE_INTEGER, image); /* make space to hold the offset */ fwrite (&offset, sizeof (syx_int32), 1, image); start = ftell (image); mpz_out_raw (image, SYX_OBJECT_LARGE_INTEGER ((SyxOop)object)); end = ftell (image); offset = end - start; /* go back to the offset */ fseek (image, - offset - sizeof (syx_int32), SEEK_CUR); /* now write the length of the data written by mpz_out_raw () */ data = SYX_COMPAT_SWAP_32 (offset); fwrite (&data, sizeof (syx_int32), 1, image); /* return again to continue normal writing */ fseek (image, offset, SEEK_CUR); } else #endif /* HAVE_LIBGMP */ { /* it's not a large integer */ fputc (SYX_MEMORY_TYPE_NORMAL, image); fwrite (object->data, sizeof (syx_int8), object->data_size, image); } } } /* Check for block closures that are not attached to any process */ if (SYX_OOP_EQ (object->klass, syx_block_closure_class)) { stack = SYX_OBJECT (object->vars[SYX_VARS_BLOCK_CLOSURE_OUTER_FRAME]); /* Check if the stack has been collected or written to the image */ if (SYX_IS_NIL (SYX_POINTER_CAST_OOP (stack)) || stack->is_marked) continue; _syx_memory_write_object_with_vars (stack, image); data = SYX_COMPAT_SWAP_32 (stack->data_size); fwrite (&data, sizeof (syx_varsize), 1, image); /* Store the index of this frame */ fputc (SYX_MEMORY_TYPE_BOF, image); data = SYX_COMPAT_SWAP_32 (0); fwrite (&data, sizeof (syx_varsize), 1, image); /* Outer frames have only one frame */ _syx_memory_write_frame (NULL, (SyxInterpFrame *)stack->data, NULL, image); fputc (SYX_MEMORY_TYPE_EOS, image); fwrite (&data, sizeof (syx_varsize), 1, image); stack->is_marked = TRUE; } } fclose (image); /* be sure all objects are unmarked */ for (object=syx_memory; object <= SYX_MEMORY_TOP; object++) object->is_marked = FALSE; return TRUE; }
void dump_scalar_to_fp(FILE *fp, mpz_t s) { mpz_out_raw(fp, s); }
/*! Dumps all the memory. \param path the file path to put all inside \return FALSE if an error occurred */ syx_bool syx_memory_save_image (syx_symbol path) { SyxObject *object; FILE *image; syx_int32 data; if (!path) path = SYX_OBJECT_SYMBOL (syx_globals_at ("ImageFileName")); if (!path) return FALSE; image = fopen (path, "wb"); if (!image) return FALSE; syx_memory_gc (); data = SYX_COMPAT_SWAP_32 (_syx_memory_size); fwrite (&data, sizeof (syx_int32), 1, image); data = SYX_COMPAT_SWAP_32 (_syx_freed_memory_top); fwrite (&data, sizeof (syx_int32), 1, image); _syx_memory_write (_syx_freed_memory, FALSE, _syx_freed_memory_top, image); _syx_scheduler_save (image); _syx_memory_write (&syx_globals, FALSE, 1, image); _syx_memory_write (&syx_symbols, FALSE, 1, image); for (object=syx_memory; object <= SYX_MEMORY_TOP; object++) { if (SYX_IS_NIL (object->klass)) continue; _syx_memory_write ((SyxOop *)&object, FALSE, 1, image); _syx_memory_write (&object->klass, FALSE, 1, image); fputc (object->has_refs, image); fputc (object->is_constant, image); /* store instance variables */ data = syx_object_vars_size ((SyxOop)object); data = SYX_COMPAT_SWAP_32(data); fwrite (&data, sizeof (syx_varsize), 1, image); _syx_memory_write (object->vars, TRUE, SYX_COMPAT_SWAP_32(data), image); /* store data */ data = SYX_COMPAT_SWAP_32 (object->data_size); fwrite (&data, sizeof (syx_varsize), 1, image); if (object->data_size > 0) { if (object->has_refs) _syx_memory_write (object->data, TRUE, object->data_size, image); else { #ifdef HAVE_LIBGMP if (SYX_OBJECT_IS_LARGE_INTEGER ((SyxOop)object)) { /* This algorithm is used to store large integers. We need to specify how many bytes GMP wrote to the image, for systems that doesn't support GMP */ syx_int32 offset = 0; syx_nint start, end; /* specify that's a large integer */ fputc (1, image); /* make space to hold the offset */ fwrite (&offset, sizeof (syx_int32), 1, image); start = ftell (image); mpz_out_raw (image, SYX_OBJECT_LARGE_INTEGER ((SyxOop)object)); end = ftell (image); offset = end - start; /* go back to the offset */ fseek (image, - offset - sizeof (syx_int32), SEEK_CUR); data = SYX_COMPAT_SWAP_32 (offset); fwrite (&data, sizeof (syx_int32), 1, image); /* return again to continue normal writing */ fseek (image, offset, SEEK_CUR); } else #endif { /* it's not a large integer */ fputc (0, image); fwrite (object->data, sizeof (syx_int8), object->data_size, image); } } } } fclose (image); return TRUE; }
// The generic `main` function. // // Define all variables at the beginning to make the C99 compiler // happy. int main(int argc, char **argv) { mpz_array s, p, out; size_t count, i; int c, vflg = 0, sflg = 0, rflg = 0, errflg = 0, r = 0; char *filename = "primes.lst"; // #### argument parsing // Boring `getopt` argument parsing. while ((c = getopt(argc, argv, ":svr")) != -1) { switch(c) { case 's': sflg++; break; case 'v': vflg++; break; case 'r': rflg++; break; case ':': fprintf(stderr, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: '-%c'\n", optopt); errflg++; } } if (optind < argc) { filename = argv[optind]; if (optind + 1 < argc) errflg++; } if (rflg && vflg) { fprintf(stderr, "\n\t-r and -v can't be used simultaneously!\n\n"); errflg++; } // Print the usage and exit if an error occurred during argument parsing. if (errflg) { fprintf(stderr, "usage: [-vsr] [file]\n"\ "\n\t-v be more verbose"\ "\n\t-r output the found coprimes in raw gmp format"\ "\n\t-s only check if there are coprimes"\ "\n\n"); exit(2); } // Print the banner. if (vflg > 0) { PRINT_BANNER; } // Load the keys. array_init(&s, 10); count = array_of_file(&s, filename); if (count == 0) { fprintf(stderr, "Can't load %s\n", filename); return 1; } if (s.used != count) { fprintf(stderr, "Array size and load count do not match\n"); return 2; } if (s.used == 0) { fprintf(stderr, "No primes loaded (empty file)\n"); return 3; } // Print the key count. if (vflg > 0) { printf("%zu public keys loaded\nStarting factorization...\n", s.used); } // Computing a coprime base for a finite set [Algorithm 18.1](copri.html#computing-a-coprime-base-for-a-finite-set). array_init(&p, 10); array_cb(&p, &s); // Check if we have found more coprime bases. if (p.used == s.used) { if (vflg > 0) printf("No coprime pairs found :-(\n"); r = 1; } else { if (vflg > 0 || sflg > 0) printf("Found ~%zu coprime pairs!!!\nSearching factors...\n", (p.used - s.used)); if (sflg == 0) { array_init(&out, 9); // Use [Algorithm 21.2](copri.html#factoring-a-set-over-a-coprime-base) to find the coprimes in the coprime base. array_find_factors(&out, &s, &p); // Output the factors. if (out.used > 0) { if ((out.used % 3) != 0) { fprintf(stderr, "Find factors returned an invalid array\n"); } else { if (rflg) { for(i = 0; i < out.used; i++) mpz_out_raw(stdout, out.array[i]); } else { for(i = 0; i < out.used; i+=3) gmp_printf("\n### Found factors of\n%Zu\n=\n%Zu\nx\n%Zu\n", out.array[i], out.array[i+1], out.array[i+2]); } } } array_clear(&out); } } array_clear(&p); array_clear(&s); return r; }