Mercurial > dropbear
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 |
rev | line source |
---|---|
1436 | 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 | 11 /* |
12 * Configuration | |
13 */ | |
14 #ifndef LTM_DEMO_TEST_VS_MTEST | |
15 #define LTM_DEMO_TEST_VS_MTEST 1 | |
16 #endif | |
17 | |
18 #ifndef LTM_DEMO_TEST_REDUCE_2K_L | |
19 /* This test takes a moment so we disable it by default, but it can be: | |
20 * 0 to disable testing | |
21 * 1 to make the test with P = 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF | |
22 * 2 to make the test with P = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F | |
23 */ | |
24 #define LTM_DEMO_TEST_REDUCE_2K_L 0 | |
25 #endif | |
26 | |
27 #ifdef LTM_DEMO_REAL_RAND | |
28 #define LTM_DEMO_RAND_SEED time(NULL) | |
29 #else | |
30 #define LTM_DEMO_RAND_SEED 23 | |
31 #endif | |
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 | 42 mp_toradix(a, buf, 16); |
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 | 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 | 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 | 67 #if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32) |
68 static FILE* fd_urandom; | |
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 | 73 (void)dat; |
74 #if defined(LTM_DEMO_REAL_RAND) | |
75 if (!fd_urandom) { | |
76 #if !defined(_WIN32) | |
77 fprintf(stderr, "\nno /dev/urandom\n"); | |
78 #endif | |
79 } | |
80 else { | |
81 return fread(dst, 1, len, fd_urandom); | |
82 } | |
83 #endif | |
84 for (x = 0; x < len; ) { | |
85 unsigned int r = (unsigned int)rand(); | |
86 do { | |
87 dst[x++] = r & 0xFF; | |
88 r >>= 8; | |
89 } while((r != 0) && (x < len)); | |
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 | 94 #if LTM_DEMO_TEST_VS_MTEST != 0 |
95 static void _panic(int l) | |
96 { | |
97 fprintf(stderr, "\n%d: fgets failed\n", l); | |
98 exit(EXIT_FAILURE); | |
99 } | |
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 | 102 mp_int a, b, c, d, e, f; |
103 | |
104 static void _cleanup(void) | |
105 { | |
106 mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL); | |
107 printf("\n"); | |
108 | |
109 #ifdef LTM_DEMO_REAL_RAND | |
110 if(fd_urandom) | |
111 fclose(fd_urandom); | |
112 #endif | |
113 } | |
114 struct mp_sqrtmod_prime_st { | |
115 unsigned long p; | |
116 unsigned long n; | |
117 mp_digit r; | |
118 }; | |
119 struct mp_sqrtmod_prime_st sqrtmod_prime[] = { | |
120 { 5, 14, 3 }, | |
121 { 7, 9, 4 }, | |
122 { 113, 2, 62 } | |
123 }; | |
124 struct mp_jacobi_st { | |
125 unsigned long n; | |
126 int c[16]; | |
127 }; | |
128 struct mp_jacobi_st jacobi[] = { | |
129 { 3, { 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } }, | |
130 { 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } }, | |
131 { 7, { 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } }, | |
132 { 9, { -1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } }, | |
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 | 138 unsigned rr; |
139 int cnt, ix; | |
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 | 142 gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n; |
143 char* ret; | |
144 #else | |
145 unsigned long s, t; | |
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 | 148 int i, n, err, should; |
149 #endif | |
150 | |
151 if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY) | |
152 return EXIT_FAILURE; | |
153 | |
154 atexit(_cleanup); | |
155 | |
156 #if defined(LTM_DEMO_REAL_RAND) | |
157 if (!fd_urandom) { | |
158 fd_urandom = fopen("/dev/urandom", "r"); | |
159 if (!fd_urandom) { | |
160 #if !defined(_WIN32) | |
161 fprintf(stderr, "\ncould not open /dev/urandom\n"); | |
162 #endif | |
163 } | |
164 } | |
165 #endif | |
166 srand(LTM_DEMO_RAND_SEED); | |
167 | |
168 #ifdef MP_8BIT | |
169 printf("Digit size 8 Bit \n"); | |
170 #endif | |
171 #ifdef MP_16BIT | |
172 printf("Digit size 16 Bit \n"); | |
173 #endif | |
174 #ifdef MP_32BIT | |
175 printf("Digit size 32 Bit \n"); | |
176 #endif | |
177 #ifdef MP_64BIT | |
178 printf("Digit size 64 Bit \n"); | |
179 #endif | |
180 printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit)); | |
181 printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word)); | |
182 printf("DIGIT_BIT: %d\n", DIGIT_BIT); | |
183 printf("MP_PREC: %d\n", MP_PREC); | |
184 | |
185 #if LTM_DEMO_TEST_VS_MTEST == 0 | |
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 | 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 | 190 mp_neg(&a, &b); |
191 if (mp_cmp(&a, &b) != MP_GT) { | |
192 return EXIT_FAILURE; | |
193 } | |
194 if (mp_cmp(&b, &a) != MP_LT) { | |
195 return EXIT_FAILURE; | |
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 | 198 mp_neg(&a, &a); |
199 if (mp_cmp(&b, &a) != MP_EQ) { | |
200 return EXIT_FAILURE; | |
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 | 203 mp_abs(&a, &b); |
204 if (mp_isneg(&b) != MP_NO) { | |
205 return EXIT_FAILURE; | |
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 | 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 | 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 | 233 mp_set_int(&a, 0); |
234 mp_set_int(&b, 1); | |
235 if ((err = mp_jacobi(&a, &b, &i)) != MP_OKAY) { | |
236 printf("Failed executing mp_jacobi(0 | 1) %s.\n", mp_error_to_string(err)); | |
237 return EXIT_FAILURE; | |
238 } | |
239 if (i != 1) { | |
240 printf("Failed trivial mp_jacobi(0 | 1) %d != 1\n", i); | |
241 return EXIT_FAILURE; | |
242 } | |
243 for (cnt = 0; cnt < (int)(sizeof(jacobi)/sizeof(jacobi[0])); ++cnt) { | |
244 mp_set_int(&b, jacobi[cnt].n); | |
245 /* only test positive values of a */ | |
246 for (n = -5; n <= 10; ++n) { | |
247 mp_set_int(&a, abs(n)); | |
248 should = MP_OKAY; | |
249 if (n < 0) { | |
250 mp_neg(&a, &a); | |
251 /* Until #44 is fixed the negative a's must fail */ | |
252 should = MP_VAL; | |
253 } | |
254 if ((err = mp_jacobi(&a, &b, &i)) != should) { | |
255 printf("Failed executing mp_jacobi(%d | %lu) %s.\n", n, jacobi[cnt].n, mp_error_to_string(err)); | |
256 return EXIT_FAILURE; | |
257 } | |
258 if (err == MP_OKAY && i != jacobi[cnt].c[n + 5]) { | |
259 printf("Failed trivial mp_jacobi(%d | %lu) %d != %d\n", n, jacobi[cnt].n, i, jacobi[cnt].c[n + 5]); | |
260 return EXIT_FAILURE; | |
261 } | |
262 } | |
263 } | |
264 | |
265 // test mp_get_int | |
266 printf("\n\nTesting: mp_get_int"); | |
267 for (i = 0; i < 1000; ++i) { | |
268 t = ((unsigned long) rand () * rand () + 1) & 0xFFFFFFFF; | |
269 mp_set_int (&a, t); | |
270 if (t != mp_get_int (&a)) { | |
271 printf ("\nmp_get_int() bad result!"); | |
272 return EXIT_FAILURE; | |
273 } | |
274 } | |
275 mp_set_int(&a, 0); | |
276 if (mp_get_int(&a) != 0) { | |
277 printf("\nmp_get_int() bad result!"); | |
278 return EXIT_FAILURE; | |
279 } | |
280 mp_set_int(&a, 0xffffffff); | |
281 if (mp_get_int(&a) != 0xffffffff) { | |
282 printf("\nmp_get_int() bad result!"); | |
283 return EXIT_FAILURE; | |
284 } | |
285 | |
286 printf("\n\nTesting: mp_get_long\n"); | |
287 for (i = 0; i < (int)(sizeof(unsigned long)*CHAR_BIT) - 1; ++i) { | |
288 t = (1ULL << (i+1)) - 1; | |
289 if (!t) | |
290 t = -1; | |
291 printf(" t = 0x%lx i = %d\r", t, i); | |
292 do { | |
293 if (mp_set_long(&a, t) != MP_OKAY) { | |
294 printf("\nmp_set_long() error!"); | |
295 return EXIT_FAILURE; | |
296 } | |
297 s = mp_get_long(&a); | |
298 if (s != t) { | |
299 printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t); | |
300 return EXIT_FAILURE; | |
301 } | |
302 t <<= 1; |