Skip to content

Commit

Permalink
RSA: Remove q_mod_n from RsaKeyPair.
Browse files Browse the repository at this point in the history
Reduce the size of RsaKeyPair by about 15%.

Importantly, this was the only non-temporary (`'static`) `Elem`
other than `One`.
  • Loading branch information
briansmith committed Nov 10, 2023
1 parent 946ce87 commit 23975ff
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/rsa/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ pub struct KeyPair {
p: PrivatePrime<P>,
q: PrivatePrime<Q>,
qInv: bigint::Elem<P, R>,

// TODO: Eliminate `q_mod_n` entirely since it is a bad space:time trade-off.
// Also, this is the only non-temporary `Elem` so if we eliminate this, we
// can make all `Elem`s temporary (borrowed) values.
q_mod_n: bigint::Elem<N, R>,

public: PublicKey,
}

Expand Down Expand Up @@ -403,7 +397,6 @@ impl KeyPair {
p,
q,
qInv,
q_mod_n,
public: public_key,
})
}
Expand Down Expand Up @@ -578,7 +571,9 @@ impl KeyPair {
// RFC 8017 Section 5.1.2: RSADP, using the Chinese Remainder Theorem
// with Garner's algorithm.

let n = &self.public.inner().n().value().modulus();
let n = self.public.inner().n().value();
let n_one = n.oneRR();
let n = &n.modulus();

// Step 1. The value zero is also rejected.
let base = bigint::Elem::from_be_bytes_padded(untrusted::Input::from(base), n)?;
Expand All @@ -603,7 +598,9 @@ impl KeyPair {
// Modular arithmetic is used simply to avoid implementing
// non-modular arithmetic.
let h = bigint::elem_widen(h, n);
let q_times_h = bigint::elem_mul(&self.q_mod_n, h, n);
let q_mod_n = self.q.modulus.to_elem(n);
let q_mod_n = bigint::elem_mul(n_one.as_ref(), q_mod_n, n);
let q_times_h = bigint::elem_mul(&q_mod_n, h, n);
let m_2 = bigint::elem_widen(m_2, n);
let m = bigint::elem_add(m_2, q_times_h, n);

Expand Down

0 comments on commit 23975ff

Please sign in to comment.