accept int as input, output

This commit is contained in:
2024-09-04 14:54:42 +08:00
parent 0f289a728b
commit 31d5d63897
5 changed files with 24 additions and 30 deletions

View File

@@ -194,13 +194,13 @@ fn point_multiplication(p: &Point, n: &BigUint) -> Point {
/// SM2 addition
#[pyfunction]
fn add(p1: (String, String), p2: (String, String)) -> (String, String) {
fn add(p1: (BigUint, BigUint), p2: (BigUint, BigUint)) -> (BigUint, BigUint) {
let curve = sm2p256v1();
let x1 = BigUint::parse_bytes(p1.0.as_bytes(), 10).unwrap();
let y1 = BigUint::parse_bytes(p1.1.as_bytes(), 10).unwrap();
let x2 = BigUint::parse_bytes(p2.0.as_bytes(), 10).unwrap();
let y2 = BigUint::parse_bytes(p2.1.as_bytes(), 10).unwrap();
let x1 = p1.0;
let y1 = p1.1;
let x2 = p2.0;
let y2 = p2.1;
// 检查 x 和 y 是否小于曲线参数 p
if x1 >= curve.p || y1 >= curve.p {
@@ -223,30 +223,28 @@ fn add(p1: (String, String), p2: (String, String)) -> (String, String) {
};
let result = point_addition(&point1, &point2);
(result.x.to_str_radix(10), result.y.to_str_radix(10))
(result.x, result.y)
}
/// SM2 multiply
#[pyfunction]
fn multiply(point: (String, String), n: String) -> (String, String) {
fn multiply(point: (BigUint, BigUint), n: BigUint) -> (BigUint, BigUint) {
let curve = sm2p256v1();
// Construct the point with BigUint values
let point = Point {
x: BigUint::parse_bytes(point.0.as_bytes(), 10).unwrap(),
y: BigUint::parse_bytes(point.1.as_bytes(), 10).unwrap(),
x: point.0.clone(),
y: point.1.clone(),
curve: curve.clone(),
};
let scalar_bn = BigUint::parse_bytes(n.as_bytes(), 10).unwrap();
let result = point_multiplication(&point, &scalar_bn);
(result.x.to_str_radix(10), result.y.to_str_radix(10))
// Perform point multiplication
let result = point_multiplication(&point, &n);
(result.x, result.y)
}
/// A Python module implemented in Rust.
#[pymodule]
fn ecc_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction_bound!(multiply, m)?)?;
m.add_function(wrap_pyfunction_bound!(add, m)?)?;
m.add_function(wrap_pyfunction!(multiply, m)?)?;
m.add_function(wrap_pyfunction!(add, m)?)?;
Ok(())
}