Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/rust/lattice-snarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use icicle_babykoala::{
ring::ScalarRing as Zq, // the scalar integer ring Zq (q~64b)
};
use icicle_runtime::{
memory::{DeviceVec, HostSlice},
memory::{DeviceVec, HostSlice, IntoIcicleSlice, IntoIcicleSliceMut},
IcicleError,
};

Expand Down Expand Up @@ -51,7 +51,7 @@ where
let input = P::generate_random(size);

// Allocate and transfer to device memory
let mut device_input = DeviceVec::from_host_slice(&input);
let mut device_input = input.clone();

let cfg = negacyclic_ntt::NegacyclicNttConfig::default();

Expand All @@ -61,7 +61,7 @@ where
// In-place NTT on device memory (timed)
// ----------------------------------------------------------------------
let start = Instant::now();
negacyclic_ntt::ntt_inplace(&mut device_input, NTTDir::kForward, &cfg).unwrap();
negacyclic_ntt::ntt_inplace(device_input.into_slice_mut(), NTTDir::kForward, &cfg).unwrap();
let duration = start.elapsed();
println!(
"[NTT] In-place forward NTT completed in {:.2?} for {} polynomials",
Expand All @@ -73,7 +73,7 @@ where
// ----------------------------------------------------------------------
let mut output = vec![P::zero(); size];
negacyclic_ntt::ntt(
&device_input,
device_input.into_slice(),
NTTDir::kForward,
&cfg,
output.into_slice_mut(),
Expand Down Expand Up @@ -114,17 +114,17 @@ where
let host_b: Vec<P> = P::generate_random(b_len);

// Allocate device memory for inputs and output
let device_a = DeviceVec::from_host_slice(&host_a);
let device_b = DeviceVec::from_host_slice(&host_b);
let device_a = host_a.into_slice();
let device_b = host_b.into_slice();
let mut device_c = DeviceVec::<P>::device_malloc(c_len).expect("Allocation failed");

// Perform matrix multiplication on device: C = A × B
let start = std::time::Instant::now();
matrix_ops::matmul::<P>(
&device_a,
device_a,
n,
m,
&device_b,
device_b,
m,
k,
&MatMulConfig::default(), // Can define Aᵗ or Bᵗ here assuming dims match
Expand Down Expand Up @@ -159,12 +159,12 @@ where
let host_input: Vec<P> = P::generate_random(len);

// Allocate device memory
let device_input = DeviceVec::from_host_slice(&host_input);
let device_input = host_input.into_slice();
let mut device_output = DeviceVec::<P>::device_malloc(len).expect("Allocation failed");

// Transpose
let start = std::time::Instant::now();
matrix_ops::matrix_transpose::<P>(&device_input, rows, cols, &VecOpsConfig::default(), &mut device_output)
matrix_ops::matrix_transpose::<P>(device_input, rows, cols, &VecOpsConfig::default(), &mut device_output)
.expect("Transpose failed");
let elapsed = start.elapsed();

Expand Down Expand Up @@ -299,15 +299,15 @@ where
// ----------------------------------------------------------------------

let host_input: Vec<P> = P::generate_random(size);
let device_input = DeviceVec::from_host_slice(&host_input);
let device_input = host_input.into_slice();

// ----------------------------------------------------------------------
// (2) JL projection on flattened device memory
// ----------------------------------------------------------------------

// Reinterpret `PolyRing` as a flat slice of base field elements (`Zq`),
// since the projection operates on individual scalars rather than polynomials.
let zq_device_slice = flatten_polyring_slice(&device_input);
let zq_device_slice = flatten_polyring_slice(device_input);
let mut device_output = DeviceVec::<P::Base>::malloc(projection_dim);

let t_start = std::time::Instant::now();
Expand Down
2 changes: 1 addition & 1 deletion examples/rust/msm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use icicle_core::projective::Projective;
use icicle_runtime::{
memory::{DeviceVec, IntoIcicleSlice, IntoIcicleSliceMut},
memory::{DeviceVec, IntoIcicleSlice},
stream::IcicleStream,
};

Expand Down
5 changes: 3 additions & 2 deletions examples/rust/ntt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use icicle_bls12_377::curve::ScalarField as BLS12377ScalarField;
use icicle_bn254::curve::ScalarField;
use icicle_core::bignum::BigNum;
use icicle_runtime::memory::{DeviceVec, HostSlice};
use icicle_runtime::memory::{DeviceVec, IntoIcicleSlice, IntoIcicleSliceMut};

use clap::Parser;
use icicle_core::{
Expand Down Expand Up @@ -55,7 +55,8 @@ fn main() {

// Setting Bn254 points and scalars
println!("Generating random inputs on host for bn254...");
let scalars = ScalarField::generate_random(size);
let mut scalars = ScalarField::generate_random(size);
let scalars_orig = scalars.clone();
let mut ntt_results = DeviceVec::<ScalarField>::malloc(size);

// Setting bls12377 points and scalars
Expand Down
2 changes: 1 addition & 1 deletion examples/rust/polynomials/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use icicle_bn254::curve::ScalarField as Bn254ScalarField;
use icicle_bn254::polynomials::DensePolynomial as PolynomialBn254;
use icicle_core::bignum::BigNum;

use icicle_runtime::memory::{DeviceVec, HostSlice, IntoIcicleSlice, IntoIcicleSliceMut};
use icicle_runtime::memory::{DeviceVec, IntoIcicleSlice};

use icicle_core::field::Field;
use icicle_core::{
Expand Down
2 changes: 1 addition & 1 deletion examples/rust/poseidon2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use icicle_core::{
poseidon2::Poseidon2,
};
use icicle_m31::field::ScalarField as M31Field;
use icicle_runtime::memory::HostSlice;
use icicle_runtime::memory::{IntoIcicleSlice, IntoIcicleSliceMut};
use rand::{random, Rng};
use std::convert::TryInto;

Expand Down
2 changes: 1 addition & 1 deletion examples/rust/sumcheck/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if [ "$DEVICE_TYPE" != "CPU" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [
echo "Building icicle and ${DEVICE_TYPE} backend"
cargo build --release --features="${DEVICE_TYPE_LOWERCASE}"
export ICICLE_BACKEND_INSTALL_DIR=$(realpath "../target/release/deps/icicle/lib/backend")
RUST_LOG=info cargo run -q--release --package sumcheck --bin sumcheck --features="${DEVICE_TYPE_LOWERCASE}" -- --device-type "${DEVICE_TYPE}"
RUST_LOG=info cargo run -q --release --package sumcheck --bin sumcheck --features="${DEVICE_TYPE_LOWERCASE}" -- --device-type "${DEVICE_TYPE}"
else
echo "Building icicle without backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}"
export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}"
Expand Down
20 changes: 20 additions & 0 deletions wrappers/rust/icicle-runtime/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,18 @@ impl<T> DeviceVec<T> {
.unwrap();
device_vec
}

/// Convert this `DeviceVec` to a `DeviceSlice` reference.
/// This inherent method allows calling `into_slice()` without importing the `IntoIcicleSlice` trait.
pub fn into_slice<'a>(&'a self) -> &'a DeviceSlice<T> {
<Self as IntoIcicleSlice<'a, T>>::into_slice(self)
}

/// Convert this `DeviceVec` to a mutable `DeviceSlice` reference.
/// This inherent method allows calling `into_slice_mut()` without importing the `IntoIcicleSliceMut` trait.
pub fn into_slice_mut<'a>(&'a mut self) -> &'a mut DeviceSlice<T> {
<Self as IntoIcicleSliceMut<'a, T>>::into_slice_mut(self)
}
}

impl<T> Drop for DeviceVec<T> {
Expand Down Expand Up @@ -674,6 +686,13 @@ impl<'a, T> IntoIcicleSlice<'a, T> for &'a [T] {
}
}

impl<'a, T: 'a> IntoIcicleSlice<'a, T> for [T] {
type Out = HostSlice<T>;
fn into_slice(&'a self) -> &'a HostSlice<T> {
HostSlice::from_slice(self)
}
}

impl<'a, T> IntoIcicleSlice<'a, T> for &'a Vec<T> {
type Out = HostSlice<T>;
fn into_slice(&'a self) -> &'a HostSlice<T> {
Expand Down Expand Up @@ -751,6 +770,7 @@ impl<'a, T: 'a> IntoIcicleSliceMut<'a, T> for DeviceVec<T> {
&mut **self
}
}

// Utility to reinterpret HostOrDeviceSlice via a UnifiedSlice or UnifiedSliceMut that also implement HostOrDeviceSlice.
pub mod reinterpret {
use super::*;
Expand Down