diff options
author | tb <> | 2022-07-28 20:06:01 +0000 |
---|---|---|
committer | tb <> | 2022-07-28 20:06:01 +0000 |
commit | d67fc64fc55ee8aa419a5e587c2e3da51c225384 (patch) | |
tree | 4ee51fc390bf5a193e6ab991c5078f142eda9512 /src | |
parent | ea13f55f1cdf021f243b6c14244e24e60cd12355 (diff) | |
download | openbsd-d67fc64fc55ee8aa419a5e587c2e3da51c225384.tar.gz openbsd-d67fc64fc55ee8aa419a5e587c2e3da51c225384.tar.bz2 openbsd-d67fc64fc55ee8aa419a5e587c2e3da51c225384.zip |
Add a second test to validate the tables in the library.
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/general/bn_isqrt.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/general/bn_isqrt.c b/src/regress/lib/libcrypto/bn/general/bn_isqrt.c index 8c932be265..3ada3d9edf 100644 --- a/src/regress/lib/libcrypto/bn/general/bn_isqrt.c +++ b/src/regress/lib/libcrypto/bn/general/bn_isqrt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_isqrt.c,v 1.3 2022/07/27 19:22:45 tb Exp $ */ | 1 | /* $OpenBSD: bn_isqrt.c,v 1.4 2022/07/28 20:06:01 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> | 3 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> |
4 | * | 4 | * |
@@ -116,6 +116,46 @@ check_tables(int print) | |||
116 | return failed; | 116 | return failed; |
117 | } | 117 | } |
118 | 118 | ||
119 | static int | ||
120 | validate_tables(void) | ||
121 | { | ||
122 | int fill[] = {11, 63, 64, 65}; | ||
123 | const uint8_t *table; | ||
124 | size_t i; | ||
125 | int j, k; | ||
126 | int failed = 0; | ||
127 | |||
128 | for (i = 0; i < sizeof(fill) / sizeof(fill[0]); i++) { | ||
129 | if ((table = get_table(fill[i])) == NULL) { | ||
130 | fprintf(stderr, "failed to get table %d\n", fill[i]); | ||
131 | failed |= 1; | ||
132 | continue; | ||
133 | } | ||
134 | |||
135 | for (j = 0; j < fill[i]; j++) { | ||
136 | for (k = 0; k < fill[i]; k++) { | ||
137 | if (j == (k * k) % fill[i]) | ||
138 | break; | ||
139 | } | ||
140 | |||
141 | if (table[j] == 0 && k < fill[i]) { | ||
142 | fprintf(stderr, "%d == %d^2 (mod %d)", j, k, | ||
143 | fill[i]); | ||
144 | failed |= 1; | ||
145 | } | ||
146 | if (table[j] == 1 && k == fill[i]) { | ||
147 | fprintf(stderr, "%d not a square (mod %d)", j, | ||
148 | fill[i]); | ||
149 | failed |= 1; | ||
150 | } | ||
151 | |||
152 | } | ||
153 | |||
154 | } | ||
155 | |||
156 | return failed; | ||
157 | } | ||
158 | |||
119 | /* | 159 | /* |
120 | * Choose a random number n of bit length between LOWER_BITS and UPPER_BITS and | 160 | * Choose a random number n of bit length between LOWER_BITS and UPPER_BITS and |
121 | * check that n == isqrt(n^2). Random numbers n^2 <= test < (n + 1)^2 are | 161 | * check that n == isqrt(n^2). Random numbers n^2 <= test < (n + 1)^2 are |
@@ -282,6 +322,7 @@ main(int argc, char *argv[]) | |||
282 | failed |= isqrt_test(); | 322 | failed |= isqrt_test(); |
283 | 323 | ||
284 | failed |= check_tables(0); | 324 | failed |= check_tables(0); |
325 | failed |= validate_tables(); | ||
285 | 326 | ||
286 | if (!failed) | 327 | if (!failed) |
287 | printf("SUCCESS\n"); | 328 | printf("SUCCESS\n"); |