Skip to content

Commit

Permalink
refactor: convert macro sizes to static const values
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Mar 12, 2024
1 parent bf0461f commit 5d02255
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
8 changes: 4 additions & 4 deletions rebuild/src/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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}; \
Expand All @@ -33,9 +33,9 @@
} \
Napi::Buffer<uint8_t> serialized = Napi::Buffer<uint8_t>::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) \
Expand Down
40 changes: 20 additions & 20 deletions rebuild/src/functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions rebuild/src/public_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ void PublicKey::Init(
Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As<Napi::Object>();
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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions rebuild/src/public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<blst::P1_Affine> smart_pointer;
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down
14 changes: 7 additions & 7 deletions rebuild/src/secret_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void SecretKey::Init(
.As<Napi::Object>()
.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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -134,7 +134,7 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) {
Napi::EscapableHandleScope scope(env);

Napi::Buffer<uint8_t> serialized =
Napi::Buffer<uint8_t>::New(env, BLST_TS_SECRET_KEY_LENGTH);
Napi::Buffer<uint8_t>::New(env, secret_key_length);
key->to_bendian(serialized.Data());

return scope.Escape(serialized);
Expand Down
2 changes: 1 addition & 1 deletion rebuild/src/secret_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretKey> {
public:
Expand Down
10 changes: 5 additions & 5 deletions rebuild/src/signature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ void Signature::Init(
Napi::Object js_exports = exports.Get("BLST_CONSTANTS").As<Napi::Object>();
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) {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions rebuild/src/signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<blst::P2_Affine> smart_pointer;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 5d02255

Please sign in to comment.