From 3e834f21e279c635530d7ce1737d41a59ffade11 Mon Sep 17 00:00:00 2001 From: Rigidity Date: Mon, 24 Jun 2024 07:57:49 -0400 Subject: [PATCH 1/2] Fix pymethods by moving outside of pybindings modules --- Cargo.toml | 2 +- crates/chia-bls/src/bls_cache.rs | 150 +++++++++++++++--------------- crates/chia-bls/src/gtelement.rs | 45 ++++----- crates/chia-bls/src/public_key.rs | 85 ++++++++--------- crates/chia-bls/src/secret_key.rs | 66 ++++++------- crates/chia-bls/src/signature.rs | 70 +++++++------- 6 files changed, 209 insertions(+), 209 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9796baca6..641250375 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ future_incompatible = { level = "deny", priority = -1 } nonstandard_style = { level = "deny", priority = -1 } unsafe_code = "deny" non_ascii_idents = "deny" -unused_imports = "deny" +unused_imports = "warn" unused_import_braces = "deny" unreachable_code = "deny" unreachable_patterns = "deny" diff --git a/crates/chia-bls/src/bls_cache.rs b/crates/chia-bls/src/bls_cache.rs index 79388f7e2..38e255c63 100644 --- a/crates/chia-bls/src/bls_cache.rs +++ b/crates/chia-bls/src/bls_cache.rs @@ -81,90 +81,86 @@ impl BlsCache { } #[cfg(feature = "py-bindings")] -mod python { - use super::*; +use pyo3::{ + exceptions::PyValueError, + pybacked::PyBackedBytes, + types::{PyAnyMethods, PyList}, + Bound, PyObject, PyResult, +}; - use pyo3::{ - exceptions::PyValueError, - pybacked::PyBackedBytes, - pymethods, - types::{PyAnyMethods, PyList}, - Bound, PyObject, PyResult, - }; - - #[pymethods] - impl BlsCache { - #[new] - #[pyo3(signature = (size=None))] - pub fn init(size: Option) -> PyResult { - let Some(size) = size else { - return Ok(Self::default()); - }; - - let Some(size) = NonZeroUsize::new(size as usize) else { - return Err(PyValueError::new_err( - "Cannot have a cache size less than one.", - )); - }; - - Ok(Self::new(size)) - } +#[cfg(feature = "py-bindings")] +#[pyo3::pymethods] +impl BlsCache { + #[new] + #[pyo3(signature = (size=None))] + pub fn init(size: Option) -> PyResult { + let Some(size) = size else { + return Ok(Self::default()); + }; + + let Some(size) = NonZeroUsize::new(size as usize) else { + return Err(PyValueError::new_err( + "Cannot have a cache size less than one.", + )); + }; + + Ok(Self::new(size)) + } - #[pyo3(name = "aggregate_verify")] - pub fn py_aggregate_verify( - &mut self, - pks: &Bound<'_, PyList>, - msgs: &Bound<'_, PyList>, - sig: &Signature, - ) -> PyResult { - let pks = pks - .iter()? - .map(|item| item?.extract()) - .collect::>>()?; - - let msgs = msgs - .iter()? - .map(|item| item?.extract()) - .collect::>>()?; - - Ok(self.aggregate_verify(pks, msgs, sig)) - } + #[pyo3(name = "aggregate_verify")] + pub fn py_aggregate_verify( + &mut self, + pks: &Bound<'_, PyList>, + msgs: &Bound<'_, PyList>, + sig: &Signature, + ) -> PyResult { + let pks = pks + .iter()? + .map(|item| item?.extract()) + .collect::>>()?; + + let msgs = msgs + .iter()? + .map(|item| item?.extract()) + .collect::>>()?; + + Ok(self.aggregate_verify(pks, msgs, sig)) + } - #[pyo3(name = "len")] - pub fn py_len(&self) -> PyResult { - Ok(self.len()) - } + #[pyo3(name = "len")] + pub fn py_len(&self) -> PyResult { + Ok(self.len()) + } - #[pyo3(name = "items")] - pub fn py_items(&self, py: pyo3::Python<'_>) -> PyResult { - use pyo3::prelude::*; - use pyo3::types::PyBytes; - let ret = PyList::empty_bound(py); - for (key, value) in &self.cache { - ret.append(( - PyBytes::new_bound(py, key), - PyBytes::new_bound(py, &value.to_bytes()), - ))?; - } - Ok(ret.into()) + #[pyo3(name = "items")] + pub fn py_items(&self, py: pyo3::Python<'_>) -> PyResult { + use pyo3::prelude::*; + use pyo3::types::PyBytes; + let ret = PyList::empty_bound(py); + for (key, value) in &self.cache { + ret.append(( + PyBytes::new_bound(py, key), + PyBytes::new_bound(py, &value.to_bytes()), + ))?; } + Ok(ret.into()) + } - #[pyo3(name = "update")] - pub fn py_update(&mut self, other: &Bound<'_, PyList>) -> PyResult<()> { - for item in other.borrow().iter()? { - let (key, value): (Vec, Vec) = item?.extract()?; - self.cache.put( - key.try_into() - .map_err(|_| PyValueError::new_err("invalid key"))?, - GTElement::from_bytes( - (&value[..]) - .try_into() - .map_err(|_| PyValueError::new_err("invalid GTElement"))?, - ), - ); - } - Ok(()) + #[pyo3(name = "update")] + pub fn py_update(&mut self, other: &Bound<'_, PyList>) -> PyResult<()> { + for item in other.borrow().iter()? { + let (key, value): (Vec, Vec) = item?.extract()?; + self.cache.put( + key.try_into() + .map_err(|_| PyValueError::new_err("invalid key"))?, + GTElement::from_bytes( + (&value[..]) + .try_into() + .map_err(|_| PyValueError::new_err("invalid GTElement"))?, + ), + ); } + Ok(()) } } diff --git a/crates/chia-bls/src/gtelement.rs b/crates/chia-bls/src/gtelement.rs index 740f42806..2375d2540 100644 --- a/crates/chia-bls/src/gtelement.rs +++ b/crates/chia-bls/src/gtelement.rs @@ -102,33 +102,34 @@ impl Streamable for GTElement { } #[cfg(feature = "py-bindings")] -mod pybindings { - use super::*; +#[pyo3::pymethods] +impl GTElement { + #[classattr] + #[pyo3(name = "SIZE")] + pub const PY_SIZE: usize = Self::SIZE; - use chia_traits::{FromJsonDict, ToJsonDict}; - use pyo3::{exceptions::PyValueError, prelude::*}; + pub fn __str__(&self) -> String { + hex::encode(self.to_bytes()) + } - #[pymethods] - impl GTElement { - #[classattr] - #[pyo3(name = "SIZE")] - const PY_SIZE: usize = Self::SIZE; + #[must_use] + pub fn __mul__(&self, rhs: &Self) -> Self { + let mut ret = self.clone(); + ret *= rhs; + ret + } - fn __str__(&self) -> String { - hex::encode(self.to_bytes()) - } + pub fn __imul__(&mut self, rhs: &Self) { + *self *= rhs; + } +} - #[must_use] - pub fn __mul__(&self, rhs: &Self) -> Self { - let mut ret = self.clone(); - ret *= rhs; - ret - } +#[cfg(feature = "py-bindings")] +mod pybindings { + use super::*; - pub fn __imul__(&mut self, rhs: &Self) { - *self *= rhs; - } - } + use chia_traits::{FromJsonDict, ToJsonDict}; + use pyo3::{exceptions::PyValueError, prelude::*}; impl ToJsonDict for GTElement { fn to_json_dict(&self, py: Python<'_>) -> PyResult { diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index 49e8ce373..6ac76ea90 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -273,7 +273,7 @@ impl DerivableKey for PublicKey { } } -pub const DST: &[u8] = b"BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_AUG_"; +pub(crate) const DST: &[u8] = b"BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_AUG_"; pub fn hash_to_g1(msg: &[u8]) -> PublicKey { hash_to_g1_with_dst(msg, DST) @@ -296,58 +296,59 @@ pub fn hash_to_g1_with_dst(msg: &[u8], dst: &[u8]) -> PublicKey { PublicKey(p1) } -#[cfg(feature = "py-bindings")] -mod pybindings { - use super::*; +#[pyo3::pymethods] +impl PublicKey { + #[classattr] + pub const SIZE: usize = 48; - use crate::{parse_hex::parse_hex_string, GTElement, Signature}; + #[new] + pub fn init() -> Self { + Self::default() + } - use chia_traits::{FromJsonDict, ToJsonDict}; - use pyo3::prelude::*; + #[staticmethod] + #[pyo3(name = "generator")] + pub fn py_generator() -> Self { + Self::generator() + } - #[pymethods] - impl PublicKey { - #[classattr] - const SIZE: usize = 48; + pub fn pair(&self, other: &crate::Signature) -> crate::GTElement { + other.pair(self) + } - #[new] - fn init() -> Self { - Self::default() - } + #[pyo3(name = "get_fingerprint")] + pub fn py_get_fingerprint(&self) -> u32 { + self.get_fingerprint() + } - #[staticmethod] - #[pyo3(name = "generator")] - fn py_generator() -> Self { - Self::generator() - } + #[pyo3(name = "derive_unhardened")] + #[must_use] + pub fn py_derive_unhardened(&self, idx: u32) -> Self { + self.derive_unhardened(idx) + } - fn pair(&self, other: &Signature) -> GTElement { - other.pair(self) - } + pub fn __str__(&self) -> String { + hex::encode(self.to_bytes()) + } - #[pyo3(name = "get_fingerprint")] - fn py_get_fingerprint(&self) -> u32 { - self.get_fingerprint() - } + #[must_use] + pub fn __add__(&self, rhs: &Self) -> Self { + self + rhs + } - #[pyo3(name = "derive_unhardened")] - fn py_derive_unhardened(&self, idx: u32) -> Self { - self.derive_unhardened(idx) - } + pub fn __iadd__(&mut self, rhs: &Self) { + *self += rhs; + } +} - fn __str__(&self) -> String { - hex::encode(self.to_bytes()) - } +#[cfg(feature = "py-bindings")] +mod pybindings { + use super::*; - #[must_use] - pub fn __add__(&self, rhs: &Self) -> Self { - self + rhs - } + use crate::parse_hex::parse_hex_string; - pub fn __iadd__(&mut self, rhs: &Self) { - *self += rhs; - } - } + use chia_traits::{FromJsonDict, ToJsonDict}; + use pyo3::prelude::*; impl ToJsonDict for PublicKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { diff --git a/crates/chia-bls/src/secret_key.rs b/crates/chia-bls/src/secret_key.rs index b221a00a9..a3c228095 100644 --- a/crates/chia-bls/src/secret_key.rs +++ b/crates/chia-bls/src/secret_key.rs @@ -236,47 +236,49 @@ impl DerivableKey for SecretKey { } } -#[cfg(feature = "py-bindings")] -mod pybindings { - use super::*; +#[pyo3::pymethods] +impl SecretKey { + #[classattr] + pub const PRIVATE_KEY_SIZE: usize = 32; - use crate::{parse_hex::parse_hex_string, PublicKey, Signature}; + pub fn sign_g2(&self, msg: &[u8]) -> crate::Signature { + crate::sign(self, msg) + } - use chia_traits::{FromJsonDict, ToJsonDict}; - use pyo3::prelude::*; + pub fn get_g1(&self) -> PublicKey { + self.public_key() + } - #[pymethods] - impl SecretKey { - #[classattr] - const PRIVATE_KEY_SIZE: usize = 32; + #[pyo3(name = "public_key")] + pub fn py_public_key(&self) -> PublicKey { + self.public_key() + } - fn sign_g2(&self, msg: &[u8]) -> Signature { - crate::sign(self, msg) - } + pub fn __str__(&self) -> String { + hex::encode(self.to_bytes()) + } - fn get_g1(&self) -> PublicKey { - self.public_key() - } + #[pyo3(name = "derive_hardened")] + #[must_use] + pub fn py_derive_hardened(&self, idx: u32) -> Self { + self.derive_hardened(idx) + } - #[pyo3(name = "public_key")] - fn py_public_key(&self) -> PublicKey { - self.public_key() - } + #[pyo3(name = "derive_unhardened")] + #[must_use] + pub fn py_derive_unhardened(&self, idx: u32) -> Self { + self.derive_unhardened(idx) + } +} - fn __str__(&self) -> String { - hex::encode(self.to_bytes()) - } +#[cfg(feature = "py-bindings")] +mod pybindings { + use super::*; - #[pyo3(name = "derive_hardened")] - fn py_derive_hardened(&self, idx: u32) -> Self { - self.derive_hardened(idx) - } + use crate::parse_hex::parse_hex_string; - #[pyo3(name = "derive_unhardened")] - fn py_derive_unhardened(&self, idx: u32) -> Self { - self.derive_unhardened(idx) - } - } + use chia_traits::{FromJsonDict, ToJsonDict}; + use pyo3::prelude::*; impl ToJsonDict for SecretKey { fn to_json_dict(&self, py: Python<'_>) -> PyResult { diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index 95081e9f1..e3f30f320 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -10,7 +10,7 @@ use std::mem::MaybeUninit; use std::ops::{Add, AddAssign, Neg, SubAssign}; // we use the augmented scheme -pub const DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_"; +pub(crate) const DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_"; #[cfg_attr( feature = "py-bindings", @@ -475,49 +475,49 @@ pub fn sign>(sk: &SecretKey, msg: Msg) -> Signature { sign_raw(sk, aug_msg) } -#[cfg(feature = "py-bindings")] -mod pybindings { - use super::*; +#[pyo3::pymethods] +impl Signature { + #[classattr] + pub const SIZE: usize = 96; - use crate::parse_hex::parse_hex_string; + #[new] + pub fn init() -> Self { + Self::default() + } - use chia_traits::{FromJsonDict, ToJsonDict}; - use pyo3::prelude::*; + #[pyo3(name = "pair")] + pub fn py_pair(&self, other: &PublicKey) -> GTElement { + self.pair(other) + } - #[pymethods] - impl Signature { - #[classattr] - const SIZE: usize = 96; + #[staticmethod] + #[pyo3(name = "generator")] + pub fn py_generator() -> Self { + Self::generator() + } - #[new] - pub fn init() -> Self { - Self::default() - } + pub fn __str__(&self) -> String { + hex::encode(self.to_bytes()) + } - #[pyo3(name = "pair")] - pub fn py_pair(&self, other: &PublicKey) -> GTElement { - self.pair(other) - } + #[must_use] + pub fn __add__(&self, rhs: &Self) -> Self { + self + rhs + } - #[staticmethod] - #[pyo3(name = "generator")] - pub fn py_generator() -> Self { - Self::generator() - } + pub fn __iadd__(&mut self, rhs: &Self) { + *self += rhs; + } +} - fn __str__(&self) -> String { - hex::encode(self.to_bytes()) - } +#[cfg(feature = "py-bindings")] +mod pybindings { + use super::*; - #[must_use] - pub fn __add__(&self, rhs: &Self) -> Self { - self + rhs - } + use crate::parse_hex::parse_hex_string; - pub fn __iadd__(&mut self, rhs: &Self) { - *self += rhs; - } - } + use chia_traits::{FromJsonDict, ToJsonDict}; + use pyo3::prelude::*; impl ToJsonDict for Signature { fn to_json_dict(&self, py: Python<'_>) -> PyResult { From dc184ce1d4ec5033ca48a79065e83044daee88ee Mon Sep 17 00:00:00 2001 From: Rigidity Date: Mon, 24 Jun 2024 08:06:17 -0400 Subject: [PATCH 2/2] Proper feature gating --- crates/chia-bls/src/public_key.rs | 1 + crates/chia-bls/src/secret_key.rs | 1 + crates/chia-bls/src/signature.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index 6ac76ea90..867e1203d 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -296,6 +296,7 @@ pub fn hash_to_g1_with_dst(msg: &[u8], dst: &[u8]) -> PublicKey { PublicKey(p1) } +#[cfg(feature = "py-bindings")] #[pyo3::pymethods] impl PublicKey { #[classattr] diff --git a/crates/chia-bls/src/secret_key.rs b/crates/chia-bls/src/secret_key.rs index a3c228095..661719814 100644 --- a/crates/chia-bls/src/secret_key.rs +++ b/crates/chia-bls/src/secret_key.rs @@ -236,6 +236,7 @@ impl DerivableKey for SecretKey { } } +#[cfg(feature = "py-bindings")] #[pyo3::pymethods] impl SecretKey { #[classattr] diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index e3f30f320..2031757a0 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -475,6 +475,7 @@ pub fn sign>(sk: &SecretKey, msg: Msg) -> Signature { sign_raw(sk, aug_msg) } +#[cfg(feature = "py-bindings")] #[pyo3::pymethods] impl Signature { #[classattr]