File tree Expand file tree Collapse file tree 5 files changed +83
-0
lines changed Expand file tree Collapse file tree 5 files changed +83
-0
lines changed Original file line number Diff line number Diff line change
1
+ build
2
+ target
3
+ Cargo.lock
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " sha_hasher"
3
+ version = " 0.1.0"
4
+ edition = " 2021"
5
+ default-run = " sha_hasher"
6
+
7
+ [dependencies ]
8
+ byteorder = " 1.5.0"
9
+ sha2 = " 0.10.8"
10
+ ziskos = { git = " https://github.com/0xPolygonHermez/zisk.git" }
11
+
12
+ # reth-ethereum-primitives = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" }
13
+ # reth-primitives-traits = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" }
14
+ # reth-stateless = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" }
15
+
Original file line number Diff line number Diff line change
1
+ use std:: fs:: { self , File } ;
2
+ use std:: io:: { self , Write } ;
3
+ use std:: path:: Path ;
4
+
5
+ // Define constants for the directory and input file name
6
+ const OUTPUT_DIR : & str = "build/" ;
7
+ const FILE_NAME : & str = "input.bin" ;
8
+
9
+ fn main ( ) -> io:: Result < ( ) > {
10
+ let n: u64 = 20 ;
11
+
12
+ // Ensure the output directory exists
13
+ let output_dir = Path :: new ( OUTPUT_DIR ) ;
14
+ if !output_dir. exists ( ) {
15
+ // Create the directory and any necessary parent directories
16
+ fs:: create_dir_all ( output_dir) ?;
17
+ }
18
+
19
+ // Create the file and write the 'n' value in little-endian format
20
+ let file_path = output_dir. join ( FILE_NAME ) ;
21
+ let mut file = File :: create ( & file_path) ?;
22
+ file. write_all ( & n. to_le_bytes ( ) ) ?;
23
+
24
+ Ok ( ( ) )
25
+ }
Original file line number Diff line number Diff line change
1
+ // This example program takes a number `n` as input and computes the SHA-256 hash `n` times sequentially.
2
+
3
+ // Mark the main function as the entry point for ZisK
4
+ #![ no_main]
5
+ ziskos:: entrypoint!( main) ;
6
+
7
+ use byteorder:: ByteOrder ;
8
+ use sha2:: { Digest , Sha256 } ;
9
+ use std:: convert:: TryInto ;
10
+ use ziskos:: { read_input, set_output} ;
11
+
12
+ fn main ( ) {
13
+ // Read the input data as a byte array from ziskos
14
+ let input: Vec < u8 > = read_input ( ) ;
15
+
16
+ // Convert the input data to a u64 integer
17
+ let n: u64 = match input. try_into ( ) {
18
+ Ok ( input_bytes) => u64:: from_le_bytes ( input_bytes) ,
19
+ Err ( input) => panic ! (
20
+ "Invalid input length. Expected 8 bytes, got {}" ,
21
+ input. len( )
22
+ ) ,
23
+ } ;
24
+
25
+ let mut hash = [ 0u8 ; 32 ] ;
26
+
27
+ // Compute SHA-256 hashing 'n' times
28
+ for _ in 0 ..n {
29
+ let mut hasher = Sha256 :: new ( ) ;
30
+ hasher. update ( hash) ;
31
+ let digest = & hasher. finalize ( ) ;
32
+ hash = Into :: < [ u8 ; 32 ] > :: into ( * digest) ;
33
+ }
34
+
35
+ // Split 'hash' value into chunks of 32 bits and write them to ziskos output
36
+ for i in 0 ..8 {
37
+ let val = byteorder:: BigEndian :: read_u32 ( & mut hash[ i * 4 ..i * 4 + 4 ] ) ;
38
+ set_output ( i, val) ;
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments