Clean up the test file a little (#97)

This commit is contained in:
Justin Traglia 2023-01-30 14:57:26 +01:00 committed by GitHub
parent 7f1fb88da9
commit 05bd73bca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,81 +1,89 @@
/* Tests for myfunctions.c, using TinyTest. */ /*
* This file contains unit tests for C-KZG-4844.
*/
#define UNIT_TESTS #define UNIT_TESTS
#include "tinytest.h" #include "tinytest.h"
#include "blst.h" #include "blst.h"
#include "c_kzg_4844.h" #include "c_kzg_4844.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
KZGSettings s; KZGSettings s;
void load_setup() { static void setup(void) {
FILE *fp; FILE *fp;
C_KZG_RET ret; C_KZG_RET ret;
fp = fopen("trusted_setup.txt", "r");
ret = load_trusted_setup_file(&s, fp); fp = fopen("trusted_setup.txt", "r");
assert(ret == C_KZG_OK); assert(fp != NULL);
fclose(fp); ret = load_trusted_setup_file(&s, fp);
assert(ret == C_KZG_OK);
fclose(fp);
} }
void get_32_rand_bytes(uint8_t *out) { static void teardown(void) {
static uint64_t seed = 0; free_trusted_setup(&s);
seed++;
blst_sha256(out, (uint8_t*)&seed, sizeof(seed));
} }
void get_rand_field_element(Bytes32 *out) { static void get_32_rand_bytes(uint8_t *out) {
fr_t tmp; static uint64_t seed = 0;
Bytes32 tmp_rand; seed++;
blst_sha256(out, (uint8_t*)&seed, sizeof(seed));
}
memset(out, 0, sizeof(Bytes32)); static void get_rand_field_element(Bytes32 *out) {
fr_t tmp_fr;
Bytes32 tmp_bytes;
// Take 32 random bytes, make them an Fr, and then turn the Fr back to a bytes array memset(out, 0, sizeof(Bytes32));
get_32_rand_bytes((uint8_t *) &tmp_rand);
hash_to_bls_field(&tmp, &tmp_rand); /*
bytes_from_bls_field(out, &tmp); * Take 32 random bytes, make them an Fr, and then
* turn the Fr back to a bytes array.
*/
get_32_rand_bytes((uint8_t *)&tmp_bytes);
hash_to_bls_field(&tmp_fr, &tmp_bytes);
bytes_from_bls_field(out, &tmp_fr);
} }
void get_rand_blob(Blob *out) { void get_rand_blob(Blob *out) {
memset(out, 0, sizeof(Blob)); memset(out, 0, sizeof(Blob));
uint8_t *blob_bytes = (uint8_t *) out; uint8_t *blob_bytes = (uint8_t *) out;
for (int i = 0; i < 128; i++) { for (int i = 0; i < 128; i++) {
get_rand_field_element((Bytes32 *)&blob_bytes[i * 32]); get_rand_field_element((Bytes32 *)&blob_bytes[i * 32]);
} }
} }
void test_compute_kzg_proof() { static void test_compute_kzg_proof(void) {
C_KZG_RET ret; C_KZG_RET ret;
Bytes48 proof; Bytes48 proof;
Bytes32 z; Bytes32 z;
KZGCommitment c; KZGCommitment c;
Blob blob; Blob blob;
get_rand_field_element(&z); get_rand_field_element(&z);
get_rand_blob(&blob); get_rand_blob(&blob);
ret = blob_to_kzg_commitment(&c, &blob, &s); ret = blob_to_kzg_commitment(&c, &blob, &s);
ASSERT_EQUALS(ret, C_KZG_OK); ASSERT_EQUALS(ret, C_KZG_OK);
ret = compute_kzg_proof(&proof, &blob, &z, &s); ret = compute_kzg_proof(&proof, &blob, &z, &s);
ASSERT_EQUALS(ret, C_KZG_OK); ASSERT_EQUALS(ret, C_KZG_OK);
// XXX now verify it! // XXX now verify it!
} }
/* test runner */ int main(void)
int main()
{ {
load_setup(); setup();
RUN(test_compute_kzg_proof);
teardown();
RUN(test_compute_kzg_proof); return TEST_REPORT();
free_trusted_setup(&s);
return TEST_REPORT();
} }