-
Notifications
You must be signed in to change notification settings - Fork 274
Add a modexp example guest program + benchmark #1067 #1119
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
f900539
afcbd43
9648d65
918ddb4
ecd02b3
069d27c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [target.x86_64-pc-windows-msvc] | ||
| linker = "rust-lld.exe" | ||
| rustflags = ["-C", "link-arg=-fuse-ld=lld"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [package] | ||
| name = "modexp" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| jolt-sdk = { path = "../../jolt-sdk", features = ["host"] } | ||
| tracing-subscriber = "0.3" | ||
| tracing = "0.1" | ||
| guest = { package = "modexp-guest", path = "./guest" } | ||
|
|
||
| hex = "0.4.3" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [package] | ||
| name = "modexp-guest" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [features] | ||
| guest = [] | ||
|
|
||
| [dependencies] | ||
| jolt = { package = "jolt-sdk", path = "../../../jolt-sdk", features = [] } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #![cfg_attr(feature = "guest", no_std)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[jolt::provable(memory_size = 10240, max_trace_length = 65536)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn modexp_chain(base: u128, exp: u128, modulus: u128, num_iters: u32) -> u128 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // multiply modulo m without overflowing by using shift-add | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn mul_mod(mut a: u128, mut b: u128, m: u128) -> u128 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut res: u128 = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a %= m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while b > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (b & 1) == 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res = (res + a) % m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a = (a << 1) % m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b >>= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| res | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn mod_pow(mut base: u128, mut exp: u128, m: u128) -> u128 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut result: u128 = 1 % m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| base %= m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while exp > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (exp & 1) == 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = mul_mod(result, base, m); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| base = mul_mod(base, base, m); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exp >>= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
22
to
35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modulus validation missing: The Add validation: fn mod_pow(mut base: u128, mut exp: u128, m: u128) -> u128 {
if m == 0 {
panic!("modulus cannot be zero");
}
// rest of implementation
}
Suggested change
Spotted by Graphite Agent |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut out: u128 = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _ in 0..num_iters { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out = mod_pow(base, exp, modulus); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut out: u128 = 0; | |
| for _ in 0..num_iters { | |
| out = mod_pow(base, exp, modulus); | |
| } | |
| let mut out: u128 = base; | |
| for _ in 0..num_iters { | |
| out = mod_pow(out, exp, modulus); | |
| } |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| use std::time::Instant; | ||
| use tracing::info; | ||
|
|
||
| pub fn main() { | ||
| tracing_subscriber::fmt::init(); | ||
|
|
||
| let target_dir = "/tmp/jolt-guest-targets"; | ||
| let mut program = guest::compile_modexp_chain(target_dir); | ||
|
|
||
| let prover_preprocessing = guest::preprocess_prover_modexp_chain(&mut program); | ||
| let verifier_preprocessing = | ||
| guest::verifier_preprocessing_from_prover_modexp_chain(&prover_preprocessing); | ||
|
|
||
| let prove_modexp_chain = guest::build_prover_modexp_chain(program, prover_preprocessing); | ||
| let verify_modexp_chain = guest::build_verifier_modexp_chain(verifier_preprocessing); | ||
|
|
||
| let input_base = 123456789u128; | ||
| let input_exp = 65537u128; | ||
| let input_mod = 1000000007u128; | ||
| let iters = 100u32; | ||
| let native_output = guest::modexp_chain(input_base, input_exp, input_mod, iters); | ||
| let now = Instant::now(); | ||
| let (output, proof, program_io) = prove_modexp_chain(input_base, input_exp, input_mod, iters); | ||
| info!("Prover runtime: {} s", now.elapsed().as_secs_f64()); | ||
| let is_valid = verify_modexp_chain(input_base, input_exp, input_mod, iters, output, program_io.panic, proof); | ||
|
|
||
| assert_eq!(output, native_output, "output mismatch"); | ||
| info!("output: {}", hex::encode(output.to_be_bytes())); | ||
| info!("native_output: {}", hex::encode(native_output.to_be_bytes())); | ||
| info!("valid: {is_valid}"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [toolchain] | ||
| channel = "1.88" | ||
| targets = ["riscv32imac-unknown-none-elf", "riscv64imac-unknown-none-elf"] | ||
| channel = "nightly" | ||
| profile = "minimal" | ||
| components = ["cargo", "rustc", "clippy", "rustfmt"] | ||
| components = ["rust-src", "cargo", "rustc", "clippy", "rustfmt"] | ||
| targets = ["riscv32imac-unknown-none-elf", "riscv64imac-unknown-none-elf"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical overflow bug in
mul_mod: Whenmodulus > u128::MAX / 2 + 1, the operationsres + a(line 11) anda << 1(line 13) can overflow before the modulo is applied, causing either:Example: If
modulus = (u128::MAX / 2) + 2, thenresandacan each be up tomodulus - 1, makingres + a > u128::MAX.Fix by using checked arithmetic:
Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.