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

Create a cache for BLS #453

Merged
merged 58 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
02f4438
rebase bls_cache off main
matt-o-how Apr 2, 2024
0f543f0
fix tests and update readme
matt-o-how Apr 2, 2024
28b309c
fmt
matt-o-how Apr 5, 2024
e42f49c
pyi stubs
matt-o-how Apr 5, 2024
02181c2
mypy fixes
matt-o-how Apr 5, 2024
ffd3862
no bytes conversion
matt-o-how Apr 5, 2024
d333221
bytes length
matt-o-how Apr 5, 2024
d273c8e
add byteorder
matt-o-how Apr 5, 2024
7c096c7
black test
matt-o-how Apr 5, 2024
7770765
remove unnecessary bytes32/48 type
matt-o-how Apr 8, 2024
13939b9
fix typo
matt-o-how Apr 8, 2024
f156286
shorten return
matt-o-how Apr 8, 2024
9bb3b9f
implement Rigidity's review comments
matt-o-how Apr 9, 2024
290fba2
fmt new changes
matt-o-how Apr 9, 2024
5891fd7
delete requirements.txt
matt-o-how Apr 25, 2024
0b4aff6
remove readme from wheel
matt-o-how Apr 25, 2024
3dff7c0
converts msg to [u8] slice
matt-o-how Apr 25, 2024
b0ce315
cargo update
matt-o-how Apr 25, 2024
930d93b
clippy and fmt
matt-o-how Apr 25, 2024
9f3b65e
add comments and privatise get_pairings
matt-o-how Apr 26, 2024
32c2d36
Clippy, map, early ret, no Borrow
Rigidity Apr 25, 2024
077dc69
move to serialized PublicKey
matt-o-how May 7, 2024
f4e9eac
remove vecs
matt-o-how May 7, 2024
8f68e7c
pass iter into aggregate_verify_gt
matt-o-how May 7, 2024
e9bfa06
clippy + fmt
matt-o-how May 7, 2024
4d7102f
remove pybindings for BLSCache
matt-o-how May 8, 2024
d3e5b9f
Revert "remove pybindings for BLSCache"
matt-o-how May 8, 2024
877eaeb
fix pybindings
matt-o-how May 8, 2024
653c459
remove unused imports in pytest
matt-o-how May 8, 2024
1052a33
pass iters into aggregate_verify
matt-o-how May 8, 2024
1c64bc7
use IntoIterator instead of Iterator
matt-o-how May 8, 2024
ca78021
fmt + clippy
matt-o-how May 8, 2024
427ad5d
Remove redundant let
matt-o-how May 9, 2024
aa85dc5
fix incorrect type in stubs
matt-o-how May 9, 2024
ca5e347
add benchmarking test
matt-o-how May 9, 2024
d827fc3
add cargo bench funcs for cache
matt-o-how May 9, 2024
a823c4c
test more representative number pairs and clone cache
matt-o-how May 9, 2024
1c3c14b
update benchmark
arvidn May 9, 2024
532e86c
Merge pull request #510 from Chia-Network/bls-cache-benchmark
matt-o-how May 10, 2024
ee330c5
Merge branch 'main' into bls_cache_rebased
matt-o-how May 10, 2024
dcfef4f
benchmarks
matt-o-how May 10, 2024
5178423
test both old and new blscache
matt-o-how May 10, 2024
f56e629
Merge branch 'bls_cache_rebased' of https://github.com/Chia-Network/c…
matt-o-how May 10, 2024
94c8f7b
black tests
matt-o-how May 10, 2024
cf1fe69
swap to nonzerousize and remove generator
matt-o-how May 13, 2024
5991858
fmt + clippy
matt-o-how May 13, 2024
35e8658
change pystubs
matt-o-how May 13, 2024
81ed976
type stubs typo fix
matt-o-how May 13, 2024
1252af5
add self to init
matt-o-how May 13, 2024
a6b9923
attempt to fix mypy error in CI
matt-o-how May 13, 2024
405728f
black pytest
matt-o-how May 13, 2024
ee22bd1
return and test overflow error in python
matt-o-how May 14, 2024
3ee0028
clippy + black
matt-o-how May 14, 2024
b2257a2
change unwrap to expect with message
matt-o-how May 14, 2024
c03c79c
add tests for empty validation
matt-o-how May 14, 2024
ed86a1b
make tests not pub
matt-o-how May 14, 2024
44da724
assert pytest.raises
matt-o-how May 14, 2024
c617acd
black formatting
matt-o-how May 14, 2024
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
25 changes: 16 additions & 9 deletions crates/chia-bls/src/cached_bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::num::NonZeroUsize;
#[cfg(feature = "py-bindings")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "py-bindings")]
use pyo3::types::{PyInt, PyList};
use pyo3::types::PyList;
#[cfg(feature = "py-bindings")]
use pyo3::{pyclass, pymethods, PyResult};

