Skip to content

Commit

Permalink
Fix naive raffle algorithm; add results for different hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
oyyblin committed Sep 23, 2024
1 parent 2de560f commit 5ccadb9
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 160 deletions.
56 changes: 14 additions & 42 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ command:
```sh
SP1_PROVER=network SP1_PRIVATE_KEY=... cargo run --release --bin evm
```

# TODO

- prover network cost?
- lightest hash algorithm?
- use case improvements?
10 changes: 8 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ path = "src/raffle.rs"
[dependencies]
alloy-sol-types = { workspace = true }
sp1-zkvm = { workspace = true }
sha2-v0-9-8 = { version = "0.9.8", package = "sha2" }
# sha2-v0-9-8 = { version = "0.9.8", package = "sha2" }
sha2-v0-10-8 = { version = "0.10.8", package = "sha2" }
# sha3-v0-10-8 = { version = "0.10.8", package = "sha3" }
# tiny-keccak = { version = "2.0", features = ["sha3"] }

[patch.crates-io]
sha2-v0-9-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.9.8" }
# sha2-v0-9-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.9.8" }
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-sha2-v0.10.8" }
# sha3-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha3", branch = "patch-sha3-v0.10.8" }
# tiny-keccak = { git = "https://github.com/sp1-patches/tiny-keccak", branch = "patch-v2.0.2" }
39 changes: 28 additions & 11 deletions lib/src/raffle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use sha2_v0_9_8::{Digest as Digest_9_8, Sha256 as Sha256_9_8};
// use sha2_v0_9_8::{Digest as Digest_sha2_9_8, Sha256 as Sha256_9_8};
use sha2_v0_10_8::{Digest as Digest_sha2_10_8, Sha256 as Sha256_10_8};
// use sha3_v0_10_8::{Digest as Digest_sha3_10_8, Keccak256 as Keccak256_10_8};
// use tiny_keccak::{Hasher, Keccak as Tiny_Keccak};

