Update nodejs bindings to support YAML ref tests (#167)
* Update nodejs bindings to support YAML ref tests * Remove c-kzg from deps * Fix test checks * Add return statements in error checks * Fix uint8array comparisons * Revert test_dir to original value
This commit is contained in:
parent
aaaacf7432
commit
54ab82c93b
|
@ -17,7 +17,9 @@
|
||||||
"rollup": "^3.2.5",
|
"rollup": "^3.2.5",
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.3",
|
||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
"typescript": "^4.8.4"
|
"typescript": "^4.8.4",
|
||||||
|
"glob": "^9.1.0",
|
||||||
|
"js-yaml": "^4.1.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"node-addon-api": "^5.0.0"
|
"node-addon-api": "^5.0.0"
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { randomBytes } from "crypto";
|
import { randomBytes } from "crypto";
|
||||||
import { existsSync } from "fs";
|
import { existsSync, readFileSync } from "fs";
|
||||||
import path = require("path");
|
import { join } from "path";
|
||||||
import fs = require("fs");
|
import { globSync } from "glob";
|
||||||
|
|
||||||
|
const yaml = require("js-yaml");
|
||||||
|
|
||||||
import {
|
import {
|
||||||
loadTrustedSetup,
|
loadTrustedSetup,
|
||||||
|
@ -28,34 +30,27 @@ const SETUP_FILE_PATH = existsSync(setupFileName)
|
||||||
const MAX_TOP_BYTE = 114;
|
const MAX_TOP_BYTE = 114;
|
||||||
|
|
||||||
const TEST_DIR = "../../tests";
|
const TEST_DIR = "../../tests";
|
||||||
const BLOB_TO_KZG_COMMITMENT_TESTS = path.join(
|
const BLOB_TO_KZG_COMMITMENT_TESTS = join(
|
||||||
TEST_DIR,
|
TEST_DIR,
|
||||||
"blob_to_kzg_commitment",
|
"blob_to_kzg_commitment/*/*/data.yaml",
|
||||||
);
|
);
|
||||||
const COMPUTE_KZG_PROOF_TESTS = path.join(TEST_DIR, "compute_kzg_proof");
|
const COMPUTE_KZG_PROOF_TESTS = join(
|
||||||
const COMPUTE_BLOB_KZG_PROOF_TESTS = path.join(
|
|
||||||
TEST_DIR,
|
TEST_DIR,
|
||||||
"compute_blob_kzg_proof",
|
"compute_kzg_proof/*/*/data.yaml",
|
||||||
);
|
);
|
||||||
const VERIFY_KZG_PROOF_TESTS = path.join(TEST_DIR, "verify_kzg_proof");
|
const COMPUTE_BLOB_KZG_PROOF_TESTS = join(
|
||||||
const VERIFY_BLOB_KZG_PROOF_TESTS = path.join(
|
|
||||||
TEST_DIR,
|
TEST_DIR,
|
||||||
"verify_blob_kzg_proof",
|
"compute_blob_kzg_proof/*/*/data.yaml",
|
||||||
);
|
);
|
||||||
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = path.join(
|
const VERIFY_KZG_PROOF_TESTS = join(TEST_DIR, "verify_kzg_proof/*/*/data.yaml");
|
||||||
|
const VERIFY_BLOB_KZG_PROOF_TESTS = join(
|
||||||
TEST_DIR,
|
TEST_DIR,
|
||||||
"verify_blob_kzg_proof_batch",
|
"verify_blob_kzg_proof/*/*/data.yaml",
|
||||||
|
);
|
||||||
|
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = join(
|
||||||
|
TEST_DIR,
|
||||||
|
"verify_blob_kzg_proof_batch/*/*/data.yaml",
|
||||||
);
|
);
|
||||||
|
|
||||||
function getBytes(file: String): Uint8Array {
|
|
||||||
const data = require("fs").readFileSync(file, "ascii");
|
|
||||||
return Buffer.from(data, "hex");
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBoolean(file: String): boolean {
|
|
||||||
const data = require("fs").readFileSync(file, "ascii");
|
|
||||||
return data.includes("true");
|
|
||||||
}
|
|
||||||
|
|
||||||
const generateRandomBlob = () => {
|
const generateRandomBlob = () => {
|
||||||
return new Uint8Array(
|
return new Uint8Array(
|
||||||
|
@ -69,6 +64,10 @@ const generateRandomBlob = () => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function bytesFromHex(hexstring: string): Uint8Array {
|
||||||
|
return Uint8Array.from(Buffer.from(hexstring.slice(2), "hex"));
|
||||||
|
}
|
||||||
|
|
||||||
describe("C-KZG", () => {
|
describe("C-KZG", () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
|
const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
|
||||||
|
@ -81,122 +80,130 @@ describe("C-KZG", () => {
|
||||||
|
|
||||||
describe("reference tests should pass", () => {
|
describe("reference tests should pass", () => {
|
||||||
it("reference tests for blobToKzgCommitment should pass", () => {
|
it("reference tests for blobToKzgCommitment should pass", () => {
|
||||||
let tests = fs.readdirSync(BLOB_TO_KZG_COMMITMENT_TESTS);
|
let tests = globSync(BLOB_TO_KZG_COMMITMENT_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(BLOB_TO_KZG_COMMITMENT_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let blob = getBytes(path.join(testPath, "blob.txt"));
|
|
||||||
|
let commitment = new Uint8Array();
|
||||||
|
let blob = bytesFromHex(test.input.blob);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let commitment = blobToKzgCommitment(blob);
|
commitment = blobToKzgCommitment(blob);
|
||||||
let expectedCommitment = getBytes(
|
|
||||||
path.join(testPath, "commitment.txt"),
|
|
||||||
);
|
|
||||||
expect(commitment.buffer).toEqual(expectedCommitment.buffer);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "commitment.txt"))).toBe(
|
expect(test.output).toBeNull();
|
||||||
false,
|
return;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(test.output).not.toBeNull();
|
||||||
|
let expectedCommitment = bytesFromHex(test.output);
|
||||||
|
expect(commitment).toEqual(expectedCommitment);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reference tests for computeKzgProof should pass", () => {
|
it("reference tests for computeKzgProof should pass", () => {
|
||||||
let tests = fs.readdirSync(COMPUTE_KZG_PROOF_TESTS);
|
let tests = globSync(COMPUTE_KZG_PROOF_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(COMPUTE_KZG_PROOF_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let blob = getBytes(path.join(testPath, "blob.txt"));
|
|
||||||
let inputPoint = getBytes(path.join(testPath, "input_point.txt"));
|
let proof = new Uint8Array();
|
||||||
|
let blob = bytesFromHex(test.input.blob);
|
||||||
|
let z = bytesFromHex(test.input.z);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let proof = computeKzgProof(blob, inputPoint);
|
proof = computeKzgProof(blob, z);
|
||||||
let expectedProof = getBytes(path.join(testPath, "proof.txt"));
|
|
||||||
expect(proof.buffer).toEqual(expectedProof.buffer);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "proof.txt"))).toBe(false);
|
expect(test.output).toBeNull();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(test.output).not.toBeNull();
|
||||||
|
let expectedProof = bytesFromHex(test.output);
|
||||||
|
expect(proof).toEqual(expectedProof);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reference tests for computeBlobKzgProof should pass", () => {
|
it("reference tests for computeBlobKzgProof should pass", () => {
|
||||||
let tests = fs.readdirSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
|
let tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(COMPUTE_BLOB_KZG_PROOF_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let blob = getBytes(path.join(testPath, "blob.txt"));
|
|
||||||
|
let proof = new Uint8Array();
|
||||||
|
let blob = bytesFromHex(test.input.blob);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let proof = computeBlobKzgProof(blob);
|
proof = computeBlobKzgProof(blob);
|
||||||
let expectedProof = getBytes(path.join(testPath, "proof.txt"));
|
|
||||||
expect(proof.buffer).toEqual(expectedProof.buffer);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "proof.txt"))).toBe(false);
|
expect(test.output).toBeNull();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(test.output).not.toBeNull();
|
||||||
|
let expectedProof = bytesFromHex(test.output);
|
||||||
|
expect(proof).toEqual(expectedProof);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reference tests for verifyKzgProof should pass", () => {
|
it("reference tests for verifyKzgProof should pass", () => {
|
||||||
let tests = fs.readdirSync(VERIFY_KZG_PROOF_TESTS);
|
let tests = globSync(VERIFY_KZG_PROOF_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(VERIFY_KZG_PROOF_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let commitment = getBytes(path.join(testPath, "commitment.txt"));
|
|
||||||
let inputPoint = getBytes(path.join(testPath, "input_point.txt"));
|
let valid;
|
||||||
let claimedValue = getBytes(path.join(testPath, "claimed_value.txt"));
|
let commitment = bytesFromHex(test.input.commitment);
|
||||||
let proof = getBytes(path.join(testPath, "proof.txt"));
|
let z = bytesFromHex(test.input.z);
|
||||||
|
let y = bytesFromHex(test.input.y);
|
||||||
|
let proof = bytesFromHex(test.input.proof);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let ok = verifyKzgProof(commitment, inputPoint, claimedValue, proof);
|
valid = verifyKzgProof(commitment, z, y, proof);
|
||||||
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
|
|
||||||
expect(ok).toEqual(expectedOk);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
|
expect(test.output).toBeNull();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(valid).toEqual(test.output);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reference tests for verifyBlobKzgProof should pass", () => {
|
it("reference tests for verifyBlobKzgProof should pass", () => {
|
||||||
let tests = fs.readdirSync(VERIFY_BLOB_KZG_PROOF_TESTS);
|
let tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(VERIFY_BLOB_KZG_PROOF_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let blob = getBytes(path.join(testPath, "blob.txt"));
|
|
||||||
let commitment = getBytes(path.join(testPath, "commitment.txt"));
|
let valid;
|
||||||
let proof = getBytes(path.join(testPath, "proof.txt"));
|
let blob = bytesFromHex(test.input.blob);
|
||||||
|
let commitment = bytesFromHex(test.input.commitment);
|
||||||
|
let proof = bytesFromHex(test.input.proof);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let ok = verifyBlobKzgProof(blob, commitment, proof);
|
valid = verifyBlobKzgProof(blob, commitment, proof);
|
||||||
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
|
|
||||||
expect(ok).toEqual(expectedOk);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
|
expect(test.output).toBeNull();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(valid).toEqual(test.output);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reference tests for verifyBlobKzgProofBatch should pass", () => {
|
it("reference tests for verifyBlobKzgProofBatch should pass", () => {
|
||||||
let tests = fs.readdirSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
|
let tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
|
||||||
tests.forEach((test) => {
|
tests.forEach((testFile: string) => {
|
||||||
let testPath = path.join(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS, test);
|
const test = yaml.load(readFileSync(testFile, "ascii"));
|
||||||
let blobs = fs
|
|
||||||
.readdirSync(path.join(testPath, "blobs"))
|
let valid;
|
||||||
.sort()
|
let blobs = test.input.blobs.map(bytesFromHex);
|
||||||
.map((filename) => {
|
let commitments = test.input.commitments.map(bytesFromHex);
|
||||||
return path.join(testPath, "blobs", filename);
|
let proofs = test.input.proofs.map(bytesFromHex);
|
||||||
})
|
|
||||||
.map(getBytes);
|
|
||||||
let commitments = fs
|
|
||||||
.readdirSync(path.join(testPath, "commitments"))
|
|
||||||
.sort()
|
|
||||||
.map((filename) => {
|
|
||||||
return path.join(testPath, "commitments", filename);
|
|
||||||
})
|
|
||||||
.map(getBytes);
|
|
||||||
let proofs = fs
|
|
||||||
.readdirSync(path.join(testPath, "proofs"))
|
|
||||||
.sort()
|
|
||||||
.map((filename) => {
|
|
||||||
return path.join(testPath, "proofs", filename);
|
|
||||||
})
|
|
||||||
.map(getBytes);
|
|
||||||
try {
|
try {
|
||||||
let ok = verifyBlobKzgProofBatch(blobs, commitments, proofs);
|
valid = verifyBlobKzgProofBatch(blobs, commitments, proofs);
|
||||||
let expectedOk = getBoolean(path.join(testPath, "ok.txt"));
|
|
||||||
expect(ok).toEqual(expectedOk);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
expect(fs.existsSync(path.join(testPath, "ok.txt"))).toBe(false);
|
expect(test.output).toBeNull();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expect(valid).toEqual(test.output);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue