Skip to content

Commit f1b5644

Browse files
authored
Add getrandom feature and Random trait support (#1057)
Renames the following methods of the `Random` trait: - `Random::random` => `Random::random_from_rng` - `Random::try_random` => `Random::try_random_from_rng` And adds new `Random::random` and `Random::try_random` gated on `getrandom` that use the new `getrandom::SysRng` to call the old `rand_core`-based methods.
1 parent 55403bf commit f1b5644

27 files changed

+429
-208
lines changed

Cargo.lock

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ num-traits = { version = "0.2.19", default-features = false }
2323
# optional dependencies
2424
der = { version = "0.8.0-rc.10", optional = true, default-features = false }
2525
hybrid-array = { version = "0.4.5", optional = true }
26+
getrandom = { version = "0.4.0-rc.0", optional = true, features = ["sys_rng"] }
2627
rand_core = { version = "0.10.0-rc-3", optional = true, default-features = false }
2728
rlp = { version = "0.6", optional = true, default-features = false }
2829
serdect = { version = "0.4", optional = true, default-features = false }
@@ -44,6 +45,7 @@ default = ["rand"]
4445
alloc = ["serdect?/alloc"]
4546

4647
extra-sizes = []
48+
getrandom = ["dep:getrandom", "rand"]
4749
rand = ["rand_core"]
4850
serde = ["dep:serdect"]
4951
subtle = ["dep:subtle", "ctutils/subtle", "hybrid-array?/subtle"]

benches/const_monty.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn bench_montgomery_conversion<M: Measurement>(group: &mut BenchmarkGroup<'_, M>
2929

3030
group.bench_function("ConstMontyForm retrieve", |b| {
3131
b.iter_batched(
32-
|| ConstMontyForm::random(&mut rng),
32+
|| ConstMontyForm::random_from_rng(&mut rng),
3333
|x| black_box(x.retrieve()),
3434
BatchSize::SmallInput,
3535
)
@@ -42,8 +42,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
4242
group.bench_function("add, U256", |b| {
4343
b.iter_batched(
4444
|| {
45-
let a = ConstMontyForm::random(&mut rng);
46-
let b = ConstMontyForm::random(&mut rng);
45+
let a = ConstMontyForm::random_from_rng(&mut rng);
46+
let b = ConstMontyForm::random_from_rng(&mut rng);
4747
(a, b)
4848
},
4949
|(a, b)| black_box(a).add(&black_box(b)),
@@ -53,7 +53,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
5353

5454
group.bench_function("double, U256", |b| {
5555
b.iter_batched(
56-
|| ConstMontyForm::random(&mut rng),
56+
|| ConstMontyForm::random_from_rng(&mut rng),
5757
|a| black_box(a).double(),
5858
BatchSize::SmallInput,
5959
)
@@ -62,8 +62,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
6262
group.bench_function("sub, U256", |b| {
6363
b.iter_batched(
6464
|| {
65-
let a = ConstMontyForm::random(&mut rng);
66-
let b = ConstMontyForm::random(&mut rng);
65+
let a = ConstMontyForm::random_from_rng(&mut rng);
66+
let b = ConstMontyForm::random_from_rng(&mut rng);
6767
(a, b)
6868
},
6969
|(a, b)| black_box(a).sub(&black_box(b)),
@@ -73,15 +73,15 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
7373

7474
group.bench_function("neg, U256", |b| {
7575
b.iter_batched(
76-
|| ConstMontyForm::random(&mut rng),
76+
|| ConstMontyForm::random_from_rng(&mut rng),
7777
|a| black_box(a).neg(),
7878
BatchSize::SmallInput,
7979
)
8080
});
8181

8282
group.bench_function("invert, U256", |b| {
8383
b.iter_batched(
84-
|| ConstMontyForm::random(&mut rng),
84+
|| ConstMontyForm::random_from_rng(&mut rng),
8585
|x| black_box(x).invert(),
8686
BatchSize::SmallInput,
8787
)
@@ -90,8 +90,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
9090
group.bench_function("multiplication, U256*U256", |b| {
9191
b.iter_batched(
9292
|| {
93-
let x = ConstMontyForm::random(&mut rng);
94-
let y = ConstMontyForm::random(&mut rng);
93+
let x = ConstMontyForm::random_from_rng(&mut rng);
94+
let y = ConstMontyForm::random_from_rng(&mut rng);
9595
(x, y)
9696
},
9797
|(x, y)| black_box(x).mul(&black_box(y)),
@@ -101,7 +101,7 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
101101

102102
group.bench_function("squaring, U256*U256", |b| {
103103
b.iter_batched(
104-
|| ConstMontyForm::random(&mut rng),
104+
|| ConstMontyForm::random_from_rng(&mut rng),
105105
|x| black_box(x).square(),
106106
BatchSize::SmallInput,
107107
)
@@ -110,8 +110,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
110110
group.bench_function("modpow, U256^U256", |b| {
111111
b.iter_batched(
112112
|| {
113-
let x_m = ConstMontyForm::random(&mut rng);
114-
let p = U256::random(&mut rng) | (U256::ONE << (U256::BITS - 1));
113+
let x_m = ConstMontyForm::random_from_rng(&mut rng);
114+
let p = U256::random_from_rng(&mut rng) | (U256::ONE << (U256::BITS - 1));
115115
(x_m, p)
116116
},
117117
|(x, p)| black_box(x.pow(&p)),
@@ -121,23 +121,23 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
121121

122122
group.bench_function("jacobi_symbol", |b| {
123123
b.iter_batched(
124-
|| ConstMontyForm::random(&mut rng),
124+
|| ConstMontyForm::random_from_rng(&mut rng),
125125
|a| a.jacobi_symbol(),
126126
BatchSize::SmallInput,
127127
)
128128
});
129129

130130
group.bench_function("jacobi_symbol_vartime", |b| {
131131
b.iter_batched(
132-
|| ConstMontyForm::random(&mut rng),
132+
|| ConstMontyForm::random_from_rng(&mut rng),
133133
|a| a.jacobi_symbol_vartime(),
134134
BatchSize::SmallInput,
135135
)
136136
});
137137

138138
group.bench_function("lincomb, U256*U256+U256*U256", |b| {
139139
b.iter_batched(
140-
|| ConstMontyForm::random(&mut rng),
140+
|| ConstMontyForm::random_from_rng(&mut rng),
141141
|a| {
142142
ConstMontyForm::lincomb(&[
143143
(black_box(a), black_box(a)),
@@ -157,8 +157,9 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
157157
|| {
158158
let bases_and_exponents: Vec<(ConstMontyForm, U256)> = (1..=i)
159159
.map(|_| {
160-
let x_m = ConstMontyForm::random(&mut rng);
161-
let p = U256::random(&mut rng) | (U256::ONE << (U256::BITS - 1));
160+
let x_m = ConstMontyForm::random_from_rng(&mut rng);
161+
let p = U256::random_from_rng(&mut rng)
162+
| (U256::ONE << (U256::BITS - 1));
162163
(x_m, p)
163164
})
164165
.collect();
@@ -185,5 +186,4 @@ fn bench_montgomery(c: &mut Criterion) {
185186
}
186187

187188
criterion_group!(benches, bench_montgomery);
188-
189189
criterion_main!(benches);

0 commit comments

Comments
 (0)