Expand All @@ -37,8 +37,9 @@ impl Default for BLSCache {

impl BLSCache {
pub fn new(cache_size: Option<NonZeroUsize>) -> BLSCache {
let cache: LruCache<[u8; 32], GTElement> =
LruCache::new(cache_size.unwrap_or(NonZeroUsize::new(50000).unwrap()));
let cache: LruCache<[u8; 32], GTElement> = LruCache::new(
cache_size.unwrap_or(NonZeroUsize::new(50000).expect("50000 should be non-zero")),
);
Self { cache }
}

Expand Down Expand Up @@ -88,18 +89,15 @@ impl BLSCache {
#[pymethods]
impl BLSCache {
#[new]
pub fn init(size: Option<&PyInt>) -> PyResult<Self> {
pub fn init(size: Option<u32>) -> PyResult<Self> {
match size {
matt-o-how marked this conversation as resolved.
Show resolved Hide resolved
Some(p_size) => {
let r_size = p_size.extract::<isize>().unwrap();
if r_size < 1 {
if p_size < 1 {
Err(PyValueError::new_err(
"Cannot have a cache size less than one.",
))
} else {
Ok(Self::new(NonZeroUsize::new(
p_size.extract::<usize>().unwrap(),
)))
Ok(Self::new(NonZeroUsize::new(p_size as usize)))
}
}
None => Ok(Self::default()),
Expand Down Expand Up @@ -216,6 +214,7 @@ pub mod tests {
let sig: Signature = sign(&sk, msg);
let pk_list: Vec<PublicKey> = [pk].to_vec();
let msg_list: Vec<&[u8]> = vec![msg];
// add to cache by validating them one at a time
assert!(bls_cache.aggregate_verify(pk_list.iter(), msg_list.iter(), &sig));
}
assert_eq!(bls_cache.cache.len(), 3);
Expand All @@ -232,4 +231,12 @@ pub mod tests {
// assert first key has been removed
assert!(bls_cache.cache.get(&h).is_none());
}

pub fn test_empty_sig() {
arvidn marked this conversation as resolved.
Show resolved Hide resolved
let mut bls_cache: BLSCache = BLSCache::default();
let sig: Signature = aggregate(&[]);
let pk_list: [PublicKey; 0] = [];
let msg_list: Vec<&[u8]> = vec![];
assert!(bls_cache.aggregate_verify(pk_list, msg_list, &sig));
}
}
21 changes: 17 additions & 4 deletions tests/test_blscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,30 @@ def test_cached_bls_repeat_pk():
assert cached_bls_old.aggregate_verify(pks_bytes, msgs, agg_sig, True)


def test_empty_sig():
sig = AugSchemeMPL.aggregate([])
cached_bls = BLSCache()
assert cached_bls.aggregate_verify([], [], sig)
assert cached_bls_old.aggregate_verify([], [], sig)


def test_bad_cache_size():
with pytest.raises(ValueError) as exc_info:
bls_cache = BLSCache(0)

assert str(exc_info.value) == "Cannot have a cache size less than one."
with pytest.raises(ValueError) as exc_info:

with pytest.raises(OverflowError) as exc_info:
bls_cache = BLSCache(-1)

assert str(exc_info.value) == "Cannot have a cache size less than one."
assert str(exc_info.value) == "can't convert negative int to unsigned"

with pytest.raises(ValueError) as exc_info:
with pytest.raises(OverflowError) as exc_info:
bls_cache = BLSCache(-100000)
arvidn marked this conversation as resolved.
Show resolved Hide resolved

assert str(exc_info.value) == "Cannot have a cache size less than one."
assert str(exc_info.value) == "can't convert negative int to unsigned"

with pytest.raises(OverflowError) as exc_info:
bls_cache = BLSCache(-9223372036854775809)

assert str(exc_info.value) == "can't convert negative int to unsigned"
arvidn marked this conversation as resolved.
Show resolved Hide resolved
Loading