From 43ed1b7b908be84653482a2ed04cbae383f33ca1 Mon Sep 17 00:00:00 2001 From: Michael Turner Date: Fri, 31 Mar 2023 00:27:07 -0500 Subject: [PATCH] Add the ability to use python to wrap Aleo functionality --- Cargo.toml | 1 + aleo_python/Cargo.toml | 9 +++++++++ aleo_python/hash_integer.py | 16 ++++++++++++++++ aleo_python/install.sh | 10 ++++++++++ aleo_python/src/hash.rs | 13 +++++++++++++ aleo_python/src/lib.rs | 12 ++++++++++++ .../aleo-cryptography/src/lib.rs | 2 +- 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 aleo_python/Cargo.toml create mode 100644 aleo_python/hash_integer.py create mode 100755 aleo_python/install.sh create mode 100644 aleo_python/src/hash.rs create mode 100644 aleo_python/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 4f1feb4..3600b08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ + "aleo_python", "applied-crypto-references/aleo-cryptography", "applied-crypto-references", "applied-crypto-references/curve-operations", diff --git a/aleo_python/Cargo.toml b/aleo_python/Cargo.toml new file mode 100644 index 0000000..8ebe2d1 --- /dev/null +++ b/aleo_python/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aleo_python" +authors = ["Mike Turner"] +version = "0.1.0" +edition = "2021" + +[dependencies] +pyo3 = { version = "0.18.2", features = ["extension-module"] } +snarkvm = { version = "0.9.14", features = ["console"] } diff --git a/aleo_python/hash_integer.py b/aleo_python/hash_integer.py new file mode 100644 index 0000000..8939fd9 --- /dev/null +++ b/aleo_python/hash_integer.py @@ -0,0 +1,16 @@ +import sys +from aleo_python import hash_int + +def hash_integer(integer): + return hash_int(integer) + +if __name__ == "__main__": + if len(sys.argv) > 1: + try: + input_integer = int(sys.argv[1]) + hashed_integer = hash_integer(input_integer) + print(f"The hash of {input_integer} is {hashed_integer}.") + except ValueError: + print("Invalid input. Please provide an integer.") + else: + print("Please provide an integer as a command line argument.") diff --git a/aleo_python/install.sh b/aleo_python/install.sh new file mode 100755 index 0000000..cb609c6 --- /dev/null +++ b/aleo_python/install.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +## This script assumes you have already installed Rust 1.6+ and Python 3.8.5+ +## The script below will install the Aleo Python bindings and create the aleo_python library allowing you to use +## the Aleo implementation of the Poseidon hash function in Python. +python -m venv .env +source .env/bin/activate +pip install maturin +maturin develop +python hash_integer.py 5 diff --git a/aleo_python/src/hash.rs b/aleo_python/src/hash.rs new file mode 100644 index 0000000..30bc20f --- /dev/null +++ b/aleo_python/src/hash.rs @@ -0,0 +1,13 @@ +use super::*; +use snarkvm::console::algorithms::Poseidon2; +use snarkvm::prelude::{Hash, Testnet3, Field}; +use ToString; + +// Takes a poseiden hash of an integer and returns the hash as a string +#[pyfunction] +pub fn hash_int(a: u64) -> PyResult { + let field = Field::from_u64(a); + let hasher = Poseidon2::setup("Poseidon2").unwrap(); + let hash: Field = hasher.hash(&[field]).unwrap(); + Ok(hash.to_string()) +} diff --git a/aleo_python/src/lib.rs b/aleo_python/src/lib.rs new file mode 100644 index 0000000..88cc761 --- /dev/null +++ b/aleo_python/src/lib.rs @@ -0,0 +1,12 @@ +use pyo3::prelude::*; + +pub mod hash; +pub use hash::*; + +/// A Python module implemented in Rust. +#[pymodule] +fn aleo_python(_py: Python<'_>, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(hash_int, m)?)?; + + Ok(()) +} diff --git a/applied-crypto-references/aleo-cryptography/src/lib.rs b/applied-crypto-references/aleo-cryptography/src/lib.rs index a6ebe65..dce4bf4 100644 --- a/applied-crypto-references/aleo-cryptography/src/lib.rs +++ b/applied-crypto-references/aleo-cryptography/src/lib.rs @@ -1 +1 @@ -mod algebra; \ No newline at end of file +mod algebra;