Skip to content

Commit

Permalink
feat: modularize toolchain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arayikhalatyan committed Dec 16, 2024
1 parent 31c5b18 commit cdb3ea8
Show file tree
Hide file tree
Showing 53 changed files with 1,267 additions and 595 deletions.
134 changes: 134 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ members = [
"extensions/algebra/transpiler",
"extensions/algebra/guest",
"extensions/algebra/moduli-setup",
"extensions/algebra/tests",
"extensions/bigint/circuit",
"extensions/bigint/transpiler",
"extensions/bigint/guest",
"extensions/bigint/tests",
"extensions/keccak256/circuit",
"extensions/keccak256/transpiler",
"extensions/keccak256/guest",
"extensions/keccak256/tests",
"extensions/native/circuit",
"extensions/native/compiler",
"extensions/native/compiler/derive",
Expand All @@ -46,10 +49,13 @@ members = [
"extensions/ecc/transpiler",
"extensions/ecc/guest",
"extensions/ecc/sw-setup",
"extensions/ecc/tests",
"extensions/pairing/circuit",
"extensions/pairing/transpiler",
"extensions/pairing/guest",
"extensions/pairing/tests",
"extensions/rv32-adapters",
"extensions/rv32im/tests",
]
exclude = ["crates/sdk/example"]
resolver = "2"
Expand Down
10 changes: 10 additions & 0 deletions crates/toolchain/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ const RUSTUP_TOOLCHAIN_NAME: &str = "nightly-2024-10-30";
/// Returns the given cargo Package from the metadata in the Cargo.toml manifest
/// within the provided `manifest_dir`.
pub fn get_package(manifest_dir: impl AsRef<Path>) -> Package {
println!(
"manifest_dir: {:?}",
manifest_dir.as_ref().join("Cargo.toml")
);
let manifest_path = fs::canonicalize(manifest_dir.as_ref().join("Cargo.toml")).unwrap();
println!(
"manifest_path: {:?}",
MetadataCommand::new()
.manifest_path(&manifest_path)
.no_deps()
);
let manifest_meta = MetadataCommand::new()
.manifest_path(&manifest_path)
.no_deps()
Expand Down
10 changes: 0 additions & 10 deletions crates/toolchain/tests/programs/.cargo/config.toml

This file was deleted.

6 changes: 0 additions & 6 deletions crates/toolchain/tests/programs/examples/empty.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ To disassemble the ELF to read the instructions, [install cargo-binutils](https:
rust-objdump -d target/riscv32im-risc0-zkvm-elf/debug/examples/openvm-fibonacci-program
```

where `-d` is short for `--disassemble`.
where `-d` is short for `--disassemble`.
60 changes: 60 additions & 0 deletions crates/toolchain/tests/src/Toolchain-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# How to write toolchain tests for an extension

Make a `test` crate inside the extension folder. As an example, here is the structure of the `rv32im-extension-test` crate:

```
extensions/rv32im/tests/
├── Cargo.toml
├── src
│ └── lib.rs
├── programs
│ └── Cargo.toml
│ └── examples
│ └── example1.rs
│ └── example2.rs
```

The `examples` folder contains the test programs in `rust`.

`fibonacci.rs` example:
```rust
#![cfg_attr(not(feature = "std"), no_main)]
#![cfg_attr(not(feature = "std"), no_std)]

openvm::entry!(main);

pub fn main() {
let n = core::hint::black_box(1 << 10);
let mut a: u32 = 0;
let mut b: u32 = 1;
for _ in 1..n {
let sum = a + b;
a = b;
b = sum;
}
if a == 0 {
panic!();
}
}
```


And then to `transpile`, `run`, and `prove` the above program, in the `src/lib.rs` file, you can do:

```rust
fn test_fibonacci_prove() -> Result<()> {
let elf = build_example_program_at_path(get_programs_dir!(), "fibonacci")?;
let exe = VmExe::from_elf(
elf,
Transpiler::<F>::default()
.with_extension(Rv32ITranspilerExtension)
.with_extension(Rv32MTranspilerExtension)
.with_extension(Rv32IoTranspilerExtension),
)?;
let config = Rv32IConfig::default();
new_air_test_with_min_segments(config, exe, vec![], 1, true);
Ok(())
}
```

Note: If the crate with example is not in `./programs`, specify the path with `get_programs_dir!("path to the programs crate")`.
Loading

0 comments on commit cdb3ea8

Please sign in to comment.