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);