Skip to content

Commit

Permalink
add rust poseidon example (#392)
Browse files Browse the repository at this point in the history
add rust poseidon example
  • Loading branch information
ChickenLover authored Feb 22, 2024
1 parent 402c9df commit efda4de
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ concurrency:

jobs:
test-examples:
runs-on: [self-hosted, Linux, X64, icicle] # ubuntu-latest
runs-on: [self-hosted, Linux, X64, icicle, examples]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
27 changes: 27 additions & 0 deletions examples/rust/poseidon/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use the specified base image
#FROM nvidia/cuda:12.2.0-devel-ubuntu22.04
FROM nvidia/cuda:12.0.0-devel-ubuntu22.04

# Update and install dependencies
RUN apt-get update && apt-get install -y \
cmake \
protobuf-compiler \
curl \
build-essential \
git \
llvm \
clang \
&& rm -rf /var/lib/apt/lists/*

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Set the working directory in the container
WORKDIR /icicle-example

# Copy the content of the local directory to the working directory
COPY . .

# Specify the default command for the container
CMD ["/bin/bash"]
23 changes: 23 additions & 0 deletions examples/rust/poseidon/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Icicle Examples: rust poseidon",
"build": {
"dockerfile": "Dockerfile"
},
"runArgs": [
"--gpus",
"all"
],
"postCreateCommand": [
"nvidia-smi"
],
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cmake-tools",
"ms-azuretools.vscode-docker",
"rust-lang.rust-analyzer",
"vadimcn.vscode-lldb"
]
}
}
}
14 changes: 14 additions & 0 deletions examples/rust/poseidon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "poseidon"
version = "1.4.0"
edition = "2018"

[dependencies]
icicle-cuda-runtime = { path = "../../../wrappers/rust/icicle-cuda-runtime" }
icicle-core = { path = "../../../wrappers/rust/icicle-core" }
icicle-bls12-381 = { path = "../../../wrappers/rust/icicle-curves/icicle-bls12-381" }

clap = { version = "<=4.4.12", features = ["derive"] }

[features]
profile = []
53 changes: 53 additions & 0 deletions examples/rust/poseidon/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use icicle_bls12_381::curve::ScalarField as F;

use icicle_cuda_runtime::device_context::DeviceContext;

use icicle_core::poseidon::{load_optimized_poseidon_constants, poseidon_hash_many, PoseidonConfig};
use icicle_core::traits::FieldImpl;
use icicle_cuda_runtime::memory::HostOrDeviceSlice;

#[cfg(feature = "profile")]
use std::time::Instant;

use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
/// Size of Poseidon input to run (20 for 2^20)
#[arg(short, long, default_value_t = 20)]
size: u8,
}

fn main() {
let args = Args::parse();
let size = args.size;
let test_size = 1 << size;

println!("Running Icicle Examples: Rust Poseidon Hash");
let arity = 2u32;
println!("---------------------- Loading optimized Poseidon constants for arity={} ------------------------", arity);
let ctx = DeviceContext::default();
let constants = load_optimized_poseidon_constants::<F>(arity, &ctx).unwrap();
let config = PoseidonConfig::default();

println!("---------------------- Input size 2^{}={} ------------------------", size, test_size);
let inputs = vec![F::one(); test_size * arity as usize];
let outputs = vec![F::zero(); test_size];
let mut input_slice = HostOrDeviceSlice::on_host(inputs);
let mut output_slice = HostOrDeviceSlice::on_host(outputs);

println!("Executing BLS12-381 Poseidon Hash on device...");
#[cfg(feature = "profile")]
let start = Instant::now();
poseidon_hash_many::<F>(
&mut input_slice,
&mut output_slice,
test_size as u32,
arity as u32,
&constants,
&config,
)
.unwrap();
#[cfg(feature = "profile")]
println!("ICICLE BLS12-381 Poseidon Hash on size 2^{size} took: {} μs", start.elapsed().as_micros());
}

0 comments on commit efda4de

Please sign in to comment.