Add eslint to node bindings (#269)

* add eslint and run for TS files

* add eslint and run for JS/TS

* remove eslint file comments

* add endOfLine: "auto" for windows

* fix yarn command to remove excess output

* update eslint versions
This commit is contained in:
Matthew Keil 2023-03-31 04:46:54 -04:00 committed by GitHub
parent 6bac4e1b6c
commit feb4037de5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1407 additions and 227 deletions

View File

@ -0,0 +1,96 @@
module.exports = {
root: true,
env: {
browser: true,
es6: true,
node: true,
mocha: true,
},
globals: {
BigInt: true,
},
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 10,
project: "./tsconfig.json",
},
plugins: ["@typescript-eslint", "eslint-plugin-import", "eslint-plugin-node", "prettier"],
extends: [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:@typescript-eslint/recommended",
],
rules: {
"prettier/prettier": ["error", {}],
"constructor-super": "off",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/explicit-function-return-type": ["off"],
"@typescript-eslint/func-call-spacing": "error",
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
varsIgnorePattern: "^_",
},
],
"@typescript-eslint/ban-ts-comment": "warn",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/semi": "error",
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {accessibility: "no-public"}],
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-return": "off",
"import/no-extraneous-dependencies": [
"error",
{
devDependencies: false,
optionalDependencies: false,
peerDependencies: false,
},
],
"func-call-spacing": "off",
"import/no-duplicates": "off",
"node/no-deprecated-api": "error",
"new-parens": "error",
"no-caller": "error",
"no-bitwise": "off",
"no-cond-assign": "error",
"no-consecutive-blank-lines": 0,
"no-console": "warn",
"no-var": "error",
"object-curly-spacing": ["error", "never"],
"object-literal-sort-keys": 0,
"no-prototype-builtins": 0,
"prefer-const": "error",
quotes: ["error", "double"],
semi: "off",
},
settings: {
"import/core-modules": ["node:child_process", "node:crypto", "node:fs", "node:os", "node:path", "node:util"],
},
overrides: [
{
files: ["**/test/**/*.ts"],
rules: {
"import/no-extraneous-dependencies": "off",
"@typescript-eslint/no-explicit-any": "off",
},
},
{
files: ["*.ts", "*.mts", "*.cts", "*.tsx"],
rules: {
"@typescript-eslint/explicit-function-return-type": [
"error",
{
allowExpressions: true,
},
],
},
},
],
};

View File

@ -1,9 +0,0 @@
{
"trailingComma": "all",
"overrides": [
{
"files": "*.gyp",
"options": { "parser": "json" }
}
]
}

View File

@ -0,0 +1,10 @@
printWidth: 120
tabWidth: 2
useTabs: false
semi: true
singleQuote: false
quoteProps: "as-needed"
trailingComma: "es5"
bracketSpacing: false
arrowParens: "always"
endOfLine: "auto"

View File

@ -61,7 +61,7 @@ test: install
# Lint js/ts code # Lint js/ts code
.PHONY: format .PHONY: format
format: install format: install
@$(YARN) prettier --loglevel=warn --write . @$(YARN) eslint --quiet --color --ext .ts lib/ test/
@clang-format -i src/kzg.cxx @clang-format -i src/kzg.cxx
# Publish package to npm (requires an auth token) # Publish package to npm (requires an auth token)

View File

