Fix fr_batch_inv

This commit is contained in:
Ramana Kumar 2022-09-17 17:06:54 +01:00
parent 9c36ca4f3d
commit 4c8787d87b
No known key found for this signature in database
GPG Key ID: ED471C788B900433
1 changed files with 21 additions and 14 deletions

View File

@ -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] a A vector of field elements, length @p len
* @param[in] len Length * @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 *prod;
fr_t inv; fr_t inv;
size_t i; size_t i;
TRY(new_fr_array(out, len));
TRY(new_fr_array(&prod, len)); TRY(new_fr_array(&prod, len));
prod[0] = a[0]; 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]); blst_fr_eucl_inverse(&inv, &prod[len - 1]);
for(i = len - 1; i > 0; i--) { 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); fr_mul(&inv, &a[i], &inv);
} }
out[0] = inv;
free(prod);
return C_KZG_OK; return C_KZG_OK;
} }
@ -218,20 +220,25 @@ void is_power_of_two_works(void) {
} }
void test_batch_inv(void) { void test_batch_inv(void) {
fr_t **inputs; fr_t *inputs, *actual, *expected;
fr_t **actual, **expected; int i;
TEST_CHECK(C_KZG_OK == new_fr_array(inputs, 32)); 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(&actual, 32));
TEST_CHECK(C_KZG_OK == new_fr_array(&expected, 32));
for (int i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
*inputs[i] = rand_fr(); inputs[i] = rand_fr();
fr_inv(expected[i], inputs[i]); fr_inv(&expected[i], &inputs[i]);
} }
fr_batch_inv(actual, *inputs, 32); fr_batch_inv(actual, inputs, 32);
for (int i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
TEST_CHECK(expected[i] == actual[i]); TEST_CHECK(fr_equal(&expected[i], &actual[i]));
} }
free(inputs);
free(actual);
free(expected);
} }
void test_log2_pow2(void) { void test_log2_pow2(void) {
@ -372,4 +379,4 @@ TEST_LIST = {
{NULL, NULL} /* zero record marks the end of the list */ {NULL, NULL} /* zero record marks the end of the list */
}; };
#endif // KZGTEST #endif // KZGTEST