From 3e70d0927b73171d2a3242bacf3579deb98304ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Wed, 18 Jan 2006 13:25:31 +0000 Subject: [PATCH] Compare numbers with BN_cmp(). git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16582 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/des/test_bn.c | 62 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/lib/des/test_bn.c b/lib/des/test_bn.c index 431107505..da4216462 100644 --- a/lib/des/test_bn.c +++ b/lib/des/test_bn.c @@ -194,7 +194,7 @@ test_BN_import_export(void) } static int -test_bn_uadd(void) +test_BN_uadd(void) { BIGNUM *a, *b, *c; char *p; @@ -266,6 +266,63 @@ test_bn_uadd(void) return 0; } +static int +test_BN_cmp(void) +{ + BIGNUM *a, *b; + + a = BN_new(); + b = BN_new(); + + if (!BN_set_word(a, 1)) + return 1; + if (!BN_set_word(b, 1)) + return 1; + + if (BN_cmp(a, b) != 0) + return 1; + if (BN_cmp(b, a) != 0) + return 1; + + if (!BN_set_word(b, 2)) + return 1; + + if (BN_cmp(a, b) >= 0) + return 1; + if (BN_cmp(b, a) <= 0) + return 1; + + BN_set_negative(b, 1); + + if (BN_cmp(a, b) <= 0) + return 1; + if (BN_cmp(b, a) >= 0) + return 1; + + BN_free(a); + BN_free(b); + + BN_hex2bn(&a, "50212A3B611D46642C825A16A354CE0FD4D85DD1"); + BN_hex2bn(&b, "50212A3B611D46642C825A16A354CE0FD4D85DD2"); + + if (BN_cmp(a, b) >= 0) + return 1; + if (BN_cmp(b, a) <= 0) + return 1; + + BN_set_negative(b, 1); + + if (BN_cmp(a, b) <= 0) + return 1; + if (BN_cmp(b, a) >= 0) + return 1; + + BN_free(a); + BN_free(b); + return 0; +} + + int main(int argc, char **argv) { @@ -274,7 +331,8 @@ main(int argc, char **argv) ret += test_BN_set_get(); ret += test_BN_bit(); ret += test_BN_import_export(); - ret += test_bn_uadd(); + ret += test_BN_uadd(); + ret += test_BN_cmp(); return ret; }