/// Naive raffle implementation
pub fn raffle_naive(num_participants: u32, num_winners: u32, random_seed: u64) -> Vec<u32> {
Expand All @@ -7,15 +10,12 @@ pub fn raffle_naive(num_participants: u32, num_winners: u32, random_seed: u64) -
let n = num_participants;
let m = num_winners;

for _ in 0..m {
while winners.len() < m as usize {
let new_winner = (seed % n as u64) as u32;
if winners.contains(&new_winner) {
seed = hash(seed, new_winner as u64);
continue;
} else {
if !winners.contains(&new_winner) {
winners.push(new_winner);
seed = hash(seed, new_winner as u64);
}
seed = hash_sha2(seed, new_winner as u64);
}

winners
Expand All @@ -27,8 +27,8 @@ pub fn raffle_fisher_yates(num_participants: u32, num_winners: u32, random_seed:
let mut seed = random_seed;

for i in (1..num_participants).rev() {
seed = hash(seed, i as u64);
let j = (seed % (i + 1) as u64) as usize;
seed = hash_sha2(seed, i as u64);
let j: usize = (seed % (i + 1) as u64) as usize;
participants.swap(i as usize, j);
}

Expand All @@ -38,10 +38,27 @@ pub fn raffle_fisher_yates(num_participants: u32, num_winners: u32, random_seed:
.collect()
}

fn hash(seed: u64, value: u64) -> u64 {
let mut sha256 = Sha256_9_8::new();
fn hash_sha2(seed: u64, value: u64) -> u64 {
let mut sha256: Sha256_10_8 = Sha256_10_8::new();
sha256.update(seed.to_le_bytes());
sha256.update(value.to_le_bytes());
let result = sha256.finalize();
u64::from_le_bytes(result[..8].try_into().unwrap())
}

// fn hash_sha3(seed: u64, value: u64) -> u64 {
// let mut keccak256: Keccak256_10_8 = Keccak256_10_8::new();
// keccak256.update(seed.to_le_bytes());
// keccak256.update(value.to_le_bytes());
// let result = keccak256.finalize();
// u64::from_le_bytes(result[..8].try_into().unwrap())
// }

// fn hash_tiny_keccak(seed: u64, value: u64) -> u64 {
// let mut keccak = Tiny_Keccak::v256();
// keccak.update(&seed.to_le_bytes());
// keccak.update(&value.to_le_bytes());
// let mut output = [0u8; 32];
// keccak.finalize(&mut output);
// u64::from_le_bytes(output[..8].try_into().unwrap())
// }
52 changes: 21 additions & 31 deletions programs/per-raffle-proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,24 @@ cargo run --release -- --execute --num-participants 1000 --num-winners 10

Data below assumes random seed of `12345`

| Participants | Winners | Cycle Count | Merkle Root | Groth16 Proof Time | Groth16 Prover Network URL |
| ------------ | ------- | ----------- | ------------------------------------------------------------------ | ------------------ | --------------------------------------------------------------------- |
| 100 | 10 | 51228 | 0xced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75 | 2m43s | https://explorer.succinct.xyz/proofrequest_01j8egy82degrts5fay3cn81wq |
| 1000 | 10 | 51228 | 0xbf449f1db6ee091b380b110ef54ff7112835222d91753c2ec8998971a2fd6c53 | | |
| 1000 | 100 | 389721 | 0xcf8d690ca84b2bae826470057e3b7585e5d3530f019e78c7fc6f262cf6869a73 | | |
| 10000 | 10 | 51228 | 0x06444ad88114b90ab4db3019d37c2955bbde6e15630d06e43074b346862cdac6 | | |
| 10000 | 100 | 395997 | 0x9971ec5531a3fad1e9c38c6a77a54d24bf6cafe950928608f8304e3a7fe0aa1c | | |
| 100000 | 10 | 52741 | 0x5d39e729f2250f4ef96561c7afbcd1bc0d03264bc634b5a9c52b07a6bddaf42d | | |
| 100000 | 100 | 420673 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 | | |
| 10000000 | 10 | 53418 | 0xb72907e34b589edee7cdb4236efe59f2367d527a2bbb7c06104100fe02f1c0fc | | |
| 10000000 | 100 | 420673 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 | 2m46s | https://explorer.succinct.xyz/proofrequest_01j8eh6905egrr635fymhr2ah7 |
| 10000000 | 1000 | 6290600 | 0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d | 3m6s | https://explorer.succinct.xyz/proofrequest_01j8ema4nvegtt76dqvk0s7brp |
| 10000000 | 10000 | 287693424 | 0xa72920a1add9f3cdf91682d2fe3fa2a09011ed4ea0975f09c621bf3204dff789 | 6m22s | https://explorer.succinct.xyz/proofrequest_01j8en6ngbegtvz4qvw4jvmvcy |

## Logs

100 participants, 10 winners(Groth16)

```
Verification Key: 0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4
Public Values: 0x0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000003039ced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75
Proof Bytes: 0x6a2906ac092b7e497bcb047ad6e008a2f2b78e77b9a2732f282e897645fa339e29cae7750fc6ed66350f1ed84402ed0ed2a3c7104b896c44dcb93e6897eee6ae42f5f5572ad3ada13877fc89e0275d292c2a176d594220db67e075f6f851a34f50d96dbb16e6a7488579d42bbe8b88badb8a1e2a0bc7194e2f241540bb238928b62195ae2196b9a19d4a4279a8581ebf7a4706dbae9f69b941eab1e62d463f503981d8252022f9d4eee756e6fc1530d2cc1b097d0c715e325ca6e06a9723ca607c6ecbca15aff32d40288ddbdaf08141f76b795584d0dabffa6d563a807da42c951452992ac8cc2676a41c1afcfec6d95f0ec12d378581bff41f802335f6a8e6911cd0a0
```

10000000 participants, 1000 winners Groth16)

```
Verification Key: 0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4
Public Values: 0x000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000003e8000000000000000000000000000000000000000000000000000000000000303962383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d
Proof Bytes: 0x6a2906ac2bdc11c34385d676b280cbaa646d584c2f65f88341b11405e12fb18a750db8aa0e2d0a73536b05d5a67fbac543df23ff862d1e61f8c4bb49acc2aa9ad5b4edee1ed8af0233d985f535aa9518f533d1a9ab06f1e1680fd12e567d495ea546c0fd0a67b50e220ec2c9576666b69084f1853c6ec0d64eda22e76a30a2cd111eb12e06dcc8dd1fc8cd8f533bb35092d0f59591c54e4ef100919b6e53bd824473b56f0f63cc18213c64404a224cf4b98f320e286ce024b9db9dc7dce3648fb04ced733056bcd3e70ed46e9417853fc41ff6e45be5a48d5ecb374e57eba8994b54926103846766c514836ac41b58d4dba5ae591e4ec8a6436c4e7c5ac536a5b80e998f
```
| Participants | Winners | Cycle Count (sha2-v0-9-8) | Merkle Root | Groth16 Proof Time | Groth16 Prover Network URL |
| ------------ | ------- | ------------------------- | ------------------------------------------------------------------ | -------------------- | --------------------------------------------------------------------- |
| 100 | 10 | 51216 | 0xced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75 | 2 minutes 56 seconds | https://explorer.succinct.xyz/proofrequest_01j8fbfz8ae2cahb2hv4p1dbdk |
| 1000 | 10 | 51216 | 0xbf449f1db6ee091b380b110ef54ff7112835222d91753c2ec8998971a2fd6c53 | | |
| 1000 | 100 | 399627 | 0x3c78346b865e4b4ae9ac1fdcbc27a00e2d0289626643e235ee0ffbbc943742d3 | | |
| 10000 | 10 | 51216 | 0x06444ad88114b90ab4db3019d37c2955bbde6e15630d06e43074b346862cdac6 | | |
| 10000 | 100 | 399627 | 0x3c78346b865e4b4ae9ac1fdcbc27a00e2d0289626643e235ee0ffbbc943742d3 | | |
| 100000 | 10 | 52741 | 0x5d39e729f2250f4ef96561c7afbcd1bc0d03264bc634b5a9c52b07a6bddaf42d | | |
| 100000 | 100 | 395895 | 0x9971ec5531a3fad1e9c38c6a77a54d24bf6cafe950928608f8304e3a7fe0aa1c | | |
| 10000000 | 10 | 53418 | 0xb72907e34b589edee7cdb4236efe59f2367d527a2bbb7c06104100fe02f1c0fc | | |
| 10000000 | 100 | 420571 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 | 2 minutes 46 seconds | https://explorer.succinct.xyz/proofrequest_01j8fbzpcjfezajrz9xhqfvccx |
| 10000000 | 1000 | 6289598 | 0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d | 3 minutes 13 seconds | https://explorer.succinct.xyz/proofrequest_01j8fc6645e6abb6k7q5266qa7 |
| 10000000 | 10000 | 288003808 | 0x647ee7b3d3e20c516d108670e48250d7fc1f76ebdc19fbf6ab86e91d25a63434 | 6 minutes 29 seconds | https://explorer.succinct.xyz/proofrequest_01j8fcd45ce6a930tvqngrqqst |

### Hash algorithm

10000000 participants, 1000 winners

| sha2-v0-9-8 (sha256) | sha2-v0-10-8 (sha256) | sha3-v0-10-8 (keccak256) | tiny-keccak (keccak256) |
| -------------------- | --------------------- | ------------------------ | ----------------------- |
| 6289598 | 9709598 | 22346206 | 22566445 |
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"numWinners": 1000,
"randomSeed": 12345,
"winnersMerkleRoot": "0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d",
"vkey": "0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4",
"vkey": "0x00c234cc871c2f30927c9413f45db38b26779fc92395cd767753d378b9f73155",
"publicValues": "0x000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000003e8000000000000000000000000000000000000000000000000000000000000303962383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d",
"proof": "0x6a2906ac2bdc11c34385d676b280cbaa646d584c2f65f88341b11405e12fb18a750db8aa0e2d0a73536b05d5a67fbac543df23ff862d1e61f8c4bb49acc2aa9ad5b4edee1ed8af0233d985f535aa9518f533d1a9ab06f1e1680fd12e567d495ea546c0fd0a67b50e220ec2c9576666b69084f1853c6ec0d64eda22e76a30a2cd111eb12e06dcc8dd1fc8cd8f533bb35092d0f59591c54e4ef100919b6e53bd824473b56f0f63cc18213c64404a224cf4b98f320e286ce024b9db9dc7dce3648fb04ced733056bcd3e70ed46e9417853fc41ff6e45be5a48d5ecb374e57eba8994b54926103846766c514836ac41b58d4dba5ae591e4ec8a6436c4e7c5ac536a5b80e998f"
"proof": "0x6a2906ac0829c608457736242f348577c4be5ca98fa85a43481fa4b691203b9e21eedf84234e37cbf9fa6773b1e507938964aa7d56a291a867dd35d7806c3a37f5f05e2c076c4beeaeab02ff9c2962867c1161101f64c1cd02c6b3d082d4a1e1c96aa7cb1ef9f960e9de8263afb3b8301ea90f831181ee0904f078792ed8ccb0a2629b2407acdb0d21b65b0893551d501adc3c86218996a82110a446277e5d8f504ffac22d0cbb8e78658add65782e72497b55df9cd6d382a175f1912d2d7cf8bf6e3ade0fbd0ddcc2dbfbca89815018c972975042359164e34325269ba1d2a044f85efd2bad94003a84208835d3935b9e6c91c518c90cdc84c3e1e80a3aa9101ead9b1e"
}
2 changes: 1 addition & 1 deletion programs/per-raffle-proof/program/Cargo.lock

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

Binary file modified programs/per-raffle-proof/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Loading

0 comments on commit 5ccadb9

Please sign in to comment.