poseison update powdr

This commit is contained in:
MuhtasimTanmoy
2024-11-21 20:37:58 +06:00
committed by tanmoy
parent d31e364b41
commit 6174f90e80
10 changed files with 4513 additions and 0 deletions

2
.idea/benchmark.iml generated
View File

@@ -35,6 +35,7 @@
<sourceFolder url="file://$MODULE_DIR$/sp1/algos/sha256/lib/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/sp1/algos/sha256/program/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/sp1/algos/sha256/script/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/powdr/poseidon/guest/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/halo2/target" />
<excludeFolder url="file://$MODULE_DIR$/jolt/algos/addition/target" />
<excludeFolder url="file://$MODULE_DIR$/jolt/algos/division/target" />
@@ -50,6 +51,7 @@
<excludeFolder url="file://$MODULE_DIR$/sp1/algos/nth_prime/native_sp1/program/target" />
<excludeFolder url="file://$MODULE_DIR$/sp1/algos/nth_prime/native_sp1/script/target" />
<excludeFolder url="file://$MODULE_DIR$/sp1/algos/sha256/target" />
<excludeFolder url="file://$MODULE_DIR$/powdr/poseidon/guest/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

21
powdr/poseidon/.gitignore vendored Normal file
View File

@@ -0,0 +1,21 @@
# Cargo build
**/target
powdr-target
# Cargo config
.cargo
# Profile-guided optimization
/tmp
pgo-data.profdata
# MacOS nuisances
.DS_Store
# Proofs
**/proof-with-pis.json
**/proof-with-io.json
# Env
.env

3920
powdr/poseidon/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
powdr/poseidon/Cargo.toml Normal file
View File

@@ -0,0 +1,25 @@
[package]
name = "poseidon"
version = "0.1.0"
edition = "2021"
[features]
default = []
simd = ["powdr/plonky3-simd"]
[dependencies]
powdr = { git = "https://github.com/powdr-labs/powdr", tag = "v0.1.2", features = ["plonky3"] }
serde = { version = "1.0", default-features = false, features = [
"alloc",
"derive",
"rc",
] }
serde_cbor = { version = "0.11.2", default-features = false, features = [
"alloc",
] }
env_logger = "0.10.2"
log = "0.4.17"
[workspace]

114
powdr/poseidon/README.md Normal file
View File

@@ -0,0 +1,114 @@
# powdrVM Usage Template
This is a foundational template for generating zero-knowledge proofs with powdrVM.
You write the code to be proven as a guest program for the zkVM host.
This template includes a structure for host/guest interaction, ZKP setup,
and artifact generation.
Guest programs are written in Rust.
When creating your guest program, you can write Rust code in the usual way,
including using std and importing packages others have written.
We provide some additional powdrVM specific functionalities via system calls,
such as IO operations for host <-> guest communication and precompiles to
accelerate complex programs via optimized circuits.
## Dependencies
- Rust/cargo
## Usage
This will run the host and generate ZK proofs.
```bash
cargo run -r
```
## AVX / Neon
You can enable AVX or Neon support by using the `simd` feature and running
the host with extra flags:
```bash
RUSTFLAGS='-C target-cpu=native' cargo run --features simd -r
```
## Structure
- `src/main.rs`: the host code. This is where you create a powdr `Session`,
prepare data to be shared with the guest, and run the prover.
- `guest`: this is the guest crate. It contains the code that will be
run inside the powdrVM.
- `powdr-target`: this is where all generated artifacts reside.
This includes the compiled guest code to powdr-asm, the compiled PIL constraints,
setup artifacts such as proving and verifying keys, and the final ZK proofs.
## Workflow
Let's look at `src/main.rs` line by line:
Here we create some data we want to share with the guest:
```rust
let some_data = vec![1, 2, 3, 4, 5];
```
Create a new powdr session where we'll be running crate `guest` in powdrVM
and all artifacts will be stored in `powdr-target`:
```rust
let mut session = Session::builder()
.guest_path("./guest")
.out_path("powdr-target")
.build()
```
Write `some_data` to channel 1 and the sum of `some_data` to channel 2.
Note that any `serde` type can be used to share data between host and guest.
The guest will read this data from the channels:
```rust
.write(1, &some_data)
.write(2, &some_data.iter().sum::<u32>());
```
The lines below also create a powdr `Session`, but tell powdrVM to use 2^18 rows
per chunk, instead of the default 2^20. This is useful to decrease memory usage,
for example, at the expense of proving time.
```rust
let mut session = Session::builder()
.guest_path("./guest")
.out_path("powdr-target")
.chunk_size_log2(18)
```
Run the session without generating a proof. Useful for testing the guest code:
```rust
session.run();
```
Generate the ZK proof:
```rust
session.prove();
```
Before generating a proof, powdrVM has to create the proving and verifying keys (setup)
for the given guest program.
When run for the first time, this can take a while.
Subsequent runs will be faster as the setup only changes if the guest changes.
powdrVM also needs to compute the witnesses for the given execution trace,
needed by the ZK prover.
Currently this is done by an automated constraint solver,
which can be slow for complex programs.
We are working on a more efficient way to generate witnesses.
You can run the host with INFO logs to have a deeper look at what's happening:
```bash
RUST_LOG=info cargo run -r
```

342
powdr/poseidon/guest/Cargo.lock generated Normal file
View File

