Skip to content

Commit a041a75

Browse files
authored
Use &Digest in ECDSA to avoid copies
Modified digest_scalar, sign_digest, and verification to pass digest::Digest by reference (&Digest) instead of by value, preventing implicit copies via Digest's Copy trait implementation, thus causing 64 byte saving in copying for each &h.
1 parent 081fe57 commit a041a75

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

src/ec/suite_b/ecdsa/digest_scalar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{digest, ec::suite_b::ops::*};
4242
/// right will give a value less than 2**255, which is less than `n`. The
4343
/// analogous argument applies for P-384. However, it does *not* apply in
4444
/// general; for example, it doesn't apply to P-521.
45-
pub(super) fn digest_scalar(n: &Modulus<N>, msg: digest::Digest) -> Scalar {
45+
pub(super) fn digest_scalar(n: &Modulus<N>, msg: &digest::Digest) -> Scalar {
4646
digest_scalar_(n, msg.as_ref())
4747
}
4848

src/ec/suite_b/ecdsa/signing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl EcdsaKeyPair {
189189
rng,
190190
};
191191

192-
self.sign_digest(h, &nonce_rng, cpu)
192+
self.sign_digest(&h, &nonce_rng, cpu)
193193
}
194194

195195
#[cfg(test)]
@@ -201,14 +201,14 @@ impl EcdsaKeyPair {
201201
// Step 4 (out of order).
202202
let h = digest::digest(self.alg.digest_alg, message);
203203

204-
self.sign_digest(h, rng, cpu::features())
204+
self.sign_digest(&h, rng, cpu::features())
205205
}
206206

207207
/// Returns the signature of message digest `h` using a "random" nonce
208208
/// generated by `rng`.
209209
fn sign_digest(
210210
&self,
211-
h: digest::Digest,
211+
h: &digest::Digest,
212212
rng: &dyn rand::SecureRandom,
213213
cpu: cpu::Features,
214214
) -> Result<signature::Signature, error::Unspecified> {

src/ec/suite_b/ecdsa/verification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl signature::VerificationAlgorithm for EcdsaVerificationAlgorithm {
6565
// NSA Guide Step 3: "Convert the bit string H to an integer e as
6666
// described in Appendix B.2."
6767
let n = &self.ops.scalar_ops.scalar_modulus(cpu);
68-
digest_scalar(n, h)
68+
digest_scalar(n, &h)
6969
};
7070

7171
self.verify_digest(public_key, e, signature)

0 commit comments

Comments
 (0)