Add napi version

This commit is contained in:
dancoffman 2022-11-02 13:21:41 -07:00
parent fdcab9d035
commit 40db00f643
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
10 changed files with 327 additions and 82 deletions

View File

@ -5,10 +5,10 @@ clean:
rm -f ckzg_wrap.cxx
build: ckzg.cxx ckzg.h Makefile
cd ../../src; make lib
swig -c++ -javascript -node ckzg.swg
# cd ../../src; make lib
# swig -c++ -javascript -node ckzg.swg
yarn build
cp build/Release/ckzg.node .
cp build/Release/kzg.node .
test:
env NODE_PATH=.: node runnable.js

View File

@ -1,21 +1,36 @@
{
'targets': [
{
'target_name': 'ckzg',
'target_name': 'kzg',
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
'sources': [
'ckzg.cxx',
'ckzg_wrap.cxx',
'kzg.cxx',
],
'include_dirs': ['../../inc', '../../src', "<!@(node -p \"require('node-addon-api').include\")"],
'libraries': [
'/Users/coffman@coinbase.com/src/c-kzg/bindings/node.js/c_kzg_4844.o',
'/Users/coffman@coinbase.com/src/c-kzg/lib/libblst.a'
],
# https://stackoverflow.com/questions/59799509/how-to-return-a-c-class-to-node-js
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}
},
# {
# 'target_name': 'ckzg-swig',
# "cflags!": [ "-fno-exceptions" ],
# "cflags_cc!": [ "-fno-exceptions" ],
# 'sources': [
# 'ckzg.cxx',
# # SWIG-generated wrapper around ckzg.cxx
# 'ckzg_wrap.cxx',
# ],
# 'include_dirs': ['../../inc', '../../src'],
# 'libraries': [
# '/Users/coffman@coinbase.com/src/c-kzg/bindings/node.js/c_kzg_4844.o',
# '/Users/coffman@coinbase.com/src/c-kzg/lib/libblst.a'
# ],
# }
]
}

View File

@ -2,7 +2,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "ckzg.h"
#include <napi.h>
int testFunction() {
return test_function();
@ -30,76 +29,76 @@ void freeTrustedSetup(KZGSettings *s) {
free(s);
}
void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s) {
Polynomial p;
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++)
bytes_to_bls_field(&p[i], &blob[i * 32]);
// void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s) {
// Polynomial p;
// for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++)
// bytes_to_bls_field(&p[i], &blob[i * 32]);
KZGCommitment c;
blob_to_kzg_commitment(&c, p, s);
// KZGCommitment c;
// blob_to_kzg_commitment(&c, p, s);
bytes_from_g1(out, &c);
}
// bytes_from_g1(out, &c);
// }
int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s) {
Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
if (p == NULL) return -1;
// int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s) {
// Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
// if (p == NULL) return -1;
KZGCommitment* c = (KZGCommitment*)calloc(n, sizeof(KZGCommitment));
if (c == NULL) { free(p); return -1; }
// KZGCommitment* c = (KZGCommitment*)calloc(n, sizeof(KZGCommitment));
// if (c == NULL) { free(p); return -1; }
C_KZG_RET ret;
// C_KZG_RET ret;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
ret = bytes_to_g1(&c[i], &commitments[i * 48]);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
}
// for (size_t i = 0; i < n; i++) {
// for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
// bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
// ret = bytes_to_g1(&c[i], &commitments[i * 48]);
// if (ret != C_KZG_OK) { free(c); free(p); return -1; }
// }
KZGProof f;
ret = bytes_to_g1(&f, proof);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
// KZGProof f;
// ret = bytes_to_g1(&f, proof);
// if (ret != C_KZG_OK) { free(c); free(p); return -1; }
bool b;
ret = verify_aggregate_kzg_proof(&b, p, c, n, &f, s);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
// bool b;
// ret = verify_aggregate_kzg_proof(&b, p, c, n, &f, s);
// if (ret != C_KZG_OK) { free(c); free(p); return -1; }
free(c); free(p);
return b ? 0 : 1;
}
// free(c); free(p);
// return b ? 0 : 1;
// }
C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s) {
Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
if (p == NULL) return C_KZG_ERROR;
// C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s) {
// Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
// if (p == NULL) return C_KZG_ERROR;
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
// for (size_t i = 0; i < n; i++)
// for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
// bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
KZGProof f;
C_KZG_RET ret = compute_aggregate_kzg_proof(&f, p, n, s);
// KZGProof f;
// C_KZG_RET ret = compute_aggregate_kzg_proof(&f, p, n, s);
free(p);
if (ret != C_KZG_OK) return ret;
// free(p);
// if (ret != C_KZG_OK) return ret;
bytes_from_g1(out, &f);
return C_KZG_OK;
}
// bytes_from_g1(out, &f);
// return C_KZG_OK;
// }
int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) {
KZGCommitment commitment;
KZGProof proof;
BLSFieldElement fx, fy;
bool out;
// int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) {
// KZGCommitment commitment;
// KZGProof proof;
// BLSFieldElement fx, fy;
// bool out;
bytes_to_bls_field(&fx, x);
bytes_to_bls_field(&fy, y);
if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1;
if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
// bytes_to_bls_field(&fx, x);
// bytes_to_bls_field(&fy, y);
// if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1;
// if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
if (verify_kzg_proof(&out, &commitment, &fx, &fy, &proof, s) != C_KZG_OK)
return -2;
// if (verify_kzg_proof(&out, &commitment, &fx, &fy, &proof, s) != C_KZG_OK)
// return -2;
return out ? 0 : 1;
}
// return out ? 0 : 1;
// }

View File

@ -1,6 +1,10 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
// #include <node.h>
// #include <node_version.h>
// #include <v8.h>
#include "c_kzg_4844.h"
int testFunction();
@ -9,10 +13,10 @@ KZGSettings* loadTrustedSetup(const char* file);
void freeTrustedSetup(KZGSettings *s);
void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s);
// void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s);
int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s);
// int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s);
int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s);
// int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s);
C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s);
// C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s);

198
bindings/node.js/kzg.cxx Normal file
View File

@ -0,0 +1,198 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define NAPI_EXPERIMENTAL
#include <napi.h>
#include "c_kzg_4844.h"
#include "blst.h"
KZGSettings* loadTrustedSetup(const char* file) {
KZGSettings* out = (KZGSettings*)malloc(sizeof(KZGSettings));
if (out == NULL) return NULL;
FILE* f = fopen(file, "r");
if (f == NULL) { free(out); return NULL; }
if (load_trusted_setup(out, f) != C_KZG_OK) { free(out); return NULL; }
return out;
}
void freeTrustedSetup(KZGSettings *s) {
free_trusted_setup(s);
free(s);
}
void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s) {
Polynomial p;
for (size_t i = 0; i < FIELD_ELEMENTS_PER_BLOB; i++)
bytes_to_bls_field(&p[i], &blob[i * 32]);
KZGCommitment c;
blob_to_kzg_commitment(&c, p, s);
bytes_from_g1(out, &c);
}
int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s) {
Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
if (p == NULL) return -1;
KZGCommitment* c = (KZGCommitment*)calloc(n, sizeof(KZGCommitment));
if (c == NULL) { free(p); return -1; }
C_KZG_RET ret;
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
ret = bytes_to_g1(&c[i], &commitments[i * 48]);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
}
KZGProof f;
ret = bytes_to_g1(&f, proof);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
bool b;
ret = verify_aggregate_kzg_proof(&b, p, c, n, &f, s);
if (ret != C_KZG_OK) { free(c); free(p); return -1; }
free(c); free(p);
return b ? 0 : 1;
}
C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s) {
Polynomial* p = (Polynomial*)calloc(n, sizeof(Polynomial));
if (p == NULL) return C_KZG_ERROR;
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < FIELD_ELEMENTS_PER_BLOB; j++)
bytes_to_bls_field(&p[i][j], &blobs[i * FIELD_ELEMENTS_PER_BLOB * 32 + j * 32]);
KZGProof f;
C_KZG_RET ret = compute_aggregate_kzg_proof(&f, p, n, s);
free(p);
if (ret != C_KZG_OK) return ret;
bytes_from_g1(out, &f);
return C_KZG_OK;
}
int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s) {
KZGCommitment commitment;
KZGProof proof;
BLSFieldElement fx, fy;
bool out;
bytes_to_bls_field(&fx, x);
bytes_to_bls_field(&fy, y);
if (bytes_to_g1(&commitment, c) != C_KZG_OK) return -1;
if (bytes_to_g1(&proof, p) != C_KZG_OK) return -1;
if (verify_kzg_proof(&out, &commitment, &fx, &fy, &proof, s) != C_KZG_OK)
return -2;
return out ? 0 : 1;
}
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
const std::string filePath = info[0].ToString().Utf8Value();
KZGSettings* kzgSettings = (KZGSettings*)malloc(sizeof(KZGSettings));
if (kzgSettings == NULL) {
Napi::TypeError::New(env, "Error while allocating memory for KZG settings").ThrowAsJavaScriptException();
return env.Null();
};
FILE* f = fopen(filePath.c_str(), "r");
if (f == NULL) {
free(kzgSettings);
Napi::TypeError::New(env, "Error opening trusted setup file").ThrowAsJavaScriptException();
return env.Null();
}
if (load_trusted_setup(kzgSettings, f) != C_KZG_OK) {
free(kzgSettings);
Napi::TypeError::New(env, "Error loading trusted setup file").ThrowAsJavaScriptException();
return env.Null();
}
// https://github.com/nodejs/node-addon-api/issues/667
Napi::External<KZGSettings> kzgSettingsPointer = Napi::External<KZGSettings>::New(info.Env(), kzgSettings);
kzgSettingsPointer.As<Napi::Object>().AddFinalizer([](Napi::Env, KZGSettings* kzgSettings) {
printf(" finalize \n"
" test %p "
"*test %x "
"&test %p\n", kzgSettings, *kzgSettings, &kzgSettings);
}, kzgSettings);
return kzgSettingsPointer;
}
void FreeTrustedSetup(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
KZGSettings* kzgSettings = info[0].As<Napi::External<KZGSettings>>().Data();
printf("Freeing: kzgSettings %p *kzgSettings %s &kzgSettings %p\n", kzgSettings, *kzgSettings, &kzgSettings);
freeTrustedSetup(kzgSettings);
}
Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
}
Napi::Value VerifyAggregateKzgProof(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
}
Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
}
Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "loadTrustedSetup"), Napi::Function::New(env, LoadTrustedSetup));
exports.Set(Napi::String::New(env, "freeTrustedSetup"), Napi::Function::New(env, FreeTrustedSetup));
exports.Set(Napi::String::New(env, "blobToKzgCommitment"), Napi::Function::New(env, BlobToKzgCommitment));
exports.Set(Napi::String::New(env, "verifyAggregateKzgProof"), Napi::Function::New(env, VerifyAggregateKzgProof));
exports.Set(Napi::String::New(env, "computeAggregateKzgProof"), Napi::Function::New(env, ComputeAggregateKzgProof));
exports.Set(Napi::String::New(env, "verifyKzgProof"), Napi::Function::New(env, VerifyKzgProof));
return exports;
}
NODE_API_MODULE(addon, Init)
// SCRATCH
// Napi::Object obj = Napi::Object::New(env);
// // Assign values to properties
// obj.Set("hello", "world");
// // obj.Set(uint32_t(42), "The Answer to Life, the Universe, and Everything");
// // obj.Set("Douglas Adams", true);
// // obj.Set("fftSettings", kzgSettings->fs);
// // Napi::Array::Array(napi_env env, napi_value value);
// const Napi::Array g1Values = Napi::Array::New(env, )
// obj.Set('g1Values', kzgSettings->g1_values);
// obj.Set('g2Values', kzgSettings->g2_values);