@@ -0,0 +1,342 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6"
dependencies = [
"libc",
]
[[package]]
name = "crypto-bigint"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76"
dependencies = [
"generic-array",
"subtle",
"zeroize",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "half"
version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "keccak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654"
dependencies = [
"cpufeatures",
]
[[package]]
name = "lambdaworks-crypto"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbc2a4da0d9e52ccfe6306801a112e81a8fc0c76aa3e4449fefeda7fef72bb34"
dependencies = [
"lambdaworks-math",
"sha2",
"sha3",
]
[[package]]
name = "lambdaworks-math"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1bd2632acbd9957afc5aeec07ad39f078ae38656654043bf16e046fa2730e23"
[[package]]
name = "libc"
version = "0.2.164"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f"
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "powdr-guest"
version = "0.1.0"
dependencies = [
"powdr-riscv-runtime",
"powdr-riscv-syscalls",
"starknet-crypto",
"starknet-types-core",
]
[[package]]
name = "powdr-riscv-runtime"
version = "0.1.2"
source = "git+https://github.com/powdr-labs/powdr?tag=v0.1.2#446fbfce98c340a308778b474330e7e2dcb929c8"
dependencies = [
"powdr-riscv-syscalls",
"serde",
"serde_cbor",
]
[[package]]
name = "powdr-riscv-syscalls"
version = "0.1.2"
source = "git+https://github.com/powdr-labs/powdr?tag=v0.1.2#446fbfce98c340a308778b474330e7e2dcb929c8"
[[package]]
name = "proc-macro2"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rfc6979"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2"
dependencies = [
"hmac",
"subtle",
]
[[package]]
name = "serde"
version = "1.0.215"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_cbor"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.215"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha2"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sha3"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest",
"keccak",
]
[[package]]
name = "starknet-crypto"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded22ccf4cb9e572ce3f77de6066af53560cd2520d508876c83bb1e6b29d5cbc"
dependencies = [
"crypto-bigint",
"hex",
"hmac",
"num-bigint",
"num-integer",
"num-traits",
"rfc6979",
"sha2",
"starknet-curve",
"starknet-types-core",
"zeroize",
]
[[package]]
name = "starknet-curve"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcde6bd74269b8161948190ace6cf069ef20ac6e79cd2ba09b320efa7500b6de"
dependencies = [
"starknet-types-core",
]
[[package]]
name = "starknet-types-core"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa1b9e01ccb217ab6d475c5cda05dbb22c30029f7bb52b192a010a00d77a3d74"
dependencies = [
"lambdaworks-crypto",
"lambdaworks-math",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "2.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "zeroize"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"

View File

@@ -0,0 +1,12 @@
[package]
name = "powdr-guest"
version = "0.1.0"
edition = "2021"
[dependencies]
powdr-riscv-syscalls = { git = "https://github.com/powdr-labs/powdr", tag = "v0.1.2" }
powdr-riscv-runtime = { git = "https://github.com/powdr-labs/powdr", tag = "v0.1.2", features = ["std"]}
starknet-crypto = { version = "0.7.2", default-features = false, features = ["alloc"] }
starknet-types-core = { version = "0.1.6", default-features = false, features = ["alloc"] }
[workspace]

View File

@@ -0,0 +1,44 @@
use powdr_riscv_runtime;
use powdr_riscv_runtime::io::read;
use starknet_crypto::{PoseidonHasher};
use starknet_types_core::felt::Felt;
/// Converts an arbitrary byte slice into `Felt` elements.
fn bytes_to_felts(input: &[u8]) -> Vec<Felt> {
const FELT_BYTE_SIZE: usize = 31; // Maximum bytes for a Felt element in BN254
input
.chunks(FELT_BYTE_SIZE)
.map(|chunk| {
let mut buffer = [0u8; 32]; // BN254 requires 32 bytes, pad with zeroes
buffer[32 - chunk.len()..].copy_from_slice(chunk); // Right-align the chunk
Felt::from_bytes_be(&buffer)
})
.collect()
}
/// Ref: https://github.com/xJonathanLEI/starknet-rs/blob/master/starknet-crypto/benches/poseidon_hash.rs
#[no_mangle]
pub fn poseidon() {
let input = &[5u8; 1000];
// Convert input into `Felt` chunks
let felt_chunks = bytes_to_felts(input);
let mut hasher = PoseidonHasher::new();
for chunk in &felt_chunks {
hasher.update(*chunk);
}
let hash = hasher.finalize();
}
fn main() {
// Any serde-deserializable type can be read from a channel.
// Read some data from channel 1.
let data: Vec<u32> = read(1);
// Read the claimed sum from channel 2.
let sum: u32 = read(2);
poseidon();
// Check that the claimed sum is correct.
// assert_eq!(data.iter().sum::<u32>(), sum);
}

View File

@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly-2024-09-21"

View File

@@ -0,0 +1,31 @@
use powdr::Session;
fn main() {
env_logger::init();
let some_data = vec![1, 2, 3, 4, 5];
// Create a new powdr session to make proofs for the `guest` crate.
// Store all temporary and final artifacts in `powdr-target`.
let mut session = Session::builder()
.guest_path("./guest")
.out_path("powdr-target")
// powdrVM splits long execution traces into chunks
// which are proven individually.
// The default size of a chunk is 2^20 = 1048576 rows.
// For experiments and smaller traces/proofs, it may be beneficial to reduce the chunk size.
// Create a new powdr session with a custom chunk size.
// 2^18 = 262144 rows per chunk.
.chunk_size_log2(18)
.build()
// Write `some_data` to channel 1 and the sum of `some_data` to channel 2.
// Any serde-serializable type can be written to a channel.
.write(1, &some_data)
.write(2, &some_data.iter().sum::<u32>());
// Fast dry run to test execution.
session.run();
// Uncomment to compute the proof.
session.prove();
}