-
Notifications
You must be signed in to change notification settings - Fork 231
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
Add StreamVerifier
#196
Closed
Closed
Add StreamVerifier
#196
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use curve25519_dalek::{edwards::EdwardsPoint, scalar::Scalar}; | ||
use sha2::{Digest, Sha512}; | ||
|
||
use crate::{signature::InternalSignature, InternalError, SignatureError, VerifyingKey}; | ||
|
||
/// An IUF verifier for ed25519. | ||
/// | ||
/// Created with [`VerifyingKey::verify_stream()`] or [`SigningKey::verify_stream()`]. | ||
/// | ||
/// [`SigningKey::verify_stream()`]: super::SigningKey::verify_stream() | ||
#[derive(Debug)] | ||
pub struct StreamVerifier { | ||
/// Public key to verify with. | ||
pub(crate) public_key: VerifyingKey, | ||
|
||
/// Candidate signature to verify against. | ||
pub(crate) signature: InternalSignature, | ||
|
||
/// Hash state. | ||
pub(crate) hasher: Sha512, | ||
} | ||
|
||
impl StreamVerifier { | ||
/// Constructs new stream verifier. | ||
/// | ||
/// Seeds hash state with public key and signature components. | ||
pub(crate) fn new(public_key: VerifyingKey, signature: InternalSignature) -> Self { | ||
let mut hasher = Sha512::new(); | ||
hasher.update(signature.R.as_bytes()); | ||
hasher.update(public_key.as_bytes()); | ||
|
||
Self { | ||
public_key, | ||
hasher, | ||
signature, | ||
} | ||
} | ||
|
||
/// Digest message chunk. | ||
pub fn update(&mut self, chunk: impl AsRef<[u8]>) { | ||
self.hasher.update(&chunk); | ||
} | ||
|
||
/// Finalize verifier and check against candidate signature. | ||
#[allow(non_snake_case)] | ||
pub fn finalize_and_verify(self) -> Result<(), SignatureError> { | ||
let minus_A: EdwardsPoint = -self.public_key.point; | ||
let k = Scalar::from_hash(self.hasher); | ||
let R = | ||
EdwardsPoint::vartime_double_scalar_mul_basepoint(&k, &(minus_A), &self.signature.s); | ||
|
||
if R.compress() == self.signature.R { | ||
Ok(()) | ||
} else { | ||
Err(InternalError::Verify.into()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
VerifyingKey::recover_R
could be further decomposed intoVerifyingKey::recover_R_from_k
or thereabouts which captures this functionality.cc @rozbb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made that change along with some tangential work in #304