ソースを参照

sdk/python: Add pasta curve serialization

x 7 ヶ月 前
コミット
09bf42ef01
1 ファイル変更23 行追加1 行削除
  1. 23 1
      src/sdk/python/src/pasta.rs

+ 23 - 1
src/sdk/python/src/pasta.rs

@@ -27,7 +27,7 @@ use pyo3::{
     basic::CompareOp,
     pyclass, pyfunction, pymethods,
     types::{PyAnyMethods, PyModule, PyModuleMethods, PyStringMethods, PyTypeMethods},
-    wrap_pyfunction, Bound, PyResult,
+    wrap_pyfunction, Bound, PyErr, PyResult,
 };
 use rand::rngs::OsRng;
 
@@ -99,6 +99,17 @@ macro_rules! impl_elem {
                 Self(self.0.square())
             }
 
+            fn serialize(&self) -> Vec<u8> {
+                darkfi_serial::serialize(&self.0)
+            }
+
+            #[staticmethod]
+            fn deserialize(data: &[u8]) -> PyResult<Self> {
+                darkfi_serial::deserialize(data)
+                    .map(Self)
+                    .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
+            }
+
             fn __str__(&self) -> PyResult<String> {
                 Ok(format!("{:?}", self.0))
             }
@@ -206,6 +217,17 @@ macro_rules! impl_point {
                 Self(<$inner>::from(p.0))
             }
 
+            fn serialize(&self) -> Vec<u8> {
+                darkfi_serial::serialize(&self.0)
+            }
+
+            #[staticmethod]
+            fn deserialize(data: &[u8]) -> PyResult<Self> {
+                darkfi_serial::deserialize(data)
+                    .map(Self)
+                    .map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
+            }
+
             fn __str__(slf: &Bound<Self>) -> PyResult<String> {
                 let affine = <$affine>::from_projective(slf);
                 let (x, y) = affine.coordinates();