Skip to content

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

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

fix(rust-client): replace RandomCoin with ChaCha20Rng#2414
ricomateo wants to merge 16 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.

Comment thread crates/rust-client/src/lib.rs Outdated
/// 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

Comment thread crates/rust-client/src/lib.rs Outdated
/// 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.

Comment thread crates/rust-client/src/lib.rs Outdated
///
/// 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

@ricomateo

Copy link
Copy Markdown
Contributor Author

@juan518munoz thanks! all comments were addressed in this commit e3b4748.

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.

We keep both secure_rng and rng because rng may be a predictable RNG (since the caller can override it with ClientBuilder::rng, passing an RNG initialized with a small seed), which makes it unsafe, so a separate RNG (secure_rng) is used for account keys and transaction input encryption, as pointed out in this comment.


#[cfg(test)]
mod tests {
/// Checks that [`ClientBuilder::rng`] rejects a generator that is not a `CryptoRng`.

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.

this is already enforced by the compiler in the type system, we can remove this test IMO

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.

If we remove the test, then we should remove this file

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

LGTM. Before merging this, it may be a good idea to create a branch on web-sdk that points to this branch, so we can assure there won't be any setbacks.

@ricomateo

Copy link
Copy Markdown
Contributor Author

@juan518munoz I created a draft PR in the web-sdk pinning the rust-sdk dependency to this branch, to check this PR doesn't break the web client. It required some changes but the CI succeeds.

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

3 participants