int test_sub(testspec_t *t, FILE *ofp) { mp_int in[3], out[1]; int v; mp_result res, expect; if(!parse_int_values(t, in, out, &expect)) return imath_errno = MP_BADARG, 0; if(strcmp(t->code, "subv") == 0) { if((res = mp_int_to_int(in[1], &v)) != MP_OK) return imath_errno = res, 0; if((res = mp_int_sub_value(in[0], v, in[2])) != expect) return imath_errno = res, 0; } else { if((res = mp_int_sub(in[0], in[1], in[2])) != expect) return imath_errno = res, 0; } if(expect == MP_OK && mp_int_compare(in[2], out[0]) != 0) { mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT); return imath_errno = OTHER_ERROR, 0; } return 1; }
/* Compute mul * atan(1/x) to prec digits of precision, and store the result in sum. Computes atan(1/x) using the formula: 1 1 1 1 atan(1/x) = --- - ---- + ---- - ---- + ... x 3x^3 5x^5 7x^7 */ mp_result arctan(mp_small radix, mp_small mul, mp_small x, mp_small prec, mp_int sum) { mpz_t t, v; mp_result res; mp_small rem, sign = 1, coeff = 1; mp_int_init(&t); mp_int_init(&v); ++prec; /* Compute mul * radix^prec * x The initial multiplication by x saves a special case in the loop for the first term of the series. */ if ((res = mp_int_expt_value(radix, prec, &t)) != MP_OK || (res = mp_int_mul_value(&t, mul, &t)) != MP_OK || (res = mp_int_mul_value(&t, x, &t)) != MP_OK) goto CLEANUP; x *= x; /* assumes x <= sqrt(MP_SMALL_MAX) */ mp_int_zero(sum); do { if ((res = mp_int_div_value(&t, x, &t, &rem)) != MP_OK) goto CLEANUP; if ((res = mp_int_div_value(&t, coeff, &v, &rem)) != MP_OK) goto CLEANUP; /* Add or subtract the result depending on the current sign (1 = add) */ if (sign > 0) res = mp_int_add(sum, &v, sum); else res = mp_int_sub(sum, &v, sum); if (res != MP_OK) goto CLEANUP; sign = -sign; coeff += 2; } while (mp_int_compare_zero(&t) != 0); res = mp_int_div_value(sum, radix, sum, NULL); CLEANUP: mp_int_clear(&v); mp_int_clear(&t); return res; }
int main(int argc, char *argv[]) { mp_result res; mpz_t sum1, sum2; int ndigits, out = 0; clock_t start, end; if (argc < 2) { fprintf(stderr, "Usage: %s <num-digits> [<radix>]\n", argv[0]); return 1; } if ((ndigits = abs(atoi(argv[1]))) == 0) { fprintf(stderr, "%s: you must request at least 1 digit\n", argv[0]); return 1; } else if ((mp_word)ndigits > MP_DIGIT_MAX) { fprintf(stderr, "%s: you may request at most %u digits\n", argv[0], (unsigned int)MP_DIGIT_MAX); return 1; } if (argc > 2) { int radix = atoi(argv[2]); if (radix < MP_MIN_RADIX || radix > MP_MAX_RADIX) { fprintf(stderr, "%s: you may only specify a radix between %d and %d\n", argv[0], MP_MIN_RADIX, MP_MAX_RADIX); return 1; } g_radix = radix; } mp_int_init(&sum1); mp_int_init(&sum2); start = clock(); /* sum1 = 16 * arctan(1/5) */ if ((res = arctan(g_radix, 16, 5, ndigits, &sum1)) != MP_OK) { fprintf(stderr, "%s: error computing arctan: %d\n", argv[0], res); out = 1; goto CLEANUP; } /* sum2 = 4 * arctan(1/239) */ if ((res = arctan(g_radix, 4, 239, ndigits, &sum2)) != MP_OK) { fprintf(stderr, "%s: error computing arctan: %d\n", argv[0], res); out = 1; goto CLEANUP; } /* pi = sum1 - sum2 */ if ((res = mp_int_sub(&sum1, &sum2, &sum1)) != MP_OK) { fprintf(stderr, "%s: error computing pi: %d\n", argv[0], res); out = 1; goto CLEANUP; } end = clock(); mp_int_to_string(&sum1, g_radix, g_buf, sizeof(g_buf)); printf("%c.%s\n", g_buf[0], g_buf + 1); fprintf(stderr, "Computation took %.2f sec.\n", (double)(end - start) / CLOCKS_PER_SEC); CLEANUP: mp_int_clear(&sum1); mp_int_clear(&sum2); return out; }
int main(int argc, char *argv[]) { int opt, modbits; FILE *ofp = stdout; char *expt = NULL; rsa_key the_key; mp_result res; /* Process command-line arguments */ while((opt = getopt(argc, argv, "e:")) != EOF) { switch(opt) { case 'e': expt = optarg; break; default: fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n"); return 1; } } if(optind >= argc) { fprintf(stderr, "Error: You must specify the number of modulus bits.\n"); fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n"); return 1; } modbits = (int) strtol(argv[optind++], NULL, 0); if(modbits < CHAR_BIT) { fprintf(stderr, "Error: Invalid value for number of modulus bits.\n"); return 1; } if(modbits % 2 == 1) ++modbits; /* Check if output file is specified */ if(optind < argc) { if((ofp = fopen(argv[optind], "wt")) == NULL) { fprintf(stderr, "Error: Unable to open output file for writing.\n" " - Filename: %s\n" " - Error: %s\n", argv[optind], strerror(errno)); return 1; } } if((res = rsa_key_init(&the_key)) != MP_OK) { fprintf(stderr, "Error initializing RSA key structure:\n" " - %s (%d)\n", mp_error_string(res), res); return 1; } /* If specified, try to load the key exponent */ if(expt != NULL) { if((res = mp_int_read_string(&(the_key.e), 10, expt)) != MP_OK) { fprintf(stderr, "Error: Invalid value for encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } } if((res = mp_int_randomize(&(the_key.p), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize first prime.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } fprintf(stderr, "p: "); find_prime(&(the_key.p), stderr); if((res = mp_int_randomize(&(the_key.q), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize second prime.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } fprintf(stderr, "\nq: "); find_prime(&(the_key.q), stderr); fputc('\n', stderr); /* Temporarily, the key's "n" field will be (p - 1) * (q - 1) for purposes of computing the decryption exponent. */ mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n)); mp_int_sub(&(the_key.n), &(the_key.p), &(the_key.n)); mp_int_sub(&(the_key.n), &(the_key.q), &(the_key.n)); mp_int_add_value(&(the_key.n), 1, &(the_key.n)); if(expt == NULL && (res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } while((res = mp_int_invmod(&(the_key.e), &(the_key.n), &(the_key.d))) != MP_OK) { if(expt != NULL) { fprintf(stderr, "Error: Unable to compute decryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } if((res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to re-randomize encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } } /* Recompute the real modulus, now that exponents are done. */ mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n)); /* Write completed key to the specified output file */ rsa_key_write(&the_key, ofp); EXIT: fclose(ofp); rsa_key_clear(&the_key); return 0; }