hashsig-glue: replace Box allocs with Zig-owned placement-init pattern - #934
Closed
zclawz wants to merge 1 commit into
Closed
hashsig-glue: replace Box allocs with Zig-owned placement-init pattern#934zclawz wants to merge 1 commit into
zclawz wants to merge 1 commit into
Conversation
Rust no longer heap-allocates the outer KeyPair / Signature / PublicKey wrapper structs. Instead, each init function accepts a caller-supplied buffer and writes into it with std::ptr::write; the matching _deinit runs std::ptr::drop_in_place (Rust Drop) without freeing the buffer. Zig side allocates the buffers via C malloc (always malloc-aligned, safe for all Rust #[repr(C)] types) and frees them after calling _deinit. New Rust exports hashsig_sizeof_keypair / _signature / _public_key — allocation size hashsig_alignof_keypair / _signature / _public_key — required alignment hashsig_keypair_generate_into (was: hashsig_keypair_generate → *mut Box) hashsig_keypair_from_ssz_into (was: hashsig_keypair_from_ssz → *mut Box) hashsig_keypair_deinit (was: hashsig_keypair_free → Box::from_raw) hashsig_sign_into (was: hashsig_sign → *mut Box) hashsig_signature_deinit (was: hashsig_signature_free) hashsig_signature_from_ssz_into hashsig_public_key_from_ssz_into hashsig_public_key_deinit Removed Rust exports (were Box-allocating): hashsig_keypair_generate, hashsig_keypair_from_ssz, hashsig_keypair_free hashsig_sign, hashsig_signature_free, hashsig_signature_from_ssz hashsig_public_key_from_ssz, hashsig_public_key_free Note: the inner XMSS types (leansig XmssPublicKey / XmssSignature etc.) still carry their own heap-allocated data (Vec<u8> inside leansig); those are inherent to the leansig API and are freed correctly when _deinit runs Drop in-place. The intermediate Vec<u8> in to_bytes() serialisation helpers is similarly a leansig API constraint; those are short-lived temporaries that do not escape the FFI call. Addresses: #918 (comment)
Contributor
Author
|
Closing — superseded by #935 which targets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the review comment on #918 requesting that key-generation and signing be refactored to avoid additional Rust heap allocations, consistent with the caller-supplies-buffer pattern already used in multisig-glue.
What changed
rust/hashsig-glue/src/lib.rsReplaced the
Box::new(T)+Box::into_rawreturn /Box::from_rawdrop pattern with a placement-init approach:Layout queries (new):
hashsig_sizeof_keypair / _signature / _public_key— Zig queries at runtime to know how much to allocatehashsig_alignof_keypair / _signature / _public_key— required alignment (C malloc always satisfies this)Placement-init (replaces Box-returning functions):
hashsig_keypair_generate_into(out: *mut KeyPair, ...)— writes viastd::ptr::write, returns i32hashsig_keypair_from_ssz_into,hashsig_sign_into,hashsig_signature_from_ssz_into,hashsig_public_key_from_ssz_into— same patternDrop-in-place (replaces
Box::from_rawfrees):hashsig_keypair_deinit/hashsig_signature_deinit/hashsig_public_key_deinit— callstd::ptr::drop_in_place, do NOT free the buffer (caller owns it)Removed:
hashsig_keypair_generate,hashsig_keypair_from_ssz,hashsig_keypair_free,hashsig_sign,hashsig_signature_free,hashsig_signature_from_ssz,hashsig_public_key_from_ssz,hashsig_public_key_freepkgs/xmss/src/hashsig.zigKeyPair,Signature,PublicKeywrappers now own their storage viacAlloc/cFree(thin wrappers overstd.c.malloc/std.c.free)max_align_t(>=8 bytes on LP64), satisfying alignment of any Rust#[repr(C)]type in hashsig-gluegenerate,fromSsz,sign,verify,toBytes,deinitmethods; callers in aggregation.zig unaffectedPublicKeyCacheslot semantics unchanged;deinitcorrectly reconstructs_buf+handlefrom the stored integer pointerWhat this does NOT eliminate
Vec<u8>data insideXmssPublicKey/XmssSignature/XmssPrivateKey— inherent to the leansig API; freed correctly whendrop_in_placeruns DropVec<u8>into_bytes()serialisation helpers — leansig API constraint; short-lived temporaries that do not escape the FFI callmultisig-glue— already used the caller-supplies-buffer pattern for proof bytes; no changes neededVerification
cargo check -p hashsig-gluepasses cleanlyzig ast-checkonhashsig.zig,aggregation.zig,lib.zigall passcc @GrapeBaBa