|
|
|
@ -3,13 +3,12 @@
|
|
|
|
|
*/
|
|
|
|
|
#define UNIT_TESTS
|
|
|
|
|
|
|
|
|
|
#include "tinytest.h"
|
|
|
|
|
#include "blst.h"
|
|
|
|
|
#include "c_kzg_4844.h"
|
|
|
|
|
#include "tinytest.h"
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Globals
|
|
|
|
@ -23,7 +22,7 @@ KZGSettings s;
|
|
|
|
|
|
|
|
|
|
static void get_rand_bytes32(Bytes32 *out) {
|
|
|
|
|
static uint64_t seed = 0;
|
|
|
|
|
blst_sha256(out->bytes, (uint8_t*)&seed, sizeof(seed));
|
|
|
|
|
blst_sha256(out->bytes, (uint8_t *)&seed, sizeof(seed));
|
|
|
|
|
seed++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -62,7 +61,7 @@ static void get_rand_g1_bytes(Bytes48 *out) {
|
|
|
|
|
static void bytes32_from_hex(Bytes32 *out, const char *hex) {
|
|
|
|
|
int matches;
|
|
|
|
|
for (int i = 0; i < sizeof(Bytes32); i++) {
|
|
|
|
|
matches = sscanf(hex + i*2, "%2hhx", &out->bytes[i]);
|
|
|
|
|
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
|
|
|
|
|
ASSERT_EQUALS(matches, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -70,7 +69,7 @@ static void bytes32_from_hex(Bytes32 *out, const char *hex) {
|
|
|
|
|
static void bytes48_from_hex(Bytes48 *out, const char *hex) {
|
|
|
|
|
int matches;
|
|
|
|
|
for (int i = 0; i < sizeof(Bytes48); i++) {
|
|
|
|
|
matches = sscanf(hex + i*2, "%2hhx", &out->bytes[i]);
|
|
|
|
|
matches = sscanf(hex + i * 2, "%2hhx", &out->bytes[i]);
|
|
|
|
|
ASSERT_EQUALS(matches, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -89,7 +88,7 @@ static void test_blob_to_kzg_commitment__succeeds_x_less_than_modulus(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
KZGCommitment c;
|
|
|
|
|
Blob blob;
|
|
|
|
|
Bytes32 b;
|
|
|
|
|
Bytes32 field_element;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A valid field element is x < BLS_MODULUS.
|
|
|
|
@ -97,10 +96,13 @@ static void test_blob_to_kzg_commitment__succeeds_x_less_than_modulus(void) {
|
|
|
|
|
*
|
|
|
|
|
* int(BLS_MODULUS - 1).to_bytes(32, 'little').hex()
|
|
|
|
|
*/
|
|
|
|
|
bytes32_from_hex(&b, "00000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73");
|
|
|
|
|
bytes32_from_hex(
|
|
|
|
|
&field_element,
|
|
|
|
|
"00000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
memset(&blob, 0, sizeof(blob));
|
|
|
|
|
memcpy(blob.bytes, b.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
ret = blob_to_kzg_commitment(&c, &blob, &s);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_OK);
|
|
|
|
|
}
|
|
|
|
@ -109,7 +111,7 @@ static void test_blob_to_kzg_commitment__fails_x_equal_to_modulus(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
KZGCommitment c;
|
|
|
|
|
Blob blob;
|
|
|
|
|
Bytes32 b;
|
|
|
|
|
Bytes32 field_element;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A valid field element is x < BLS_MODULUS.
|
|
|
|
@ -117,10 +119,13 @@ static void test_blob_to_kzg_commitment__fails_x_equal_to_modulus(void) {
|
|
|
|
|
*
|
|
|
|
|
* int(BLS_MODULUS).to_bytes(32, 'little').hex()
|
|
|
|
|
*/
|
|
|
|
|
bytes32_from_hex(&b, "01000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73");
|
|
|
|
|
bytes32_from_hex(
|
|
|
|
|
&field_element,
|
|
|
|
|
"01000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
memset(&blob, 0, sizeof(blob));
|
|
|
|
|
memcpy(blob.bytes, b.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
ret = blob_to_kzg_commitment(&c, &blob, &s);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
@ -129,7 +134,7 @@ static void test_blob_to_kzg_commitment__fails_x_greater_than_modulus(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
KZGCommitment c;
|
|
|
|
|
Blob blob;
|
|
|
|
|
Bytes32 b;
|
|
|
|
|
Bytes32 field_element;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A valid field element is x < BLS_MODULUS.
|
|
|
|
@ -137,10 +142,13 @@ static void test_blob_to_kzg_commitment__fails_x_greater_than_modulus(void) {
|
|
|
|
|
*
|
|
|
|
|
* int(BLS_MODULUS + 1).to_bytes(32, 'little').hex()
|
|
|
|
|
*/
|
|
|
|
|
bytes32_from_hex(&b, "02000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73");
|
|
|
|
|
bytes32_from_hex(
|
|
|
|
|
&field_element,
|
|
|
|
|
"02000000fffffffffe5bfeff02a4bd5305d8a10908d83933487d9d2953a7ed73"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
memset(&blob, 0, sizeof(blob));
|
|
|
|
|
memcpy(blob.bytes, b.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
memcpy(blob.bytes, field_element.bytes, BYTES_PER_FIELD_ELEMENT);
|
|
|
|
|
ret = blob_to_kzg_commitment(&c, &blob, &s);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
@ -162,7 +170,11 @@ static void test_blob_to_kzg_commitment__succeeds_point_at_infinity(void) {
|
|
|
|
|
/*
|
|
|
|
|
* The commitment should be the serialized point at infinity.
|
|
|
|
|
*/
|
|
|
|
|
bytes48_from_hex(&point_at_infinity, "c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&point_at_infinity,
|
|
|
|
|
"c00000000000000000000000000000000000000000000000"
|
|
|
|
|
"000000000000000000000000000000000000000000000000"
|
|
|
|
|
);
|
|
|
|
|
diff = memcmp(c.bytes, point_at_infinity.bytes, BYTES_PER_COMMITMENT);
|
|
|
|
|
ASSERT_EQUALS(diff, 0);
|
|
|
|
|
}
|
|
|
|
@ -185,7 +197,11 @@ static void test_blob_to_kzg_commitment__succeeds_consistent_commitment(void) {
|
|
|
|
|
* We expect the commitment to match. If it doesn't
|
|
|
|
|
* match, something important has changed.
|
|
|
|
|
*/
|
|
|
|
|
bytes48_from_hex(&expected_commitment, "af19e460169c57959c04786c958e01f984c195bc56e99b04c07e0c9747e5dfa566a4771b8b138cd8eed67efa81165663");
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&expected_commitment,
|
|
|
|
|
"af19e460169c57959c04786c958e01f984c195bc56e99b04"
|
|
|
|
|
"c07e0c9747e5dfa566a4771b8b138cd8eed67efa81165663"
|
|
|
|
|
);
|
|
|
|
|
diff = memcmp(c.bytes, expected_commitment.bytes, BYTES_PER_COMMITMENT);
|
|
|
|
|
ASSERT_EQUALS(diff, 0);
|
|
|
|
|
}
|
|
|
|
@ -211,111 +227,155 @@ static void test_validate_kzg_g1__succeeds_round_trip(void) {
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__succeeds_correct_point(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e264"
|
|
|
|
|
"4f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_not_in_g1(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"8123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_not_in_curve(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"8123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcde0"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_x_equal_to_modulus(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf"
|
|
|
|
|
"6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_x_greater_than_modulus(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf"
|
|
|
|
|
"6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__succeeds_infinity_with_true_b_flag(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"c00000000000000000000000000000000000000000000000"
|
|
|
|
|
"000000000000000000000000000000000000000000000000"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_infinity_with_true_b_flag(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"c01000000000000000000000000000000000000000000000"
|
|
|
|
|
"000000000000000000000000000000000000000000000000"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_infinity_with_false_b_flag(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"800000000000000000000000000000000000000000000000"
|
|
|
|
|
"000000000000000000000000000000000000000000000000"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_with_wrong_c_flag(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_with_b_flag_and_x_nonzero(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "c123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"c123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_validate_kzg_g1__fails_with_b_flag_and_a_flag_true(void) {
|
|
|
|
|
C_KZG_RET ret;
|
|
|
|
|
Bytes48 b;
|
|
|
|
|
Bytes48 g1_bytes;
|
|
|
|
|
g1_t g1;
|
|
|
|
|
|
|
|
|
|
bytes48_from_hex(&b, "e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
|
ret = validate_kzg_g1(&g1, &b);
|
|
|
|
|
bytes48_from_hex(
|
|
|
|
|
&g1_bytes,
|
|
|
|
|
"e00000000000000000000000000000000000000000000000"
|
|
|
|
|
"000000000000000000000000000000000000000000000000"
|
|
|
|
|
);
|
|
|
|
|
ret = validate_kzg_g1(&g1, &g1_bytes);
|
|
|
|
|
ASSERT_EQUALS(ret, C_KZG_BADARGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|