-
Notifications
You must be signed in to change notification settings - Fork 41
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
Fix BLST bindings: Error handling for infinite values of sigs and vks #2322
Open
curiecrypt
wants to merge
9
commits into
main
Choose a base branch
from
curiecrypt/fix-bls-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8858065
error handling done for inf values of sigs and vks
curiecrypt f36b695
tests added
curiecrypt 90d7e2a
update crates version
curiecrypt 3f622c7
Update mithril-stm/src/error.rs
curiecrypt 6f4f4a8
key as input of blst_err_to_mithril
curiecrypt 1fc1077
check expected error
curiecrypt d39e877
correct box usage for errors
curiecrypt 09087d3
error handling for multi sig tests
curiecrypt cf63d31
revert serialization tests
curiecrypt 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -88,7 +88,7 @@ impl SigningKey { | |
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> { | ||
match BlstSk::from_bytes(&bytes[..32]) { | ||
Ok(sk) => Ok(Self(sk)), | ||
Err(e) => Err(blst_err_to_mithril(e, None) | ||
Err(e) => Err(blst_err_to_mithril(e, None, None) | ||
.expect_err("If deserialization is not successful, blst returns and error different to SUCCESS.")) | ||
} | ||
} | ||
|
@@ -108,7 +108,7 @@ impl VerificationKey { | |
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> { | ||
match BlstVk::key_validate(&bytes[..96]) { | ||
Ok(vk) => Ok(Self(vk)), | ||
Err(e) => Err(blst_err_to_mithril(e, None) | ||
Err(e) => Err(blst_err_to_mithril(e, None, None) | ||
.expect_err("If deserialization is not successful, blst returns and error different to SUCCESS.")) | ||
} | ||
} | ||
|
@@ -196,15 +196,19 @@ impl VerificationKeyPoP { | |
// If we are really looking for performance improvements, we can combine the | ||
// two final exponentiations (for verifying k1 and k2) into a single one. | ||
pub fn check(&self) -> Result<(), MultiSignatureError> { | ||
let result = verify_pairing(&self.vk, &self.pop); | ||
|
||
if !(self.pop.k1.verify(false, POP, &[], &[], &self.vk.0, false) | ||
== BLST_ERROR::BLST_SUCCESS | ||
&& result) | ||
{ | ||
return Err(MultiSignatureError::KeyInvalid(Box::new(*self))); | ||
match self.vk.0.validate() { | ||
Ok(_) => { | ||
let result = verify_pairing(&self.vk, &self.pop); | ||
if !(self.pop.k1.verify(false, POP, &[], &[], &self.vk.0, false) | ||
== BLST_ERROR::BLST_SUCCESS | ||
&& result) | ||
{ | ||
return Err(MultiSignatureError::KeyInvalid(Box::new(*self))); | ||
} | ||
Ok(()) | ||
} | ||
Err(e) => blst_err_to_mithril(e, None, Some(self.vk)), | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Convert to a 144 byte string. | ||
|
@@ -261,7 +265,7 @@ impl ProofOfPossession { | |
let k1 = match BlstSig::from_bytes(&bytes[..48]) { | ||
Ok(key) => key, | ||
Err(e) => { | ||
return Err(blst_err_to_mithril(e, None) | ||
return Err(blst_err_to_mithril(e, None, None) | ||
.expect_err("If it passed, blst returns and error different to SUCCESS.")) | ||
} | ||
}; | ||
|
@@ -287,10 +291,14 @@ impl From<&SigningKey> for ProofOfPossession { | |
impl Signature { | ||
/// Verify a signature against a verification key. | ||
pub fn verify(&self, msg: &[u8], mvk: &VerificationKey) -> Result<(), MultiSignatureError> { | ||
blst_err_to_mithril( | ||
self.0.verify(false, msg, &[], &[], &mvk.0, false), | ||
Some(*self), | ||
) | ||
match self.0.validate(true) { | ||
Ok(_) => blst_err_to_mithril( | ||
self.0.verify(false, msg, &[], &[], &mvk.0, false), | ||
Some(*self), | ||
None, | ||
), | ||
Err(e) => blst_err_to_mithril(e, Some(*self), None), | ||
} | ||
} | ||
|
||
/// Dense mapping function indexed by the index to be evaluated. | ||
|
@@ -323,7 +331,7 @@ impl Signature { | |
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> { | ||
match BlstSig::sig_validate(&bytes[..48], true) { | ||
Ok(sig) => Ok(Self(sig)), | ||
Err(e) => Err(blst_err_to_mithril(e, None) | ||
Err(e) => Err(blst_err_to_mithril(e, None, None) | ||
.expect_err("If deserialization is not successful, blst returns and error different to SUCCESS.")) | ||
} | ||
} | ||
|
@@ -376,7 +384,6 @@ impl Signature { | |
} | ||
|
||
let transmuted_vks: Vec<blst_p2> = vks.iter().map(vk_from_p2_affine).collect(); | ||
|
||
let transmuted_sigs: Vec<blst_p1> = signatures.iter().map(sig_to_p1).collect(); | ||
|
||
let grouped_vks = p2_affines::from(transmuted_vks.as_slice()); | ||
|
@@ -400,6 +407,7 @@ impl Signature { | |
blst_err_to_mithril( | ||
aggr_sig.0.verify(false, msg, &[], &[], &aggr_vk.0, false), | ||
Some(aggr_sig), | ||
None, | ||
) | ||
} | ||
|
||
|
@@ -415,7 +423,7 @@ impl Signature { | |
false, | ||
) { | ||
Ok(sig) => BlstSig::from_aggregate(&sig), | ||
Err(e) => return blst_err_to_mithril(e, None), | ||
Err(e) => return blst_err_to_mithril(e, None, None), | ||
}; | ||
|
||
let p2_vks: Vec<&BlstVk> = vks.iter().map(|vk| &vk.0).collect(); | ||
|
@@ -427,6 +435,7 @@ impl Signature { | |
blst_err_to_mithril( | ||
batched_sig.aggregate_verify(false, &slice_msgs, &[], &p2_vks, false), | ||
None, | ||
None, | ||
) | ||
.map_err(|_| MultiSignatureError::BatchInvalid) | ||
} | ||
|
@@ -628,6 +637,8 @@ mod unsafe_helpers { | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::error::RegisterError; | ||
use crate::key_reg::KeyReg; | ||
use proptest::prelude::*; | ||
use rand_chacha::ChaCha20Rng; | ||
use rand_core::{OsRng, SeedableRng}; | ||
|
@@ -643,19 +654,69 @@ mod tests { | |
let sk = SigningKey::gen(&mut ChaCha20Rng::from_seed(seed)); | ||
let vk = VerificationKey::from(&sk); | ||
let sig = sk.sign(&msg); | ||
assert!(sig.verify(&msg, &vk).is_ok()); | ||
|
||
let result = sig.verify(&msg, &vk); | ||
assert!(result.is_ok(), "verify {result:?}"); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_sig(msg in prop::collection::vec(any::<u8>(), 1..128), | ||
seed in any::<[u8;32]>(), | ||
) { | ||
fn test_invalid_sig(msg in prop::collection::vec(any::<u8>(), 1..128), seed in any::<[u8;32]>()) { | ||
let mut rng = ChaCha20Rng::from_seed(seed); | ||
let sk1 = SigningKey::gen(&mut rng); | ||
let vk1 = VerificationKey::from(&sk1); | ||
let sk2 = SigningKey::gen(&mut rng); | ||
let fake_sig = sk2.sign(&msg); | ||
assert!(fake_sig.verify(&msg, &vk1).is_err()); | ||
|
||
let result = fake_sig.verify(&msg, &vk1); | ||
assert_eq!(result, Err(MultiSignatureError::SignatureInvalid(fake_sig))); | ||
} | ||
|
||
#[test] | ||
fn test_infinity_sig(msg in prop::collection::vec(any::<u8>(), 1..128), seed in any::<[u8;32]>()) { | ||
let mut rng = ChaCha20Rng::from_seed(seed); | ||
let sk = SigningKey::gen(&mut rng); | ||
let vk = VerificationKey::from(&sk); | ||
|
||
let p1 = blst_p1::default(); | ||
let sig_infinity = Signature(p1_affine_to_sig(&p1)); | ||
|
||
let result = sig_infinity.verify(&msg, &vk); | ||
assert_eq!(result, Err(MultiSignatureError::SignatureInfinity(sig_infinity))); | ||
} | ||
|
||
#[test] | ||
fn test_infinity_vk(seed in any::<[u8;32]>()) { | ||
let mut rng = ChaCha20Rng::from_seed(seed); | ||
let sk = SigningKey::gen(&mut rng); | ||
let pop = ProofOfPossession::from(&sk); | ||
|
||
let p2 = blst_p2::default(); | ||
let vk_infinity = VerificationKey(p2_affine_to_vk(&p2)); | ||
let vkpop_infinity = VerificationKeyPoP { vk: vk_infinity, pop }; | ||
|
||
let result = vkpop_infinity.check(); | ||
assert_eq!(result, Err(MultiSignatureError::VerificationKeyInfinity(Box::new(vkpop_infinity.vk)))); | ||
} | ||
|
||
#[test] | ||
fn test_keyreg_with_infinity_vk(num_sigs in 2..16usize, seed in any::<[u8;32]>()) { | ||
let mut rng = ChaCha20Rng::from_seed(seed); | ||
let mut kr = KeyReg::init(); | ||
|
||
let sk = SigningKey::gen(&mut rng); | ||
let pop = ProofOfPossession::from(&sk); | ||
let p2 = blst_p2::default(); | ||
let vk_infinity = VerificationKey(p2_affine_to_vk(&p2)); | ||
let vkpop_infinity = VerificationKeyPoP { vk: vk_infinity, pop }; | ||
|
||
for _ in 0..num_sigs { | ||
let sk = SigningKey::gen(&mut rng); | ||
let vkpop = VerificationKeyPoP::from(&sk); | ||
let _ = kr.register(1, vkpop); | ||
} | ||
|
||
let result = kr.register(1, vkpop_infinity); | ||
assert_eq!(result, Err(RegisterError::VerificationKeyInfinity(Box::new(vkpop_infinity.vk)))); | ||
} | ||
|
||
#[test] | ||
|
@@ -675,7 +736,8 @@ mod tests { | |
mvks.push(vk); | ||
} | ||
|
||
assert!(Signature::verify_aggregate(&msg, &mvks, &sigs).is_ok()); | ||
let result = Signature::verify_aggregate(&msg, &mvks, &sigs); | ||
assert!(result.is_ok(), "Aggregate verification failed {result:?}"); | ||
} | ||
|
||
#[test] | ||
|
@@ -749,13 +811,23 @@ mod tests { | |
sigs.push(sig); | ||
mvks.push(vk); | ||
} | ||
assert!(Signature::verify_aggregate(&msg, &mvks, &sigs).is_ok()); | ||
let (agg_vk, agg_sig) = Signature::aggregate(&mvks, &sigs).unwrap(); | ||
batch_msgs.push(msg.to_vec()); | ||
batch_vk.push(agg_vk); | ||
batch_sig.push(agg_sig); | ||
let verify_aggregate = Signature::verify_aggregate(&msg, &mvks, &sigs); | ||
assert!(verify_aggregate.is_ok(), "Aggregate verification {verify_aggregate:?}"); | ||
|
||
match Signature::aggregate(&mvks, &sigs) { | ||
Ok((agg_vk, agg_sig)) => { | ||
batch_msgs.push(msg.to_vec()); | ||
batch_vk.push(agg_vk); | ||
batch_sig.push(agg_sig); | ||
} | ||
Err(MultiSignatureError::AggregateSignatureInvalid) => { | ||
println!("Aggregation failed."); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
assert!(Signature::batch_verify_aggregates(&batch_msgs, &batch_vk, &batch_sig).is_ok()); | ||
let batch_verify_aggregates = Signature::batch_verify_aggregates(&batch_msgs, &batch_vk, &batch_sig); | ||
assert!(batch_verify_aggregates.is_ok(), "{batch_verify_aggregates:?}"); | ||
Comment on lines
+814
to
+830
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. This should be also rolled back as we have discussed. |
||
|
||
// If we have an invalid signature, the batch verification will fail | ||
let mut msg = [0u8; 32]; | ||
|
@@ -764,7 +836,8 @@ mod tests { | |
let fake_sig = sk.sign(&msg); | ||
batch_sig[0] = fake_sig; | ||
|
||
assert!(Signature::batch_verify_aggregates(&batch_msgs, &batch_vk, &batch_sig).is_err()); | ||
let batch_result = Signature::batch_verify_aggregates(&batch_msgs, &batch_vk, &batch_sig); | ||
assert_eq!(batch_result, Err(MultiSignatureError::BatchInvalid)); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
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.
Maybe this would be more readable: