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 therandscomponents are both in the valid range1..nwhen serialized as concatenated big endian integers.Err(err)if therand/orscomponent 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 therandscomponents are both in the valid range1..nwhen serialized as concatenated big endian integers.Err(err)if therand/orscomponent 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>
Available on crate features digest and hazmat only.ECDSA ObjectIdentifier which identifies the digest used by default
with the Signer and Verifier traits.
impl<C> AssociatedOid for Signature<C>
digest and hazmat only.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<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.
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,
Available on crate feature digest only.
impl<C> From<SignatureWithOid<C>> for Signature<C>where
C: PrimeCurve,
digest only.Source§fn from(sig: SignatureWithOid<C>) -> Signature<C>
fn from(sig: SignatureWithOid<C>) -> Signature<C>
Source§impl<C> PrehashSignature for Signature<C>
Available on crate feature digest only.
impl<C> PrehashSignature for Signature<C>
digest only.Source§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>,
Sign message prehash using a deterministic ephemeral scalar (k)
computed using the algorithm described in RFC6979 § 3.2.
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>
Source§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>,
Sign message using a deterministic ephemeral scalar (k)
computed using the algorithm described in RFC6979 § 3.2.
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: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynAssociatedOid for Twhere
T: AssociatedOid,
impl<T> DynAssociatedOid for Twhere
T: AssociatedOid,
Source§fn oid(&self) -> ObjectIdentifier
fn oid(&self) -> ObjectIdentifier
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.