Skip to content

Commit 652cc9f

Browse files
authored
refactor(patch-testing): sha2, rsa versions (#2240)
1 parent 3b4d4af commit 652cc9f

File tree

7 files changed

+138
-86
lines changed

7 files changed

+138
-86
lines changed

patch-testing/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

patch-testing/RustCrypto-rsa/program/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ num-bigint = "0.4.0"
1414
rsa = { version = "=0.9.6", features = ["serde", "sha2"] }
1515

1616
[patch.crates-io]
17-
rsa = { git = "https://github.com/sp1-patches/RustCrypto-RSA/", tag = "patch-0.9.6-sp1-4.0.0" }
17+
rsa = { git = "https://github.com/sp1-patches/RustCrypto-RSA/", tag = "patch-0.9.6-sp1-4.0.0-v2" }
1818
sp1-lib = { path = "../../../crates/zkvm/lib" }

patch-testing/sha/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ sp1-zkvm.workspace = true
99
sp1-sdk.workspace = true
1010
sp1-core-executor.workspace = true
1111

12-
sha2-v0-9-8 = { version = "0.9.8", package = "sha2" }
13-
sha2-v0-10-6 = { version = "0.10.6", package = "sha2" }
12+
sha2 = { version = "0.9.9", package = "sha2" }
1413
rand = "0.8.5"
1514
sp1-test.workspace = true
1615
sha3 = "=0.10.6"

patch-testing/sha/build.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
use sp1_build::BuildArgs;
2+
13
fn main() {
2-
sp1_build::build_program("./program");
4+
sp1_build::build_program_with_args(
5+
"./program",
6+
BuildArgs { features: vec!["v0-9-9".to_string()], ..Default::default() },
7+
);
8+
sp1_build::build_program_with_args(
9+
"./program",
10+
BuildArgs { features: vec!["v0-10-6".to_string()], ..Default::default() },
11+
);
12+
sp1_build::build_program_with_args(
13+
"./program",
14+
BuildArgs { features: vec!["v0-10-8".to_string()], ..Default::default() },
15+
);
316
}

patch-testing/sha/program/Cargo.toml

+21-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@ version = "0.1.0"
55
edition = "2021"
66

77
[[bin]]
8-
name = "sha2"
8+
name = "sha2_v0_9_9"
99
path = "bin/sha2.rs"
10+
required-features = ["v0-9-9"]
11+
12+
[[bin]]
13+
name = "sha2_v0_10_6"
14+
path = "bin/sha2.rs"
15+
required-features = ["v0-10-6"]
16+
17+
[[bin]]
18+
name = "sha2_v0_10_8"
19+
path = "bin/sha2.rs"
20+
required-features = ["v0-10-8"]
1021

1122
[[bin]]
1223
name = "sha3"
@@ -16,15 +27,20 @@ path = "bin/sha3.rs"
1627
sp1-zkvm = { path = "../../../crates/zkvm/entrypoint" }
1728
serde = { version = "1.0.215", features = ["derive"] }
1829

19-
# note: 9.8 was yanked
20-
sha2-v0-9-8 = { version = "0.9.9", package = "sha2" }
21-
sha2-v0-10-8 = { version = "0.10.8", package = "sha2" }
30+
sha2-v0-9-9 = { version = "0.9.9", package = "sha2", optional = true }
31+
sha2-v0-10-6 = { version = "0.10.6", package = "sha2", optional = true }
32+
sha2-v0-10-8 = { version = "0.10.8", package = "sha2", optional = true }
2233

2334
sha3 = { version = "0.10.8", package = "sha3" }
2435

36+
[features]
37+
v0-9-9 = ["dep:sha2-v0-9-9"]
38+
v0-10-6 = ["dep:sha2-v0-10-6"]
39+
v0-10-8 = ["dep:sha2-v0-10-8"]
40+
2541
[patch.crates-io]
2642
sha3 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha3", tag = "patch-sha3-0.10.8-sp1-4.0.0" }
2743

2844
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", tag = "patch-sha2-0.10.8-sp1-4.0.0" }
29-
# todo: 0.9.8 is yanked?
45+
sha2-v0-10-6 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", tag = "patch-sha2-0.10.6-sp1-4.0.0" }
3046
sha2-v0-9-9 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", tag = "sha2-v0.9.9-patch-v1" }

patch-testing/sha/program/bin/sha2.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#![no_main]
22
sp1_zkvm::entrypoint!(main);
33

4-
use sha2_v0_9_8::{Digest as D1, Sha256 as Sha256_9_8};
5-
use sha2_v0_10_8::{Digest as D2, Sha256 as Sha256_10_8};
4+
#[cfg(feature = "v0-9-9")]
5+
extern crate sha2_v0_9_9 as sha2;
6+
7+
#[cfg(feature = "v0-10-6")]
8+
extern crate sha2_v0_10_6 as sha2;
9+
10+
#[cfg(feature = "v0-10-8")]
11+
extern crate sha2_v0_10_8 as sha2;
12+
13+
use sha2::{Digest, Sha256};
614

715
/// Emits SHA_COMPRESS and SHA_EXTEND syscalls.
816
pub fn main() {
@@ -11,15 +19,11 @@ pub fn main() {
1119
for _ in 0..times {
1220
let preimage = sp1_zkvm::io::read_vec();
1321

14-
let mut sha256_9_8 = Sha256_9_8::new();
15-
sha256_9_8.update(&preimage);
16-
17-
let mut sha256_10_6 = Sha256_10_8::new();
18-
sha256_10_6.update(&preimage);
22+
let mut sha256 = Sha256::new();
23+
sha256.update(&preimage);
1924

20-
let output_9_8: [u8; 32] = sha256_9_8.finalize().into();
21-
let output_10_6: [u8; 32] = sha256_10_6.finalize().into();
25+
let output: [u8; 32] = sha256.finalize().into();
2226

23-
sp1_zkvm::io::commit(&(output_9_8, output_10_6));
27+
sp1_zkvm::io::commit(&output);
2428
}
2529
}

patch-testing/sha/src/lib.rs

+87-66
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,104 @@
11
#[cfg(test)]
2-
use sp1_sdk::SP1PublicValues;
3-
use sp1_test::sp1_test;
2+
mod tests {
3+
use sha2::Digest;
4+
use sp1_sdk::SP1PublicValues;
5+
use sp1_test::sp1_test;
6+
7+
#[sp1_test("sha2_v0_9_9", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
8+
fn test_sha2_v0_9_9_expected_digest_lte_100_times(
9+
stdin: &mut sp1_sdk::SP1Stdin,
10+
) -> impl FnOnce(SP1PublicValues) {
11+
sha2_expected_digest_lte_100_times(stdin)
12+
}
413

5-
#[sp1_test("sha2", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
6-
fn test_sha2_expected_digest_lte_100_times(
7-
stdin: &mut sp1_sdk::SP1Stdin,
8-
) -> impl FnOnce(SP1PublicValues) {
9-
use sha2_v0_10_6::Digest as D2;
10-
use sha2_v0_9_8::Digest as D1;
14+
#[sp1_test("sha2_v0_10_6", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
15+
fn test_sha2_v0_10_6_expected_digest_lte_100_times(
16+
stdin: &mut sp1_sdk::SP1Stdin,
17+
) -> impl FnOnce(SP1PublicValues) {
18+
sha2_expected_digest_lte_100_times(stdin)
19+
}
1120

12-
use sp1_test::DEFAULT_CORPUS_COUNT;
13-
use sp1_test::DEFAULT_CORPUS_MAX_LEN;
21+
#[sp1_test("sha2_v0_10_8", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
22+
fn test_sha2_v0_10_8_expected_digest_lte_100_times(
23+
stdin: &mut sp1_sdk::SP1Stdin,
24+
) -> impl FnOnce(SP1PublicValues) {
25+
sha2_expected_digest_lte_100_times(stdin)
26+
}
1427

15-
let mut preimages =
16-
sp1_test::random_preimages_with_bounded_len(DEFAULT_CORPUS_COUNT, DEFAULT_CORPUS_MAX_LEN);
28+
fn sha2_expected_digest_lte_100_times(
29+
stdin: &mut sp1_sdk::SP1Stdin,
30+
) -> impl FnOnce(SP1PublicValues) {
31+
use sp1_test::DEFAULT_CORPUS_COUNT;
32+
use sp1_test::DEFAULT_CORPUS_MAX_LEN;
1733

18-
sp1_test::add_hash_fn_edge_cases(&mut preimages);
34+
let mut preimages = sp1_test::random_preimages_with_bounded_len(
35+
DEFAULT_CORPUS_COUNT,
36+
DEFAULT_CORPUS_MAX_LEN,
37+
);
1938

20-
let digests = preimages
21-
.iter()
22-
.map(|preimage| {
23-
let mut sha256_9_8 = sha2_v0_9_8::Sha256::new();
24-
sha256_9_8.update(preimage);
39+
sp1_test::add_hash_fn_edge_cases(&mut preimages);
2540

26-
let mut sha256_10_6 = sha2_v0_10_6::Sha256::new();
27-
sha256_10_6.update(preimage);
41+
let digests = preimages
42+
.iter()
43+
.map(|preimage| {
44+
let mut sha256 = sha2::Sha256::new();
45+
sha256.update(preimage);
2846

29-
(sha256_9_8.finalize().into(), sha256_10_6.finalize().into())
30-
})
31-
.collect::<Vec<([u8; 32], [u8; 32])>>();
47+
sha256.finalize().into()
48+
})
49+
.collect::<Vec<[u8; 32]>>();
3250

33-
// Write the number of preimages to the SP1Stdin
34-
// This should be equal to the number of digests.
35-
stdin.write(&preimages.len());
36-
preimages.iter().for_each(|preimage| stdin.write_slice(preimage.as_slice()));
51+
// Write the number of preimages to the SP1Stdin
52+
// This should be equal to the number of digests.
53+
stdin.write(&preimages.len());
54+
preimages.iter().for_each(|preimage| stdin.write_slice(preimage.as_slice()));
3755

38-
move |mut public| {
39-
for digest in digests {
40-
let committed = public.read::<([u8; 32], [u8; 32])>();
56+
move |mut public| {
57+
for digest in digests {
58+
let committed = public.read::<[u8; 32]>();
4159

42-
assert_eq!(digest, committed);
60+
assert_eq!(digest, committed);
61+
}
4362
}
4463
}
45-
}
46-
47-
#[sp1_test("sha3", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
48-
fn test_sha3_expected_digest_lte_100_times(
49-
stdin: &mut sp1_sdk::SP1Stdin,
50-
) -> impl FnOnce(SP1PublicValues) {
51-
use sha3::Digest;
52-
use sha3::Sha3_256;
53-
54-
use sp1_test::DEFAULT_CORPUS_COUNT;
55-
use sp1_test::DEFAULT_CORPUS_MAX_LEN;
56-
57-
let mut preimages: Vec<Vec<u8>> =
58-
sp1_test::random_preimages_with_bounded_len(DEFAULT_CORPUS_COUNT, DEFAULT_CORPUS_MAX_LEN);
59-
60-
sp1_test::add_hash_fn_edge_cases(&mut preimages);
61-
62-
let digests = preimages
63-
.iter()
64-
.map(|preimage| {
65-
let mut sha3 = Sha3_256::new();
66-
sha3.update(preimage);
67-
68-
sha3.finalize().into()
69-
})
70-
.collect::<Vec<[u8; 32]>>();
71-
72-
// Write the number of preimages to the SP1Stdin
73-
// This should be equal to the number of digests.
74-
stdin.write(&preimages.len());
75-
preimages.iter().for_each(|preimage| stdin.write_slice(preimage.as_slice()));
7664

77-
move |mut public| {
78-
for digest in digests {
79-
let committed = public.read::<[u8; 32]>();
80-
assert_eq!(digest, committed);
65+
#[sp1_test("sha3", syscalls = [SHA_COMPRESS, SHA_EXTEND], gpu, prove)]
66+
fn test_sha3_expected_digest_lte_100_times(
67+
stdin: &mut sp1_sdk::SP1Stdin,
68+
) -> impl FnOnce(SP1PublicValues) {
69+
use sha3::Digest;
70+
use sha3::Sha3_256;
71+
72+
use sp1_test::DEFAULT_CORPUS_COUNT;
73+
use sp1_test::DEFAULT_CORPUS_MAX_LEN;
74+
75+
let mut preimages: Vec<Vec<u8>> = sp1_test::random_preimages_with_bounded_len(
76+
DEFAULT_CORPUS_COUNT,
77+
DEFAULT_CORPUS_MAX_LEN,
78+
);
79+
80+
sp1_test::add_hash_fn_edge_cases(&mut preimages);
81+
82+
let digests = preimages
83+
.iter()
84+
.map(|preimage| {
85+
let mut sha3 = Sha3_256::new();
86+
sha3.update(preimage);
87+
88+
sha3.finalize().into()
89+
})
90+
.collect::<Vec<[u8; 32]>>();
91+
92+
// Write the number of preimages to the SP1Stdin
93+
// This should be equal to the number of digests.
94+
stdin.write(&preimages.len());
95+
preimages.iter().for_each(|preimage| stdin.write_slice(preimage.as_slice()));
96+
97+
move |mut public| {
98+
for digest in digests {
99+
let committed = public.read::<[u8; 32]>();
100+
assert_eq!(digest, committed);
101+
}
81102
}
82103
}
83104
}

0 commit comments

Comments
 (0)