Skip to content

Commit

Permalink
Merge pull request #38 from ChihChengLiang/bump-upstrea
Browse files Browse the repository at this point in the history
Bump upstream and cleanup error handling
  • Loading branch information
ChihChengLiang authored May 13, 2022
2 parents 78b4096 + b17ae0b commit d7d4cf3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 145 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.7
architecture: x64
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.7
architecture: ${{ matrix.target }}
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -146,7 +146,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.7
architecture: x64
- name: Build Wheels
uses: messense/maturin-action@v1
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "milagro-bls-binding"
version = "1.8.1"
version = "1.9.0"
edition = "2018"
authors = ["[email protected]"]
readme = "README.md"
Expand All @@ -15,14 +15,14 @@ classifier = [
]
maintainer = "Chih-Cheng Liang"
maintainer-email = "[email protected]"
requires-python = ">=3.6"
requires-python = ">=3.7"
project-url = { Source = "https://github.com/ChihChengLiang/milagro_bls_binding/" }

[lib]
name = "milagro_bls_binding"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.15.1", features = ["extension-module", "abi3-py36"] }
milagro_bls = {git = "https://github.com/sigp/milagro_bls", tag= "v1.4.2" }
rand = { version = "0.7.2", default-features = false }
pyo3 = { version = "0.16.4", features = ["extension-module", "abi3-py37"] }
milagro_bls = {git = "https://github.com/sigp/milagro_bls", tag= "v1.5.0" }
rand = { version = "0.8.5", default-features = false }
223 changes: 86 additions & 137 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,175 +5,124 @@ use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyList};
use pyo3::wrap_pyfunction;

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

fn to_py_err(e: AmclError) -> PyErr {
PyValueError::new_err(format!("{:?}", e))
}

#[pyfunction]
fn SkToPk(_py: Python, SK: &PyBytes) -> PyResult<PyObject> {
let sk_bytes: Vec<u8> = SK.extract()?;
let sk = SecretKey::from_bytes(&sk_bytes)
.map_err(|e| PyValueError::new_err(format!("Bad key: {:?}", e)))?;
let pk = PublicKey::from_secret_key(&sk).as_bytes();
Ok(PyBytes::new(_py, &pk).to_object(_py))
SecretKey::from_bytes(SK.extract()?)
.map_err(to_py_err)
.map(|sk| PublicKey::from_secret_key(&sk).as_bytes())
.map(|pk| PyBytes::new(_py, &pk).to_object(_py))
}

#[pyfunction]
fn Sign(_py: Python, SK: &PyBytes, message: &PyBytes) -> PyResult<PyObject> {
let sk_bytes: Vec<u8> = SK.extract()?;
let msg_bytes: Vec<u8> = message.extract()?;
let sk = SecretKey::from_bytes(&sk_bytes)
.map_err(|e| PyValueError::new_err(format!("Bad key: {:?}", e)))?;
let sig = Signature::new(&msg_bytes, &sk).as_bytes();
Ok(PyBytes::new(_py, &sig).to_object(_py))
SecretKey::from_bytes(SK.extract()?)
.map_err(to_py_err)
.and_then(|sk| Ok(Signature::new(message.extract()?, &sk).as_bytes()))
.map(|sig| PyBytes::new(_py, &sig).to_object(_py))
}

#[pyfunction]
fn Verify(_py: Python, PK: &PyBytes, message: &PyBytes, signature: &PyBytes) -> bool {
let pubkey_bytes: Vec<u8> = match PK.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let msg_bytes: Vec<u8> = match message.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let sig_bytes: Vec<u8> = match signature.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let pk = match PublicKey::from_bytes(&pubkey_bytes) {
Ok(_pk) => _pk,
Err(_) => return false,
};
let sig = match Signature::from_bytes(&sig_bytes) {
Ok(_sig) => _sig,
Err(_) => return false,
};
sig.verify(&msg_bytes, &pk)
PK.extract()
.and_then(|pk| PublicKey::from_bytes(pk).map_err(to_py_err))
.and_then(|pk| {
let sig = Signature::from_bytes(signature.extract()?).map_err(to_py_err)?;
Ok(sig.verify(message.extract()?, &pk))
})
.unwrap_or(false)
}

#[pyfunction]
fn Aggregate(_py: Python, signatures: &PyList) -> PyResult<PyObject> {
let mut agg_sig = AggregateSignature::new();
for sig in signatures {
let sig_bytes: Vec<u8> = sig.extract()?;
let sig = Signature::from_bytes(&sig_bytes)
.map_err(|e| PyValueError::new_err(format!("Bad Signature: {:?}", e)))?;
agg_sig.add(&sig);
}
let agg_sig = signatures
.iter()
.map(|sig| Signature::from_bytes(sig.extract()?).map_err(to_py_err))
.collect::<Result<Vec<Signature>, _>>()?
.iter()
.fold(AggregateSignature::new(), |mut agg_sig, sig| {
agg_sig.add(sig);
agg_sig
});
Ok(PyBytes::new(_py, agg_sig.as_bytes().as_ref()).to_object(_py))
}

#[pyfunction]
fn _AggregatePKs(_py: Python, PKs: &PyList) -> PyResult<PyObject> {
let mut pks: Vec<PublicKey> = vec![];
for pubkey in PKs {
let pubkey_bytes = pubkey.extract()?;
let pk = PublicKey::from_bytes(pubkey_bytes)
.map_err(|e| PyValueError::new_err(format!("Bad Public Key: {:?}", e)))?;
pks.push(pk);
}
let aggregation = AggregatePublicKey::into_aggregate(&pks)
.map_err(|e| PyValueError::new_err(format!("Aggregation error: {:?}", e)))?;
let agg_pub = PublicKey {
point: aggregation.point,
}
.as_bytes();
Ok(PyBytes::new(_py, &agg_pub).to_object(_py))
PKs.iter()
.map(|pubkey| PublicKey::from_bytes(pubkey.extract()?).map_err(to_py_err))
.collect::<Result<Vec<PublicKey>, PyErr>>()
.and_then(|pks| AggregatePublicKey::into_aggregate(&pks).map_err(to_py_err))
.map(|agg| PublicKey { point: agg.point }.as_bytes())
.map(|agg_pub| PyBytes::new(_py, &agg_pub).to_object(_py))
}

