-
Notifications
You must be signed in to change notification settings - Fork 208
[bls-signatures] Add support for custom payloads for bls PoP #452
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
Merged
Merged
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ use { | |
| /// Domain separation tag used when hashing public keys to G2 in the proof of | ||
| /// possession signing and verification functions. See the | ||
| /// [standard](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05#section-4.2.3). | ||
| pub const POP_DST: &[u8] = b"BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_POP_"; | ||
| pub const POP_DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also realized that this domain separation tag was written for G1 while we hash the message on G2 for PoP, so I fixed this. |
||
|
|
||
| /// Size of a BLS proof of possession in a compressed point representation | ||
| pub const BLS_PROOF_OF_POSSESSION_COMPRESSED_SIZE: usize = 96; | ||
|
|
@@ -50,9 +50,13 @@ pub trait AsProofOfPossession { | |
| #[cfg(not(target_os = "solana"))] | ||
| pub trait VerifiableProofOfPossession: AsProofOfPossessionProjective { | ||
| /// Verifies the proof of possession against any convertible public key type. | ||
| fn verify<P: VerifiablePubkey>(&self, pubkey: &P) -> Result<bool, BlsError> { | ||
| fn verify<P: VerifiablePubkey>( | ||
| &self, | ||
| pubkey: &P, | ||
| payload: Option<&[u8]>, | ||
| ) -> Result<bool, BlsError> { | ||
| let proof_projective = self.try_as_projective()?; | ||
| pubkey.verify_proof_of_possession(&proof_projective) | ||
| pubkey.verify_proof_of_possession(&proof_projective, payload) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -172,7 +176,7 @@ mod tests { | |
| #[test] | ||
| fn test_proof_of_possession() { | ||
| let keypair = Keypair::new(); | ||
| let proof_projective = keypair.proof_of_possession(); | ||
| let proof_projective = keypair.proof_of_possession(None); | ||
|
|
||
| let pubkey_projective: PubkeyProjective = (&keypair.public).try_into().unwrap(); | ||
| let pubkey_affine: Pubkey = keypair.public; | ||
|
|
@@ -181,17 +185,17 @@ mod tests { | |
| let proof_affine: ProofOfPossession = proof_projective.into(); | ||
| let proof_compressed: ProofOfPossessionCompressed = proof_affine.try_into().unwrap(); | ||
|
|
||
| assert!(proof_projective.verify(&pubkey_projective).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_projective).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_projective).unwrap()); | ||
| assert!(proof_projective.verify(&pubkey_projective, None).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_projective, None).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_projective, None).unwrap()); | ||
|
|
||
| assert!(proof_projective.verify(&pubkey_affine).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_affine).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_affine).unwrap()); | ||
| assert!(proof_projective.verify(&pubkey_affine, None).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_affine, None).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_affine, None).unwrap()); | ||
|
|
||
| assert!(proof_projective.verify(&pubkey_compressed).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_compressed).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_compressed).unwrap()); | ||
| assert!(proof_projective.verify(&pubkey_compressed, None).unwrap()); | ||
| assert!(proof_affine.verify(&pubkey_compressed, None).unwrap()); | ||
| assert!(proof_compressed.verify(&pubkey_compressed, None).unwrap()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -240,4 +244,41 @@ mod tests { | |
| let deserialized: ProofOfPossessionCompressed = bincode::deserialize(&serialized).unwrap(); | ||
| assert_eq!(original, deserialized); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_proof_of_possession_with_custom_payload() { | ||
| let keypair = Keypair::new(); | ||
| let custom_payload = b"SIMD-0387-context-data"; | ||
|
|
||
| let proof_custom = keypair.proof_of_possession(Some(custom_payload)); | ||
| assert!(keypair | ||
| .public | ||
| .verify_proof_of_possession(&proof_custom, Some(custom_payload)) | ||
| .unwrap()); | ||
|
|
||
| assert!(!keypair | ||
| .public | ||
| .verify_proof_of_possession(&proof_custom, None) // try verify with `None` | ||
| .unwrap()); | ||
|
|
||
| let wrong_payload = b"wrong-context"; | ||
| assert!(!keypair | ||
| .public | ||
| // try verify with wrong payload | ||
| .verify_proof_of_possession(&proof_custom, Some(wrong_payload)) | ||
| .unwrap()); | ||
|
|
||
| // verify standard PoP behavior | ||
| let proof_standard = keypair.proof_of_possession(None); | ||
| // standard passes with None | ||
| assert!(keypair | ||
| .public | ||
| .verify_proof_of_possession(&proof_standard, None) | ||
| .unwrap()); | ||
| // standard fails with custom payload | ||
| assert!(!keypair | ||
| .public | ||
| .verify_proof_of_possession(&proof_standard, Some(custom_payload)) | ||
| .unwrap()); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
nits: this means
bytes = Some(&[])andbytes=Nonehave different outputs. Is this behavior acceptable? Or shouldbytes=Nonealso hash the public key?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.
Yeah, I was thinking that this is acceptable.
I originally made the function take in
prefix: Option<&[u8]>instead. It would then concatenateprefix || pubkey_bytesand then use this as the payload. This is probably the safer API because the public key will always be hashed. But if someone wants to add a suffix instead of prefixpubkey_bytes || suffix, for example, they cannot use this API. This is why I made the function to take inpaylod: Option<&[u8]>instead.I think hashing on
bytes = Some(&[])is not any safer than hashing any other string that does not have the public key bytes in it anyway, so there doesn't seem to be a justification to specialize for this case. Of course, if you think specializing addressing the caseSome(&[])to hash the public key bytes instead, then let me know 😄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.
sounds good to me