From 44ff07e01874ea8be0c72bf9d20cb7f13b76cca8 Mon Sep 17 00:00:00 2001 From: tb <> Date: Thu, 3 Aug 2023 18:53:56 +0000 Subject: Make the bn_rand_interval() API a bit more ergonomic Provide bn_rand_in_range() which is a slightly tweaked version of what was previously called bn_rand_range(). The way bn_rand_range() is called in libcrypto, the lower bound is always expressible as a word. In fact, most of the time it is 1, the DH code uses a 2, the MR tests in BPSW use 3 and an exceptinally high number appears in the Tonelli-Shanks implementation where we use 32. Converting these lower bounds to BIGNUMs on the call site is annoying so let bn_rand_interval() do that internally and route that through bn_rand_in_range(). This way we can avoid using BN_sub_word(). Adjust the bn_isqrt() test to use bn_rand_in_range() since that's the only caller that uses actual BIGNUMs as lower bounds. ok jsing --- src/regress/lib/libcrypto/bn/bn_isqrt.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/regress') diff --git a/src/regress/lib/libcrypto/bn/bn_isqrt.c b/src/regress/lib/libcrypto/bn/bn_isqrt.c index 2663bb74e9..d8a2d2755f 100644 --- a/src/regress/lib/libcrypto/bn/bn_isqrt.c +++ b/src/regress/lib/libcrypto/bn/bn_isqrt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_isqrt.c,v 1.3 2023/03/08 06:28:08 tb Exp $ */ +/* $OpenBSD: bn_isqrt.c,v 1.4 2023/08/03 18:53:56 tb Exp $ */ /* * Copyright (c) 2022 Theo Buehler * @@ -194,8 +194,8 @@ isqrt_test(void) if (!BN_set_bit(upper, UPPER_BITS)) errx(1, "BN_set_bit(upper, %d)", UPPER_BITS); - if (!bn_rand_interval(n, lower, upper)) - errx(1, "bn_rand_interval n"); + if (!bn_rand_in_range(n, lower, upper)) + errx(1, "bn_rand_in_range n"); /* n_sqr = n^2 */ if (!BN_sqr(n_sqr, n, ctx)) @@ -246,8 +246,8 @@ isqrt_test(void) */ for (i = 0; i < N_TESTS; i++) { - if (!bn_rand_interval(testcase, n_sqr, upper)) - errx(1, "bn_rand_interval testcase"); + if (!bn_rand_in_range(testcase, n_sqr, upper)) + errx(1, "bn_rand_in_range testcase"); if (!bn_isqrt(isqrt, &is_perfect_square, testcase, ctx)) errx(1, "bn_isqrt testcase"); -- cgit v1.2.3-55-g6feb