@ -68,10 +68,7 @@ export function computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult;
* *
* @throws {TypeError} - For invalid arguments or failure of the native library * @throws {TypeError} - For invalid arguments or failure of the native library
*/ */
export function computeBlobKzgProof( export function computeBlobKzgProof(blob: Blob, commitmentBytes: Bytes48): KZGProof;
blob: Blob,
commitmentBytes: Bytes48,
): KZGProof;
/** /**
* Verify a KZG poof claiming that `p(z) == y`. * Verify a KZG poof claiming that `p(z) == y`.
@ -85,12 +82,7 @@ export function computeBlobKzgProof(
* *
* @throws {TypeError} - For invalid arguments or failure of the native library * @throws {TypeError} - For invalid arguments or failure of the native library
*/ */
export function verifyKzgProof( export function verifyKzgProof(commitment: Bytes48, zBytes: Bytes32, yBytes: Bytes32, proof: Bytes48): boolean;
commitment: Bytes48,
zBytes: Bytes32,
yBytes: Bytes32,
proof: Bytes48,
): boolean;
/** /**
* Given a blob and its proof, verify that it corresponds to the provided * Given a blob and its proof, verify that it corresponds to the provided
@ -104,11 +96,7 @@ export function verifyKzgProof(
* *
* @throws {TypeError} - For invalid arguments or failure of the native library * @throws {TypeError} - For invalid arguments or failure of the native library
*/ */
export function verifyBlobKzgProof( export function verifyBlobKzgProof(blob: Blob, commitment: Bytes48, proof: Bytes48): boolean;
blob: Blob,
commitment: Bytes48,
proof: Bytes48,
): boolean;
/** /**
* Given an array of blobs and their proofs, verify that they corresponds to their * Given an array of blobs and their proofs, verify that they corresponds to their
@ -124,8 +112,4 @@ export function verifyBlobKzgProof(
* *
* @throws {TypeError} - For invalid arguments or failure of the native library * @throws {TypeError} - For invalid arguments or failure of the native library
*/ */
export function verifyBlobKzgProofBatch( export function verifyBlobKzgProofBatch(blobs: Blob[], commitments: Bytes48[], proofs: Bytes48[]): boolean;
blobs: Blob[],
commitments: Bytes48[],
proofs: Bytes48[],
): boolean;

View File

