forked from nexus-xyz/nexus-zkvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RISC-V support for cycle and clock counters (nexus-xyz#243)
- Loading branch information
Showing
18 changed files
with
370 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ Cargo.lock | |
nexus-proof | ||
|
||
# macos | ||
.DS_Store | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Used in the CI as a small example that uses memory store | ||
#![cfg_attr(target_arch = "riscv32", no_std, no_main)] | ||
|
||
#[nexus_rt::profile] | ||
fn fib(n: u32) -> u32 { | ||
match n { | ||
0 => 0, | ||
1 => 1, | ||
_ => fib(n - 1) + fib(n - 2), | ||
} | ||
} | ||
|
||
#[nexus_rt::profile] | ||
fn fib2(n: u32) -> u32 { | ||
if n == 0 { | ||
return 0; | ||
} | ||
if n == 1 { | ||
return 1; | ||
} | ||
let mut a = 0; | ||
let mut b = 1; | ||
let mut result = 0; | ||
for _ in 2..=n { | ||
result = a + b; | ||
a = b; | ||
b = result; | ||
} | ||
result | ||
} | ||
|
||
#[nexus_rt::main] | ||
fn main() { | ||
let n = 3; | ||
assert_eq!(fib(n), 2); | ||
assert_eq!(fib2(n), 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use proc_macro2::TokenStream; | ||
use proc_macro_crate::{crate_name, FoundCrate}; | ||
use quote::{format_ident, quote}; | ||
use syn::{parse2, ItemFn}; | ||
|
||
fn get_nexus_rt_ident() -> proc_macro2::Ident { | ||
match crate_name("nexus_rt") { | ||
Ok(FoundCrate::Name(name)) => format_ident!("{}", name), | ||
_ => format_ident!("nexus_rt"), | ||
} | ||
} | ||
|
||
pub fn profile(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input: ItemFn = parse2(item).expect("Invalid code block"); | ||
|
||
let ItemFn { | ||
vis: visibility, sig: signature, block, .. | ||
} = input; | ||
|
||
let name: &syn::Ident = &signature.ident; | ||
let nexus_rt = get_nexus_rt_ident(); | ||
|
||
quote! { | ||
#visibility #signature { | ||
#[cfg(feature = "cycles")] | ||
#nexus_rt::cycle_count_ecall(concat!("^#", file!(), ":", stringify!(#name))); | ||
let result = (|| #block)(); | ||
#[cfg(feature = "cycles")] | ||
#nexus_rt::cycle_count_ecall(concat!("$#", file!(), ":", stringify!(#name))); | ||
result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.