Feat: Kaspa-Math 32 bit malachite support#889
Draft
hmoog wants to merge 3 commits intokaspanet:masterfrom
Draft
Conversation
| // feature is enabled (e.g. by risc0 via Cargo feature unification). | ||
| // On little-endian, [u64; N] and [Limb; N * 8/sizeof(Limb)] have | ||
| // identical byte layouts, so we reinterpret directly without copying. | ||
| #[cfg(not(target_endian = "little"))] |
Contributor
There was a problem hiding this comment.
we can allow not(target_endian = "little") if core::mem::size_of::<u64>() == core::mem::size_of::<Limb>()
Collaborator
|
https://github.com/biryukovmaxim/rusty-kaspa/blob/e77acc2afb1950ffdffb568e467cd4f6005761d3/Cargo.toml#L222-L223 |
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.
This PR fixes a compilation failure when kaspa-math is used in a workspace alongside risc0-based crates.
Problem
kaspa-math's mod_inverse passes its internal [u64; N] limb array directly to malachite's Natural::from_limbs_asc(&[Limb]), assuming Limb = u64. This breaks when malachite's 32_bit_limbs feature is enabled, causing a type
mismatch (expected &[u32], found &[u64; N]).
Root cause: Cargo feature unification
malachite-nz has an optional 32_bit_limbs feature that switches Limb from u64 to u32. By default it is disabled, so Limb = u64 and kaspa-math compiles fine in the rusty-kaspa workspace.
However, Cargo unifies features across an entire workspace. In the vprogs workspace, risc0-circuit-rv32im (RISC Zero zkVM) depends on malachite with:
features = ["naturals_and_integers", "32_bit_limbs"]This activates 32_bit_limbs globally - including for kaspa-math - making Limb = u32 and breaking mod_inverse.
Fix
Make mod_inverse generic over the Limb type by converting between the internal u64 words and malachite Limbs at the boundary:
The size_of::() check is a compile-time constant, so the compiler eliminates the unused branch entirely - zero runtime cost.