1f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#include <time.h> 2f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 3f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifdef IOWNANATHLON 4f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#include <unistd.h> 5f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define SLEEP sleep(4) 6f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#else 7f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define SLEEP 8f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif 9f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 10f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#include "tommath.h" 11f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 12f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectvoid ndraw(mp_int * a, char *name) 13f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{ 14f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project char buf[16000]; 15f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 16f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%s: ", name); 17f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_toradix(a, buf, 10); 18f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%s\n", buf); 19f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} 20f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 21f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectstatic void draw(mp_int * a) 22f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{ 23f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ndraw(a, ""); 24f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} 25f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 26f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 27f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectunsigned long lfsr = 0xAAAAAAAAUL; 28f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 29f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectint lbit(void) 30f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{ 31f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (lfsr & 0x80000000UL) { 32f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL; 33f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 34f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else { 35f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project lfsr <<= 1; 36f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 37f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 38f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} 39f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 40f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectint myrng(unsigned char *dst, int len, void *dat) 41f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{ 42f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project int x; 43f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 44f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (x = 0; x < len; x++) 45f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project dst[x] = rand() & 0xFF; 46f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return len; 47f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} 48f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 49f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 50f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 51f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectchar cmd[4096], buf[4096]; 52f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectint main(void) 53f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project{ 54f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_int a, b, c, d, e, f; 55f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, 56f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n, t; 57f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project unsigned rr; 58f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project int i, n, err, cnt, ix, old_kara_m, old_kara_s; 59f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_digit mp; 60f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 61f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 62f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&a); 63f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&b); 64f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&c); 65f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&d); 66f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&e); 67f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&f); 68f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 69f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project srand(time(NULL)); 70f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 71f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0 72f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project // test montgomery 73f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing montgomery...\n"); 74f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (i = 1; i < 10; i++) { 75f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing digit size: %d\n", i); 76f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (n = 0; n < 1000; n++) { 77f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&a, i); 78f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.dp[0] |= 1; 79f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 80f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project // let's see if R is right 81f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_montgomery_calc_normalization(&b, &a); 82f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_montgomery_setup(&a, &mp); 83f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 84f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project // now test a random reduction 85f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 0; ix < 100; ix++) { 86f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&c, 1 + abs(rand()) % (2*i)); 87f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&c, &d); 88f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&c, &e); 89f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 90f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mod(&d, &a, &d); 91f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_montgomery_reduce(&c, &a, mp); 92f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mulmod(&c, &b, &a, &c); 93f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 94f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 95f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectprintf("d = e mod a, c = e MOD a\n"); 96f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectmp_todecimal(&a, buf); printf("a = %s\n", buf); 97f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectmp_todecimal(&e, buf); printf("e = %s\n", buf); 98f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectmp_todecimal(&d, buf); printf("d = %s\n", buf); 99f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectmp_todecimal(&c, buf); printf("c = %s\n", buf); 100f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectprintf("compare no compare!\n"); exit(EXIT_FAILURE); } 101f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 102f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 103f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 104f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("done\n"); 105f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 106f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project // test mp_get_int 107f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing: mp_get_int\n"); 108f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (i = 0; i < 1000; ++i) { 109f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project t = ((unsigned long) rand() * rand() + 1) & 0xFFFFFFFF; 110f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_set_int(&a, t); 111f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (t != mp_get_int(&a)) { 112f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_get_int() bad result!\n"); 113f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 114f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 115f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 116f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_set_int(&a, 0); 117f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_get_int(&a) != 0) { 118f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_get_int() bad result!\n"); 119f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 120f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 121f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_set_int(&a, 0xffffffff); 122f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_get_int(&a) != 0xffffffff) { 123f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_get_int() bad result!\n"); 124f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 125f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 126f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project // test mp_sqrt 127f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing: mp_sqrt\n"); 128f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (i = 0; i < 1000; ++i) { 129f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%6d\r", i); 130f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 131f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project n = (rand() & 15) + 1; 132f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&a, n); 133f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_sqrt(&a, &b) != MP_OKAY) { 134f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_sqrt() error!\n"); 135f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 136f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 137f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_n_root(&a, 2, &a); 138f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp_mag(&b, &a) != MP_EQ) { 139f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_sqrt() bad result!\n"); 140f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 141f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 142f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 143f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 144f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\nTesting: mp_is_square\n"); 145f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (i = 0; i < 1000; ++i) { 146f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%6d\r", i); 147f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 148f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 149f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* test mp_is_square false negatives */ 150f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project n = (rand() & 7) + 1; 151f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&a, n); 152f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sqr(&a, &a); 153f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_is_square(&a, &n) != MP_OKAY) { 154f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("fn:mp_is_square() error!\n"); 155f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 156f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 157f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (n == 0) { 158f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("fn:mp_is_square() bad result!\n"); 159f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 160f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 161f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 162f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* test for false positives */ 163f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add_d(&a, 1, &a); 164f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_is_square(&a, &n) != MP_OKAY) { 165f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("fp:mp_is_square() error!\n"); 166f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 167f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 168f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (n == 1) { 169f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("fp:mp_is_square() bad result!\n"); 170f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 1; 171f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 172f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 173f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 174f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\n\n"); 175f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 176f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* test for size */ 177f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 10; ix < 128; ix++) { 178f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing (not safe-prime): %9d bits \r", ix); 179f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 180f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project err = 181f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_prime_random_ex(&a, 8, ix, 182f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project (rand() & 1) ? LTM_PRIME_2MSB_OFF : 183f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project LTM_PRIME_2MSB_ON, myrng, NULL); 184f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (err != MP_OKAY) { 185f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("failed with err code %d\n", err); 186f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 187f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 188f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_count_bits(&a) != ix) { 189f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix); 190f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 191f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 192f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 193f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 194f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 16; ix < 128; ix++) { 195f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing ( safe-prime): %9d bits \r", ix); 196f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 197f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project err = 198f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_prime_random_ex(&a, 8, ix, 199f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ((rand() & 1) ? LTM_PRIME_2MSB_OFF : 200f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE, myrng, 201f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project NULL); 202f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (err != MP_OKAY) { 203f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("failed with err code %d\n", err); 204f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 205f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 206f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_count_bits(&a) != ix) { 207f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix); 208f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 209f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 210f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* let's see if it's really a safe prime */ 211f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub_d(&a, 1, &a); 212f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div_2(&a, &a); 213f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_prime_is_prime(&a, 8, &cnt); 214f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (cnt != MP_YES) { 215f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("sub is not prime!\n"); 216f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 217f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 218f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 219f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 220f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\n\n"); 221f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 222f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, "123456", 10); 223f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_toradix_n(&a, buf, 10, 3); 224f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("a == %s\n", buf); 225f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_toradix_n(&a, buf, 10, 4); 226f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("a == %s\n", buf); 227f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_toradix_n(&a, buf, 10, 30); 228f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("a == %s\n", buf); 229f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 230f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 231f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0 232f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (;;) { 233f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, sizeof(buf), stdin); 234f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 10); 235f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_prime_next_prime(&a, 5, 1); 236f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_toradix(&a, buf, 10); 237f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%s, %lu\n", buf, a.dp[0] & 3); 238f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 239f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif 240f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 241f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* test mp_cnt_lsb */ 242f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("testing mp_cnt_lsb...\n"); 243f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_set(&a, 1); 244f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 0; ix < 1024; ix++) { 245f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cnt_lsb(&a) != ix) { 246f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a)); 247f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 248f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 249f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mul_2(&a, &a); 250f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 251f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 252f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* test mp_reduce_2k */ 253f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing mp_reduce_2k...\n"); 254f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (cnt = 3; cnt <= 128; ++cnt) { 255f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_digit tmp; 256f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 257f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_2expt(&a, cnt); 258f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */ 259f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 260f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 261f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\nTesting %4d bits", cnt); 262f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("(%d)", mp_reduce_is_2k(&a)); 263f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_reduce_2k_setup(&a, &tmp); 264f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("(%d)", tmp); 265f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 0; ix < 1000; ix++) { 266f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (!(ix & 127)) { 267f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("."); 268f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 269f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 270f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2); 271f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&c, &b); 272f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mod(&c, &a, &c); 273f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_reduce_2k(&b, &a, 2); 274f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &b)) { 275f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("FAILED\n"); 276f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project exit(0); 277f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 278f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 279f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 280f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 281f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* test mp_div_3 */ 282f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Testing mp_div_3...\n"); 283f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_set(&d, 3); 284f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (cnt = 0; cnt < 10000;) { 285f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_digit r1, r2; 286f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 287f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (!(++cnt & 127)) 288f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%9d\r", cnt); 289f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&a, abs(rand()) % 128 + 1); 290f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div(&a, &d, &b, &e); 291f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div_3(&a, &c, &r2); 292f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 293f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) || mp_cmp_d(&e, r2)) { 294f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\n\nmp_div_3 => Failure\n"); 295f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 296f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 297f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("\n\nPassed div_3 testing\n"); 298f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 299f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* test the DR reduction */ 300f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("testing mp_dr_reduce...\n"); 301f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (cnt = 2; cnt < 32; cnt++) { 302f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%d digit modulus\n", cnt); 303f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_grow(&a, cnt); 304f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_zero(&a); 305f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (ix = 1; ix < cnt; ix++) { 306f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.dp[ix] = MP_MASK; 307f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 308f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.used = cnt; 309f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.dp[0] = 3; 310f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 311f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&b, cnt - 1); 312f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&b, &c); 313f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 314f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project rr = 0; 315f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project do { 316f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (!(rr & 127)) { 317f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%9lu\r", rr); 318f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 319f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 320f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sqr(&b, &b); 321f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add_d(&b, 1, &b); 322f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&b, &c); 323f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 324f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mod(&b, &a, &b); 325f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_dr_reduce(&c, &a, (((mp_digit) 1) << DIGIT_BIT) - a.dp[0]); 326f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 327f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) != MP_EQ) { 328f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Failed on trial %lu\n", rr); 329f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project exit(-1); 330f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 331f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 332f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } while (++rr < 500); 333f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("Passed DR test for %d digits\n", cnt); 334f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 335f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 336f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif 337f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 338f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* test the mp_reduce_2k_l code */ 339f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0 340f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0 341f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* first load P with 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF */ 342f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_2expt(&a, 1024); 343f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, "2A434B9FDEC95D8F9D550FFFFFFFFFFFFFFFF", 16); 344f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub(&a, &b, &a); 345f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#elif 1 346f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* p = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F */ 347f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_2expt(&a, 2048); 348f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, 349f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project "1000000000000000000000000000000004945DDBF8EA2A91D5776399BB83E188F", 350f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 16); 351f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub(&a, &b, &a); 352f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif 353f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 354f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_todecimal(&a, buf); 355f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("p==%s\n", buf); 356f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* now mp_reduce_is_2k_l() should return */ 357f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_reduce_is_2k_l(&a) != 1) { 358f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_reduce_is_2k_l() return 0, should be 1\n"); 359f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 360f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 361f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_reduce_2k_setup_l(&a, &d); 362f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* now do a million square+1 to see if it varies */ 363f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_rand(&b, 64); 364f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mod(&b, &a, &b); 365f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&b, &c); 366f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("testing mp_reduce_2k_l..."); 367f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 368f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (cnt = 0; cnt < (1UL << 20); cnt++) { 369f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sqr(&b, &b); 370f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add_d(&b, 1, &b); 371f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_reduce_2k_l(&b, &a, &d); 372f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sqr(&c, &c); 373f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add_d(&c, 1, &c); 374f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mod(&c, &a, &c); 375f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) != MP_EQ) { 376f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_reduce_2k_l() failed at step %lu\n", cnt); 377f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_tohex(&b, buf); 378f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("b == %s\n", buf); 379f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_tohex(&c, buf); 380f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("c == %s\n", buf); 381f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return EXIT_FAILURE; 382f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 383f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 384f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("...Passed\n"); 385f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif 386f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 387f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n = 388f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n = 389f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sub_d_n = 0; 390f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 391f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* force KARA and TOOM to enable despite cutoffs */ 392f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8; 393f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 16; 394f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 395f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project for (;;) { 396f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* randomly clear and re-init one variable, this has the affect of triming the alloc space */ 397f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project switch (abs(rand()) % 7) { 398f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 0: 399f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&a); 400f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&a); 401f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 402f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 1: 403f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&b); 404f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&b); 405f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 406f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 2: 407f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&c); 408f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&c); 409f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 410f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 3: 411f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&d); 412f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&d); 413f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 414f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 4: 415f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&e); 416f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&e); 417f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 418f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 5: 419f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_clear(&f); 420f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_init(&f); 421f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; 422f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project case 6: 423f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project break; /* don't clear any */ 424f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 425f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 426f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 427f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf 428f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ", 429f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, 430f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n); 431f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(cmd, 4095, stdin); 432f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project cmd[strlen(cmd) - 1] = 0; 433f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("%s ]\r", cmd); 434f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fflush(stdout); 435f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (!strcmp(cmd, "mul2d")) { 436f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++mul2d_n; 437f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 438f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 439f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 440f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sscanf(buf, "%d", &rr); 441f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 442f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 443f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 444f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mul_2d(&a, rr, &a); 445f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.sign = b.sign; 446f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&a, &b) != MP_EQ) { 447f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mul2d failed, rr == %d\n", rr); 448f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 449f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 450f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 451f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 452f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "div2d")) { 453f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++div2d_n; 454f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 455f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 456f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 457f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sscanf(buf, "%d", &rr); 458f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 459f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 460f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 461f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div_2d(&a, rr, &a, &e); 462f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.sign = b.sign; 463f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (a.used == b.used && a.used == 0) { 464f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project a.sign = b.sign = MP_ZPOS; 465f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 466f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&a, &b) != MP_EQ) { 467f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("div2d failed, rr == %d\n", rr); 468f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 469f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 470f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 471f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 472f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "add")) { 473f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++add_n; 474f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 475f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 476f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 477f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 478f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 479f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 480f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &d); 481f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add(&d, &b, &d); 482f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 483f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("add %lu failure!\n", add_n); 484f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 485f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 486f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 487f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 488f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 489f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 490f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 491f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project /* test the sign/unsigned storage functions */ 492f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 493f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project rr = mp_signed_bin_size(&c); 494f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_to_signed_bin(&c, (unsigned char *) cmd); 495f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project memset(cmd + rr, rand() & 255, sizeof(cmd) - rr); 496f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_signed_bin(&d, (unsigned char *) cmd, rr); 497f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 498f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_signed_bin failure!\n"); 499f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 500f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 501f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 502f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 503f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 504f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 505f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project rr = mp_unsigned_bin_size(&c); 506f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_to_unsigned_bin(&c, (unsigned char *) cmd); 507f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project memset(cmd + rr, rand() & 255, sizeof(cmd) - rr); 508f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_unsigned_bin(&d, (unsigned char *) cmd, rr); 509f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp_mag(&c, &d) != MP_EQ) { 510f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mp_unsigned_bin failure!\n"); 511f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 512f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 513f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 514f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 515f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 516f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "sub")) { 517f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++sub_n; 518f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 519f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 520f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 521f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 522f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 523f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 524f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &d); 525f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub(&d, &b, &d); 526f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 527f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("sub %lu failure!\n", sub_n); 528f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 529f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 530f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 531f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 532f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 533f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 534f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "mul")) { 535f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++mul_n; 536f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 537f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 538f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 539f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 540f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 541f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 542f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &d); 543f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mul(&d, &b, &d); 544f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 545f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mul %lu failure!\n", mul_n); 546f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 547f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 548f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 549f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 550f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 551f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 552f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "div")) { 553f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++div_n; 554f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 555f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 556f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 557f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 558f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 559f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 560f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 561f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&d, buf, 64); 562f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 563f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div(&a, &b, &e, &f); 564f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) { 565f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e), 566f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_cmp(&d, &f)); 567f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 568f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 569f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 570f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 571f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&e); 572f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&f); 573f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 574f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 575f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 576f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "sqr")) { 577f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++sqr_n; 578f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 579f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 580f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 581f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 582f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &c); 583f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sqr(&c, &c); 584f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) != MP_EQ) { 585f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("sqr %lu failure!\n", sqr_n); 586f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 587f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 588f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 589f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 590f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 591f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "gcd")) { 592f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++gcd_n; 593f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 594f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 595f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 596f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 597f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 598f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 599f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &d); 600f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_gcd(&d, &b, &d); 601f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project d.sign = c.sign; 602f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 603f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("gcd %lu failure!\n", gcd_n); 604f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 605f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 606f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 607f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 608f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 609f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 610f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "lcm")) { 611f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++lcm_n; 612f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 613f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 614f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 615f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 616f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 617f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 618f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &d); 619f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_lcm(&d, &b, &d); 620f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project d.sign = c.sign; 621f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &d) != MP_EQ) { 622f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("lcm %lu failure!\n", lcm_n); 623f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 624f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 625f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 626f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 627f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 628f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 629f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "expt")) { 630f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++expt_n; 631f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 632f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 633f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 634f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 635f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 636f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 637f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 638f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&d, buf, 64); 639f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_copy(&a, &e); 640f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_exptmod(&e, &b, &c, &e); 641f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&d, &e) != MP_EQ) { 642f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("expt %lu failure!\n", expt_n); 643f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 644f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 645f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 646f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 647f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&e); 648f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 649f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 650f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "invmod")) { 651f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++inv_n; 652f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 653f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 654f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 655f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 656f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 657f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&c, buf, 64); 658f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_invmod(&a, &b, &d); 659f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mulmod(&d, &a, &b, &e); 660f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp_d(&e, 1) != MP_EQ) { 661f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("inv [wrong value from MPI?!] failure\n"); 662f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 663f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 664f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 665f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&d); 666f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_gcd(&a, &b, &e); 667f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&e); 668f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 669f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 670f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 671f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "div2")) { 672f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++div2_n; 673f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 674f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 675f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 676f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 677f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_div_2(&a, &c); 678f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &b) != MP_EQ) { 679f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("div_2 %lu failure\n", div2_n); 680f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 681f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 682f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 683f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 684f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 685f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "mul2")) { 686f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++mul2_n; 687f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 688f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 689f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 690f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 691f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_mul_2(&a, &c); 692f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&c, &b) != MP_EQ) { 693f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("mul_2 %lu failure\n", mul2_n); 694f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 695f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 696f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 697f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 698f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 699f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "add_d")) { 700f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++add_d_n; 701f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 702f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 703f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 704f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sscanf(buf, "%d", &ix); 705f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 706f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 707f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_add_d(&a, ix, &c); 708f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) != MP_EQ) { 709f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("add_d %lu failure\n", add_d_n); 710f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 711f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 712f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 713f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("d == %d\n", ix); 714f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 715f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 716f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } else if (!strcmp(cmd, "sub_d")) { 717f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project ++sub_d_n; 718f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 719f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&a, buf, 64); 720f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 721f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project sscanf(buf, "%d", &ix); 722f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project fgets(buf, 4095, stdin); 723f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_read_radix(&b, buf, 64); 724f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project mp_sub_d(&a, ix, &c); 725f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project if (mp_cmp(&b, &c) != MP_EQ) { 726f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("sub_d %lu failure\n", sub_d_n); 727f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&a); 728f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&b); 729f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project draw(&c); 730f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project printf("d == %d\n", ix); 731f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 732f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 733f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 734f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project } 735f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project return 0; 736f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} 737f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project 738f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Source: /cvs/libtom/libtommath/demo/demo.c,v $ */ 739f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Revision: 1.3 $ */ 740f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Date: 2005/06/24 11:32:07 $ */ 741