Define a `poly` type
This commit is contained in:
parent
f5650d8e23
commit
b9f4e7737e
18
src/poly.c
18
src/poly.c
|
@ -28,24 +28,24 @@ uint64_t poly_long_div_length(const uint64_t len_dividend, const uint64_t len_di
|
|||
|
||||
// `out` must have been pre-allocated to the correct size, and the length is provided
|
||||
// as a check
|
||||
C_KZG_RET poly_long_div(blst_fr *out, const uint64_t len_out, const blst_fr *dividend, const uint64_t len_dividend, const blst_fr *divisor, const uint64_t len_divisor) {
|
||||
uint64_t a_pos = len_dividend - 1;
|
||||
uint64_t b_pos = len_divisor - 1;
|
||||
C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
||||
uint64_t a_pos = dividend->length - 1;
|
||||
uint64_t b_pos = divisor->length - 1;
|
||||
uint64_t diff = a_pos - b_pos;
|
||||
blst_fr a[len_dividend];
|
||||
blst_fr a[dividend->length];
|
||||
|
||||
ASSERT(len_out == diff + 1, C_KZG_BADARGS);
|
||||
ASSERT(out->length == diff + 1, C_KZG_BADARGS);
|
||||
|
||||
for (uint64_t i = 0; i < len_dividend; i++) {
|
||||
a[i] = dividend[i];
|
||||
for (uint64_t i = 0; i < dividend->length; i++) {
|
||||
a[i] = dividend->coeffs[i];
|
||||
}
|
||||
|
||||
while (true) {
|
||||
poly_factor_div(&out[diff], &a[a_pos], &divisor[b_pos]);
|
||||
poly_factor_div(&out->coeffs[diff], &a[a_pos], &divisor->coeffs[b_pos]);
|
||||
for (uint64_t i = 0; i <= b_pos; i++) {
|
||||
blst_fr tmp;
|
||||
// a[diff + i] -= b[i] * quot
|
||||
blst_fr_mul(&tmp, &out[diff], &divisor[i]);
|
||||
blst_fr_mul(&tmp, &out->coeffs[diff], &divisor->coeffs[i]);
|
||||
blst_fr_sub(&a[diff + i], &a[diff + i], &tmp);
|
||||
}
|
||||
if (diff == 0) break;
|
||||
|
|
|
@ -16,5 +16,10 @@
|
|||
|
||||
#include "c_kzg.h"
|
||||
|
||||
typedef struct {
|
||||
blst_fr *coeffs; // coeffs[i] is the x^i term
|
||||
uint64_t length; // one more than the polynomial's degree
|
||||
} poly;
|
||||
|
||||
uint64_t poly_long_div_length(const uint64_t len_dividend, const uint64_t len_divisor);
|
||||
C_KZG_RET poly_long_div(blst_fr *out, const uint64_t len_out, const blst_fr *dividend, const uint64_t len_dividend, const blst_fr *divisor, const uint64_t len_divisor);
|
||||
C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor);
|
||||
|
|
|
@ -24,63 +24,77 @@ void poly_div_length() {
|
|||
}
|
||||
|
||||
void poly_div_0() {
|
||||
blst_fr a[3], b[2], c[2], expected[2];
|
||||
poly dividend, divisor, actual;
|
||||
|
||||
// Calculate (x^2 - 1) / (x + 1) = x - 1
|
||||
blst_fr dividend[3];
|
||||
blst_fr divisor[2];
|
||||
blst_fr expected[2], actual[2];
|
||||
|
||||
// Set up dividend
|
||||
fr_from_uint64(÷nd[0], 1);
|
||||
fr_negate(÷nd[0], ÷nd[0]);
|
||||
fr_from_uint64(÷nd[1], 0);
|
||||
fr_from_uint64(÷nd[2], 1);
|
||||
// Dividend
|
||||
fr_from_uint64(&a[0], 1);
|
||||
fr_negate(&a[0], &a[0]);
|
||||
fr_from_uint64(&a[1], 0);
|
||||
fr_from_uint64(&a[2], 1);
|
||||
dividend.length = 3;
|
||||
dividend.coeffs = a;
|
||||
|
||||
// Set up divisor
|
||||
fr_from_uint64(&divisor[0], 1);
|
||||
fr_from_uint64(&divisor[1], 1);
|
||||
// Divisor
|
||||
fr_from_uint64(&b[0], 1);
|
||||
fr_from_uint64(&b[1], 1);
|
||||
divisor.length = 2;
|
||||
divisor.coeffs = b;
|
||||
|
||||
// Set up result
|
||||
// Known result
|
||||
fr_from_uint64(&expected[0], 1);
|
||||
fr_negate(&expected[0], &expected[0]);
|
||||
fr_from_uint64(&expected[1], 1);
|
||||
|
||||
TEST_CHECK(poly_long_div(actual, 2, dividend, 3, divisor, 2) == C_KZG_SUCCESS);
|
||||
TEST_CHECK(fr_equal(expected + 0, actual + 0));
|
||||
TEST_CHECK(fr_equal(expected + 1, actual + 1));
|
||||
actual.length = 2;
|
||||
actual.coeffs = c;
|
||||
|
||||
TEST_CHECK(poly_long_div(&actual, ÷nd, &divisor) == C_KZG_SUCCESS);
|
||||
TEST_CHECK(fr_equal(&expected[0], &actual.coeffs[0]));
|
||||
TEST_CHECK(fr_equal(&expected[1], &actual.coeffs[1]));
|
||||
}
|
||||
|
||||
void poly_div_1() {
|
||||
blst_fr a[4], b[2], c[3], expected[3];
|
||||
poly dividend, divisor, actual;
|
||||
|
||||
// Calculate (12x^3 - 11x^2 + 9x + 18) / (4x + 3) = 3x^2 - 5x + 6
|
||||
blst_fr dividend[4];
|
||||
blst_fr divisor[2];
|
||||
blst_fr expected[3], actual[3];
|
||||
|
||||
// Set up dividend
|
||||
fr_from_uint64(÷nd[0], 18);
|
||||
fr_from_uint64(÷nd[1], 9);
|
||||
fr_from_uint64(÷nd[2], 11);
|
||||
fr_negate(÷nd[2], ÷nd[2]);
|
||||
fr_from_uint64(÷nd[3], 12);
|
||||
// Dividend
|
||||
fr_from_uint64(&a[0], 18);
|
||||
fr_from_uint64(&a[1], 9);
|
||||
fr_from_uint64(&a[2], 11);
|
||||
fr_negate(&a[2], &a[2]);
|
||||
fr_from_uint64(&a[3], 12);
|
||||
dividend.length = 4;
|
||||
dividend.coeffs = a;
|
||||
|
||||
// Set up divisor
|
||||
fr_from_uint64(&divisor[0], 3);
|
||||
fr_from_uint64(&divisor[1], 4);
|
||||
// Divisor
|
||||
fr_from_uint64(&b[0], 3);
|
||||
fr_from_uint64(&b[1], 4);
|
||||
divisor.length = 2;
|
||||
divisor.coeffs = b;
|
||||
|
||||
// Set up result
|
||||
// Known result
|
||||
fr_from_uint64(&expected[0], 6);
|
||||
fr_from_uint64(&expected[1], 5);
|
||||
fr_negate(&expected[1], &expected[1]);
|
||||
fr_from_uint64(&expected[2], 3);
|
||||
|
||||
TEST_CHECK(poly_long_div(actual, 3, dividend, 4, divisor, 2) == C_KZG_SUCCESS);
|
||||
TEST_CHECK(fr_equal(expected + 0, actual + 0));
|
||||
TEST_CHECK(fr_equal(expected + 1, actual + 1));
|
||||
TEST_CHECK(fr_equal(expected + 2, actual + 2));
|
||||
actual.length = 3;
|
||||
actual.coeffs = c;
|
||||
|
||||
TEST_CHECK(poly_long_div(&actual, ÷nd, &divisor) == C_KZG_SUCCESS);
|
||||
TEST_CHECK(fr_equal(&expected[0], &actual.coeffs[0]));
|
||||
TEST_CHECK(fr_equal(&expected[1], &actual.coeffs[1]));
|
||||
TEST_CHECK(fr_equal(&expected[2], &actual.coeffs[2]));
|
||||
}
|
||||
|
||||
void poly_wrong_size(void) {
|
||||
blst_fr dividend[1], divisor[1], result[1];
|
||||
TEST_CHECK(poly_long_div(result, 5, dividend, 20, divisor, 7) == C_KZG_BADARGS);
|
||||
poly dividend, divisor, result;
|
||||
TEST_CHECK(poly_long_div(&result, ÷nd, &divisor) == C_KZG_BADARGS);
|
||||
}
|
||||
|
||||
TEST_LIST =
|
||||
|
|
Loading…
Reference in New Issue