From 8683fac75a582ca4217dfd03d5fdde18fb052f8b Mon Sep 17 00:00:00 2001 From: dancoffman Date: Fri, 4 Nov 2022 16:56:16 -0700 Subject: [PATCH 01/16] Basic setup instructions in README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 1f2be84..ec0668e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # C-KZG-4844: A minimal library for EIP-4844 Polynomial Commitments This is a copy of C-KZG stripped down to support the [Polynomial Commitments](https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md) API: + - `compute_aggregate_kzg_proof` - `verify_aggregate_kzg_proof` - `blob_to_kzg_commitment` @@ -9,3 +10,25 @@ This is a copy of C-KZG stripped down to support the [Polynomial Commitments](ht We also provide `load_trusted_setup` and `free_trusted_setup` to load the trusted setup data from a file into an object that can be passed to the API functions, and functions for converting commitments/proofs/points to/from bytes. + +## Installation + +Install the blst submodule + +``` +git submodule update --init +``` + +Build blst + +``` +cd src +make blst +``` + +Build the C-KZG code + +``` +cd src +make +``` From 1d1254e3803689dc168095d6870a4022838b0ab5 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Fri, 4 Nov 2022 17:00:26 -0700 Subject: [PATCH 02/16] gitignore dist --- bindings/node.js/dist/dts/kzg.d.ts | 11 -------- bindings/node.js/dist/dts/test.d.ts | 1 - bindings/node.js/dist/kzg.js | 44 ----------------------------- 3 files changed, 56 deletions(-) delete mode 100644 bindings/node.js/dist/dts/kzg.d.ts delete mode 100644 bindings/node.js/dist/dts/test.d.ts delete mode 100644 bindings/node.js/dist/kzg.js diff --git a/bindings/node.js/dist/dts/kzg.d.ts b/bindings/node.js/dist/dts/kzg.d.ts deleted file mode 100644 index c542a1f..0000000 --- a/bindings/node.js/dist/dts/kzg.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -export declare type BLSFieldElement = Uint8Array; -export declare type KZGProof = Uint8Array; -export declare type KZGCommitment = Uint8Array; -export declare type Blob = Uint8Array; -export declare const FIELD_ELEMENTS_PER_BLOB: number; -export declare const BYTES_PER_FIELD_ELEMENT: number; -export declare function loadTrustedSetup(filePath: string): void; -export declare function freeTrustedSetup(): void; -export declare function blobToKzgCommitment(blob: Blob): KZGCommitment; -export declare function computeAggregateKzgProof(blobs: Blob[]): KZGProof; -export declare function verifyAggregateKzgProof(blobs: Blob[], expectedKzgCommitments: KZGCommitment[], kzgAggregatedProof: KZGProof): boolean; diff --git a/bindings/node.js/dist/dts/test.d.ts b/bindings/node.js/dist/dts/test.d.ts deleted file mode 100644 index cb0ff5c..0000000 --- a/bindings/node.js/dist/dts/test.d.ts +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/bindings/node.js/dist/kzg.js b/bindings/node.js/dist/kzg.js deleted file mode 100644 index d4aeac5..0000000 --- a/bindings/node.js/dist/kzg.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -/** - * The public interface of this module exposes the functions as specified by - * https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#kzg - */ -const kzg = require("./kzg.node"); -const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB; -const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT; -// Stored as internal state -let setupHandle; -function requireSetupHandle() { - if (!setupHandle) { - throw new Error("You must call loadTrustedSetup to initialize KZG."); - } - return setupHandle; -} -function loadTrustedSetup(filePath) { - if (setupHandle) { - throw new Error("Call freeTrustedSetup before loading a new trusted setup."); - } - setupHandle = kzg.loadTrustedSetup(filePath); -} -function freeTrustedSetup() { - kzg.freeTrustedSetup(requireSetupHandle()); - setupHandle = undefined; -} -function blobToKzgCommitment(blob) { - return kzg.blobToKzgCommitment(blob, requireSetupHandle()); -} -function computeAggregateKzgProof(blobs) { - return kzg.computeAggregateKzgProof(blobs, requireSetupHandle()); -} -function verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof) { - return kzg.verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof, requireSetupHandle()); -} - -exports.BYTES_PER_FIELD_ELEMENT = BYTES_PER_FIELD_ELEMENT; -exports.FIELD_ELEMENTS_PER_BLOB = FIELD_ELEMENTS_PER_BLOB; -exports.blobToKzgCommitment = blobToKzgCommitment; -exports.computeAggregateKzgProof = computeAggregateKzgProof; -exports.freeTrustedSetup = freeTrustedSetup; -exports.loadTrustedSetup = loadTrustedSetup; -exports.verifyAggregateKzgProof = verifyAggregateKzgProof; From c033f782b84167d6fe1a4280690f8929a009c0ed Mon Sep 17 00:00:00 2001 From: dancoffman Date: Fri, 4 Nov 2022 18:47:30 -0700 Subject: [PATCH 03/16] Start working on NPM publish --- bindings/node.js/.npmignore | 16 ++++++++++++++++ bindings/node.js/Makefile | 11 +++++++++-- bindings/node.js/package.json | 2 +- bindings/node.js/rollup.config.js | 6 +++++- 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 bindings/node.js/.npmignore diff --git a/bindings/node.js/.npmignore b/bindings/node.js/.npmignore new file mode 100644 index 0000000..f1513e1 --- /dev/null +++ b/bindings/node.js/.npmignore @@ -0,0 +1,16 @@ +.vscode +build +node_modules +.gitignore +.npmignore +.prettierignore +.prettierrc.json +*.o +*.node +test.ts +kzg.ts +jest.config.js +rollup.config.js +tsconfig.json +babel.config.js +dist/blst/bindings diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index 9aae264..8280cb6 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -3,8 +3,8 @@ all: clean build format test bundle clean: yarn node-gyp clean rm -rf build + rm -rf dist rm -f *.node - rm -f dist/kzg.node rm -f *.a rm -f *.o @@ -18,5 +18,12 @@ test: build format: yarn prettier --write . -bundle: +bundle: clean yarn rollup --config rollup.config.js --bundleConfigAsCjs + mkdir -p dist/deps/c-kzg + cp -r ../../blst dist/deps + cp ../../src/c_kzg_4844.c dist/deps/c-kzg + cp ../../src/c_kzg_4844.h dist/deps/c-kzg + +publish: + npm publish diff --git a/bindings/node.js/package.json b/bindings/node.js/package.json index 5c15850..7103aa9 100644 --- a/bindings/node.js/package.json +++ b/bindings/node.js/package.json @@ -1,6 +1,6 @@ { "name": "c-kzg", - "version": "0.0.1", + "version": "0.0.2", "description": "NodeJS bindings for C-KZG", "author": "Dan Coffman", "license": "MIT", diff --git a/bindings/node.js/rollup.config.js b/bindings/node.js/rollup.config.js index 383c4bc..5d0528d 100644 --- a/bindings/node.js/rollup.config.js +++ b/bindings/node.js/rollup.config.js @@ -6,5 +6,9 @@ export default { dir: "dist", format: "cjs", }, - plugins: [typescript()], + plugins: [ + typescript({ + exclude: ["test.ts"], + }), + ], }; From d09b1009b6f73a4d564d22ea42edafdea86bfca7 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Sat, 5 Nov 2022 00:28:36 -0700 Subject: [PATCH 04/16] gyp that maybe works on yarn install --- bindings/node.js/.gitignore | 1 + bindings/node.js/.npmignore | 2 +- bindings/node.js/Makefile | 4 +++ bindings/node.js/binding.dist.gyp | 60 +++++++++++++++++++++++++++++++ bindings/node.js/binding.gyp | 4 --- 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 bindings/node.js/binding.dist.gyp diff --git a/bindings/node.js/.gitignore b/bindings/node.js/.gitignore index 6a76a2b..38f45ff 100644 --- a/bindings/node.js/.gitignore +++ b/bindings/node.js/.gitignore @@ -1,3 +1,4 @@ build +dist *.node node_modules diff --git a/bindings/node.js/.npmignore b/bindings/node.js/.npmignore index f1513e1..2d92dbf 100644 --- a/bindings/node.js/.npmignore +++ b/bindings/node.js/.npmignore @@ -13,4 +13,4 @@ jest.config.js rollup.config.js tsconfig.json babel.config.js -dist/blst/bindings +*.bak diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index 8280cb6..0dc2dc7 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -26,4 +26,8 @@ bundle: clean cp ../../src/c_kzg_4844.h dist/deps/c-kzg publish: + mv binding.gyp binding.gyp.bak + mv binding.dist.gyp binding.gyp npm publish + mv binding.gyp binding.dist.gyp + mv binding.gyp.bak binding.gyp diff --git a/bindings/node.js/binding.dist.gyp b/bindings/node.js/binding.dist.gyp new file mode 100644 index 0000000..9746356 --- /dev/null +++ b/bindings/node.js/binding.dist.gyp @@ -0,0 +1,60 @@ +{ + "targets": [ + { + "target_name": "kzg", + "cflags!": ["-fno-exceptions"], + "cflags_cc!": ["-fno-exceptions"], + "xcode_settings": { + "GCC_ENABLE_CPP_EXCEPTIONS": "YES", + "CLANG_CXX_LIBRARY": "libc++", + "MACOSX_DEPLOYMENT_TARGET": "13.0" + }, + "sources": ["kzg.cxx"], + "include_dirs": [ + "../../inc", + "../../src", + " Date: Sat, 5 Nov 2022 00:36:38 -0700 Subject: [PATCH 05/16] Required to publish blst/build --- bindings/node.js/.npmignore | 1 + bindings/node.js/Makefile | 2 +- bindings/node.js/package.json | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bindings/node.js/.npmignore b/bindings/node.js/.npmignore index 2d92dbf..de0f059 100644 --- a/bindings/node.js/.npmignore +++ b/bindings/node.js/.npmignore @@ -1,5 +1,6 @@ .vscode build +!dist/deps/blst/build node_modules .gitignore .npmignore diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index 0dc2dc7..da40fbe 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -25,7 +25,7 @@ bundle: clean cp ../../src/c_kzg_4844.c dist/deps/c-kzg cp ../../src/c_kzg_4844.h dist/deps/c-kzg -publish: +publish: bundle mv binding.gyp binding.gyp.bak mv binding.dist.gyp binding.gyp npm publish diff --git a/bindings/node.js/package.json b/bindings/node.js/package.json index 7103aa9..dc9c20e 100644 --- a/bindings/node.js/package.json +++ b/bindings/node.js/package.json @@ -1,6 +1,6 @@ { "name": "c-kzg", - "version": "0.0.2", + "version": "0.0.4", "description": "NodeJS bindings for C-KZG", "author": "Dan Coffman", "license": "MIT", From f3069d1ec9848fe6bdc9eaa89daeed9e3818d4aa Mon Sep 17 00:00:00 2001 From: dancoffman Date: Sat, 5 Nov 2022 00:46:26 -0700 Subject: [PATCH 06/16] Messy publish but it works --- bindings/node.js/.prettierrc.json | 2 +- bindings/node.js/binding.dist.gyp | 4 ++-- bindings/node.js/package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bindings/node.js/.prettierrc.json b/bindings/node.js/.prettierrc.json index 6b2adca..3ec457f 100644 --- a/bindings/node.js/.prettierrc.json +++ b/bindings/node.js/.prettierrc.json @@ -2,7 +2,7 @@ "trailingComma": "all", "overrides": [ { - "files": "binding.gyp", + "files": "*.gyp", "options": { "parser": "json" } } ] diff --git a/bindings/node.js/binding.dist.gyp b/bindings/node.js/binding.dist.gyp index 9746356..f1a7267 100644 --- a/bindings/node.js/binding.dist.gyp +++ b/bindings/node.js/binding.dist.gyp @@ -11,8 +11,8 @@ }, "sources": ["kzg.cxx"], "include_dirs": [ - "../../inc", - "../../src", + "<(module_root_dir)/dist/deps/blst/bindings", + "<(module_root_dir)/dist/deps/c-kzg", " Date: Mon, 7 Nov 2022 11:45:36 -0800 Subject: [PATCH 07/16] cc works on Linux --- bindings/node.js/binding.dist.gyp | 2 +- bindings/node.js/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/node.js/binding.dist.gyp b/bindings/node.js/binding.dist.gyp index f1a7267..82e2a2b 100644 --- a/bindings/node.js/binding.dist.gyp +++ b/bindings/node.js/binding.dist.gyp @@ -36,7 +36,7 @@ ], "outputs": ["<(module_root_dir)/c_kzg_4844.o"], "action": [ - "clang", + "cc", "-I<(module_root_dir)/dist/deps/blst/bindings", "-O2", "-c", diff --git a/bindings/node.js/package.json b/bindings/node.js/package.json index df1cedf..1d16a00 100644 --- a/bindings/node.js/package.json +++ b/bindings/node.js/package.json @@ -1,6 +1,6 @@ { "name": "c-kzg", - "version": "0.0.6", + "version": "0.0.7", "description": "NodeJS bindings for C-KZG", "author": "Dan Coffman", "license": "MIT", From 26f5178d06b4f80e5fc7be27d3e5e8dbb2b41e06 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Mon, 7 Nov 2022 14:35:10 -0800 Subject: [PATCH 08/16] git ignore xcode autogenerated file --- .gitignore | 1 + bindings/node.js/Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4523091..370b496 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ inc/blst_aux.h* *bindings/csharp/*.exe *bindings/csharp/*.dll __pycache__ +.DS_Store diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index da40fbe..dafc576 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -27,7 +27,7 @@ bundle: clean publish: bundle mv binding.gyp binding.gyp.bak - mv binding.dist.gyp binding.gyp + cp binding.dist.gyp binding.gyp npm publish mv binding.gyp binding.dist.gyp mv binding.gyp.bak binding.gyp From e98ffa8c8e5b9dce8a48453257dfc85940aaa15f Mon Sep 17 00:00:00 2001 From: dancoffman Date: Mon, 7 Nov 2022 14:37:46 -0800 Subject: [PATCH 09/16] Make publish less able to leave files in bad state --- bindings/node.js/Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index dafc576..c5b56f7 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -28,6 +28,4 @@ bundle: clean publish: bundle mv binding.gyp binding.gyp.bak cp binding.dist.gyp binding.gyp - npm publish - mv binding.gyp binding.dist.gyp - mv binding.gyp.bak binding.gyp + npm publish && mv binding.gyp.bak binding.gyp || mv binding.gyp.bak binding.gyp From fec7acb87ef98da1701ba118c71e563a643ab615 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Tue, 8 Nov 2022 20:06:57 +0000 Subject: [PATCH 10/16] Handle n < 2 cases better in compute_aggregated_poly_and_commitment --- src/c_kzg_4844.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/c_kzg_4844.c b/src/c_kzg_4844.c index 89dcc42..385d894 100644 --- a/src/c_kzg_4844.c +++ b/src/c_kzg_4844.c @@ -1109,14 +1109,25 @@ static C_KZG_RET compute_aggregated_poly_and_commitment(Polynomial poly_out, KZG const Polynomial polys[], const KZGCommitment kzg_commitments[], size_t n) { - BLSFieldElement* r_powers = calloc(n, sizeof(BLSFieldElement)); - if (r_powers == NULL) return C_KZG_MALLOC; + if (n == 0) return C_KZG_BADARGS; C_KZG_RET ret; uint8_t* hash = calloc(32, sizeof(uint8_t)); - if (hash == NULL) { free(r_powers); return C_KZG_MALLOC; } + if (hash == NULL) return C_KZG_MALLOC; ret = hash_to_bytes(hash, polys, kzg_commitments, n); - if (ret != C_KZG_OK) { free(r_powers); free(hash); return ret; } + if (ret != C_KZG_OK) { free(hash); return ret; } + + if (n == 1) { + bytes_to_bls_field(chal_out, hash); + poly_lincomb(poly_out, polys, chal_out, n); + g1_lincomb(comm_out, kzg_commitments, chal_out, n); + free(hash); + return C_KZG_OK; + } + + BLSFieldElement* r_powers = calloc(n, sizeof(BLSFieldElement)); + if (r_powers == NULL) { free(hash); return C_KZG_MALLOC; } + bytes_to_bls_field(&r_powers[1], hash); free(hash); From 72f696e1196511331c8d705eaf11c2937b88ac50 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Mon, 7 Nov 2022 17:08:21 -0800 Subject: [PATCH 11/16] File order matters to the linker. GCC demands this order. --- bindings/node.js/binding.dist.gyp | 4 ++-- bindings/node.js/binding.gyp | 4 ++-- bindings/node.js/package.json | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/node.js/binding.dist.gyp b/bindings/node.js/binding.dist.gyp index 82e2a2b..2fb9786 100644 --- a/bindings/node.js/binding.dist.gyp +++ b/bindings/node.js/binding.dist.gyp @@ -16,8 +16,8 @@ " Date: Tue, 8 Nov 2022 08:38:07 -0800 Subject: [PATCH 12/16] Add test harness for building and running NodeJS tests on Linux --- bindings/node.js/.npmignore | 1 + bindings/node.js/Dockerfile | 20 ++++++++++++++++++++ bindings/node.js/Makefile | 7 +++++++ bindings/node.js/binding.dist.gyp | 4 ++++ bindings/node.js/kzg.cxx | 1 + bindings/node.js/package.json | 4 +++- bindings/node.js/test.ts | 30 +++++++++++++++++++++++++----- 7 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 bindings/node.js/Dockerfile diff --git a/bindings/node.js/.npmignore b/bindings/node.js/.npmignore index de0f059..f18e799 100644 --- a/bindings/node.js/.npmignore +++ b/bindings/node.js/.npmignore @@ -15,3 +15,4 @@ rollup.config.js tsconfig.json babel.config.js *.bak +Dockerfile diff --git a/bindings/node.js/Dockerfile b/bindings/node.js/Dockerfile new file mode 100644 index 0000000..7c05cdf --- /dev/null +++ b/bindings/node.js/Dockerfile @@ -0,0 +1,20 @@ +# Exists as a test harness for building and running tests in Linux + +FROM node:16 + +COPY ./dist/ /app/dist/ +COPY test.ts /app +COPY trusted_setup.txt /app +COPY kzg.ts /app +COPY kzg.cxx /app +COPY package.json /app +COPY tsconfig.json /app +COPY babel.config.js /app +COPY jest.config.js /app +COPY binding.dist.gyp /app/binding.gyp + +WORKDIR /app + +RUN yarn install + +CMD ["yarn", "jest"] diff --git a/bindings/node.js/Makefile b/bindings/node.js/Makefile index c5b56f7..b87ce55 100644 --- a/bindings/node.js/Makefile +++ b/bindings/node.js/Makefile @@ -29,3 +29,10 @@ publish: bundle mv binding.gyp binding.gyp.bak cp binding.dist.gyp binding.gyp npm publish && mv binding.gyp.bak binding.gyp || mv binding.gyp.bak binding.gyp + + +linux-test: bundle + cp ../../src/trusted_setup.txt . + docker build -t "linux-test" . + rm trusted_setup.txt + docker logs --follow `docker run -d linux-test` diff --git a/bindings/node.js/binding.dist.gyp b/bindings/node.js/binding.dist.gyp index 2fb9786..89851df 100644 --- a/bindings/node.js/binding.dist.gyp +++ b/bindings/node.js/binding.dist.gyp @@ -53,6 +53,10 @@ { "files": ["./build/Release/kzg.node"], "destination": "./dist" + }, + { + "files": ["./build/Release/kzg.node"], + "destination": "./" } ] } diff --git a/bindings/node.js/kzg.cxx b/bindings/node.js/kzg.cxx index e98dc82..f8e510e 100644 --- a/bindings/node.js/kzg.cxx +++ b/bindings/node.js/kzg.cxx @@ -155,6 +155,7 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) { auto kzg_settings = info[1].As>().Data(); auto blobs_count = blobs_param.Length(); + auto blobs = (Blob*)calloc(blobs_count, sizeof(Blob)); for (uint32_t blob_index = 0; blob_index < blobs_count; blob_index++) { diff --git a/bindings/node.js/package.json b/bindings/node.js/package.json index 9d96ec2..cbf43dd 100644 --- a/bindings/node.js/package.json +++ b/bindings/node.js/package.json @@ -12,12 +12,14 @@ "@rollup/plugin-typescript": "^9.0.2", "@types/jest": "^29.2.1", "jest": "^29.2.2", - "node-addon-api": "^5.0.0", "node-gyp": "^9.3.0", "prettier": "2.7.1", "rollup": "^3.2.5", "ts-jest": "^29.0.3", "tslib": "^2.4.1", "typescript": "^4.8.4" + }, + "dependencies": { + "node-addon-api": "^5.0.0" } } diff --git a/bindings/node.js/test.ts b/bindings/node.js/test.ts index 0b61441..89362eb 100644 --- a/bindings/node.js/test.ts +++ b/bindings/node.js/test.ts @@ -1,4 +1,6 @@ import { randomBytes } from "crypto"; +import { existsSync } from "fs"; + import { loadTrustedSetup, freeTrustedSetup, @@ -9,7 +11,12 @@ import { FIELD_ELEMENTS_PER_BLOB, } from "./kzg"; -const SETUP_FILE_PATH = "../../src/trusted_setup.txt"; +const setupFileName = "trusted_setup.txt"; + +const SETUP_FILE_PATH = existsSync(setupFileName) + ? setupFileName + : `../../src/${setupFileName}`; + const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT; const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT)); @@ -23,13 +30,26 @@ describe("C-KZG", () => { freeTrustedSetup(); }); - it("computes the correct commitments and aggregate proofs from blobs", () => { - const blobs = new Array(2).fill(0).map(generateRandomBlob); - const commitments = blobs.map(blobToKzgCommitment); - const proof = computeAggregateKzgProof(blobs); + it("computes the correct commitments and aggregate proof from blobs", () => { + let blobs = new Array(2).fill(0).map(generateRandomBlob); + let commitments = blobs.map(blobToKzgCommitment); + let proof = computeAggregateKzgProof(blobs); expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true); }); + it("computes the aggregate proof when blobs is an empty array", () => { + const proof = computeAggregateKzgProof([]); + + // Is this actually what the aggregate proof for an empty array should be? + expect(proof.toString()).toEqual( + [ + 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, + ].toString(), + ); + }); + it("fails when given incorrect commitments", () => { const blobs = new Array(2).fill(0).map(generateRandomBlob); const commitments = blobs.map(blobToKzgCommitment); From 6c50c402476ff3a4c69bad41764b1f762df887c3 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Tue, 8 Nov 2022 12:53:36 -0800 Subject: [PATCH 13/16] Update tests to cover blobs arrays of length zero and one --- bindings/node.js/test.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bindings/node.js/test.ts b/bindings/node.js/test.ts index 89362eb..da4ecc3 100644 --- a/bindings/node.js/test.ts +++ b/bindings/node.js/test.ts @@ -37,19 +37,19 @@ describe("C-KZG", () => { expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true); }); - it("computes the aggregate proof when blobs is an empty array", () => { - const proof = computeAggregateKzgProof([]); - - // Is this actually what the aggregate proof for an empty array should be? - expect(proof.toString()).toEqual( - [ - 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, - ].toString(), + it("throws an error when blobs is an empty array", () => { + expect(() => computeAggregateKzgProof([])).toThrowError( + "Failed to compute proof", ); }); + it("computes the aggregate proof when for a single blob", () => { + let blobs = new Array(1).fill(0).map(generateRandomBlob); + let commitments = blobs.map(blobToKzgCommitment); + let proof = computeAggregateKzgProof(blobs); + expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true); + }); + it("fails when given incorrect commitments", () => { const blobs = new Array(2).fill(0).map(generateRandomBlob); const commitments = blobs.map(blobToKzgCommitment); From ef3f77be0620c66993ea02c05733135c366f5798 Mon Sep 17 00:00:00 2001 From: dancoffman Date: Tue, 8 Nov 2022 12:57:15 -0800 Subject: [PATCH 14/16] Ignore a couple more files that do not need to be published --- bindings/node.js/.npmignore | 2 ++ bindings/node.js/package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/node.js/.npmignore b/bindings/node.js/.npmignore index f18e799..415dcf4 100644 --- a/bindings/node.js/.npmignore +++ b/bindings/node.js/.npmignore @@ -16,3 +16,5 @@ tsconfig.json babel.config.js *.bak Dockerfile +binding.dist.gyp +Makefile diff --git a/bindings/node.js/package.json b/bindings/node.js/package.json index cbf43dd..488d4a5 100644 --- a/bindings/node.js/package.json +++ b/bindings/node.js/package.json @@ -1,6 +1,6 @@ { "name": "c-kzg", - "version": "0.0.8", + "version": "0.0.9", "description": "NodeJS bindings for C-KZG", "author": "Dan Coffman", "license": "MIT", From c8fab42ac0b31d361e4f38a0936991145d518f23 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Tue, 8 Nov 2022 22:02:38 +0000 Subject: [PATCH 15/16] Fix node.js build - thanks to dgcoffman --- bindings/node.js/binding.gyp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/node.js/binding.gyp b/bindings/node.js/binding.gyp index 7d9a807..83ed8b7 100644 --- a/bindings/node.js/binding.gyp +++ b/bindings/node.js/binding.gyp @@ -16,8 +16,8 @@ " Date: Tue, 8 Nov 2022 23:11:19 +0000 Subject: [PATCH 16/16] Add n=0 special case for compute_aggregate_kzg_proof --- src/c_kzg_4844.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/c_kzg_4844.c b/src/c_kzg_4844.c index 385d894..a55e153 100644 --- a/src/c_kzg_4844.c +++ b/src/c_kzg_4844.c @@ -1146,6 +1146,11 @@ C_KZG_RET compute_aggregate_kzg_proof(KZGProof *out, const Blob blobs[], size_t n, const KZGSettings *s) { + if (n == 0) { + *out = g1_identity; + return C_KZG_OK; + } + KZGCommitment* commitments = calloc(n, sizeof(KZGCommitment)); if (commitments == NULL) return C_KZG_MALLOC;