#[macro_use]
extern crate cpython;
use cpython::{Python, PyResult};
// file: main.rs
use concrete::*;
fn get_cypher(_py: Python, val: &f64) -> PyResult<()> {
// generate a secret key
let secret_key = LWESecretKey::new(&LWE128_1024);
// encoder
let encoder = Encoder::new(100., 210., 8, 0)?;
// encode and encrypt
let message = 106.276;
let mut ciphertext = LWE::encode_encrypt(&secret_key, message, &encoder)?;
// addition between ciphertext and a constant
let constant = 102.0;
ciphertext.add_constant_static_encoder_inplace(constant)?;
// decryption
let output = ciphertext.decrypt_decode(&secret_key)?;
println!("{} + {} = {}", message, constant, output);
Ok(())
}
py_module_initializer!(libmyrustlib, initlibmyrustlib, PyInit_myrustlib, |py, m | {
(m.add(py, "__doc__", "This module is implemented in Rust"))?;
(m.add(py, "get_cypher", py_fn!(py, get_cypher(val: &f64))))?;
Ok(())
});
so, I'm trying to compile this rust code to be able to call it in python, but I get the following error:
9 | fn get_cypher(_py: Python, val: &f64) -> PyResult<()> {
| ------------ expected `PyErr` because of this
...
24 | let output = ciphertext.decrypt_decode(&secret_key)?;
| ^ the trait `From<CryptoAPIError>` is not implemented for `PyErr`
How can I pass on an error from rust to python so I can compile this code? I guess in general, when a rust library implements an error, how can I make sure that I can still compile code using that library?
Cargo file:
[package]
name = "fhe_play"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
concrete = "0.1.0"
itertools = "0.9.0"
[dependencies.cpython]
version = "0.1"
features = ["extension-module"]
You need to manually convert from one error type to the other if there's not already a conversion defined. You can do this with
.map_err()
. Here's an example:See
PyErr
for a few ways to make a Python error.