-
Notifications
You must be signed in to change notification settings - Fork 12
refactor(bls): use pointer arrays #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -468,13 +468,12 @@ pub fn Signature_aggregate(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.Value | |
| const sigs_len = try sigs_array.getArrayLength(); | ||
| if (sigs_len == 0) return error.EmptySignatureArray; | ||
|
|
||
| const sigs = try allocator.alloc(Signature, sigs_len); | ||
| const sigs = try allocator.alloc(*const Signature, sigs_len); | ||
| defer allocator.free(sigs); | ||
|
|
||
| for (0..sigs_len) |i| { | ||
| const sig_value = try sigs_array.getElement(@intCast(i)); | ||
| const sig = try env.unwrap(Signature, sig_value); | ||
| sigs[i] = sig.*; | ||
| sigs[i] = try env.unwrap(Signature, sig_value); | ||
| } | ||
|
|
||
| const agg_sig = AggregateSignature.aggregate(sigs, sigs_groupcheck) catch return error.AggregationFailed; | ||
|
|
@@ -557,8 +556,9 @@ pub fn blst_aggregateVerify( | |
|
|
||
| const msgs = try allocator.alloc([32]u8, msgs_len); | ||
| defer allocator.free(msgs); | ||
| const pk_ptrs = try allocator.alloc(*PublicKey, pks_len); | ||
| defer allocator.free(pk_ptrs); | ||
|
|
||
| const pks = try allocator.alloc(*PublicKey, pks_len); | ||
| defer allocator.free(pks); | ||
|
|
||
| for (0..msgs_len) |i| { | ||
| const msg_value = try msgs_array.getElement(@intCast(i)); | ||
|
|
@@ -567,11 +567,11 @@ pub fn blst_aggregateVerify( | |
| @memcpy(&msgs[i], msg_info.data[0..32]); | ||
|
|
||
| const pk_value = try pks_array.getElement(@intCast(i)); | ||
| pk_ptrs[i] = try env.unwrap(PublicKey, pk_value); | ||
| pks[i] = try env.unwrap(PublicKey, pk_value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM: Following the style guide, explicitly pass options to References
|
||
| } | ||
|
|
||
| const pool = thread_pool orelse @panic("ThreadPool not initialized; call initThreadPool first"); | ||
| const result = pool.aggregateVerify(sig, sig_groupcheck, msgs, DST, pk_ptrs, pks_validate) catch { | ||
| const result = pool.aggregateVerify(sig, sig_groupcheck, msgs, DST, pks, pks_validate) catch { | ||
| return try env.getBoolean(false); | ||
| }; | ||
|
|
||
|
|
@@ -603,13 +603,12 @@ pub fn blst_fastAggregateVerify(env: napi.Env, cb: napi.CallbackInfo(4)) !napi.V | |
| return try env.getBoolean(false); | ||
| } | ||
|
|
||
| const pks = try allocator.alloc(PublicKey, pks_len); | ||
| const pks = try allocator.alloc(*const PublicKey, pks_len); | ||
| defer allocator.free(pks); | ||
|
|
||
| for (0..pks_len) |i| { | ||
| const pk_value = try pks_array.getElement(@intCast(i)); | ||
| const pk = try env.unwrap(PublicKey, pk_value); | ||
| pks[i] = pk.*; | ||
| pks[i] = try env.unwrap(PublicKey, pk_value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM: Per the style guide, explicitly pass options to References
|
||
| } | ||
|
|
||
| var pairing_buf: [Pairing.sizeOf()]u8 align(@alignOf(Pairing)) = undefined; | ||
|
|
@@ -717,13 +716,12 @@ pub fn blst_aggregateSignatures(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.V | |
|
|
||
| if (sigs_len == 0) return error.EmptySignatureArray; | ||
|
|
||
| const sigs = try allocator.alloc(Signature, sigs_len); | ||
| const sigs = try allocator.alloc(*const Signature, sigs_len); | ||
| defer allocator.free(sigs); | ||
|
|
||
| for (0..sigs_len) |i| { | ||
| const sig_value = try sigs_array.getElement(@intCast(i)); | ||
| const sig = try env.unwrap(Signature, sig_value); | ||
| sigs[i] = sig.*; | ||
| sigs[i] = try env.unwrap(Signature, sig_value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM: For consistency and to adhere to the style guide, explicitly pass options to References
|
||
| } | ||
|
|
||
| const agg_sig = AggregateSignature.aggregate(sigs, sigs_groupcheck) catch return error.AggregationFailed; | ||
|
|
@@ -754,13 +752,12 @@ pub fn blst_aggregatePublicKeys(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.V | |
| return error.EmptyPublicKeyArray; | ||
| } | ||
|
|
||
| const pks = try allocator.alloc(PublicKey, pks_len); | ||
| const pks = try allocator.alloc(*const PublicKey, pks_len); | ||
| defer allocator.free(pks); | ||
|
|
||
| for (0..pks_len) |i| { | ||
| const pk_value = try pks_array.getElement(@intCast(i)); | ||
| const pk = try env.unwrap(PublicKey, pk_value); | ||
| pks[i] = pk.*; | ||
| pks[i] = try env.unwrap(PublicKey, pk_value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM: To improve code clarity and prevent potential issues, explicitly pass options to References
|
||
| } | ||
|
|
||
| const agg_pk = AggregatePublicKey.aggregate(pks, pks_validate) catch return error.AggregationFailed; | ||
|
|
@@ -789,16 +786,19 @@ pub fn blst_aggregateSerializedPublicKeys(env: napi.Env, cb: napi.CallbackInfo(2 | |
|
|
||
| const pks = try allocator.alloc(PublicKey, pks_len); | ||
| defer allocator.free(pks); | ||
| const pk_ptrs = try allocator.alloc(*const PublicKey, pks_len); | ||
| defer allocator.free(pk_ptrs); | ||
|
|
||
| for (0..pks_len) |i| { | ||
| const pk_bytes_value = try pks_array.getElement(@intCast(i)); | ||
| const bytes_info = try pk_bytes_value.getTypedarrayInfo(); | ||
|
|
||
| pks[i] = PublicKey.deserialize(bytes_info.data) catch | ||
| return error.DeserializationFailed; | ||
| pk_ptrs[i] = &pks[i]; | ||
| } | ||
|
|
||
| const agg_pk = AggregatePublicKey.aggregate(pks, pks_validate) catch return error.AggregationFailed; | ||
| const agg_pk = AggregatePublicKey.aggregate(pk_ptrs, pks_validate) catch return error.AggregationFailed; | ||
| const result_pk = agg_pk.toPublicKey(); | ||
|
|
||
| const pk_value = try newPublicKeyInstance(env); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,7 +64,7 @@ pub fn aggregateVerify( | |
| buffer: *align(Pairing.buf_align) [Pairing.sizeOf()]u8, | ||
| msgs: []const [32]u8, | ||
| dst: []const u8, | ||
| pks: []const PublicKey, | ||
| pks: []const *const PublicKey, | ||
| pks_validate: bool, | ||
| ) BlstError!bool { | ||
| const n_elems = pks.len; | ||
|
|
@@ -73,7 +73,7 @@ pub fn aggregateVerify( | |
| } | ||
| var pairing = Pairing.init(buffer, true, dst); | ||
| try pairing.aggregate( | ||
| &pks[0], | ||
| pks[0], | ||
| pks_validate, | ||
|
Comment on lines
+76
to
77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: The code is dereferencing a pointer without ensuring that the References
|
||
| self, | ||
| sig_groupcheck, | ||
|
|
@@ -83,7 +83,7 @@ pub fn aggregateVerify( | |
|
|
||
| for (1..n_elems) |i| { | ||
| try pairing.aggregate( | ||
| &pks[i], | ||
| pks[i], | ||
| pks_validate, | ||
| null, | ||
| sig_groupcheck, | ||
|
|
@@ -108,7 +108,7 @@ pub fn fastAggregateVerify( | |
| buffer: *align(Pairing.buf_align) [Pairing.sizeOf()]u8, | ||
| msg: *const [32]u8, | ||
| dst: []const u8, | ||
| pks: []const PublicKey, | ||
| pks: []const *const PublicKey, | ||
| pks_validate: bool, | ||
| ) BlstError!bool { | ||
| const agg_pk = try AggregatePublicKey.aggregate(pks, pks_validate); | ||
|
|
@@ -119,7 +119,7 @@ pub fn fastAggregateVerify( | |
| buffer, | ||
| @ptrCast(msg), | ||
| dst, | ||
| &[_]PublicKey{pk}, | ||
| &[_]*const PublicKey{&pk}, | ||
| false, | ||
| ); | ||
| } | ||
|
|
@@ -135,13 +135,12 @@ pub fn fastAggregateVerifyPreAggregated( | |
| dst: []const u8, | ||
| pk: *const PublicKey, | ||
| ) BlstError!bool { | ||
| const pks: [*]const PublicKey = @ptrCast(pk); | ||
| return try self.aggregateVerify( | ||
| sig_groupcheck, | ||
| buffer, | ||
| @ptrCast(msg), | ||
| dst, | ||
| pks[0..1], | ||
| &[_]*const PublicKey{pk}, | ||
| false, | ||
| ); | ||
| } | ||
|
|
@@ -281,8 +280,15 @@ test aggregateVerify { | |
| sigs[i] = sig; | ||
| } | ||
|
|
||
| const agg_sig = try AggregateSignature.aggregate(&sigs, false); | ||
| var sig_ptrs: [num_sigs]*const @This() = undefined; | ||
| var pk_ptrs: [num_sigs]*const PublicKey = undefined; | ||
| for (0..num_sigs) |i| { | ||
| sig_ptrs[i] = &sigs[i]; | ||
| pk_ptrs[i] = &pks[i]; | ||
| } | ||
|
|
||
| const agg_sig = try AggregateSignature.aggregate(&sig_ptrs, false); | ||
| const sig = @This().fromAggregate(&agg_sig); | ||
|
|
||
| try std.testing.expect(try sig.aggregateVerify(false, &buffer, &msgs, dst, &pks, false)); | ||
| try std.testing.expect(try sig.aggregateVerify(false, &buffer, &msgs, dst, &pk_ptrs, false)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MEDIUM: According to the Lodestar State Transition Zig Style Guide, it's better to explicitly pass options to library functions instead of relying on defaults. While
napi.newInstancemight have sensible defaults, explicitly passing options improves readability and prevents potential bugs if the library changes its defaults in the future. See Repository Style Guide, lines 170-173.References