ecqv 0.1.0

SEC 4 ECQV implicit certificates on P-256: reconstruct a public key from a certificate and the issuer's key, with no signature to carry
Documentation
//! ECQV protocol operations.
//!
//! These are the SEC 4 scheme primitives over P-256, expressed as pure
//! functions so the known-answer harness can drive each role independently.
//! The higher-level types from the API design (`CertificateAuthority`,
//! `CertificateRequest`, `UnvalidatedPublicKey`, ...) are built on top of
//! these.
//!
//! SEC 4 references: Cert Generate 3.4, Cert PK Extraction 3.5, Cert
//! Reception 3.6.

use p256::{AffinePoint, ProjectivePoint, Scalar};

use crate::hash::hash_to_scalar;

/// SEC 4 v1.0 section 2.3: `e = Hn(CertU)`, the leftmost 255 bits of the
/// SHA-256 digest. See `src/hash.rs` for why this is truncation (right shift
/// by one), never reduction mod n.
fn derive_e(cert: &[u8]) -> Scalar {
    hash_to_scalar(cert)
}

/// CA side: `PU = RU + k*G`, the user's public key reconstruction point.
/// `k` is the CA's per-certificate nonce.
pub fn reconstruction_point(request_pk: &AffinePoint, k: &Scalar) -> AffinePoint {
    AffinePoint::from(ProjectivePoint::from(*request_pk) + ProjectivePoint::GENERATOR * k)
}

/// CA side: `r = e*k + d_CA (mod n)`, the private key reconstruction value.
/// `e` is derived from the certificate bytes (the vector's exact `CertU`).
pub fn issue_private_value(cert: &[u8], k: &Scalar, d_ca: &Scalar) -> Scalar {
    let e = derive_e(cert);
    e * k + d_ca
}

/// Requester side: `d_U = e*k_U + r (mod n)`, the user's reconstructed private
/// key. The requester MUST then check `d_U*G == Q_U` before using the key.
pub fn requester_private_key(cert: &[u8], k_u: &Scalar, r: &Scalar) -> Scalar {
    let e = derive_e(cert);
    e * k_u + r
}

/// Any party: `Q_U = e*P_U + Q_CA`, the user's reconstructed public key.
/// This always succeeds; the result is an unvalidated key until it is used.
pub fn user_public_key(cert: &[u8], p_u: &AffinePoint, q_ca: &AffinePoint) -> AffinePoint {
    let e = derive_e(cert);
    AffinePoint::from(ProjectivePoint::from(*p_u) * e + ProjectivePoint::from(*q_ca))
}

/// Helper for the requester-side self-check: `Q == d*G`.
pub fn public_from_private(d: &Scalar) -> AffinePoint {
    AffinePoint::from(ProjectivePoint::GENERATOR * d)
}