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

CHIA-899: Add Python binding to scalar_multiply #598

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/chia-bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/Chia-Network/chia_rs"
workspace = true

[features]
py-bindings = ["dep:pyo3", "chia_py_streamable_macro", "chia-traits/py-bindings"]
py-bindings = ["dep:pyo3", "dep:num-bigint", "chia_py_streamable_macro", "chia-traits/py-bindings"]
arbitrary = ["dep:arbitrary"]

[dependencies]
Expand All @@ -24,9 +24,10 @@ hkdf = { workspace = true }
blst = { workspace = true }
hex = { workspace = true }
thiserror = { workspace = true }
pyo3 = { workspace = true, features = ["multiple-pymethods"], optional = true }
pyo3 = { workspace = true, features = ["multiple-pymethods", "num-bigint"], optional = true }
arbitrary = { workspace = true, optional = true }
lru = { workspace = true }
num-bigint = { workspace = true, optional = true }

[dev-dependencies]
rand = { workspace = true }
Expand Down
13 changes: 13 additions & 0 deletions crates/chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ pub fn hash_to_g1_with_dst(msg: &[u8], dst: &[u8]) -> PublicKey {
PublicKey(p1)
}

#[cfg(feature = "py-bindings")]
use num_bigint::BigInt;

#[cfg(feature = "py-bindings")]
#[pyo3::pymethods]
impl PublicKey {
Expand Down Expand Up @@ -340,6 +343,16 @@ impl PublicKey {
pub fn __iadd__(&mut self, rhs: &Self) {
*self += rhs;
}

#[must_use]
#[pyo3(name = "scalar_multiply")]
#[allow(clippy::needless_pass_by_value)]
pub fn py_scalar_multiply(&self, scalar: BigInt) -> Self {
let mut clone = *self;
let bytes = scalar.to_signed_bytes_be();
clone.scalar_multiply(&bytes);
clone
}
}

#[cfg(feature = "py-bindings")]
Expand Down
34 changes: 34 additions & 0 deletions crates/chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ pub fn sign<Msg: AsRef<[u8]>>(sk: &SecretKey, msg: Msg) -> Signature {
sign_raw(sk, aug_msg)
}

#[cfg(feature = "py-bindings")]
use num_bigint::BigInt;

#[cfg(feature = "py-bindings")]
#[pyo3::pymethods]
impl Signature {
Expand Down Expand Up @@ -509,6 +512,16 @@ impl Signature {
pub fn __iadd__(&mut self, rhs: &Self) {
*self += rhs;
}

#[must_use]
#[pyo3(name = "scalar_multiply")]
#[allow(clippy::needless_pass_by_value)]
pub fn py_scalar_multiply(&self, scalar: BigInt) -> Self {
let mut clone = self.clone();
let bytes = scalar.to_signed_bytes_be();
clone.scalar_multiply(&bytes);
clone
}
}

#[cfg(feature = "py-bindings")]
Expand Down Expand Up @@ -1203,6 +1216,27 @@ mod tests {
}
}

#[test]
fn test_scalar_multiply_large() {
let mut rng = StdRng::seed_from_u64(1337);
let mut data = [0; 4198];
rng.fill(data.as_mut_slice());
let seed: [u8; 32] = rng.gen();
let msg: [u8; 32] = rng.gen();
let sk = SecretKey::from_seed(&seed);
let mut g2 = sign(&sk, msg);
g2.scalar_multiply(&data);
assert_eq!(
hex::encode(g2.to_bytes()),
"
ae4d384a25c51283b8be8c6546d23e1555995c87fbcc0fe12169b63a052c4a1f
c9d3a020e8e010d4be619e3c0980a1f213b951fe75375012c5df6a690548a637
ef25f8da1e4f9c8f4d2062531ce688c040258a76543831abde774872e00af74b
"
.replace([' ', '\n'], "")
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should have a comment with a reference where this test vector comes from, or you could turn it into a property-check, to ensure the result satisfies some expected properties of scalar multiplication

}

#[test]
fn test_hash_to_g2_different_dst() {
const DEFAULT_DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_";
Expand Down
2 changes: 2 additions & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def __init__(
"def __add__(self, other: G1Element) -> G1Element: ...",
"def __iadd__(self, other: G1Element) -> G1Element: ...",
"def derive_unhardened(self, int) -> G1Element: ...",
"def scalar_multiply(self, value: int) -> G1Element: ...",
],
)
print_class(
Expand All @@ -404,6 +405,7 @@ def __init__(
"def __str__(self) -> str: ...",
"def __add__(self, other: G2Element) -> G2Element: ...",
"def __iadd__(self, other: G2Element) -> G2Element: ...",
"def scalar_multiply(self, value: int) -> G2Element: ...",
],
)
print_class(
Expand Down
2 changes: 2 additions & 0 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class G1Element:
def __add__(self, other: G1Element) -> G1Element: ...
def __iadd__(self, other: G1Element) -> G1Element: ...
def derive_unhardened(self, int) -> G1Element: ...
def scalar_multiply(self, value: int) -> G1Element: ...
def __init__(
self
) -> None: ...
Expand Down Expand Up @@ -158,6 +159,7 @@ class G2Element:
def __str__(self) -> str: ...
def __add__(self, other: G2Element) -> G2Element: ...
def __iadd__(self, other: G2Element) -> G2Element: ...
def scalar_multiply(self, value: int) -> G2Element: ...
def __init__(
self
) -> None: ...
Expand Down
Loading