Skip to content

Commit 78f70ae

Browse files
authored
Rename BernsteinYang* to SafeGcd* (#655)
And `bernstein_yang` => `safegcd` for snake case. I've been noticing that in code, the algorithm tends to be referred to using the shorter "safegcd" name as opposed to the much longer "Bernstein-Yang". This renames the implementation accordingly.
1 parent ce240f1 commit 78f70ae

File tree

18 files changed

+72
-81
lines changed

18 files changed

+72
-81
lines changed

src/const_choice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use subtle::{Choice, CtOption};
22

3-
use crate::{modular::BernsteinYangInverter, Limb, NonZero, Odd, Uint, WideWord, Word};
3+
use crate::{modular::SafeGcdInverter, Limb, NonZero, Odd, Uint, WideWord, Word};
44

55
/// A boolean value returned by constant-time `const fn`s.
66
// TODO: should be replaced by `subtle::Choice` or `CtOption`
@@ -428,7 +428,7 @@ impl ConstCtOption<NonZero<Limb>> {
428428
}
429429

430430
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
431-
ConstCtOption<BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>>
431+
ConstCtOption<SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>>
432432
{
433433
/// Returns the contained value, consuming the `self` value.
434434
///
@@ -437,7 +437,7 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
437437
/// Panics if the value is none with a custom panic message provided by
438438
/// `msg`.
439439
#[inline]
440-
pub const fn expect(self, msg: &str) -> BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS> {
440+
pub const fn expect(self, msg: &str) -> SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS> {
441441
assert!(self.is_some.is_true_vartime(), "{}", msg);
442442
self.value
443443
}

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ macro_rules! nlimbs {
1515
/// We need to ensure that:
1616
///
1717
/// ```text
18-
/// $bits <= (bernstein_yang_nlimbs($bits) * 62) - 64
18+
/// $bits <= (safegcd_nlimbs($bits) * 62) - 64
1919
/// ```
2020
// TODO(tarcieri): replace with `generic_const_exprs` (rust-lang/rust#76560) when stable
21-
macro_rules! bernstein_yang_nlimbs {
21+
macro_rules! safegcd_nlimbs {
2222
($bits:expr) => {
2323
($bits + 64).div_ceil(62)
2424
};

src/modular.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ mod monty_form;
2121
mod reduction;
2222

2323
mod add;
24-
pub(crate) mod bernstein_yang;
2524
mod div_by_2;
2625
mod mul;
2726
mod pow;
27+
pub(crate) mod safegcd;
2828
mod sub;
2929

3030
#[cfg(feature = "alloc")]
3131
pub(crate) mod boxed_monty_form;
3232

3333
pub use self::{
34-
bernstein_yang::BernsteinYangInverter,
3534
const_monty_form::{inv::ConstMontyFormInverter, ConstMontyForm, ConstMontyParams},
3635
monty_form::{inv::MontyFormInverter, MontyForm, MontyParams},
3736
reduction::montgomery_reduction,
37+
safegcd::SafeGcdInverter,
3838
};
3939

4040
#[cfg(feature = "alloc")]
4141
pub use self::{
42-
bernstein_yang::boxed::BoxedBernsteinYangInverter,
4342
boxed_monty_form::{BoxedMontyForm, BoxedMontyParams},
43+
safegcd::boxed::BoxedSafeGcdInverter,
4444
};
4545

4646
/// A generalization for numbers kept in optimized representations (e.g. Montgomery)

src/modular/boxed_monty_form/inv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::{BoxedMontyForm, BoxedMontyParams};
44
use crate::{
5-
modular::BoxedBernsteinYangInverter, Invert, Inverter, PrecomputeInverter,
5+
modular::BoxedSafeGcdInverter, Invert, Inverter, PrecomputeInverter,
66
PrecomputeInverterWithAdjuster,
77
};
88
use alloc::sync::Arc;
@@ -40,7 +40,7 @@ impl PrecomputeInverter for BoxedMontyParams {
4040
/// Bernstein-Yang inverter which inverts [`DynResidue`] types.
4141
pub struct BoxedMontyFormInverter {
4242
/// Precomputed Bernstein-Yang inverter.
43-
inverter: BoxedBernsteinYangInverter,
43+
inverter: BoxedSafeGcdInverter,
4444

4545
/// Residue parameters.
4646
params: Arc<BoxedMontyParams>,

src/modular/const_monty_form.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod pow;
88
mod sub;
99

1010
use self::inv::ConstMontyFormInverter;
11-
use super::{div_by_2::div_by_2, reduction::montgomery_reduction, BernsteinYangInverter, Retrieve};
11+
use super::{div_by_2::div_by_2, reduction::montgomery_reduction, Retrieve, SafeGcdInverter};
1212
use crate::{ConstZero, Limb, Odd, PrecomputeInverter, Uint};
1313
use core::{fmt::Debug, marker::PhantomData};
1414
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -56,7 +56,7 @@ pub trait ConstMontyParams<const LIMBS: usize>:
5656
fn precompute_inverter<const UNSAT_LIMBS: usize>() -> ConstMontyFormInverter<Self, LIMBS>
5757
where
5858
Odd<Uint<LIMBS>>: PrecomputeInverter<
59-
Inverter = BernsteinYangInverter<LIMBS, UNSAT_LIMBS>,
59+
Inverter = SafeGcdInverter<LIMBS, UNSAT_LIMBS>,
6060
Output = Uint<LIMBS>,
6161
>,
6262
{

src/modular/const_monty_form/inv.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::{ConstMontyForm, ConstMontyParams};
44
use crate::{
5-
modular::BernsteinYangInverter, ConstCtOption, Invert, Inverter, Odd, PrecomputeInverter, Uint,
5+
modular::SafeGcdInverter, ConstCtOption, Invert, Inverter, Odd, PrecomputeInverter, Uint,
66
};
77
use core::{fmt, marker::PhantomData};
88
use subtle::CtOption;
@@ -11,7 +11,7 @@ impl<MOD: ConstMontyParams<SAT_LIMBS>, const SAT_LIMBS: usize, const UNSAT_LIMBS
1111
ConstMontyForm<MOD, SAT_LIMBS>
1212
where
1313
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
14-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
14+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
1515
Output = Uint<SAT_LIMBS>,
1616
>,
1717
{
@@ -39,7 +39,7 @@ impl<MOD: ConstMontyParams<SAT_LIMBS>, const SAT_LIMBS: usize, const UNSAT_LIMBS
3939
for ConstMontyForm<MOD, SAT_LIMBS>
4040
where
4141
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
42-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
42+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
4343
Output = Uint<SAT_LIMBS>,
4444
>,
4545
{
@@ -62,13 +62,13 @@ impl<MOD: ConstMontyParams<SAT_LIMBS>, const SAT_LIMBS: usize, const UNSAT_LIMBS
6262
ConstMontyFormInverter<MOD, SAT_LIMBS>
6363
where
6464
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
65-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
65+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
6666
Output = Uint<SAT_LIMBS>,
6767
>,
6868
{
6969
/// Create a new [`ConstMontyFormInverter`] for the given [`ConstMontyParams`].
7070
pub const fn new() -> Self {
71-
let inverter = BernsteinYangInverter::new(&MOD::MODULUS, &MOD::R2);
71+
let inverter = SafeGcdInverter::new(&MOD::MODULUS, &MOD::R2);
7272

7373
Self {
7474
inverter,
@@ -96,7 +96,7 @@ impl<MOD: ConstMontyParams<SAT_LIMBS>, const SAT_LIMBS: usize, const UNSAT_LIMBS
9696
for ConstMontyFormInverter<MOD, SAT_LIMBS>
9797
where
9898
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
99-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
99+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
100100
Output = Uint<SAT_LIMBS>,
101101
>,
102102
{
@@ -111,7 +111,7 @@ impl<MOD: ConstMontyParams<SAT_LIMBS>, const SAT_LIMBS: usize, const UNSAT_LIMBS
111111
for ConstMontyFormInverter<MOD, SAT_LIMBS>
112112
where
113113
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
114-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
114+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
115115
Output = Uint<SAT_LIMBS>,
116116
>,
117117
{

src/modular/monty_form/inv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
33
use super::{MontyForm, MontyParams};
44
use crate::{
5-
modular::BernsteinYangInverter, traits::Invert, ConstCtOption, Inverter, Odd,
6-
PrecomputeInverter, PrecomputeInverterWithAdjuster, Uint,
5+
modular::SafeGcdInverter, traits::Invert, ConstCtOption, Inverter, Odd, PrecomputeInverter,
6+
PrecomputeInverterWithAdjuster, Uint,
77
};
88
use core::fmt;
99
use subtle::CtOption;
1010

1111
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> MontyForm<SAT_LIMBS>
1212
where
1313
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
14-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
14+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
1515
Output = Uint<SAT_LIMBS>,
1616
>,
1717
{
@@ -40,7 +40,7 @@ where
4040
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Invert for MontyForm<SAT_LIMBS>
4141
where
4242
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
43-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
43+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
4444
Output = Uint<SAT_LIMBS>,
4545
>,
4646
{
@@ -97,7 +97,7 @@ where
9797
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> fmt::Debug for MontyFormInverter<SAT_LIMBS>
9898
where
9999
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
100-
Inverter = BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>,
100+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
101101
Output = Uint<SAT_LIMBS>,
102102
>,
103103
{

src/modular/bernstein_yang.rs renamed to src/modular/safegcd.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
//! Implementation of Bernstein-Yang modular inversion and GCD algorithm as described in:
2-
//! <https://eprint.iacr.org/2019/266>.
1+
//! Implementation of Bernstein-Yang modular inversion and GCD algorithm (a.k.a. safegcd)
2+
//! as described in: <https://eprint.iacr.org/2019/266>.
33
//!
44
//! Adapted from the Apache 2.0+MIT licensed implementation originally from:
5+
//! <https://github.com/taikoxyz/halo2curves/pull/2>
56
//! <https://github.com/privacy-scaling-explorations/halo2curves/pull/83>
67
//!
78
//! Copyright (c) 2023 Privacy Scaling Explorations Team
@@ -44,7 +45,7 @@ use subtle::CtOption;
4445
/// - P. Wuille, "The safegcd implementation in libsecp256k1 explained",
4546
/// <https://github.com/bitcoin-core/secp256k1/blob/master/doc/safegcd_implementation.md>
4647
#[derive(Clone, Debug)]
47-
pub struct BernsteinYangInverter<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> {
48+
pub struct SafeGcdInverter<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> {
4849
/// Modulus
4950
pub(super) modulus: UnsatInt<UNSAT_LIMBS>,
5051

@@ -58,9 +59,7 @@ pub struct BernsteinYangInverter<const SAT_LIMBS: usize, const UNSAT_LIMBS: usiz
5859
/// Type of the Bernstein-Yang transition matrix multiplied by 2^62
5960
type Matrix = [[i64; 2]; 2];
6061

61-
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
62-
BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>
63-
{
62+
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS> {
6463
/// Creates the inverter for specified modulus and adjusting parameter.
6564
///
6665
/// Modulus must be odd. Returns `None` if it is not.
@@ -135,7 +134,7 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize>
135134
}
136135

137136
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Inverter
138-
for BernsteinYangInverter<SAT_LIMBS, UNSAT_LIMBS>
137+
for SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>
139138
{
140139
type Output = Uint<SAT_LIMBS>;
141140

@@ -386,7 +385,7 @@ impl<const LIMBS: usize> UnsatInt<LIMBS> {
386385
/// The ordering of the chunks in these arrays is little-endian.
387386
#[allow(trivial_numeric_casts)]
388387
pub const fn from_uint<const SAT_LIMBS: usize>(input: &Uint<SAT_LIMBS>) -> Self {
389-
if LIMBS != bernstein_yang_nlimbs!(SAT_LIMBS * Limb::BITS as usize) {
388+
if LIMBS != safegcd_nlimbs!(SAT_LIMBS * Limb::BITS as usize) {
390389
panic!("incorrect number of limbs");
391390
}
392391

@@ -410,7 +409,7 @@ impl<const LIMBS: usize> UnsatInt<LIMBS> {
410409
"can't convert negative number to Uint"
411410
);
412411

413-
if LIMBS != bernstein_yang_nlimbs!(SAT_LIMBS * Limb::BITS as usize) {
412+
if LIMBS != safegcd_nlimbs!(SAT_LIMBS * Limb::BITS as usize) {
414413
panic!("incorrect number of limbs");
415414
}
416415

@@ -564,7 +563,7 @@ mod tests {
564563

565564
type UnsatInt = super::UnsatInt<4>;
566565

567-
impl<const LIMBS: usize> PartialEq for crate::modular::bernstein_yang::UnsatInt<LIMBS> {
566+
impl<const LIMBS: usize> PartialEq for crate::modular::safegcd::UnsatInt<LIMBS> {
568567
fn eq(&self, other: &Self) -> bool {
569568
self.eq(other).to_bool_vartime()
570569
}

src/modular/bernstein_yang/boxed.rs renamed to src/modular/safegcd/boxed.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//! Implementation of Bernstein-Yang modular inversion and GCD algorithm as described in:
2-
//! <https://eprint.iacr.org/2019/266>.
1+
//! Implementation of Bernstein-Yang modular inversion and GCD algorithm (a.k.a. safegcd)
2+
//! as described in: <https://eprint.iacr.org/2019/266>.
33
//!
44
//! See parent module for more information.
55
@@ -14,9 +14,9 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreate
1414

1515
/// Modular multiplicative inverter based on the Bernstein-Yang method.
1616
///
17-
/// See [`super::BernsteinYangInverter`] for more information.
17+
/// See [`super::SafeGcdInverter`] for more information.
1818
#[derive(Clone, Debug)]
19-
pub struct BoxedBernsteinYangInverter {
19+
pub struct BoxedSafeGcdInverter {
2020
/// Modulus
2121
pub(crate) modulus: BoxedUnsatInt,
2222

@@ -27,7 +27,7 @@ pub struct BoxedBernsteinYangInverter {
2727
inverse: i64,
2828
}
2929

30-
impl BoxedBernsteinYangInverter {
30+
impl BoxedSafeGcdInverter {
3131
/// Creates the inverter for specified modulus and adjusting parameter.
3232
///
3333
/// Modulus must be odd. Returns `None` if it is not.
@@ -50,7 +50,7 @@ impl BoxedBernsteinYangInverter {
5050
}
5151
}
5252

53-
impl Inverter for BoxedBernsteinYangInverter {
53+
impl Inverter for BoxedSafeGcdInverter {
5454
type Output = BoxedUint;
5555

5656
fn invert(&self, value: &BoxedUint) -> CtOption<Self::Output> {
@@ -78,7 +78,7 @@ fn unsat_nlimbs_for_sat_nlimbs(saturated_nlimbs: usize) -> usize {
7878
saturated_nlimbs
7979
};
8080

81-
bernstein_yang_nlimbs!(saturated_nlimbs * Limb::BITS as usize)
81+
safegcd_nlimbs!(saturated_nlimbs * Limb::BITS as usize)
8282
}
8383

8484
/// Returns the greatest common divisor (GCD) of the two given numbers.
@@ -300,10 +300,7 @@ impl BoxedUnsatInt {
300300
bits_precision = 64;
301301
}
302302

303-
debug_assert_eq!(
304-
self.nlimbs(),
305-
bernstein_yang_nlimbs!(bits_precision as usize)
306-
);
303+
debug_assert_eq!(self.nlimbs(), safegcd_nlimbs!(bits_precision as usize));
307304
assert!(
308305
!bool::from(self.is_negative()),
309306
"can't convert negative number to BoxedUint"
@@ -522,7 +519,7 @@ mod tests {
522519
use subtle::ConstantTimeEq;
523520

524521
#[cfg(not(miri))]
525-
use crate::modular::bernstein_yang::UnsatInt;
522+
use crate::modular::safegcd::UnsatInt;
526523

527524
impl PartialEq for BoxedUnsatInt {
528525
fn eq(&self, other: &Self) -> bool {
@@ -669,8 +666,8 @@ mod tests {
669666
#[test]
670667
#[cfg(not(miri))]
671668
fn boxed_unsatint_add(x in u256(), y in u256()) {
672-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
673-
let y_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&y);
669+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
670+
let y_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&y);
674671
let mut x_boxed = BoxedUnsatInt::from(&x.into());
675672
let y_boxed = BoxedUnsatInt::from(&y.into());
676673

@@ -682,7 +679,7 @@ mod tests {
682679
#[test]
683680
#[cfg(not(miri))]
684681
fn boxed_unsatint_mul(x in u256(), y in any::<i64>()) {
685-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
682+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
686683
let x_boxed = BoxedUnsatInt::from(&x.into());
687684

688685
let expected = x_ref.mul(y);
@@ -693,7 +690,7 @@ mod tests {
693690
#[test]
694691
#[cfg(not(miri))]
695692
fn boxed_unsatint_neg(x in u256()) {
696-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
693+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
697694
let x_boxed = BoxedUnsatInt::from(&x.into());
698695

699696
let expected = x_ref.neg();
@@ -704,7 +701,7 @@ mod tests {
704701
#[test]
705702
#[cfg(not(miri))]
706703
fn boxed_unsatint_shr(x in u256()) {
707-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
704+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
708705
let mut x_boxed = BoxedUnsatInt::from(&x.into());
709706
x_boxed.shr_assign();
710707

@@ -716,7 +713,7 @@ mod tests {
716713
#[cfg(not(miri))]
717714

718715
fn boxed_unsatint_is_negative(x in u256()) {
719-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
716+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
720717
let x_boxed = BoxedUnsatInt::from(&x.into());
721718
assert_eq!(x_ref.is_negative().to_bool_vartime(), bool::from(x_boxed.is_negative()));
722719
}
@@ -725,7 +722,7 @@ mod tests {
725722
#[cfg(not(miri))]
726723

727724
fn boxed_unsatint_is_minus_one(x in u256()) {
728-
let x_ref = UnsatInt::<{ bernstein_yang_nlimbs!(256usize) }>::from_uint(&x);
725+
let x_ref = UnsatInt::<{ safegcd_nlimbs!(256usize) }>::from_uint(&x);
729726
let x_boxed = BoxedUnsatInt::from(&x.into());
730727
assert!(bool::from(x_boxed.is_minus_one().ct_eq(&x_ref.eq(&UnsatInt::MINUS_ONE).into())));
731728
}
File renamed without changes.

0 commit comments

Comments
 (0)