annotate libtommath/demo/demo.c @ 1551:1acbdf64088e

add guard HAVE_GETGROUPLIST
author Matt Johnston <matt@ucc.asn.au>
date Tue, 27 Feb 2018 21:49:10 +0800
parents 8bba51a55704
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
1 #include <string.h>
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include <time.h>
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 #ifdef IOWNANATHLON
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 #include <unistd.h>
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #define SLEEP sleep(4)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #define SLEEP
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
11 /*
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
12 * Configuration
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
13 */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
14 #ifndef LTM_DEMO_TEST_VS_MTEST
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
15 #define LTM_DEMO_TEST_VS_MTEST 1
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
16 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
17
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
18 #ifndef LTM_DEMO_TEST_REDUCE_2K_L
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
19 /* This test takes a moment so we disable it by default, but it can be:
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
20 * 0 to disable testing
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
21 * 1 to make the test with P = 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
22 * 2 to make the test with P = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
23 */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
24 #define LTM_DEMO_TEST_REDUCE_2K_L 0
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
25 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
26
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
27 #ifdef LTM_DEMO_REAL_RAND
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
28 #define LTM_DEMO_RAND_SEED time(NULL)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
29 #else
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
30 #define LTM_DEMO_RAND_SEED 23
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
31 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
32
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "tommath.h"
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 void ndraw(mp_int * a, char *name)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 char buf[16000];
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 printf("%s: ", name);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 mp_toradix(a, buf, 10);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 printf("%s\n", buf);
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
42 mp_toradix(a, buf, 16);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
43 printf("0x%s\n", buf);
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
46 #if LTM_DEMO_TEST_VS_MTEST
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 static void draw(mp_int * a)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 ndraw(a, "");
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 }
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
51 #endif
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 unsigned long lfsr = 0xAAAAAAAAUL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 int lbit(void)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if (lfsr & 0x80000000UL) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 } else {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 lfsr <<= 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 return 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
67 #if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
68 static FILE* fd_urandom;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
69 #endif
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 int myrng(unsigned char *dst, int len, void *dat)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 int x;
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
73 (void)dat;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
74 #if defined(LTM_DEMO_REAL_RAND)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
75 if (!fd_urandom) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
76 #if !defined(_WIN32)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
77 fprintf(stderr, "\nno /dev/urandom\n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
78 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
79 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
80 else {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
81 return fread(dst, 1, len, fd_urandom);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
82 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
83 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
84 for (x = 0; x < len; ) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
85 unsigned int r = (unsigned int)rand();
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
86 do {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
87 dst[x++] = r & 0xFF;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
88 r >>= 8;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
89 } while((r != 0) && (x < len));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
90 }
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 return len;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
94 #if LTM_DEMO_TEST_VS_MTEST != 0
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
95 static void _panic(int l)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
96 {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
97 fprintf(stderr, "\n%d: fgets failed\n", l);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
98 exit(EXIT_FAILURE);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
99 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
100 #endif
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
102 mp_int a, b, c, d, e, f;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
103
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
104 static void _cleanup(void)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
105 {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
106 mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
107 printf("\n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
108
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
109 #ifdef LTM_DEMO_REAL_RAND
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
110 if(fd_urandom)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
111 fclose(fd_urandom);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
112 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
113 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
114 struct mp_sqrtmod_prime_st {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
115 unsigned long p;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
116 unsigned long n;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
117 mp_digit r;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
118 };
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
119 struct mp_sqrtmod_prime_st sqrtmod_prime[] = {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
120 { 5, 14, 3 },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
121 { 7, 9, 4 },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
122 { 113, 2, 62 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
123 };
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
124 struct mp_jacobi_st {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
125 unsigned long n;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
126 int c[16];
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
127 };
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
128 struct mp_jacobi_st jacobi[] = {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
129 { 3, { 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
130 { 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
131 { 7, { 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
132 { 9, { -1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } },
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
133 };
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 char cmd[4096], buf[4096];
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 int main(void)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 {
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
138 unsigned rr;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
139 int cnt, ix;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
140 #if LTM_DEMO_TEST_VS_MTEST
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n,
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
142 gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
143 char* ret;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
144 #else
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
145 unsigned long s, t;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
146 unsigned long long q, r;
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 mp_digit mp;
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
148 int i, n, err, should;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
149 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
150
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
151 if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
152 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
153
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
154 atexit(_cleanup);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
155
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
156 #if defined(LTM_DEMO_REAL_RAND)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
157 if (!fd_urandom) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
158 fd_urandom = fopen("/dev/urandom", "r");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
159 if (!fd_urandom) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
160 #if !defined(_WIN32)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
161 fprintf(stderr, "\ncould not open /dev/urandom\n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
162 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
163 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
164 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
165 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
166 srand(LTM_DEMO_RAND_SEED);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
167
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
168 #ifdef MP_8BIT
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
169 printf("Digit size 8 Bit \n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
170 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
171 #ifdef MP_16BIT
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
172 printf("Digit size 16 Bit \n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
173 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
174 #ifdef MP_32BIT
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
175 printf("Digit size 32 Bit \n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
176 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
177 #ifdef MP_64BIT
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
178 printf("Digit size 64 Bit \n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
179 #endif
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
180 printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
181 printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
182 printf("DIGIT_BIT: %d\n", DIGIT_BIT);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
183 printf("MP_PREC: %d\n", MP_PREC);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
184
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
185 #if LTM_DEMO_TEST_VS_MTEST == 0
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
186 // trivial stuff
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
187 // a: 0->5
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
188 mp_set_int(&a, 5);
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
189 // a: 5-> b: -5
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
190 mp_neg(&a, &b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
191 if (mp_cmp(&a, &b) != MP_GT) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
192 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
193 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
194 if (mp_cmp(&b, &a) != MP_LT) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
195 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
196 }
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
197 // a: 5-> a: -5
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
198 mp_neg(&a, &a);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
199 if (mp_cmp(&b, &a) != MP_EQ) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
200 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
201 }
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
202 // a: -5-> b: 5
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
203 mp_abs(&a, &b);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
204 if (mp_isneg(&b) != MP_NO) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
205 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
206 }
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
207 // a: -5-> b: -4
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
208 mp_add_d(&a, 1, &b);
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
209 if (mp_isneg(&b) != MP_YES) {
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
210 return EXIT_FAILURE;
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
211 }
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
212 if (mp_get_int(&b) != 4) {
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
213 return EXIT_FAILURE;
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
214 }
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
215 // a: -5-> b: 1
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
216 mp_add_d(&a, 6, &b);
1470
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
217 if (mp_get_int(&b) != 1) {
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
218 return EXIT_FAILURE;
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
219 }
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
220 // a: -5-> a: 1
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
221 mp_add_d(&a, 6, &a);
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
222 if (mp_get_int(&a) != 1) {
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
223 return EXIT_FAILURE;
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
224 }
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
225 mp_zero(&a);
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
226 // a: 0-> a: 6
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
227 mp_add_d(&a, 6, &a);
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
228 if (mp_get_int(&a) != 6) {
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
229 return EXIT_FAILURE;
8bba51a55704 Update to libtommath v1.0.1
Matt Johnston <matt@ucc.asn.au>
parents: 1436
diff changeset
230 }
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232
1436
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
233 mp_set_int(&a, 0);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
234 mp_set_int(&b, 1);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
235 if ((err = mp_jacobi(&a, &b, &i)) != MP_OKAY) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
236 printf("Failed executing mp_jacobi(0 | 1) %s.\n", mp_error_to_string(err));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
237 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
238 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
239 if (i != 1) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
240 printf("Failed trivial mp_jacobi(0 | 1) %d != 1\n", i);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
241 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
242 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
243 for (cnt = 0; cnt < (int)(sizeof(jacobi)/sizeof(jacobi[0])); ++cnt) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
244 mp_set_int(&b, jacobi[cnt].n);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
245 /* only test positive values of a */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
246 for (n = -5; n <= 10; ++n) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
247 mp_set_int(&a, abs(n));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
248 should = MP_OKAY;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
249 if (n < 0) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
250 mp_neg(&a, &a);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
251 /* Until #44 is fixed the negative a's must fail */
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
252 should = MP_VAL;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
253 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
254 if ((err = mp_jacobi(&a, &b, &i)) != should) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
255 printf("Failed executing mp_jacobi(%d | %lu) %s.\n", n, jacobi[cnt].n, mp_error_to_string(err));
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
256 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
257 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
258 if (err == MP_OKAY && i != jacobi[cnt].c[n + 5]) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
259 printf("Failed trivial mp_jacobi(%d | %lu) %d != %d\n", n, jacobi[cnt].n, i, jacobi[cnt].c[n + 5]);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
260 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
261 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
262 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
263 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
264
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
265 // test mp_get_int
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
266 printf("\n\nTesting: mp_get_int");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
267 for (i = 0; i < 1000; ++i) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
268 t = ((unsigned long) rand () * rand () + 1) & 0xFFFFFFFF;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
269 mp_set_int (&a, t);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
270 if (t != mp_get_int (&a)) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
271 printf ("\nmp_get_int() bad result!");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
272 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
273 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
274 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
275 mp_set_int(&a, 0);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
276 if (mp_get_int(&a) != 0) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
277 printf("\nmp_get_int() bad result!");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
278 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
279 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
280 mp_set_int(&a, 0xffffffff);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
281 if (mp_get_int(&a) != 0xffffffff) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
282 printf("\nmp_get_int() bad result!");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
283 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
284 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
285
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
286 printf("\n\nTesting: mp_get_long\n");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
287 for (i = 0; i < (int)(sizeof(unsigned long)*CHAR_BIT) - 1; ++i) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
288 t = (1ULL << (i+1)) - 1;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
289 if (!t)
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
290 t = -1;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
291 printf(" t = 0x%lx i = %d\r", t, i);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
292 do {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
293 if (mp_set_long(&a, t) != MP_OKAY) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
294 printf("\nmp_set_long() error!");
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
295 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
296 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
297 s = mp_get_long(&a);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
298 if (s != t) {
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
299 printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
300 return EXIT_FAILURE;
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
301 }
60fc6476e044 Update to libtommath v1.0
Matt Johnston <matt@ucc.asn.au>
parents: 389
diff changeset
302 t <<= 1;