QVariant RootsModel::data(const QModelIndex &index, int role) const { int i = index.row(); if (role != Qt::DisplayRole && role < Qt::UserRole) return QVariant(); if (i < 0 || i > length) return QVariant(); else { rdpe_t root_module; mpc_rmod (root_module, m_roots.at(i)->value); int digits = (rdpe_Esp (root_module) - rdpe_Esp (m_roots.at(i)->radius)) / LOG2_10; char * buffer = NULL; switch (role) { case SHORT_APPROXIMATION: digits = 4; // fallthrough case Qt::DisplayRole: buffer = new char[2 * digits + 15]; if (m_roots[i]->get_imag_part() > 0) gmp_sprintf (buffer, "%.*Ff + %.*Ffi", digits, mpc_Re (m_roots[i]->value), digits, mpc_Im (m_roots[i]->value)); else gmp_sprintf (buffer, "%.*Ff %.*Ffi", digits, mpc_Re (m_roots[i]->value), digits, mpc_Im (m_roots[i]->value)); return QString(buffer); break; case STATUS: return QString(MPS_ROOT_STATUS_TO_STRING (m_roots[i]->status)); case RADIUS: return QString("%1").arg(m_roots[i]->get_radius()); case ROOT: return QVariant::fromValue((void*) m_roots[i]); default: qDebug() << "Invalid role"; return QVariant(); } } }
int main(int argc, char* argv[]) { uint32_t number = 100; /* Default */ uint32_t i,sum; char *str; mpz_t factorial; if(argc > 1 &&!strcmp(argv[1],"--help")) return print_help(argv[0]), EXIT_SUCCESS; if(argc > 1) sscanf(argv[1],"%u",&number); mpz_init(factorial); mpz_fac_ui(factorial,number); str = malloc((mpz_sizeinbase(factorial,10)+2) * sizeof *str); gmp_sprintf(str,"%Zd",factorial); for(i=sum=0; str[i]; ++i) sum += str[i] - ASCII_OFFSET; printf("%u\n",sum); free(str); mpz_clear(factorial); return EXIT_SUCCESS; }
void hash_payload(mpz_t hashed, mpz_t payload) { int i; char payload_str[strlen(DHE_PRIME)]; //not going to be hashing anything larger char hex_hash[HEX_HASH_STR_LEN]; gmp_sprintf(payload_str, "%Zx", payload); memcpy(hex_hash, SHA256_hash(payload_str), HEX_HASH_STR_LEN); gmp_sscanf(hex_hash, "%Zx", hashed); }
/*----------------------------------------------------------------------------*/ int SetNetEnv(char *dev_name_list, char *port_stat_list) { int eidx = 0; int i, j; int set_all_inf = (strncmp(dev_name_list, ALL_STRING, sizeof(ALL_STRING))==0); TRACE_CONFIG("Loading interface setting\n"); CONFIG.eths = (struct eth_table *) calloc(MAX_DEVICES, sizeof(struct eth_table)); if (!CONFIG.eths) { TRACE_ERROR("Can't allocate space for CONFIG.eths\n"); exit(EXIT_FAILURE); } if (current_iomodule_func == &ps_module_func) { #ifndef DISABLE_PSIO struct ifreq ifr; /* calculate num_devices now! */ num_devices = ps_list_devices(devices); if (num_devices == -1) { perror("ps_list_devices"); exit(EXIT_FAILURE); } /* Create socket */ int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock == -1) { TRACE_ERROR("socket"); exit(EXIT_FAILURE); } /* To Do: Parse dev_name_list rather than use strstr */ for (i = 0; i < num_devices; i++) { strcpy(ifr.ifr_name, devices[i].name); /* getting interface information */ if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) { if (!set_all_inf && strstr(dev_name_list, ifr.ifr_name) == NULL) continue; /* Setting informations */ eidx = CONFIG.eths_num++; strcpy(CONFIG.eths[eidx].dev_name, ifr.ifr_name); CONFIG.eths[eidx].ifindex = devices[i].ifindex; /* getting address */ if (ioctl(sock, SIOCGIFADDR, &ifr) == 0 ) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].ip_addr = *(uint32_t *)&sin; } if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0 ) { for (j = 0; j < ETH_ALEN; j ++) { CONFIG.eths[eidx].haddr[j] = ifr.ifr_addr.sa_data[j]; } } /* Net MASK */ if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].netmask = *(uint32_t *)&sin; } /* add to attached devices */ for (j = 0; j < num_devices_attached; j++) { if (devices_attached[j] == devices[i].ifindex) { break; } } devices_attached[num_devices_attached] = devices[i].ifindex; num_devices_attached++; } else { perror("SIOCGIFFLAGS"); } } num_queues = GetNumQueues(); if (num_queues <= 0) { TRACE_CONFIG("Failed to find NIC queues!\n"); close(sock); return -1; } if (num_queues > num_cpus) { TRACE_CONFIG("Too many NIC queues available.\n"); close(sock); return -1; } close(sock); #endif /* !PSIO_MODULE */ } else if (current_iomodule_func == &dpdk_module_func) { #ifndef DISABLE_DPDK int cpu = CONFIG.num_cores; mpz_t _cpumask; char cpumaskbuf[32] = ""; char mem_channels[8] = ""; char socket_mem_str[32] = ""; // int i; int ret, socket_mem; static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; /* STEP 1: first determine CPU mask */ mpz_init(_cpumask); if (!mpz_cmp(_cpumask, CONFIG._cpumask)) { /* get the cpu mask */ for (ret = 0; ret < cpu; ret++) mpz_setbit(_cpumask, ret); gmp_sprintf(cpumaskbuf, "%ZX", _cpumask); } else gmp_sprintf(cpumaskbuf, "%ZX", CONFIG._cpumask); mpz_clear(_cpumask); /* STEP 2: determine memory channels per socket */ /* get the mem channels per socket */ if (CONFIG.num_mem_ch == 0) { TRACE_ERROR("DPDK module requires # of memory channels " "per socket parameter!\n"); exit(EXIT_FAILURE); } sprintf(mem_channels, "%d", CONFIG.num_mem_ch); /* STEP 3: determine socket memory */ /* get socket memory threshold (in MB) */ socket_mem = RTE_ALIGN_CEIL((unsigned long)ceil((CONFIG.num_cores * (CONFIG.rcvbuf_size + CONFIG.sndbuf_size + sizeof(struct tcp_stream) + sizeof(struct tcp_recv_vars) + sizeof(struct tcp_send_vars) + sizeof(struct fragment_ctx)) * CONFIG.max_concurrency)/RTE_SOCKET_MEM_SHIFT), RTE_CACHE_LINE_SIZE); /* initialize the rte env, what a waste of implementation effort! */ int argc = 6;//8; char *argv[RTE_ARGC_MAX] = {"", "-c", cpumaskbuf, "-n", mem_channels, #if 0 "--socket-mem", socket_mem_str, #endif "--proc-type=auto" }; ret = probe_all_rte_devices(argv, &argc, dev_name_list); /* STEP 4: build up socket mem parameter */ sprintf(socket_mem_str, "%d", socket_mem); #if 0 char *smsptr = socket_mem_str + strlen(socket_mem_str); for (i = 1; i < ret + 1; i++) { sprintf(smsptr, ",%d", socket_mem); smsptr += strlen(smsptr); } TRACE_DBG("socket_mem: %s\n", socket_mem_str); #endif /* * re-set getopt extern variable optind. * this issue was a bitch to debug * rte_eal_init() internally uses getopt() syscall * mtcp applications that also use an `external' getopt * will cause a violent crash if optind is not reset to zero * prior to calling the func below... * see man getopt(3) for more details */ optind = 0; #ifdef DEBUG /* print argv's */ for (i = 0; i < argc; i++) TRACE_INFO("argv[%d]: %s\n", i, argv[i]); #endif /* initialize the dpdk eal env */ ret = rte_eal_init(argc, argv); if (ret < 0) { TRACE_ERROR("Invalid EAL args!\n"); exit(EXIT_FAILURE); } /* give me the count of 'detected' ethernet ports */ #if RTE_VERSION < RTE_VERSION_NUM(18, 5, 0, 0) num_devices = rte_eth_dev_count(); #else num_devices = rte_eth_dev_count_avail(); #endif if (num_devices == 0) { TRACE_ERROR("No Ethernet port!\n"); exit(EXIT_FAILURE); } /* get mac addr entries of 'detected' dpdk ports */ for (ret = 0; ret < num_devices; ret++) rte_eth_macaddr_get(ret, &ports_eth_addr[ret]); num_queues = MIN(CONFIG.num_cores, MAX_CPUS); struct ifaddrs *ifap; struct ifaddrs *iter_if; char *seek; if (getifaddrs(&ifap) != 0) { perror("getifaddrs: "); exit(EXIT_FAILURE); } iter_if = ifap; do { if (iter_if->ifa_addr && iter_if->ifa_addr->sa_family == AF_INET && !set_all_inf && (seek=strstr(dev_name_list, iter_if->ifa_name)) != NULL && /* check if the interface was not aliased */ *(seek + strlen(iter_if->ifa_name)) != ':') { struct ifreq ifr; /* Setting informations */ eidx = CONFIG.eths_num++; strcpy(CONFIG.eths[eidx].dev_name, iter_if->ifa_name); strcpy(ifr.ifr_name, iter_if->ifa_name); /* Create socket */ int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock == -1) { perror("socket"); exit(EXIT_FAILURE); } /* getting address */ if (ioctl(sock, SIOCGIFADDR, &ifr) == 0 ) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].ip_addr = *(uint32_t *)&sin; } if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0 ) { for (j = 0; j < ETH_ALEN; j ++) { CONFIG.eths[eidx].haddr[j] = ifr.ifr_addr.sa_data[j]; } } /* Net MASK */ if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].netmask = *(uint32_t *)&sin; } close(sock); for (j = 0; j < num_devices; j++) { if (!memcmp(&CONFIG.eths[eidx].haddr[0], &ports_eth_addr[j], ETH_ALEN)) CONFIG.eths[eidx].ifindex = j; } /* add to attached devices */ for (j = 0; j < num_devices_attached; j++) { if (devices_attached[j] == CONFIG.eths[eidx].ifindex) { break; } } devices_attached[num_devices_attached] = CONFIG.eths[eidx].ifindex; num_devices_attached++; fprintf(stderr, "Total number of attached devices: %d\n", num_devices_attached); fprintf(stderr, "Interface name: %s\n", iter_if->ifa_name); } iter_if = iter_if->ifa_next; } while (iter_if != NULL); freeifaddrs(ifap); #if 0 /* * XXX: It seems that there is a bug in the RTE SDK. * The dynamically allocated rte_argv params are left * as dangling pointers. Freeing them causes program * to crash. */ /* free up all resources */ for (; rte_argc >= 9; rte_argc--) { if (rte_argv[rte_argc] != NULL) { fprintf(stderr, "Cleaning up rte_argv[%d]: %s (%p)\n", rte_argc, rte_argv[rte_argc], rte_argv[rte_argc]); free(rte_argv[rte_argc]); rte_argv[rte_argc] = NULL; } } #endif /* check if process is primary or secondary */ CONFIG.multi_process_is_master = (eal_proc_type_detect() == RTE_PROC_PRIMARY) ? 1 : 0; #endif /* !DISABLE_DPDK */ } else if (current_iomodule_func == &netmap_module_func) { #ifndef DISABLE_NETMAP struct ifaddrs *ifap; struct ifaddrs *iter_if; char *seek; num_queues = MIN(CONFIG.num_cores, MAX_CPUS); if (getifaddrs(&ifap) != 0) { perror("getifaddrs: "); exit(EXIT_FAILURE); } iter_if = ifap; do { if (iter_if->ifa_addr && iter_if->ifa_addr->sa_family == AF_INET && !set_all_inf && (seek=strstr(dev_name_list, iter_if->ifa_name)) != NULL && /* check if the interface was not aliased */ *(seek + strlen(iter_if->ifa_name)) != ':') { struct ifreq ifr; /* Setting informations */ eidx = CONFIG.eths_num++; strcpy(CONFIG.eths[eidx].dev_name, iter_if->ifa_name); strcpy(ifr.ifr_name, iter_if->ifa_name); /* Create socket */ int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock == -1) { perror("socket"); exit(EXIT_FAILURE); } /* getting address */ if (ioctl(sock, SIOCGIFADDR, &ifr) == 0 ) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].ip_addr = *(uint32_t *)&sin; } if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0 ) { for (j = 0; j < ETH_ALEN; j ++) { CONFIG.eths[eidx].haddr[j] = ifr.ifr_addr.sa_data[j]; } } /* Net MASK */ if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].netmask = *(uint32_t *)&sin; } close(sock); #if 0 for (j = 0; j < num_devices; j++) { if (!memcmp(&CONFIG.eths[eidx].haddr[0], &ports_eth_addr[j], ETH_ALEN)) CONFIG.eths[eidx].ifindex = ifr.ifr_ifindex; #endif CONFIG.eths[eidx].ifindex = eidx; TRACE_INFO("Ifindex of interface %s is: %d\n", ifr.ifr_name, CONFIG.eths[eidx].ifindex); #if 0 } #endif /* add to attached devices */ for (j = 0; j < num_devices_attached; j++) { if (devices_attached[j] == CONFIG.eths[eidx].ifindex) { break; } } devices_attached[num_devices_attached] = if_nametoindex(ifr.ifr_name); num_devices_attached++; fprintf(stderr, "Total number of attached devices: %d\n", num_devices_attached); fprintf(stderr, "Interface name: %s\n", iter_if->ifa_name); } iter_if = iter_if->ifa_next; } while (iter_if != NULL); freeifaddrs(ifap); #endif /* !DISABLE_NETMAP */ } else if (current_iomodule_func == &onvm_module_func) { #ifdef ENABLE_ONVM int cpu = CONFIG.num_cores; mpz_t cpumask; char cpumaskbuf[32]; char mem_channels[8]; char service[6]; char instance[6]; int ret; mpz_init(cpumask); /* get the cpu mask */ for (ret = 0; ret < cpu; ret++) mpz_setbit(cpumask, ret); gmp_sprintf(cpumaskbuf, "%ZX", cpumask); mpz_clear(cpumask); /* get the mem channels per socket */ if (CONFIG.num_mem_ch == 0) { TRACE_ERROR("DPDK module requires # of memory channels " "per socket parameter!\n"); exit(EXIT_FAILURE); } sprintf(mem_channels, "%d", CONFIG.num_mem_ch); sprintf(service, "%d", CONFIG.onvm_serv); sprintf(instance, "%d", CONFIG.onvm_inst); /* initialize the rte env first, what a waste of implementation effort! */ char *argv[] = {"", "-c", cpumaskbuf, "-n", mem_channels, "--proc-type=secondary", "--", "-r", service, instance, "" }; const int argc = 10; /* * re-set getopt extern variable optind. * this issue was a bitch to debug * rte_eal_init() internally uses getopt() syscall * mtcp applications that also use an `external' getopt * will cause a violent crash if optind is not reset to zero * prior to calling the func below... * see man getopt(3) for more details */ optind = 0; /* initialize the dpdk eal env */ ret = onvm_nflib_init(argc, argv, "mtcp_nf", &CONFIG.nf_info); if (ret < 0) { TRACE_ERROR("Invalid EAL args!\n"); exit(EXIT_FAILURE); } /* give me the count of 'detected' ethernet ports */ num_devices = ports->num_ports; if (num_devices == 0) { TRACE_ERROR("No Ethernet port!\n"); exit(EXIT_FAILURE); } num_queues = MIN(CONFIG.num_cores, MAX_CPUS); struct ifaddrs *ifap; struct ifaddrs *iter_if; char *seek; if (getifaddrs(&ifap) != 0) { perror("getifaddrs: "); exit(EXIT_FAILURE); } iter_if = ifap; do { if (iter_if->ifa_addr && iter_if->ifa_addr->sa_family == AF_INET && !set_all_inf && (seek=strstr(dev_name_list, iter_if->ifa_name)) != NULL && /* check if the interface was not aliased */ *(seek + strlen(iter_if->ifa_name)) != ':') { struct ifreq ifr; /* Setting informations */ eidx = CONFIG.eths_num++; strcpy(CONFIG.eths[eidx].dev_name, iter_if->ifa_name); strcpy(ifr.ifr_name, iter_if->ifa_name); /* Create socket */ int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); if (sock == -1) { perror("socket"); } /* getting address */ if (ioctl(sock, SIOCGIFADDR, &ifr) == 0 ) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].ip_addr = *(uint32_t *)&sin; } for (j = 0; j < ETH_ALEN; j ++) { CONFIG.eths[eidx].haddr[j] = ports->mac[eidx].addr_bytes[j]; }; /* Net MASK */ if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) { struct in_addr sin = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; CONFIG.eths[eidx].netmask = *(uint32_t *)&sin; } close(sock); CONFIG.eths[eidx].ifindex = ports->id[eidx]; devices_attached[num_devices_attached] = CONFIG.eths[eidx].ifindex; num_devices_attached++; fprintf(stderr, "Total number of attached devices: %d\n", num_devices_attached); fprintf(stderr, "Interface name: %s\n", iter_if->ifa_name); } iter_if = iter_if->ifa_next; } while (iter_if != NULL); freeifaddrs(ifap); #endif /* ENABLE_ONVM */ } CONFIG.nif_to_eidx = (int*)calloc(MAX_DEVICES, sizeof(int)); if (!CONFIG.nif_to_eidx) { exit(EXIT_FAILURE); } for (i = 0; i < MAX_DEVICES; ++i) { CONFIG.nif_to_eidx[i] = -1; } for (i = 0; i < CONFIG.eths_num; ++i) { j = CONFIG.eths[i].ifindex; if (j >= MAX_DEVICES) { TRACE_ERROR("ifindex of eths_%d exceed the limit: %d\n", i, j); exit(EXIT_FAILURE); } /* the physic port index of the i-th port listed in the config file is j*/ CONFIG.nif_to_eidx[j] = i; /* finally set the port stats option `on' */ if (strcmp(CONFIG.eths[i].dev_name, port_stat_list) == 0) CONFIG.eths[i].stat_print = TRUE; } return 0; }
char *vio_uneval_val(vio_val *v) { char *out, **vout; uint32_t total; switch (v->what) { case vv_str: out = (char *)malloc(v->len + 3); if (out == NULL) goto die; out[0] = '"'; strncpy(out + 1, v->s, v->len); out[v->len + 1] = '"'; out[v->len + 2] = '\0'; break; case vv_int: out = (char *)malloc(22); /* maximum digits of a vio_int type + sign + null terminator */ if (out == NULL) goto die; snprintf(out, 22, "%ld", v->i32); break; case vv_float: out = (char *)malloc(20); /* 16 + dot + sign + e + null */ if (out == NULL) goto die; snprintf(out, 20, "%gf", v->f32); break; case vv_num: out = (char *)malloc(gmp_snprintf(NULL, 0, "%Ff", v->n) + 1); if (out == NULL) goto die; gmp_sprintf(out, "%Ff", v->n); break; case vv_tagword: vout = (char **)malloc(v->vlen * sizeof(char *)); total = 5 + v->len + v->vlen; /* also include space for tag name */ for (uint32_t i = 0; i < v->vlen; ++i) { vout[i] = vio_uneval_val(v->vv[i]); total += strlen(vout[i]); } out = (char *)malloc(total); out[0] = '.'; strncpy(out + 1, v->s, v->len); out[v->len + 1] = '{'; out[v->len + 2] = ' '; out[v->len + 3] = '\0'; for (uint32_t i = 0; i < v->vlen; ++i) { strcat(out, vout[i]); strcat(out, " "); } out[total-2] = '}'; out[total-1] = '\0'; break; case vv_vec: vout = (char **)malloc(v->vlen * sizeof(char *)); total = 4 + v->vlen; /* spaces between each value */ for (uint32_t i = 0; i < v->vlen; ++i) { vout[i] = vio_uneval_val(v->vv[i]); total += strlen(vout[i]); } out = (char *)malloc(total); out[0] = '{'; out[1] = ' '; out[2] = '\0'; for (uint32_t i = 0; i < v->vlen; ++i) { strcat(out, vout[i]); strcat(out, " "); } out[total-2] = '}'; out[total-1] = '\0'; break; default: out = (char *)malloc(40); /* arbitrary but should fit */ snprintf(out, 40, "#<%s %p>", vio_val_type_name(v->what), (void *)v); } return out; die: fprintf(stderr, "vio_uneval() failed to allocate memory for output string. :("); exit(EX_OSERR); }
QDebug operator<<(QDebug s, const mpf_class& m) { char b[65536] = ""; gmp_sprintf (b, "%.9Ff", m.get_mpf_t()); s << b; return s; }
/* Recursive routine to prove via ECPP */ static int ecpp_down(int i, mpz_t Ni, int facstage, IV* dlist, mpz_t* sfacs, int* nsfacs, char** prooftextptr) { mpz_t a, b, u, v, m, q, minfactor, sqrtn, mD, t, t2; mpz_t mlist[6]; UV nm1a; IV np1lp, np1lq; struct ec_affine_point P; int k, dnum, nidigits, facresult, curveresult, downresult, stage, D; int verbose = get_verbose_level(); nidigits = mpz_sizeinbase(Ni, 10); k = _GMP_is_prob_prime(Ni); if (k == 0) return 0; if (k == 2) { /* No need to put anything in the proof */ if (verbose) printf("%*sN[%d] (%d dig) PRIME\n", i, "", i, nidigits); return 2; } downresult = 0; if (verbose) { printf("%*sN[%d] (%d dig)", i, "", i, nidigits); if (facstage > 1) printf(" FS %d", facstage); fflush(stdout); } mpz_init(a); mpz_init(b); mpz_init(u); mpz_init(v); mpz_init(m); mpz_init(q); mpz_init(mD); mpz_init(minfactor); mpz_init(sqrtn); mpz_init(t); mpz_init(t2); mpz_init(P.x);mpz_init(P.y); for (k = 0; k < 6; k++) mpz_init(mlist[k]); /* Any factors q found must be strictly > minfactor. * See Atkin and Morain, 1992, section 6.4 */ mpz_root(minfactor, Ni, 4); mpz_add_ui(minfactor, minfactor, 1); mpz_mul(minfactor, minfactor, minfactor); mpz_sqrt(sqrtn, Ni); for (stage = (i == 0) ? facstage : 1; stage <= facstage; stage++) { int next_stage = (stage > 1) ? stage : 1; for (dnum = 0; dlist[dnum] != 0; dnum++) { int poly_type; /* just for debugging/verbose */ int poly_degree; D = -dlist[dnum]; if (D > 1) continue; /* Marked for skip */ if (D == 1) { /* n-1 test */ mpz_sub_ui(m, Ni, 1); /* m = N-1 */ mpz_sub_ui(t2, sqrtn, 1); mpz_tdiv_q_2exp(t2, t2, 1); /* t2 = minfactor */ facresult = check_for_factor2(q, m, t2, t, stage, sfacs, nsfacs, 0); if (facresult <= 0) continue; if (verbose) { printf(" n-1\n"); fflush(stdout); } downresult = ecpp_down(i+1, q, next_stage, dlist, sfacs, nsfacs, prooftextptr); if (downresult == 0) /* composite */ goto end_down; if (downresult == 1) { /* nothing found at this stage */ if (verbose) { printf("%*sN[%d] (%d dig)", i, "", i, nidigits); if (facstage > 1) printf(" FS %d", facstage); fflush(stdout); } continue; } if (verbose) { printf("%*sN[%d] (%d dig) n-1", i, "", i, nidigits); fflush(stdout); } curveresult = _GMP_primality_bls_3(Ni, q, &nm1a); if (verbose) { printf(" %d\n", curveresult); fflush(stdout); } if ( ! curveresult ) { /* This ought not happen */ dlist[dnum] = -2; /* skip this D value from now on */ if (verbose) gmp_printf("\n Could not prove n-1 with N = %Zd\n", D, Ni); downresult = 0; continue; } goto end_down; } if (D == -1) { /* n+1 test */ mpz_add_ui(m, Ni, 1); /* m = N+1 */ mpz_add_ui(t2, sqrtn, 1); mpz_tdiv_q_2exp(t2, t2, 1); /* t2 = minfactor */ facresult = check_for_factor2(q, m, t2, t, stage, sfacs, nsfacs, 0); if (facresult <= 0) continue; if (verbose) { printf(" n+1\n"); fflush(stdout); } downresult = ecpp_down(i+1, q, next_stage, dlist, sfacs, nsfacs, prooftextptr); if (downresult == 0) /* composite */ goto end_down; if (downresult == 1) { /* nothing found at this stage */ if (verbose) { printf("%*sN[%d] (%d dig)", i, "", i, nidigits); if (facstage > 1) printf(" FS %d", facstage); fflush(stdout); } continue; } if (verbose) { printf("%*sN[%d] (%d dig) n-1", i, "", i, nidigits); fflush(stdout); } curveresult = _GMP_primality_bls_15(Ni, q, &np1lp, &np1lq); if (verbose) { printf(" %d\n", curveresult); fflush(stdout); } if ( ! curveresult ) { /* This ought not happen */ dlist[dnum] = -2; /* skip this D value from now on */ if (verbose) gmp_printf("\n Could not prove n-1 with N = %Zd\n", D, Ni); downresult = 0; continue; } goto end_down; } if ( (-D % 4) != 3 && (-D % 16) != 4 && (-D % 16) != 8 ) croak("Invalid discriminant '%d' in list\n", D); /* D must also be squarefree in odd divisors, but assume it. */ /* Make sure we can get a class polynomial for this D. */ poly_degree = poly_class_poly(D, NULL, &poly_type); if (poly_degree == 0) continue; /* We'll save time in the long run by not looking at big polys once * we've found a good path from the start. TODO: Needs more tuning. */ if (stage == 0 && i >= 0 && poly_degree > 2) break; if (facstage == 1) { if (i > 2 && nidigits < 1100 && poly_degree > 24) break; if (i > 3 && nidigits < 950 && poly_degree > 15) break; if (i > 4 && nidigits < 800 && poly_degree > 11) break; if (i > 8 && nidigits < 700 && poly_degree > 9) break; if (i > 16 && nidigits < 600 && poly_degree > 8) break; } mpz_set_si(mD, D); /* (D/N) must be 1, and we have to have a u,v solution */ if (mpz_jacobi(mD, Ni) != 1) continue; if ( ! modified_cornacchia(u, v, mD, Ni) ) continue; if (verbose > 1) { printf(" %d", D); fflush(stdout); } choose_m(mlist, D, u, v, Ni, t, t2); for (k = 0; k < 6; k++) { facresult = check_for_factor2(q, mlist[k], minfactor, t, stage, sfacs, nsfacs, poly_degree); /* -1 = couldn't find, 0 = no big factors, 1 = found */ if (facresult <= 0) continue; mpz_set(m, mlist[k]); if (verbose) { printf(" %d (%s %d)\n", D, (poly_type == 1) ? "Hilbert" : "Weber", poly_degree); fflush(stdout); } /* Great, now go down. */ downresult = ecpp_down(i+1, q, next_stage, dlist, sfacs, nsfacs, prooftextptr); if (downresult == 0) /* composite */ goto end_down; if (downresult == 1) { /* nothing found at this stage */ if (verbose) { printf("%*sN[%d] (%d dig)", i, "", i, nidigits); if (facstage > 1) printf(" FS %d", facstage); fflush(stdout); } continue; } /* Awesome, we found the q chain and are in STAGE 2 */ if (verbose) { printf("%*sN[%d] (%d dig) %d (%s %d)", i, "", i, nidigits, D, (poly_type == 1) ? "Hilbert" : "Weber", poly_degree); fflush(stdout); } curveresult = find_curve(a, b, P.x, P.y, D, m, q, Ni); if (verbose) { printf(" %d\n", curveresult); fflush(stdout); } if (curveresult == 1) { /* Oh no! We can't find a point on the curve. Something is right * messed up, and we've wasted a lot of time. Sigh. */ dlist[dnum] = -2; /* skip this D value from now on */ if (verbose) gmp_printf("\n Invalidated D = %d with N = %Zd\n", D, Ni); downresult = 0; continue; } /* We found it was composite or proved it */ goto end_down; } /* k loop for D */ } /* D */ } /* fac stage */ /* Nothing at this level */ downresult = 1; if (verbose) { printf(" ---\n"); fflush(stdout); } end_down: if (downresult == 2) { if (0 && verbose > 1) { if (D == 1) { gmp_printf("\n"); gmp_printf("Type BLS3\n"); gmp_printf("N %Zd\n", Ni); gmp_printf("Q %Zd\n", q); gmp_printf("A %lu\n", (unsigned long) nm1a); gmp_printf("\n"); fflush(stdout); } else if (D == -1) { gmp_printf("\n"); gmp_printf("Type BLS15\n"); gmp_printf("N %Zd\n", Ni); gmp_printf("Q %Zd\n", q); gmp_printf("LP %ld\n", (signed long) np1lp); gmp_printf("LQ %ld\n", (signed long) np1lq); gmp_printf("\n"); fflush(stdout); } else { gmp_printf("\n"); gmp_printf("Type ECPP\n"); gmp_printf("N %Zd\n", Ni); gmp_printf("A %Zd\n", a); gmp_printf("B %Zd\n", b); gmp_printf("M %Zd\n", m); gmp_printf("Q %Zd\n", q); gmp_printf("X %Zd\n", P.x); gmp_printf("Y %Zd\n", P.y); gmp_printf("\n"); fflush(stdout); } } /* Prepend our proof to anything that exists. */ if (prooftextptr != 0) { char *proofstr, *proofptr; int curprooflen = (*prooftextptr == 0) ? 0 : strlen(*prooftextptr); if (D == 1) { int myprooflen = 20 + 2*(4 + mpz_sizeinbase(Ni, 10)) + 1*21; New(0, proofstr, myprooflen + curprooflen + 1, char); proofptr = proofstr; proofptr += gmp_sprintf(proofptr, "Type BLS3\nN %Zd\nQ %Zd\nA %"UVuf"\n", Ni, q, nm1a); } else if (D == -1) {
char * _fmpz_poly_get_str_pretty(const fmpz * poly, long len, const char *x) { char *str; size_t off; long i, bound, nz; if (len == 0) { str = flint_malloc(2); str[0] = '0'; str[1] = '\0'; return str; } if (len == 1) { str = fmpz_get_str(NULL, 10, poly); return str; } nz = 0; bound = 1; for (i = 0; i < len; i++) if (!fmpz_is_zero(poly + i)) { bound += fmpz_sizeinbase(poly + i, 10) + 1; nz++; } bound += nz * (3 + strlen(x) + (long) (ceil(log10(len)))); str = flint_malloc(bound); off = 0; i = len - 1; if (poly[i] == 1L) { } else if (poly[i] == -1L) str[off++] = '-'; else if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld*", poly[i]); else off += gmp_sprintf(str + off, "%Zd*", COEFF_TO_PTR(poly[i])); if (i > 1) off += sprintf(str + off, "%s^%ld", x, i); else off += sprintf(str + off, "%s", x); for (--i; i > 0; --i) { if (poly[i] == 0L) continue; if (fmpz_sgn(poly + i) > 0) str[off++] = '+'; if (poly[i] == -1L) str[off++] = '-'; if (poly[i] != 1L && poly[i] != -1L) { if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld*", poly[i]); else off += gmp_sprintf(str + off, "%Zd*", COEFF_TO_PTR(poly[i])); } if (i > 1) off += sprintf(str + off, "%s^%ld", x, i); else off += sprintf(str + off, "%s", x); } if (poly[i] != 0L) { if (fmpz_sgn(poly + i) > 0) str[off++] = '+'; if (!COEFF_IS_MPZ(poly[i])) off += sprintf(str + off, "%ld", poly[i]); else off += gmp_sprintf(str + off, "%Zd", COEFF_TO_PTR(poly[i])); } return str; }
/* Recursive routine to prove via ECPP */ static int ecpp_down(int i, mpz_t Ni, int facstage, int *pmaxH, int* dilist, mpz_t* sfacs, int* nsfacs, char** prooftextptr) { mpz_t a, b, u, v, m, q, minfactor, sqrtn, mD, t, t2; mpz_t mlist[6]; mpz_t qlist[6]; UV nm1a; IV np1lp, np1lq; struct ec_affine_point P; int k, dindex, pindex, nidigits, facresult, curveresult, downresult, stage, D; int verbose = get_verbose_level(); nidigits = mpz_sizeinbase(Ni, 10); downresult = _GMP_is_prob_prime(Ni); if (downresult == 0) return 0; if (downresult == 2) { if (mpz_sizeinbase(Ni,2) <= 64) { /* No need to put anything in the proof */ if (verbose) printf("%*sN[%d] (%d dig) PRIME\n", i, "", i, nidigits); return 2; } downresult = 1; } if (i == 0 && facstage == 2 && miller_rabin_random(Ni, 2, 0) == 0) { gmp_printf("\n\n**** BPSW counter-example found? ****\n**** N = %Zd ****\n\n", Ni); return 0; } VERBOSE_PRINT_N(i, nidigits, *pmaxH, facstage); mpz_init(a); mpz_init(b); mpz_init(u); mpz_init(v); mpz_init(m); mpz_init(q); mpz_init(mD); mpz_init(minfactor); mpz_init(sqrtn); mpz_init(t); mpz_init(t2); mpz_init(P.x);mpz_init(P.y); for (k = 0; k < 6; k++) { mpz_init(mlist[k]); mpz_init(qlist[k]); } /* Any factors q found must be strictly > minfactor. * See Atkin and Morain, 1992, section 6.4 */ mpz_root(minfactor, Ni, 4); mpz_add_ui(minfactor, minfactor, 1); mpz_mul(minfactor, minfactor, minfactor); mpz_sqrt(sqrtn, Ni); stage = 0; if (nidigits > 700) stage = 1; /* Too rare to find them */ if (i == 0 && facstage > 1) stage = facstage; for ( ; stage <= facstage; stage++) { int next_stage = (stage > 1) ? stage : 1; for (dindex = -1; dindex < 0 || dilist[dindex] != 0; dindex++) { int poly_type; /* just for debugging/verbose */ int poly_degree; int allq = (nidigits < 400); /* Do all q values together, or not */ if (dindex == -1) { /* n-1 and n+1 tests */ int nm1_success = 0; int np1_success = 0; const char* ptype = ""; mpz_sub_ui(m, Ni, 1); mpz_sub_ui(t2, sqrtn, 1); mpz_tdiv_q_2exp(t2, t2, 1); /* t2 = minfactor */ nm1_success = check_for_factor(u, m, t2, t, stage, sfacs, nsfacs, 0); mpz_add_ui(m, Ni, 1); mpz_add_ui(t2, sqrtn, 1); mpz_tdiv_q_2exp(t2, t2, 1); /* t2 = minfactor */ np1_success = check_for_factor(v, m, t2, t, stage, sfacs, nsfacs, 0); /* If both successful, pick smallest */ if (nm1_success > 0 && np1_success > 0) { if (mpz_cmp(u, v) <= 0) np1_success = 0; else nm1_success = 0; } if (nm1_success > 0) { ptype = "n-1"; mpz_set(q, u); D = 1; } else if (np1_success > 0) { ptype = "n+1"; mpz_set(q, v); D = -1; } else continue; if (verbose) { printf(" %s\n", ptype); fflush(stdout); } downresult = ecpp_down(i+1, q, next_stage, pmaxH, dilist, sfacs, nsfacs, prooftextptr); if (downresult == 0) goto end_down; /* composite */ if (downresult == 1) { /* nothing found at this stage */ VERBOSE_PRINT_N(i, nidigits, *pmaxH, facstage); continue; } if (verbose) { printf("%*sN[%d] (%d dig) %s", i, "", i, nidigits, ptype); fflush(stdout); } curveresult = (nm1_success > 0) ? _GMP_primality_bls_3(Ni, q, &nm1a) : _GMP_primality_bls_15(Ni, q, &np1lp, &np1lq); if (verbose) { printf(" %d\n", curveresult); fflush(stdout); } if ( ! curveresult ) { /* This ought not happen */ if (verbose) gmp_printf("\n Could not prove %s with N = %Zd\n", ptype, Ni); downresult = 1; continue; } goto end_down; } pindex = dilist[dindex]; if (pindex < 0) continue; /* We marked this for skip */ /* Get the values for D, degree, and poly type */ poly_degree = poly_class_poly_num(pindex, &D, NULL, &poly_type); if (poly_degree == 0) croak("Unknown value in dilist[%d]: %d\n", dindex, pindex); if ( (-D % 4) != 3 && (-D % 16) != 4 && (-D % 16) != 8 ) croak("Invalid discriminant '%d' in list\n", D); /* D must also be squarefree in odd divisors, but assume it. */ /* Make sure we can get a class polynomial for this D. */ if (poly_degree > 16 && stage == 0) { if (verbose) printf(" [1]"); break; } /* Make the continue-search vs. backtrack decision */ if (*pmaxH > 0 && poly_degree > *pmaxH) break; mpz_set_si(mD, D); /* (D/N) must be 1, and we have to have a u,v solution */ if (mpz_jacobi(mD, Ni) != 1) continue; if ( ! modified_cornacchia(u, v, mD, Ni) ) continue; if (verbose > 1) { printf(" %d", D); fflush(stdout); } /* We're going to factor all the values for this discriminant then pick * the smallest. This adds a little time, but it means we go down * faster. This makes smaller proofs, and might even save time. */ choose_m(mlist, D, u, v, Ni, t, t2); if (allq) { int x, y; /* We have 0 to 6 m values. Try to factor them, put in qlist. */ for (k = 0; k < 6; k++) { mpz_set_ui(qlist[k], 0); if (mpz_sgn(mlist[k])) { facresult = check_for_factor(qlist[k], mlist[k], minfactor, t, stage, sfacs, nsfacs, poly_degree); /* -1 = couldn't find, 0 = no big factors, 1 = found */ if (facresult <= 0) mpz_set_ui(qlist[k], 0); } } /* Sort any q values by size, so we work on the smallest first */ for (x = 0; x < 5; x++) if (mpz_sgn(qlist[x])) for (y = x+1; y < 6; y++) if (mpz_sgn(qlist[y]) && mpz_cmp(qlist[x],qlist[y]) > 0) { mpz_swap( qlist[x], qlist[y] ); mpz_swap( mlist[x], mlist[y] ); } } /* Try to make a proof with the first (smallest) q value. * Repeat for others if we have to. */ for (k = 0; k < 6; k++) { int maxH = *pmaxH; int minH = (nidigits <= 240) ? 7 : (nidigits+39)/40; if (allq) { if (mpz_sgn(qlist[k]) == 0) continue; mpz_set(m, mlist[k]); mpz_set(q, qlist[k]); } else { if (mpz_sgn(mlist[k]) == 0) continue; mpz_set(m, mlist[k]); facresult = check_for_factor(q, m, minfactor, t, stage, sfacs, nsfacs, poly_degree); if (facresult <= 0) continue; } if (verbose) { printf(" %d (%s %d)\n", D, poly_class_type_name(poly_type), poly_degree); fflush(stdout); } if (maxH == 0) { maxH = minH-1 + poly_degree; if (facstage > 1) /* We worked hard to get here, */ maxH = 2*maxH + 10; /* try hard to make use of it. */ } else if (maxH > minH && maxH > (poly_degree+2)) { maxH--; } /* Great, now go down. */ downresult = ecpp_down(i+1, q, next_stage, &maxH, dilist, sfacs, nsfacs, prooftextptr); /* Nothing found, look at more polys in the future */ if (downresult == 1 && *pmaxH > 0) *pmaxH = maxH; if (downresult == 0) goto end_down; /* composite */ if (downresult == 1) { /* nothing found at this stage */ VERBOSE_PRINT_N(i, nidigits, *pmaxH, facstage); continue; } /* Awesome, we found the q chain and are in STAGE 2 */ if (verbose) { printf("%*sN[%d] (%d dig) %d (%s %d)", i, "", i, nidigits, D, poly_class_type_name(poly_type), poly_degree); fflush(stdout); } /* Try with only one or two roots, then 8 if that didn't work. */ /* TODO: This should be done using a root iterator in find_curve() */ curveresult = find_curve(a, b, P.x, P.y, D, pindex, m, q, Ni, 1); if (curveresult == 1) { if (verbose) { printf(" [redo roots]"); fflush(stdout); } curveresult = find_curve(a, b, P.x, P.y, D, pindex, m, q, Ni, 8); } if (verbose) { printf(" %d\n", curveresult); fflush(stdout); } if (curveresult == 1) { /* Something is wrong. Very likely the class poly coefficients are incorrect. We've wasted lots of time, and need to try again. */ dilist[dindex] = -2; /* skip this D value from now on */ if (verbose) gmp_printf("\n Invalidated D = %d with N = %Zd\n", D, Ni); downresult = 1; continue; } /* We found it was composite or proved it */ goto end_down; } /* k loop for D */ } /* D */ } /* fac stage */ /* Nothing at this level */ if (downresult != 1) croak("ECPP internal error: downresult is %d at end\n", downresult); if (verbose) { if (*pmaxH > 0) printf(" (max %d)", *pmaxH); printf(" ---\n"); fflush(stdout); } if (*pmaxH > 0) *pmaxH = *pmaxH + 2; end_down: if (downresult == 2) { if (0 && verbose > 1) { gmp_printf("\n"); if (D == 1) { gmp_printf("Type BLS3\nN %Zd\nQ %Zd\nA %"UVuf"\n", Ni, q, nm1a); } else if (D == -1) { gmp_printf("Type BLS15\nN %Zd\nQ %Zd\nLP %"IVdf"\nLQ %"IVdf"\n", Ni, q, np1lp, np1lq); } else { gmp_printf("Type ECPP\nN %Zd\nA %Zd\nB %Zd\nM %Zd\nQ %Zd\nX %Zd\nY %Zd\n", Ni, a, b, m, q, P.x, P.y); } gmp_printf("\n"); fflush(stdout); } /* Prepend our proof to anything that exists. */ if (prooftextptr != 0) { char *proofstr, *proofptr; int curprooflen = (*prooftextptr == 0) ? 0 : strlen(*prooftextptr); if (D == 1) { int myprooflen = 20 + 2*(4 + mpz_sizeinbase(Ni, 10)) + 1*21; New(0, proofstr, myprooflen + curprooflen + 1, char); proofptr = proofstr; proofptr += gmp_sprintf(proofptr, "Type BLS3\nN %Zd\nQ %Zd\n", Ni,q); proofptr += sprintf(proofptr, "A %"UVuf"\n", nm1a); } else if (D == -1) {
int encrypt_vec_to_file( int vsizelocal, const char * input_file_name, const char * output_file_name, const char * key_file_name) { int input_size = 0, i, temp; mpz_t *vec1; char* temp_str = NULL; FILE *input_file, *output_file; vsize = vsizelocal; input_file = fopen(input_file_name, "r"); output_file = fopen(output_file_name, "w"); strncpy(g_key_file_name, key_file_name, sizeof(g_key_file_name)); printf("Number of vector dimensions = %d\n", vsizelocal); //printf("p_vec2:%p, ENOMEM:%d\n", p_vec2, (errno == ENOMEM)?1:0); //initialize vectors and big number variables //Dynamically creating the array vec1 = (mpz_t *)malloc(vsizelocal*sizeof(mpz_t)); //We have to write encrypted values to file //to hold each value in string before we need //a large string to copy the big number //Although the key size is 4096 bits, //we will allocate the chars capable of storing //8192 bit values. No. of chars required = //log(2^{8192}) base 10 = keysize*log 2 = 8192 * log 2 = 2466.03<2500 temp_str = (char *)malloc(BIG_NUM_SZ); //initialize vectors and big number variables for (i = 0; i < vsizelocal; i++) mpz_init(*(vec1+i)); //variables are set to 0 init(); for (i = 0; i < vsizelocal; i++) { mpz_init(*(vec1+i)); } //variables are set to 0 //init(); //check if files are opened properly if (input_file == NULL) { printf("\n%s", "Error: open input_file!"); return -2; } if (output_file == NULL) { printf("\n%s", "Error: open output_file!"); return -3; } //fill in the first vector for( fscanf(input_file,"%d", &temp); temp != EOF && input_size < vsizelocal; fscanf(input_file, "%d", &temp) ){ //temp = (int) temp2; //printf("doc1:: temp2: %" PRId64 ", temp:%d\n", temp2, temp); printf("doc1::Wt:%d\n", temp); encrypt(*(vec1+input_size), temp); //gmp_printf("No. of chars:%d, BIGNO:%s\n", gmp_sprintf(temp_str, "%Zd", *(vec1+input_size)), temp_str); //TODO: Optimization area use fprintf directly using %Zd instead of temp_str. No memory needed. gmp_sprintf(temp_str, "%Zd", *(vec1+input_size)); //decrypt(vec1[input_size]); //gmp_printf("%d: %Zd\n", input_size, *(vec1+input_size)); fprintf(output_file, "%s", temp_str); fflush(output_file); sync(); input_size ++; if ( vsizelocal!=1 && input_size < vsizelocal ) { fprintf(output_file, "\n"); } } fclose(input_file); fflush(output_file); fclose(output_file); //release space used by big number variables for (i = 0; i < vsizelocal; i++) mpz_clear(*(vec1+i)); clear(); free(vec1); free(temp_str); return 0; }