Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting example #753

Open
wants to merge 5 commits into
base: matthias/ceno_host
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions ceno_host/tests/test_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ fn test_hints() -> Result<()> {
Ok(())
}

#[test]
fn test_sorting() -> Result<()> {
use rand::Rng;
let mut hints = CenoStdin::default();
let mut rng = rand::thread_rng();

// Provide some random numbers to sort.
hints.write(&(0..1000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>())?;

let all_messages = ceno_host::run(CENO_PLATFORM, ceno_examples::sorting, &hints);
for (i, msg) in enumerate(&all_messages) {
println!("{i}: {msg}");
}
Ok(())
}

#[test]
fn test_median() -> Result<()> {
use rand::Rng;
let mut hints = CenoStdin::default();
let mut rng = rand::thread_rng();

// Provide some random numbers to find the median of.
let mut nums = (0..1000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>();
hints.write(&nums)?;
nums.sort();
hints.write(&nums[nums.len() / 2])?;

let all_messages = ceno_host::run(CENO_PLATFORM, ceno_examples::median, &hints);
assert!(!all_messages.is_empty());
for (i, msg) in enumerate(&all_messages) {
println!("{i}: {msg}");
}
Ok(())
}

fn run(state: &mut VMState) -> Result<Vec<StepRecord>> {
let steps = state.iter_until_halt().collect::<Result<Vec<_>>>()?;
eprintln!("Emulator ran for {} steps.", steps.len());
Expand Down
2 changes: 2 additions & 0 deletions examples-builder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const EXAMPLES: &[&str] = &[
"ceno_rt_mini",
"ceno_rt_panic",
"hints",
"sorting",
"median",
];
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

Expand Down
20 changes: 20 additions & 0 deletions examples/examples/median.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Find the median of a collection of numbers.
//!
//! Of course, we are asking our good friend, the host, for help, but we still need to verify the answer.
#![no_main]
#![no_std]

extern crate ceno_rt;
use ceno_rt::println;
use core::fmt::Write;
use rkyv::{Archived, vec::ArchivedVec};

ceno_rt::entry!(main);
fn main() {
let numbers: &ArchivedVec<u32> = ceno_rt::read();
let median_candidate: &Archived<u32> = ceno_rt::read();
let median_candidate = &&median_candidate.to_native();
let smaller = numbers.iter().filter(move |x| x < median_candidate).count();
assert_eq!(smaller, numbers.len() / 2);
println!("{}", median_candidate);
}
16 changes: 16 additions & 0 deletions examples/examples/sorting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![no_main]
#![no_std]

extern crate ceno_rt;
use ceno_rt::println;
use core::fmt::Write;
use rkyv::vec::ArchivedVec;

ceno_rt::entry!(main);
fn main() {
let input: &ArchivedVec<u32> = ceno_rt::read();
let mut scratch = input.to_vec();
scratch.sort();
// Print any output you feel like, eg the first element of the sorted vector:
println!("{}", scratch[0]);
}