Fix up zero polynomial tests

This commit is contained in:
Ben Edgington 2021-02-06 20:00:39 +00:00
parent 61bc2c186a
commit 951ce118cd
3 changed files with 54 additions and 12 deletions

View File

@ -145,10 +145,32 @@ void proof_multi(void) {
free(s2);
}
void commit_to_nil_poly(void) {
poly a;
FFTSettings fs;
KZGSettings ks;
uint64_t secrets_len = 16;
blst_p1 *s1 = malloc(secrets_len * sizeof(blst_p1));
blst_p2 *s2 = malloc(secrets_len * sizeof(blst_p2));
blst_p1 result;
blst_p1_affine result_affine;
// Initialise the (arbitrary) secrets and data structures
generate_setup(s1, s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, &fs, s1, s2, secrets_len));
init_poly(&a, 0);
commit_to_poly(&result, &ks, &a);
blst_p1_to_affine(&result_affine, &result);
TEST_CHECK(blst_p1_affine_is_equal(&identity_g1_affine, &result_affine));
}
TEST_LIST =
{
{"KZG_PROOFS_TEST", title},
{"proof_single", proof_single},
{"proof_multi", proof_multi},
{"commit_to_nil_poly", commit_to_nil_poly},
{ NULL, NULL } /* zero record marks the end of the list */
};

View File

@ -69,6 +69,7 @@ C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
uint64_t diff = a_pos - b_pos;
blst_fr a[dividend->length];
ASSERT(divisor->length > 0, C_KZG_BADARGS);
ASSERT(out->length == poly_quotient_length(dividend, divisor), C_KZG_BADARGS);
// If the divisor is larger than the dividend, the result is zero-length

View File

@ -102,33 +102,51 @@ void poly_div_1(void) {
}
void poly_div_2(void) {
blst_fr a[3], b[2];
blst_fr a[2], b[3];
poly dividend, divisor, actual;
// Calculate (x + 1) / (x^2 - 1) = nil
// Dividend
fr_from_uint64(&b[0], 1);
fr_from_uint64(&b[1], 1);
fr_from_uint64(&a[0], 1);
fr_from_uint64(&a[1], 1);
dividend.length = 2;
dividend.coeffs = b;
dividend.coeffs = a;
// Divisor
fr_from_uint64(&a[0], 1);
fr_negate(&a[0], &a[0]);
fr_from_uint64(&a[1], 0);
fr_from_uint64(&a[2], 1);
fr_from_uint64(&b[0], 1);
fr_negate(&b[0], &b[0]);
fr_from_uint64(&b[1], 0);
fr_from_uint64(&b[2], 1);
divisor.length = 3;
divisor.coeffs = a;
divisor.coeffs = b;
init_poly(&actual, poly_quotient_length(&dividend, &divisor));
TEST_CHECK(C_KZG_OK == poly_long_div(&actual, &dividend, &divisor));
TEST_CHECK(fr_equal(NULL, actual.coeffs));
TEST_CHECK(NULL == actual.coeffs);
free_poly(&actual);
}
void poly_div_by_zero(void) {
blst_fr a[2];
poly dividend, divisor;
// Calculate (x + 1) / 0 = FAIL
// Dividend
fr_from_uint64(&a[0], 1);
fr_from_uint64(&a[1], 1);
dividend.length = 2;
dividend.coeffs = a;
// Divisor
init_poly(&divisor, 0);
TEST_CHECK(C_KZG_BADARGS == poly_long_div(NULL, &dividend, &divisor));
}
void poly_eval_check(void) {
uint64_t n = 10;
blst_fr actual, expected;
@ -182,9 +200,10 @@ TEST_LIST =
{"poly_div_length", poly_div_length},
{"poly_div_0", poly_div_0},
{"poly_div_1", poly_div_1},
{"poly_div_2", poly_div_1},
{"poly_div_2", poly_div_2},
{"poly_div_by_zero", poly_div_by_zero},
{"poly_eval_check", poly_eval_check},
{"poly_eval_0_check", poly_eval_0_check},
{"poly_eval_nil_check", poly_eval_0_check},
{"poly_eval_nil_check", poly_eval_nil_check},
{ NULL, NULL } /* zero record marks the end of the list */
};