Add compute_kzg_proof to nodejs bindings (#92)

This commit is contained in:
Justin Traglia 2023-01-27 16:15:17 +01:00 committed by GitHub
parent 60ead4ee49
commit 9076280cd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -188,6 +188,37 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) {
); );
free(blobs); free(blobs);
if (ret != C_KZG_OK) {
Napi::Error::New(env, "Failed to compute aggregated proof")
.ThrowAsJavaScriptException();
return env.Undefined();
};
return napi_typed_array_from_bytes((uint8_t *)(&proof), BYTES_PER_PROOF, env);
}
// computeKzgProof: (blob: Blob, zBytes: Bytes32, setupHandle: SetupHandle) => KZGProof;
Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) {
auto env = info.Env();
size_t argument_count = info.Length();
size_t expected_argument_count = 3;
if (argument_count != expected_argument_count) {
return throw_invalid_arguments_count(expected_argument_count, argument_count, env);
}
auto blob = extract_byte_array_from_param(info, 0, "blob");
auto z_bytes = extract_byte_array_from_param(info, 1, "zBytes");
auto kzg_settings = info[2].As<Napi::External<KZGSettings>>().Data();
KZGProof proof;
C_KZG_RET ret = compute_kzg_proof(
&proof,
(Blob *)blob,
(Bytes32 *)z_bytes,
kzg_settings
);
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
Napi::Error::New(env, "Failed to compute proof") Napi::Error::New(env, "Failed to compute proof")
.ThrowAsJavaScriptException(); .ThrowAsJavaScriptException();
@ -308,8 +339,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
// Functions // Functions
exports["loadTrustedSetup"] = Napi::Function::New(env, LoadTrustedSetup); exports["loadTrustedSetup"] = Napi::Function::New(env, LoadTrustedSetup);
exports["freeTrustedSetup"] = Napi::Function::New(env, FreeTrustedSetup); exports["freeTrustedSetup"] = Napi::Function::New(env, FreeTrustedSetup);
exports["verifyKzgProof"] = Napi::Function::New(env, VerifyKzgProof);
exports["blobToKzgCommitment"] = Napi::Function::New(env, BlobToKzgCommitment); exports["blobToKzgCommitment"] = Napi::Function::New(env, BlobToKzgCommitment);
exports["computeKzgProof"] = Napi::Function::New(env, ComputeKzgProof);
exports["verifyKzgProof"] = Napi::Function::New(env, VerifyKzgProof);
exports["computeAggregateKzgProof"] = Napi::Function::New(env, ComputeAggregateKzgProof); exports["computeAggregateKzgProof"] = Napi::Function::New(env, ComputeAggregateKzgProof);
exports["verifyAggregateKzgProof"] = Napi::Function::New(env, VerifyAggregateKzgProof); exports["verifyAggregateKzgProof"] = Napi::Function::New(env, VerifyAggregateKzgProof);

View File

@ -24,6 +24,12 @@ type KZG = {
blobToKzgCommitment: (blob: Blob, setupHandle: SetupHandle) => KZGCommitment; blobToKzgCommitment: (blob: Blob, setupHandle: SetupHandle) => KZGCommitment;
computeKzgProof: (
blob: Blob,
zBytes: Bytes32,
setupHandle: SetupHandle,
) => KZGProof;
computeAggregateKzgProof: ( computeAggregateKzgProof: (
blobs: Blob[], blobs: Blob[],
setupHandle: SetupHandle, setupHandle: SetupHandle,
@ -110,6 +116,10 @@ export function blobToKzgCommitment(blob: Blob): KZGCommitment {
return kzg.blobToKzgCommitment(blob, requireSetupHandle()); return kzg.blobToKzgCommitment(blob, requireSetupHandle());
} }
export function computeKzgProof(blob: Blob, zBytes: Bytes32): KZGProof {
return kzg.computeKzgProof(blob, zBytes, requireSetupHandle());
}
export function computeAggregateKzgProof(blobs: Blob[]): KZGProof { export function computeAggregateKzgProof(blobs: Blob[]): KZGProof {
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle()); return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
} }

View File

@ -6,6 +6,7 @@ import {
freeTrustedSetup, freeTrustedSetup,
blobToKzgCommitment, blobToKzgCommitment,
verifyKzgProof, verifyKzgProof,
computeKzgProof,
computeAggregateKzgProof, computeAggregateKzgProof,
verifyAggregateKzgProof, verifyAggregateKzgProof,
BYTES_PER_FIELD_ELEMENT, BYTES_PER_FIELD_ELEMENT,
@ -45,6 +46,13 @@ describe("C-KZG", () => {
freeTrustedSetup(); freeTrustedSetup();
}); });
it("computes a proof from blob", () => {
let blob = generateRandomBlob();
const zBytes = new Uint8Array(32).fill(0);
computeKzgProof(blob, zBytes);
// No check, just make sure it doesn't crash.
});
it("computes the correct commitments and aggregate proof from blobs", () => { it("computes the correct commitments and aggregate proof from blobs", () => {
let blobs = new Array(2).fill(0).map(generateRandomBlob); let blobs = new Array(2).fill(0).map(generateRandomBlob);
let commitments = blobs.map(blobToKzgCommitment); let commitments = blobs.map(blobToKzgCommitment);