Skip to content

Commit

Permalink
Make pow work with different sized exponents (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
ycscaly authored Jun 25, 2023
1 parent 3c89a6e commit a491899
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/uint/modular/constant_mod/const_pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ use super::{Residue, ResidueParams};

impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
/// Raises to the `exponent` power.
pub const fn pow(&self, exponent: &Uint<LIMBS>) -> Residue<MOD, LIMBS> {
self.pow_bounded_exp(exponent, Uint::<LIMBS>::BITS)
pub const fn pow<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<RHS_LIMBS>,
) -> Residue<MOD, LIMBS> {
self.pow_bounded_exp(exponent, Uint::<RHS_LIMBS>::BITS)
}

/// Raises to the `exponent` power,
/// with `exponent_bits` representing the number of (least significant) bits
/// to take into account for the exponent.
///
/// NOTE: `exponent_bits` may be leaked in the time pattern.
pub const fn pow_bounded_exp(
pub const fn pow_bounded_exp<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<LIMBS>,
exponent: &Uint<RHS_LIMBS>,
exponent_bits: usize,
) -> Residue<MOD, LIMBS> {
Self {
Expand Down
4 changes: 2 additions & 2 deletions src/uint/modular/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use super::mul::{mul_montgomery_form, square_montgomery_form};
/// `exponent_bits` represents the number of bits to take into account for the exponent.
///
/// NOTE: this value is leaked in the time pattern.
pub const fn pow_montgomery_form<const LIMBS: usize>(
pub const fn pow_montgomery_form<const LIMBS: usize, const RHS_LIMBS: usize>(
x: &Uint<LIMBS>,
exponent: &Uint<LIMBS>,
exponent: &Uint<RHS_LIMBS>,
exponent_bits: usize,
modulus: &Uint<LIMBS>,
r: &Uint<LIMBS>,
Expand Down
13 changes: 10 additions & 3 deletions src/uint/modular/runtime_mod/runtime_pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ use super::DynResidue;

impl<const LIMBS: usize> DynResidue<LIMBS> {
/// Raises to the `exponent` power.
pub const fn pow(&self, exponent: &Uint<LIMBS>) -> DynResidue<LIMBS> {
self.pow_bounded_exp(exponent, Uint::<LIMBS>::BITS)
pub const fn pow<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<RHS_LIMBS>,
) -> DynResidue<LIMBS> {
self.pow_bounded_exp(exponent, Uint::<RHS_LIMBS>::BITS)
}

/// Raises to the `exponent` power,
/// with `exponent_bits` representing the number of (least significant) bits
/// to take into account for the exponent.
///
/// NOTE: `exponent_bits` may be leaked in the time pattern.
pub const fn pow_bounded_exp(&self, exponent: &Uint<LIMBS>, exponent_bits: usize) -> Self {
pub const fn pow_bounded_exp<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<RHS_LIMBS>,
exponent_bits: usize,
) -> Self {
Self {
montgomery_form: pow_montgomery_form(
&self.montgomery_form,
Expand Down

0 comments on commit a491899

Please sign in to comment.