Skip to content
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

feat: is_prime example #240

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
463 changes: 463 additions & 0 deletions examples/is-prime/program/Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions examples/is-prime/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
[package]
version = "0.1.0"
name = "is-prime-program"
edition = "2021"

[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git" }
Binary file not shown.
32 changes: 32 additions & 0 deletions examples/is-prime/program/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

pub fn main() {
let n = sp1_zkvm::io::read::<u64>();

let is_prime = is_prime(n);

sp1_zkvm::io::write(&is_prime);
}

// Returns if divisible via immediate checks than 6k ± 1.
// Source: https://en.wikipedia.org/wiki/Primality_test#Rust
fn is_prime(n: u64) -> bool {
if n <= 1 {
return false;
}
if n <= 3 {
return true;
}
if n % 2 == 0 || n % 3 == 0 {
return false;
}
let mut i = 5;
while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false;
}
i += 6;
}
true
}
Loading
Loading