pub struct Signature<C: PrimeCurve> { /* private fields */ }
Expand description
ECDSA signature (fixed-size). Generic over elliptic curve types.
Serialized as fixed-sized big endian scalar values with no added framing:
r
: field element size for the given curve, big-endians
: field element size for the given curve, big-endian
Both r
and s
MUST be non-zero.
For example, in a curve with a 256-bit modulus like NIST P-256 or
secp256k1, r
and s
will both be 32-bytes and serialized as big endian,
resulting in a signature with a total of 64-bytes.
ASN.1 DER-encoded signatures also supported via the
Signature::from_der
and Signature::to_der
methods.
§serde
support
When the serde
feature of this crate is enabled, it provides support for
serializing and deserializing ECDSA signatures using the Serialize
and
Deserialize
traits.
The serialization uses a hexadecimal encoding when used with “human readable” text formats, and a binary encoding otherwise.
Implementations§
source§impl<C> Signature<C>
impl<C> Signature<C>
sourcepub fn from_bytes(bytes: &SignatureBytes<C>) -> Result<Self>
pub fn from_bytes(bytes: &SignatureBytes<C>) -> Result<Self>
Parse a signature from fixed-width bytes, i.e. 2 * the size of
FieldBytes
for a particular curve.
§Returns
Ok(signature)
if ther
ands
components are both in the valid range1..n
when serialized as concatenated big endian integers.Err(err)
if ther
and/ors
component of the signature is out-of-range when interpreted as a big endian integer.
sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
pub fn from_slice(slice: &[u8]) -> Result<Self>
Parse a signature from a byte slice.
sourcepub fn from_der(bytes: &[u8]) -> Result<Self>where
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
pub fn from_der(bytes: &[u8]) -> Result<Self>where
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Parse a signature from ASN.1 DER.
sourcepub fn from_scalars(
r: impl Into<FieldBytes<C>>,
s: impl Into<FieldBytes<C>>,
) -> Result<Self>
pub fn from_scalars( r: impl Into<FieldBytes<C>>, s: impl Into<FieldBytes<C>>, ) -> Result<Self>
Create a Signature
from the serialized r
and s
scalar values
which comprise the signature.
§Returns
Ok(signature)
if ther
ands
components are both in the valid range1..n
when serialized as concatenated big endian integers.Err(err)
if ther
and/ors
component of the signature is out-of-range when interpreted as a big endian integer.
sourcepub fn split_bytes(&self) -> (FieldBytes<C>, FieldBytes<C>)
pub fn split_bytes(&self) -> (FieldBytes<C>, FieldBytes<C>)
Split the signature into its r
and s
components, represented as bytes.
sourcepub fn to_bytes(&self) -> SignatureBytes<C>
pub fn to_bytes(&self) -> SignatureBytes<C>
Serialize this signature as bytes.
sourcepub fn to_der(&self) -> Signature<C>where
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
pub fn to_der(&self) -> Signature<C>where
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Serialize this signature as ASN.1 DER.
source§impl<C> Signature<C>
impl<C> Signature<C>
sourcepub fn r(&self) -> NonZeroScalar<C>
pub fn r(&self) -> NonZeroScalar<C>
Get the r
component of this signature
sourcepub fn s(&self) -> NonZeroScalar<C>
pub fn s(&self) -> NonZeroScalar<C>
Get the s
component of this signature
sourcepub fn split_scalars(&self) -> (NonZeroScalar<C>, NonZeroScalar<C>)
pub fn split_scalars(&self) -> (NonZeroScalar<C>, NonZeroScalar<C>)
Split the signature into its r
and s
scalars.
sourcepub fn normalize_s(&self) -> Option<Self>
pub fn normalize_s(&self) -> Option<Self>
Normalize signature into “low S” form as described in BIP 0062: Dealing with Malleability.
Trait Implementations§
source§impl<C> AssociatedOid for Signature<C>
impl<C> AssociatedOid for Signature<C>
ECDSA ObjectIdentifier
which identifies the digest used by default
with the Signer
and Verifier
traits.
To support non-default digest algorithms, use the SignatureWithOid
type instead.
source§const OID: ObjectIdentifier = _
const OID: ObjectIdentifier = _
source§impl<'de, C> Deserialize<'de> for Signature<C>
impl<'de, C> Deserialize<'de> for Signature<C>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
Sign message digest using a deterministic ephemeral scalar (k
)
computed using the algorithm described in RFC6979 § 3.2.
source§fn try_sign_digest(&self, msg_digest: D) -> Result<Signature<C>>
fn try_sign_digest(&self, msg_digest: D) -> Result<Signature<C>>
Digest
, returning a
digital signature on success, or an error if something went wrong.source§fn sign_digest(&self, digest: D) -> S
fn sign_digest(&self, digest: D) -> S
source§impl<C, D> DigestVerifier<D, Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C, D> DigestVerifier<D, Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
source§impl<C> From<Signature<C>> for SignatureBytes<C>
impl<C> From<Signature<C>> for SignatureBytes<C>
source§fn from(signature: Signature<C>) -> SignatureBytes<C>
fn from(signature: Signature<C>) -> SignatureBytes<C>
source§impl<C> From<Signature<C>> for Signature<C>where
C: PrimeCurve,
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
impl<C> From<Signature<C>> for Signature<C>where
C: PrimeCurve,
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
source§impl<C> From<SignatureWithOid<C>> for Signature<C>where
C: PrimeCurve,
impl<C> From<SignatureWithOid<C>> for Signature<C>where
C: PrimeCurve,
source§fn from(sig: SignatureWithOid<C>) -> Signature<C>
fn from(sig: SignatureWithOid<C>) -> Signature<C>
source§impl<C: PartialEq + PrimeCurve> PartialEq for Signature<C>
impl<C: PartialEq + PrimeCurve> PartialEq for Signature<C>
source§impl<C> PrehashSignature for Signature<C>
impl<C> PrehashSignature for Signature<C>
§type Digest = <C as DigestPrimitive>::Digest
type Digest = <C as DigestPrimitive>::Digest
Digest
algorithm to use when computing this signature type.source§impl<C> PrehashSigner<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> PrehashSigner<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
Sign message prehash using a deterministic ephemeral scalar (k
)
computed using the algorithm described in RFC6979 § 3.2.
source§impl<C> PrehashVerifier<Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> PrehashVerifier<Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
source§impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
D: Digest + FixedOutput<OutputSize = FieldBytesSize<C>>,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
source§fn try_sign_digest_with_rng(
&self,
rng: &mut impl CryptoRngCore,
msg_digest: D,
) -> Result<Signature<C>>
fn try_sign_digest_with_rng( &self, rng: &mut impl CryptoRngCore, msg_digest: D, ) -> Result<Signature<C>>
Digest
, returning a
digital signature on success, or an error if something went wrong.source§fn sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D) -> S
fn sign_digest_with_rng(&self, rng: &mut impl CryptoRngCore, digest: D) -> S
Digest
, returning a signature. Read moresource§impl<C> RandomizedPrehashSigner<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> RandomizedPrehashSigner<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
source§fn sign_prehash_with_rng(
&self,
rng: &mut impl CryptoRngCore,
prehash: &[u8],
) -> Result<Signature<C>>
fn sign_prehash_with_rng( &self, rng: &mut impl CryptoRngCore, prehash: &[u8], ) -> Result<Signature<C>>
source§impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>where
Self: RandomizedDigestSigner<C::Digest, Signature<C>>,
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>where
Self: RandomizedDigestSigner<C::Digest, Signature<C>>,
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
source§fn try_sign_with_rng(
&self,
rng: &mut impl CryptoRngCore,
msg: &[u8],
) -> Result<Signature<C>>
fn try_sign_with_rng( &self, rng: &mut impl CryptoRngCore, msg: &[u8], ) -> Result<Signature<C>>
source§fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S
fn sign_with_rng(&self, rng: &mut impl CryptoRngCore, msg: &[u8]) -> S
source§impl<C> SignatureEncoding for Signature<C>
impl<C> SignatureEncoding for Signature<C>
§type Repr = GenericArray<u8, <<C as Curve>::FieldBytesSize as Add>::Output>
type Repr = GenericArray<u8, <<C as Curve>::FieldBytesSize as Add>::Output>
source§fn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
source§impl<C> Signer<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> Signer<Signature<C>> for SigningKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
Scalar<C>: Invert<Output = CtOption<Scalar<C>>> + SignPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
Sign message using a deterministic ephemeral scalar (k
)
computed using the algorithm described in RFC6979 § 3.2.
source§impl<C> TryFrom<Signature<C>> for Signature<C>where
C: PrimeCurve,
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
impl<C> TryFrom<Signature<C>> for Signature<C>where
C: PrimeCurve,
MaxSize<C>: ArrayLength<u8>,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
source§impl<C> Verifier<Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> Verifier<Signature<C>> for VerifyingKey<C>where
C: PrimeCurve + CurveArithmetic + DigestPrimitive,
AffinePoint<C>: VerifyPrimitive<C>,
SignatureSize<C>: ArrayLength<u8>,
impl<C> Copy for Signature<C>where
C: PrimeCurve,
SignatureSize<C>: ArrayLength<u8>,
<SignatureSize<C> as ArrayLength<u8>>::ArrayType: Copy,
impl<C: Eq + PrimeCurve> Eq for Signature<C>
impl<C: PrimeCurve> StructuralPartialEq for Signature<C>
Auto Trait Implementations§
impl<C> Freeze for Signature<C>
impl<C> RefUnwindSafe for Signature<C>
impl<C> Send for Signature<C>
impl<C> Sync for Signature<C>
impl<C> Unpin for Signature<C>
impl<C> UnwindSafe for Signature<C>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)