Skip to content

Make circuit crate generic over BitzIntRing - #135

Merged
zkfriendly merged 5 commits into
mainfrom
fs/ring
Sep 28, 2026
Merged

zkfriendly merged 5 commits into
mainfrom
fs/ring

Conversation

@frozenspider

@frozenspider frozenspider commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Goes toward #136

  • Introduce BitzSemiring and BitzRing traits (with blanket implementations) and abstract circuit crate over it as much as was viable.
    • The only implementations we use today are still BigUint and BigInt respectively, but they aren't hardcoded anymore (except for p256, there we merely alias them)
  • Also introduce Bits and IntoWords traits to help this abstraction.
  • Slightly tweak code toward more idiomatic approach - From trait, LazyLock, thiserror crate.

Nothing in this PR changes observable behaviour.

This is made into a separate self-contained PR in order to make the overall rework more granular.

@frozenspider frozenspider changed the title [WIP] Make circuit crate generic over BitzIntRing Make circuit crate generic over BitzIntRing Sep 24, 2026
@frozenspider
frozenspider marked this pull request as ready for review September 24, 2026 13:41

@zkfriendly zkfriendly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks. Left a minor comment

Comment thread crates/common/src/lib.rs

impl<T> BitzSemiring for T where T: Semiring + From<u64> {}

// Since BigInt does not support CheckedNeg and CheckedRem, we can't use Ring here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting workaround of crypto-primitives interface. I explored this a bit, and I was wondering if we can relax the CheckedNeg bound upstream? Also since SemiRing already requires checked_sub one could implement checked_neg like:

fn checked_neg(&self, x: &R) -> Option<R> {
    R::zero().checked_sub(x)
}

@frozenspider frozenspider Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crypto-primitives strives to be generic, and having checked sub/rem sounds like a reasonable thing for a ring consumer to use. Also, we can't use this workaround as a blanket for semirings since it would interfere with upstream types that provide actual CheckedNeg.
What we can do upstream though is to make a BigInt wrapper similarly to crypto_bigint::Int one, but that doesn't seem necessary in this case.

Btw here's the PR I opened to BigInt to implement missing traits: rust-num/num-bigint#357

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

Comment thread crates/circuit/src/lib.rs Outdated
@zkfriendly
zkfriendly merged commit 89bf741 into main Sep 28, 2026
3 checks passed
@zkfriendly
zkfriendly deleted the fs/ring branch September 28, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants