Skip to content

Commit

Permalink
rsa: NFC: Eliminate unnecessary Nonnegative -> Modulus conversion.
Browse files Browse the repository at this point in the history
After 5ed0a45 we no longer needs `p`
or `q` in `Nonnegative` form.
  • Loading branch information
briansmith committed Nov 22, 2023
1 parent bc00f7e commit ab2dfe8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
10 changes: 1 addition & 9 deletions src/arithmetic/bigint/modulus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use super::{
super::{montgomery::RR, n0::N0},
BoxedLimbs, Elem, Nonnegative, One, PublicModulus, SmallerModulus, Unencoded,
BoxedLimbs, Elem, One, PublicModulus, SmallerModulus, Unencoded,
};
use crate::{
bits::BitLength,
Expand Down Expand Up @@ -113,14 +113,6 @@ impl<M> OwnedModulusWithOne<M> {
Self::from_boxed_limbs(limbs, cpu_features)
}

pub(crate) fn from_nonnegative(
n: Nonnegative,
cpu_features: cpu::Features,
) -> Result<Self, error::KeyRejected> {
let limbs = BoxedLimbs::new_unchecked(n.into_limbs());
Self::from_boxed_limbs(limbs, cpu_features)
}

fn from_boxed_limbs(
n: BoxedLimbs<M>,
cpu_features: cpu::Features,
Expand Down
7 changes: 1 addition & 6 deletions src/arithmetic/nonnegative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
bits, error,
limb::{self, Limb, LimbMask, LIMB_BYTES},
};
use alloc::{boxed::Box, vec, vec::Vec};
use alloc::{vec, vec::Vec};

/// Nonnegative integers.
pub(crate) struct Nonnegative {
Expand Down Expand Up @@ -45,9 +45,4 @@ impl Nonnegative {
pub fn limbs(&self) -> &[Limb] {
&self.limbs
}

#[inline]
pub fn into_limbs(self) -> Box<[Limb]> {
self.limbs.into_boxed_slice()
}
}
9 changes: 2 additions & 7 deletions src/rsa/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ impl KeyPair {
let dQ = untrusted::Input::from(dQ);
let qInv = untrusted::Input::from(qInv);

let (p, _p_bits) = bigint::Nonnegative::from_be_bytes_with_bit_length(p)
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())?;
let (q, _q_bits) = bigint::Nonnegative::from_be_bytes_with_bit_length(q)
.map_err(|error::Unspecified| KeyRejected::invalid_encoding())?;

// XXX: Some steps are done out of order, but the NIST steps are worded
// in such a way that it is clear that NIST intends for them to be done
// in order. TODO: Does this matter at all?
Expand Down Expand Up @@ -410,12 +405,12 @@ impl<M> PrivatePrime<M> {
/// Constructs a `PrivatePrime` from the private prime `p` and `dP` where
/// dP == d % (p - 1).
fn new(
p: bigint::Nonnegative,
p: untrusted::Input,
dP: untrusted::Input,
n_bits: BitLength,
cpu_features: cpu::Features,
) -> Result<Self, KeyRejected> {
let p = bigint::OwnedModulusWithOne::from_nonnegative(p, cpu_features)?;
let p = bigint::OwnedModulusWithOne::from_be_bytes(p, cpu_features)?;

// 5.c / 5.g:
//
Expand Down

0 comments on commit ab2dfe8

Please sign in to comment.