Expose verifyKzgProof

This commit is contained in:
dancoffman 2022-12-13 11:21:43 -08:00
parent c0e5adff1d
commit 4ccc48cbff
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
3 changed files with 47 additions and 29 deletions

View File

@ -59,6 +59,15 @@ Napi::TypedArrayOf<uint8_t> napi_typed_array_from_bytes(uint8_t* array, size_t l
return Napi::Uint8Array::New(env, length, buffer, 0);
}
const uint8_t * extract_byte_array_from_param(const Napi::CallbackInfo& info, const int index, const std::string name) {
auto param = info[index].As<Napi::TypedArray>();
if (!param.IsTypedArray() || param.TypedArrayType() != napi_uint8_array) {
throw_invalid_argument_type(info.Env(), name, "UInt8Array");
}
return param.As<Napi::Uint8Array>().Data();
}
// loadTrustedSetup: (filePath: string) => SetupHandle;
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
auto env = info.Env();
@ -125,11 +134,10 @@ Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) {
return throw_invalid_arguments_count(expected_argument_count, argument_count, env);
}
auto blob_param = info[0].As<Napi::TypedArray>();
if (!blob_param.IsTypedArray() || blob_param.TypedArrayType() != napi_uint8_array) {
return throw_invalid_argument_type(env, "blob", "UInt8Array");
auto blob = extract_byte_array_from_param(info, 0, "blob");
if (env.IsExceptionPending()) {
return env.Null();
}
auto blob = blob_param.As<Napi::Uint8Array>().Data();
auto kzg_settings = info[1].As<Napi::External<KZGSettings>>().Data();
@ -280,32 +288,16 @@ Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) {
return throw_invalid_arguments_count(expected_argument_count, argument_count, env);
}
auto c_param = info[0].As<Napi::TypedArray>();
if (c_param.TypedArrayType() != napi_uint8_array) {
return throw_invalid_argument_type(env, "polynomialKzg", "UInt8Array");
}
auto polynomial_kzg = c_param.As<Napi::Uint8Array>().Data();
auto z_param = info[1].As<Napi::TypedArray>();
if (z_param.TypedArrayType() != napi_uint8_array) {
return throw_invalid_argument_type(env, "z", "UInt8Array");
}
auto z = z_param.As<Napi::Uint8Array>().Data();
auto y_param = info[2].As<Napi::TypedArray>();
if (y_param.TypedArrayType() != napi_uint8_array) {
return throw_invalid_argument_type(env, "y", "UInt8Array");
}
auto y = y_param.As<Napi::Uint8Array>().Data();
auto proof_param = info[3].As<Napi::TypedArray>();
if (proof_param.TypedArrayType() != napi_uint8_array) {
return throw_invalid_argument_type(env, "kzgProof", "UInt8Array");
}
auto kzg_proof = proof_param.As<Napi::Uint8Array>().Data();
auto polynomial_kzg = extract_byte_array_from_param(info, 0, "polynomialKzg");
auto z = extract_byte_array_from_param(info, 1, "z");
auto y = extract_byte_array_from_param(info, 2, "y");
auto kzg_proof = extract_byte_array_from_param(info, 3, "kzgProof");
auto kzg_settings = info[4].As<Napi::External<KZGSettings>>().Data();
if (env.IsExceptionPending()) {
return env.Null();
}
KZGCommitment commitment;
auto ret = bytes_to_g1(&commitment, polynomial_kzg);
if (ret != C_KZG_OK) {

View File

@ -35,7 +35,6 @@ type KZG = {
setupHandle: SetupHandle,
) => boolean;
// Currently unused -- not exported
verifyKzgProof: (
polynomialKzg: KZGCommitment,
z: BLSFieldElement,
@ -113,6 +112,21 @@ export function computeAggregateKzgProof(blobs: Blob[]): KZGProof {
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
}
export function verifyKzgProof(
polynomialKzg: KZGCommitment,
z: BLSFieldElement,
y: BLSFieldElement,
kzgProof: KZGProof,
): boolean {
return kzg.verifyKzgProof(
polynomialKzg,
z,
y,
kzgProof,
requireSetupHandle(),
);
}
export function verifyAggregateKzgProof(
blobs: Blob[],
expectedKzgCommitments: KZGCommitment[],

View File

@ -5,6 +5,7 @@ import {
loadTrustedSetup,
freeTrustedSetup,
blobToKzgCommitment,
verifyKzgProof,
computeAggregateKzgProof,
verifyAggregateKzgProof,
BYTES_PER_FIELD_ELEMENT,
@ -68,6 +69,17 @@ describe("C-KZG", () => {
);
});
it("verifies an invalid KZG proof without crashing", () => {
const commitment = new Uint8Array(48).fill(0);
commitment[0] = 0xc0;
const z = new Uint8Array(32).fill(0);
const y = new Uint8Array(32).fill(0);
const proof = new Uint8Array(48).fill(0);
proof[0] = 0xc0;
verifyKzgProof(commitment, z, y, proof);
});
it("computes the aggregate proof when for a single blob", () => {
let blobs = new Array(1).fill(0).map(generateRandomBlob);
let commitments = blobs.map(blobToKzgCommitment);