Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecdsa: add recover_from_prehash_noverify #831

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 29 additions & 8 deletions ecdsa/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,31 @@ where

/// Recover a [`VerifyingKey`] from the given `prehash` of a message, the
/// signature over that prehashed message, and a [`RecoveryId`].
#[allow(non_snake_case)]
pub fn recover_from_prehash(
prehash: &[u8],
signature: &Signature<C>,
recovery_id: RecoveryId,
) -> Result<Self> {
Self::recover_internal(prehash, signature, recovery_id, true)
}
/// Recover a [`VerifyingKey`] from the given `prehash` of a message, the
/// signature over that prehashed message, and a [`RecoveryId`]. Compared to
/// `recover_from_prehash`, this function skips verification with the
/// recovered key.
pub fn recover_from_prehash_noverify(
prehash: &[u8],
signature: &Signature<C>,
recovery_id: RecoveryId,
) -> Result<Self> {
Self::recover_internal(prehash, signature, recovery_id, false)
}

#[allow(non_snake_case)]
fn recover_internal(
tarcieri marked this conversation as resolved.
Show resolved Hide resolved
prehash: &[u8],
signature: &Signature<C>,
recovery_id: RecoveryId,
verify: bool,
) -> Result<Self> {
let (r, s) = signature.split_scalars();
let z = <Scalar<C> as Reduce<C::Uint>>::reduce_bytes(&bits2field::<C>(prehash)?);
Expand Down Expand Up @@ -298,13 +318,14 @@ where
let pk = ProjectivePoint::<C>::lincomb(&[(ProjectivePoint::<C>::generator(), u1), (R, u2)]);
let vk = Self::from_affine(pk.into())?;

// Ensure signature verifies with the recovered key
verify_prehashed::<C>(
&ProjectivePoint::<C>::from(*vk.as_affine()),
&bits2field::<C>(prehash)?,
signature,
)?;

if verify {
// Ensure signature verifies with the recovered key
verify_prehashed::<C>(
&ProjectivePoint::<C>::from(*vk.as_affine()),
&bits2field::<C>(prehash)?,
signature,
)?;
}
Ok(vk)
}
}
Expand Down
Loading