Skip to content

Commit

Permalink
Merge branch 'master' into matthias/upgrade-toolchain-2025-01
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgoergens authored Jan 20, 2025
2 parents a7b1b4a + 7919da2 commit 4ea6a4c
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 142 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.

23 changes: 22 additions & 1 deletion ceno_emul/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::BTreeSet, ops::Range};
use core::fmt::{self, Formatter};
use std::{collections::BTreeSet, fmt::Display, ops::Range};

use crate::addr::{Addr, RegIdx};

Expand All @@ -19,6 +20,26 @@ pub struct Platform {
pub unsafe_ecall_nop: bool,
}

impl Display for Platform {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let prog_data: Option<Range<Addr>> = match (self.prog_data.first(), self.prog_data.last()) {
(Some(first), Some(last)) => Some(*first..*last),
_ => None,
};
write!(
f,
"Platform {{ rom: {:?}, prog_data: {:?}, stack: {:?}, heap: {:?}, public_io: {:?}, hints: {:?}, unsafe_ecall_nop: {} }}",
self.rom,
prog_data,
self.stack,
self.heap,
self.public_io,
self.hints,
self.unsafe_ecall_nop
)
}
}

pub const CENO_PLATFORM: Platform = Platform {
rom: 0x2000_0000..0x3000_0000,
prog_data: BTreeSet::new(),
Expand Down
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
2 changes: 1 addition & 1 deletion ceno_zkvm/src/bin/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn main() {
args.heap_size,
pub_io_size,
);
tracing::info!("Running on platform {:?} {:?}", args.platform, platform);
tracing::info!("Running on platform {:?} {}", args.platform, platform);
tracing::info!(
"Stack: {} bytes. Heap: {} bytes.",
args.stack_size,
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/chip_handler/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
pub fn lk_table_record<NR, N>(
&mut self,
name_fn: N,
table_len: usize,
table_spec: SetTableSpec,
rom_type: ROMType,
record: Vec<Expression<E>>,
multiplicity: Expression<E>,
Expand All @@ -105,7 +105,7 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
N: FnOnce() -> NR,
{
self.cs
.lk_table_record(name_fn, table_len, rom_type, record, multiplicity)
.lk_table_record(name_fn, table_spec, rom_type, record, multiplicity)
}

pub fn r_table_record<NR, N>(
Expand Down
13 changes: 3 additions & 10 deletions ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ceno_emul::Addr;
use itertools::{Itertools, chain};
use std::{collections::HashMap, iter::once, marker::PhantomData};

Expand Down Expand Up @@ -56,13 +55,7 @@ impl NameSpace {
pub struct LogupTableExpression<E: ExtensionField> {
pub multiplicity: Expression<E>,
pub values: Expression<E>,
pub table_len: usize,
}

#[derive(Clone, Debug)]
pub struct DynamicAddr {
pub addr_witin_id: usize,
pub offset: Addr,
pub table_spec: SetTableSpec,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -297,7 +290,7 @@ impl<E: ExtensionField> ConstraintSystem<E> {
pub fn lk_table_record<NR, N>(
&mut self,
name_fn: N,
table_len: usize,
table_spec: SetTableSpec,
rom_type: ROMType,
record: Vec<Expression<E>>,
multiplicity: Expression<E>,
Expand All @@ -321,7 +314,7 @@ impl<E: ExtensionField> ConstraintSystem<E> {
self.lk_table_expressions.push(LogupTableExpression {
values: rlc_record,
multiplicity,
table_len,
table_spec,
});
let path = self.ns.compute_path(name_fn().into());
self.lk_expressions_namespace_map.push(path);
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
10 changes: 7 additions & 3 deletions ceno_zkvm/src/scheme/mock_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ Hints:
);

let mut wit_mles = HashMap::new();
let mut structural_wit_mles = HashMap::new();
let mut fixed_mles = HashMap::new();
let mut num_instances = HashMap::new();

Expand All @@ -815,15 +816,17 @@ Hints:

if witness.num_instances() == 0 {
wit_mles.insert(circuit_name.clone(), vec![]);
structural_wit_mles.insert(circuit_name.clone(), vec![]);
fixed_mles.insert(circuit_name.clone(), vec![]);
num_instances.insert(circuit_name.clone(), num_rows);
continue;
}
let witness = witness
let mut witness = witness
.into_mles()
.into_iter()
.map(|w| w.into())
.collect_vec();
let structural_witness = witness.split_off(cs.num_witin as usize);
let fixed: Vec<_> = fixed_trace
.circuit_fixed_traces
.remove(circuit_name)
Expand Down Expand Up @@ -876,7 +879,7 @@ Hints:
let lk_table = wit_infer_by_expr(
&fixed,
&witness,
&[],
&structural_witness,
&pi_mles,
&challenges,
&expr.values,
Expand All @@ -887,7 +890,7 @@ Hints:
let multiplicity = wit_infer_by_expr(
&fixed,
&witness,
&[],
&structural_witness,
&pi_mles,
&challenges,
&expr.multiplicity,
Expand All @@ -905,6 +908,7 @@ Hints:
}
}
wit_mles.insert(circuit_name.clone(), witness);
structural_wit_mles.insert(circuit_name.clone(), structural_witness);
fixed_mles.insert(circuit_name.clone(), fixed);
num_instances.insert(circuit_name.clone(), num_rows);
}
Expand Down
9 changes: 3 additions & 6 deletions ceno_zkvm/src/scheme/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,9 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
);
exit_span!(tower_span);

// same point sumcheck is optional when all witin + fixed are in same num_vars
let is_skip_same_point_sumcheck = witnesses
.iter()
.chain(fixed.iter())
.map(|v| v.num_vars())
.all_equal();
// In table proof, we always skip same point sumcheck for now
// as tower sumcheck batch product argument/logup in same length
let is_skip_same_point_sumcheck = true;

let (input_open_point, same_r_sumcheck_proofs, rw_in_evals, lk_in_evals) =
if is_skip_same_point_sumcheck {
Expand Down
Loading

0 comments on commit 4ea6a4c

Please sign in to comment.