Skip to content

fix(rust-client): replace RandomCoin with ChaCha20Rng - #2414

Open
ricomateo wants to merge 12 commits into
nextfrom
replace-random-coin
Open

fix(rust-client): replace RandomCoin with ChaCha20Rng#2414
ricomateo wants to merge 12 commits into
nextfrom
replace-random-coin

Conversation

@ricomateo

@ricomateo ricomateo commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Closes #2204

  • Replaces the usage of RandomCoin with ChaCha20Rng.
  • ClientBuilder::rng now requires the RNG to implement CryptoRng (i.e. RandomCoin is no longer accepted, since it is not a CryptoRng).
  • Added Client::secure_rng, a second RNG intended to be used to generate randomness for secret values.
  • Added a trybuild test asserting that ClientBuilder::rng rejects a FeltRng which is not a CryptoRng.

@ricomateo
ricomateo marked this pull request as ready for review August 19, 2026 15:51
@ricomateo ricomateo changed the title fix(rust-client): replace RandomCoin fix(rust-client): replace RandomCoin with ChaCha20Rng Aug 19, 2026

@juan518munoz juan518munoz 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.

Looks good overall. What I still don't understand is why we are keeping rng and secure_rng separately, for what I see we could just get away with the new secure implementation in all places.

If I'm not mistaken, keeping a single rng would also make the trybuild test redundant.

/// the field modulus, which keeps the result uniform over the field. The rejection
/// probability is about 2^-32.
pub fn draw_felt(rng: &mut impl rand::Rng) -> crate::Felt {
use rand::RngExt;

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.

we should move the import outside this function

/// Uses rejection sampling: [`Felt::new`](crate::Felt::new) rejects any `u64` at or beyond
/// the field modulus, which keeps the result uniform over the field. The rejection
/// probability is about 2^-32.
pub fn draw_felt(rng: &mut impl rand::Rng) -> crate::Felt {

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.

Maybe this functions are better suited in a separate file, similar to how we do with utils.rs, we could add an rng.rs file.

///
/// Use this for note serial numbers when building notes from a plain [`rand`] generator, which
/// does not implement [`FeltRng`].
pub fn draw_word(rng: &mut impl rand::Rng) -> crate::Word {

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.

Same as other comment

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.

Investigate replacing RandomCoin

2 participants