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

Fix more bls v4 issue #20

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "milagro-bls-binding"
version = "1.4.0"
version = "1.5.0"
edition = "2018"
authors = ["[email protected]"]

Expand Down
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ use pyo3::wrap_pyfunction;

use milagro_bls::{AggregatePublicKey, AggregateSignature, PublicKey, SecretKey, Signature};

fn is_zilch(w: &[u8]) -> bool {
for i in 0..32 {
if w[i] != 0 {
return false;
}
}
true
}


#[pyfunction]
fn SkToPk(_py: Python, SK: &PyBytes) -> PyResult<PyObject> {
let sk_bytes: Vec<u8> = SK.extract()?;
Expand All @@ -19,6 +29,9 @@ fn SkToPk(_py: Python, SK: &PyBytes) -> PyResult<PyObject> {
#[pyfunction]
fn Sign(_py: Python, SK: &PyBytes, message: &PyBytes) -> PyResult<PyObject> {
let sk_bytes: Vec<u8> = SK.extract()?;
if is_zilch(&sk_bytes) {
return Err(PyErr::new::<ValueError, _>("Zero key"));
}
let msg_bytes: Vec<u8> = message.extract()?;
let sk = SecretKey::from_bytes(&sk_bytes)
.map_err(|e| PyErr::new::<ValueError, _>(format!("Bad key: {:?}", e)))?;
Expand Down Expand Up @@ -104,7 +117,13 @@ fn FastAggregateVerify(
Err(_) => return false,
};
match PublicKey::from_bytes(pubkey_bytes) {
Ok(_pk) => pks.push(_pk),
Ok(_pk) => {
if _pk.key_validate() {
pks.push(_pk);
} else {
return false;
}
},
Err(_) => return false,
};
}
Expand Down
19 changes: 8 additions & 11 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ def test_sign_verify(privkey_int):
assert bls.Verify(pub, msg, sig)


def test_sign_verify_zero():
privkey = to_bytes(0)
msg = "foooooo".encode('utf-8')
pub = bls.SkToPk(privkey)
sig = bls.Sign(privkey, msg)
assert not bls.Verify(pub, msg, sig)


@pytest.mark.parametrize(
'signature_points,result_point',
[
Expand Down Expand Up @@ -94,6 +86,11 @@ def test_fast_aggregate_verify(SKs, message):


def test_weird_cases():
bad_signature = b'\x00' * 96
assert not bls.AggregateVerify([], [], bad_signature)
assert bls.Aggregate([]) == b'\xc0' + b'\x00' * 95
Z1_PUBKEY = b'\xc0' + b'\x00' * 47
Z2_SIGNATURE = b'\xc0' + b'\x00' * 95
assert not bls.AggregateVerify([], [], Z2_SIGNATURE)
assert bls.Aggregate([]) == Z2_SIGNATURE
with pytest.raises(ValueError , match=r"Zero*"):
bls.Sign(to_bytes(0), b'abcd')
assert not bls.Verify(Z1_PUBKEY, b'abcd', Z2_SIGNATURE)
assert not bls.FastAggregateVerify([Z1_PUBKEY], b'abcd', Z2_SIGNATURE)