19
bindings/node.js/kzg.h Normal file
View File

@ -0,0 +1,19 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "c_kzg_4844.h"
int testFunction();
KZGSettings* loadTrustedSetup(const char* file);
void freeTrustedSetup(KZGSettings *s);
// void blobToKzgCommitment(uint8_t out[48], const uint8_t blob[FIELD_ELEMENTS_PER_BLOB * 32], const KZGSettings *s);
// int verifyKzgProof(const uint8_t c[48], const uint8_t x[32], const uint8_t y[32], const uint8_t p[48], KZGSettings *s);
// int verifyAggregateKzgProof(const uint8_t blobs[], const uint8_t commitments[], size_t n, const uint8_t proof[48], const KZGSettings *s);
// C_KZG_RET computeAggregateKzgProof(uint8_t out[48], const uint8_t blobs[], size_t n, const KZGSettings *s);

BIN
bindings/node.js/kzg.node Executable file

Binary file not shown.

View File

@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "",
"author": "Dan Coffman",
"main": "runnable.js",
"gypfile": true,
"scripts": {
"clean": "node-gyp clean",
@ -12,6 +13,7 @@
"node-gyp": "^9.3.0"
},
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^5.0.0"
}
}

View File

@ -1,29 +1,25 @@
'use strict';
const kzg = require('ckzg');
var bindings = require('bindings');
console.log('Bindings', bindings);
const kzg = bindings('kzg.node');
console.log('Loaded KZG library with functions:');
console.log(kzg);
console.log('Sanity checking C interop by calling no-op function...');
console.log('PASS', kzg.testFunction());
// console.log('Sanity checking C interop by calling no-op function...');
// console.log('PASS', kzg.testFunction());
// KZGSettings* loadTrustSetup(const char* file);
console.log('Invoking load_trusted_setup...');
const kzgSettings = kzg.loadTrustedSetup('../../src/trusted_setup.txt');
const kzgSettingsHandle = kzg.loadTrustedSetup('../../src/trusted_setup.txt');
console.log('PASS');
console.log(
'load_trusted_setup yielded KZGSettings: ',
{ ...kzgSettings },
kzgSettings.fs,
'load_trusted_setup yielded KZGSettings with handle: ',
kzgSettingsHandle,
);
for (var key in kzgSettings) {
console.log(key);
}
console.log(kzgSettings.getCPtr());
// void freeTrustedSetup(KZGSettings *s);
let config = {};
console.log('Invoking free_trusted_setup...');
const freeResult = kzg.freeTrustedSetup(kzgSettings);
const freeResult = kzg.freeTrustedSetup(kzgSettingsHandle);
console.log('PASS', freeResult);

View File

@ -80,6 +80,13 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"
brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
@ -183,6 +190,11 @@ err-code@^2.0.2:
resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9"
integrity sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA==
file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
fs-minipass@^2.0.0, fs-minipass@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"