diff --git a/README.md b/README.md index f9471223..538c591f 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,12 @@ git clone https://github.com/zkMIPS/zkm-project-template.git ### 1. Build the guest program ELF -Please refer to [this](guest-program/README.md) guide +Please refer to [this](guest-program/README.md) guide. ### 2. Build the host program ```sh +cd zkm-project-template sdk/src/local/libsnark/compile.sh # compile snark library cargo build --release # build host programs ``` @@ -99,116 +100,193 @@ cargo build --release # build host programs If the program executes successfully, it will generate one binary files in `target/release`: `zkm-prove` > [!NOTE] -> The host program executes local proving when the environmental variable `ZKM_PROVER` is set to "local" and performs network proving when `ZKM_PROVER` is set to "network" +> You can run the guest program without generating a proof by setting the environmental variable `EXECUTE_ONLY` to "true".https://github.com/zkMIPS/zkm/issues/152 ### 3. Generate groth16 proof and verifier contract ```sh -cd host-program +cd zkm-project-template/host-program ``` > [!NOTE] -> You can run the guest program without generating a proof by setting the environmental variable `EXECUTE_ONLY` to "true".https://github.com/zkMIPS/zkm/issues/152 +> 1. The host program executes local proving when the environmental variable `ZKM_PROVER` is set to "local" and performs network proving when `ZKM_PROVER` is set to "network" + +> 2. There are two script programs available: run_local_proving.sh and run_network_proving.sh. These scripts facilitate the +generation of proofs on the local machine and over the proof network, respectively. + +> 3. There are three guest programs(sha2-rust, sha2-go, mem-alloc-vec), each capable of generating a SNARK proof on a machine +equipped with an AMD EPYC 7R13 processor and 250GB of memory. The following will use sha2-rust as an example to demonstrate local and network proofs. + +> [!WARNING] +> The environmental variable `SEG_SIZE` in the run-xxx_proving.sh affects the final proof generation. + +> The guest program's ELF with the input is split into segments according the SEG_SIZE, based on the cycle count. +> When generating proofs on the local machine, if the log shows "!!!*******seg_num: 1", please reduce SEG_SIZE or increase the input. If generating proofs through the proof network, SEG_SIZE must be within the range [65536, 262144]. -### Example : `sha2-go` +### Example : `sha2-rust` -This program takes struct Data as public input +This host program sends the private input pri_input = vec![5u8; 1024] and its hash (hash(pri_input)) to the guest program for verification of the hash value. #### Local Proving Make any edits to [`run-local-proving.sh`](host-program/run-local-proving.sh) and run the program: ```sh -./run-local-proving.sh sha2-go +./run-local-proving.sh sha2-rust ``` If successful, it will output a similar log: -##### **`sha2-go-local-proving.log`** - -``` -[2024-10-07T08:11:59Z INFO zkm_prove] new prover client. -[2024-10-07T08:11:59Z INFO zkm_prove] new prover client,ok. -[2024-10-07T08:11:59Z INFO zkm_sdk::local::prover] calling request_proof. -[2024-10-07T08:11:59Z INFO zkm_sdk::local::prover] calling wait_proof, proof_id=f8a8243f-8631-4f92-916c-754a438d8c57 -[2024-10-07T08:11:59Z INFO zkm_sdk::local::prover] waiting the proof result. -[2024-10-07T08:11:59Z INFO zkm_emulator::utils] Split done 347823 : 446125 -[2024-10-07T08:12:51Z INFO zkm_sdk::local::util] Process segment /tmp/f8a8243f-8631-4f92-916c-754a438d8c57/input/segments/0 -[2024-10-07T08:12:55Z INFO zkm_prover::cpu::bootstrap_kernel] Bootstrapping took 21159 cycles -[2024-10-07T08:12:55Z INFO zkm_prover::generation] CPU halted after 261370 cycles -[2024-10-07T08:12:55Z INFO zkm_prover::generation] CPU trace padded to 262144 cycles -[2024-10-07T08:12:55Z INFO zkm_prover::generation] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 131963, cpu_len: 262144, poseidon_len: 21158, poseidon_sponge_len: 21158, logic_len: 16739, memory_len: 1949524 } -[2024-10-07T08:13:42Z INFO plonky2::util::timing] 50.7883s to prove root first -[2024-10-07T08:13:42Z INFO zkm_sdk::local::util] Process segment /tmp/f8a8243f-8631-4f92-916c-754a438d8c57/input/segments/1 -[2024-10-07T08:13:46Z INFO zkm_prover::cpu::bootstrap_kernel] Bootstrapping took 21804 cycles -[2024-10-07T08:13:46Z INFO zkm_prover::generation] CPU halted after 129420 cycles -[2024-10-07T08:13:46Z INFO zkm_prover::generation] CPU trace padded to 131072 cycles -[2024-10-07T08:13:46Z INFO zkm_prover::generation] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 44577, cpu_len: 131072, poseidon_len: 21803, poseidon_sponge_len: 21803, logic_len: 5164, memory_len: 1362331 } -[2024-10-07T08:14:20Z INFO plonky2::util::timing] 37.3177s to prove root second -[2024-10-07T08:14:21Z INFO plonky2::util::timing] 1.3277s to prove aggression -[2024-10-07T08:14:23Z INFO zkm_sdk::local::util] proof size: 412755 -[2024-10-07T08:14:29Z INFO zkm_sdk::local::util] build finish -[2024-10-07T08:14:35Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote common circuit data to common_circuit_data.json -[2024-10-07T08:14:35Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote verifier data to verifier_only_circuit_data.json -[2024-10-07T08:14:35Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote proof to proof_with_public_inputs.json -[2024-10-07T08:14:35Z INFO plonky2::util::timing] 156.1002s to prove total time -08:14:39 INF compiling circuit -08:14:39 INF parsed circuit inputs nbPublic=1 nbSecret=11182 -08:15:11 INF building constraint builder nbConstraints=5815132 -Generating witness 2024-10-07 08:21:33.36586361 +0000 UTC m=+573.868286665 -frontend.NewWitness cost time: 171 ms -Creating proof 2024-10-07 08:21:33.536971193 +0000 UTC m=+574.039394228 -08:21:39 DBG constraint system solver done nbConstraints=5815132 took=5950.579212 -08:21:47 DBG prover done acceleration=none backend=groth16 curve=bn254 nbConstraints=5815132 took=7963.861692 -groth16.Prove cost time: 13914 ms -Verifying proof 2024-10-07 08:21:47.451908504 +0000 UTC m=+587.954331579 -08:21:47 DBG verifier done backend=groth16 curve=bn254 took=1.707085 +##### **`sha2-rust-local-proving.log`** + +``` +[2024-10-09T03:31:00Z INFO zkm_prove] new prover client. +[2024-10-09T03:31:00Z INFO zkm_prove] new prover client,ok. +[2024-10-09T03:31:00Z INFO zkm_sdk::local::prover] calling request_proof. +[2024-10-09T03:31:00Z INFO zkm_sdk::local::prover] calling wait_proof, proof_id=5c14330a-cc5a-4ad8-9ba4-3996ddafbc57 +[2024-10-09T03:31:00Z INFO zkm_sdk::local::prover] waiting the proof result. +[2024-10-09T03:31:00Z INFO zkm_emulator::utils] Split done 66446 : 89443 +[2024-10-09T03:31:00Z INFO zkm_sdk::local::stark] !!!*******seg_num:2 +[2024-10-09T03:32:01Z INFO zkm_sdk::local::util] Process segment /tmp/5c14330a-cc5a-4ad8-9ba4-3996ddafbc57/input/segments/0 +[2024-10-09T03:32:05Z INFO zkm_prover::cpu::bootstrap_kernel] Bootstrapping took 3228 cycles +[2024-10-09T03:32:05Z INFO zkm_prover::generation] CPU halted after 64762 cycles +[2024-10-09T03:32:05Z INFO zkm_prover::generation] CPU trace padded to 65536 cycles +[2024-10-09T03:32:05Z INFO zkm_prover::generation] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 18057, cpu_len: 65536, poseidon_len: 3227, poseidon_sponge_len: 3227, logic_len: 15372, memory_len: 390244 } +[2024-10-09T03:32:22Z INFO plonky2::util::timing] 20.9778s to prove root first +[2024-10-09T03:32:22Z INFO zkm_sdk::local::util] Process segment /tmp/5c14330a-cc5a-4ad8-9ba4-3996ddafbc57/input/segments/1 +[2024-10-09T03:32:26Z INFO zkm_prover::cpu::bootstrap_kernel] Bootstrapping took 2583 cycles +[2024-10-09T03:32:26Z INFO zkm_prover::generation] CPU halted after 7530 cycles +[2024-10-09T03:32:26Z INFO zkm_prover::generation] CPU trace padded to 8192 cycles +[2024-10-09T03:32:26Z INFO zkm_prover::generation] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 1511, cpu_len: 8192, poseidon_len: 2582, poseidon_sponge_len: 2582, logic_len: 1157, memory_len: 123358 } +[2024-10-09T03:32:38Z INFO plonky2::util::timing] 15.7468s to prove root second +[2024-10-09T03:32:40Z INFO plonky2::util::timing] 1.4158s to prove aggression +[2024-10-09T03:32:42Z INFO zkm_sdk::local::util] proof size: 412960 +[2024-10-09T03:32:48Z INFO zkm_sdk::local::util] build finish +[2024-10-09T03:32:55Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote common circuit data to common_circuit_data.json +[2024-10-09T03:32:55Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote verifier data to verifier_only_circuit_data.json +[2024-10-09T03:32:55Z INFO plonky2x::backend::wrapper::wrap] Succesfully wrote proof to proof_with_public_inputs.json +[2024-10-09T03:32:55Z INFO plonky2::util::timing] 114.2421s to prove total time +03:32:59 INF compiling circuit +03:32:59 INF parsed circuit inputs nbPublic=1 nbSecret=11182 +03:33:39 INF building constraint builder nbConstraints=5815132 +Generating witness 2024-10-09 03:40:27.357393651 +0000 UTC m=+565.926341691 +frontend.NewWitness cost time: 130 ms +Creating proof 2024-10-09 03:40:27.487971493 +0000 UTC m=+566.056919503 +03:40:32 DBG constraint system solver done nbConstraints=5815132 took=5409.755661 +03:40:40 DBG prover done acceleration=none backend=groth16 curve=bn254 nbConstraints=5815132 took=7795.659351 +groth16.Prove cost time: 13205 ms +Verifying proof 2024-10-09 03:40:40.69365127 +0000 UTC m=+579.262599280 +03:40:40 DBG verifier done backend=groth16 curve=bn254 took=1.583203 groth16.Verify cost time: 1 ms before len of publicWitness:1 after len of publicWitness:2 -08:21:47 DBG verifier done backend=groth16 curve=bn254 took=1.244218 -[2024-10-07T08:21:47Z INFO zkm_prove] Proof: successfully written 1260 bytes. -[2024-10-07T08:21:47Z INFO zkm_prove] Contract: successfully written 10330 bytes. -[2024-10-07T08:21:47Z INFO zkm_prove] Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src . -[2024-10-07T08:21:47Z INFO zkm_prove] Elapsed time: 587 secs +03:40:40 DBG verifier done backend=groth16 curve=bn254 took=1.247698 +[2024-10-09T03:40:40Z INFO zkm_prove] Proof: successfully written 1265 bytes. +[2024-10-09T03:40:40Z INFO zkm_prove] Contract: successfully written 10334 bytes. +[2024-10-09T03:40:40Z INFO zkm_prove] Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src . +[2024-10-09T03:40:40Z INFO zkm_prove] Elapsed time: 579 secs ``` -The proof and contract file will be in the contracts/verifier and contracts/src +The result proof and contract file will be in the contracts/verifier and contracts/src respectively. #### Network Proving -Make any edits to [`run-network-proving.sh`](host-program/run-network-proving.sh) and run the program: +> [!NOTE] +> The proving network may sometimes experience high traffic, causing proof tasks to be queued for hours. + +> The proving task requires several stages: queuing, splitting, proving, aggregating and finalizing. Each stage involves a varying duration. + +Must set the `PRIVATE_KEY` in [`run-network-proving.sh`](host-program/run-network-proving.sh) and run the program: ```sh -./run-network-proving.sh sha2-go +./run-network-proving.sh sha2-rust ``` If successful, it will output a similar log: -##### **`sha2-go-network-proving.log`** +##### **`sha2-rust-network-proving.log`** ``` -[2024-10-07T08:42:02Z INFO zkm_prove] new prover client. -[2024-10-07T08:42:02Z INFO zkm_prove] new prover client,ok. -[2024-10-07T08:42:02Z INFO zkm_sdk::network::prover] calling request_proof. -[2024-10-07T08:42:04Z INFO zkm_sdk::network::prover] calling wait_proof, proof_id=d9a9bf8f-aff5-4103-af9e-b14ac6556a45 -[2024-10-07T08:42:05Z INFO zkm_sdk::network::prover] generate_proof : queuing the task. -[2024-10-07T08:42:35Z INFO zkm_sdk::network::prover] generate_proof : aggregating the proof. -[2024-10-07T08:43:07Z INFO zkm_prove] Proof: successfully written 1268 bytes. -[2024-10-07T08:43:07Z INFO zkm_prove] Contract: successfully written 10329 bytes. -[2024-10-07T08:43:07Z INFO zkm_prove] Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src . -[2024-10-07T08:43:07Z INFO zkm_prove] Elapsed time: 64 secs +[2024-10-09T03:55:06Z INFO zkm_prove] new prover client. +[2024-10-09T03:55:07Z INFO zkm_prove] new prover client,ok. +[2024-10-09T03:55:07Z INFO zkm_sdk::network::prover] calling request_proof. +[2024-10-09T03:55:10Z INFO zkm_sdk::network::prover] calling wait_proof, proof_id=fccd67f1-0bb3-498d-a048-50578b4a6360 +[2024-10-09T03:55:10Z INFO zkm_sdk::network::prover] generate_proof : queuing the task. +[2024-10-09T03:55:40Z INFO zkm_sdk::network::prover] generate_proof : finalizing the proof. +[2024-10-09T03:56:14Z INFO zkm_prove] Proof: successfully written 1262 bytes. +[2024-10-09T03:56:14Z INFO zkm_prove] Contract: successfully written 10329 bytes. +[2024-10-09T03:56:14Z INFO zkm_prove] Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src . +[2024-10-09T03:56:14Z INFO zkm_prove] Elapsed time: 66 secs ``` -The proof and contract file will be in the contracts/verifier and contracts/src +The result proof and contract file will be in the contracts/verifier and contracts/src. -#### 4. Deploy the Verifier Contract +### 4. Deploy the Verifier Contract If your system does not has Foundry, please install it: ```sh curl -L https://foundry.paradigm.xyz | bash ``` +#### Verify the snark proof generateing in the step 3 + +``` +cd zkm-project-template/contracts +forge test +``` + +If successful, it will output a similar log: + +``` +[⠊] Compiling... +[⠊] Compiling 2 files with Solc 0.8.26 +[⠢] Solc 0.8.26 finished in 921.86ms +Compiler run successful! + +Ran 1 test for test/verifier.t.sol:VerifierTest +[PASS] test_ValidProof() (gas: 286833) +Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 8.16ms (7.51ms CPU time) + +Ran 1 test suite in 9.02ms (8.16ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests) +``` + +#### Deploy the contract generateing in the step 3 + +Please edit the following parameters according your aim blockchain. + +``` +forge script script/verifier.s.sol:VerifierScript --rpc-url https://eth-sepolia.g.alchemy.com/v2/RH793ZL_pQkZb7KttcWcTlOjPrN0BjOW --private-key df4bc5647fdb9600ceb4943d4adff3749956a8512e5707716357b13d5ee687d9 +``` + +If successful, it will output a similar log: + +``` +[⠊] Compiling... +[⠘] Compiling 2 files with Solc 0.8.26 +[⠊] Solc 0.8.26 finished in 699.26ms +Compiler run successful! +Script ran successfully. + +## Setting up 1 EVM. + +========================== + +Chain 11155111 + +Estimated gas price: 0.000035894 gwei + +Estimated total gas used for script: 1228147 + +Estimated amount required: 0.000000044083108418 ETH + +========================== + +SIMULATION COMPLETE. To broadcast these transactions, add --broadcast and wallet configuration(s) to the previous command. See forge script --help for more. + +Transactions saved to: /mnt/data/zkm-project-template/contracts/broadcast/verifier.s.sol/11155111/dry-run/run-latest.json + +Sensitive values saved to: /mnt/data/zkm-project-template/contracts/cache/verifier.s.sol/11155111/dry-run/run-latest.json +``` -Next, deploy the contract as detailed in [this](contracts/README.md) guide +For more details, please refer to [this](contracts/README.md) guide. diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index 93c079bb..e53cb5a8 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -5,51 +5,40 @@ // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pragma solidity ^0.8.0; - library Pairing { struct G1Point { - uint256 X; - uint256 Y; + uint X; + uint Y; } // Encoding of field elements is: X[0] * z + X[1] - struct G2Point { - uint256[2] X; - uint256[2] Y; + uint[2] X; + uint[2] Y; } /// @return the generator of G1 - - function P1() internal pure returns (G1Point memory) { + function P1() pure internal returns (G1Point memory) { return G1Point(1, 2); } /// @return the generator of G2 - - function P2() internal pure returns (G2Point memory) { + function P2() pure internal returns (G2Point memory) { return G2Point( - [ - 10857046999023057135944570762232829481370756359578518086990519993285655852781, - 11559732032986387107991004021392285783925812861821192530917403151452391805634 - ], - [ - 8495653923123431417604973247489272438418190587263600148770280649306958101930, - 4082367875863433681332203403145435568316851327593401208105741076214120093531 - ] + [10857046999023057135944570762232829481370756359578518086990519993285655852781, + 11559732032986387107991004021392285783925812861821192530917403151452391805634], + [8495653923123431417604973247489272438418190587263600148770280649306958101930, + 4082367875863433681332203403145435568316851327593401208105741076214120093531] ); } /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. - - function negate(G1Point memory p) internal pure returns (G1Point memory) { + function negate(G1Point memory p) pure internal returns (G1Point memory) { // The prime q in the base field F_q for G1 - uint256 q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; - if (p.X == 0 && p.Y == 0) { + uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; + if (p.X == 0 && p.Y == 0) return G1Point(0, 0); - } return G1Point(p.X, q - (p.Y % q)); } /// @return r the sum of two points of G1 - function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) { - uint256[4] memory input; + uint[4] memory input; input[0] = p1.X; input[1] = p1.Y; input[2] = p2.X; @@ -58,41 +47,40 @@ library Pairing { assembly { success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60) // Use "invalid" to make gas estimation work - switch success - case 0 { invalid() } + switch success case 0 { invalid() } } require(success); } + /// @return r the product of a point on G1 and a scalar, i.e. /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. - function scalar_mul(G1Point memory p, uint256 s) internal view returns (G1Point memory r) { - uint256[3] memory input; + function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) { + uint[3] memory input; input[0] = p.X; input[1] = p.Y; input[2] = s; bool success; - + assembly { success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) // Use "invalid" to make gas estimation work - switch success - case 0 { invalid() } + switch success case 0 { invalid() } } - - require(success); + + require (success); } /// @return the result of computing the pairing check /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should /// return true. - function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) { require(p1.length == p2.length); - uint256 elements = p1.length; - uint256 inputSize = elements * 6; - uint256[] memory input = new uint256[](inputSize); - for (uint256 i = 0; i < elements; i++) { + uint elements = p1.length; + uint inputSize = elements * 6; + uint[] memory input = new uint[](inputSize); + for (uint i = 0; i < elements; i++) + { input[i * 6 + 0] = p1[i].X; input[i * 6 + 1] = p1[i].Y; input[i * 6 + 2] = p2[i].X[1]; @@ -100,25 +88,20 @@ library Pairing { input[i * 6 + 4] = p2[i].Y[1]; input[i * 6 + 5] = p2[i].Y[0]; } - uint256[1] memory out; + uint[1] memory out; bool success; - + assembly { success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) // Use "invalid" to make gas estimation work // switch success case 0 { invalid() } } - - require(success, "no"); + + require(success,"no"); return out[0] != 0; } /// Convenience method for a pairing check for two pairs. - - function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) - internal - view - returns (bool) - { + function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](2); G2Point[] memory p2 = new G2Point[](2); p1[0] = a1; @@ -128,14 +111,10 @@ library Pairing { return pairing(p1, p2); } /// Convenience method for a pairing check for three pairs. - function pairingProd3( - G1Point memory a1, - G2Point memory a2, - G1Point memory b1, - G2Point memory b2, - G1Point memory c1, - G2Point memory c2 + G1Point memory a1, G2Point memory a2, + G1Point memory b1, G2Point memory b2, + G1Point memory c1, G2Point memory c2 ) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](3); G2Point[] memory p2 = new G2Point[](3); @@ -148,16 +127,11 @@ library Pairing { return pairing(p1, p2); } /// Convenience method for a pairing check for four pairs. - function pairingProd4( - G1Point memory a1, - G2Point memory a2, - G1Point memory b1, - G2Point memory b2, - G1Point memory c1, - G2Point memory c2, - G1Point memory d1, - G2Point memory d2 + G1Point memory a1, G2Point memory a2, + G1Point memory b1, G2Point memory b2, + G1Point memory c1, G2Point memory c2, + G1Point memory d1, G2Point memory d2 ) internal view returns (bool) { G1Point[] memory p1 = new G1Point[](4); G2Point[] memory p2 = new G2Point[](4); @@ -175,10 +149,9 @@ library Pairing { contract Verifier { event VerifyEvent(address user); - event Value(uint256 x, uint256 y); + event Value(uint x, uint y); using Pairing for *; - struct VerifyingKey { Pairing.G1Point alpha; Pairing.G2Point beta; @@ -186,135 +159,30 @@ contract Verifier { Pairing.G2Point delta; Pairing.G1Point[] gamma_abc; } - struct Proof { Pairing.G1Point a; Pairing.G2Point b; Pairing.G1Point c; } + function verifyingKey() pure internal returns (VerifyingKey memory vk) { + vk.alpha = Pairing.G1Point(uint256(5238594224415803428903552163224573733888230552897477768815476746088817694665), uint256(16328026879972510425634956651757872870689877162649076255746551787054864220355)); + vk.beta = Pairing.G2Point([uint256(20324176648632662611169376759002700320443304453272693285118269429660227814847), uint256(9549073425549497017615537849571476830587596656015613611017956266089308903531)], [uint256(9337569445850381137129018980746968145714905364224388186337838316616854951754), uint256(3513913997312605870476238882256631130522705321741945577205945205501138882378)]); + vk.gamma = Pairing.G2Point([uint256(15161880990256081123123373723003760974001614786036869673944011467642681350273), uint256(9163286039211553980285501179844267362965858818412298332978707992237561271164)], [uint256(8680437061849217529408110466997375226429075645841820069852588072607969726492), uint256(18800549715559362373103731617654666483740554675125608300590590295497678086269)]); + vk.delta = Pairing.G2Point([uint256(5084358276310810830336673597459062196788688825694978068951204319614533728072), uint256(2821074186679041829554082022899130273208453252341587777698126697055071930462)], [uint256(21456200227661305460882352714936824169326675407976258160476432406731382755331), uint256(7296447269627149028431860329913792045260832865974902439493666631626649609193)]); + vk.gamma_abc = new Pairing.G1Point[](3); + vk.gamma_abc[0] = Pairing.G1Point(uint256(12714166869634051746888443609265681012145928347211427641836759112149836905420), uint256(11793561626188782503504016672377056451459939677293717125670005645761619823785)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(12843888235824976460769557467680522067504783112310502594161600686576288283623), uint256(21500613568509565203375400055914065701770933353363264002526901521910619984442)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(8046476541142506270894150739369274117716896582126953035714106843523496457093), uint256(11973219905447798721811213848944330597161079598635743680895679571487377395568)); - function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point( - uint256(1302117677361910542468231459920151979091943974637170989682078414965483188281), - uint256(3867027232541254772075321782990110912188042242600509887048224814657257159698) - ); - vk.beta = Pairing.G2Point( - [ - uint256(14801598391870170737198692701386770912604845156083007898115207806880331104566), - uint256(8887197963407115464952338401821009369268043467003949766013809662131496593072) - ], - [ - uint256(2302950197816344635982649983475492746659754717141582763685467658478763816306), - uint256(14546187023230700546432589234473482335485386237848823348478034929543161739283) - ] - ); - vk.gamma = Pairing.G2Point( - [ - uint256(21193141255951568057241429222082940283695501709413235369564469208569004052793), - uint256(3543300415735709873986269632293216615803290229738110628079419132119895168426) - ], - [ - uint256(19424433138581986634666612545039047288274363442950180138503536129758457024677), - uint256(6362422927587009743413699214616594497342377797306122883504232193179957350849) - ] - ); - vk.delta = Pairing.G2Point( - [ - uint256(7385068669210247546745717886468250961916074106623897850964828221823027078300), - uint256(19192861161778484819667846292071148227610103974433548788489138394128409122900) - ], - [ - uint256(16629788660241964719751663969097289423751591856184503635027243409639557586033), - uint256(12621057362679150933080455091106081399032880392542297635602848266829657178901) - ] - ); - vk.gamma_abc = new Pairing.G1Point[](66); - vk.gamma_abc[0] = Pairing.G1Point( - uint256(8227080285934790069042597020674361445558585705178995755056818730155727831239), - uint256(10964499248881051681906280027475314624544858740993358611805036213651538481961) - ); - vk.gamma_abc[1] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[3] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[4] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[5] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[6] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[7] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[8] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[9] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[10] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[11] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[12] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[13] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[14] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[15] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[16] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[17] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[18] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[19] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[20] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[21] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[22] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[23] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[24] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[25] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[26] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[27] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[28] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[29] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[30] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[31] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[32] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[33] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[34] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[35] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[36] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[37] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[38] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[39] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[40] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[41] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[42] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[43] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[44] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[45] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[46] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[47] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[48] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[49] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[50] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[51] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[52] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[53] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[54] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[55] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[56] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[57] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[58] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[59] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[60] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[61] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[62] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[63] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[64] = Pairing.G1Point(uint256(0), uint256(0)); - vk.gamma_abc[65] = Pairing.G1Point( - uint256(4811611970921445049598943815770920513454731152861450007955704045904060821183), - uint256(5288069492546863186831394199978690427589784869292363777070175867300583082895) - ); } - - function verify(uint256[65] memory input, Proof memory proof, uint256[2] memory proof_commitment) - public - view - returns (uint256) - { + function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; - + VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.gamma_abc.length); // Compute the linear combination vk_x Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); - for (uint256 i = 0; i < input.length; i++) { + for (uint i = 0; i < input.length; i++) { require(input[i] < snark_scalar_field); vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.gamma_abc[i + 1], input[i])); } @@ -323,33 +191,23 @@ contract Verifier { vk_x = Pairing.addition(vk_x, vk.gamma_abc[0]); vk_x = Pairing.addition(vk_x, p_c); - if ( - !Pairing.pairingProd4( - proof.a, - proof.b, - Pairing.negate(vk_x), - vk.gamma, - Pairing.negate(proof.c), - vk.delta, - Pairing.negate(vk.alpha), - vk.beta - ) - ) { + if(!Pairing.pairingProd4( + proof.a, proof.b, + Pairing.negate(vk_x), vk.gamma, + Pairing.negate(proof.c), vk.delta, + Pairing.negate(vk.alpha), vk.beta)) { return 1; } return 0; } - - function verifyTx(Proof memory proof, uint256[65] memory input, uint256[2] memory proof_commitment) - public - returns (bool r) - { - if (verify(input, proof, proof_commitment) == 0) { + function verifyTx(Proof memory proof, uint[2] memory input,uint[2] memory proof_commitment) public returns (bool r) { + if (verify(input, proof , proof_commitment) == 0) { emit VerifyEvent(msg.sender); return true; } else { return false; } + } } diff --git a/contracts/verifier/snark_proof_with_public_inputs.json b/contracts/verifier/snark_proof_with_public_inputs.json index d1b01449..85400eb2 100644 --- a/contracts/verifier/snark_proof_with_public_inputs.json +++ b/contracts/verifier/snark_proof_with_public_inputs.json @@ -1 +1 @@ -{"Proof":{"Ar":{"X":"20986100223742189794428594083244953701408789026843989908435048719917632304610","Y":"13312112978838104297936765092731357265583798633367270419949950449739950262299"},"Krs":{"X":"21414385011717857401350318474624945713895831525422758574143367432063095862610","Y":"16423991045129868936811412810791234200886558293119184537442651275319977235533"},"Bs":{"X":{"A0":"11334377274679574919826609438981352168291239642667203135465250445892409261829","A1":"502135620489275023389952763328439911399553648581965110109386553942302300788"},"Y":{"A0":"17625733828018823879938709981005705770193800659886938923296199657306934864258","A1":"4260326572269494480125515901189030920228567526305403135494458666071296191877"}},"Commitments":[{"X":"17273959889587427045931700521956542100389478452246646316053540421122551664021","Y":"18318673986437913163676530971051070041062128124589353307942427661936033278456"}],"CommitmentPok":{"X":"5949616844048677802101960585122116163044527517837594064636603012726545439747","Y":"4465071354076921449919725370557406791935776422229056588281381686837540526870"}},"PublicWitness":["3","176","196","66","152","252","28","20","154","251","244","200","153","111","185","36","39","174","65","228","100","155","147","76","164","149","153","27","120","82","184","85","3","176","196","66","152","252","28","20","154","251","244","200","153","111","185","36","39","174","65","228","100","155","147","76","164","149","153","27","120","82","184","85","5009287862160307920686587520880862879519265129424853749905862185648451074148"]} +{"Proof":{"Ar":{"X":"3223137300515445726621629654577034181236916187189785984102453772249712255827","Y":"2907185160819486517309582444046991589553988543708172017947532573798459447921"},"Krs":{"X":"7066186176465048809733280497216598020523106556044957581849945800840269416379","Y":"3732024050162698049865139506205846573121886992437414133229103952366510011770"},"Bs":{"X":{"A0":"3647581900857928744838950884624912135501405932076964650706778167110063289368","A1":"10872264759571928291757378913843731979794182183285532312639432495005516036045"},"Y":{"A0":"9235359712151784672496663847728441610467493809670596197164822332601059021360","A1":"8731929874657632816532791785050050105892306814781557898819317253682948163858"}},"Commitments":[{"X":"15835086452791747466946468947786652690521774906036198026832796976111003763868","Y":"6518905239803628186814550149282583795606243706243533259996248696440258609839"}],"CommitmentPok":{"X":"12811251794403685998689516148413985560091857813334005178016204587919771507178","Y":"9168106137408576766699902480770799271786919492461811035776871447094514990722"}},"PublicWitness":["6362267552962215457950634796317935612781553830779390456909670246098990432035","21220226220220531830097386689602577053359950660605734611847341805198693553528"]} \ No newline at end of file diff --git a/guest-program/README.md b/guest-program/README.md index 80fa95f4..8030b3c5 100644 --- a/guest-program/README.md +++ b/guest-program/README.md @@ -26,8 +26,9 @@ chmod +x install_mips_rust_tool ``` cd zkm-project-template/guest-program/sha2-go -GOOS=linux GOARCH=mips GOMIPS=softfloat go build -o zkm-mips-elf-sha2-go +GOOS=linux GOARCH=mips GOMIPS=softfloat go build -o sha2-go ``` +The compiled mips ELF is in the current path. * Compile the rust guest program @@ -42,5 +43,5 @@ cd zkm-project-template/guest-program/mem-alloc-vec cargo build --target=mips-unknown-linux-musl --release ``` -The compiled mips ELF is in the zkm-project-template/guest-program/revme/target/mips-unknown-linux-musl/release/zkm-mips-elf-xxx +The compiled mips ELF is in the zkm-project-template/guest-program/revme/target/mips-unknown-linux-musl/release/ . diff --git a/host-program/Cargo.toml b/host-program/Cargo.toml index 9a59779a..a8dbe30b 100644 --- a/host-program/Cargo.toml +++ b/host-program/Cargo.toml @@ -13,7 +13,7 @@ path = "src/bin/zkm-prove.rs" zkm-sdk = { path = "../sdk", features = ["snark"] } bincode = "1.3.3" common = { git = "https://github.com/zkMIPS/zkm-prover", branch = "main", default-features = false } - +sha2 = { version = "0.10.8", default-features = false } tonic = "0.8.1" prost = "0.11.0" diff --git a/host-program/run-local-proving.sh b/host-program/run-local-proving.sh index b144d038..4c8bd0c4 100755 --- a/host-program/run-local-proving.sh +++ b/host-program/run-local-proving.sh @@ -1,3 +1,8 @@ +if [ $# -lt 1 ]; then + echo "usage: ./run_local_proving sha2-go [or sha2-rust or mem-alloc-vec]" + exit 1 +fi + set -e program=$1 BASEDIR=$(cd $(dirname $0); pwd) @@ -7,16 +12,26 @@ export RUST_LOG=info export SEG_SIZE=262144 export ARGS="711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d world!" export ELF_PATH=${BASEDIR}/../guest-program/$program/target/mips-unknown-linux-musl/release/$program +export EXECUTE_ONLY=false echo "Compile guest-program ${program}" -cd $BASEDIR/../guest-program/$program -cargo build -r --target=mips-unknown-linux-musl +if [[ "$program" =~ .*go$ ]];then + cd $BASEDIR/../guest-program/$program + GOOS=linux GOARCH=mips GOMIPS=softfloat go build -o $program + export ELF_PATH=${BASEDIR}/../guest-program/$program/$program +else + cd $BASEDIR/../guest-program/$program + cargo build -r --target=mips-unknown-linux-musl +fi cd - -echo "Setup input for guest-program ${program}" -#export PUBLIC_INPUT_PATH=host-program/test-vectors/244.json -#export OUTPUT_DIR=/tmp/$program +if [ "$program" == "sha2-rust" ];then + export SEG_SIZE=65536 +elif [ "$program" == "mem-alloc-vec" ];then + export SEG_SIZE=65536 +fi +echo "SEG_SIZE:$SEG_SIZE" echo "BASEDIR:$BASEDIR" nohup ../target/release/zkm-prove $program >./$program-local-proving.log 2>&1 & -echo 'Check out the log by tail -f $program-local-proving.log' +echo "Check out the log by tail -f $program-local-proving.log" diff --git a/host-program/run-network-proving.sh b/host-program/run-network-proving.sh index 3124214c..c744faa7 100755 --- a/host-program/run-network-proving.sh +++ b/host-program/run-network-proving.sh @@ -1,3 +1,9 @@ +if [ $# -lt 1 ]; then + echo "usage: ./run_network_proving sha2-go [or sha2-rust or mem-alloc-vec]" + exit 1 +fi + +set -e program=$1 BASEDIR=$(cd $(dirname $0); pwd) export LD_LIBRARY_PATH=$BASEDIR/../sdk/src/local/libsnark:$LD_LIBRARY_PATH ##Modify it according your template @@ -6,13 +12,28 @@ export PRIVATE_KEY=xxxx ##The private key corresponding to the public key when export ENDPOINT=https://152.32.186.45:20002 ##the test entry of zkm proving network export ZKM_PROVER=network export RUST_LOG=info -export SEG_SIZE=62144 +export SEG_SIZE=262144 export ARGS="711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d world!" -#export ELF_PATH=${BASEDIR}/../guest-program/$program/target/mips-unknown-linux-musl/release/$program -#export PUBLIC_INPUT_PATH=host-program/test-vectors/244.json -#export OUTPUT_DIR=/tmp/$program -echo "BASEDIR:$BASEDIR" - +export ELF_PATH=${BASEDIR}/../guest-program/$program/target/mips-unknown-linux-musl/release/$program +export EXECUTE_ONLY=false +echo "Compile guest-program ${program}" +if [[ "$program" =~ .*go$ ]];then + cd $BASEDIR/../guest-program/$program + GOOS=linux GOARCH=mips GOMIPS=softfloat go build -o $program + export ELF_PATH=${BASEDIR}/../guest-program/$program/$program +else + cd $BASEDIR/../guest-program/$program + cargo build -r --target=mips-unknown-linux-musl +fi +cd - +##Notice: proof network, SEG_SIZE in [65536, 262144] +if [ "$program" == "sha2-rust" ];then + export SEG_SIZE=65536 +elif [ "$program" == "mem-alloc-vec" ];then + export SEG_SIZE=65536 +fi +echo "SEG_SIZE:$SEG_SIZE" +echo "BASEDIR:$BASEDIR" nohup ../target/release/zkm-prove $program >./$program-network-proving.log 2>&1 & -echo 'check out the log by tail -f $program-network-proving.log' +echo "Check out the log by tail -f $program-network-proving.log" diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 5dc954d7..2a5f90cd 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -1,9 +1,9 @@ use common::file; use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; use std::env; use std::path::Path; use std::time::Instant; - use zkm_sdk::{prover::ProverInput, ProverClient}; use std::fs::read; @@ -24,13 +24,10 @@ async fn main() -> Result<(), Box> { let prover_client = ProverClient::new().await; log::info!("new prover client,ok."); - let seg_size = env::var("SEG_SIZE").unwrap_or("65536".to_string()); + let seg_size = env::var("SEG_SIZE").unwrap_or("8192".to_string()); let seg_size2 = seg_size.parse::<_>().unwrap_or(65536); let execute_only = env::var("EXECUTE_ONLY").unwrap_or("false".to_string()); let execute_only2 = execute_only.parse::().unwrap_or(false); - //let elf_path = env::var("ELF_PATH").unwrap(); - let output_dir = env::var("OUTPUT_DIR").unwrap_or("/tmp/zkm".to_string()); - tokio::fs::create_dir_all(&output_dir).await?; let input: ProverInput = match args[1].as_str() { "sha2-rust" => { @@ -56,6 +53,13 @@ async fn main() -> Result<(), Box> { match proving_result { Ok(Some(prover_result)) => { if !execute_only2 { + if prover_result.proof_with_public_inputs.is_empty() { + log::info!( + "Fail: snark_proof_with_public_inputs.len() is : {}.Please try setting SEG_SIZE={}", + prover_result.proof_with_public_inputs.len(), seg_size2/2 + ); + return Err("SEG_SIZE is excessively large".into()); + } let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); let proof_result_path = output_path.join("snark_proof_with_public_inputs.json"); @@ -85,18 +89,37 @@ async fn main() -> Result<(), Box> { } log::info!("Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src ."); } else { - if prover_result.output_stream.is_empty() { - log::info!( - "output_stream.len() is too short: {}", - prover_result.output_stream.len() - ); - return Err("output_stream.len() is too short".into()); + match args[1].as_str() { + "sha2-rust" => { + //The guest program returns the basic type + if prover_result.output_stream.is_empty() { + log::info!( + "output_stream.len() is too short: {}", + prover_result.output_stream.len() + ); + return Err("output_stream.len() is too short".into()); + } + log::info!("Executing the guest program successfully."); + log::info!("ret_data: {:?}", prover_result.output_stream); + } + "sha2-go" => { + //If the guest program returns the structure, the result need the bincode::deserialize ! + if prover_result.output_stream.is_empty() { + log::info!( + "output_stream.len() is too short: {}", + prover_result.output_stream.len() + ); + return Err("output_stream.len() is too short".into()); + } + log::info!("Executing the guest program successfully."); + let ret_data: Data = + bincode::deserialize_from(prover_result.output_stream.as_slice()) + .expect("deserialization failed"); + log::info!("ret_data: {:?}", ret_data); + } + "mem-alloc-vec" => log::info!("Executing the guest program successfully."), //The guest program returns nothing. + _ => log::info!("Do nothing."), } - log::info!("Executing the guest program successfully."); - let ret_data: Data = - bincode::deserialize_from(prover_result.output_stream.as_slice()) - .expect("deserialization failed"); - log::info!("ret_data: {:?}", ret_data); } } Ok(None) => { @@ -116,22 +139,20 @@ async fn main() -> Result<(), Box> { } fn set_sha2_rust_intput(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { - let elf_path = env::var("ELF_PATH").unwrap_or( - "../guest-program/sha2-rust/target/mips-unknown-linux-musl/release/zkm-mips-elf-sha2-rust" - .to_string(), - ); - let args = env::var("ARGS").unwrap_or("data-to-hash".to_string()); + let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); + let num_bytes: usize = 1024; //Notice! : if this value is small, it will not generate the snark proof. + let pri_input = vec![5u8; num_bytes]; + let mut hasher = Sha256::new(); + hasher.update(&pri_input); + let result = hasher.finalize(); + let output: [u8; 32] = result.into(); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. - let args: Vec<&str> = args.split_whitespace().collect(); - assert_eq!(args.len(), 2); - let public_input: Vec = hex::decode(args[0]).unwrap(); - let private_input = args[1].as_bytes().to_vec(); + let public_input = output.to_vec(); let mut pub_buf = Vec::new(); bincode::serialize_into(&mut pub_buf, &public_input) .expect("public_input serialization failed"); let mut pri_buf = Vec::new(); - bincode::serialize_into(&mut pri_buf, &private_input) - .expect("private_input serialization failed"); + bincode::serialize_into(&mut pri_buf, &pri_input).expect("private_input serialization failed"); let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, @@ -193,8 +214,7 @@ impl Data { } fn set_sha2_go_intput(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { - let elf_path = - env::var("ELF_PATH").unwrap_or("../guest-program/sha2-go/zkm-mips-elf-sha2-go".to_string()); + let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); let args = env::var("ARGS").unwrap_or("data-to-hash".to_string()); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let args: Vec<&str> = args.split_whitespace().collect(); diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index 19671f57..d3532563 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -25,7 +25,7 @@ pub fn prove_stark( if input.execute_only { return Ok(false); } - + log::info!("!!!*******seg_num:{}", &seg_num); if seg_num == 1 { let seg_file = format!("{seg_path}/{}", 0); util::prove_single_seg_common(&seg_file, "", "", "")?;