Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmucb committed Apr 27, 2024
1 parent 97c7bef commit d2acd81
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
16 changes: 14 additions & 2 deletions v2/kyber/kyber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,30 @@ void print_kyber_parameters(kyber_parameters& p) {
}

void print_coefficient_vector(coefficient_vector& v) {
bool something_printed = false;
int num_printed = 0;

if (v.c_.size() == 0)
return;

int k = (int)v.c_.size() - 1;
while (v.c_[k] == 0 && k > 0)
k--;
if (k > 0)
if (k > 0) {
printf("(%d[%d] + ", v.c_[k], k);
something_printed = true;
num_printed++;
}
else
printf("(");

for (int i = k - 1; i > 0; i--) {
if (v.c_[i] == 0)
continue;
printf("%d[%d] + ", v.c_[i], i);
if ((i%8) ==0)
something_printed = true;
num_printed++;
if ((num_printed%8) ==0)
printf("\n ");
}
printf("%d[%d])\n", v.c_[0], 0);
Expand Down
72 changes: 72 additions & 0 deletions v2/kyber/test_kyber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,81 @@ bool test_kyber_support() {
coefficient_vector product_ntt(p.q_, p.n_);
coefficient_vector transformed_product_ntt(p.q_, p.n_);

if (!coefficient_vector_zero(&f)) {
return false;
}
if (!coefficient_vector_zero(&h)) {
return false;
}
if (!coefficient_vector_zero(&product)) {
return false;
}
f.c_[255] = 1;
h.c_[1] = 1;
if (!coefficient_mult(f, h, &product)) {
printf("f x h coefficient_mult fails\n");
return false;
}
if (FLAGS_print_all) {
printf("f:\n");
print_coefficient_vector(f);
printf("\n");
printf("h:\n");
print_coefficient_vector(h);
printf("\n");
printf("f x h:\n");
print_coefficient_vector(product);
printf("\n");
}
if (product.c_[0] != (p.q_ - 1)) {
printf("wrong answer f x h\n");
return false;
}

h.c_[2] = 1;
if (!coefficient_mult(f, h, &product)) {
printf("f x h coefficient_mult fails\n");
return false;
}
if (FLAGS_print_all) {
printf("f:\n");
print_coefficient_vector(f);
printf("\n");
printf("h:\n");
print_coefficient_vector(h);
printf("\n");
printf("f x h:\n");
print_coefficient_vector(product);
printf("\n");
}

if (!coefficient_vector_zero(&product)) {
return false;
}
h.c_[255] = 1;
if (!coefficient_mult(f, h, &product)) {
printf("f x h coefficient_mult fails\n");
return false;
}
if (FLAGS_print_all) {
printf("f:\n");
print_coefficient_vector(f);
printf("\n");
printf("h:\n");
print_coefficient_vector(h);
printf("\n");
printf("f x h:\n");
print_coefficient_vector(product);
printf("\n");
}

for (int i = 0; i < f.len_; i++) {
/*
f.c_[i] = i;
h.c_[i] = f.len_ - i;
*/
f.c_[i] = 1;
h.c_[i] = 1;
}
if (!ntt(g, f, &f_ntt)) {
printf("f x h ntt transform fails\n");
Expand Down

0 comments on commit d2acd81

Please sign in to comment.