Format node bindings with clang-format (#268)

This commit is contained in:
Matthew Keil 2023-03-30 12:20:25 -04:00 committed by GitHub
parent dd5ec7ba7e
commit e9c9c912f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 404 additions and 340 deletions

View File

@ -27,9 +27,13 @@ jobs:
uses: actions/setup-node@v1 uses: actions/setup-node@v1
with: with:
node-version: ${{matrix.node}} node-version: ${{matrix.node}}
- name: Check formatting
if: matrix.os == 'ubuntu-latest'
working-directory: bindings/node.js
run: make format
- name: Build/test bindings - name: Build/test bindings
working-directory: bindings/node.js working-directory: bindings/node.js
run: make run: make build test bundle
- name: Install distribution - name: Install distribution
working-directory: bindings/node.js/dist working-directory: bindings/node.js/dist
run: npm install run: npm install

View File

@ -0,0 +1,19 @@
#
# This file is a copy/paste from the one at the root of this repo. It was
# placed here for convenience as not all IDE's will pickup the settings
# in the root file and there was a fight between the IDE formatting and the
# official repo settings. Please keep this file in sync with the root file
#
BasedOnStyle: llvm
IndentWidth: 4
AllowAllParametersOfDeclarationOnNextLine: True
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: OnlyFirstIf
AlignAfterOpenBracket: BlockIndent
AlignEscapedNewlines: DontAlign
AlwaysBreakAfterDefinitionReturnType: None
BinPackArguments: False
BinPackParameters: False
PenaltyReturnTypeOnItsOwnLine: 1000
PenaltyBreakAssignment: 100

View File

@ -62,6 +62,7 @@ test: install
.PHONY: format .PHONY: format
format: install format: install
@$(YARN) prettier --loglevel=warn --write . @$(YARN) prettier --loglevel=warn --write .
@clang-format -i src/kzg.cxx
# Publish package to npm (requires an auth token) # Publish package to npm (requires an auth token)
.PHONY: publish .PHONY: publish

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <iostream>
#include <sstream> // std::ostringstream
#include <string_view>
#include <napi.h>
#include "c_kzg_4844.h"
#include "blst.h" #include "blst.h"
#include "c_kzg_4844.h"
#include <iostream>
#include <napi.h>
#include <sstream> // std::ostringstream
#include <stdio.h>
#include <string_view>
/** /**
* Convert C_KZG_RET to a string representation for error messages. * Convert C_KZG_RET to a string representation for error messages.
@ -23,7 +23,6 @@ std::string from_c_kzg_ret(C_KZG_RET ret) {
std::ostringstream msg; std::ostringstream msg;
msg << "UNKNOWN (" << ret << ")"; msg << "UNKNOWN (" << ret << ")";
return msg.str(); return msg.str();
} }
} }
@ -58,9 +57,9 @@ typedef struct {
* @param[in] data Pointer KzgAddonData stored by the runtime * @param[in] data Pointer KzgAddonData stored by the runtime
* @param[in] hint (unused) * @param[in] hint (unused)
*/ */
void delete_kzg_addon_data(napi_env /*env*/, void *data, void* /*hint*/) { void delete_kzg_addon_data(napi_env /*env*/, void *data, void * /*hint*/) {
if (((KzgAddonData*)data)->is_setup) { if (((KzgAddonData *)data)->is_setup) {
free_trusted_setup(&((KzgAddonData*)data)->settings); free_trusted_setup(&((KzgAddonData *)data)->settings);
} }
free(data); free(data);
} }
@ -84,7 +83,11 @@ void delete_kzg_addon_data(napi_env /*env*/, void *data, void* /*hint*/) {
KZGSettings *get_kzg_settings(Napi::Env &env, const Napi::CallbackInfo &info) { KZGSettings *get_kzg_settings(Napi::Env &env, const Napi::CallbackInfo &info) {
KzgAddonData *data = env.GetInstanceData<KzgAddonData>(); KzgAddonData *data = env.GetInstanceData<KzgAddonData>();
if (!data->is_setup) { if (!data->is_setup) {
Napi::Error::New(env, "Must run loadTrustedSetup before running any other c-kzg functions").ThrowAsJavaScriptException(); Napi::Error::New(
env,
"Must run loadTrustedSetup before running any other c-kzg functions"
)
.ThrowAsJavaScriptException();
return nullptr; return nullptr;
} }
return &(data->settings); return &(data->settings);
@ -121,9 +124,10 @@ inline uint8_t *get_bytes(
const Napi::Env &env, const Napi::Env &env,
const Napi::Value &val, const Napi::Value &val,
size_t length, size_t length,
std::string_view name) std::string_view name
{ ) {
if (!val.IsTypedArray() || val.As<Napi::TypedArray>().TypedArrayType() != napi_uint8_array) { if (!val.IsTypedArray() ||
val.As<Napi::TypedArray>().TypedArrayType() != napi_uint8_array) {
std::ostringstream msg; std::ostringstream msg;
msg << "Expected " << name << " to be a Uint8Array"; msg << "Expected " << name << " to be a Uint8Array";
Napi::TypeError::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::TypeError::New(env, msg.str()).ThrowAsJavaScriptException();
@ -139,22 +143,32 @@ inline uint8_t *get_bytes(
return array.Data(); return array.Data();
} }
inline Blob *get_blob(const Napi::Env &env, const Napi::Value &val) { inline Blob *get_blob(const Napi::Env &env, const Napi::Value &val) {
return reinterpret_cast<Blob *>(get_bytes(env, val, BYTES_PER_BLOB, "blob")); return reinterpret_cast<Blob *>(get_bytes(env, val, BYTES_PER_BLOB, "blob")
);
} }
inline Bytes32 *get_bytes32(const Napi::Env &env, const Napi::Value &val, std::string_view name) { inline Bytes32 *get_bytes32(
return reinterpret_cast<Bytes32 *>(get_bytes(env, val, BYTES_PER_FIELD_ELEMENT, name)); const Napi::Env &env, const Napi::Value &val, std::string_view name
) {
return reinterpret_cast<Bytes32 *>(
get_bytes(env, val, BYTES_PER_FIELD_ELEMENT, name)
);
} }
inline Bytes48 *get_bytes48(const Napi::Env &env, const Napi::Value &val, std::string_view name) { inline Bytes48 *get_bytes48(
return reinterpret_cast<Bytes48 *>(get_bytes(env, val, BYTES_PER_COMMITMENT, name)); const Napi::Env &env, const Napi::Value &val, std::string_view name
) {
return reinterpret_cast<Bytes48 *>(
get_bytes(env, val, BYTES_PER_COMMITMENT, name)
);
} }
Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) { Napi::Value LoadTrustedSetup(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
// Check if the trusted setup is already loaded // Check if the trusted setup is already loaded
KzgAddonData *data = env.GetInstanceData<KzgAddonData>(); KzgAddonData *data = env.GetInstanceData<KzgAddonData>();
if (data->is_setup) { if (data->is_setup) {
Napi::Error::New(env, "Error trusted setup is already loaded").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error trusted setup is already loaded")
.ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
@ -162,7 +176,8 @@ Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
std::string file_path = info[0].As<Napi::String>().Utf8Value(); std::string file_path = info[0].As<Napi::String>().Utf8Value();
FILE *file_handle = fopen(file_path.c_str(), "r"); FILE *file_handle = fopen(file_path.c_str(), "r");
if (file_handle == nullptr) { if (file_handle == nullptr) {
Napi::Error::New(env, "Error opening trusted setup file: " + file_path).ThrowAsJavaScriptException(); Napi::Error::New(env, "Error opening trusted setup file: " + file_path)
.ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
@ -186,33 +201,36 @@ Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
/** /**
* Convert a blob to a KZG commitment. * Convert a blob to a KZG commitment.
* *
* @param[in] {Blob} blob - The blob representing the polynomial to be committed to * @param[in] {Blob} blob - The blob representing the polynomial to be
* committed to
* *
* @return {KZGCommitment} - The resulting commitment * @return {KZGCommitment} - The resulting commitment
* *
* @throws {TypeError} - For invalid arguments or failure of the native library * @throws {TypeError} - For invalid arguments or failure of the native library
*/ */
Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) { Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Blob *blob = get_blob(env, info[0]); Blob *blob = get_blob(env, info[0]);
if (blob == nullptr) { if (blob == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGCommitment commitment; KZGCommitment commitment;
C_KZG_RET ret = blob_to_kzg_commitment(&commitment, blob, kzg_settings); C_KZG_RET ret = blob_to_kzg_commitment(&commitment, blob, kzg_settings);
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Failed to convert blob to commitment: " << from_c_kzg_ret(ret) ; msg << "Failed to convert blob to commitment: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
return Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&commitment), BYTES_PER_COMMITMENT); return Napi::Buffer<uint8_t>::Copy(
env, reinterpret_cast<uint8_t *>(&commitment), BYTES_PER_COMMITMENT
);
} }
/** /**
@ -226,45 +244,44 @@ Napi::Value BlobToKzgCommitment(const Napi::CallbackInfo& info) {
* *
* @throws {TypeError} - for invalid arguments or failure of the native library * @throws {TypeError} - for invalid arguments or failure of the native library
*/ */
Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) { Napi::Value ComputeKzgProof(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Blob *blob = get_blob(env, info[0]); Blob *blob = get_blob(env, info[0]);
if (blob == nullptr) { if (blob == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes32 *z_bytes = get_bytes32(env, info[1], "zBytes"); Bytes32 *z_bytes = get_bytes32(env, info[1], "zBytes");
if (z_bytes == nullptr) { if (z_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGProof proof; KZGProof proof;
Bytes32 y_out; Bytes32 y_out;
C_KZG_RET ret = compute_kzg_proof( C_KZG_RET ret = compute_kzg_proof(
&proof, &proof, &y_out, blob, z_bytes, kzg_settings
&y_out,
blob,
z_bytes,
kzg_settings
); );
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Failed to compute proof: " << from_c_kzg_ret(ret) ; msg << "Failed to compute proof: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
Napi::Array tuple = Napi::Array::New(env, 2); Napi::Array tuple = Napi::Array::New(env, 2);
tuple[(uint32_t)0] = Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF); tuple[(uint32_t)0] = Napi::Buffer<uint8_t>::Copy(
tuple[(uint32_t)1] = Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&y_out), BYTES_PER_FIELD_ELEMENT); env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF
);
tuple[(uint32_t)1] = Napi::Buffer<uint8_t>::Copy(
env, reinterpret_cast<uint8_t *>(&y_out), BYTES_PER_FIELD_ELEMENT
);
return tuple; return tuple;
} }
/** /**
* Given a blob, return the KZG proof that is used to verify it against the * Given a blob, return the KZG proof that is used to verify it against the
* commitment. * commitment.
@ -276,43 +293,43 @@ Napi::Value ComputeKzgProof(const Napi::CallbackInfo& info) {
* *
* @throws {TypeError} - for invalid arguments or failure of the native library * @throws {TypeError} - for invalid arguments or failure of the native library
*/ */
Napi::Value ComputeBlobKzgProof(const Napi::CallbackInfo& info) { Napi::Value ComputeBlobKzgProof(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Blob *blob = get_blob(env, info[0]); Blob *blob = get_blob(env, info[0]);
if (blob == nullptr) { if (blob == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes48 *commitment_bytes = get_bytes48(env, info[1], "commitmentBytes"); Bytes48 *commitment_bytes = get_bytes48(env, info[1], "commitmentBytes");
if (commitment_bytes == nullptr) { if (commitment_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGProof proof; KZGProof proof;
C_KZG_RET ret = compute_blob_kzg_proof( C_KZG_RET ret = compute_blob_kzg_proof(
&proof, &proof, blob, commitment_bytes, kzg_settings
blob,
commitment_bytes,
kzg_settings
); );
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Error in computeBlobKzgProof: " << from_c_kzg_ret(ret) ; msg << "Error in computeBlobKzgProof: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
return Napi::Buffer<uint8_t>::Copy(env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF); return Napi::Buffer<uint8_t>::Copy(
env, reinterpret_cast<uint8_t *>(&proof), BYTES_PER_PROOF
);
} }
/** /**
* Verify a KZG poof claiming that `p(z) == y`. * Verify a KZG poof claiming that `p(z) == y`.
* *
* @param[in] {Bytes48} commitmentBytes - The serialized commitment corresponding to polynomial p(x) * @param[in] {Bytes48} commitmentBytes - The serialized commitment
* corresponding to polynomial p(x)
* @param[in] {Bytes32} zBytes - The serialized evaluation point * @param[in] {Bytes32} zBytes - The serialized evaluation point
* @param[in] {Bytes32} yBytes - The serialized claimed evaluation result * @param[in] {Bytes32} yBytes - The serialized claimed evaluation result
* @param[in] {Bytes48} proofBytes - The serialized KZG proof * @param[in] {Bytes48} proofBytes - The serialized KZG proof
@ -321,42 +338,37 @@ Napi::Value ComputeBlobKzgProof(const Napi::CallbackInfo& info) {
* *
* @throws {TypeError} - for invalid arguments or failure of the native library * @throws {TypeError} - for invalid arguments or failure of the native library
*/ */
Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) { Napi::Value VerifyKzgProof(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Bytes48 *commitment_bytes = get_bytes48(env, info[0], "commitmentBytes"); Bytes48 *commitment_bytes = get_bytes48(env, info[0], "commitmentBytes");
if (commitment_bytes == nullptr) { if (commitment_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes32 *z_bytes = get_bytes32(env, info[1], "zBytes"); Bytes32 *z_bytes = get_bytes32(env, info[1], "zBytes");
if (z_bytes == nullptr) { if (z_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes32 *y_bytes = get_bytes32(env, info[2], "yBytes"); Bytes32 *y_bytes = get_bytes32(env, info[2], "yBytes");
if (y_bytes == nullptr) { if (y_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes48 *proof_bytes = get_bytes48(env, info[3], "proofBytes"); Bytes48 *proof_bytes = get_bytes48(env, info[3], "proofBytes");
if (proof_bytes == nullptr) { if (proof_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
bool out; bool out;
C_KZG_RET ret = verify_kzg_proof( C_KZG_RET ret = verify_kzg_proof(
&out, &out, commitment_bytes, z_bytes, y_bytes, proof_bytes, kzg_settings
commitment_bytes,
z_bytes,
y_bytes,
proof_bytes,
kzg_settings
); );
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Failed to verify KZG proof: " << from_c_kzg_ret(ret) ; msg << "Failed to verify KZG proof: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
@ -376,36 +388,33 @@ Napi::Value VerifyKzgProof(const Napi::CallbackInfo& info) {
* *
* @throws {TypeError} - for invalid arguments or failure of the native library * @throws {TypeError} - for invalid arguments or failure of the native library
*/ */
Napi::Value VerifyBlobKzgProof(const Napi::CallbackInfo& info) { Napi::Value VerifyBlobKzgProof(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Blob *blob_bytes = get_blob(env, info[0]); Blob *blob_bytes = get_blob(env, info[0]);
if (blob_bytes == nullptr) { if (blob_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes48 *commitment_bytes = get_bytes48(env, info[1], "commitmentBytes"); Bytes48 *commitment_bytes = get_bytes48(env, info[1], "commitmentBytes");
if (commitment_bytes == nullptr) { if (commitment_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
Bytes48 *proof_bytes = get_bytes48(env, info[2], "proofBytes"); Bytes48 *proof_bytes = get_bytes48(env, info[2], "proofBytes");
if (proof_bytes == nullptr) { if (proof_bytes == nullptr) {
return env.Undefined(); return env.Null();
} }
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
bool out; bool out;
C_KZG_RET ret = verify_blob_kzg_proof( C_KZG_RET ret = verify_blob_kzg_proof(
&out, &out, blob_bytes, commitment_bytes, proof_bytes, kzg_settings
blob_bytes, );
commitment_bytes,
proof_bytes,
kzg_settings);
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Error in verifyBlobKzgProof: " << from_c_kzg_ret(ret) ; msg << "Error in verifyBlobKzgProof: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
return env.Undefined(); return env.Undefined();
} }
@ -414,20 +423,22 @@ Napi::Value VerifyBlobKzgProof(const Napi::CallbackInfo& info) {
} }
/** /**
* 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
* provided commitment. * their provided commitment.
* *
* @remark blobs[0] relates to commitmentBytes[0] and proofBytes[0] * @remark blobs[0] relates to commitmentBytes[0] and proofBytes[0]
* *
* @param[in] {Blob} blobs - An array of serialized blobs to verify * @param[in] {Blob} blobs - An array of serialized blobs to verify
* @param[in] {Bytes48} commitmentBytes - An array of serialized commitments to verify * @param[in] {Bytes48} commitmentBytes - An array of serialized commitments to
* @param[in] {Bytes48} proofBytes - An array of serialized KZG proofs for verification * verify
* @param[in] {Bytes48} proofBytes - An array of serialized KZG proofs for
* verification
* *
* @return {boolean} - true/false depending on batch validity * @return {boolean} - true/false depending on batch validity
* *
* @throws {TypeError} - for invalid arguments or failure of the native library * @throws {TypeError} - for invalid arguments or failure of the native library
*/ */
Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo& info) { Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
C_KZG_RET ret; C_KZG_RET ret;
Blob *blobs = NULL; Blob *blobs = NULL;
@ -435,7 +446,10 @@ Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo& info) {
Bytes48 *proofs = NULL; Bytes48 *proofs = NULL;
Napi::Value result = env.Null(); Napi::Value result = env.Null();
if (!(info[0].IsArray() && info[1].IsArray() && info[2].IsArray())) { if (!(info[0].IsArray() && info[1].IsArray() && info[2].IsArray())) {
Napi::Error::New(env, "Blobs, commitments, and proofs must all be arrays").ThrowAsJavaScriptException(); Napi::Error::New(
env, "Blobs, commitments, and proofs must all be arrays"
)
.ThrowAsJavaScriptException();
return result; return result;
} }
Napi::Array blobs_param = info[0].As<Napi::Array>(); Napi::Array blobs_param = info[0].As<Napi::Array>();
@ -443,26 +457,32 @@ Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo& info) {
Napi::Array proofs_param = info[2].As<Napi::Array>(); Napi::Array proofs_param = info[2].As<Napi::Array>();
KZGSettings *kzg_settings = get_kzg_settings(env, info); KZGSettings *kzg_settings = get_kzg_settings(env, info);
if (kzg_settings == nullptr) { if (kzg_settings == nullptr) {
return env.Undefined(); return env.Null();
} }
uint32_t count = blobs_param.Length(); uint32_t count = blobs_param.Length();
if (count != commitments_param.Length() || count != proofs_param.Length()) { if (count != commitments_param.Length() || count != proofs_param.Length()) {
Napi::Error::New(env, "Requires equal number of blobs/commitments/proofs").ThrowAsJavaScriptException(); Napi::Error::New(
env, "Requires equal number of blobs/commitments/proofs"
)
.ThrowAsJavaScriptException();
return result; return result;
} }
blobs = (Blob *)calloc(count, sizeof(Blob)); blobs = (Blob *)calloc(count, sizeof(Blob));
if (blobs == nullptr) { if (blobs == nullptr) {
Napi::Error::New(env, "Error while allocating memory for blobs").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error while allocating memory for blobs")
.ThrowAsJavaScriptException();
goto out; goto out;
} }
commitments = (Bytes48 *)calloc(count, sizeof(Bytes48)); commitments = (Bytes48 *)calloc(count, sizeof(Bytes48));
if (commitments == nullptr) { if (commitments == nullptr) {
Napi::Error::New(env, "Error while allocating memory for commitments").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error while allocating memory for commitments")
.ThrowAsJavaScriptException();
goto out; goto out;
} }
proofs = (Bytes48 *)calloc(count, sizeof(Bytes48)); proofs = (Bytes48 *)calloc(count, sizeof(Bytes48));
if (proofs == nullptr) { if (proofs == nullptr) {
Napi::Error::New(env, "Error while allocating memory for proofs").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error while allocating memory for proofs")
.ThrowAsJavaScriptException();
goto out; goto out;
} }
@ -475,7 +495,9 @@ Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo& info) {
goto out; goto out;
} }
memcpy(&blobs[index], blob, BYTES_PER_BLOB); memcpy(&blobs[index], blob, BYTES_PER_BLOB);
Bytes48 *commitment = get_bytes48(env, commitments_param[index], "commitmentBytes"); Bytes48 *commitment = get_bytes48(
env, commitments_param[index], "commitmentBytes"
);
if (commitment == nullptr) { if (commitment == nullptr) {
goto out; goto out;
} }
@ -489,17 +511,12 @@ Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo& info) {
bool out; bool out;
ret = verify_blob_kzg_proof_batch( ret = verify_blob_kzg_proof_batch(
&out, &out, blobs, commitments, proofs, count, kzg_settings
blobs,
commitments,
proofs,
count,
kzg_settings
); );
if (ret != C_KZG_OK) { if (ret != C_KZG_OK) {
std::ostringstream msg; std::ostringstream msg;
msg << "Error in verifyBlobKzgProofBatch: " << from_c_kzg_ret(ret) ; msg << "Error in verifyBlobKzgProofBatch: " << from_c_kzg_ret(ret);
Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException();
goto out; goto out;
} }
@ -513,35 +530,58 @@ out:
return result; return result;
} }
Napi::Object Init(Napi::Env env, Napi::Object exports) { Napi::Object Init(Napi::Env env, Napi::Object exports) {
KzgAddonData* data = (KzgAddonData*)malloc(sizeof(KzgAddonData)); KzgAddonData *data = (KzgAddonData *)malloc(sizeof(KzgAddonData));
if (data == nullptr) { if (data == nullptr) {
Napi::Error::New(env, "Error allocating memory for kzg setup handle").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error allocating memory for kzg setup handle")
.ThrowAsJavaScriptException();
return exports; return exports;
} }
data->is_setup = false; data->is_setup = false;
napi_status status = napi_set_instance_data(env, data, delete_kzg_addon_data, NULL); napi_status status = napi_set_instance_data(
env, data, delete_kzg_addon_data, NULL
);
if (status != napi_ok) { if (status != napi_ok) {
Napi::Error::New(env, "Error setting kzg bindings instance data").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error setting kzg bindings instance data")
.ThrowAsJavaScriptException();
return exports; return exports;
} }
// Functions // Functions
exports["loadTrustedSetup"] = Napi::Function::New(env, LoadTrustedSetup, "setup"); exports["loadTrustedSetup"] = Napi::Function::New(
exports["blobToKzgCommitment"] = Napi::Function::New(env, BlobToKzgCommitment, "blobToKzgCommitment"); env, LoadTrustedSetup, "setup"
exports["computeKzgProof"] = Napi::Function::New(env, ComputeKzgProof, "computeKzgProof"); );
exports["computeBlobKzgProof"] = Napi::Function::New(env, ComputeBlobKzgProof, "computeBlobKzgProof"); exports["blobToKzgCommitment"] = Napi::Function::New(
exports["verifyKzgProof"] = Napi::Function::New(env, VerifyKzgProof, "verifyKzgProof"); env, BlobToKzgCommitment, "blobToKzgCommitment"
exports["verifyBlobKzgProof"] = Napi::Function::New(env, VerifyBlobKzgProof, "verifyBlobKzgProof"); );
exports["verifyBlobKzgProofBatch"] = Napi::Function::New(env, VerifyBlobKzgProofBatch, "verifyBlobKzgProofBatch"); exports["computeKzgProof"] = Napi::Function::New(
env, ComputeKzgProof, "computeKzgProof"
);
exports["computeBlobKzgProof"] = Napi::Function::New(
env, ComputeBlobKzgProof, "computeBlobKzgProof"
);
exports["verifyKzgProof"] = Napi::Function::New(
env, VerifyKzgProof, "verifyKzgProof"
);
exports["verifyBlobKzgProof"] = Napi::Function::New(
env, VerifyBlobKzgProof, "verifyBlobKzgProof"
);
exports["verifyBlobKzgProofBatch"] = Napi::Function::New(
env, VerifyBlobKzgProofBatch, "verifyBlobKzgProofBatch"
);
// Constants // Constants
exports["BYTES_PER_BLOB"] = Napi::Number::New(env, BYTES_PER_BLOB); exports["BYTES_PER_BLOB"] = Napi::Number::New(env, BYTES_PER_BLOB);
exports["BYTES_PER_COMMITMENT"] = Napi::Number::New(env, BYTES_PER_COMMITMENT); exports["BYTES_PER_COMMITMENT"] = Napi::Number::New(
exports["BYTES_PER_FIELD_ELEMENT"] = Napi::Number::New(env, BYTES_PER_FIELD_ELEMENT); env, BYTES_PER_COMMITMENT
);
exports["BYTES_PER_FIELD_ELEMENT"] = Napi::Number::New(
env, BYTES_PER_FIELD_ELEMENT
);
exports["BYTES_PER_PROOF"] = Napi::Number::New(env, BYTES_PER_PROOF); exports["BYTES_PER_PROOF"] = Napi::Number::New(env, BYTES_PER_PROOF);
exports["FIELD_ELEMENTS_PER_BLOB"] = Napi::Number::New(env, FIELD_ELEMENTS_PER_BLOB); exports["FIELD_ELEMENTS_PER_BLOB"] = Napi::Number::New(
env, FIELD_ELEMENTS_PER_BLOB
);
return exports; return exports;
} }