From b011c5057e15649efaae01e8e5745a387237c735 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 8 Mar 2024 23:29:33 +0800 Subject: [PATCH 01/32] fix: addon.cc for PR comments --- rebuild/src/addon.cc | 65 ++++++++++++++++++++++++++++++++++++-------- rebuild/src/addon.h | 20 +++++++------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index a311b04a..29023e72 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -1,20 +1,57 @@ #include "addon.h" +/** + * Checks if a specified range of bytes within a byte array consists only of + * zeros. + * + * @param data A pointer to the first element of the byte array to be checked. + * @param starting_index The offset (index) from the beginning of the array + * where the check should start. (0 starts at *data) + * @param byte_length The total length of the data array + * + * @return Returns true if all bytes from start_byte to the end of the array are + * zeros. Returns false if the starting offset is beyond the array length or if + * any byte in the specified range is not zero. + */ bool is_zero_bytes( - const uint8_t *data, const size_t start_byte, const size_t byte_length) { - for (size_t i = start_byte; i < byte_length; i++) { - if (data[i] != 0) { - return false; - } + const uint8_t *data, + const size_t starting_index, + const size_t byte_length) noexcept { + if (starting_index < byte_length) { + return std::all_of( + data + starting_index, data + byte_length, [](uint8_t x) { + return x == 0x00; + }); } - return true; + return false; } +/** + * Validates that a given byte length matches one of two specified lengths, + * optionally recording an error message if the validation fails. + * + * @param error_out A reference to a std::string where the error message is + * appended if the byte length does not match either length1 or length2. + * @param byte_length The length to be validated against length1 and length2. + * @param length1 The first valid length that byte_length can be. A value of 0 + * is considered as not set and thus not compared. + * @param length2 The second valid length that byte_length can be. A value of 0 + * is considered as not set and thus not compared. + * + * @return Returns true if byte_length matches length1 or (if length2 is not 0) + * length2. Returns false if byte_length matches neither, appending an + * appropriate error message to error_out. + * + * @note If both length1 and length2 are provided (non-zero), the error message + * will indicate that the valid byte_length must be either length1 or length2. + * If only one length is provided (the other being 0), the error message will + * only reference the provided length. + */ bool is_valid_length( std::string &error_out, size_t byte_length, size_t length1, - size_t length2) { + size_t length2) noexcept { if (byte_length == length1 || (length2 != 0 && byte_length == length2)) { return true; } @@ -68,6 +105,14 @@ BlstTsAddon::BlstTsAddon(Napi::Env env, Napi::Object exports) } std::string BlstTsAddon::GetBlstErrorString(const blst::BLST_ERROR &err) { + size_t err_index = static_cast(err); + // size of total array divided by size of one element minus 1 for 0 index + // basis + size_t max_index = + sizeof(_blst_error_strings) / sizeof(_blst_error_strings[0]) - 1; + if (err_index > max_index) { + return "BLST_ERROR::UNKNOWN_ERROR_CODE"; + } return _blst_error_strings[err]; } @@ -77,10 +122,8 @@ bool BlstTsAddon::GetRandomBytes(blst::byte *bytes, size_t length) { // [RandomBytesTraits::DeriveBits](https://github.com/nodejs/node/blob/4166d40d0873b6d8a0c7291872c8d20dc680b1d7/src/crypto/crypto_random.cc#L65) // [CSPRNG](https://github.com/nodejs/node/blob/4166d40d0873b6d8a0c7291872c8d20dc680b1d7/src/crypto/crypto_util.cc#L63) do { - if (1 == RAND_status()) { - if (1 == RAND_bytes(bytes, length)) { - return true; - } + if ((1 == RAND_status()) && (1 == RAND_bytes(bytes, length))) { + return true; } } while (1 == RAND_poll()); diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 0c88dd59..b533e31f 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -1,8 +1,8 @@ -#ifndef BLST_TS_ADDON_H__ -#define BLST_TS_ADDON_H__ +#pragma once #include +#include #include #include #include @@ -23,12 +23,12 @@ using std::endl; Napi::EscapableHandleScope scope(env); \ BlstTsAddon *module = env.GetInstanceData(); -#define BLST_TS_IS_INFINITY \ +#define BLST_TS_IS_INFINITY \ Napi::Env env = info.Env(); \ Napi::EscapableHandleScope scope(env); \ return scope.Escape(Napi::Boolean::New(env, _point->IsInfinite())); -#define BLST_TS_SERIALIZE_POINT(macro_name) \ +#define BLST_TS_SERIALIZE_POINT(macro_name) \ Napi::Env env = info.Env(); \ Napi::EscapableHandleScope scope(env); \ bool compressed{true}; \ @@ -42,7 +42,7 @@ using std::endl; _point->Serialize(compressed, serialized.Data()); \ return scope.Escape(serialized); -#define BLST_TS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ +#define BLST_TS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ if (!value_name.IsTypedArray()) { \ Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ @@ -59,7 +59,7 @@ using std::endl; Napi::Uint8Array arr_name = \ arr_name##_array.As>(); -#define BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ +#define BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ if (!value_name.IsTypedArray()) { \ Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ @@ -94,7 +94,9 @@ typedef enum { Affine, Jacobian } CoordType; * @return bool */ bool is_zero_bytes( - const uint8_t *data, const size_t start_byte, const size_t byte_length); + const uint8_t *data, + const size_t start_byte, + const size_t byte_length) noexcept; /** * Checks if a byte array is a valid length. If not, sets the error message and @@ -111,7 +113,7 @@ bool is_valid_length( std::string &error_out, size_t byte_length, size_t length1, - size_t length2 = 0); + size_t length2 = 0) noexcept; /** * Circular dependency if these are moved up to the top of the file. @@ -173,5 +175,3 @@ class BlstTsAddon : public Napi::Addon { */ bool GetRandomBytes(blst::byte *ikm, size_t length); }; - -#endif /* BLST_TS_ADDON_H__ */ From dcb01cd3ac80859660261d9804ae2379de77ab0b Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 8 Mar 2024 23:37:21 +0800 Subject: [PATCH 02/32] fix: use #pragma once --- rebuild/src/functions.h | 5 +---- rebuild/src/public_key.h | 5 +---- rebuild/src/secret_key.h | 5 +---- rebuild/src/signature.h | 5 +---- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/rebuild/src/functions.h b/rebuild/src/functions.h index dc3c5f31..de03c8d3 100644 --- a/rebuild/src/functions.h +++ b/rebuild/src/functions.h @@ -1,5 +1,4 @@ -#ifndef BLST_TS_FUNCTIONS_H__ -#define BLST_TS_FUNCTIONS_H__ +#pragma once #include "addon.h" #include "blst.hpp" @@ -8,5 +7,3 @@ namespace Functions { void Init(const Napi::Env &env, Napi::Object &exports); } - -#endif /* BLST_TS_FUNCTIONS_H__ */ diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 50ebf9e4..cb5ee2b8 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -1,5 +1,4 @@ -#ifndef BLST_TS_PUBLIC_KEY_H__ -#define BLST_TS_PUBLIC_KEY_H__ +#pragma once #include @@ -92,5 +91,3 @@ class PublicKey : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; - -#endif /* BLST_TS_PUBLIC_KEY_H__ */ diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index 1b9ca9f3..ac9a14ae 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -1,5 +1,4 @@ -#ifndef BLST_TS_SECRET_KEY_H__ -#define BLST_TS_SECRET_KEY_H__ +#pragma once #include @@ -24,5 +23,3 @@ class SecretKey : public Napi::ObjectWrap { Napi::Value ToPublicKey(const Napi::CallbackInfo &info); Napi::Value Sign(const Napi::CallbackInfo &info); }; - -#endif /* BLST_TS_SECRET_KEY_H__ */ diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 47f968f9..a3e95392 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -1,5 +1,4 @@ -#ifndef BLST_TS_SIGNATURE_H__ -#define BLST_TS_SIGNATURE_H__ +#pragma once #include @@ -115,5 +114,3 @@ class Signature : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; - -#endif /* BLST_TS_SIGNATURE_H__ */ From 99002cc12dff497309dd3d23361ba7cd44e206d3 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 8 Mar 2024 23:41:29 +0800 Subject: [PATCH 03/32] fix: remove using cout and endl --- rebuild/src/addon.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index b533e31f..9f938415 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -12,10 +12,6 @@ #include "blst.hpp" #include "napi.h" -// TODO: these should come out post PR review -using std::cout; -using std::endl; - #define BLST_TS_RANDOM_BYTES_LENGTH 8U #define BLST_TS_FUNCTION_PREAMBLE(info, env, module) \ From 4bc04742f68d8b5b11dfb2f84b993cad15bb509d Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Sat, 9 Mar 2024 00:00:27 +0800 Subject: [PATCH 04/32] fix: update visibility of BlstTsAddon members and remove _ for publics --- rebuild/src/addon.cc | 24 ++++++++++++------------ rebuild/src/addon.h | 12 +++++++----- rebuild/src/functions.cc | 12 ++++++------ rebuild/src/public_key.cc | 8 ++++---- rebuild/src/secret_key.cc | 12 ++++++------ rebuild/src/signature.cc | 8 ++++---- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index 29023e72..84c19887 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -70,20 +70,20 @@ bool is_valid_length( } BlstTsAddon::BlstTsAddon(Napi::Env env, Napi::Object exports) - : _dst{"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"}, - _blst_error_strings{ - "BLST_SUCCESS", - "BLST_ERROR::BLST_BAD_ENCODING", - "BLST_ERROR::BLST_POINT_NOT_ON_CURVE", - "BLST_ERROR::BLST_POINT_NOT_IN_GROUP", - "BLST_ERROR::BLST_AGGR_TYPE_MISMATCH", - "BLST_ERROR::BLST_VERIFY_FAIL", - "BLST_ERROR::BLST_PK_IS_INFINITY", - "BLST_ERROR::BLST_BAD_SCALAR", - } { + : _blst_error_strings{ + "BLST_SUCCESS", + "BLST_ERROR::BLST_BAD_ENCODING", + "BLST_ERROR::BLST_POINT_NOT_ON_CURVE", + "BLST_ERROR::BLST_POINT_NOT_IN_GROUP", + "BLST_ERROR::BLST_AGGR_TYPE_MISMATCH", + "BLST_ERROR::BLST_VERIFY_FAIL", + "BLST_ERROR::BLST_PK_IS_INFINITY", + "BLST_ERROR::BLST_BAD_SCALAR", + }, + dst{"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"} { Napi::Object js_constants = Napi::Object::New(env); js_constants.Set( - Napi::String::New(env, "DST"), Napi::String::New(env, _dst)); + Napi::String::New(env, "DST"), Napi::String::New(env, dst)); DefineAddon( exports, { diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 9f938415..56dd595f 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -124,12 +124,14 @@ bool is_valid_length( * for initialization and holding global values. */ class BlstTsAddon : public Napi::Addon { - public: - std::string _dst; + private: std::string _blst_error_strings[8]; - Napi::FunctionReference _secret_key_ctr; - Napi::FunctionReference _public_key_ctr; - Napi::FunctionReference _signature_ctr; + + public: + std::string dst; + Napi::FunctionReference secret_key_ctr; + Napi::FunctionReference public_key_ctr; + Napi::FunctionReference signature_ctr; /** * BlstTsAddon::BlstTsAddon constructor used by Node.js to create an diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index 68378573..8668b67b 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -68,7 +68,7 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { } } - return scope.Escape(module->_public_key_ctr.New( + return scope.Escape(module->public_key_ctr.New( {Napi::External::New(env, new P1{std::move(aggregate)})})); } @@ -131,7 +131,7 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { } } - return scope.Escape(module->_signature_ctr.New( + return scope.Escape(module->signature_ctr.New( {Napi::External::New(env, new P2{std::move(aggregate)})})); } @@ -201,7 +201,7 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { } std::unique_ptr ctx{ - new blst::Pairing(true, module->_dst)}; + new blst::Pairing(true, module->dst)}; for (uint32_t i = 0; i < pk_array_length; i++) { Napi::Value msg_value = msgs_array[i]; BLST_TS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "msg") @@ -273,7 +273,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { m_deferred{Env()}, m_has_error{false}, m_module{Env().GetInstanceData()}, - m_ctx{new blst::Pairing(true, m_module->_dst)}, + m_ctx{new blst::Pairing(true, m_module->dst)}, m_sig_point{}, m_sets{}, m_is_invalid{false}, @@ -459,7 +459,7 @@ Napi::Value AsyncAggregateVerify(const Napi::CallbackInfo &info) { Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) - std::unique_ptr ctx{new blst::Pairing(true, module->_dst)}; + std::unique_ptr ctx{new blst::Pairing(true, module->dst)}; try { if (!info[0].IsArray()) { Napi::TypeError::New( @@ -584,7 +584,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { m_deferred{Env()}, m_has_error{false}, m_module{Env().GetInstanceData()}, - m_ctx{new blst::Pairing(true, m_module->_dst)}, + m_ctx{new blst::Pairing(true, m_module->dst)}, m_sets{}, m_result{false} { Napi::Env env = Env(); diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index 247b66c0..c5c8748f 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -28,7 +28,7 @@ void PublicKey::Init( }; Napi::Function ctr = DefineClass(env, "PublicKey", proto, module); - module->_public_key_ctr = Napi::Persistent(ctr); + module->public_key_ctr = Napi::Persistent(ctr); exports.Set(Napi::String::New(env, "PublicKey"), ctr); Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As(); @@ -87,13 +87,13 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { try { if (is_affine) { return scope.Escape( - module->_public_key_ctr.New({Napi::External::New( + module->public_key_ctr.New({Napi::External::New( env, new P1Affine{blst::P1_Affine{ pk_bytes.Data(), pk_bytes.ByteLength()}})})); } return scope.Escape( - module->_public_key_ctr.New({Napi::External::New( + module->public_key_ctr.New({Napi::External::New( env, new P1{blst::P1{pk_bytes.Data(), pk_bytes.ByteLength()}})})); } catch (blst::BLST_ERROR err) { @@ -142,7 +142,7 @@ Napi::Value PublicKey::MultiplyBy(const Napi::CallbackInfo &info) { Napi::Value rand_bytes_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(rand_bytes_value, rand_bytes, "randomBytes") - Napi::Object pk_obj = module->_public_key_ctr.New( + Napi::Object pk_obj = module->public_key_ctr.New( // Default to jacobian coordinates {Napi::External::New( env, diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 69da19ab..bcac30c9 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -29,7 +29,7 @@ void SecretKey::Init( }; Napi::Function ctr = DefineClass(env, "SecretKey", proto, module); - module->_secret_key_ctr = Napi::Persistent(ctr); + module->secret_key_ctr = Napi::Persistent(ctr); exports.Set(Napi::String::New(env, "SecretKey"), ctr); exports.Get("BLST_CONSTANTS") @@ -55,7 +55,7 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { } bool is_external = true; - Napi::Object wrapped = module->_secret_key_ctr.New( + Napi::Object wrapped = module->secret_key_ctr.New( {Napi::External::New(env, &is_external)}); SecretKey *sk = SecretKey::Unwrap(wrapped); @@ -99,7 +99,7 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { } bool is_external = true; - Napi::Object wrapped = module->_secret_key_ctr.New( + Napi::Object wrapped = module->secret_key_ctr.New( {Napi::External::New(env, &is_external)}); SecretKey *sk = SecretKey::Unwrap(wrapped); @@ -142,7 +142,7 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) { Napi::Value SecretKey::ToPublicKey(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) - return scope.Escape(module->_public_key_ctr.New( + return scope.Escape(module->public_key_ctr.New( {Napi::External::New(env, new P1{blst::P1{*_key}}), Napi::Boolean::New(env, false)})); } @@ -160,12 +160,12 @@ Napi::Value SecretKey::Sign(const Napi::CallbackInfo &info) { Napi::Value msg_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "msg") - Napi::Object sig_obj = module->_signature_ctr.New( + Napi::Object sig_obj = module->signature_ctr.New( // Default to jacobian coordinates {Napi::External::New(env, new P2{blst::P2{}}), Napi::Boolean::New(env, false)}); Signature *sig = Napi::ObjectWrap::Unwrap(sig_obj); - sig->_point->Sign(*_key, msg.Data(), msg.ByteLength(), module->_dst); + sig->_point->Sign(*_key, msg.Data(), msg.ByteLength(), module->dst); return scope.Escape(sig_obj); } diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index aa4b00ca..20986fa8 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -29,7 +29,7 @@ void Signature::Init( }; Napi::Function ctr = DefineClass(env, "Signature", proto, module); - module->_signature_ctr = Napi::Persistent(ctr); + module->signature_ctr = Napi::Persistent(ctr); exports.Set(Napi::String::New(env, "Signature"), ctr); Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As(); @@ -88,13 +88,13 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { try { if (is_affine) { return scope.Escape( - module->_signature_ctr.New({Napi::External::New( + module->signature_ctr.New({Napi::External::New( env, new P2Affine{blst::P2_Affine{ sig_bytes.Data(), sig_bytes.ByteLength()}})})); } return scope.Escape( - module->_signature_ctr.New({Napi::External::New( + module->signature_ctr.New({Napi::External::New( env, new P2{blst::P2{sig_bytes.Data(), sig_bytes.ByteLength()}})})); } catch (blst::BLST_ERROR err) { @@ -139,7 +139,7 @@ Napi::Value Signature::MultiplyBy(const Napi::CallbackInfo &info) { Napi::Value rand_bytes_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(rand_bytes_value, rand_bytes, "randomBytes") - Napi::Object sig_obj = module->_signature_ctr.New( + Napi::Object sig_obj = module->signature_ctr.New( // Default to jacobian coordinates {Napi::External::New( env, From 254be1a734985b1f829f0812eed06ef2f3506691 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Sat, 9 Mar 2024 00:03:56 +0800 Subject: [PATCH 05/32] refactor: lowercase functions namespace and init --- rebuild/src/addon.cc | 2 +- rebuild/src/functions.cc | 4 ++-- rebuild/src/functions.h | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index 84c19887..601c3800 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -92,7 +92,7 @@ BlstTsAddon::BlstTsAddon(Napi::Env env, Napi::Object exports) SecretKey::Init(env, exports, this); PublicKey::Init(env, exports, this); Signature::Init(env, exports, this); - Functions::Init(env, exports); + functions::init(env, exports); env.SetInstanceData(this); // Check that openssl PRNG is seeded diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index 8668b67b..67eff5da 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -751,8 +751,8 @@ Napi::Value AsyncVerifyMultipleAggregateSignatures( } // anonymous namespace -namespace Functions { -void Init(const Napi::Env &env, Napi::Object &exports) { +namespace functions { +void init(const Napi::Env &env, Napi::Object &exports) { exports.Set( Napi::String::New(env, "aggregatePublicKeys"), Napi::Function::New(env, AggregatePublicKeys)); diff --git a/rebuild/src/functions.h b/rebuild/src/functions.h index de03c8d3..9967e890 100644 --- a/rebuild/src/functions.h +++ b/rebuild/src/functions.h @@ -4,6 +4,6 @@ #include "blst.hpp" #include "napi.h" -namespace Functions { -void Init(const Napi::Env &env, Napi::Object &exports); +namespace functions { +void init(const Napi::Env &env, Napi::Object &exports); } From a2a9b7149c2f97e1d667bfb7054b7d34dc374a42 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Sat, 9 Mar 2024 00:09:00 +0800 Subject: [PATCH 06/32] refactor: remove functions.h --- rebuild/src/addon.h | 4 +++- rebuild/src/functions.cc | 4 +++- rebuild/src/functions.h | 9 --------- 3 files changed, 6 insertions(+), 11 deletions(-) delete mode 100644 rebuild/src/functions.h diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 56dd595f..e754e073 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -114,10 +114,12 @@ bool is_valid_length( /** * Circular dependency if these are moved up to the top of the file. */ -#include "functions.h" #include "public_key.h" #include "secret_key.h" #include "signature.h" +namespace functions { +void init(const Napi::Env &env, Napi::Object &exports); +} /** * BlstTsAddon is the main entry point for the library. It is responsible diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index 67eff5da..bfba85d5 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -1,4 +1,6 @@ -#include "functions.h" +#include "addon.h" +#include "blst.hpp" +#include "napi.h" namespace { Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { diff --git a/rebuild/src/functions.h b/rebuild/src/functions.h deleted file mode 100644 index 9967e890..00000000 --- a/rebuild/src/functions.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "addon.h" -#include "blst.hpp" -#include "napi.h" - -namespace functions { -void init(const Napi::Env &env, Napi::Object &exports); -} From 8a2c5617f15eb6151be6fc5dfc518312a5cc1411 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Sat, 9 Mar 2024 00:26:38 +0800 Subject: [PATCH 07/32] fix: and ... catch to PublicKey::Deserialize and catch blst error by const & --- rebuild/src/public_key.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index c5c8748f..27652826 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -96,10 +96,14 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { module->public_key_ctr.New({Napi::External::New( env, new P1{blst::P1{pk_bytes.Data(), pk_bytes.ByteLength()}})})); - } catch (blst::BLST_ERROR err) { + } catch (const blst::BLST_ERROR &err) { Napi::RangeError::New(env, module->GetBlstErrorString(err)) .ThrowAsJavaScriptException(); return env.Undefined(); + } catch (...) { + Napi::Error::New(env, "BLST_ERROR: Unknown error deserializing PublicKey") + .ThrowAsJavaScriptException(); + return env.Undefined(); } } From 9919eb1fe20dbd587a4f39d35f97a40277626610 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Sat, 9 Mar 2024 00:27:45 +0800 Subject: [PATCH 08/32] fix: add is_affine = false case --- rebuild/src/public_key.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index 27652826..11545f60 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -70,7 +70,7 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { is_affine = true; break; case 1: - // is_affine defaults to false + is_affine = false; break; default: Napi::TypeError::New( From 3a5f64fb9669714ec8c4b5ecc3d511f0042ad8a3 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 17:35:26 +0800 Subject: [PATCH 09/32] refactor: remove auto type for proto in class init --- rebuild/src/public_key.cc | 2 +- rebuild/src/secret_key.cc | 2 +- rebuild/src/signature.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index 11545f60..f79744ec 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -3,7 +3,7 @@ void PublicKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope(env); - auto proto = { + std::initializer_list> proto = { StaticMethod( "deserialize", &PublicKey::Deserialize, diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index bcac30c9..7fc86b1c 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -3,7 +3,7 @@ void SecretKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope(env); - auto proto = { + std::initializer_list> proto = { StaticMethod( "fromKeygen", &SecretKey::FromKeygen, diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index 20986fa8..6c995ff2 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -4,7 +4,7 @@ void Signature::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope( env); // no need to Escape, Persistent will take care of it - auto proto = { + std::initializer_list> proto = { StaticMethod( "deserialize", &Signature::Deserialize, From 3d138e599a29adef3b25520fb6f008257f08657d Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 17:39:15 +0800 Subject: [PATCH 10/32] fix: change override to final in point wrapers --- rebuild/src/public_key.h | 24 ++++++++++++------------ rebuild/src/signature.h | 28 ++++++++++++++-------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index cb5ee2b8..89eef462 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -31,14 +31,14 @@ class P1 : public P1Wrapper { public: P1(blst::P1 point) : _point(std::move(point)) {} - bool IsInfinite() override { return _point.is_inf(); } - bool InGroup() override { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) override { + bool IsInfinite() final { return _point.is_inf(); } + bool InGroup() final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P1 &point) override { point.add(_point); }; + void AddTo(blst::P1 &point) final { point.add(_point); }; blst::P1 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) override { + blst::byte *rand_bytes, size_t rand_bytes_length) final { blst::byte out[96]; _point.serialize(out); // this should get std::move all the way into the P1 member value @@ -46,7 +46,7 @@ class P1 : public P1Wrapper { point.mult(rand_bytes, rand_bytes_length); return point; }; - P1AffineGroup AsAffine() override { + P1AffineGroup AsAffine() final { P1AffineGroup group{std::make_unique(_point), nullptr}; group.raw_point = group.smart_pointer.get(); return group; @@ -58,14 +58,14 @@ class P1Affine : public P1Wrapper { public: P1Affine(blst::P1_Affine point) : _point(std::move(point)) {} - bool IsInfinite() override { return _point.is_inf(); } - bool InGroup() override { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) override { + bool IsInfinite() final { return _point.is_inf(); } + bool InGroup() final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P1 &point) override { point.add(_point); }; + void AddTo(blst::P1 &point) final { point.add(_point); }; blst::P1 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) override { + blst::byte *rand_bytes, size_t rand_bytes_length) final { blst::byte out[96]; _point.serialize(out); // this should get std::move all the way into the P1 member value @@ -73,7 +73,7 @@ class P1Affine : public P1Wrapper { point.mult(rand_bytes, rand_bytes_length); return point; }; - P1AffineGroup AsAffine() override { + P1AffineGroup AsAffine() final { P1AffineGroup group{nullptr, &_point}; return group; } diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index a3e95392..aa968443 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -36,14 +36,14 @@ class P2 : public P2Wrapper { public: P2(blst::P2 point) : _point(std::move(point)) {} - bool IsInfinite() override { return _point.is_inf(); } - bool InGroup() override { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) override { + bool IsInfinite() final { return _point.is_inf(); } + bool InGroup() final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P2 &point) override { point.add(_point); }; + void AddTo(blst::P2 &point) final { point.add(_point); }; blst::P2 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) override { + blst::byte *rand_bytes, size_t rand_bytes_length) final { blst::byte out[192]; _point.serialize(out); // this should get std::move all the way into the P2 member value @@ -51,7 +51,7 @@ class P2 : public P2Wrapper { point.mult(rand_bytes, rand_bytes_length); return point; }; - P2AffineGroup AsAffine() override { + P2AffineGroup AsAffine() final { P2AffineGroup group{std::make_unique(_point), nullptr}; group.raw_point = group.smart_pointer.get(); return group; @@ -60,7 +60,7 @@ class P2 : public P2Wrapper { const blst::SecretKey &key, uint8_t *msg, size_t msg_length, - const std::string &dst) override { + const std::string &dst) final { _point.hash_to(msg, msg_length, dst); _point.sign_with(key); }; @@ -71,14 +71,14 @@ class P2Affine : public P2Wrapper { public: P2Affine(blst::P2_Affine point) : _point(std::move(point)) {} - bool IsInfinite() override { return _point.is_inf(); } - bool InGroup() override { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) override { + bool IsInfinite() final { return _point.is_inf(); } + bool InGroup() final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P2 &point) override { point.add(_point); }; + void AddTo(blst::P2 &point) final { point.add(_point); }; blst::P2 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) override { + blst::byte *rand_bytes, size_t rand_bytes_length) final { blst::byte out[192]; _point.serialize(out); // this should get std::move all the way into the P2 member value @@ -86,7 +86,7 @@ class P2Affine : public P2Wrapper { point.mult(rand_bytes, rand_bytes_length); return point; }; - P2AffineGroup AsAffine() override { + P2AffineGroup AsAffine() final { P2AffineGroup group{nullptr, &_point}; return group; } @@ -94,7 +94,7 @@ class P2Affine : public P2Wrapper { const blst::SecretKey &key, uint8_t *msg, size_t msg_length, - const std::string &dst) override { + const std::string &dst) final { blst::P2 jacobian{_point}; jacobian.hash_to(msg, msg_length, dst); jacobian.sign_with(key); From 13cbfb80ce5e9243e37d6920f58aa3a7cdea7198 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 17:41:49 +0800 Subject: [PATCH 11/32] fix: replace hard coded numbers to macros --- rebuild/src/public_key.h | 4 ++-- rebuild/src/signature.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 89eef462..79db49f4 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -66,10 +66,10 @@ class P1Affine : public P1Wrapper { void AddTo(blst::P1 &point) final { point.add(_point); }; blst::P1 MultiplyBy( blst::byte *rand_bytes, size_t rand_bytes_length) final { - blst::byte out[96]; + blst::byte out[BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P1 member value - blst::P1 point{out, 96}; + blst::P1 point{out, BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED}; point.mult(rand_bytes, rand_bytes_length); return point; }; diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index aa968443..8fa1cdf0 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -44,10 +44,10 @@ class P2 : public P2Wrapper { void AddTo(blst::P2 &point) final { point.add(_point); }; blst::P2 MultiplyBy( blst::byte *rand_bytes, size_t rand_bytes_length) final { - blst::byte out[192]; + blst::byte out[BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P2 member value - blst::P2 point{out, 192}; + blst::P2 point{out, BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED}; point.mult(rand_bytes, rand_bytes_length); return point; }; From 4f52d9a32ef0031a653950704ca1b7594610a362 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 17:43:15 +0800 Subject: [PATCH 12/32] fix: make points in wrappers private --- rebuild/src/public_key.h | 2 ++ rebuild/src/signature.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 79db49f4..f4fdd2d4 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -27,6 +27,7 @@ class P1Wrapper { }; class P1 : public P1Wrapper { + private: blst::P1 _point; public: @@ -54,6 +55,7 @@ class P1 : public P1Wrapper { }; class P1Affine : public P1Wrapper { + private: blst::P1_Affine _point; public: diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 8fa1cdf0..504f290c 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -32,6 +32,7 @@ class P2Wrapper { }; class P2 : public P2Wrapper { + private: blst::P2 _point; public: @@ -67,6 +68,7 @@ class P2 : public P2Wrapper { }; class P2Affine : public P2Wrapper { + private: blst::P2_Affine _point; public: From d4122871789720b07446363fd4fec013145a9b32 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 19:08:25 +0800 Subject: [PATCH 13/32] fix: add const casting to all point wrapper methods where feasible --- rebuild/src/public_key.h | 32 +++++++++++++++-------------- rebuild/src/signature.h | 44 +++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index f4fdd2d4..a22abe3a 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -17,12 +17,12 @@ typedef struct { class P1Wrapper { public: virtual ~P1Wrapper() = default; - virtual bool IsInfinite() = 0; - virtual bool InGroup() = 0; - virtual void Serialize(bool compress, blst::byte *out) = 0; - virtual void AddTo(blst::P1 &point) = 0; + virtual bool IsInfinite() const = 0; + virtual bool InGroup() const = 0; + virtual void Serialize(bool compress, blst::byte *out) const = 0; + virtual void AddTo(blst::P1 &point) const = 0; virtual blst::P1 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) = 0; + const blst::byte *rand_bytes, const size_t rand_bytes_length) const = 0; virtual P1AffineGroup AsAffine() = 0; }; @@ -32,14 +32,15 @@ class P1 : public P1Wrapper { public: P1(blst::P1 point) : _point(std::move(point)) {} - bool IsInfinite() final { return _point.is_inf(); } - bool InGroup() final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) final { + bool IsInfinite() const final { return _point.is_inf(); } + bool InGroup() const final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) const final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P1 &point) final { point.add(_point); }; + void AddTo(blst::P1 &point) const final { point.add(_point); }; blst::P1 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) final { + const blst::byte *rand_bytes, + const size_t rand_bytes_length) const final { blst::byte out[96]; _point.serialize(out); // this should get std::move all the way into the P1 member value @@ -60,14 +61,15 @@ class P1Affine : public P1Wrapper { public: P1Affine(blst::P1_Affine point) : _point(std::move(point)) {} - bool IsInfinite() final { return _point.is_inf(); } - bool InGroup() final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) final { + bool IsInfinite() const final { return _point.is_inf(); } + bool InGroup() const final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) const final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P1 &point) final { point.add(_point); }; + void AddTo(blst::P1 &point) const final { point.add(_point); }; blst::P1 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) final { + const blst::byte *rand_bytes, + const size_t rand_bytes_length) const final { blst::byte out[BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P1 member value diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 504f290c..7c7004eb 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -17,17 +17,17 @@ typedef struct { class P2Wrapper { public: virtual ~P2Wrapper() = default; - virtual bool IsInfinite() = 0; - virtual bool InGroup() = 0; - virtual void Serialize(bool compress, blst::byte *out) = 0; - virtual void AddTo(blst::P2 &point) = 0; + virtual bool IsInfinite() const = 0; + virtual bool InGroup() const = 0; + virtual void Serialize(bool compress, blst::byte *out) const = 0; + virtual void AddTo(blst::P2 &point) const = 0; virtual blst::P2 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) = 0; + const blst::byte *rand_bytes, const size_t rand_bytes_length) const = 0; virtual P2AffineGroup AsAffine() = 0; virtual void Sign( const blst::SecretKey &key, - uint8_t *msg, - size_t msg_length, + const uint8_t *msg, + const size_t msg_length, const std::string &dst) = 0; }; @@ -37,14 +37,15 @@ class P2 : public P2Wrapper { public: P2(blst::P2 point) : _point(std::move(point)) {} - bool IsInfinite() final { return _point.is_inf(); } - bool InGroup() final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) final { + bool IsInfinite() const final { return _point.is_inf(); } + bool InGroup() const final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) const final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P2 &point) final { point.add(_point); }; + void AddTo(blst::P2 &point) const final { point.add(_point); }; blst::P2 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) final { + const blst::byte *rand_bytes, + const size_t rand_bytes_length) const final { blst::byte out[BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P2 member value @@ -59,8 +60,8 @@ class P2 : public P2Wrapper { }; void Sign( const blst::SecretKey &key, - uint8_t *msg, - size_t msg_length, + const uint8_t *msg, + const size_t msg_length, const std::string &dst) final { _point.hash_to(msg, msg_length, dst); _point.sign_with(key); @@ -73,14 +74,15 @@ class P2Affine : public P2Wrapper { public: P2Affine(blst::P2_Affine point) : _point(std::move(point)) {} - bool IsInfinite() final { return _point.is_inf(); } - bool InGroup() final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) final { + bool IsInfinite() const final { return _point.is_inf(); } + bool InGroup() const final { return _point.in_group(); } + void Serialize(bool compress, blst::byte *out) const final { compress ? _point.compress(out) : _point.serialize(out); } - void AddTo(blst::P2 &point) final { point.add(_point); }; + void AddTo(blst::P2 &point) const final { point.add(_point); }; blst::P2 MultiplyBy( - blst::byte *rand_bytes, size_t rand_bytes_length) final { + const blst::byte *rand_bytes, + const size_t rand_bytes_length) const final { blst::byte out[192]; _point.serialize(out); // this should get std::move all the way into the P2 member value @@ -94,8 +96,8 @@ class P2Affine : public P2Wrapper { } void Sign( const blst::SecretKey &key, - uint8_t *msg, - size_t msg_length, + const uint8_t *msg, + const size_t msg_length, const std::string &dst) final { blst::P2 jacobian{_point}; jacobian.hash_to(msg, msg_length, dst); From f8c1239806775ab9e6e635cf11a2cd7fb580ef21 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:00:51 +0800 Subject: [PATCH 14/32] fix: missed swapping hard coded numbers to macros --- rebuild/src/public_key.h | 4 ++-- rebuild/src/signature.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index a22abe3a..01dabf86 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -41,10 +41,10 @@ class P1 : public P1Wrapper { blst::P1 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[96]; + blst::byte out[BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P1 member value - blst::P1 point{out, 96}; + blst::P1 point{out, BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED}; point.mult(rand_bytes, rand_bytes_length); return point; }; diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 7c7004eb..3863fd24 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -83,10 +83,10 @@ class P2Affine : public P2Wrapper { blst::P2 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[192]; + blst::byte out[BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED]; _point.serialize(out); // this should get std::move all the way into the P2 member value - blst::P2 point{out, 192}; + blst::P2 point{out, BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED}; point.mult(rand_bytes, rand_bytes_length); return point; }; From 962abccbbf13f7b9edc62223ed94756f734bf7c2 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:01:16 +0800 Subject: [PATCH 15/32] feat: make gyp file c++17 compliant --- rebuild/binding.gyp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rebuild/binding.gyp b/rebuild/binding.gyp index 27a29439..7550693c 100644 --- a/rebuild/binding.gyp +++ b/rebuild/binding.gyp @@ -27,6 +27,7 @@ '-Wpedantic', ], 'cflags_cc': [ + '-std=c++17', '-fexceptions', '-Werror', '-Wall', @@ -41,6 +42,7 @@ 'VCCLCompilerTool': { 'ExceptionHandling': 1, 'EnablePREfast': 'true', + 'AdditionalOptions': [ '/std:c++17' ], }, }, } @@ -52,6 +54,7 @@ ['OS=="mac"', { 'xcode_settings': { 'OTHER_CFLAGS': ['-fvisibility=hidden'], + 'CLANG_CXX_LANGUAGE_STANDARD': 'c++17', 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', 'CLANG_CXX_LIBRARY': 'libc++', 'MACOSX_DEPLOYMENT_TARGET': '12', From 560e699492ee1c45e3d6c6e67a65a475a8a295ff Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:07:50 +0800 Subject: [PATCH 16/32] fix: remove duplicate headers from secret_key.h --- rebuild/src/secret_key.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index ac9a14ae..3c49a229 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -3,8 +3,6 @@ #include #include "addon.h" -#include "blst.hpp" -#include "napi.h" #include "public_key.h" #include "signature.h" From bf0461f5a8d0a6db403ae4fdcf9af4cf925a69b7 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:19:07 +0800 Subject: [PATCH 17/32] refactor: remove underscore from public members --- rebuild/src/addon.h | 4 ++-- rebuild/src/functions.cc | 20 ++++++++++---------- rebuild/src/public_key.cc | 10 +++++----- rebuild/src/public_key.h | 2 +- rebuild/src/secret_key.cc | 24 ++++++++++++------------ rebuild/src/secret_key.h | 4 ++-- rebuild/src/signature.cc | 8 ++++---- rebuild/src/signature.h | 2 +- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index e754e073..f0741d45 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -22,7 +22,7 @@ #define BLST_TS_IS_INFINITY \ Napi::Env env = info.Env(); \ Napi::EscapableHandleScope scope(env); \ - return scope.Escape(Napi::Boolean::New(env, _point->IsInfinite())); + return scope.Escape(Napi::Boolean::New(env, point->IsInfinite())); #define BLST_TS_SERIALIZE_POINT(macro_name) \ Napi::Env env = info.Env(); \ @@ -35,7 +35,7 @@ env, \ compressed ? BLST_TS_##macro_name##_LENGTH_COMPRESSED \ : BLST_TS_##macro_name##_LENGTH_UNCOMPRESSED); \ - _point->Serialize(compressed, serialized.Data()); \ + point->Serialize(compressed, serialized.Data()); \ return scope.Escape(serialized); #define BLST_TS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index bfba85d5..ee9ae96b 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -51,7 +51,7 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { } else { PublicKey *to_aggregate = PublicKey::Unwrap(val.As()); - to_aggregate->_point->AddTo(aggregate); + to_aggregate->point->AddTo(aggregate); } if (has_error) { return env.Undefined(); @@ -114,7 +114,7 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { } else { Signature *to_aggregate = Signature::Unwrap(val.As()); - to_aggregate->_point->AddTo(aggregate); + to_aggregate->point->AddTo(aggregate); } if (has_error) { return env.Undefined(); @@ -160,7 +160,7 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { } else { Signature *to_verify = Signature::Unwrap(sig_val.As()); - sig_point = to_verify->_point->AsAffine(); + sig_point = to_verify->point->AsAffine(); } if (!info[0].IsArray()) { @@ -237,7 +237,7 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { } else { PublicKey *to_verify = PublicKey::Unwrap(pk_val.As()); - pk_point = to_verify->_point->AsAffine(); + pk_point = to_verify->point->AsAffine(); } blst::BLST_ERROR err = ctx->aggregate( @@ -304,7 +304,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { } else { Signature *to_verify = Signature::Unwrap(sig_val.As()); - m_sig_point = to_verify->_point->AsAffine(); + m_sig_point = to_verify->point->AsAffine(); } if (!info[0].IsArray()) { @@ -397,7 +397,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { } else { PublicKey *to_verify = PublicKey::Unwrap(pk_val.As()); - m_sets[i].pk_point = to_verify->_point->AsAffine(); + m_sets[i].pk_point = to_verify->point->AsAffine(); } } } catch (...) { @@ -522,7 +522,7 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { } else { PublicKey *to_verify = PublicKey::Unwrap(pk_val.As()); - pk_point = to_verify->_point->AsAffine(); + pk_point = to_verify->point->AsAffine(); } Napi::Value sig_val = set.Get("signature"); @@ -546,7 +546,7 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { } else { Signature *to_verify = Signature::Unwrap(sig_val.As()); - sig_point = to_verify->_point->AsAffine(); + sig_point = to_verify->point->AsAffine(); } blst::BLST_ERROR err = ctx->mul_n_aggregate( @@ -656,7 +656,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { } else { PublicKey *to_verify = PublicKey::Unwrap(pk_val.As()); - m_sets[i].pk_point = to_verify->_point->AsAffine(); + m_sets[i].pk_point = to_verify->point->AsAffine(); } Napi::Value sig_val = set.Get("signature"); @@ -683,7 +683,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { } else { Signature *to_verify = Signature::Unwrap(sig_val.As()); - m_sets[i].sig_point = to_verify->_point->AsAffine(); + m_sets[i].sig_point = to_verify->point->AsAffine(); } } } catch (const blst::BLST_ERROR &err) { diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index f79744ec..ceff371d 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -108,7 +108,7 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { } PublicKey::PublicKey(const Napi::CallbackInfo &info) - : Napi::ObjectWrap{info}, _point{nullptr} { + : Napi::ObjectWrap{info}, point{nullptr} { Napi::Env env = info.Env(); Napi::HandleScope scope(env); // Check that constructor was called from C++ and not JS. Externals can only @@ -118,7 +118,7 @@ PublicKey::PublicKey(const Napi::CallbackInfo &info) .ThrowAsJavaScriptException(); return; } - _point.reset(info[0].As>().Data()); + point.reset(info[0].As>().Data()); } Napi::Value PublicKey::Serialize(const Napi::CallbackInfo &info){ @@ -127,11 +127,11 @@ Napi::Value PublicKey::Serialize(const Napi::CallbackInfo &info){ Napi::Value PublicKey::KeyValidate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - if (_point->IsInfinite()) { + if (point->IsInfinite()) { Napi::Error::New(env, "BLST_ERROR::BLST_PK_IS_INFINITY") .ThrowAsJavaScriptException(); } - if (!_point->InGroup()) { + if (!point->InGroup()) { Napi::Error::New(env, "BLST_ERROR::BLST_POINT_NOT_IN_GROUP") .ThrowAsJavaScriptException(); } @@ -150,7 +150,7 @@ Napi::Value PublicKey::MultiplyBy(const Napi::CallbackInfo &info) { // Default to jacobian coordinates {Napi::External::New( env, - new P1{_point->MultiplyBy( + new P1{point->MultiplyBy( rand_bytes.Data(), rand_bytes.ByteLength())}), Napi::Boolean::New(env, false)}); diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 01dabf86..8bafe7ea 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -85,7 +85,7 @@ class P1Affine : public P1Wrapper { class PublicKey : public Napi::ObjectWrap { public: - std::unique_ptr _point; + std::unique_ptr point; static void Init(Napi::Env env, Napi::Object &exports, BlstTsAddon *module); static Napi::Value Deserialize(const Napi::CallbackInfo &info); diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 7fc86b1c..1c837123 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -66,21 +66,21 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { .ThrowAsJavaScriptException(); return env.Undefined(); } - sk->_key->keygen( + sk->key->keygen( ikm.Data(), ikm.ByteLength(), info[1].As().Utf8Value()); } else { - sk->_key->keygen(ikm.Data(), ikm.ByteLength()); + sk->key->keygen(ikm.Data(), ikm.ByteLength()); } // Check if key is zero and set flag if so. Several specs depend on this // check (signing with zero key). Do after building instead of checking // incoming bytes incase info changes the key blst::byte key_bytes[BLST_TS_SECRET_KEY_LENGTH]; - sk->_key->to_bendian(key_bytes); + sk->key->to_bendian(key_bytes); if (is_zero_bytes(key_bytes, 0, BLST_TS_SECRET_KEY_LENGTH)) { - sk->_is_zero_key = true; + sk->is_zero_key = true; } return scope.Escape(wrapped); @@ -106,18 +106,18 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { // Check if key is zero and set flag if so. Several specs depend on this // check (signing with zero key) if (is_zero_bytes(sk_bytes.Data(), 0, sk_bytes.ByteLength())) { - sk->_is_zero_key = true; + sk->is_zero_key = true; } // Deserialize key - sk->_key->from_bendian(sk_bytes.Data()); + sk->key->from_bendian(sk_bytes.Data()); return scope.Escape(wrapped); } SecretKey::SecretKey(const Napi::CallbackInfo &info) : Napi::ObjectWrap{info}, - _key{std::make_unique()}, - _is_zero_key{false} { + key{std::make_unique()}, + is_zero_key{false} { Napi::Env env = info.Env(); Napi::HandleScope scope(env); // Check that constructor was called from C++ and not JS. Externals can only @@ -135,7 +135,7 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) { Napi::Buffer serialized = Napi::Buffer::New(env, BLST_TS_SECRET_KEY_LENGTH); - _key->to_bendian(serialized.Data()); + key->to_bendian(serialized.Data()); return scope.Escape(serialized); } @@ -143,14 +143,14 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) { Napi::Value SecretKey::ToPublicKey(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) return scope.Escape(module->public_key_ctr.New( - {Napi::External::New(env, new P1{blst::P1{*_key}}), + {Napi::External::New(env, new P1{blst::P1{*key}}), Napi::Boolean::New(env, false)})); } Napi::Value SecretKey::Sign(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) // Check for zero key and throw error to meet spec requirements - if (_is_zero_key) { + if (is_zero_key) { Napi::TypeError::New( env, "BLST_ERROR: cannot sign message with zero private key") .ThrowAsJavaScriptException(); @@ -165,7 +165,7 @@ Napi::Value SecretKey::Sign(const Napi::CallbackInfo &info) { {Napi::External::New(env, new P2{blst::P2{}}), Napi::Boolean::New(env, false)}); Signature *sig = Napi::ObjectWrap::Unwrap(sig_obj); - sig->_point->Sign(*_key, msg.Data(), msg.ByteLength(), module->dst); + sig->point->Sign(*key, msg.Data(), msg.ByteLength(), module->dst); return scope.Escape(sig_obj); } diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index 3c49a229..24171cdf 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -10,8 +10,8 @@ class SecretKey : public Napi::ObjectWrap { public: - std::unique_ptr _key; - bool _is_zero_key; + std::unique_ptr key; + bool is_zero_key; static void Init(Napi::Env env, Napi::Object &exports, BlstTsAddon *module); static Napi::Value FromKeygen(const Napi::CallbackInfo &info); diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index 6c995ff2..3e8188fb 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -105,7 +105,7 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { } Signature::Signature(const Napi::CallbackInfo &info) - : Napi::ObjectWrap{info}, _point{nullptr} { + : Napi::ObjectWrap{info}, point{nullptr} { Napi::Env env = info.Env(); Napi::HandleScope scope(env); // Check that constructor was called from C++ and not JS. Externals can only @@ -115,7 +115,7 @@ Signature::Signature(const Napi::CallbackInfo &info) .ThrowAsJavaScriptException(); return; } - _point.reset(info[0].As>().Data()); + point.reset(info[0].As>().Data()); } Napi::Value Signature::Serialize(const Napi::CallbackInfo &info){ @@ -124,7 +124,7 @@ Napi::Value Signature::Serialize(const Napi::CallbackInfo &info){ Napi::Value Signature::SigValidate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - if (!_point->InGroup()) { + if (!point->InGroup()) { Napi::Error::New(env, "BLST_ERROR::BLST_POINT_NOT_IN_GROUP") .ThrowAsJavaScriptException(); } @@ -143,7 +143,7 @@ Napi::Value Signature::MultiplyBy(const Napi::CallbackInfo &info) { // Default to jacobian coordinates {Napi::External::New( env, - new P2{_point->MultiplyBy( + new P2{point->MultiplyBy( rand_bytes.Data(), rand_bytes.ByteLength())}), Napi::Boolean::New(env, false)}); diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 3863fd24..d52eb4b8 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -108,7 +108,7 @@ class P2Affine : public P2Wrapper { class Signature : public Napi::ObjectWrap { public: - std::unique_ptr _point; + std::unique_ptr point; static void Init(Napi::Env env, Napi::Object &exports, BlstTsAddon *module); static Napi::Value Deserialize(const Napi::CallbackInfo &info); From 5d0225597d38fbb563ad64a6b2e908f817b2a906 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:39:31 +0800 Subject: [PATCH 18/32] refactor: convert macro sizes to static const values --- rebuild/src/addon.h | 8 ++++---- rebuild/src/functions.cc | 40 +++++++++++++++++++-------------------- rebuild/src/public_key.cc | 10 +++++----- rebuild/src/public_key.h | 12 ++++++------ rebuild/src/secret_key.cc | 14 +++++++------- rebuild/src/secret_key.h | 2 +- rebuild/src/signature.cc | 10 +++++----- rebuild/src/signature.h | 12 ++++++------ 8 files changed, 54 insertions(+), 54 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index f0741d45..6f49f07a 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -24,7 +24,7 @@ Napi::EscapableHandleScope scope(env); \ return scope.Escape(Napi::Boolean::New(env, point->IsInfinite())); -#define BLST_TS_SERIALIZE_POINT(macro_name) \ +#define BLST_TS_SERIALIZE_POINT(snake_case_name) \ Napi::Env env = info.Env(); \ Napi::EscapableHandleScope scope(env); \ bool compressed{true}; \ @@ -33,9 +33,9 @@ } \ Napi::Buffer serialized = Napi::Buffer::New( \ env, \ - compressed ? BLST_TS_##macro_name##_LENGTH_COMPRESSED \ - : BLST_TS_##macro_name##_LENGTH_UNCOMPRESSED); \ - point->Serialize(compressed, serialized.Data()); \ + compressed ? snake_case_name##_length_compressed \ + : snake_case_name##_length_uncompressed); \ + point->Serialize(compressed, serialized.Data()); \ return scope.Escape(serialized); #define BLST_TS_UNWRAP_UINT_8_ARRAY(value_name, arr_name, js_name) \ diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index ee9ae96b..db214cff 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -31,8 +31,8 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); has_error = true; @@ -102,8 +102,8 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); has_error = true; @@ -148,8 +148,8 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } else { @@ -216,8 +216,8 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); @@ -289,8 +289,8 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; @@ -371,8 +371,8 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_is_invalid = true; @@ -501,8 +501,8 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); @@ -533,8 +533,8 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); @@ -630,8 +630,8 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; @@ -667,8 +667,8 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { if (!is_valid_length( err_out, typed_array.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index ceff371d..a9f24f2b 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -34,10 +34,10 @@ void PublicKey::Init( Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As(); js_exports.Set( Napi::String::New(env, "PUBLIC_KEY_LENGTH_COMPRESSED"), - Napi::Number::New(env, BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED)); + Napi::Number::New(env, public_key_length_compressed)); js_exports.Set( Napi::String::New(env, "PUBLIC_KEY_LENGTH_UNCOMPRESSED"), - Napi::Number::New(env, BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)); + Napi::Number::New(env, public_key_length_uncompressed)); } Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { @@ -49,8 +49,8 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, pk_bytes.ByteLength(), - BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED, - BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED)) { + public_key_length_compressed, + public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -122,7 +122,7 @@ PublicKey::PublicKey(const Napi::CallbackInfo &info) } Napi::Value PublicKey::Serialize(const Napi::CallbackInfo &info){ - BLST_TS_SERIALIZE_POINT(PUBLIC_KEY)} + BLST_TS_SERIALIZE_POINT(public_key)} Napi::Value PublicKey::KeyValidate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 8bafe7ea..83e6c85f 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -6,8 +6,8 @@ #include "blst.hpp" #include "napi.h" -#define BLST_TS_PUBLIC_KEY_LENGTH_COMPRESSED 48U -#define BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED 96U +static const size_t public_key_length_compressed = 48; +static const size_t public_key_length_uncompressed = 96; typedef struct { std::unique_ptr smart_pointer; @@ -41,10 +41,10 @@ class P1 : public P1Wrapper { blst::P1 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED]; + blst::byte out[public_key_length_uncompressed]; _point.serialize(out); // this should get std::move all the way into the P1 member value - blst::P1 point{out, BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED}; + blst::P1 point{out, public_key_length_uncompressed}; point.mult(rand_bytes, rand_bytes_length); return point; }; @@ -70,10 +70,10 @@ class P1Affine : public P1Wrapper { blst::P1 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED]; + blst::byte out[public_key_length_uncompressed]; _point.serialize(out); // this should get std::move all the way into the P1 member value - blst::P1 point{out, BLST_TS_PUBLIC_KEY_LENGTH_UNCOMPRESSED}; + blst::P1 point{out, public_key_length_uncompressed}; point.mult(rand_bytes, rand_bytes_length); return point; }; diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 1c837123..b502f600 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -36,7 +36,7 @@ void SecretKey::Init( .As() .Set( Napi::String::New(env, "SECRET_KEY_LENGTH"), - Napi::Number::New(env, BLST_TS_SECRET_KEY_LENGTH)); + Napi::Number::New(env, secret_key_length)); } Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { @@ -46,10 +46,10 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(ikm_value, ikm, "ikm") // Check for less than 32 bytes so consumers don't accidentally create // zero keys - if (ikm.ByteLength() < BLST_TS_SECRET_KEY_LENGTH) { + if (ikm.ByteLength() < secret_key_length) { std::ostringstream msg; msg << "ikm must be greater than or equal to " - << BLST_TS_SECRET_KEY_LENGTH << " bytes"; + << secret_key_length << " bytes"; Napi::TypeError::New(env, msg.str()).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -77,9 +77,9 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { // Check if key is zero and set flag if so. Several specs depend on this // check (signing with zero key). Do after building instead of checking // incoming bytes incase info changes the key - blst::byte key_bytes[BLST_TS_SECRET_KEY_LENGTH]; + blst::byte key_bytes[secret_key_length]; sk->key->to_bendian(key_bytes); - if (is_zero_bytes(key_bytes, 0, BLST_TS_SECRET_KEY_LENGTH)) { + if (is_zero_bytes(key_bytes, 0, secret_key_length)) { sk->is_zero_key = true; } @@ -93,7 +93,7 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(sk_bytes_value, sk_bytes, "skBytes") std::string err_out{"BLST_ERROR: skBytes"}; if (!is_valid_length( - err_out, sk_bytes.ByteLength(), BLST_TS_SECRET_KEY_LENGTH)) { + err_out, sk_bytes.ByteLength(), secret_key_length)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -134,7 +134,7 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) { Napi::EscapableHandleScope scope(env); Napi::Buffer serialized = - Napi::Buffer::New(env, BLST_TS_SECRET_KEY_LENGTH); + Napi::Buffer::New(env, secret_key_length); key->to_bendian(serialized.Data()); return scope.Escape(serialized); diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index 24171cdf..609b3e78 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -6,7 +6,7 @@ #include "public_key.h" #include "signature.h" -#define BLST_TS_SECRET_KEY_LENGTH 32U +static const size_t secret_key_length = 32; class SecretKey : public Napi::ObjectWrap { public: diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index 3e8188fb..a16c68b6 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -35,10 +35,10 @@ void Signature::Init( Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As(); js_exports.Set( Napi::String::New(env, "SIGNATURE_LENGTH_COMPRESSED"), - Napi::Number::New(env, BLST_TS_SIGNATURE_LENGTH_COMPRESSED)); + Napi::Number::New(env, signature_length_compressed)); js_exports.Set( Napi::String::New(env, "SIGNATURE_LENGTH_UNCOMPRESSED"), - Napi::Number::New(env, BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)); + Napi::Number::New(env, signature_length_uncompressed)); } Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { @@ -50,8 +50,8 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { if (!is_valid_length( err_out, sig_bytes.ByteLength(), - BLST_TS_SIGNATURE_LENGTH_COMPRESSED, - BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED)) { + signature_length_compressed, + signature_length_uncompressed)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -119,7 +119,7 @@ Signature::Signature(const Napi::CallbackInfo &info) } Napi::Value Signature::Serialize(const Napi::CallbackInfo &info){ - BLST_TS_SERIALIZE_POINT(SIGNATURE)} + BLST_TS_SERIALIZE_POINT(signature)} Napi::Value Signature::SigValidate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index d52eb4b8..58ac68ed 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -6,8 +6,8 @@ #include "blst.hpp" #include "napi.h" -#define BLST_TS_SIGNATURE_LENGTH_COMPRESSED 96U -#define BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED 192U +static const size_t signature_length_compressed = 96; +static const size_t signature_length_uncompressed = 192; typedef struct { std::unique_ptr smart_pointer; @@ -46,10 +46,10 @@ class P2 : public P2Wrapper { blst::P2 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED]; + blst::byte out[signature_length_uncompressed]; _point.serialize(out); // this should get std::move all the way into the P2 member value - blst::P2 point{out, BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED}; + blst::P2 point{out, signature_length_uncompressed}; point.mult(rand_bytes, rand_bytes_length); return point; }; @@ -83,10 +83,10 @@ class P2Affine : public P2Wrapper { blst::P2 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const final { - blst::byte out[BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED]; + blst::byte out[signature_length_uncompressed]; _point.serialize(out); // this should get std::move all the way into the P2 member value - blst::P2 point{out, BLST_TS_SIGNATURE_LENGTH_UNCOMPRESSED}; + blst::P2 point{out, signature_length_uncompressed}; point.mult(rand_bytes, rand_bytes_length); return point; }; From 86c4cadb8d53dd6ab7968fbf87c82fecce2e7b91 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 20:46:32 +0800 Subject: [PATCH 19/32] feat: mark main classes as final --- rebuild/src/public_key.h | 2 +- rebuild/src/secret_key.h | 2 +- rebuild/src/signature.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 83e6c85f..c63a7622 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -83,7 +83,7 @@ class P1Affine : public P1Wrapper { } }; -class PublicKey : public Napi::ObjectWrap { +class PublicKey final : public Napi::ObjectWrap { public: std::unique_ptr point; diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index 609b3e78..22ac365d 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -8,7 +8,7 @@ static const size_t secret_key_length = 32; -class SecretKey : public Napi::ObjectWrap { +class SecretKey final : public Napi::ObjectWrap { public: std::unique_ptr key; bool is_zero_key; diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 58ac68ed..f7dd0099 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -106,7 +106,7 @@ class P2Affine : public P2Wrapper { }; }; -class Signature : public Napi::ObjectWrap { +class Signature final : public Napi::ObjectWrap { public: std::unique_ptr point; From f17928eaa0d86ab16f021e37ed33a866bf136e1e Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Tue, 12 Mar 2024 21:19:59 +0800 Subject: [PATCH 20/32] feat: create blst_ts namespace --- rebuild/src/addon.cc | 10 +-- rebuild/src/addon.h | 11 +-- rebuild/src/functions.cc | 146 +++++++++++++++++++------------------- rebuild/src/public_key.cc | 7 +- rebuild/src/public_key.h | 2 + rebuild/src/secret_key.cc | 15 ++-- rebuild/src/secret_key.h | 2 + rebuild/src/signature.cc | 4 +- rebuild/src/signature.h | 2 + 9 files changed, 108 insertions(+), 91 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index 601c3800..a3974677 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -1,5 +1,6 @@ #include "addon.h" +namespace blst_ts { /** * Checks if a specified range of bytes within a byte array consists only of * zeros. @@ -68,6 +69,7 @@ bool is_valid_length( error_out.append(" bytes long"); return false; } +} // namespace blst_ts BlstTsAddon::BlstTsAddon(Napi::Env env, Napi::Object exports) : _blst_error_strings{ @@ -89,10 +91,10 @@ BlstTsAddon::BlstTsAddon(Napi::Env env, Napi::Object exports) { InstanceValue("BLST_CONSTANTS", js_constants, napi_enumerable), }); - SecretKey::Init(env, exports, this); - PublicKey::Init(env, exports, this); - Signature::Init(env, exports, this); - functions::init(env, exports); + blst_ts::SecretKey::Init(env, exports, this); + blst_ts::PublicKey::Init(env, exports, this); + blst_ts::Signature::Init(env, exports, this); + blst_ts_functions::init(env, exports); env.SetInstanceData(this); // Check that openssl PRNG is seeded diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 6f49f07a..4f12256e 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -12,6 +12,7 @@ #include "blst.hpp" #include "napi.h" +namespace blst_ts { #define BLST_TS_RANDOM_BYTES_LENGTH 8U #define BLST_TS_FUNCTION_PREAMBLE(info, env, module) \ @@ -33,8 +34,8 @@ } \ Napi::Buffer serialized = Napi::Buffer::New( \ env, \ - compressed ? snake_case_name##_length_compressed \ - : snake_case_name##_length_uncompressed); \ + compressed ? snake_case_name##_length_compressed \ + : snake_case_name##_length_uncompressed); \ point->Serialize(compressed, serialized.Data()); \ return scope.Escape(serialized); @@ -74,8 +75,6 @@ Napi::Uint8Array arr_name = \ arr_name##_array.As>(); -class BlstTsAddon; - typedef enum { Affine, Jacobian } CoordType; /** @@ -110,14 +109,16 @@ bool is_valid_length( size_t byte_length, size_t length1, size_t length2 = 0) noexcept; +} // namespace blst_ts +class BlstTsAddon; /** * Circular dependency if these are moved up to the top of the file. */ #include "public_key.h" #include "secret_key.h" #include "signature.h" -namespace functions { +namespace blst_ts_functions { void init(const Napi::Env &env, Napi::Object &exports); } diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index db214cff..812c3bed 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -28,15 +28,15 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { if (val.IsTypedArray()) { Napi::Uint8Array typed_array = val.As(); std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - public_key_length_compressed, - public_key_length_uncompressed)) { + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); has_error = true; - } else if (is_zero_bytes( + } else if (blst_ts::is_zero_bytes( typed_array.Data(), 0, typed_array.ByteLength())) { @@ -49,8 +49,8 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { blst::P1{typed_array.Data(), typed_array.ByteLength()}); } } else { - PublicKey *to_aggregate = - PublicKey::Unwrap(val.As()); + blst_ts::PublicKey *to_aggregate = + blst_ts::PublicKey::Unwrap(val.As()); to_aggregate->point->AddTo(aggregate); } if (has_error) { @@ -70,8 +70,9 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { } } - return scope.Escape(module->public_key_ctr.New( - {Napi::External::New(env, new P1{std::move(aggregate)})})); + return scope.Escape( + module->public_key_ctr.New({Napi::External::New( + env, new blst_ts::P1{std::move(aggregate)})})); } Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { @@ -99,11 +100,11 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { if (val.IsTypedArray()) { Napi::Uint8Array typed_array = val.As(); std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - signature_length_compressed, - signature_length_uncompressed)) { + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); has_error = true; @@ -112,8 +113,8 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { blst::P2{typed_array.Data(), typed_array.ByteLength()}); } } else { - Signature *to_aggregate = - Signature::Unwrap(val.As()); + blst_ts::Signature *to_aggregate = + blst_ts::Signature::Unwrap(val.As()); to_aggregate->point->AddTo(aggregate); } if (has_error) { @@ -133,23 +134,24 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { } } - return scope.Escape(module->signature_ctr.New( - {Napi::External::New(env, new P2{std::move(aggregate)})})); + return scope.Escape( + module->signature_ctr.New({Napi::External::New( + env, new blst_ts::P2{std::move(aggregate)})})); } Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) try { Napi::Value sig_val = info[2]; - P2AffineGroup sig_point; + blst_ts::P2AffineGroup sig_point; if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - signature_length_compressed, - signature_length_uncompressed)) { + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } else { @@ -158,8 +160,8 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { sig_point.raw_point = sig_point.smart_pointer.get(); } } else { - Signature *to_verify = - Signature::Unwrap(sig_val.As()); + blst_ts::Signature *to_verify = + blst_ts::Signature::Unwrap(sig_val.As()); sig_point = to_verify->point->AsAffine(); } @@ -209,19 +211,19 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "msg") Napi::Value pk_val = pk_array[i]; - P1AffineGroup pk_point; + blst_ts::P1AffineGroup pk_point; if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - public_key_length_compressed, - public_key_length_uncompressed)) { + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); - } else if (is_zero_bytes( + } else if (blst_ts::is_zero_bytes( typed_array.Data(), 0, typed_array.ByteLength())) { @@ -235,8 +237,8 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { pk_point.raw_point = pk_point.smart_pointer.get(); } } else { - PublicKey *to_verify = - PublicKey::Unwrap(pk_val.As()); + blst_ts::PublicKey *to_verify = + blst_ts::PublicKey::Unwrap(pk_val.As()); pk_point = to_verify->point->AsAffine(); } @@ -263,7 +265,7 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { } typedef struct { - P1AffineGroup pk_point; + blst_ts::P1AffineGroup pk_point; uint8_t *msg; size_t msg_len; } AggregateVerifySet; @@ -286,11 +288,11 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - signature_length_compressed, - signature_length_uncompressed)) { + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; @@ -302,8 +304,8 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { m_sig_point.raw_point = m_sig_point.smart_pointer.get(); } } else { - Signature *to_verify = - Signature::Unwrap(sig_val.As()); + blst_ts::Signature *to_verify = + blst_ts::Signature::Unwrap(sig_val.As()); m_sig_point = to_verify->point->AsAffine(); } @@ -356,7 +358,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { m_sets.reserve(pk_array_length); for (uint32_t i = 0; i < pk_array_length; i++) { - m_sets.push_back({P1AffineGroup{}, nullptr, 0}); + m_sets.push_back({blst_ts::P1AffineGroup{}, nullptr, 0}); Napi::Value msg_value = msgs_array[i]; BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "msg") @@ -368,16 +370,16 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { Napi::Uint8Array typed_array = pk_val.As(); std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - public_key_length_compressed, - public_key_length_uncompressed)) { + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_is_invalid = true; return; - } else if (is_zero_bytes( + } else if (blst_ts::is_zero_bytes( typed_array.Data(), 0, typed_array.ByteLength())) { @@ -395,8 +397,8 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { m_sets[i].pk_point.smart_pointer.get(); } } else { - PublicKey *to_verify = - PublicKey::Unwrap(pk_val.As()); + blst_ts::PublicKey *to_verify = + blst_ts::PublicKey::Unwrap(pk_val.As()); m_sets[i].pk_point = to_verify->point->AsAffine(); } } @@ -443,7 +445,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { private: BlstTsAddon *m_module; std::unique_ptr m_ctx; - P2AffineGroup m_sig_point; + blst_ts::P2AffineGroup m_sig_point; std::vector m_sets; bool m_is_invalid; bool m_result; @@ -494,19 +496,19 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "message") Napi::Value pk_val = set.Get("publicKey"); - P1AffineGroup pk_point; + blst_ts::P1AffineGroup pk_point; if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - public_key_length_compressed, - public_key_length_uncompressed)) { + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); - } else if (is_zero_bytes( + } else if (blst_ts::is_zero_bytes( typed_array.Data(), 0, typed_array.ByteLength())) { @@ -520,21 +522,21 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { pk_point.raw_point = pk_point.smart_pointer.get(); } } else { - PublicKey *to_verify = - PublicKey::Unwrap(pk_val.As()); + blst_ts::PublicKey *to_verify = + blst_ts::PublicKey::Unwrap(pk_val.As()); pk_point = to_verify->point->AsAffine(); } Napi::Value sig_val = set.Get("signature"); - P2AffineGroup sig_point; + blst_ts::P2AffineGroup sig_point; if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - signature_length_compressed, - signature_length_uncompressed)) { + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); return env.Undefined(); @@ -544,8 +546,8 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { sig_point.raw_point = sig_point.smart_pointer.get(); } } else { - Signature *to_verify = - Signature::Unwrap(sig_val.As()); + blst_ts::Signature *to_verify = + blst_ts::Signature::Unwrap(sig_val.As()); sig_point = to_verify->point->AsAffine(); } @@ -572,8 +574,8 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { } typedef struct { - P1AffineGroup pk_point; - P2AffineGroup sig_point; + blst_ts::P1AffineGroup pk_point; + blst_ts::P2AffineGroup sig_point; uint8_t *msg; size_t msg_len; } SignatureSet; @@ -617,8 +619,8 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "message") m_sets.push_back( - {P1AffineGroup{}, - P2AffineGroup{}, + {blst_ts::P1AffineGroup{}, + blst_ts::P2AffineGroup{}, msg.Data(), msg.ByteLength()}); @@ -627,16 +629,16 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { Napi::Uint8Array typed_array = pk_val.As(); std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - public_key_length_compressed, - public_key_length_uncompressed)) { + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; return; - } else if (is_zero_bytes( + } else if (blst_ts::is_zero_bytes( typed_array.Data(), 0, typed_array.ByteLength())) { @@ -654,8 +656,8 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { m_sets[i].pk_point.smart_pointer.get(); } } else { - PublicKey *to_verify = - PublicKey::Unwrap(pk_val.As()); + blst_ts::PublicKey *to_verify = + blst_ts::PublicKey::Unwrap(pk_val.As()); m_sets[i].pk_point = to_verify->point->AsAffine(); } @@ -664,11 +666,11 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { Napi::Uint8Array typed_array = sig_val.As(); std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, typed_array.ByteLength(), - signature_length_compressed, - signature_length_uncompressed)) { + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); m_has_error = true; @@ -681,8 +683,8 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { m_sets[i].sig_point.smart_pointer.get(); } } else { - Signature *to_verify = - Signature::Unwrap(sig_val.As()); + blst_ts::Signature *to_verify = + blst_ts::Signature::Unwrap(sig_val.As()); m_sets[i].sig_point = to_verify->point->AsAffine(); } } @@ -753,7 +755,7 @@ Napi::Value AsyncVerifyMultipleAggregateSignatures( } // anonymous namespace -namespace functions { +namespace blst_ts_functions { void init(const Napi::Env &env, Napi::Object &exports) { exports.Set( Napi::String::New(env, "aggregatePublicKeys"), @@ -774,4 +776,4 @@ void init(const Napi::Env &env, Napi::Object &exports) { Napi::String::New(env, "asyncVerifyMultipleAggregateSignatures"), Napi::Function::New(env, AsyncVerifyMultipleAggregateSignatures)); } -} // namespace Functions +} // namespace functions diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index a9f24f2b..ad949a0d 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -1,5 +1,6 @@ #include "public_key.h" +namespace blst_ts { void PublicKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope(env); @@ -46,7 +47,7 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(pk_bytes_val, pk_bytes, "pkBytes") std::string err_out{"BLST_ERROR: pkBytes"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, pk_bytes.ByteLength(), public_key_length_compressed, @@ -101,7 +102,8 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { .ThrowAsJavaScriptException(); return env.Undefined(); } catch (...) { - Napi::Error::New(env, "BLST_ERROR: Unknown error deserializing PublicKey") + Napi::Error::New( + env, "BLST_ERROR: Unknown error deserializing PublicKey") .ThrowAsJavaScriptException(); return env.Undefined(); } @@ -156,3 +158,4 @@ Napi::Value PublicKey::MultiplyBy(const Napi::CallbackInfo &info) { return scope.Escape(pk_obj); } +} // namespace blst_ts diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index c63a7622..6f9cb2c5 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -6,6 +6,7 @@ #include "blst.hpp" #include "napi.h" +namespace blst_ts { static const size_t public_key_length_compressed = 48; static const size_t public_key_length_uncompressed = 96; @@ -95,3 +96,4 @@ class PublicKey final : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; +} // namespace blst_ts \ No newline at end of file diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index b502f600..3dfa8037 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -1,5 +1,6 @@ #include "secret_key.h" +namespace blst_ts { void SecretKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope(env); @@ -45,11 +46,11 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(ikm_value, ikm, "ikm") // Check for less than 32 bytes so consumers don't accidentally create - // zero keys + // zero keys if (ikm.ByteLength() < secret_key_length) { std::ostringstream msg; - msg << "ikm must be greater than or equal to " - << secret_key_length << " bytes"; + msg << "ikm must be greater than or equal to " << secret_key_length + << " bytes"; Napi::TypeError::New(env, msg.str()).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -79,7 +80,7 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { // incoming bytes incase info changes the key blst::byte key_bytes[secret_key_length]; sk->key->to_bendian(key_bytes); - if (is_zero_bytes(key_bytes, 0, secret_key_length)) { + if (blst_ts::is_zero_bytes(key_bytes, 0, secret_key_length)) { sk->is_zero_key = true; } @@ -92,8 +93,7 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { Napi::Value sk_bytes_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(sk_bytes_value, sk_bytes, "skBytes") std::string err_out{"BLST_ERROR: skBytes"}; - if (!is_valid_length( - err_out, sk_bytes.ByteLength(), secret_key_length)) { + if (!blst_ts::is_valid_length(err_out, sk_bytes.ByteLength(), secret_key_length)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -105,7 +105,7 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { // Check if key is zero and set flag if so. Several specs depend on this // check (signing with zero key) - if (is_zero_bytes(sk_bytes.Data(), 0, sk_bytes.ByteLength())) { + if (blst_ts::is_zero_bytes(sk_bytes.Data(), 0, sk_bytes.ByteLength())) { sk->is_zero_key = true; } // Deserialize key @@ -169,3 +169,4 @@ Napi::Value SecretKey::Sign(const Napi::CallbackInfo &info) { return scope.Escape(sig_obj); } +} // namespace blst_ts diff --git a/rebuild/src/secret_key.h b/rebuild/src/secret_key.h index 22ac365d..a3e9f62a 100644 --- a/rebuild/src/secret_key.h +++ b/rebuild/src/secret_key.h @@ -6,6 +6,7 @@ #include "public_key.h" #include "signature.h" +namespace blst_ts { static const size_t secret_key_length = 32; class SecretKey final : public Napi::ObjectWrap { @@ -21,3 +22,4 @@ class SecretKey final : public Napi::ObjectWrap { Napi::Value ToPublicKey(const Napi::CallbackInfo &info); Napi::Value Sign(const Napi::CallbackInfo &info); }; +} // namespace blst_ts diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index a16c68b6..b55f2b07 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -1,5 +1,6 @@ #include "signature.h" +namespace blst_ts { void Signature::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope( @@ -47,7 +48,7 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { BLST_TS_UNWRAP_UINT_8_ARRAY(sig_bytes_val, sig_bytes, "sigBytes") std::string err_out{"BLST_ERROR: sigBytes"}; - if (!is_valid_length( + if (!blst_ts::is_valid_length( err_out, sig_bytes.ByteLength(), signature_length_compressed, @@ -149,3 +150,4 @@ Napi::Value Signature::MultiplyBy(const Napi::CallbackInfo &info) { return scope.Escape(sig_obj); } +} // namespace blst_ts \ No newline at end of file diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index f7dd0099..77fb0f3e 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -6,6 +6,7 @@ #include "blst.hpp" #include "napi.h" +namespace blst_ts { static const size_t signature_length_compressed = 96; static const size_t signature_length_uncompressed = 192; @@ -118,3 +119,4 @@ class Signature final : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; +} // namespace blst_ts \ No newline at end of file From e5010f83d633dd2326d09c40ab98d929a55652cc Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Thu, 14 Mar 2024 17:16:23 +0800 Subject: [PATCH 21/32] refactor: remove IS_INFINITY macro --- rebuild/src/addon.h | 5 ----- rebuild/src/public_key.cc | 7 +++++-- rebuild/src/signature.cc | 9 ++++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 4f12256e..49eda06b 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -20,11 +20,6 @@ namespace blst_ts { Napi::EscapableHandleScope scope(env); \ BlstTsAddon *module = env.GetInstanceData(); -#define BLST_TS_IS_INFINITY \ - Napi::Env env = info.Env(); \ - Napi::EscapableHandleScope scope(env); \ - return scope.Escape(Napi::Boolean::New(env, point->IsInfinite())); - #define BLST_TS_SERIALIZE_POINT(snake_case_name) \ Napi::Env env = info.Env(); \ Napi::EscapableHandleScope scope(env); \ diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index ad949a0d..a279d6dd 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -140,8 +140,11 @@ Napi::Value PublicKey::KeyValidate(const Napi::CallbackInfo &info) { return info.Env().Undefined(); } -Napi::Value PublicKey::IsInfinity(const Napi::CallbackInfo &info){ - BLST_TS_IS_INFINITY} +Napi::Value PublicKey::IsInfinity(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::EscapableHandleScope scope(env); + return scope.Escape(Napi::Boolean::New(env, point->IsInfinite())); +} Napi::Value PublicKey::MultiplyBy(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index b55f2b07..0dda6ec6 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -132,8 +132,11 @@ Napi::Value Signature::SigValidate(const Napi::CallbackInfo &info) { return env.Undefined(); } -Napi::Value Signature::IsInfinity(const Napi::CallbackInfo &info){ - BLST_TS_IS_INFINITY} +Napi::Value Signature::IsInfinity(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::EscapableHandleScope scope(env); + return scope.Escape(Napi::Boolean::New(env, point->IsInfinite())); +} Napi::Value Signature::MultiplyBy(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) @@ -150,4 +153,4 @@ Napi::Value Signature::MultiplyBy(const Napi::CallbackInfo &info) { return scope.Escape(sig_obj); } -} // namespace blst_ts \ No newline at end of file +} // namespace blst_ts From 658cda34b6683f4b4b9e37a14a4f18a46da069fa Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Thu, 14 Mar 2024 17:36:26 +0800 Subject: [PATCH 22/32] refactor: move wrapper function definitions to .cc --- rebuild/src/public_key.cc | 40 ++++++++++++++++++++++++++ rebuild/src/public_key.h | 49 +++++++------------------------- rebuild/src/signature.cc | 59 +++++++++++++++++++++++++++++++++++++++ rebuild/src/signature.h | 59 ++++++++------------------------------- 4 files changed, 120 insertions(+), 87 deletions(-) diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index a279d6dd..9a1c4d52 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -1,6 +1,46 @@ #include "public_key.h" namespace blst_ts { + +void P1::Serialize(bool compress, blst::byte *out) const { + compress ? _point.compress(out) : _point.serialize(out); +} + +P1AffineGroup P1::AsAffine() { + P1AffineGroup group{std::make_unique(_point), nullptr}; + group.raw_point = group.smart_pointer.get(); + return group; +} + +blst::P1 P1::MultiplyBy( + const blst::byte *rand_bytes, const size_t rand_bytes_length) const { + blst::byte out[public_key_length_uncompressed]; + _point.serialize(out); + // this should get std::move all the way into the P1 member value + blst::P1 point{out, public_key_length_uncompressed}; + point.mult(rand_bytes, rand_bytes_length); + return point; +} + +void P1Affine::Serialize(bool compress, blst::byte *out) const { + compress ? _point.compress(out) : _point.serialize(out); +} + +P1AffineGroup P1Affine::AsAffine() { + P1AffineGroup group{nullptr, &_point}; + return group; +} + +blst::P1 P1Affine::MultiplyBy( + const blst::byte *rand_bytes, const size_t rand_bytes_length) const { + blst::byte out[public_key_length_uncompressed]; + _point.serialize(out); + // this should get std::move all the way into the P1 member value + blst::P1 point{out, public_key_length_uncompressed}; + point.mult(rand_bytes, rand_bytes_length); + return point; +} + void PublicKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope(env); diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 6f9cb2c5..0513f0e9 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -1,10 +1,6 @@ #pragma once -#include - #include "addon.h" -#include "blst.hpp" -#include "napi.h" namespace blst_ts { static const size_t public_key_length_compressed = 48; @@ -20,11 +16,11 @@ class P1Wrapper { virtual ~P1Wrapper() = default; virtual bool IsInfinite() const = 0; virtual bool InGroup() const = 0; - virtual void Serialize(bool compress, blst::byte *out) const = 0; virtual void AddTo(blst::P1 &point) const = 0; + virtual void Serialize(bool compress, blst::byte *out) const = 0; + virtual P1AffineGroup AsAffine() = 0; virtual blst::P1 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const = 0; - virtual P1AffineGroup AsAffine() = 0; }; class P1 : public P1Wrapper { @@ -35,25 +31,12 @@ class P1 : public P1Wrapper { P1(blst::P1 point) : _point(std::move(point)) {} bool IsInfinite() const final { return _point.is_inf(); } bool InGroup() const final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) const final { - compress ? _point.compress(out) : _point.serialize(out); - } void AddTo(blst::P1 &point) const final { point.add(_point); }; + void Serialize(bool compress, blst::byte *out) const final; + P1AffineGroup AsAffine() final; blst::P1 MultiplyBy( const blst::byte *rand_bytes, - const size_t rand_bytes_length) const final { - blst::byte out[public_key_length_uncompressed]; - _point.serialize(out); - // this should get std::move all the way into the P1 member value - blst::P1 point{out, public_key_length_uncompressed}; - point.mult(rand_bytes, rand_bytes_length); - return point; - }; - P1AffineGroup AsAffine() final { - P1AffineGroup group{std::make_unique(_point), nullptr}; - group.raw_point = group.smart_pointer.get(); - return group; - }; + const size_t rand_bytes_length) const final; }; class P1Affine : public P1Wrapper { @@ -64,24 +47,12 @@ class P1Affine : public P1Wrapper { P1Affine(blst::P1_Affine point) : _point(std::move(point)) {} bool IsInfinite() const final { return _point.is_inf(); } bool InGroup() const final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) const final { - compress ? _point.compress(out) : _point.serialize(out); - } - void AddTo(blst::P1 &point) const final { point.add(_point); }; + void AddTo(blst::P1 &point) const final { point.add(_point); } + void Serialize(bool compress, blst::byte *out) const final; + P1AffineGroup AsAffine() final; blst::P1 MultiplyBy( const blst::byte *rand_bytes, - const size_t rand_bytes_length) const final { - blst::byte out[public_key_length_uncompressed]; - _point.serialize(out); - // this should get std::move all the way into the P1 member value - blst::P1 point{out, public_key_length_uncompressed}; - point.mult(rand_bytes, rand_bytes_length); - return point; - }; - P1AffineGroup AsAffine() final { - P1AffineGroup group{nullptr, &_point}; - return group; - } + const size_t rand_bytes_length) const final; }; class PublicKey final : public Napi::ObjectWrap { @@ -96,4 +67,4 @@ class PublicKey final : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; -} // namespace blst_ts \ No newline at end of file +} // namespace blst_ts diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index 0dda6ec6..a9227a71 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -1,6 +1,65 @@ #include "signature.h" namespace blst_ts { +void P2::Serialize(bool compress, blst::byte *out) const { + compress ? _point.compress(out) : _point.serialize(out); +} + +P2AffineGroup P2::AsAffine() { + P2AffineGroup group{std::make_unique(_point), nullptr}; + group.raw_point = group.smart_pointer.get(); + return group; +} + +blst::P2 P2::MultiplyBy( + const blst::byte *rand_bytes, const size_t rand_bytes_length) const { + blst::byte out[signature_length_uncompressed]; + _point.serialize(out); + // this should get std::move all the way into the P2 member value + blst::P2 point{out, signature_length_uncompressed}; + point.mult(rand_bytes, rand_bytes_length); + return point; +} + +void P2::Sign( + const blst::SecretKey &key, + const uint8_t *msg, + const size_t msg_length, + const std::string &dst) { + _point.hash_to(msg, msg_length, dst); + _point.sign_with(key); +} + +void P2Affine::Serialize(bool compress, blst::byte *out) const { + compress ? _point.compress(out) : _point.serialize(out); +} + +P2AffineGroup P2Affine::AsAffine() { + P2AffineGroup group{nullptr, &_point}; + return group; +} + +blst::P2 P2Affine::MultiplyBy( + const blst::byte *rand_bytes, const size_t rand_bytes_length) const { + blst::byte out[signature_length_uncompressed]; + _point.serialize(out); + // this should get std::move all the way into the P2 member value + blst::P2 point{out, signature_length_uncompressed}; + point.mult(rand_bytes, rand_bytes_length); + return point; +} + +void P2Affine::Sign( + const blst::SecretKey &key, + const uint8_t *msg, + const size_t msg_length, + const std::string &dst) { + blst::P2 jacobian{_point}; + jacobian.hash_to(msg, msg_length, dst); + jacobian.sign_with(key); + _point = jacobian.to_affine(); +} + void Signature::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { Napi::HandleScope scope( diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index 77fb0f3e..c3dd1dc4 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -1,10 +1,6 @@ #pragma once -#include - #include "addon.h" -#include "blst.hpp" -#include "napi.h" namespace blst_ts { static const size_t signature_length_compressed = 96; @@ -20,11 +16,11 @@ class P2Wrapper { virtual ~P2Wrapper() = default; virtual bool IsInfinite() const = 0; virtual bool InGroup() const = 0; - virtual void Serialize(bool compress, blst::byte *out) const = 0; virtual void AddTo(blst::P2 &point) const = 0; + virtual void Serialize(bool compress, blst::byte *out) const = 0; + virtual P2AffineGroup AsAffine() = 0; virtual blst::P2 MultiplyBy( const blst::byte *rand_bytes, const size_t rand_bytes_length) const = 0; - virtual P2AffineGroup AsAffine() = 0; virtual void Sign( const blst::SecretKey &key, const uint8_t *msg, @@ -40,33 +36,17 @@ class P2 : public P2Wrapper { P2(blst::P2 point) : _point(std::move(point)) {} bool IsInfinite() const final { return _point.is_inf(); } bool InGroup() const final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) const final { - compress ? _point.compress(out) : _point.serialize(out); - } void AddTo(blst::P2 &point) const final { point.add(_point); }; + void Serialize(bool compress, blst::byte *out) const final; + P2AffineGroup AsAffine() final; blst::P2 MultiplyBy( const blst::byte *rand_bytes, - const size_t rand_bytes_length) const final { - blst::byte out[signature_length_uncompressed]; - _point.serialize(out); - // this should get std::move all the way into the P2 member value - blst::P2 point{out, signature_length_uncompressed}; - point.mult(rand_bytes, rand_bytes_length); - return point; - }; - P2AffineGroup AsAffine() final { - P2AffineGroup group{std::make_unique(_point), nullptr}; - group.raw_point = group.smart_pointer.get(); - return group; - }; + const size_t rand_bytes_length) const final; void Sign( const blst::SecretKey &key, const uint8_t *msg, const size_t msg_length, - const std::string &dst) final { - _point.hash_to(msg, msg_length, dst); - _point.sign_with(key); - }; + const std::string &dst) final; }; class P2Affine : public P2Wrapper { @@ -77,34 +57,17 @@ class P2Affine : public P2Wrapper { P2Affine(blst::P2_Affine point) : _point(std::move(point)) {} bool IsInfinite() const final { return _point.is_inf(); } bool InGroup() const final { return _point.in_group(); } - void Serialize(bool compress, blst::byte *out) const final { - compress ? _point.compress(out) : _point.serialize(out); - } void AddTo(blst::P2 &point) const final { point.add(_point); }; + void Serialize(bool compress, blst::byte *out) const final; + P2AffineGroup AsAffine() final; blst::P2 MultiplyBy( const blst::byte *rand_bytes, - const size_t rand_bytes_length) const final { - blst::byte out[signature_length_uncompressed]; - _point.serialize(out); - // this should get std::move all the way into the P2 member value - blst::P2 point{out, signature_length_uncompressed}; - point.mult(rand_bytes, rand_bytes_length); - return point; - }; - P2AffineGroup AsAffine() final { - P2AffineGroup group{nullptr, &_point}; - return group; - } + const size_t rand_bytes_length) const final; void Sign( const blst::SecretKey &key, const uint8_t *msg, const size_t msg_length, - const std::string &dst) final { - blst::P2 jacobian{_point}; - jacobian.hash_to(msg, msg_length, dst); - jacobian.sign_with(key); - _point = jacobian.to_affine(); - }; + const std::string &dst) final; }; class Signature final : public Napi::ObjectWrap { @@ -119,4 +82,4 @@ class Signature final : public Napi::ObjectWrap { Napi::Value IsInfinity(const Napi::CallbackInfo &info); Napi::Value MultiplyBy(const Napi::CallbackInfo &info); }; -} // namespace blst_ts \ No newline at end of file +} // namespace blst_ts From cba443eb047ba05e779f029962fc443413d574e4 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 16:31:29 +0800 Subject: [PATCH 23/32] feat: break out info[1] in FromKeygen --- rebuild/src/secret_key.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 3dfa8037..77ce0199 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -43,6 +43,7 @@ void SecretKey::Init( Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { BLST_TS_FUNCTION_PREAMBLE(info, env, module) Napi::Value ikm_value = info[0]; + Napi::Value info_value = info[1]; BLST_TS_UNWRAP_UINT_8_ARRAY(ikm_value, ikm, "ikm") // Check for less than 32 bytes so consumers don't accidentally create @@ -61,8 +62,8 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { SecretKey *sk = SecretKey::Unwrap(wrapped); // If `info` string is passed use it otherwise use default without - if (!info[1].IsUndefined()) { - if (!info[1].IsString()) { + if (!info_value.IsUndefined()) { + if (!info_value.IsString()) { Napi::TypeError::New(env, "BLST_ERROR: info must be a string") .ThrowAsJavaScriptException(); return env.Undefined(); @@ -70,7 +71,7 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { sk->key->keygen( ikm.Data(), ikm.ByteLength(), - info[1].As().Utf8Value()); + info_value.As().Utf8Value()); } else { sk->key->keygen(ikm.Data(), ikm.ByteLength()); } From 2e41be9123e8d450bbe04e269d340008f10c876c Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 16:35:23 +0800 Subject: [PATCH 24/32] refactor: directly set is_zero_bytes --- rebuild/src/secret_key.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 77ce0199..0a291c5c 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -80,10 +80,9 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { // check (signing with zero key). Do after building instead of checking // incoming bytes incase info changes the key blst::byte key_bytes[secret_key_length]; + sk->key->to_bendian(key_bytes); - if (blst_ts::is_zero_bytes(key_bytes, 0, secret_key_length)) { - sk->is_zero_key = true; - } + sk->is_zero_key = blst_ts::is_zero_bytes(key_bytes, 0, secret_key_length); return scope.Escape(wrapped); } @@ -94,7 +93,8 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { Napi::Value sk_bytes_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(sk_bytes_value, sk_bytes, "skBytes") std::string err_out{"BLST_ERROR: skBytes"}; - if (!blst_ts::is_valid_length(err_out, sk_bytes.ByteLength(), secret_key_length)) { + if (!blst_ts::is_valid_length( + err_out, sk_bytes.ByteLength(), secret_key_length)) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } @@ -106,9 +106,9 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { // Check if key is zero and set flag if so. Several specs depend on this // check (signing with zero key) - if (blst_ts::is_zero_bytes(sk_bytes.Data(), 0, sk_bytes.ByteLength())) { - sk->is_zero_key = true; - } + sk->is_zero_key = + blst_ts::is_zero_bytes(sk_bytes.Data(), 0, sk_bytes.ByteLength()); + // Deserialize key sk->key->from_bendian(sk_bytes.Data()); From 2e11c9e548e8aad4c21354cedd4416baff8652ef Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 16:38:51 +0800 Subject: [PATCH 25/32] fix: remove erroneous return and add comment about is_external for secretkey creation --- rebuild/src/secret_key.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 0a291c5c..f4ccffe9 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -56,6 +56,8 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { return env.Undefined(); } + // just need to pass some value to the External constructor. debug builds + // have hard crash check for nullptr bool is_external = true; Napi::Object wrapped = module->secret_key_ctr.New( {Napi::External::New(env, &is_external)}); @@ -98,7 +100,9 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } - + + // just need to pass some value to the External constructor. debug builds + // have hard crash check for nullptr bool is_external = true; Napi::Object wrapped = module->secret_key_ctr.New( {Napi::External::New(env, &is_external)}); @@ -126,7 +130,6 @@ SecretKey::SecretKey(const Napi::CallbackInfo &info) if (!info[0].IsExternal()) { Napi::Error::New(env, "SecretKey constructor is private") .ThrowAsJavaScriptException(); - return; } } From 2c85cf1847cfdf318c72998f93e437aa0fcfca6b Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 16:47:45 +0800 Subject: [PATCH 26/32] fix: catch by const ref --- rebuild/src/signature.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index a9227a71..0a7324c8 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -157,10 +157,15 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { module->signature_ctr.New({Napi::External::New( env, new P2{blst::P2{sig_bytes.Data(), sig_bytes.ByteLength()}})})); - } catch (blst::BLST_ERROR err) { + } catch (const blst::BLST_ERROR &err) { Napi::RangeError::New(env, module->GetBlstErrorString(err)) .ThrowAsJavaScriptException(); return env.Undefined(); + } catch (...) { + Napi::Error::New( + env, "BLST_ERROR: Unknown error deserializing PublicKey") + .ThrowAsJavaScriptException(); + return env.Undefined(); } } From 5de98fa9dd7e22485d29362dd4da078c14de9093 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 17:10:29 +0800 Subject: [PATCH 27/32] fix: remove ostringstream --- rebuild/src/addon.h | 1 - rebuild/src/functions.cc | 76 ++++++++++++++++++++++----------------- rebuild/src/secret_key.cc | 13 ++++--- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 49eda06b..bd33621d 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index 812c3bed..d49d0335 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -1,6 +1,6 @@ #include "addon.h" -#include "blst.hpp" -#include "napi.h" + +using namespace std::string_literals; namespace { Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { @@ -57,15 +57,18 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { return env.Undefined(); } } catch (const blst::BLST_ERROR &err) { - std::ostringstream msg; - msg << "BLST_ERROR::" << module->GetBlstErrorString(err) - << ": Invalid key at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + "BLST_ERROR::"s + module->GetBlstErrorString(err) + + ": Invalid key at index "s + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } catch (...) { - std::ostringstream msg; - msg << "BLST_ERROR: Invalid PublicKeyArg at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + "BLST_ERROR: Invalid PublicKeyArg at index "s + + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } } @@ -121,15 +124,18 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { return env.Undefined(); } } catch (const blst::BLST_ERROR &err) { - std::ostringstream msg; - msg << module->GetBlstErrorString(err) - << " - Invalid signature at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + module->GetBlstErrorString(err) + + " - Invalid signature at index "s + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } catch (...) { - std::ostringstream msg; - msg << "BLST_ERROR - Invalid SignatureArg at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + "BLST_ERROR - Invalid SignatureArg at index "s + + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } } @@ -248,10 +254,12 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { msg.Data(), msg.ByteLength()); if (err != blst::BLST_ERROR::BLST_SUCCESS) { - std::ostringstream msg; - msg << "BLST_ERROR::" << module->GetBlstErrorString(err) - << ": Invalid verification aggregate at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + "BLST_ERROR::"s + module->GetBlstErrorString(err) + + ": Invalid verification aggregate at index "s + + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } } @@ -424,10 +432,10 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { m_sets[i].msg, m_sets[i].msg_len); if (err != blst::BLST_ERROR::BLST_SUCCESS) { - std::ostringstream msg; - msg << "BLST_ERROR::" << m_module->GetBlstErrorString(err) - << ": Invalid verification aggregate at index " << i; - SetError(msg.str()); + SetError( + "BLST_ERROR::"s + m_module->GetBlstErrorString(err) + + ": Invalid verification aggregate at index "s + + std::to_string(i)); return; } } @@ -559,10 +567,12 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { msg.Data(), msg.ByteLength()); if (err != blst::BLST_ERROR::BLST_SUCCESS) { - std::ostringstream msg; - msg << module->GetBlstErrorString(err) - << ": Invalid batch aggregation at index " << i; - Napi::Error::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::Error::New( + env, + module->GetBlstErrorString(err) + + ": Invalid batch aggregation at index "s + + std::to_string(i)) + .ThrowAsJavaScriptException(); return env.Undefined(); } } @@ -717,10 +727,10 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { m_sets[i].msg, m_sets[i].msg_len); if (err != blst::BLST_ERROR::BLST_SUCCESS) { - std::ostringstream msg; - msg << m_module->GetBlstErrorString(err) - << ": Invalid batch aggregation at index " << i; - SetError(msg.str()); + SetError( + m_module->GetBlstErrorString(err) + + ": Invalid batch aggregation at index "s + + std::to_string(i)); return; } } @@ -776,4 +786,4 @@ void init(const Napi::Env &env, Napi::Object &exports) { Napi::String::New(env, "asyncVerifyMultipleAggregateSignatures"), Napi::Function::New(env, AsyncVerifyMultipleAggregateSignatures)); } -} // namespace functions +} // namespace blst_ts_functions diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index f4ccffe9..33df705f 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -1,5 +1,7 @@ #include "secret_key.h" +using namespace std::string_literals; + namespace blst_ts { void SecretKey::Init( Napi::Env env, Napi::Object &exports, BlstTsAddon *module) { @@ -49,10 +51,11 @@ Napi::Value SecretKey::FromKeygen(const Napi::CallbackInfo &info) { // Check for less than 32 bytes so consumers don't accidentally create // zero keys if (ikm.ByteLength() < secret_key_length) { - std::ostringstream msg; - msg << "ikm must be greater than or equal to " << secret_key_length - << " bytes"; - Napi::TypeError::New(env, msg.str()).ThrowAsJavaScriptException(); + Napi::TypeError::New( + env, + "ikm must be greater than or equal to "s + + std::to_string(secret_key_length) + " bytes"s) + .ThrowAsJavaScriptException(); return env.Undefined(); } @@ -100,7 +103,7 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); return env.Undefined(); } - + // just need to pass some value to the External constructor. debug builds // have hard crash check for nullptr bool is_external = true; From 91c46cd130902c3d0f2a487a8e610fe5352b17d4 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 17:20:46 +0800 Subject: [PATCH 28/32] fix: remove hungarian notation --- rebuild/src/addon.h | 4 +- rebuild/src/functions.cc | 196 +++++++++++++++++++-------------------- 2 files changed, 100 insertions(+), 100 deletions(-) diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index bd33621d..3bd6e56a 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -55,7 +55,7 @@ namespace blst_ts { Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ .ThrowAsJavaScriptException(); \ - m_has_error = true; \ + has_error = true; \ return; \ } \ Napi::TypedArray arr_name##_array = value_name.As(); \ @@ -63,7 +63,7 @@ namespace blst_ts { Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ .ThrowAsJavaScriptException(); \ - m_has_error = true; \ + has_error = true; \ return; \ } \ Napi::Uint8Array arr_name = \ diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index d49d0335..aabd75a2 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -282,14 +282,14 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { public: AggregateVerifyWorker(const Napi::CallbackInfo &info) : Napi::AsyncWorker{info.Env(), "AggregateVerifyWorker"}, - m_deferred{Env()}, - m_has_error{false}, - m_module{Env().GetInstanceData()}, - m_ctx{new blst::Pairing(true, m_module->dst)}, - m_sig_point{}, - m_sets{}, - m_is_invalid{false}, - m_result{false} { + deferred{Env()}, + has_error{false}, + _module{Env().GetInstanceData()}, + _ctx{new blst::Pairing(true, _module->dst)}, + _sig_point{}, + _sets{}, + _is_invalid{false}, + _result{false} { Napi::Env env = Env(); try { Napi::Value sig_val = info[2]; @@ -303,25 +303,25 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } else { - m_sig_point.smart_pointer = + _sig_point.smart_pointer = std::make_unique( typed_array.Data(), typed_array.ByteLength()); - m_sig_point.raw_point = m_sig_point.smart_pointer.get(); + _sig_point.raw_point = _sig_point.smart_pointer.get(); } } else { blst_ts::Signature *to_verify = blst_ts::Signature::Unwrap(sig_val.As()); - m_sig_point = to_verify->point->AsAffine(); + _sig_point = to_verify->point->AsAffine(); } if (!info[0].IsArray()) { Napi::TypeError::New( env, "BLST_ERROR: msgs must be of type BlstBuffer[]") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } Napi::Array msgs_array = info[0].As(); @@ -330,7 +330,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { Napi::TypeError::New( env, "BLST_ERROR: msgs must have length > 0") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } @@ -338,20 +338,20 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { Napi::TypeError::New( env, "publicKeys must be of type PublicKeyArg[]") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } Napi::Array pk_array = info[1].As(); uint32_t pk_array_length = pk_array.Length(); if (pk_array_length == 0) { - if (m_sig_point.raw_point->is_inf()) { - m_is_invalid = true; + if (_sig_point.raw_point->is_inf()) { + _is_invalid = true; return; } Napi::TypeError::New( env, "BLST_ERROR: publicKeys must have length > 0") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } @@ -360,18 +360,18 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { env, "BLST_ERROR: msgs and publicKeys must be the same length") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } - m_sets.reserve(pk_array_length); + _sets.reserve(pk_array_length); for (uint32_t i = 0; i < pk_array_length; i++) { - m_sets.push_back({blst_ts::P1AffineGroup{}, nullptr, 0}); + _sets.push_back({blst_ts::P1AffineGroup{}, nullptr, 0}); Napi::Value msg_value = msgs_array[i]; BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "msg") - m_sets[i].msg = msg.Data(); - m_sets[i].msg_len = msg.ByteLength(); + _sets[i].msg = msg.Data(); + _sets[i].msg_len = msg.ByteLength(); Napi::Value pk_val = pk_array[i]; if (pk_val.IsTypedArray()) { @@ -385,7 +385,7 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); - m_is_invalid = true; + _is_invalid = true; return; } else if (blst_ts::is_zero_bytes( typed_array.Data(), @@ -395,73 +395,73 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { env, "BLST_ERROR: PublicKeyArg must not be zero key") .ThrowAsJavaScriptException(); - m_is_invalid = true; + _is_invalid = true; return; } else { - m_sets[i].pk_point.smart_pointer = + _sets[i].pk_point.smart_pointer = std::make_unique( typed_array.Data(), typed_array.ByteLength()); - m_sets[i].pk_point.raw_point = - m_sets[i].pk_point.smart_pointer.get(); + _sets[i].pk_point.raw_point = + _sets[i].pk_point.smart_pointer.get(); } } else { blst_ts::PublicKey *to_verify = blst_ts::PublicKey::Unwrap(pk_val.As()); - m_sets[i].pk_point = to_verify->point->AsAffine(); + _sets[i].pk_point = to_verify->point->AsAffine(); } } } catch (...) { - m_is_invalid = true; + _is_invalid = true; } } /** - * GetPromise associated with _deferred for return to JS + * GetPromise associated with deferred for return to JS */ - Napi::Promise GetPromise() { return m_deferred.Promise(); } + Napi::Promise GetPromise() { return deferred.Promise(); } protected: void Execute() { - if (m_is_invalid) { + if (_is_invalid) { return; } - for (uint32_t i = 0; i < m_sets.size(); i++) { - blst::BLST_ERROR err = m_ctx->aggregate( - m_sets[i].pk_point.raw_point, - m_sig_point.raw_point, - m_sets[i].msg, - m_sets[i].msg_len); + for (uint32_t i = 0; i < _sets.size(); i++) { + blst::BLST_ERROR err = _ctx->aggregate( + _sets[i].pk_point.raw_point, + _sig_point.raw_point, + _sets[i].msg, + _sets[i].msg_len); if (err != blst::BLST_ERROR::BLST_SUCCESS) { SetError( - "BLST_ERROR::"s + m_module->GetBlstErrorString(err) + + "BLST_ERROR::"s + _module->GetBlstErrorString(err) + ": Invalid verification aggregate at index "s + std::to_string(i)); return; } } - m_ctx->commit(); - blst::PT pt{*m_sig_point.raw_point}; - m_result = m_ctx->finalverify(&pt); + _ctx->commit(); + blst::PT pt{*_sig_point.raw_point}; + _result = _ctx->finalverify(&pt); } - void OnOK() { m_deferred.Resolve(Napi::Boolean::New(Env(), m_result)); } - void OnError(const Napi::Error &err) { m_deferred.Reject(err.Value()); } + void OnOK() { deferred.Resolve(Napi::Boolean::New(Env(), _result)); } + void OnError(const Napi::Error &err) { deferred.Reject(err.Value()); } public: - Napi::Promise::Deferred m_deferred; - bool m_has_error; + Napi::Promise::Deferred deferred; + bool has_error; private: - BlstTsAddon *m_module; - std::unique_ptr m_ctx; - blst_ts::P2AffineGroup m_sig_point; - std::vector m_sets; - bool m_is_invalid; - bool m_result; + BlstTsAddon *_module; + std::unique_ptr _ctx; + blst_ts::P2AffineGroup _sig_point; + std::vector _sets; + bool _is_invalid; + bool _result; }; Napi::Value AsyncAggregateVerify(const Napi::CallbackInfo &info) { AggregateVerifyWorker *worker = new AggregateVerifyWorker(info); - if (worker->m_has_error) { + if (worker->has_error) { delete worker; return info.Env().Undefined(); } @@ -595,24 +595,24 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { VerifyMultipleAggregateSignaturesWorker(const Napi::CallbackInfo &info) : Napi:: AsyncWorker{info.Env(), "VerifyMultipleAggregateSignaturesWorker"}, - m_deferred{Env()}, - m_has_error{false}, - m_module{Env().GetInstanceData()}, - m_ctx{new blst::Pairing(true, m_module->dst)}, - m_sets{}, - m_result{false} { + deferred{Env()}, + has_error{false}, + _module{Env().GetInstanceData()}, + _ctx{new blst::Pairing(true, _module->dst)}, + _sets{}, + _result{false} { Napi::Env env = Env(); if (!info[0].IsArray()) { Napi::Error::New( env, "BLST_ERROR: signatureSets must be of type SignatureSet[]") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } Napi::Array sets_array = info[0].As(); uint32_t sets_array_length = sets_array.Length(); - m_sets.reserve(sets_array_length); + _sets.reserve(sets_array_length); try { for (uint32_t i = 0; i < sets_array_length; i++) { Napi::Value set_value = sets_array[i]; @@ -620,7 +620,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { Napi::Error::New( env, "BLST_ERROR: signatureSet must be an object") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } Napi::Object set = set_value.As(); @@ -628,7 +628,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { Napi::Value msg_value = set.Get("message"); BLST_TS_CLASS_UNWRAP_UINT_8_ARRAY(msg_value, msg, "message") - m_sets.push_back( + _sets.push_back( {blst_ts::P1AffineGroup{}, blst_ts::P2AffineGroup{}, msg.Data(), @@ -646,7 +646,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { blst_ts::public_key_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } else if (blst_ts::is_zero_bytes( typed_array.Data(), @@ -656,19 +656,19 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { env, "BLST_ERROR: PublicKeyArg must not be zero key") .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } else { - m_sets[i].pk_point.smart_pointer = + _sets[i].pk_point.smart_pointer = std::make_unique( typed_array.Data(), typed_array.ByteLength()); - m_sets[i].pk_point.raw_point = - m_sets[i].pk_point.smart_pointer.get(); + _sets[i].pk_point.raw_point = + _sets[i].pk_point.smart_pointer.get(); } } else { blst_ts::PublicKey *to_verify = blst_ts::PublicKey::Unwrap(pk_val.As()); - m_sets[i].pk_point = to_verify->point->AsAffine(); + _sets[i].pk_point = to_verify->point->AsAffine(); } Napi::Value sig_val = set.Get("signature"); @@ -683,79 +683,79 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { blst_ts::signature_length_uncompressed)) { Napi::TypeError::New(env, err_out) .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; return; } else { - m_sets[i].sig_point.smart_pointer = + _sets[i].sig_point.smart_pointer = std::make_unique( typed_array.Data(), typed_array.ByteLength()); - m_sets[i].sig_point.raw_point = - m_sets[i].sig_point.smart_pointer.get(); + _sets[i].sig_point.raw_point = + _sets[i].sig_point.smart_pointer.get(); } } else { blst_ts::Signature *to_verify = blst_ts::Signature::Unwrap(sig_val.As()); - m_sets[i].sig_point = to_verify->point->AsAffine(); + _sets[i].sig_point = to_verify->point->AsAffine(); } } } catch (const blst::BLST_ERROR &err) { - Napi::Error::New(env, m_module->GetBlstErrorString(err)) + Napi::Error::New(env, _module->GetBlstErrorString(err)) .ThrowAsJavaScriptException(); - m_has_error = true; + has_error = true; } } /** - * GetPromise associated with _deferred for return to JS + * GetPromise associated with deferred for return to JS */ - Napi::Promise GetPromise() { return m_deferred.Promise(); } + Napi::Promise GetPromise() { return deferred.Promise(); } protected: void Execute() { - for (uint32_t i = 0; i < m_sets.size(); i++) { + for (uint32_t i = 0; i < _sets.size(); i++) { blst::byte rand[BLST_TS_RANDOM_BYTES_LENGTH]; - if (!m_module->GetRandomBytes(rand, BLST_TS_RANDOM_BYTES_LENGTH)) { + if (!_module->GetRandomBytes(rand, BLST_TS_RANDOM_BYTES_LENGTH)) { SetError("BLST_ERROR: Failed to generate random bytes"); return; } - blst::BLST_ERROR err = m_ctx->mul_n_aggregate( - m_sets[i].pk_point.raw_point, - m_sets[i].sig_point.raw_point, + blst::BLST_ERROR err = _ctx->mul_n_aggregate( + _sets[i].pk_point.raw_point, + _sets[i].sig_point.raw_point, rand, BLST_TS_RANDOM_BYTES_LENGTH, - m_sets[i].msg, - m_sets[i].msg_len); + _sets[i].msg, + _sets[i].msg_len); if (err != blst::BLST_ERROR::BLST_SUCCESS) { SetError( - m_module->GetBlstErrorString(err) + + _module->GetBlstErrorString(err) + ": Invalid batch aggregation at index "s + std::to_string(i)); return; } } - m_ctx->commit(); - m_result = m_ctx->finalverify(); + _ctx->commit(); + _result = _ctx->finalverify(); } - void OnOK() { m_deferred.Resolve(Napi::Boolean::New(Env(), m_result)); } - void OnError(const Napi::Error &err) { m_deferred.Reject(err.Value()); } + void OnOK() { deferred.Resolve(Napi::Boolean::New(Env(), _result)); } + void OnError(const Napi::Error &err) { deferred.Reject(err.Value()); } public: - Napi::Promise::Deferred m_deferred; - bool m_has_error; + Napi::Promise::Deferred deferred; + bool has_error; private: - BlstTsAddon *m_module; - std::unique_ptr m_ctx; - std::vector m_sets; - bool m_result; + BlstTsAddon *_module; + std::unique_ptr _ctx; + std::vector _sets; + bool _result; }; Napi::Value AsyncVerifyMultipleAggregateSignatures( const Napi::CallbackInfo &info) { VerifyMultipleAggregateSignaturesWorker *worker = new VerifyMultipleAggregateSignaturesWorker(info); - if (worker->m_has_error) { + if (worker->has_error) { delete worker; return info.Env().Undefined(); } From 3028b07f0ebe8bd407494981ec6c9bcd79092598 Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 17:59:02 +0800 Subject: [PATCH 29/32] feat: return optional from is_valid_length --- rebuild/src/addon.cc | 21 +++--- rebuild/src/addon.h | 12 ++-- rebuild/src/functions.cc | 134 +++++++++++++++++++------------------- rebuild/src/public_key.cc | 9 +-- rebuild/src/secret_key.cc | 8 +-- rebuild/src/signature.cc | 8 +-- 6 files changed, 94 insertions(+), 98 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index a3974677..f038b748 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -48,26 +48,23 @@ bool is_zero_bytes( * If only one length is provided (the other being 0), the error message will * only reference the provided length. */ -bool is_valid_length( - std::string &error_out, - size_t byte_length, - size_t length1, - size_t length2) noexcept { +[[nodiscard]] std::optional is_valid_length( + size_t byte_length, size_t length1, size_t length2) noexcept { if (byte_length == length1 || (length2 != 0 && byte_length == length2)) { - return true; + return std::nullopt; } - error_out.append(" must be "); + std::string err_msg{" must be "}; if (length1 != 0) { - error_out.append(std::to_string(length1)); + err_msg.append(std::to_string(length1)); }; if (length2 != 0) { if (length1 != 0) { - error_out.append(" or "); + err_msg.append(" or "); } - error_out.append(std::to_string(length2)); + err_msg.append(std::to_string(length2)); }; - error_out.append(" bytes long"); - return false; + err_msg.append(" bytes long"); + return err_msg; } } // namespace blst_ts diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 3bd6e56a..80fced5c 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -55,7 +56,7 @@ namespace blst_ts { Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ .ThrowAsJavaScriptException(); \ - has_error = true; \ + has_error = true; \ return; \ } \ Napi::TypedArray arr_name##_array = value_name.As(); \ @@ -63,7 +64,7 @@ namespace blst_ts { Napi::TypeError::New( \ env, "BLST_ERROR: " js_name " must be a BlstBuffer") \ .ThrowAsJavaScriptException(); \ - has_error = true; \ + has_error = true; \ return; \ } \ Napi::Uint8Array arr_name = \ @@ -98,11 +99,8 @@ bool is_zero_bytes( * * @return bool */ -bool is_valid_length( - std::string &error_out, - size_t byte_length, - size_t length1, - size_t length2 = 0) noexcept; +[[nodiscard]] std::optional is_valid_length( + size_t byte_length, size_t length1, size_t length2 = 0) noexcept; } // namespace blst_ts class BlstTsAddon; diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index aabd75a2..de272430 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -27,13 +27,13 @@ Napi::Value AggregatePublicKeys(const Napi::CallbackInfo &info) { try { if (val.IsTypedArray()) { Napi::Uint8Array typed_array = val.As(); - std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::public_key_length_compressed, - blst_ts::public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: PublicKeyArg"s + *err_msg) .ThrowAsJavaScriptException(); has_error = true; } else if (blst_ts::is_zero_bytes( @@ -102,13 +102,13 @@ Napi::Value AggregateSignatures(const Napi::CallbackInfo &info) { try { if (val.IsTypedArray()) { Napi::Uint8Array typed_array = val.As(); - std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::signature_length_compressed, - blst_ts::signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: SignatureArg"s + *err_msg) .ThrowAsJavaScriptException(); has_error = true; } else { @@ -152,13 +152,13 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { blst_ts::P2AffineGroup sig_point; if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); - std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!blst_ts::is_valid_length( - err_out, + if (std::optional err_msg = blst_ts::is_valid_length( typed_array.ByteLength(), blst_ts::signature_length_compressed, blst_ts::signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); + Napi::TypeError::New( + env, "BLST_ERROR: SignatureArg"s + *err_msg) + .ThrowAsJavaScriptException(); return env.Undefined(); } else { sig_point.smart_pointer = std::make_unique( @@ -220,13 +220,13 @@ Napi::Value AggregateVerify(const Napi::CallbackInfo &info) { blst_ts::P1AffineGroup pk_point; if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); - std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::public_key_length_compressed, - blst_ts::public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: PublicKeyArg"s + *err_msg) .ThrowAsJavaScriptException(); return env.Undefined(); } else if (blst_ts::is_zero_bytes( @@ -295,13 +295,13 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { Napi::Value sig_val = info[2]; if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); - std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::signature_length_compressed, - blst_ts::signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: SignatureArg"s + *err_msg) .ThrowAsJavaScriptException(); has_error = true; return; @@ -377,13 +377,13 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); - std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::public_key_length_compressed, - blst_ts::public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: PublicKeyArg"s + *err_msg) .ThrowAsJavaScriptException(); _is_invalid = true; return; @@ -507,13 +507,13 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { blst_ts::P1AffineGroup pk_point; if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); - std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::public_key_length_compressed, - blst_ts::public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: PublicKeyArg"s + *err_msg) .ThrowAsJavaScriptException(); return env.Undefined(); } else if (blst_ts::is_zero_bytes( @@ -539,13 +539,13 @@ Napi::Value VerifyMultipleAggregateSignatures(const Napi::CallbackInfo &info) { blst_ts::P2AffineGroup sig_point; if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); - std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::signature_length_compressed, - blst_ts::signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: SignatureArg"s + *err_msg) .ThrowAsJavaScriptException(); return env.Undefined(); } else { @@ -638,13 +638,13 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { if (pk_val.IsTypedArray()) { Napi::Uint8Array typed_array = pk_val.As(); - std::string err_out{"BLST_ERROR: PublicKeyArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::public_key_length_compressed, - blst_ts::public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::public_key_length_compressed, + blst_ts::public_key_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: PublicKeyArg"s + *err_msg) .ThrowAsJavaScriptException(); has_error = true; return; @@ -675,13 +675,13 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { if (sig_val.IsTypedArray()) { Napi::Uint8Array typed_array = sig_val.As(); - std::string err_out{"BLST_ERROR: SignatureArg"}; - if (!blst_ts::is_valid_length( - err_out, - typed_array.ByteLength(), - blst_ts::signature_length_compressed, - blst_ts::signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out) + if (std::optional err_msg = + blst_ts::is_valid_length( + typed_array.ByteLength(), + blst_ts::signature_length_compressed, + blst_ts::signature_length_uncompressed)) { + Napi::TypeError::New( + env, "BLST_ERROR: SignatureArg"s + *err_msg) .ThrowAsJavaScriptException(); has_error = true; return; diff --git a/rebuild/src/public_key.cc b/rebuild/src/public_key.cc index 9a1c4d52..c87afca3 100644 --- a/rebuild/src/public_key.cc +++ b/rebuild/src/public_key.cc @@ -1,5 +1,7 @@ #include "public_key.h" +using namespace std::string_literals; + namespace blst_ts { void P1::Serialize(bool compress, blst::byte *out) const { @@ -86,13 +88,12 @@ Napi::Value PublicKey::Deserialize(const Napi::CallbackInfo &info) { Napi::Value pk_bytes_val = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(pk_bytes_val, pk_bytes, "pkBytes") - std::string err_out{"BLST_ERROR: pkBytes"}; - if (!blst_ts::is_valid_length( - err_out, + if (std::optional err_msg = blst_ts::is_valid_length( pk_bytes.ByteLength(), public_key_length_compressed, public_key_length_uncompressed)) { - Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); + Napi::TypeError::New(env, "BLST_ERROR: pkBytes"s + *err_msg) + .ThrowAsJavaScriptException(); return env.Undefined(); } diff --git a/rebuild/src/secret_key.cc b/rebuild/src/secret_key.cc index 33df705f..4d943cb4 100644 --- a/rebuild/src/secret_key.cc +++ b/rebuild/src/secret_key.cc @@ -97,10 +97,10 @@ Napi::Value SecretKey::Deserialize(const Napi::CallbackInfo &info) { Napi::Value sk_bytes_value = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(sk_bytes_value, sk_bytes, "skBytes") - std::string err_out{"BLST_ERROR: skBytes"}; - if (!blst_ts::is_valid_length( - err_out, sk_bytes.ByteLength(), secret_key_length)) { - Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); + if (std::optional err_msg = blst_ts::is_valid_length( + sk_bytes.ByteLength(), secret_key_length)) { + Napi::TypeError::New(env, "BLST_ERROR: skBytes"s + *err_msg) + .ThrowAsJavaScriptException(); return env.Undefined(); } diff --git a/rebuild/src/signature.cc b/rebuild/src/signature.cc index 0a7324c8..c96acca5 100644 --- a/rebuild/src/signature.cc +++ b/rebuild/src/signature.cc @@ -1,5 +1,6 @@ #include "signature.h" +using namespace std::string_literals; namespace blst_ts { void P2::Serialize(bool compress, blst::byte *out) const { compress ? _point.compress(out) : _point.serialize(out); @@ -106,13 +107,12 @@ Napi::Value Signature::Deserialize(const Napi::CallbackInfo &info) { Napi::Value sig_bytes_val = info[0]; BLST_TS_UNWRAP_UINT_8_ARRAY(sig_bytes_val, sig_bytes, "sigBytes") - std::string err_out{"BLST_ERROR: sigBytes"}; - if (!blst_ts::is_valid_length( - err_out, + if (std::optional err_msg = blst_ts::is_valid_length( sig_bytes.ByteLength(), signature_length_compressed, signature_length_uncompressed)) { - Napi::TypeError::New(env, err_out).ThrowAsJavaScriptException(); + Napi::TypeError::New(env, "BLST_ERROR: sigBytes"s + *err_msg) + .ThrowAsJavaScriptException(); return env.Undefined(); } From 5353fc08b20dffb4569879d6201990122b1564cd Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 18:04:50 +0800 Subject: [PATCH 30/32] refactor: update docstring and move to header --- rebuild/src/addon.cc | 34 ---------------------------------- rebuild/src/addon.h | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 48 deletions(-) diff --git a/rebuild/src/addon.cc b/rebuild/src/addon.cc index f038b748..fd26f2c0 100644 --- a/rebuild/src/addon.cc +++ b/rebuild/src/addon.cc @@ -1,19 +1,6 @@ #include "addon.h" namespace blst_ts { -/** - * Checks if a specified range of bytes within a byte array consists only of - * zeros. - * - * @param data A pointer to the first element of the byte array to be checked. - * @param starting_index The offset (index) from the beginning of the array - * where the check should start. (0 starts at *data) - * @param byte_length The total length of the data array - * - * @return Returns true if all bytes from start_byte to the end of the array are - * zeros. Returns false if the starting offset is beyond the array length or if - * any byte in the specified range is not zero. - */ bool is_zero_bytes( const uint8_t *data, const size_t starting_index, @@ -27,27 +14,6 @@ bool is_zero_bytes( return false; } -/** - * Validates that a given byte length matches one of two specified lengths, - * optionally recording an error message if the validation fails. - * - * @param error_out A reference to a std::string where the error message is - * appended if the byte length does not match either length1 or length2. - * @param byte_length The length to be validated against length1 and length2. - * @param length1 The first valid length that byte_length can be. A value of 0 - * is considered as not set and thus not compared. - * @param length2 The second valid length that byte_length can be. A value of 0 - * is considered as not set and thus not compared. - * - * @return Returns true if byte_length matches length1 or (if length2 is not 0) - * length2. Returns false if byte_length matches neither, appending an - * appropriate error message to error_out. - * - * @note If both length1 and length2 are provided (non-zero), the error message - * will indicate that the valid byte_length must be either length1 or length2. - * If only one length is provided (the other being 0), the error message will - * only reference the provided length. - */ [[nodiscard]] std::optional is_valid_length( size_t byte_length, size_t length1, size_t length2) noexcept { if (byte_length == length1 || (length2 != 0 && byte_length == length2)) { diff --git a/rebuild/src/addon.h b/rebuild/src/addon.h index 80fced5c..02f94c62 100644 --- a/rebuild/src/addon.h +++ b/rebuild/src/addon.h @@ -73,15 +73,17 @@ namespace blst_ts { typedef enum { Affine, Jacobian } CoordType; /** - * Checks a byte array to see if it is all zeros. Can pass start byte for the - * cases where the first byte is the tag (infinity point and - * compress/uncompressed). + * Checks if a specified range of bytes within a byte array consists only of + * zeros. * - * @param data uint8_t* - * @param start_byte size_t - * @param byte_length size_t + * @param data A pointer to the first element of the byte array to be checked. + * @param starting_index The offset (index) from the beginning of the array + * where the check should start. (0 starts at *data) + * @param byte_length The total length of the data array * - * @return bool + * @return Returns true if all bytes from start_byte to the end of the array are + * zeros. Returns false if the starting offset is beyond the array length or if + * any byte in the specified range is not zero. */ bool is_zero_bytes( const uint8_t *data, @@ -89,15 +91,26 @@ bool is_zero_bytes( const size_t byte_length) noexcept; /** - * Checks if a byte array is a valid length. If not, sets the error message and - * returns false. If valid returns true for use in conditional statements. + * Validates that a given byte length matches one of two specified lengths, + * returning an optional error message if the validation fails. * - * @param[out] error_out &std::string - error message to set if invalid - * @param[in] byte_length size_t - length of the byte array to validate - * @param[in] length1 size_t - first valid length - * @param[in] length2 size_t - second valid length (optional) + * @param byte_length The length to be validated against length1 and length2. + * @param length1 The first valid length that byte_length can be. A value of 0 + * is considered as not set and thus not compared. + * @param length2 The second valid length that byte_length can be. A value of 0 + * is considered as not set and thus not compared. * - * @return bool + * @return An std::optional that contains an error message if + * byte_length does not match either length1 or (if length2 is not 0) length2. + * If byte_length is valid, std::nullopt is returned. The error message, if + * returned, specifies the valid lengths byte_length must match. + * + * @note If both length1 and length2 are provided (non-zero), and byte_length + * does not match, the error message indicates that the valid byte_length must + * be either length1 or length2. If only one length is provided (the other being + * 0), the error message will only reference the provided length. This function + * is marked with [[nodiscard]] to ensure that the caller handles the potential + * error message, promoting safer and more intentional error checking. */ [[nodiscard]] std::optional is_valid_length( size_t byte_length, size_t length1, size_t length2 = 0) noexcept; From ed48ba50a8689bbbc9e7c651e4a38f0801c39a0b Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Fri, 15 Mar 2024 18:49:24 +0800 Subject: [PATCH 31/32] feat: add Refs to async functions to prevent GC --- rebuild/src/functions.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rebuild/src/functions.cc b/rebuild/src/functions.cc index de272430..10bb3bcc 100644 --- a/rebuild/src/functions.cc +++ b/rebuild/src/functions.cc @@ -288,6 +288,9 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { _ctx{new blst::Pairing(true, _module->dst)}, _sig_point{}, _sets{}, + _msgs_ref{Napi::Persistent(info[0])}, + _pks_ref{Napi::Persistent(info[1])}, + _sig_ref{Napi::Persistent(info[2])}, _is_invalid{false}, _result{false} { Napi::Env env = Env(); @@ -455,6 +458,9 @@ class AggregateVerifyWorker : public Napi::AsyncWorker { std::unique_ptr _ctx; blst_ts::P2AffineGroup _sig_point; std::vector _sets; + Napi::Reference _msgs_ref; + Napi::Reference _pks_ref; + Napi::Reference _sig_ref; bool _is_invalid; bool _result; }; @@ -600,6 +606,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { _module{Env().GetInstanceData()}, _ctx{new blst::Pairing(true, _module->dst)}, _sets{}, + _sets_ref{Napi::Persistent(info[0])}, _result{false} { Napi::Env env = Env(); if (!info[0].IsArray()) { @@ -748,6 +755,7 @@ class VerifyMultipleAggregateSignaturesWorker : public Napi::AsyncWorker { BlstTsAddon *_module; std::unique_ptr _ctx; std::vector _sets; + Napi::Reference _sets_ref; bool _result; }; From 1625ff055e0b22d07f175c2c9aa30d636de2c02c Mon Sep 17 00:00:00 2001 From: matthewkeil Date: Thu, 28 Mar 2024 22:42:38 +0700 Subject: [PATCH 32/32] feat: add final to point wrappers --- rebuild/src/public_key.h | 4 ++-- rebuild/src/signature.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rebuild/src/public_key.h b/rebuild/src/public_key.h index 0513f0e9..48f8bf0e 100644 --- a/rebuild/src/public_key.h +++ b/rebuild/src/public_key.h @@ -23,7 +23,7 @@ class P1Wrapper { const blst::byte *rand_bytes, const size_t rand_bytes_length) const = 0; }; -class P1 : public P1Wrapper { +class P1 final : public P1Wrapper { private: blst::P1 _point; @@ -39,7 +39,7 @@ class P1 : public P1Wrapper { const size_t rand_bytes_length) const final; }; -class P1Affine : public P1Wrapper { +class P1Affine final : public P1Wrapper { private: blst::P1_Affine _point; diff --git a/rebuild/src/signature.h b/rebuild/src/signature.h index c3dd1dc4..a54160e7 100644 --- a/rebuild/src/signature.h +++ b/rebuild/src/signature.h @@ -28,7 +28,7 @@ class P2Wrapper { const std::string &dst) = 0; }; -class P2 : public P2Wrapper { +class P2 final : public P2Wrapper { private: blst::P2 _point; @@ -49,7 +49,7 @@ class P2 : public P2Wrapper { const std::string &dst) final; }; -class P2Affine : public P2Wrapper { +class P2Affine final : public P2Wrapper { private: blst::P2_Affine _point;