Skip to content

Commit

Permalink
p384: scalar multiplication test vectors (#570)
Browse files Browse the repository at this point in the history
Tests scalar multiplication using vectors from:

http://point-at-infinity.org/ecc/nisttv
  • Loading branch information
tarcieri authored May 27, 2022
1 parent 38ea146 commit 1f35666
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 94 deletions.
29 changes: 26 additions & 3 deletions p384/src/arithmetic/projective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ impl TryFrom<&ProjectivePoint> for PublicKey {

#[cfg(test)]
mod tests {
use super::{AffinePoint, ProjectivePoint};
use crate::test_vectors::group::ADD_TEST_VECTORS;
use elliptic_curve::group::prime::PrimeCurveAffine;
use super::{AffinePoint, ProjectivePoint, Scalar};
use crate::test_vectors::group::{ADD_TEST_VECTORS, MUL_TEST_VECTORS};
use elliptic_curve::{group::prime::PrimeCurveAffine, PrimeField};

#[test]
fn affine_to_projective() {
Expand Down Expand Up @@ -708,4 +708,27 @@ mod tests {
let generator = ProjectivePoint::GENERATOR;
assert_eq!(generator.double() - &generator, generator);
}

#[test]
fn test_vector_scalar_mult() {
let generator = ProjectivePoint::GENERATOR;

for (k, coords) in ADD_TEST_VECTORS
.iter()
.enumerate()
.map(|(k, coords)| (Scalar::from(k as u64 + 1), *coords))
.chain(
MUL_TEST_VECTORS
.iter()
.cloned()
.map(|(k, x, y)| (Scalar::from_repr(k.into()).unwrap(), (x, y))),
)
{
// dbg!(&k);

let res = (generator * &k).to_affine();
assert_eq!(res.x.to_sec1(), coords.0.into());
assert_eq!(res.y.to_sec1(), coords.1.into());
}
}
}
2 changes: 1 addition & 1 deletion p384/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl From<ScalarCore<NistP384>> for Scalar {
impl From<u64> for Scalar {
fn from(n: u64) -> Scalar {
let mut limbs = NonMontFe::default();
limbs[limbs.len() - 1] = n;
limbs[0] = n;
let mut fe = Fe::default();
fiat_p384_scalar_to_montgomery(&mut fe, &limbs);
Scalar(fe)
Expand Down
1 change: 0 additions & 1 deletion p384/src/test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

#[cfg(test)]
pub mod ecdsa;
//pub mod field;
pub mod group;
Loading

0 comments on commit 1f35666

Please sign in to comment.