#[pyfunction]
fn FastAggregateVerify(_py: Python, PKs: &PyList, message: &PyBytes, signature: &PyBytes) -> bool {
let msg_bytes: Vec<u8> = match message.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let sig_bytes: Vec<u8> = match signature.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let agg_sig = match AggregateSignature::from_bytes(&sig_bytes) {
Ok(_agg_sig) => _agg_sig,
Err(_) => return false,
};
let mut pks: Vec<PublicKey> = vec![];

for pk in PKs {
let pubkey_bytes = match pk.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
match PublicKey::from_bytes(pubkey_bytes) {
Ok(_pk) => pks.push(_pk),
Err(_) => return false,
};
}

let pks_ref: Vec<&PublicKey> = pks.iter().collect();

agg_sig.fast_aggregate_verify(&msg_bytes, &pks_ref)
PKs.iter()
.map(|pubkey| PublicKey::from_bytes(pubkey.extract()?).map_err(to_py_err))
.collect::<Result<Vec<PublicKey>, PyErr>>()
.and_then(|pks| {
Ok(AggregateSignature::from_bytes(signature.extract()?)
.map_err(to_py_err)?
.fast_aggregate_verify(
message.extract()?,
&pks.iter().collect::<Vec<&PublicKey>>(),
))
})
.unwrap_or(false)
}

#[pyfunction]
fn AggregateVerify(_py: Python, PKs: &PyList, messages: &PyList, signature: &PyBytes) -> bool {
let sig_bytes: Vec<u8> = match signature.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
let agg_sig = match AggregateSignature::from_bytes(&sig_bytes) {
Ok(_agg_sig) => _agg_sig,
Err(_) => return false,
};

let mut pks: Vec<PublicKey> = vec![];
for pk in PKs {
let pubkey_bytes = match pk.extract() {
Ok(bytes) => bytes,
Err(_) => return false,
};
match PublicKey::from_bytes(pubkey_bytes) {
Ok(_pk) => pks.push(_pk),
Err(_) => return false,
};
}
let pks_ref: Vec<&PublicKey> = pks.iter().collect();

let mut msgs: Vec<Vec<u8>> = vec![];
for msg in messages {
match msg.extract() {
Ok(msg_bytes) => msgs.push(msg_bytes),
Err(_) => return false,
};
}

let msgs_refs: Vec<&[u8]> = msgs.iter().map(|x| x.as_slice()).collect();

agg_sig.aggregate_verify(&msgs_refs, &pks_ref)
PKs.iter()
.map(|pubkey| PublicKey::from_bytes(pubkey.extract()?).map_err(to_py_err))
.collect::<Result<Vec<PublicKey>, PyErr>>()
.and_then(|pks| {
let msgs = messages
.iter()
.map(|msg| msg.extract())
.collect::<Result<Vec<Vec<u8>>, PyErr>>()?;
Ok(AggregateSignature::from_bytes(signature.extract()?)
.map_err(to_py_err)?
.aggregate_verify(
&msgs
.iter()
.map(|msg| msg.as_slice())
.collect::<Vec<&[u8]>>(),
&pks.iter().collect::<Vec<&PublicKey>>(),
))
})
.unwrap_or(false)
}

#[pyfunction]
fn VerifyMultipleAggregateSignatures(_py: Python, SignatureSets: &PyList) -> bool {
let mut signature_sets: Vec<(AggregateSignature, AggregatePublicKey, Vec<u8>)> = Vec::new();
for set in SignatureSets {
let tuple: Vec<Vec<u8>> = match set.extract() {
Ok(_tuple) => _tuple,
Err(_) => return false,
};
let aggsig = match AggregateSignature::from_bytes(&tuple[0]) {
Ok(_aggsig) => _aggsig,
Err(_) => return false,
};
let aggkey = match PublicKey::from_bytes(&tuple[1]) {
Ok(_pubkey) => AggregatePublicKey::from_public_key(&_pubkey),
Err(_) => return false,
};
signature_sets.push((aggsig, aggkey, tuple[2].clone()));
}

let mut rng = &mut rand::thread_rng();
AggregateSignature::verify_multiple_aggregate_signatures(
&mut rng,
signature_sets.iter().map(|x| (&x.0, &x.1, x.2.as_slice())),
)
SignatureSets
.iter()
.map(|set| {
let tuple: Vec<Vec<u8>> = set.extract()?;
let aggsig = AggregateSignature::from_bytes(&tuple[0]).map_err(to_py_err)?;
let aggkey = AggregatePublicKey::from_public_key(
&PublicKey::from_bytes(&tuple[1]).map_err(to_py_err)?,
);
Ok((aggsig, aggkey, tuple[2].clone()))
})
.collect::<Result<Vec<(AggregateSignature, AggregatePublicKey, Vec<u8>)>, PyErr>>()
.map(|signature_sets| {
AggregateSignature::verify_multiple_aggregate_signatures(
&mut rand::thread_rng(),
signature_sets.iter().map(|x| (&x.0, &x.1, x.2.as_slice())),
)
})
.unwrap_or(false)
}

/// This module is a python module implemented in Rust.
Expand Down

0 comments on commit d7d4cf3

Please sign in to comment.