@ -35,9 +35,7 @@ const originalLoadTrustedSetup = bindings.loadTrustedSetup;
// docstring in ./kzg.d.ts with exported definition // docstring in ./kzg.d.ts with exported definition
bindings.loadTrustedSetup = function loadTrustedSetup(filePath) { bindings.loadTrustedSetup = function loadTrustedSetup(filePath) {
if (!(filePath && typeof filePath === "string")) { if (!(filePath && typeof filePath === "string")) {
throw new TypeError( throw new TypeError("must initialize kzg with the filePath to a txt/json trusted setup");
"must initialize kzg with the filePath to a txt/json trusted setup",
);
} }
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
throw new Error(`no trusted setup found: ${filePath}`); throw new Error(`no trusted setup found: ${filePath}`);

View File

@ -13,6 +13,12 @@
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^29.2.1", "@types/jest": "^29.2.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"eslint": "^8.37.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.2.2", "jest": "^29.2.2",
"node-gyp": "^9.3.0", "node-gyp": "^9.3.0",
"prettier": "2.7.1", "prettier": "2.7.1",

View File

@ -1,14 +1,11 @@
import { randomBytes } from "crypto"; import {randomBytes} from "crypto";
import { readFileSync } from "fs"; import {readFileSync} from "fs";
import { resolve } from "path"; import {resolve} from "path";
import { globSync } from "glob"; import {globSync} from "glob";
const yaml = require("js-yaml"); const yaml = require("js-yaml");
interface TestMeta< interface TestMeta<I extends Record<string, any>, O extends boolean | string | string[] | Record<string, any>> {
I extends Record<string, any>,
O extends boolean | string | string[] | Record<string, any>,
> {
input: I; input: I;
output: O; output: O;
} }
@ -28,45 +25,25 @@ import {
ProofResult, ProofResult,
} from "../lib/kzg"; } from "../lib/kzg";
const SETUP_FILE_PATH = resolve( const SETUP_FILE_PATH = resolve(__dirname, "__fixtures__", "testing_trusted_setups.json");
__dirname,
"__fixtures__",
"testing_trusted_setups.json",
);
const MAX_TOP_BYTE = 114; const MAX_TOP_BYTE = 114;
const BLOB_TO_KZG_COMMITMENT_TESTS = const BLOB_TO_KZG_COMMITMENT_TESTS = "../../tests/blob_to_kzg_commitment/*/*/data.yaml";
"../../tests/blob_to_kzg_commitment/*/*/data.yaml";
const COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/*/*/data.yaml"; const COMPUTE_KZG_PROOF_TESTS = "../../tests/compute_kzg_proof/*/*/data.yaml";
const COMPUTE_BLOB_KZG_PROOF_TESTS = const COMPUTE_BLOB_KZG_PROOF_TESTS = "../../tests/compute_blob_kzg_proof/*/*/data.yaml";
"../../tests/compute_blob_kzg_proof/*/*/data.yaml";
const VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/*/*/data.yaml"; const VERIFY_KZG_PROOF_TESTS = "../../tests/verify_kzg_proof/*/*/data.yaml";
const VERIFY_BLOB_KZG_PROOF_TESTS = const VERIFY_BLOB_KZG_PROOF_TESTS = "../../tests/verify_blob_kzg_proof/*/*/data.yaml";
"../../tests/verify_blob_kzg_proof/*/*/data.yaml"; const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS = "../../tests/verify_blob_kzg_proof_batch/*/*/data.yaml";
const VERIFY_BLOB_KZG_PROOF_BATCH_TESTS =
"../../tests/verify_blob_kzg_proof_batch/*/*/data.yaml";
type BlobToKzgCommitmentTest = TestMeta<{ blob: string }, string>; type BlobToKzgCommitmentTest = TestMeta<{blob: string}, string>;
type ComputeKzgProofTest = TestMeta<{ blob: string; z: string }, string[]>; type ComputeKzgProofTest = TestMeta<{blob: string; z: string}, string[]>;
type ComputeBlobKzgProofTest = TestMeta< type ComputeBlobKzgProofTest = TestMeta<{blob: string; commitment: string}, string>;
{ blob: string; commitment: string }, type VerifyKzgProofTest = TestMeta<{commitment: string; y: string; z: string; proof: string}, boolean>;
string type VerifyBlobKzgProofTest = TestMeta<{blob: string; commitment: string; proof: string}, boolean>;
>; type VerifyBatchKzgProofTest = TestMeta<{blobs: string[]; commitments: string[]; proofs: string[]}, boolean>;
type VerifyKzgProofTest = TestMeta<
{ commitment: string; y: string; z: string; proof: string },
boolean
>;
type VerifyBlobKzgProofTest = TestMeta<
{ blob: string; commitment: string; proof: string },
boolean
>;
type VerifyBatchKzgProofTest = TestMeta<
{ blobs: string[]; commitments: string[]; proofs: string[] },
boolean
>;
const generateRandomBlob = () => { const generateRandomBlob = (): Uint8Array => {
return new Uint8Array( return new Uint8Array(
randomBytes(BYTES_PER_BLOB).map((x, i) => { randomBytes(BYTES_PER_BLOB).map((x, i) => {
// Set the top byte to be low enough that the field element doesn't overflow the BLS modulus // Set the top byte to be low enough that the field element doesn't overflow the BLS modulus
@ -74,7 +51,7 @@ const generateRandomBlob = () => {
return Math.floor(Math.random() * MAX_TOP_BYTE); return Math.floor(Math.random() * MAX_TOP_BYTE);
} }
return x; return x;
}), })
); );
}; };
@ -98,16 +75,14 @@ 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 = globSync(BLOB_TO_KZG_COMMITMENT_TESTS); const tests = globSync(BLOB_TO_KZG_COMMITMENT_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: BlobToKzgCommitmentTest = yaml.load( const test: BlobToKzgCommitmentTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let commitment: Buffer; let commitment: Buffer;
let blob = bytesFromHex(test.input.blob); const blob = bytesFromHex(test.input.blob);
try { try {
commitment = blobToKzgCommitment(blob); commitment = blobToKzgCommitment(blob);
@ -117,23 +92,21 @@ describe("C-KZG", () => {
} }
expect(test.output).not.toBeNull(); expect(test.output).not.toBeNull();
let expectedCommitment = bytesFromHex(test.output); const expectedCommitment = bytesFromHex(test.output);
expect(commitment).toEqual(expectedCommitment); expect(commitment).toEqual(expectedCommitment);
}); });
}); });
it("reference tests for computeKzgProof should pass", () => { it("reference tests for computeKzgProof should pass", () => {
let tests = globSync(COMPUTE_KZG_PROOF_TESTS); const tests = globSync(COMPUTE_KZG_PROOF_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: ComputeKzgProofTest = yaml.load( const test: ComputeKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let proof: ProofResult; let proof: ProofResult;
let blob = bytesFromHex(test.input.blob); const blob = bytesFromHex(test.input.blob);
let z = bytesFromHex(test.input.z); const z = bytesFromHex(test.input.z);
try { try {
proof = computeKzgProof(blob, z); proof = computeKzgProof(blob, z);
@ -148,17 +121,15 @@ describe("C-KZG", () => {
}); });
it("reference tests for computeBlobKzgProof should pass", () => { it("reference tests for computeBlobKzgProof should pass", () => {
let tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS); const tests = globSync(COMPUTE_BLOB_KZG_PROOF_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: ComputeBlobKzgProofTest = yaml.load( const test: ComputeBlobKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let proof: Buffer; let proof: Buffer;
let blob = bytesFromHex(test.input.blob); const blob = bytesFromHex(test.input.blob);
let commitment = bytesFromHex(test.input.commitment); const commitment = bytesFromHex(test.input.commitment);
try { try {
proof = computeBlobKzgProof(blob, commitment); proof = computeBlobKzgProof(blob, commitment);
@ -168,25 +139,23 @@ describe("C-KZG", () => {
} }
expect(test.output).not.toBeNull(); expect(test.output).not.toBeNull();
let expectedProof = bytesFromHex(test.output); const expectedProof = bytesFromHex(test.output);
expect(proof).toEqual(expectedProof); expect(proof).toEqual(expectedProof);
}); });
}); });
it("reference tests for verifyKzgProof should pass", () => { it("reference tests for verifyKzgProof should pass", () => {
let tests = globSync(VERIFY_KZG_PROOF_TESTS); const tests = globSync(VERIFY_KZG_PROOF_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: VerifyKzgProofTest = yaml.load( const test: VerifyKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let valid; let valid;
let commitment = bytesFromHex(test.input.commitment); const commitment = bytesFromHex(test.input.commitment);
let z = bytesFromHex(test.input.z); const z = bytesFromHex(test.input.z);
let y = bytesFromHex(test.input.y); const y = bytesFromHex(test.input.y);
let proof = bytesFromHex(test.input.proof); const proof = bytesFromHex(test.input.proof);
try { try {
valid = verifyKzgProof(commitment, z, y, proof); valid = verifyKzgProof(commitment, z, y, proof);
@ -200,18 +169,16 @@ describe("C-KZG", () => {
}); });
it("reference tests for verifyBlobKzgProof should pass", () => { it("reference tests for verifyBlobKzgProof should pass", () => {
let tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS); const tests = globSync(VERIFY_BLOB_KZG_PROOF_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: VerifyBlobKzgProofTest = yaml.load( const test: VerifyBlobKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let valid; let valid;
let blob = bytesFromHex(test.input.blob); const blob = bytesFromHex(test.input.blob);
let commitment = bytesFromHex(test.input.commitment); const commitment = bytesFromHex(test.input.commitment);
let proof = bytesFromHex(test.input.proof); const proof = bytesFromHex(test.input.proof);
try { try {
valid = verifyBlobKzgProof(blob, commitment, proof); valid = verifyBlobKzgProof(blob, commitment, proof);
@ -225,18 +192,16 @@ describe("C-KZG", () => {
}); });
it("reference tests for verifyBlobKzgProofBatch should pass", () => { it("reference tests for verifyBlobKzgProofBatch should pass", () => {
let tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS); const tests = globSync(VERIFY_BLOB_KZG_PROOF_BATCH_TESTS);
expect(tests.length).toBeGreaterThan(0); expect(tests.length).toBeGreaterThan(0);
tests.forEach((testFile: string) => { tests.forEach((testFile: string) => {
const test: VerifyBatchKzgProofTest = yaml.load( const test: VerifyBatchKzgProofTest = yaml.load(readFileSync(testFile, "ascii"));
readFileSync(testFile, "ascii"),
);
let valid; let valid;
let blobs = test.input.blobs.map(bytesFromHex); const blobs = test.input.blobs.map(bytesFromHex);
let commitments = test.input.commitments.map(bytesFromHex); const commitments = test.input.commitments.map(bytesFromHex);
let proofs = test.input.proofs.map(bytesFromHex); const proofs = test.input.proofs.map(bytesFromHex);
try { try {
valid = verifyBlobKzgProofBatch(blobs, commitments, proofs); valid = verifyBlobKzgProofBatch(blobs, commitments, proofs);
@ -252,50 +217,44 @@ describe("C-KZG", () => {
describe("edge cases for blobToKzgCommitment", () => { describe("edge cases for blobToKzgCommitment", () => {
it("throws as expected when given an argument of invalid type", () => { it("throws as expected when given an argument of invalid type", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error // @ts-expect-error
expect(() => blobToKzgCommitment("wrong type")).toThrowError( expect(() => blobToKzgCommitment("wrong type")).toThrowError("Expected blob to be a Uint8Array");
"Expected blob to be a Uint8Array",
);
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
expect(() => blobToKzgCommitment(blobBadLength)).toThrowError( expect(() => blobToKzgCommitment(blobBadLength)).toThrowError("Expected blob to be 131072 bytes");
"Expected blob to be 131072 bytes",
);
}); });
}); });
// TODO: add more tests for this function. // TODO: add more tests for this function.
describe("edge cases for computeKzgProof", () => { describe("edge cases for computeKzgProof", () => {
it("computes a proof from blob/field element", () => { it("computes a proof from blob/field element", () => {
let blob = generateRandomBlob(); const blob = generateRandomBlob();
const zBytes = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0); const zBytes = new Uint8Array(BYTES_PER_FIELD_ELEMENT).fill(0);
computeKzgProof(blob, zBytes); computeKzgProof(blob, zBytes);
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
expect(() => expect(() => computeKzgProof(blobBadLength, fieldElementValidLength)).toThrowError(
computeKzgProof(blobBadLength, fieldElementValidLength), "Expected blob to be 131072 bytes"
).toThrowError("Expected blob to be 131072 bytes"); );
expect(() => expect(() => computeKzgProof(blobValidLength, fieldElementBadLength)).toThrowError(
computeKzgProof(blobValidLength, fieldElementBadLength), "Expected zBytes to be 32 bytes"
).toThrowError("Expected zBytes to be 32 bytes"); );
}); });
}); });
// TODO: add more tests for this function. // TODO: add more tests for this function.
describe("edge cases for computeBlobKzgProof", () => { describe("edge cases for computeBlobKzgProof", () => {
it("computes a proof from blob", () => { it("computes a proof from blob", () => {
let blob = generateRandomBlob(); const blob = generateRandomBlob();
let commitment = blobToKzgCommitment(blob); const commitment = blobToKzgCommitment(blob);
computeBlobKzgProof(blob, commitment); computeBlobKzgProof(blob, commitment);
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
expect(() => expect(() => computeBlobKzgProof(blobBadLength, blobToKzgCommitment(generateRandomBlob()))).toThrowError(
computeBlobKzgProof( "Expected blob to be 131072 bytes"
blobBadLength, );
blobToKzgCommitment(generateRandomBlob()),
),
).toThrowError("Expected blob to be 131072 bytes");
}); });
}); });
@ -323,85 +282,53 @@ describe("C-KZG", () => {
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
expect(() => expect(() =>
verifyKzgProof( verifyKzgProof(commitmentBadLength, fieldElementValidLength, fieldElementValidLength, proofValidLength)
commitmentBadLength,
fieldElementValidLength,
fieldElementValidLength,
proofValidLength,
),
).toThrowError("Expected commitmentBytes to be 48 bytes"); ).toThrowError("Expected commitmentBytes to be 48 bytes");
expect(() => expect(() =>
verifyKzgProof( verifyKzgProof(commitmentValidLength, fieldElementBadLength, fieldElementValidLength, proofValidLength)
commitmentValidLength,
fieldElementBadLength,
fieldElementValidLength,
proofValidLength,
),
).toThrowError("Expected zBytes to be 32 bytes"); ).toThrowError("Expected zBytes to be 32 bytes");
expect(() => expect(() =>
verifyKzgProof( verifyKzgProof(commitmentValidLength, fieldElementValidLength, fieldElementBadLength, proofValidLength)
commitmentValidLength,
fieldElementValidLength,
fieldElementBadLength,
proofValidLength,
),
).toThrowError("Expected yBytes to be 32 bytes"); ).toThrowError("Expected yBytes to be 32 bytes");
expect(() => expect(() =>
verifyKzgProof( verifyKzgProof(commitmentValidLength, fieldElementValidLength, fieldElementValidLength, proofBadLength)
commitmentValidLength,
fieldElementValidLength,
fieldElementValidLength,
proofBadLength,
),
).toThrowError("Expected proofBytes to be 48 bytes"); ).toThrowError("Expected proofBytes to be 48 bytes");
}); });
}); });
describe("edge cases for verifyBlobKzgProof", () => { describe("edge cases for verifyBlobKzgProof", () => {
it("correct blob/commitment/proof should verify as true", () => { it("correct blob/commitment/proof should verify as true", () => {
let blob = generateRandomBlob(); const blob = generateRandomBlob();
let commitment = blobToKzgCommitment(blob); const commitment = blobToKzgCommitment(blob);
let proof = computeBlobKzgProof(blob, commitment); const proof = computeBlobKzgProof(blob, commitment);
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(true); expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(true);
}); });
it("incorrect commitment should verify as false", () => { it("incorrect commitment should verify as false", () => {
let blob = generateRandomBlob(); const blob = generateRandomBlob();
let commitment = blobToKzgCommitment(generateRandomBlob()); const commitment = blobToKzgCommitment(generateRandomBlob());
let proof = computeBlobKzgProof(blob, commitment); const proof = computeBlobKzgProof(blob, commitment);
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false); expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
}); });
it("incorrect proof should verify as false", () => { it("incorrect proof should verify as false", () => {
let blob = generateRandomBlob(); const blob = generateRandomBlob();
let commitment = blobToKzgCommitment(blob); const commitment = blobToKzgCommitment(blob);
let randomBlob = generateRandomBlob(); const randomBlob = generateRandomBlob();
let randomCommitment = blobToKzgCommitment(randomBlob); const randomCommitment = blobToKzgCommitment(randomBlob);
let proof = computeBlobKzgProof(randomBlob, randomCommitment); const proof = computeBlobKzgProof(randomBlob, randomCommitment);
expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false); expect(verifyBlobKzgProof(blob, commitment, proof)).toBe(false);
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
expect(() => expect(() => verifyBlobKzgProof(blobBadLength, commitmentValidLength, proofValidLength)).toThrowError(
verifyBlobKzgProof( "Expected blob to be 131072 bytes"
blobBadLength, );
commitmentValidLength, expect(() => verifyBlobKzgProof(blobValidLength, commitmentBadLength, proofValidLength)).toThrowError(
proofValidLength, "Expected commitmentBytes to be 48 bytes"
), );
).toThrowError("Expected blob to be 131072 bytes"); expect(() => verifyBlobKzgProof(blobValidLength, commitmentValidLength, proofBadLength)).toThrowError(
expect(() => "Expected proofBytes to be 48 bytes"
verifyBlobKzgProof( );
blobValidLength,
commitmentBadLength,
proofValidLength,
),
).toThrowError("Expected commitmentBytes to be 48 bytes");
expect(() =>
verifyBlobKzgProof(
blobValidLength,
commitmentValidLength,
proofBadLength,
),
).toThrowError("Expected proofBytes to be 48 bytes");
}); });
}); });
@ -411,8 +338,8 @@ describe("C-KZG", () => {
verifyBlobKzgProofBatch( verifyBlobKzgProofBatch(
2 as unknown as Uint8Array[], 2 as unknown as Uint8Array[],
[commitmentValidLength, commitmentValidLength], [commitmentValidLength, commitmentValidLength],
[proofValidLength, proofValidLength], [proofValidLength, proofValidLength]
), )
).toThrowError("Blobs, commitments, and proofs must all be arrays"); ).toThrowError("Blobs, commitments, and proofs must all be arrays");
}); });
it("should reject non-bytearray blob", () => { it("should reject non-bytearray blob", () => {
@ -420,8 +347,8 @@ describe("C-KZG", () => {
verifyBlobKzgProofBatch( verifyBlobKzgProofBatch(
["foo", "bar"] as unknown as Uint8Array[], ["foo", "bar"] as unknown as Uint8Array[],
[commitmentValidLength, commitmentValidLength], [commitmentValidLength, commitmentValidLength],
[proofValidLength, proofValidLength], [proofValidLength, proofValidLength]
), )
).toThrowError("Expected blob to be a Uint8Array"); ).toThrowError("Expected blob to be a Uint8Array");
}); });
it("throws as expected when given an argument of invalid length", () => { it("throws as expected when given an argument of invalid length", () => {
@ -429,22 +356,22 @@ describe("C-KZG", () => {
verifyBlobKzgProofBatch( verifyBlobKzgProofBatch(
[blobBadLength, blobValidLength], [blobBadLength, blobValidLength],
[commitmentValidLength, commitmentValidLength], [commitmentValidLength, commitmentValidLength],
[proofValidLength, proofValidLength], [proofValidLength, proofValidLength]
), )
).toThrowError("Expected blob to be 131072 bytes"); ).toThrowError("Expected blob to be 131072 bytes");
expect(() => expect(() =>
verifyBlobKzgProofBatch( verifyBlobKzgProofBatch(
[blobValidLength, blobValidLength], [blobValidLength, blobValidLength],
[commitmentBadLength, commitmentValidLength], [commitmentBadLength, commitmentValidLength],
[proofValidLength, proofValidLength], [proofValidLength, proofValidLength]
), )
).toThrowError("Expected commitmentBytes to be 48 bytes"); ).toThrowError("Expected commitmentBytes to be 48 bytes");
expect(() => expect(() =>
verifyBlobKzgProofBatch( verifyBlobKzgProofBatch(
[blobValidLength, blobValidLength], [blobValidLength, blobValidLength],
[commitmentValidLength, commitmentValidLength], [commitmentValidLength, commitmentValidLength],
[proofValidLength, proofBadLength], [proofValidLength, proofBadLength]
), )
).toThrowError("Expected proofBytes to be 48 bytes"); ).toThrowError("Expected proofBytes to be 48 bytes");
}); });
@ -453,27 +380,27 @@ describe("C-KZG", () => {
}); });
it("mismatching blobs/commitments/proofs should throw error", () => { it("mismatching blobs/commitments/proofs should throw error", () => {
let count = 3; const count = 3;
let blobs = new Array(count); const blobs = new Array(count);
let commitments = new Array(count); const commitments = new Array(count);
let proofs = new Array(count); const proofs = new Array(count);
for (let [i] of blobs.entries()) { for (const [i] of blobs.entries()) {
blobs[i] = generateRandomBlob(); blobs[i] = generateRandomBlob();
commitments[i] = blobToKzgCommitment(blobs[i]); commitments[i] = blobToKzgCommitment(blobs[i]);
proofs[i] = computeBlobKzgProof(blobs[i], commitments[i]); proofs[i] = computeBlobKzgProof(blobs[i], commitments[i]);
} }
expect(verifyBlobKzgProofBatch(blobs, commitments, proofs)).toBe(true); expect(verifyBlobKzgProofBatch(blobs, commitments, proofs)).toBe(true);
expect(() => expect(() => verifyBlobKzgProofBatch(blobs.slice(0, 1), commitments, proofs)).toThrowError(
verifyBlobKzgProofBatch(blobs.slice(0, 1), commitments, proofs), "Requires equal number of blobs/commitments/proofs"
).toThrowError("Requires equal number of blobs/commitments/proofs"); );
expect(() => expect(() => verifyBlobKzgProofBatch(blobs, commitments.slice(0, 1), proofs)).toThrowError(
verifyBlobKzgProofBatch(blobs, commitments.slice(0, 1), proofs), "Requires equal number of blobs/commitments/proofs"
).toThrowError("Requires equal number of blobs/commitments/proofs"); );
expect(() => expect(() => verifyBlobKzgProofBatch(blobs, commitments, proofs.slice(0, 1))).toThrowError(
verifyBlobKzgProofBatch(blobs, commitments, proofs.slice(0, 1)), "Requires equal number of blobs/commitments/proofs"
).toThrowError("Requires equal number of blobs/commitments/proofs"); );
}); });
}); });
}); });

File diff suppressed because it is too large Load Diff