Skip to content

Commit

Permalink
Add the ability to use python to wrap Aleo functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalwaysuncomfortable committed Mar 31, 2023
1 parent 16514ff commit 43ed1b7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"aleo_python",
"applied-crypto-references/aleo-cryptography",
"applied-crypto-references",
"applied-crypto-references/curve-operations",
Expand Down
9 changes: 9 additions & 0 deletions aleo_python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
16 changes: 16 additions & 0 deletions aleo_python/hash_integer.py
Original file line number Diff line number Diff line change
@@ -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.")
10 changes: 10 additions & 0 deletions aleo_python/install.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions aleo_python/src/hash.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
let field = Field::from_u64(a);
let hasher = Poseidon2::setup("Poseidon2").unwrap();
let hash: Field<Testnet3> = hasher.hash(&[field]).unwrap();
Ok(hash.to_string())
}
12 changes: 12 additions & 0 deletions aleo_python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 1 addition & 1 deletion applied-crypto-references/aleo-cryptography/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod algebra;
mod algebra;

0 comments on commit 43ed1b7

Please sign in to comment.