#[cfg(feature = "arithmetic")]
use {
crate::{Result, SignatureSize},
core::borrow::Borrow,
elliptic_curve_flow::{ops::Invert, ProjectiveArithmetic, Scalar},
};
#[cfg(feature = "digest")]
use {
crate::signature_flow::{digest::Digest, PrehashSignature},
elliptic_curve_flow::FieldSize,
};
#[cfg(any(feature = "arithmetic", feature = "digest"))]
use crate::{
elliptic_curve_flow::{generic_array::ArrayLength, PrimeCurve},
Signature,
};
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait SignPrimitive<C>
where
C: PrimeCurve + ProjectiveArithmetic,
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign_prehashed<K: Borrow<Scalar<C>> + Invert<Output = Scalar<C>>>(
&self,
ephemeral_scalar: &K,
hashed_msg: &Scalar<C>,
) -> Result<Signature<C>>;
}
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait RecoverableSignPrimitive<C>
where
C: PrimeCurve + ProjectiveArithmetic,
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign_recoverable_prehashed<K: Borrow<Scalar<C>> + Invert<Output = Scalar<C>>>(
&self,
ephemeral_scalar: &K,
hashed_msg: &Scalar<C>,
) -> Result<(Signature<C>, bool)>;
}
#[cfg(feature = "arithmetic")]
impl<C, T> SignPrimitive<C> for T
where
C: PrimeCurve + ProjectiveArithmetic,
T: RecoverableSignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
{
fn try_sign_prehashed<K: Borrow<Scalar<C>> + Invert<Output = Scalar<C>>>(
&self,
ephemeral_scalar: &K,
hashed_msg: &Scalar<C>,
) -> Result<Signature<C>> {
self.try_sign_recoverable_prehashed(ephemeral_scalar, hashed_msg)
.map(|res| res.0)
}
}
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait VerifyPrimitive<C>
where
C: PrimeCurve + ProjectiveArithmetic,
SignatureSize<C>: ArrayLength<u8>,
{
fn verify_prehashed(&self, hashed_msg: &Scalar<C>, signature: &Signature<C>) -> Result<()>;
}
#[cfg(feature = "digest")]
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
pub trait DigestPrimitive: PrimeCurve {
type Digest: Digest;
}
#[cfg(feature = "digest")]
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
pub trait FromDigest<C: PrimeCurve> {
fn from_digest<D>(digest: D) -> Self
where
D: Digest<OutputSize = FieldSize<C>>;
}
#[cfg(feature = "digest")]
impl<C> PrehashSignature for Signature<C>
where
C: DigestPrimitive,
<FieldSize<C> as core::ops::Add>::Output: ArrayLength<u8>,
{
type Digest = C::Digest;
}