Skip to content

Commit

Permalink
Add compute_kzg_proof to nodejs bindings (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Jan 27, 2023
1 parent 60ead4e commit 9076280
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
34 changes: 33 additions & 1 deletion bindings/node.js/kzg.cxx
Expand Up @@ -188,6 +188,37 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) {
);
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) {
Napi::Error::New(env, "Failed to compute proof")
.ThrowAsJavaScriptException();
Expand Down Expand Up @@ -308,8 +339,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
// Functions
exports["loadTrustedSetup"] = Napi::Function::New(env, LoadTrustedSetup);
exports["freeTrustedSetup"] = Napi::Function::New(env, FreeTrustedSetup);
exports["verifyKzgProof"] = Napi::Function::New(env, VerifyKzgProof);
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["verifyAggregateKzgProof"] = Napi::Function::New(env, VerifyAggregateKzgProof);

Expand Down
10 changes: 10 additions & 0 deletions bindings/node.js/kzg.ts
Expand Up @@ -24,6 +24,12 @@ type KZG = {

blobToKzgCommitment: (blob: Blob, setupHandle: SetupHandle) => KZGCommitment;

computeKzgProof: (
blob: Blob,
zBytes: Bytes32,
setupHandle: SetupHandle,
) => KZGProof;

computeAggregateKzgProof: (
blobs: Blob[],
setupHandle: SetupHandle,
Expand Down Expand Up @@ -110,6 +116,10 @@ export function blobToKzgCommitment(blob: Blob): KZGCommitment {
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 {
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
}
Expand Down
8 changes: 8 additions & 0 deletions bindings/node.js/test.ts
Expand Up @@ -6,6 +6,7 @@ import {
freeTrustedSetup,
blobToKzgCommitment,
verifyKzgProof,
computeKzgProof,
computeAggregateKzgProof,
verifyAggregateKzgProof,
BYTES_PER_FIELD_ELEMENT,
Expand Down Expand Up @@ -45,6 +46,13 @@ describe("C-KZG", () => {
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", () => {
let blobs = new Array(2).fill(0).map(generateRandomBlob);
let commitments = blobs.map(blobToKzgCommitment);
Expand Down

0 comments on commit 9076280

Please sign in to comment.