Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeJS bindings #3

Merged
merged 46 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a581964
Working node-gyp
dgcoffman Nov 1, 2022
6ec86af
Generate bindings using SWIG and node-gyp
dgcoffman Nov 1, 2022
354281a
Checkpoint
dgcoffman Nov 2, 2022
2846d0b
checkpoint 2
dgcoffman Nov 2, 2022
2030336
I learned about extern C
dgcoffman Nov 2, 2022
002fc3f
Tweaks
dgcoffman Nov 2, 2022
fdcab9d
Install node-gyp with yarn
dgcoffman Nov 2, 2022
40db00f
Add napi version
dgcoffman Nov 2, 2022
b169d4a
Remove SWIG
dgcoffman Nov 2, 2022
d478234
Revert changes that were only to make SWIG work
dgcoffman Nov 2, 2022
726fd38
Revert unnecessary platform-specific makefile change
dgcoffman Nov 2, 2022
ac17623
TypeScript test
dgcoffman Nov 2, 2022
da8d5dd
Try to test VerifyKzgProof (does not work)
dgcoffman Nov 2, 2022
dbf2a1d
Plausibly working BlobToKzgCommitment
dgcoffman Nov 3, 2022
fcd7fbd
Checkpoint
dgcoffman Nov 3, 2022
a5ca064
Some cleanup
dgcoffman Nov 3, 2022
672346f
Closer to working
dgcoffman Nov 3, 2022
b8151db
Cleanup
dgcoffman Nov 3, 2022
1514d5b
computeAggregateKzgProof + verifyAggregateKzgProof test passes
dgcoffman Nov 3, 2022
4bfce9a
Test verifyKzgProof
dgcoffman Nov 3, 2022
3201111
Actually return the result of verify_kzg_proof
dgcoffman Nov 3, 2022
ab04cdd
Add a test for mismatch
dgcoffman Nov 4, 2022
05fd880
Run all the tests
dgcoffman Nov 4, 2022
0a99015
Attempt more consistency. Remove test for verifyKzgProof which is not…
dgcoffman Nov 4, 2022
83bed36
Add return types
dgcoffman Nov 4, 2022
bbc90b7
Use rollup to product distributable JS file
dgcoffman Nov 4, 2022
7668c4f
Generate type defs
dgcoffman Nov 4, 2022
b031251
Do not run prettier on dist
dgcoffman Nov 4, 2022
b661552
Always make, never package.json scripts
dgcoffman Nov 4, 2022
c3d11f9
build is a dep of test
dgcoffman Nov 4, 2022
3f66b35
Fix type
dgcoffman Nov 4, 2022
92242c7
Have gyp copy the .node file
dgcoffman Nov 4, 2022
e90e485
Add blst as submodule
dgcoffman Nov 4, 2022
a29b158
Make task for blst
dgcoffman Nov 4, 2022
09d8405
nodejs binding make build should do less
dgcoffman Nov 4, 2022
be0f51b
make blst now works
dgcoffman Nov 4, 2022
46fb7cc
Update README
dgcoffman Nov 4, 2022
f819fe1
Patch blst submodule sha
dgcoffman Nov 4, 2022
82c89e1
Add gypfile to package.json
dgcoffman Nov 4, 2022
8ca4fd9
Merge branch '4844_3038' into dgcoffman/nodejs-bindings
dgcoffman Nov 4, 2022
ac65930
Use BYTES_PER_FIELD_ELEMENT
dgcoffman Nov 4, 2022
062e4e0
Simplify, given updated API
dgcoffman Nov 4, 2022
1d20797
Regen dist
dgcoffman Nov 4, 2022
84bd55b
Be better at TypeScript
dgcoffman Nov 4, 2022
d007bf7
Do not change the default behavior of src `make`
dgcoffman Nov 4, 2022
fa8eebb
free earlier when both subsequent branches will free. Throw appropria…
dgcoffman Nov 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
inc/blst.h*
inc/blst_aux.h*
.vscode/
*.json
.clang-format
*bindings/*/*.so
*bindings/csharp/*.exe
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "blst"]
path = blst
url = https://github.com/supranational/blst
ignore = dirty # because we apply a patch
3 changes: 3 additions & 0 deletions bindings/node.js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
*.node
node_modules
3 changes: 3 additions & 0 deletions bindings/node.js/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
dist
9 changes: 9 additions & 0 deletions bindings/node.js/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"trailingComma": "all",
"overrides": [
{
"files": "binding.gyp",
"options": { "parser": "json" }
}
]
}
22 changes: 22 additions & 0 deletions bindings/node.js/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all: clean build format test bundle

clean:
yarn node-gyp clean
rm -rf build
rm -f *.node
rm -f dist/kzg.node
rm -f *.a
rm -f *.o

build: kzg.cxx kzg.ts package.json binding.gyp Makefile
cd ../../src; make lib
yarn node-gyp rebuild

test: build
yarn jest

format:
yarn prettier --write .

bundle:
yarn rollup --config rollup.config.js --bundleConfigAsCjs
57 changes: 57 additions & 0 deletions bindings/node.js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
This directory contains the code necessary to generate NodeJS bindings for C-KZG.

```js

loadTrustedSetup: (filePath: string) => SetupHandle;

freeTrustedSetup: (setupHandle: SetupHandle) => void;

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

computeAggregateKzgProof: (
blobs: Blob[],
setupHandle: SetupHandle
) => KZGProof;

verifyAggregateKzgProof: (
blobs: Blob[],
expectedKzgCommitments: KZGCommitment[],
kzgAggregatedProof: KZGProof,
setupHandle: SetupHandle
) => boolean;
```

Spec: https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md

First,
`npm install -g yarn` if you don't have it.

Install the blst submodule

```sh
git submodule update --init
```

Build blst and c_kzg_4844.c

```
cd src && make blst lib
```

Generate NodeJS bindings and run the TypeScript tests against them

```sh
cd ../bindings/node.js && yarn install && make test
```

After doing this once, you can re-build (if necessary) and re-run the tests with

```sh
make build test
```

After making changes, regenerate the distributable JS and type defs

```sh
make bundle
```
6 changes: 6 additions & 0 deletions bindings/node.js/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
41 changes: 41 additions & 0 deletions bindings/node.js/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"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",
"<!@(node -p \"require('node-addon-api').include\")"
],
"libraries": [
"<(module_root_dir)/c_kzg_4844.o",
"<(module_root_dir)/../../lib/libblst.a"
],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
},
{
"target_name": "action_after_build",
"type": "none",
"dependencies": ["kzg"],
"copies": [
{
"files": ["./build/Release/kzg.node"],
"destination": "."
},
{
"files": ["./build/Release/kzg.node"],
"destination": "./dist"
}
]
}
]
}
11 changes: 11 additions & 0 deletions bindings/node.js/dist/dts/kzg.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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;
1 change: 1 addition & 0 deletions bindings/node.js/dist/dts/test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
44 changes: 44 additions & 0 deletions bindings/node.js/dist/kzg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'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;
5 changes: 5 additions & 0 deletions bindings/node.js/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
Loading