Merge pull request #29 from dgcoffman/dgc/expose-verify-proof

Expose verifyKzgProof
This commit is contained in:
Ramana Kumar 2022-12-13 21:06:32 +00:00 committed by GitHub
commit 130b629d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 30 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); 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; // loadTrustedSetup: (filePath: string) => SetupHandle;
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) { Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
auto env = info.Env(); 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); return throw_invalid_arguments_count(expected_argument_count, argument_count, env);
} }
auto blob_param = info[0].As<Napi::TypedArray>(); auto blob = extract_byte_array_from_param(info, 0, "blob");
if (!blob_param.IsTypedArray() || blob_param.TypedArrayType() != napi_uint8_array) { if (env.IsExceptionPending()) {
return throw_invalid_argument_type(env, "blob", "UInt8Array"); return env.Null();
} }
auto blob = blob_param.As<Napi::Uint8Array>().Data();
auto kzg_settings = info[1].As<Napi::External<KZGSettings>>().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); return throw_invalid_arguments_count(expected_argument_count, argument_count, env);
} }
auto c_param = info[0].As<Napi::TypedArray>(); auto polynomial_kzg = extract_byte_array_from_param(info, 0, "polynomialKzg");
if (c_param.TypedArrayType() != napi_uint8_array) { auto z = extract_byte_array_from_param(info, 1, "z");
return throw_invalid_argument_type(env, "polynomialKzg", "UInt8Array"); auto y = extract_byte_array_from_param(info, 2, "y");
} auto kzg_proof = extract_byte_array_from_param(info, 3, "kzgProof");
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 kzg_settings = info[4].As<Napi::External<KZGSettings>>().Data(); auto kzg_settings = info[4].As<Napi::External<KZGSettings>>().Data();
if (env.IsExceptionPending()) {
return env.Null();
}
KZGCommitment commitment; KZGCommitment commitment;
auto ret = bytes_to_g1(&commitment, polynomial_kzg); auto ret = bytes_to_g1(&commitment, polynomial_kzg);
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name": "c-kzg", "name": "c-kzg",
"version": "1.0.7", "version": "1.0.8",
"description": "NodeJS bindings for C-KZG", "description": "NodeJS bindings for C-KZG",
"author": "Dan Coffman", "author": "Dan Coffman",
"license": "MIT", "license": "MIT",

View File

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