|
1 | 1 | #[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 | + } |
4 | 13 |
|
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 | + } |
11 | 20 |
|
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 | + } |
14 | 27 |
|
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; |
17 | 33 |
|
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 | + ); |
19 | 38 |
|
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); |
25 | 40 |
|
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); |
28 | 46 |
|
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]>>(); |
32 | 50 |
|
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())); |
37 | 55 |
|
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]>(); |
41 | 59 |
|
42 |
| - assert_eq!(digest, committed); |
| 60 | + assert_eq!(digest, committed); |
| 61 | + } |
43 | 62 | }
|
44 | 63 | }
|
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())); |
76 | 64 |
|
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 | + } |
81 | 102 | }
|
82 | 103 | }
|
83 | 104 | }
|
0 commit comments