work a bit on code coverage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
diff --git a/demo/demo.c b/demo/demo.c
index 36c397d..f1ab899 100644
--- a/demo/demo.c
+++ b/demo/demo.c
@@ -165,6 +165,26 @@ int main(void)
printf("MP_PREC: %d\n", MP_PREC);
#if LTM_DEMO_TEST_VS_MTEST == 0
+ // trivial stuff
+ mp_set_int(&a, 5);
+ mp_neg(&a, &b);
+ if (mp_cmp(&a, &b) != MP_GT) {
+ return EXIT_FAILURE;
+ }
+ if (mp_cmp(&b, &a) != MP_LT) {
+ return EXIT_FAILURE;
+ }
+ mp_neg(&a, &a);
+ if (mp_cmp(&b, &a) != MP_EQ) {
+ return EXIT_FAILURE;
+ }
+ mp_abs(&a, &b);
+ if (mp_isneg(&b) != MP_NO) {
+ return EXIT_FAILURE;
+ }
+ mp_add_d(&a, 1, &b);
+ mp_add_d(&a, 6, &b);
+
// test montgomery
printf("Testing: montgomery...\n");
for (i = 1; i < 10; i++) {
@@ -202,11 +222,11 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
// test mp_get_int
printf("\n\nTesting: mp_get_int");
for (i = 0; i < 1000; ++i) {
- t = ((unsigned long) rand() * rand() + 1) & 0xFFFFFFFF;
- mp_set_int(&a, t);
- if (t != mp_get_int(&a)) {
- printf("\nmp_get_int() bad result!");
- return EXIT_FAILURE;
+ t = ((unsigned long) rand () * rand () + 1) & 0xFFFFFFFF;
+ mp_set_int (&a, t);
+ if (t != mp_get_int (&a)) {
+ printf ("\nmp_get_int() bad result!");
+ return EXIT_FAILURE;
}
}
mp_set_int(&a, 0);
@@ -263,53 +283,53 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
// test mp_sqrt
printf("\n\nTesting: mp_sqrt\n");
for (i = 0; i < 1000; ++i) {
- printf("%6d\r", i);
- fflush(stdout);
- n = (rand() & 15) + 1;
- mp_rand(&a, n);
- if (mp_sqrt(&a, &b) != MP_OKAY) {
- printf("\nmp_sqrt() error!");
- return EXIT_FAILURE;
+ printf ("%6d\r", i);
+ fflush (stdout);
+ n = (rand () & 15) + 1;
+ mp_rand (&a, n);
+ if (mp_sqrt (&a, &b) != MP_OKAY) {
+ printf ("\nmp_sqrt() error!");
+ return EXIT_FAILURE;
}
- mp_n_root_ex(&a, 2, &c, 0);
- mp_n_root_ex(&a, 2, &d, 1);
- if (mp_cmp_mag(&c, &d) != MP_EQ) {
- printf("\nmp_n_root_ex() bad result!");
- return EXIT_FAILURE;
+ mp_n_root_ex (&a, 2, &c, 0);
+ mp_n_root_ex (&a, 2, &d, 1);
+ if (mp_cmp_mag (&c, &d) != MP_EQ) {
+ printf ("\nmp_n_root_ex() bad result!");
+ return EXIT_FAILURE;
}
- if (mp_cmp_mag(&b, &c) != MP_EQ) {
- printf("mp_sqrt() bad result!\n");
- return 1;
+ if (mp_cmp_mag (&b, &c) != MP_EQ) {
+ printf ("mp_sqrt() bad result!\n");
+ return EXIT_FAILURE;
}
}
printf("\n\nTesting: mp_is_square\n");
for (i = 0; i < 1000; ++i) {
- printf("%6d\r", i);
- fflush(stdout);
+ printf ("%6d\r", i);
+ fflush (stdout);
/* test mp_is_square false negatives */
- n = (rand() & 7) + 1;
- mp_rand(&a, n);
- mp_sqr(&a, &a);
- if (mp_is_square(&a, &n) != MP_OKAY) {
- printf("\nfn:mp_is_square() error!");
- return EXIT_FAILURE;
+ n = (rand () & 7) + 1;
+ mp_rand (&a, n);
+ mp_sqr (&a, &a);
+ if (mp_is_square (&a, &n) != MP_OKAY) {
+ printf ("\nfn:mp_is_square() error!");
+ return EXIT_FAILURE;
}
if (n == 0) {
- printf("\nfn:mp_is_square() bad result!");
- return EXIT_FAILURE;
+ printf ("\nfn:mp_is_square() bad result!");
+ return EXIT_FAILURE;
}
/* test for false positives */
- mp_add_d(&a, 1, &a);
- if (mp_is_square(&a, &n) != MP_OKAY) {
- printf("\nfp:mp_is_square() error!");
- return EXIT_FAILURE;
+ mp_add_d (&a, 1, &a);
+ if (mp_is_square (&a, &n) != MP_OKAY) {
+ printf ("\nfp:mp_is_square() error!");
+ return EXIT_FAILURE;
}
if (n == 1) {
- printf("\nfp:mp_is_square() bad result!");
- return EXIT_FAILURE;
+ printf ("\nfp:mp_is_square() bad result!");
+ return EXIT_FAILURE;
}
}
@@ -332,45 +352,43 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
/* test for size */
for (ix = 10; ix < 128; ix++) {
- printf("Testing (not safe-prime): %9d bits \r", ix);
- fflush(stdout);
- err =
- mp_prime_random_ex(&a, 8, ix,
- (rand() & 1) ? 0 : LTM_PRIME_2MSB_ON,
- myrng, NULL);
+ printf ("Testing (not safe-prime): %9d bits \r", ix);
+ fflush (stdout);
+ err = mp_prime_random_ex (&a, 8, ix,
+ (rand () & 1) ? 0 : LTM_PRIME_2MSB_ON, myrng,
+ NULL);
if (err != MP_OKAY) {
- printf("failed with err code %d\n", err);
- return EXIT_FAILURE;
+ printf ("failed with err code %d\n", err);
+ return EXIT_FAILURE;
}
- if (mp_count_bits(&a) != ix) {
- printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
- return EXIT_FAILURE;
+ if (mp_count_bits (&a) != ix) {
+ printf ("Prime is %d not %d bits!!!\n", mp_count_bits (&a), ix);
+ return EXIT_FAILURE;
}
}
printf("\n");
for (ix = 16; ix < 128; ix++) {
- printf("Testing ( safe-prime): %9d bits \r", ix);
- fflush(stdout);
- err =
- mp_prime_random_ex(&a, 8, ix,
- ((rand() & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
- myrng, NULL);
+ printf ("Testing ( safe-prime): %9d bits \r", ix);
+ fflush (stdout);
+ err = mp_prime_random_ex (
+ &a, 8, ix, ((rand () & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
+ myrng, NULL);
if (err != MP_OKAY) {
- printf("failed with err code %d\n", err);
- return EXIT_FAILURE;
+ printf ("failed with err code %d\n", err);
+ return EXIT_FAILURE;
}
- if (mp_count_bits(&a) != ix) {
- printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
- return EXIT_FAILURE;
+ if (mp_count_bits (&a) != ix) {
+ printf ("Prime is %d not %d bits!!!\n", mp_count_bits (&a), ix);
+ return EXIT_FAILURE;
}
/* let's see if it's really a safe prime */
- mp_sub_d(&a, 1, &a);
- mp_div_2(&a, &a);
- mp_prime_is_prime(&a, 8, &cnt);
+ mp_sub_d (&a, 1, &a);
+ mp_div_2 (&a, &a);
+ mp_prime_is_prime (&a, 8, &cnt);
if (cnt != MP_YES) {
- printf("sub is not prime!\n");
- return EXIT_FAILURE;
+ printf ("sub is not prime!\n");
+ return EXIT_FAILURE;
}
}
@@ -399,11 +417,11 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
printf("\n\nTesting: mp_cnt_lsb");
mp_set(&a, 1);
for (ix = 0; ix < 1024; ix++) {
- if (mp_cnt_lsb(&a) != ix) {
- printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a));
- return EXIT_FAILURE;
+ if (mp_cnt_lsb (&a) != ix) {
+ printf ("Failed at %d, %d\n", ix, mp_cnt_lsb (&a));
+ return EXIT_FAILURE;
}
- mp_mul_2(&a, &a);
+ mp_mul_2 (&a, &a);
}
/* test mp_reduce_2k */
@@ -411,27 +429,26 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
for (cnt = 3; cnt <= 128; ++cnt) {
mp_digit tmp;
- mp_2expt(&a, cnt);
- mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */
-
+ mp_2expt (&a, cnt);
+ mp_sub_d (&a, 2, &a); /* a = 2**cnt - 2 */
- printf("\r %4d bits", cnt);
- printf("(%d)", mp_reduce_is_2k(&a));
- mp_reduce_2k_setup(&a, &tmp);
- printf("(%lu)", (unsigned long)tmp);
+ printf ("\r %4d bits", cnt);
+ printf ("(%d)", mp_reduce_is_2k (&a));
+ mp_reduce_2k_setup (&a, &tmp);
+ printf ("(%lu)", (unsigned long) tmp);
for (ix = 0; ix < 1000; ix++) {
- if (!(ix & 127)) {
- printf(".");
- fflush(stdout);
- }
- mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2);
- mp_copy(&c, &b);
- mp_mod(&c, &a, &c);
- mp_reduce_2k(&b, &a, 2);
- if (mp_cmp(&c, &b)) {
- printf("FAILED\n");
- exit(0);
- }
+ if (!(ix & 127)) {
+ printf (".");
+ fflush (stdout);
+ }
+ mp_rand (&b, (cnt / DIGIT_BIT + 1) * 2);
+ mp_copy (&c, &b);
+ mp_mod (&c, &a, &c);
+ mp_reduce_2k (&b, &a, 2);
+ if (mp_cmp (&c, &b)) {
+ printf ("FAILED\n");
+ return EXIT_FAILURE;
+ }
}
}
@@ -459,39 +476,39 @@ printf("compare no compare!\n"); return EXIT_FAILURE; }
/* test the DR reduction */
printf("\n\nTesting: mp_dr_reduce...\n");
for (cnt = 2; cnt < 32; cnt++) {
- printf("\r%d digit modulus", cnt);
- mp_grow(&a, cnt);
- mp_zero(&a);
+ printf ("\r%d digit modulus", cnt);
+ mp_grow (&a, cnt);
+ mp_zero (&a);
for (ix = 1; ix < cnt; ix++) {
- a.dp[ix] = MP_MASK;
+ a.dp[ix] = MP_MASK;
}
a.used = cnt;
a.dp[0] = 3;
- mp_rand(&b, cnt - 1);
- mp_copy(&b, &c);
+ mp_rand (&b, cnt - 1);
+ mp_copy (&b, &c);
rr = 0;
do {
- if (!(rr & 127)) {
- printf(".");
- fflush(stdout);
- }
- mp_sqr(&b, &b);
- mp_add_d(&b, 1, &b);
- mp_copy(&b, &c);
-
- mp_mod(&b, &a, &b);
- mp_dr_reduce(&c, &a, (((mp_digit) 1) << DIGIT_BIT) - a.dp[0]);
+ if (!(rr & 127)) {
+ printf (".");
+ fflush (stdout);
+ }
+ mp_sqr (&b, &b);
+ mp_add_d (&b, 1, &b);
+ mp_copy (&b, &c);
- if (mp_cmp(&b, &c) != MP_EQ) {
- printf("Failed on trial %u\n", rr);
- exit(-1);
+ mp_mod (&b, &a, &b);
+ mp_dr_setup(&a, &mp),
+ mp_dr_reduce (&c, &a, mp);
- }
+ if (mp_cmp (&b, &c) != MP_EQ) {
+ printf ("Failed on trial %u\n", rr);
+ return EXIT_FAILURE;
+ }
} while (++rr < 500);
- printf(" passed");
- fflush(stdout);
+ printf (" passed");
+ fflush (stdout);
}
#if LTM_DEMO_TEST_REDUCE_2K_L
diff --git a/demo/timing.c b/demo/timing.c
index cf94772..233872a 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -1,5 +1,6 @@
#include <tommath.h>
#include <time.h>
+#include <unistd.h>
ulong64 _tt;