-
Notifications
You must be signed in to change notification settings - Fork 1
feat: refactor #49
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
feat: refactor #49
Conversation
568c6d1 to
a53ee89
Compare
| Install and generate bun bindings: | ||
|
|
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.
Is the plan in this PR to keep the bun bindings and move to lodestar-bun in a separate PR?
(I'm not planning on giving a deep review of the bun code until a PR into lodestar-bun)
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.
I kept this here for ease of development, just to make sure the bun side still passes tests+benchmarks without the hassle. I will move the bun bindings from this PR to a new PR in lodestar-bun.
| @@ -0,0 +1,134 @@ | |||
| //! AggregatePublicKey definition for BLS signature scheme based on BLS12-381. | |||
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.
is there a reason AggregatePublicKey.zig and AggregateSignature.zig are top-level structs vs public_key.zig, signature.zig etc? They shouldn't need to be extern structs imo.
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.
good point, I just kept them that way picking up from your PR. Let's change it
src/pairing.zig
Outdated
| return PairingError.DstTooSmall; | ||
| } | ||
| pub fn init(buffer: *[Self.sizeOf()]u8, hash_or_encode: bool, dst: []const u8) Self { | ||
| std.debug.assert(buffer.len == Self.sizeOf()); |
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.
is it possible for an array to have a length that's different than its defined length?
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.
Nope, I think we can remove this assert
src/pairing.zig
Outdated
| /// Initializes a pairing context with the provided `buffer` and other parameters. | ||
| /// | ||
| /// Note: Rust always use a heap allocation here, but adding an allocator as param for Zig is too complex | ||
| /// instead of that we provide a buffer that's big enough for the struct to operate on so that: | ||
| /// - it does not have allocator in its api | ||
| /// - can use stack allocation at consumer side | ||
| /// - can reuse memory if it makes sense at consumer side |
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.
Might add that the Pairing takes ownership of the buffer
src/public_key.zig
Outdated
| pub fn fromAggregate(agg_pk: *const min_pk.AggPublicKey) Self { | ||
| var pk_aff = min_pk.PublicKey{}; | ||
| c.blst_p1_to_affine(&pk_aff.point, &agg_pk.point); | ||
| return pk_aff; | ||
| } | ||
|
|
||
| /// Convert a regular `PublicKey` to a `AggPublicKey`. | ||
| pub fn toAggregate(self: *const Self) min_pk.AggPublicKey { | ||
| var agg_pk = min_pk.AggPublicKey{}; | ||
| c.blst_p1_from_affine(&agg_pk.point, &self.point); |
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.
an example of why the min_pk types are a bit confusing.
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.
Hmm, what do you mean here?
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.
I don't think agg_pk.point exists in either fn. Confusing bc its confused min_pk.AggPublicKey with AggregatePublicKey. If we just use c.blst_pX we won't have this issue
* add blst/ to .gitignore * fix test * feat: remove sig_groupcheck * fix(testSets): SECRET_KEY_LENGTH -> SECRET_KEY_SIZE * fix(index): remove missing verify.js * rebirth * chore: a bit more restructuring on zig * more updates * feat: implement verifyMultipleAggregateSignatures * remove redundant log * chore: simplify signature and pk size consts * update aggregateWithRandomness with writing pks and sigs * simple test for AggregateSignature * revert some changes * fix(sig): fix fromBytes * update bindings * fix(verifyMultipleAggSigs): fix writers for verifyMultipleAggregateSignatures * uncomment tests * missing *Signature * chore(signature): remove unused sigValidate * fix(signature.ts): do fromBytes first then validate * fix test * fix randomness * fix: uncomment aggregateVerify test * chore: simplify naming for signature length constants * remove unused code * add writeSignatures * more test fixes * test(pk): add test for aggregatePublicKeys * define and use MAX_AGGREGATE_PER_JOB * use pointers to PublicKey in aggregatePublicKey * fix length for aggregate signatures * remove redundant print * AggregateSignature: fix rands input for aggregateWithRandomness * fix scratch for aggsig and aggpk * remove comment * uncomment more checks * chore: remove redundant SignatureSet * fix rands input to aggregateWithRandomness * fix stuff related to rands * fixes * remove redundant c abi code; mostly unused stuff * update README * chore(error): remove redundant error definitions * chore(aggPk): cleanup AggregatePublicKey.zig * small correciton * more minor changes to aggPk * chore(aggSig): doc comment and tidy up * add dst as a const * fix link for DST * doc comments for pairing.zig * finish doc comments * fix comment * remove comment * remove unnecessary import
dcca904 to
6c3db51
Compare
Moved declarations to more appropriate places; for example for sizes of `PublicKey` (serialize and compressed), moved their declarations to `PublicKey`.
13cbd59 to
704c411
Compare
Performance Report✔️ no performance regression detected Full benchmark results
|
6369ec9 to
bfbae85
Compare
|
I've converted all the extern structs to top-level native zig structs, and I think I addressed some of the issues as well:
|
|
Seems like there's a flaky test in aggregating public keys, not quite sure what's the issue just yet. Had to re-run the macOS job 3 times for CI to pass. Creating an issue for this. |
src/AggregatePublicKey.zig
Outdated
| if (pks_validate) for (pks) |pk| try pk.validate(); | ||
|
|
||
| var scalars_refs: [MAX_AGGREGATE_PER_JOB]*const u8 = undefined; | ||
| var pk_points: [MAX_AGGREGATE_PER_JOB]*const PublicKey.Point = undefined; |
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.
this is the only use of PublicKey.Point (and Signature.Point is not used).
Consider not copying pointer here, using the same cast as in AggregateSignature.aggregateWithRandomness
Also consider removing Point entirely. (As an alternative, consider adding a doc comment to PublicKey, Signature, etc that says something to the effect of eg: "PublicKey is a p1_affine" or "PublicKey is a point in G1 in affine form." which can mitigate the possible confusion)
706a139 to
d20d683
Compare
27dd49f to
7bb299f
Compare
14ea1e4 to
7b8c78c
Compare
| ["Uint32Array", new Uint32Array()], | ||
| ["Map", new Map()], | ||
| ["Set", new Set()], | ||
| // ["boolean", true], |
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.
i'm thinking to even remove these tests because i'm not sure if testing for invalid inputs make sense at all
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.
great work, looks really good
| const testing = std.testing; | ||
|
|
||
| /// Expose c types for lodestar-bun bindings | ||
| pub const c = @cImport({ |
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.
can we reexport the upstream blst artifact in build.zig? would that be equivalent?
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.
can be resolved later
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.
lgtm
This is an opinionated refactoring, an effort to refactor the zig codebase into 1. a zig library (for consumption in zig) and 2. a bun-centric c-abi wrapper (for consumption in js).
build.zig and bun bindings are also tweaked.
leaving as draft because this is still not feature complete with current main branch.
TODO
EDIT (bing):
dcca904 merges #50 into this PR, which is another opinionated refactor of this refactor that only implements the
min_pkvariant of theblstlibrary.Here are some notes on what the PR changed:
min_pkvariant. This means that we do not supportmin_sig, and we do not need to haveSigVarianttypes, or the confusingcreateSigVariantfrom before, which can be confusing to follow when reading the code.memory_pool.zigandthread_pool.zigimplementation. Had some initial discussions in private with @wemeetagain to decouple the memory related implementations from the library, but after some refactor work it feels like a memory/thread pool purpose-built for this library seems necessary?