ec suite_b: Hide COMMON_OPS.n and reduce direct use of .num_limbs.

This commit is contained in:
Brian Smith 2023-12-04 12:26:20 -08:00
parent 2926ebb500
commit e952c64eba
3 changed files with 12 additions and 6 deletions

View File

@ -183,7 +183,7 @@ mod tests {
// getting that value from the PRNG.
let mut n_bytes = [0u8; ec::SCALAR_MAX_BYTES];
let num_bytes = curve.elem_scalar_seed_len;
limb::big_endian_from_limbs(&ops.n.limbs[..ops.num_limbs], &mut n_bytes[..num_bytes]);
limb::big_endian_from_limbs(ops.n_limbs(), &mut n_bytes[..num_bytes]);
{
let n_bytes = &mut n_bytes[..num_bytes];
let rng = test::rand::FixedSliceRandom { bytes: n_bytes };

View File

@ -157,10 +157,7 @@ impl EcdsaVerificationAlgorithm {
return Ok(());
}
if self.ops.elem_less_than(&r, &self.ops.q_minus_n) {
self.ops
.scalar_ops
.common
.elem_add(&mut r, &public_key_ops.common.n);
self.ops.scalar_ops.common.elem_add(&mut r, self.ops.n());
if sig_r_equals_x(self.ops, &r, &x, &z2) {
return Ok(());
}

View File

@ -54,7 +54,7 @@ impl Point {
pub struct CommonOps {
pub num_limbs: usize,
q: Modulus,
pub n: Elem<Unencoded>,
n: Elem<Unencoded>,
pub a: Elem<R>, // Must be -3 mod q
pub b: Elem<R>,
@ -73,6 +73,11 @@ impl CommonOps {
self.num_limbs * LIMB_BYTES
}
#[cfg(test)]
pub(super) fn n_limbs(&self) -> &[Limb] {
&self.n.limbs[..self.num_limbs]
}
#[inline]
pub fn elem_add<E: Encoding>(&self, a: &mut Elem<E>, b: &Elem<E>) {
let num_limbs = self.num_limbs;
@ -280,6 +285,10 @@ pub struct PublicScalarOps {
}
impl PublicScalarOps {
pub fn n(&self) -> &Elem<Unencoded> {
&self.scalar_ops.common.n
}
#[inline]
pub fn scalar_as_elem(&self, a: &Scalar) -> Elem<Unencoded> {
Elem {