From 4c8787d87bf414ea0903eac03d0750cc07bc75b8 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Sat, 17 Sep 2022 17:06:54 +0100 Subject: [PATCH] Fix fr_batch_inv --- src/utility.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/utility.c b/src/utility.c index 5d67a35..b86e9a9 100644 --- a/src/utility.c +++ b/src/utility.c @@ -162,12 +162,11 @@ C_KZG_RET reverse_bit_order(void *values, size_t size, uint64_t n) { * @param[in] a A vector of field elements, length @p len * @param[in] len Length */ -C_KZG_RET fr_batch_inv(fr_t **out, const fr_t *a, size_t len) { +C_KZG_RET fr_batch_inv(fr_t *out, const fr_t *a, size_t len) { fr_t *prod; fr_t inv; size_t i; - TRY(new_fr_array(out, len)); TRY(new_fr_array(&prod, len)); prod[0] = a[0]; @@ -179,9 +178,12 @@ C_KZG_RET fr_batch_inv(fr_t **out, const fr_t *a, size_t len) { blst_fr_eucl_inverse(&inv, &prod[len - 1]); for(i = len - 1; i > 0; i--) { - fr_mul(out[i], &inv, &prod[i - 1]); + fr_mul(&out[i], &inv, &prod[i - 1]); fr_mul(&inv, &a[i], &inv); } + out[0] = inv; + + free(prod); return C_KZG_OK; } @@ -218,20 +220,25 @@ void is_power_of_two_works(void) { } void test_batch_inv(void) { - fr_t **inputs; - fr_t **actual, **expected; + fr_t *inputs, *actual, *expected; + int i; - TEST_CHECK(C_KZG_OK == new_fr_array(inputs, 32)); - TEST_CHECK(C_KZG_OK == new_fr_array(expected, 32)); + TEST_CHECK(C_KZG_OK == new_fr_array(&inputs, 32)); + TEST_CHECK(C_KZG_OK == new_fr_array(&actual, 32)); + TEST_CHECK(C_KZG_OK == new_fr_array(&expected, 32)); - for (int i = 0; i < 32; i++) { - *inputs[i] = rand_fr(); - fr_inv(expected[i], inputs[i]); + for (i = 0; i < 32; i++) { + inputs[i] = rand_fr(); + fr_inv(&expected[i], &inputs[i]); } - fr_batch_inv(actual, *inputs, 32); - for (int i = 0; i < 32; i++) { - TEST_CHECK(expected[i] == actual[i]); + fr_batch_inv(actual, inputs, 32); + for (i = 0; i < 32; i++) { + TEST_CHECK(fr_equal(&expected[i], &actual[i])); } + + free(inputs); + free(actual); + free(expected); } void test_log2_pow2(void) { @@ -372,4 +379,4 @@ TEST_LIST = { {NULL, NULL} /* zero record marks the end of the list */ }; -#endif // KZGTEST \ No newline at end of file +#endif // KZGTEST