Skip to content

Commit

Permalink
Merge branch 'master' into feat/skip_fixed_commit_of_range_table
Browse files Browse the repository at this point in the history
  • Loading branch information
10to4 authored Jan 10, 2025
2 parents 386fca6 + 083b2d4 commit 5837734
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 65 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions ceno_host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ impl From<&CenoStdin> for Vec<u32> {
}

impl CenoStdin {
pub fn write_slice(&mut self, bytes: AlignedVec) {
pub fn write_slice(&mut self, bytes: AlignedVec) -> &mut Self {
self.items.push(bytes);
self
}

pub fn write(
&mut self,
item: &impl for<'a> Serialize<HighSerializer<AlignedVec, ArenaHandle<'a>, Error>>,
) -> Result<(), Error> {
) -> Result<&mut Self, Error> {
to_bytes::<Error>(item).map(|bytes| self.write_slice(bytes))
}
}
Expand Down
63 changes: 26 additions & 37 deletions ceno_host/tests/test_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ceno_emul::{
};
use ceno_host::CenoStdin;
use itertools::{Itertools, enumerate, izip};
use rand::{Rng, thread_rng};
use tiny_keccak::keccakf;

#[test]
Expand Down Expand Up @@ -111,14 +112,15 @@ fn test_ceno_rt_io() -> Result<()> {

#[test]
fn test_hints() -> Result<()> {
let mut hints = CenoStdin::default();
hints.write(&true)?;
hints.write(&"This is my hint string.".to_string())?;
hints.write(&1997_u32)?;
hints.write(&1999_u32)?;

let all_messages =
messages_to_strings(&ceno_host::run(CENO_PLATFORM, ceno_examples::hints, &hints));
let all_messages = messages_to_strings(&ceno_host::run(
CENO_PLATFORM,
ceno_examples::hints,
CenoStdin::default()
.write(&true)?
.write(&"This is my hint string.".to_string())?
.write(&1997_u32)?
.write(&1999_u32)?,
));
for (i, msg) in enumerate(&all_messages) {
println!("{i}: {msg}");
}
Expand All @@ -128,17 +130,12 @@ fn test_hints() -> Result<()> {

#[test]
fn test_bubble_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..1_000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>())?;

let mut rng = thread_rng();
let all_messages = messages_to_strings(&ceno_host::run(
CENO_PLATFORM,
ceno_examples::bubble_sorting,
&hints,
ceno_examples::quadratic_sorting,
// Provide some random numbers to sort.
CenoStdin::default().write(&(0..1_000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>())?,
));
for msg in &all_messages {
print!("{msg}");
Expand All @@ -147,17 +144,12 @@ fn test_bubble_sorting() -> Result<()> {
}
#[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 mut rng = thread_rng();
let all_messages = messages_to_strings(&ceno_host::run(
CENO_PLATFORM,
ceno_examples::sorting,
&hints,
// Provide some random numbers to sort.
CenoStdin::default().write(&(0..1000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>())?,
));
for (i, msg) in enumerate(&all_messages) {
println!("{i}: {msg}");
Expand All @@ -167,9 +159,8 @@ fn test_sorting() -> Result<()> {

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

// Provide some random numbers to find the median of.
let mut nums = (0..1000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>();
Expand All @@ -192,23 +183,22 @@ fn test_median() -> Result<()> {
#[test]
#[should_panic(expected = "Trap IllegalInstruction")]
fn test_hashing_fail() {
use rand::Rng;
let mut hints = CenoStdin::default();
let mut rng = rand::thread_rng();
let mut rng = thread_rng();

let mut nums = (0..1_000).map(|_| rng.gen::<u32>()).collect::<Vec<_>>();
// Add a duplicate number to make uniqueness check fail:
nums[211] = nums[907];
hints.write(&nums).unwrap();

let _ = ceno_host::run(CENO_PLATFORM, ceno_examples::hashing, &hints);
let _ = ceno_host::run(
CENO_PLATFORM,
ceno_examples::hashing,
CenoStdin::default().write(&nums).unwrap(),
);
}

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

// Provide some unique random numbers to verify:
let uniques: Vec<u32> = {
Expand All @@ -219,11 +209,10 @@ fn test_hashing() -> Result<()> {
.collect::<Vec<_>>()
};

hints.write(&uniques)?;
let all_messages = messages_to_strings(&ceno_host::run(
CENO_PLATFORM,
ceno_examples::hashing,
&hints,
CenoStdin::default().write(&uniques)?,
));
assert!(!all_messages.is_empty());
for (i, msg) in enumerate(&all_messages) {
Expand Down
17 changes: 8 additions & 9 deletions ceno_zkvm/src/instructions/riscv/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ use ceno_emul::{
};
use ecall::EcallDummy;
use ff_ext::ExtensionField;
use itertools::Itertools;
use itertools::{Itertools, izip};
use mul::{MulInstruction, MulhInstruction, MulhsuInstruction};
use shift::SraInstruction;
use slt::{SltInstruction, SltuInstruction};
use slti::SltiuInstruction;
use std::collections::{BTreeMap, BTreeSet};
use std::{
cmp::Reverse,
collections::{BTreeMap, BTreeSet},
};
use strum::IntoEnumIterator;

use super::{
Expand Down Expand Up @@ -337,14 +340,10 @@ impl<E: ExtensionField> Rv32imConfig<E> {
}
});

for (insn_kind, (_, records)) in InsnKind::iter()
.zip(all_records.iter())
.sorted_by(|a, b| Ord::cmp(&a.1.1.len(), &b.1.1.len()))
.rev()
for (insn_kind, (_, records)) in
izip!(InsnKind::iter(), &all_records).sorted_by_key(|(_, (_, a))| Reverse(a.len()))
{
if !records.is_empty() {
tracing::info!("tracer generated {:?} {} records", insn_kind, records.len());
}
tracing::info!("tracer generated {:?} {} records", insn_kind, records.len());
}

macro_rules! assign_opcode {
Expand Down
3 changes: 3 additions & 0 deletions examples-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ license.workspace = true
name = "ceno-examples"
repository.workspace = true
version.workspace = true

[build-dependencies]
glob = "0.3"
24 changes: 7 additions & 17 deletions examples-builder/build.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
use glob::glob;
use std::{
fs::{File, read_dir},
io::{self, Write},
path::Path,
process::Command,
};

/// Add each example to this list.
///
/// Contact Matthias, if your examples get complicated enough to need their own crates, instead of just being one file.
const EXAMPLES: &[&str] = &[
"ceno_rt_alloc",
"ceno_rt_io",
"ceno_rt_mem",
"ceno_rt_mini",
"ceno_rt_panic",
"ceno_rt_keccak",
"hints",
"sorting",
"median",
"bubble_sorting",
"hashing",
];
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

fn rerun_all_but_target(dir: &Path) {
Expand Down Expand Up @@ -51,7 +36,12 @@ fn build_elfs() {
io::stderr().write_all(&output.stderr).unwrap();
panic!("cargo build of examples failed.");
}
for example in EXAMPLES {
// Contact Matthias, if your examples get complicated enough to need their own crates, instead of just being one file.
for example in glob("../examples/examples/*.rs")
.unwrap()
.map(Result::unwrap)
{
let example = example.file_stem().unwrap().to_str().unwrap();
writeln!(
dest,
r#"#[allow(non_upper_case_globals)]
Expand Down
File renamed without changes.

0 comments on commit 5837734

Please sign in to comment.