Update Compute*KZGProof in node bindings (#188)
* feat(node-bindings): update Compute*KZGProof * ComputationProof -> ProofResult --------- Co-authored-by: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Co-authored-by: George Kadianakis <desnacked@riseup.net>
This commit is contained in:
parent
87a3e4148d
commit
5ebf750a1e
|
@ -189,7 +189,8 @@ Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) {
|
|||
* @param[in] {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param[in] {Bytes32} zBytes - The generator z-value for the evaluation points
|
||||
*
|
||||
* @return {KZGProof} - The resulting proof
|
||||
* @return {ProofResult} - Tuple containing the resulting proof and evaluation
|
||||
* of the polynomial at the evaluation point z
|
||||
*
|
||||
* @throws {TypeError} - for invalid arguments or failure of the native library
|
||||
*/
|
||||
|
@ -209,8 +210,10 @@ Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) {
|
|||
}
|
||||
|
||||
KZGProof proof;
|
||||
Bytes32 y_out;
|
||||
C_KZG_RET ret = compute_kzg_proof(
|
||||
&proof,
|
||||
&y_out,
|
||||
blob,
|
||||
z_bytes,
|
||||
kzg_settings
|
||||
|
@ -222,7 +225,10 @@ Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) {
|
|||
return env.Null();
|
||||
}
|
||||
|
||||
return Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF);
|
||||
Napi::Array tuple = Napi::Array::New(env, 2);
|
||||
tuple[(uint32_t)0] = Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF);
|
||||
tuple[(uint32_t)1] = Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&y_out), BYTES_PER_FIELD_ELEMENT);
|
||||
return tuple;
|
||||
}
|
||||
|
||||
|
||||
|
@ -230,7 +236,8 @@ Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) {
|
|||
* Given a blob, return the KZG proof that is used to verify it against the
|
||||
* commitment.
|
||||
*
|
||||
* @param[in] {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param[in] {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param[in] {Bytes48} commitmentBytes - Commitment to verify
|
||||
*
|
||||
* @return {KZGProof} - The resulting proof
|
||||
*
|
||||
|
@ -242,6 +249,10 @@ Napi::Value ComputeBlobKzgProof(const Napi::CallbackInfo& info) {
|
|||
if (blob == nullptr) {
|
||||
return env.Null();
|
||||
}
|
||||
Bytes48 *commitment_bytes = get_bytes48(env, info[1], "commitmentBytes");
|
||||
if (commitment_bytes == nullptr) {
|
||||
return env.Null();
|
||||
}
|
||||
KZGSettings *kzg_settings = get_kzg_settings(env, info);
|
||||
if (kzg_settings == nullptr) {
|
||||
return env.Null();
|
||||
|
@ -251,6 +262,7 @@ Napi::Value ComputeBlobKzgProof(const Napi::CallbackInfo& info) {
|
|||
C_KZG_RET ret = compute_blob_kzg_proof(
|
||||
&proof,
|
||||
blob,
|
||||
commitment_bytes,
|
||||
kzg_settings
|
||||
);
|
||||
|
||||
|
|
|
@ -11,12 +11,15 @@ export type Bytes48 = Uint8Array; // 48 bytes
|
|||
export type KZGProof = Buffer; // 48 bytes
|
||||
export type KZGCommitment = Buffer; // 48 bytes
|
||||
export type Blob = Uint8Array; // 4096 * 32 bytes
|
||||
export type ProofResult = [KZGProof, Bytes32];
|
||||
|
||||
export interface TrustedSetupJson {
|
||||
setup_G1: string[];
|
||||
setup_G2: string[];
|
||||
setup_G1_lagrange: string[];
|
||||
roots_of_unity: string[];
|
||||
}
|
||||
|
||||
// The C++ native addon interface
|
||||
interface KZG {
|
||||
BYTES_PER_BLOB: number;
|
||||
|
@ -29,9 +32,9 @@ interface KZG {
|
|||
|
||||
blobToKzgCommitment: (blob: Blob) => KZGCommitment;
|
||||
|
||||
computeKzgProof: (blob: Blob, zBytes: Bytes32) => KZGProof;
|
||||
computeKzgProof: (blob: Blob, zBytes: Bytes32) => ProofResult;
|
||||
|
||||
computeBlobKzgProof: (blob: Blob) => KZGProof;
|
||||
computeBlobKzgProof: (blob: Blob, commitmentBytes: Bytes48) => KZGProof;
|
||||
|
||||
verifyKzgProof: (
|
||||
commitmentBytes: Bytes48,
|
||||
|
@ -132,11 +135,12 @@ export function blobToKzgCommitment(blob: Blob): KZGCommitment {
|
|||
* @param {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param {Bytes32} zBytes - The generator z-value for the evaluation points
|
||||
*
|
||||
* @return {KZGProof} - The resulting proof
|
||||
* @return {ProofResult} - Tuple containing the resulting proof and evaluation
|
||||
* of the polynomial at the evaluation point z
|
||||
*
|
||||
* @throws {TypeError} - For invalid arguments or failure of the native library
|
||||
*/
|
||||
export function computeKzgProof(blob: Blob, zBytes: Bytes32): KZGProof {
|
||||
export function computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult {
|
||||
return kzg.computeKzgProof(blob, zBytes);
|
||||
}
|
||||
|
||||
|
@ -144,14 +148,18 @@ export function computeKzgProof(blob: Blob, zBytes: Bytes32): KZGProof {
|
|||
* Given a blob, return the KZG proof that is used to verify it against the
|
||||
* commitment.
|
||||
*
|
||||
* @param {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param {Blob} blob - The blob (polynomial) to generate a proof for
|
||||
* @param {Bytes48} commitmentBytes - Commitment to verify
|
||||
*
|
||||
* @return {KZGProof} - The resulting proof
|
||||
*
|
||||
* @throws {TypeError} - For invalid arguments or failure of the native library
|
||||
*/
|
||||
export function computeBlobKzgProof(blob: Blob): KZGProof {
|
||||
return kzg.computeBlobKzgProof(blob);
|
||||
export function computeBlobKzgProof(
|
||||
blob: Blob,
|
||||
commitmentBytes: Bytes48,
|
||||
): KZGProof {
|
||||
return kzg.computeBlobKzgProof(blob, commitmentBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,14 @@ import { globSync } from "glob";
|
|||
|
||||
const yaml = require("js-yaml");
|
||||
|
||||
interface TestMeta<
|
||||
I extends Record<string, any>,
|
||||
O extends boolean | string | string[] | Record<string, any>,
|
||||
> {
|
||||
input: I;
|
||||
output: O;
|
||||
}
|
||||
|
||||
import {
|
||||
loadTrustedSetup,
|
||||
blobToKzgCommitment,
|
||||
|
@ -17,6 +25,7 @@ import {
|
|||
BYTES_PER_COMMITMENT,
|
||||
BYTES_PER_PROOF,
|
||||
BYTES_PER_FIELD_ELEMENT,
|
||||
ProofResult,
|
||||
} from "./kzg";
|
||||
|
||||
const setupFileName = "testing_trusted_setups.json";
|
||||
|
@ -28,23 +37,41 @@ const SETUP_FILE_PATH = existsSync(setupFileName)
|
|||
const MAX_TOP_BYTE = 114;
|
||||
|
||||
const TEST_DIR = "../../tests";
|
||||
type BlobToKzgCommitmentTest = TestMeta<{ blob: string }, string>;
|
||||
const BLOB_TO_KZG_COMMITMENT_TESTS = join(
|
||||
TEST_DIR,
|
||||
"blob_to_kzg_commitment/*/*/data.yaml",
|
||||
);
|
||||
type ComputeKzgProofTest = TestMeta<{ blob: string; z: string }, string[]>;
|
||||
const COMPUTE_KZG_PROOF_TESTS = join(
|
||||
TEST_DIR,
|
||||
"compute_kzg_proof/*/*/data.yaml",
|
||||
);
|
||||
type ComputeBlobKzgProofTest = TestMeta<
|
||||
{ blob: string; commitment: string },
|
||||
string
|
||||
>;
|
||||
const COMPUTE_BLOB_KZG_PROOF_TESTS = join(
|
||||
TEST_DIR,
|
||||
"compute_blob_kzg_proof/*/*/data.yaml",
|
||||
);
|
||||
type VerifyKzgProofTest = TestMeta<
|
||||
{ commitment: string; y: string; z: string; proof: string },
|
||||
boolean
|
||||
>;
|
||||
const VERIFY_KZG_PROOF_TESTS = join(TEST_DIR, "verify_kzg_proof/*/*/data.yaml");
|
||||
type VerifyBlobKzgProofTest = TestMeta<
|
||||
{ blob: string; commitment: string; proof: string },
|
||||
boolean
|
||||
>;
|
||||
const VERIFY_BLOB_KZG_PROOF_TESTS = join(
|
||||
TEST_DIR,
|
||||
"verify_blob_kzg_proof/*/*/data.yaml",
|
||||
);
|
||||
type VerifyBatchKzgProofTest = TestMeta<
|
||||
{ blobs: string[]; commitments: string[]; proofs: string[] },
|
||||
boolean
|
||||
>;
|
||||
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = join(
|
||||
TEST_DIR,
|
||||
"verify_blob_kzg_proof_batch/*/*/data.yaml",
|
||||
|
@ -84,7 +111,9 @@ describe("C-KZG", () => {
|
|||
it("reference tests for blobToKzgCommitment should pass", () => {
|
||||
let tests = globSync(BLOB_TO_KZG_COMMITMENT_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: BlobToKzgCommitmentTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let commitment: Buffer;
|
||||
let blob = bytesFromHex(test.input.blob);
|
||||
|
@ -105,9 +134,11 @@ describe("C-KZG", () => {
|
|||
it("reference tests for computeKzgProof should pass", () => {
|
||||
let tests = globSync(COMPUTE_KZG_PROOF_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: ComputeKzgProofTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let proof: Buffer;
|
||||
let proof: ProofResult;
|
||||
let blob = bytesFromHex(test.input.blob);
|
||||
let z = bytesFromHex(test.input.z);
|
||||
|
||||
|
@ -119,21 +150,23 @@ describe("C-KZG", () => {
|
|||
}
|
||||
|
||||
expect(test.output).not.toBeNull();
|
||||
let expectedProof = bytesFromHex(test.output);
|
||||
expect(proof).toEqual(expectedProof);
|
||||
expect(proof).toEqual(test.output.map((hex) => bytesFromHex(hex)));
|
||||
});
|
||||
});
|
||||
|
||||
it("reference tests for computeBlobKzgProof should pass", () => {
|
||||
let tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: ComputeBlobKzgProofTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let proof: Buffer;
|
||||
let blob = bytesFromHex(test.input.blob);
|
||||
let commitment = bytesFromHex(test.input.commitment);
|
||||
|
||||
try {
|
||||
proof = computeBlobKzgProof(blob);
|
||||
proof = computeBlobKzgProof(blob, commitment);
|
||||
} catch (err) {
|
||||
expect(test.output).toBeNull();
|
||||
return;
|
||||
|
@ -148,7 +181,9 @@ describe("C-KZG", () => {
|
|||
it("reference tests for verifyKzgProof should pass", () => {
|
||||
let tests = globSync(VERIFY_KZG_PROOF_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: VerifyKzgProofTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let valid;
|
||||
let commitment = bytesFromHex(test.input.commitment);
|
||||
|
@ -170,7 +205,9 @@ describe("C-KZG", () => {
|
|||
it("reference tests for verifyBlobKzgProof should pass", () => {
|
||||
let tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: VerifyBlobKzgProofTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let valid;
|
||||
let blob = bytesFromHex(test.input.blob);
|
||||
|
@ -191,7 +228,9 @@ describe("C-KZG", () => {
|
|||
it("reference tests for verifyBlobKzgProofBatch should pass", () => {
|
||||
let tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
|
||||
tests.forEach((testFile: string) => {
|
||||
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||
const test: VerifyBatchKzgProofTest = yaml.load(
|
||||
readFileSync(testFile, "ascii"),
|
||||
);
|
||||
|
||||
let valid;
|
||||
let blobs = test.input.blobs.map(bytesFromHex);
|
||||
|
@ -246,12 +285,16 @@ describe("C-KZG", () => {
|
|||
describe("edge cases for computeBlobKzgProof", () => {
|
||||
it("computes a proof from blob", () => {
|
||||
let blob = generateRandomBlob();
|
||||
computeBlobKzgProof(blob);
|
||||
let commitment = blobToKzgCommitment(blob);
|
||||
computeBlobKzgProof(blob, commitment);
|
||||
});
|
||||
it("throws as expected when given an argument of invalid length", () => {
|
||||
expect(() => computeBlobKzgProof(blobBadLength)).toThrowError(
|
||||
"Expected blob to be 131072 bytes",
|
||||
);
|
||||
expect(() =>
|
||||
computeBlobKzgProof(
|
||||
blobBadLength,
|
||||
blobToKzgCommitment(generateRandomBlob()),
|
||||
),
|
||||
).toThrowError("Expected blob to be 131072 bytes");
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -317,21 +360,23 @@ describe("C-KZG", () => {
|
|||
it("correct blob/commitment/proof should verify as true", () => {
|
||||
let blob = generateRandomBlob();
|
||||
let commitment = blobToKzgCommitment(blob);
|
||||
let proof = computeBlobKzgProof(blob);
|
||||
let proof = computeBlobKzgProof(blob, commitment);
|
||||
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(true);
|
||||
});
|
||||
|
||||
it("incorrect commitment should verify as false", () => {
|
||||
let blob = generateRandomBlob();
|
||||
let commitment = blobToKzgCommitment(generateRandomBlob());
|
||||
let proof = computeBlobKzgProof(blob);
|
||||
let proof = computeBlobKzgProof(blob, commitment);
|
||||
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
|
||||
});
|
||||
|
||||
it("incorrect proof should verify as false", () => {
|
||||
let blob = generateRandomBlob();
|
||||
let commitment = blobToKzgCommitment(blob);
|
||||
let proof = computeBlobKzgProof(generateRandomBlob());
|
||||
let randomBlob = generateRandomBlob();
|
||||
let randomCommitment = blobToKzgCommitment(randomBlob);
|
||||
let proof = computeBlobKzgProof(randomBlob, randomCommitment);
|
||||
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
|
||||
});
|
||||
it("throws as expected when given an argument of invalid length", () => {
|
||||
|
@ -415,7 +460,7 @@ describe("C-KZG", () => {
|
|||
for (let [i] of blobs.entries()) {
|
||||
blobs[i] = generateRandomBlob();
|
||||
commitments[i] = blobToKzgCommitment(blobs[i]);
|
||||
proofs[i] = computeBlobKzgProof(blobs[i]);
|
||||
proofs[i] = computeBlobKzgProof(blobs[i], commitments[i]);
|
||||
}
|
||||
|
||||
expect(verifyBlobKzgProofBatch(blobs, commitments, proofs)).toBe(true);
|
||||
|
|
Loading…
Reference in New Issue