Skip to content

refactor: use Distribution trait for random samplers#824

Open
Farukest wants to merge 1 commit into0xMiden:nextfrom
Farukest:feat/refactor-random-samplers-distribution
Open

refactor: use Distribution trait for random samplers#824
Farukest wants to merge 1 commit into0xMiden:nextfrom
Farukest:feat/refactor-random-samplers-distribution

Conversation

@Farukest
Copy link
Contributor

Closes #722

This PR refactors random samplers across the codebase to use the Distribution trait from the rand crate.

Changes

  • Implement Distribution<Word> for StandardUniform, enabling idiomatic rng.random::<Word>() usage
  • Update random_felt() to use Uniform::new(0, Felt::ORDER_U64) for proper uniform sampling over the Goldilocks field
  • Update random_word() to delegate to the new Distribution<Word> impl
  • Refactor test helpers in aead_poseidon2, xchacha, ies, and smt tests to use uniform field element sampling
  • Update benchmark random word generation to use the new Distribution impl

Notes

  • Felt is a foreign type alias (p3_goldilocks::Goldilocks), so Distribution<Felt> cannot be implemented due to the orphan rule. Instead, Uniform::new(0, Felt::ORDER_U64) is used directly where needed.
  • Word is a local type, so Distribution<Word> for StandardUniform is implemented in word/mod.rs.
  • The existing Distribution impls for SecretKey/Nonce in aead_poseidon2 are kept as-is (out of scope per Refactor all random samplers to use Distribution #722).

@Farukest Farukest force-pushed the feat/refactor-random-samplers-distribution branch 2 times, most recently from fe96db0 to 64c8de5 Compare February 11, 2026 13:12
Implement `Distribution<Word>` for `StandardUniform` and update all
random field element sampling to use `Uniform::new(0, Felt::ORDER_U64)`
for proper uniform distribution over the Goldilocks field.

- Add `Distribution<Word> for StandardUniform` in word/mod.rs
- Update `random_felt()` and `random_word()` in rand/mod.rs
- Refactor test helpers in aead, ies, and smt tests
- Update benchmark random word generation

Closes 0xMiden#722
@Farukest Farukest force-pushed the feat/refactor-random-samplers-distribution branch from 64c8de5 to fec9f0b Compare February 14, 2026 05: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.

Refactor all random samplers to use Distribution

1 participant