Skip to content

Commit 97fd5ae

Browse files
Clean up GCD API (#923)
This mostly avoids exposing the separate `safegcd`/`bingcd` implementations. Both use the same pre-processing for inputs so the active variant only needs to be selected in `Odd<Uint>`. The `safegcd(_vartime)` and `bingcd(_vartime)` methods are only exposed for benchmarking at the moment. Because a `const gcd` method on each integer type would have a higher priority than the trait method, it would produce type errors when trying to compute the GCD between different types (like `Uint` and `Int`). I've gone with the name `gcd_uint` for these methods. --------- Signed-off-by: Andrew Whitehead <[email protected]>
1 parent 41f3ea5 commit 97fd5ae

File tree

7 files changed

+272
-373
lines changed

7 files changed

+272
-373
lines changed

benches/uint.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,42 @@ fn gcd_bench<const LIMBS: usize>(g: &mut BenchmarkGroup<WallTime>, _x: Uint<LIMB
427427
BatchSize::SmallInput,
428428
)
429429
});
430+
431+
g.bench_function(BenchmarkId::new("safegcd", LIMBS), |b| {
432+
b.iter_batched(
433+
|| {
434+
(
435+
OddUint::<LIMBS>::random(&mut rng),
436+
Uint::<LIMBS>::random(&mut rng),
437+
)
438+
},
439+
|(f, g)| black_box(f.safegcd(&g)),
440+
BatchSize::SmallInput,
441+
)
442+
});
443+
444+
g.bench_function(BenchmarkId::new("safegcd_vartime", LIMBS), |b| {
445+
b.iter_batched(
446+
|| {
447+
(
448+
OddUint::<LIMBS>::random(&mut rng),
449+
Uint::<LIMBS>::random(&mut rng),
450+
)
451+
},
452+
|(f, g)| black_box(f.safegcd_vartime(&g)),
453+
BatchSize::SmallInput,
454+
)
455+
});
430456
}
431457

432458
fn bench_gcd(c: &mut Criterion) {
433459
let mut group = c.benchmark_group("greatest common divisor");
434460

435461
gcd_bench(&mut group, Uint::<1>::ZERO);
436462
gcd_bench(&mut group, Uint::<2>::ZERO);
437-
gcd_bench(&mut group, Uint::<3>::ZERO);
438463
gcd_bench(&mut group, Uint::<4>::ZERO);
439-
gcd_bench(&mut group, Uint::<5>::ZERO);
440-
gcd_bench(&mut group, Uint::<6>::ZERO);
441-
gcd_bench(&mut group, Uint::<7>::ZERO);
442464
gcd_bench(&mut group, Uint::<8>::ZERO);
443-
gcd_bench(&mut group, Uint::<16>::ZERO);
444-
gcd_bench(&mut group, Uint::<32>::ZERO);
445465
gcd_bench(&mut group, Uint::<64>::ZERO);
446-
gcd_bench(&mut group, Uint::<128>::ZERO);
447466
gcd_bench(&mut group, Uint::<256>::ZERO);
448467

449468
group.finish();

0 commit comments

Comments
 (0)