From 308363d41f29688c2c4407e04b377a03f05837ac Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Thu, 17 Oct 2024 11:21:22 +0800 Subject: [PATCH 01/76] new snark proof --- README.md | 32 +++++++++++++++++++++++--- contracts/test/verifier.t.sol | 43 +++++++++++++++++++++++++++++++++++ guest-program/README.md | 21 +++++++++++++---- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 538c591f..6f5bd1d5 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,8 @@ cd zkm-project-template/host-program > 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. +> 3. There are four guest programs(sha2-rust, sha2-go, mem-alloc-vec,revme), each capable of generating a SNARK proof on a machine +equipped with an AMD EPYC 7R13 processor and 246GB of memory. The following will use sha2-rust and revme 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. @@ -124,7 +124,7 @@ equipped with an AMD EPYC 7R13 processor and 250GB of memory. The following will > 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-rust` +### Example 1 : `sha2-rust` 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. @@ -290,3 +290,29 @@ Sensitive values saved to: /mnt/data/zkm-project-template/contracts/cache/verifi ``` For more details, please refer to [this](contracts/README.md) guide. + +### Example 2 : `revme` + +The revme guest program takes a block data as input and its running is as same as the sha2-rust. Here, the focus is on explaining how to generate block data(the revme's input). + +#### Generating the public input about a specific block + +> [!NOTE] +> The local node is the [GOAT](https://goat.network) test chain in the following example. You must use the Eth-Compatible local node. + +```sh +cd ~ +git clone https://github.com/zkMIPS/revme +cd revme +RPC_URL=http://localhost:8545 CHAIN_ID=1337 BLOCK_NO=244 RUST_LOG=debug SUITE_JSON_PATH=./test-vectors/244.json cargo run --example process +``` + +If successfully, it will generate `244.json` in the path test-vectors + +```sh +cp test-vectors/244.json zkm-project-template/host-program/test-vectors/ +``` + +Next, you need to edit the `JSON_PATH` variable in the [`run-local-proving.sh`](host-program/run-local-proving.sh) or [`run-network-proving.sh`](host-program/run-network-proving.sh) to match the name of the JSON file mentioned above. + +Then, you can execute the run-xxx-proving.sh by following the steps outlined in `Example 1: sha2-rust`. \ No newline at end of file diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index 7e87eaf3..67c9cc5f 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -56,6 +56,7 @@ struct VerifierProof { PairingG1Point C ; } + contract VerifierTest is Test { using stdJson for string; @@ -119,4 +120,46 @@ contract VerifierTest is Test { } + function test_ValidPublicInputs() public { + string memory root = vm.projectRoot(); + string memory path1 = string.concat(root, "/verifier/snark_proof_with_public_inputs.json"); + string memory json1 = vm.readFile(path1); + bytes memory publicWitness = json1.parseRaw(".PublicWitness"); + string[] memory pubwit = abi.decode(publicWitness, ( string[])); + uint256 [2] memory input; + for (uint256 i = 0; i < pubwit.length; i++ ){ + input[i] = vm.parseUint(pubwit[i]); //--> uint256 + } + + string memory path = string.concat(root, "/verifier/public_inputs.json"); + string memory json = vm.readFile(path); + + bytes memory rootBefore = json.parseRaw(".roots_before.root"); + uint32[] memory rootBe = abi.decode(rootBefore, ( uint32[])); + uint32[8] memory rootb; + for (uint256 i = 0; i < rootBe.length; i++ ){ + //console.log("--before[i=%d], value:%s", i, rootBe[i]); + rootb[i] = rootBe[i]; + } + + bytes memory rootAfter = json.parseRaw(".roots_after.root"); + uint32[] memory rootAf = abi.decode(rootAfter, ( uint32[])); + uint32[8] memory roota; + for (uint256 i = 0; i < rootAf.length; i++ ){ + roota[i] = rootAf[i]; + } + + bytes memory userdata = json.parseRaw(".userdata"); + uint8[] memory dataU = abi.decode(userdata, ( uint8[])); + uint8[32] memory data; + for (uint256 i = 0; i < dataU.length; i++ ){ + data[i] = dataU[i]; + } + + uint256 returnNum = verifier.verifyUserData(data, rootb, roota); + + assert(returnNum == input[0]); + + } + } diff --git a/guest-program/README.md b/guest-program/README.md index 8030b3c5..2a66cbb8 100644 --- a/guest-program/README.md +++ b/guest-program/README.md @@ -4,12 +4,19 @@ ZKM can generate proof for Go and Rust (guest) Programs. * sha2-go -A simple program that takes struct Data as input, and operate the elements as an output. + A simple program that takes struct Data as input, and operates the elements as an output. * sha2-rust -It takes a public input and a private input ,then check the hash(private input)= public input. + + It takes a public input and a private input ,then checks the hash(private input)= public input. + +* mem-alloc-vec + + It allocs memories for vector ,then operates the memory(push and pop). -* mem-allox-vec +* revme + + This program is more complex, taking a block data as input and simulating the Ethereum Virtual Machine's computation for that block. > [!NOTE] > If you want to compile the guest programs, you should use a x86 Ubuntu22 machine with Rust: 1.81.0-nightly and Go : 1.22.1 @@ -43,5 +50,11 @@ 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/ . +or +``` +cd zkm-project-template/guest-program/revme +cargo build --target=mips-unknown-linux-musl --release +``` + +The compiled mips ELF is in the zkm-project-template/guest-program/{sha2-rust,mem-alloc-vec,revme}/target/mips-unknown-linux-musl/release/ . From 438d3699549a08edca1994a0cd4ceae656d262a0 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Thu, 17 Oct 2024 11:32:25 +0800 Subject: [PATCH 02/76] update host program --- host-program/src/bin/zkm-prove.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 777c6550..52892cab 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -77,6 +77,20 @@ async fn main() -> Result<(), Box> { return Err("Proof: failed to write to file".into()); } } + //public inputs + let output_dir = "../contracts/verifier".to_string(); + let output_path = Path::new(&output_dir); + let proof_result_path = output_path.join("public_inputs.json"); + let mut f = file::new(&proof_result_path.to_string_lossy()); + match f.write(prover_result.public_values.as_slice()) { + Ok(bytes_written) => { + log::info!("public_inputs: successfully written {} bytes.", bytes_written); + } + Err(e) => { + log::info!("public_inputs: failed to write to file: {}", e); + return Err("public_inputs: failed to write to file".into()); + } + } //contract let output_dir = "../contracts/src".to_string(); let output_path = Path::new(&output_dir); From 8a54191ecd3d7c34c2f2fc92aa814ab2905e3c3d Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 10:23:16 +0800 Subject: [PATCH 03/76] fix the user_data zero in the public_inputs --- host-program/src/bin/zkm-prove.rs | 5 +++++ sdk/src/local/stark.rs | 6 ++++++ sdk/src/network/prover.rs | 1 + sdk/src/prover.rs | 1 + 4 files changed, 13 insertions(+) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 52892cab..17cd806b 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -48,6 +48,7 @@ async fn main() -> Result<(), Box> { private_inputstream: "".into(), seg_size: 0, execute_only: false, + args: "".into(), } } }; @@ -178,6 +179,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, + args: "".into(), }; Ok(input) @@ -250,6 +252,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Res private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, + args: "".into(), }; Ok(input) @@ -277,6 +281,7 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result::minimal_parse(input.elf.as_slice()) .expect("opening elf file failed"); + let mut args: Vec<&str> = input.args.split_whitespace().collect(); + if args.len() > 2 { + args.truncate(2); + } + log::info!("args: {:?}", args); let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); + state.patch_stack(args); state.input_stream.push(input.public_inputstream.clone()); state.input_stream.push(input.private_inputstream.clone()); diff --git a/sdk/src/network/prover.rs b/sdk/src/network/prover.rs index a8ae75ef..a94bca81 100644 --- a/sdk/src/network/prover.rs +++ b/sdk/src/network/prover.rs @@ -98,6 +98,7 @@ impl Prover for NetworkProver { public_input_stream: input.public_inputstream.clone(), private_input_stream: input.private_inputstream.clone(), execute_only: input.execute_only, + args: input.args.clone(), ..Default::default() }; self.sign_ecdsa(&mut request).await; diff --git a/sdk/src/prover.rs b/sdk/src/prover.rs index f9156acb..21ca64f8 100644 --- a/sdk/src/prover.rs +++ b/sdk/src/prover.rs @@ -10,6 +10,7 @@ pub struct ProverInput { pub private_inputstream: Vec, pub seg_size: u32, pub execute_only: bool, + pub args: String, } #[derive(Debug, Default, Deserialize, Serialize, Clone)] From f82c3ec62475aca5d89dd66fe38c0bd7638aa61d Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 10:44:21 +0800 Subject: [PATCH 04/76] update host program --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 17cd806b..653f9250 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -252,7 +252,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result Date: Sat, 19 Oct 2024 10:46:05 +0800 Subject: [PATCH 05/76] update host program --- host-program/src/bin/zkm-prove.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 653f9250..e1af1243 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -236,9 +236,9 @@ impl Data { fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); - let args = env::var("ARGS").unwrap_or("data-to-hash".to_string()); + let args1 = 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(); + let args: Vec<&str> = args1.split_whitespace().collect(); assert_eq!(args.len(), 2); let mut data = Data::new(); // Fill in the input data @@ -252,7 +252,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result Date: Sat, 19 Oct 2024 11:03:55 +0800 Subject: [PATCH 06/76] update host program --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index e1af1243..3706a4c3 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -252,7 +252,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result Date: Sat, 19 Oct 2024 11:21:32 +0800 Subject: [PATCH 07/76] update host --- host-program/src/bin/zkm-prove.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 3706a4c3..a9d6a85a 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -5,7 +5,7 @@ use std::env; use std::path::Path; use std::time::Instant; use zkm_sdk::{prover::ProverInput, ProverClient}; - +use hex; use std::fs::read; #[tokio::main] @@ -166,6 +166,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); + let public_str = hex::encode(result); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -179,7 +180,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: "".into(), + args: public_str, }; Ok(input) From 1f56fc321f040e8b9fa69f061fb3523900e13aeb Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 11:29:30 +0800 Subject: [PATCH 08/76] update host --- host-program/src/bin/zkm-prove.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index a9d6a85a..1d682e1a 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,7 +166,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - let public_str = hex::encode(result); + //let public_str = hex::encode(result); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -174,6 +174,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< .expect("public_input serialization failed"); let mut pri_buf = Vec::new(); bincode::serialize_into(&mut pri_buf, &pri_input).expect("private_input serialization failed"); + let public_str = String::from_utf8(&pub_buf);; let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, From 840ccaee473370607bfb84c72a3c0c7974bea27f Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 11:30:53 +0800 Subject: [PATCH 09/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 1d682e1a..161145cd 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -181,7 +181,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: public_str, + args: public_str?, }; Ok(input) From 5ecd7c23885c0e7f5d10484820bfca4394f4a2aa Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 11:32:13 +0800 Subject: [PATCH 10/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 161145cd..9d3188d2 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -174,7 +174,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< .expect("public_input serialization failed"); let mut pri_buf = Vec::new(); bincode::serialize_into(&mut pri_buf, &pri_input).expect("private_input serialization failed"); - let public_str = String::from_utf8(&pub_buf);; + let public_str = String::from_utf8(pub_buf.clone());; let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, From 5316b8c1b962586455aedc200f277db2055391fd Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 19 Oct 2024 14:50:37 +0800 Subject: [PATCH 11/76] update host --- host-program/src/bin/zkm-prove.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 9d3188d2..889da5bc 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,7 +166,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - //let public_str = hex::encode(result); + let public_str = hex::encode(result); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -174,14 +174,14 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< .expect("public_input serialization failed"); let mut pri_buf = Vec::new(); bincode::serialize_into(&mut pri_buf, &pri_input).expect("private_input serialization failed"); - let public_str = String::from_utf8(pub_buf.clone());; + //let public_str = String::from_utf8(pub_buf.clone())?; let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: public_str?, + args: public_str, }; Ok(input) From d0fa62dc55b5e57e7435ccfef8d77fed23041342 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:11:29 +0800 Subject: [PATCH 12/76] update sdk --- host-program/src/bin/zkm-prove.rs | 10 +++++----- sdk/src/local/stark.rs | 15 +++++++++------ sdk/src/network/prover.rs | 2 +- sdk/src/prover.rs | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 889da5bc..cb8b5b59 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,7 +166,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - let public_str = hex::encode(result); + //let public_str = hex::encode(result); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -181,7 +181,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: public_str, + // args: public_str, }; Ok(input) @@ -254,7 +254,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Res private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, - args: "".into(), + // args: "".into(), }; Ok(input) @@ -283,7 +283,7 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result::minimal_parse(input.elf.as_slice()) .expect("opening elf file failed"); - let mut args: Vec<&str> = input.args.split_whitespace().collect(); - if args.len() > 2 { - args.truncate(2); - } - log::info!("args: {:?}", args); + //let mut args: Vec<&str> = input.args.split_whitespace().collect(); + //if args.len() > 2 { + // args.truncate(2); + //} + let public_input = input.public_inputstream.clone(); + let org_public_input = bincode::deserialize_from(public_input.as_slice()) + .expect("public_input deserialization failed"); + log::info!("args: {:?}", org_public_input); let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); - state.patch_stack(args); + state.patch_stack(org_public_input); state.input_stream.push(input.public_inputstream.clone()); state.input_stream.push(input.private_inputstream.clone()); diff --git a/sdk/src/network/prover.rs b/sdk/src/network/prover.rs index a94bca81..83fa69bb 100644 --- a/sdk/src/network/prover.rs +++ b/sdk/src/network/prover.rs @@ -98,7 +98,7 @@ impl Prover for NetworkProver { public_input_stream: input.public_inputstream.clone(), private_input_stream: input.private_inputstream.clone(), execute_only: input.execute_only, - args: input.args.clone(), + // args: input.args.clone(), ..Default::default() }; self.sign_ecdsa(&mut request).await; diff --git a/sdk/src/prover.rs b/sdk/src/prover.rs index 21ca64f8..94c56f84 100644 --- a/sdk/src/prover.rs +++ b/sdk/src/prover.rs @@ -10,7 +10,7 @@ pub struct ProverInput { pub private_inputstream: Vec, pub seg_size: u32, pub execute_only: bool, - pub args: String, + // pub args: String, } #[derive(Debug, Default, Deserialize, Serialize, Clone)] From 3eff278a18cc7afe1963e860323eeb0df9cc80dd Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:13:06 +0800 Subject: [PATCH 13/76] update sdk --- sdk/src/local/stark.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index 93d65a53..f7daa5aa 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -18,8 +18,7 @@ pub fn prove_stark( // args.truncate(2); //} let public_input = input.public_inputstream.clone(); - let org_public_input = bincode::deserialize_from(public_input.as_slice()) - .expect("public_input deserialization failed"); + let org_public_input = bincode::deserialize_from(public_input.as_slice()).expect("public_input deserialization failed"); log::info!("args: {:?}", org_public_input); let mut state = State::load_elf(&file); state.patch_elf(&file); From 046bb263e2ae9a089f006969e52c7b05347bc977 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:27:07 +0800 Subject: [PATCH 14/76] update sdk --- sdk/src/local/stark.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index f7daa5aa..c3733943 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -17,17 +17,21 @@ pub fn prove_stark( //if args.len() > 2 { // args.truncate(2); //} - let public_input = input.public_inputstream.clone(); - let org_public_input = bincode::deserialize_from(public_input.as_slice()).expect("public_input deserialization failed"); - log::info!("args: {:?}", org_public_input); + let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); - state.patch_stack(org_public_input); + //state.patch_stack(org_public_input); state.input_stream.push(input.public_inputstream.clone()); state.input_stream.push(input.private_inputstream.clone()); + let org_public_input = state.read_public_values::<[u8; 32]>(); + log::info!("public value: {:X?}", org_public_input); + log::info!("public value: {} in hex", hex::encode(org_public_input)); + let mut args: Vec<&str> = org_public_input.args.split_whitespace().collect(); + state.patch_stack(args); + let (_total_steps, seg_num, state) = split_prog_into_segs(state, &seg_path, "", seg_size); result.output_stream = state.public_values_stream.clone(); if input.execute_only { From 6b68ca397df97b664a3c3ce8fbe9f53868449f20 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:28:25 +0800 Subject: [PATCH 15/76] update sdk --- sdk/src/local/stark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index c3733943..10f3ee28 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -29,7 +29,7 @@ pub fn prove_stark( let org_public_input = state.read_public_values::<[u8; 32]>(); log::info!("public value: {:X?}", org_public_input); log::info!("public value: {} in hex", hex::encode(org_public_input)); - let mut args: Vec<&str> = org_public_input.args.split_whitespace().collect(); + let mut args: Vec<&str> = org_public_input.split_whitespace().collect(); state.patch_stack(args); let (_total_steps, seg_num, state) = split_prog_into_segs(state, &seg_path, "", seg_size); From 5f1c74122e22581e89a53f3edf019bbf471cd3c1 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:32:01 +0800 Subject: [PATCH 16/76] update sdk --- sdk/src/local/stark.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index 10f3ee28..c63ec2e6 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -29,8 +29,8 @@ pub fn prove_stark( let org_public_input = state.read_public_values::<[u8; 32]>(); log::info!("public value: {:X?}", org_public_input); log::info!("public value: {} in hex", hex::encode(org_public_input)); - let mut args: Vec<&str> = org_public_input.split_whitespace().collect(); - state.patch_stack(args); + //let mut args: Vec<&str> = hex::encode(org_public_input); + state.patch_stack(hex::encode(org_public_input)); let (_total_steps, seg_num, state) = split_prog_into_segs(state, &seg_path, "", seg_size); result.output_stream = state.public_values_stream.clone(); From ccfb5e399d5459fea874d1c118432de171e674f4 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 14:34:59 +0800 Subject: [PATCH 17/76] update sdk --- sdk/src/local/stark.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index c63ec2e6..007bd110 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -29,8 +29,8 @@ pub fn prove_stark( let org_public_input = state.read_public_values::<[u8; 32]>(); log::info!("public value: {:X?}", org_public_input); log::info!("public value: {} in hex", hex::encode(org_public_input)); - //let mut args: Vec<&str> = hex::encode(org_public_input); - state.patch_stack(hex::encode(org_public_input)); + let mut args: Vec<&str> = hex::encode(org_public_input); + state.patch_stack(args); let (_total_steps, seg_num, state) = split_prog_into_segs(state, &seg_path, "", seg_size); result.output_stream = state.public_values_stream.clone(); From fc2ac0713e8c533b8a50f8dd845919ad97bd4131 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:19:58 +0800 Subject: [PATCH 18/76] update sdk --- host-program/src/bin/zkm-prove.rs | 4 ++-- sdk/src/local/stark.rs | 18 ++++++------------ sdk/src/network/prover.rs | 2 +- sdk/src/prover.rs | 2 +- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index cb8b5b59..8dd056d5 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,7 +166,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - //let public_str = hex::encode(result); + let public_str = hex::encode(result); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -181,7 +181,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - // args: public_str, + args: public_str, }; Ok(input) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index 007bd110..941f8f3f 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -13,25 +13,19 @@ pub fn prove_stark( let seg_size = input.seg_size as usize; let file = ElfBytes::::minimal_parse(input.elf.as_slice()) .expect("opening elf file failed"); - //let mut args: Vec<&str> = input.args.split_whitespace().collect(); - //if args.len() > 2 { - // args.truncate(2); - //} - + let mut args: Vec<&str> = input.args.split_whitespace().collect(); + if args.len() > 1 { + args.truncate(1); + } + log::info!("args [len]:{}, [value]: {:?} ,", args[0].len(), args); let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); - //state.patch_stack(org_public_input); + state.patch_stack(args); state.input_stream.push(input.public_inputstream.clone()); state.input_stream.push(input.private_inputstream.clone()); - let org_public_input = state.read_public_values::<[u8; 32]>(); - log::info!("public value: {:X?}", org_public_input); - log::info!("public value: {} in hex", hex::encode(org_public_input)); - let mut args: Vec<&str> = hex::encode(org_public_input); - state.patch_stack(args); - let (_total_steps, seg_num, state) = split_prog_into_segs(state, &seg_path, "", seg_size); result.output_stream = state.public_values_stream.clone(); if input.execute_only { diff --git a/sdk/src/network/prover.rs b/sdk/src/network/prover.rs index 83fa69bb..a94bca81 100644 --- a/sdk/src/network/prover.rs +++ b/sdk/src/network/prover.rs @@ -98,7 +98,7 @@ impl Prover for NetworkProver { public_input_stream: input.public_inputstream.clone(), private_input_stream: input.private_inputstream.clone(), execute_only: input.execute_only, - // args: input.args.clone(), + args: input.args.clone(), ..Default::default() }; self.sign_ecdsa(&mut request).await; diff --git a/sdk/src/prover.rs b/sdk/src/prover.rs index 94c56f84..21ca64f8 100644 --- a/sdk/src/prover.rs +++ b/sdk/src/prover.rs @@ -10,7 +10,7 @@ pub struct ProverInput { pub private_inputstream: Vec, pub seg_size: u32, pub execute_only: bool, - // pub args: String, + pub args: String, } #[derive(Debug, Default, Deserialize, Serialize, Clone)] From 6438703332f47e855a8599bb0d6f7421abd969be Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:22:52 +0800 Subject: [PATCH 19/76] update host --- host-program/src/bin/zkm-prove.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 8dd056d5..889da5bc 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -254,7 +254,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Res private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, - // args: "".into(), + args: "".into(), }; Ok(input) @@ -283,7 +283,7 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result Date: Tue, 22 Oct 2024 15:32:39 +0800 Subject: [PATCH 20/76] update host --- host-program/src/bin/zkm-prove.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 889da5bc..26ca5cca 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,7 +166,9 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - let public_str = hex::encode(result); + //let public_str = hex::encode(result); + let s = std::str::from_utf8(&output).unwrap(); + let public_str: Vec<&str> = s.split_whitespace().collect(); // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); @@ -174,7 +176,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< .expect("public_input serialization failed"); let mut pri_buf = Vec::new(); bincode::serialize_into(&mut pri_buf, &pri_input).expect("private_input serialization failed"); - //let public_str = String::from_utf8(pub_buf.clone())?; + let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, From 7d6216382b8dfeda2cbdcbefb4956a3dc1c8e3f9 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:35:58 +0800 Subject: [PATCH 21/76] update host --- host-program/src/bin/zkm-prove.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 26ca5cca..99a4f6e3 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -167,8 +167,8 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< let result = hasher.finalize(); let output: [u8; 32] = result.into(); //let public_str = hex::encode(result); - let s = std::str::from_utf8(&output).unwrap(); - let public_str: Vec<&str> = s.split_whitespace().collect(); + let public_str = std::str::from_utf8(&output).unwrap(); + // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); From b23676f36ceca862a25ea9f77a41c64840af1fbc Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:38:56 +0800 Subject: [PATCH 22/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 99a4f6e3..6425eab3 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -183,7 +183,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: public_str, + args: public_str.to_string(), }; Ok(input) From 1f99947ca62c9b02e89567a0a6c5eda34f3d1bdf Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:45:23 +0800 Subject: [PATCH 23/76] update host --- host-program/src/bin/zkm-prove.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 6425eab3..398e05c5 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -167,7 +167,10 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< let result = hasher.finalize(); let output: [u8; 32] = result.into(); //let public_str = hex::encode(result); - let public_str = std::str::from_utf8(&output).unwrap(); + let public_str = match std::str::from_utf8(&output) { + Ok(v) => v, + Err(e) => panic!("Invalid UTF-8 sequence: {}", e), + }; // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); From d0713504f9149cb2f49ae5dcc06dd5c57673741a Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:48:33 +0800 Subject: [PATCH 24/76] update host --- host-program/src/bin/zkm-prove.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 398e05c5..f068dc5a 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -167,10 +167,15 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< let result = hasher.finalize(); let output: [u8; 32] = result.into(); //let public_str = hex::encode(result); - let public_str = match std::str::from_utf8(&output) { + /*let public_str = match std::str::from_utf8(&output) { Ok(v) => v, Err(e) => panic!("Invalid UTF-8 sequence: {}", e), - }; + };*/ + if let Ok(public_str) = std::str::from_utf8(&output) { + log::info!("public_str:{}", public_str); + } else { + panic!("Failed to convert byte array to UTF-8 string"); + } // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); From e6db4852f90032c60553a079453239b918ab3cd9 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:53:55 +0800 Subject: [PATCH 25/76] update host --- host-program/src/bin/zkm-prove.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index f068dc5a..1a84f959 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -167,15 +167,13 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< let result = hasher.finalize(); let output: [u8; 32] = result.into(); //let public_str = hex::encode(result); - /*let public_str = match std::str::from_utf8(&output) { + let public_str = match std::str::from_utf8(&output) { Ok(v) => v, - Err(e) => panic!("Invalid UTF-8 sequence: {}", e), - };*/ - if let Ok(public_str) = std::str::from_utf8(&output) { - log::info!("public_str:{}", public_str); - } else { - panic!("Failed to convert byte array to UTF-8 string"); - } + Err(e) => { + log::info!("Invalid UTF-8 sequence: {}", e); + "".to_string() + }, + }; // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); From c7eb8dc4ae09a107d86543920d918908f62d3069 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 15:55:31 +0800 Subject: [PATCH 26/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 1a84f959..2a0ad5d3 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -171,7 +171,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< Ok(v) => v, Err(e) => { log::info!("Invalid UTF-8 sequence: {}", e); - "".to_string() + &"".to_string() }, }; From 1edead6fa0abfa39c6c189a1c156d489ca9818c0 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 16:48:25 +0800 Subject: [PATCH 27/76] update host --- host-program/src/bin/zkm-prove.rs | 9 +++++---- sdk/src/local/stark.rs | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 2a0ad5d3..3d494221 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,14 +166,15 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - //let public_str = hex::encode(result); - let public_str = match std::str::from_utf8(&output) { + let public_str = hex::encode(result); + let first_32_chars = &public_str[..32]; + /*let public_str = match std::str::from_utf8(&output) { Ok(v) => v, Err(e) => { log::info!("Invalid UTF-8 sequence: {}", e); &"".to_string() }, - }; + }; */ // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); @@ -189,7 +190,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: public_str.to_string(), + args: first_32_chars, }; Ok(input) diff --git a/sdk/src/local/stark.rs b/sdk/src/local/stark.rs index 941f8f3f..75d6c60f 100644 --- a/sdk/src/local/stark.rs +++ b/sdk/src/local/stark.rs @@ -17,7 +17,12 @@ pub fn prove_stark( if args.len() > 1 { args.truncate(1); } - log::info!("args [len]:{}, [value]: {:?} ,", args[0].len(), args); + if args.len() > 0 { + log::info!("args [len]:{}, [value]: {:?} ,", args[0].len(), args); + }else{ + log::info!("args is null "); + } + let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); From f4c529a2478492171dcbf3e991521500d66dc779 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 22 Oct 2024 16:49:29 +0800 Subject: [PATCH 28/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 3d494221..f0ca319d 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -190,7 +190,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: first_32_chars, + args: first_32_chars.to_string(), }; Ok(input) From c6f1cd88497be4f21455708b4c3a77820475ad91 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Fri, 25 Oct 2024 14:49:09 +0800 Subject: [PATCH 29/76] zkm and gnark updating --- host-program/src/bin/zkm-prove.rs | 13 ------------- sdk/src/local/libsnark/contract.go | 8 +++----- sdk/src/local/stark.rs | 11 +---------- sdk/src/network/prover.rs | 1 - sdk/src/prover.rs | 1 - 5 files changed, 4 insertions(+), 30 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index f0ca319d..afadcea4 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -166,15 +166,6 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< hasher.update(&pri_input); let result = hasher.finalize(); let output: [u8; 32] = result.into(); - let public_str = hex::encode(result); - let first_32_chars = &public_str[..32]; - /*let public_str = match std::str::from_utf8(&output) { - Ok(v) => v, - Err(e) => { - log::info!("Invalid UTF-8 sequence: {}", e); - &"".to_string() - }, - }; */ // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. let public_input = output.to_vec(); @@ -190,7 +181,6 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< private_inputstream: pri_buf, seg_size: seg_size_u, execute_only: execute_only_b, - args: first_32_chars.to_string(), }; Ok(input) @@ -263,7 +253,6 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Res private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, - args: "".into(), }; Ok(input) @@ -292,7 +280,6 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result::minimal_parse(input.elf.as_slice()) .expect("opening elf file failed"); - let mut args: Vec<&str> = input.args.split_whitespace().collect(); - if args.len() > 1 { - args.truncate(1); - } - if args.len() > 0 { - log::info!("args [len]:{}, [value]: {:?} ,", args[0].len(), args); - }else{ - log::info!("args is null "); - } + let mut state = State::load_elf(&file); state.patch_elf(&file); state.patch_stack(vec![]); - state.patch_stack(args); state.input_stream.push(input.public_inputstream.clone()); state.input_stream.push(input.private_inputstream.clone()); diff --git a/sdk/src/network/prover.rs b/sdk/src/network/prover.rs index a94bca81..a8ae75ef 100644 --- a/sdk/src/network/prover.rs +++ b/sdk/src/network/prover.rs @@ -98,7 +98,6 @@ impl Prover for NetworkProver { public_input_stream: input.public_inputstream.clone(), private_input_stream: input.private_inputstream.clone(), execute_only: input.execute_only, - args: input.args.clone(), ..Default::default() }; self.sign_ecdsa(&mut request).await; diff --git a/sdk/src/prover.rs b/sdk/src/prover.rs index 21ca64f8..f9156acb 100644 --- a/sdk/src/prover.rs +++ b/sdk/src/prover.rs @@ -10,7 +10,6 @@ pub struct ProverInput { pub private_inputstream: Vec, pub seg_size: u32, pub execute_only: bool, - pub args: String, } #[derive(Debug, Default, Deserialize, Serialize, Clone)] From 4ba1505ac672921a9ddbacf804fa99854dd38600 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Fri, 25 Oct 2024 14:53:36 +0800 Subject: [PATCH 30/76] update host --- host-program/src/bin/zkm-prove.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index afadcea4..2b91dcc7 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -48,7 +48,6 @@ async fn main() -> Result<(), Box> { private_inputstream: "".into(), seg_size: 0, execute_only: false, - args: "".into(), } } }; From 077965f796911fefcfc5840fabf843e5d1a1bf76 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Fri, 25 Oct 2024 15:00:59 +0800 Subject: [PATCH 31/76] update host --- host-program/src/bin/zkm-prove.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 2b91dcc7..c31c1e5f 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -171,6 +171,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< 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, &pri_input).expect("private_input serialization failed"); @@ -181,7 +182,7 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result< seg_size: seg_size_u, execute_only: execute_only_b, }; - + log::info!("sha2_rust, bincode(pulic_input): {:?} ", &input.public_inputstream); Ok(input) } From 190c3d26350f200dc3b39f90e5e47aba1f18f643 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Fri, 25 Oct 2024 15:22:49 +0800 Subject: [PATCH 32/76] update sdk --- sdk/src/local/prover.rs | 2 +- sdk/src/local/util.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/sdk/src/local/prover.rs b/sdk/src/local/prover.rs index be419f23..37d8e085 100644 --- a/sdk/src/local/prover.rs +++ b/sdk/src/local/prover.rs @@ -51,7 +51,7 @@ impl ProverTask { result.solidity_verifier = std::fs::read(format!("{}/verifier.sol", outputdir)).unwrap(); result.public_values = - std::fs::read(format!("{}/public_values.json", inputdir)).unwrap(); + std::fs::read(format!("{}/block_public_inputs.json", inputdir)).unwrap(); } else { log::error!("Failed to generate snark proof."); } diff --git a/sdk/src/local/util.rs b/sdk/src/local/util.rs index 6d6e24cb..95e5cd44 100644 --- a/sdk/src/local/util.rs +++ b/sdk/src/local/util.rs @@ -217,13 +217,11 @@ pub fn prove_multi_seg_common( let wrapped_proof = wrapped_circuit.prove(&block_proof)?; wrapped_proof.save(outdir)?; - let outdir_path = std::path::Path::new(outdir); - let public_values_file = File::create(outdir_path.join("public_values.json"))?; - serde_json::to_writer(&public_values_file, &updated_agg_public_values)?; let block_public_inputs = serde_json::json!({ "public_inputs": block_proof.public_inputs, }); + let outdir_path = std::path::Path::new(outdir); let block_public_inputs_file = File::create(outdir_path.join("block_public_inputs.json"))?; serde_json::to_writer(&block_public_inputs_file, &block_public_inputs)?; From ae8da48876276c7bfa859c75e9e544b9d6b7c1b3 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Fri, 25 Oct 2024 15:49:04 +0800 Subject: [PATCH 33/76] update sdk --- sdk/src/local/prover.rs | 2 +- sdk/src/local/util.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/src/local/prover.rs b/sdk/src/local/prover.rs index 37d8e085..be419f23 100644 --- a/sdk/src/local/prover.rs +++ b/sdk/src/local/prover.rs @@ -51,7 +51,7 @@ impl ProverTask { result.solidity_verifier = std::fs::read(format!("{}/verifier.sol", outputdir)).unwrap(); result.public_values = - std::fs::read(format!("{}/block_public_inputs.json", inputdir)).unwrap(); + std::fs::read(format!("{}/public_values.json", inputdir)).unwrap(); } else { log::error!("Failed to generate snark proof."); } diff --git a/sdk/src/local/util.rs b/sdk/src/local/util.rs index 95e5cd44..0f0e4827 100644 --- a/sdk/src/local/util.rs +++ b/sdk/src/local/util.rs @@ -222,6 +222,9 @@ pub fn prove_multi_seg_common( "public_inputs": block_proof.public_inputs, }); let outdir_path = std::path::Path::new(outdir); + let public_values_file = File::create(outdir_path.join("public_values.json"))?; + serde_json::to_writer(&public_values_file, &updated_agg_public_values)?; + let block_public_inputs_file = File::create(outdir_path.join("block_public_inputs.json"))?; serde_json::to_writer(&block_public_inputs_file, &block_public_inputs)?; From e65c8a0a3251eaff0a183c04aab4216db98dd7b3 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 26 Oct 2024 16:40:25 +0800 Subject: [PATCH 34/76] update contract --- contracts/src/verifier.sol | 88 +++++++++++++++++++++++++------ contracts/test/verifier.t.sol | 5 +- host-program/src/bin/zkm-prove.rs | 2 + 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index e53cb5a8..a070cd5a 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -61,13 +61,13 @@ library Pairing { 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() } } - + require (success); } /// @return the result of computing the pairing check @@ -90,13 +90,13 @@ library Pairing { } 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"); return out[0] != 0; } @@ -148,6 +148,10 @@ library Pairing { } contract Verifier { + uint256 constant MASK = ~(uint256(0x7) << 253); + uint256 constant EMPTY_HASH = 0x3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855; + uint256 constant CIRCUIT_DIGEST = 17524917735085582509473322861927874526127848298611193781519203348111540784568; + event VerifyEvent(address user); event Value(uint x, uint y); @@ -165,19 +169,19 @@ contract Verifier { 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.alpha = Pairing.G1Point(uint256(16970347033103955758452994845685620017539555605614894841156398765252506042767), uint256(19158373549445638233271089317629397149950934530472186843813485792440726957241)); + vk.beta = Pairing.G2Point([uint256(8594263778771240697085764384849976393935685551424691094053894522562615783202), uint256(14184284680305080454547767982784123571813768288032879701682773614591782820464)], [uint256(3556204116477024470349043277879940666058107557138763110017093340808523859932), uint256(16256723636770504506049412551137171251497590732423939733133077873514510250540)]); + vk.gamma = Pairing.G2Point([uint256(5412681696539927907954705324217704281257179160986050833906852431824007377057), uint256(5157531605499783593998549928067353330643371844279110479212347327076701413139)], [uint256(12966475329661050129732425900072508193447453410510178246941553556512434741524), uint256(6452779364774476237870863262871258356673055268258452734391170290825134399481)]); + vk.delta = Pairing.G2Point([uint256(17582950633545913603684840509403214813189383356468975658594446151686323783700), uint256(9454897951491568633111951250862278168211961750495113360891053905298558991297)], [uint256(11281656033268678724865587168281738757382106431761958169178960537780006910274), uint256(6468248143583027225475112512495741562106994516233417225589306221530761847139)]); 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)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(12174941994645993736452108908683741256598597386960162354462886506985329539098), uint256(17229206348022982564916949531496247121219085409407697442796491258711837521805)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(18019694165351403779432422368588161319089235118048159912613396436357595061940), uint256(1212776829082822447015286891876234195601214475928427439028328609344343634762)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(2930232800117937744666966066364644873238929118209633871556876438652528620473), uint256(5984491430675216143188597428234276982818650818981787372181415725551138926341)); } 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 @@ -201,13 +205,67 @@ contract Verifier { return 0; } - function verifyTx(Proof memory proof, uint[2] memory input,uint[2] memory proof_commitment) public returns (bool r) { + 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; } - + } -} + + function verifyUserData( + bytes memory _userData, + uint32[8] memory _memRootBefore, + uint32[8] memory _memRootAfter + ) public pure returns (uint256) { + bytes32 userData = sha256(_userData); + + uint256 memRootBefore = 0; + for (uint256 i = 0; i < 8; i++) { + memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); + } + uint256 memRootAfter = 0; + for (uint256 i = 0; i < 8; i++) { + memRootAfter |= uint256(_memRootAfter[i]) << (32 * (7 - i)); + } + + bytes memory dataToHash = abi.encodePacked( + memRootBefore, + memRootAfter, + userData, + CIRCUIT_DIGEST, + getConstantSigmasCap() + ); + + uint256 hash_o = uint256(sha256(dataToHash)) & MASK; + uint256 hashValue = uint256(sha256(abi.encodePacked(EMPTY_HASH,hash_o))) & MASK; + + return hashValue; + } + + function getConstantSigmasCap() public pure returns (uint256[16] memory) { + return [ + 91383584712019272681377229182172954852761112840230485576576219640999915415908, + 59255968443781732618398964440087269720283395714373383774650673994811543257891, + 43522785206513529710314565972458653794534948943367780523535399673207256542667, + 40342750684549940679043033475171365072773076053340772573915667403010979823501, + 27317612392857105781687183282424868606341901129358521256675721204392832410439, + 43307034001377032728710254791751651528006774034804994236242404941502133194788, + 96847759610594182728575775013319360940143540443603110434763689062442262276056, + 103816866397357498456560831382884881537689393646419658130688223819092141266482, + 92927370392150370847456421756552282870716539874859278967093815334673659047245, + 48196270063118112994752430351531600491056188761932189573202142214229711823214, + 72300508929423173342535162393269108469073018007670992928238766269813213409236, + 96435373004420944780577491829986863106913456501189307806899876787207854460461, + 49751597273171012664300923220213149590083424576410050544981754459509775460226, + 34852111753877916137017959036830401094053286275843684490553560491310958812859, + 50803581021794237060724898598173476602507622641970514917690126680609140114800, + 97509144034546411327314284027738859064756953812076402994654732676369028259664 + ]; + } +} \ No newline at end of file diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index 67c9cc5f..e67d0153 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -151,12 +151,13 @@ contract VerifierTest is Test { bytes memory userdata = json.parseRaw(".userdata"); uint8[] memory dataU = abi.decode(userdata, ( uint8[])); - uint8[32] memory data; + uint8[40] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ data[i] = dataU[i]; + console.log("--user data[i=%d], value:%s", i, dataU[i]); } - uint256 returnNum = verifier.verifyUserData(data, rootb, roota); + uint256 returnNum = verifier.verifyUserData(userdata, rootb, roota); assert(returnNum == input[0]); diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index c31c1e5f..d7702bd2 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -80,6 +80,8 @@ async fn main() -> Result<(), Box> { //public inputs let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); + //replace the user data with the bincode of the public_input + let proof_result_path = output_path.join("public_inputs.json"); let mut f = file::new(&proof_result_path.to_string_lossy()); match f.write(prover_result.public_values.as_slice()) { From ae77dd9be7121eb130e320596e9bc47a410c1759 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 26 Oct 2024 17:09:42 +0800 Subject: [PATCH 35/76] update contract --- contracts/src/verifier.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index a070cd5a..36bf3c60 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -4,6 +4,7 @@ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // 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. +import {console} from "forge-std/console.sol"; pragma solidity ^0.8.0; library Pairing { struct G1Point { @@ -224,7 +225,7 @@ contract Verifier { uint32[8] memory _memRootAfter ) public pure returns (uint256) { bytes32 userData = sha256(_userData); - +console.log("********* userData :%s",userData); uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); From 82cd9581331047db7ff379911175627337f18ab2 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 26 Oct 2024 17:16:29 +0800 Subject: [PATCH 36/76] update contract --- contracts/src/verifier.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index 36bf3c60..69f76210 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -225,7 +225,7 @@ contract Verifier { uint32[8] memory _memRootAfter ) public pure returns (uint256) { bytes32 userData = sha256(_userData); -console.log("********* userData :%s",userData); +console.log("********* userData 11111111"); uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); From 4c5d0862482e41b2132a6eef80023c0836821bd1 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sat, 26 Oct 2024 17:19:11 +0800 Subject: [PATCH 37/76] update contract --- contracts/src/verifier.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index 69f76210..d0e21625 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -4,7 +4,7 @@ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // 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. -import {console} from "forge-std/console.sol"; + pragma solidity ^0.8.0; library Pairing { struct G1Point { @@ -225,7 +225,7 @@ contract Verifier { uint32[8] memory _memRootAfter ) public pure returns (uint256) { bytes32 userData = sha256(_userData); -console.log("********* userData 11111111"); + uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); From 9925ecf66b3e55f51d4167a61cff4bef8f91027f Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 08:46:08 +0800 Subject: [PATCH 38/76] update contract --- contracts/src/verifier.sol | 15 +++++++-------- contracts/test/verifier.t.sol | 3 +-- .../verifier/snark_proof_with_public_inputs.json | 2 +- sdk/src/local/libsnark/contract.go | 7 +++++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index d0e21625..cdcbce47 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -4,7 +4,6 @@ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // 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 { @@ -170,14 +169,14 @@ contract Verifier { Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point(uint256(16970347033103955758452994845685620017539555605614894841156398765252506042767), uint256(19158373549445638233271089317629397149950934530472186843813485792440726957241)); - vk.beta = Pairing.G2Point([uint256(8594263778771240697085764384849976393935685551424691094053894522562615783202), uint256(14184284680305080454547767982784123571813768288032879701682773614591782820464)], [uint256(3556204116477024470349043277879940666058107557138763110017093340808523859932), uint256(16256723636770504506049412551137171251497590732423939733133077873514510250540)]); - vk.gamma = Pairing.G2Point([uint256(5412681696539927907954705324217704281257179160986050833906852431824007377057), uint256(5157531605499783593998549928067353330643371844279110479212347327076701413139)], [uint256(12966475329661050129732425900072508193447453410510178246941553556512434741524), uint256(6452779364774476237870863262871258356673055268258452734391170290825134399481)]); - vk.delta = Pairing.G2Point([uint256(17582950633545913603684840509403214813189383356468975658594446151686323783700), uint256(9454897951491568633111951250862278168211961750495113360891053905298558991297)], [uint256(11281656033268678724865587168281738757382106431761958169178960537780006910274), uint256(6468248143583027225475112512495741562106994516233417225589306221530761847139)]); + vk.alpha = Pairing.G1Point(uint256(12213467821920820614707148851046325635862298693838622950893582979895118929487), uint256(4059718347818326615319162910259532091818465155501291697278660304496107954649)); + vk.beta = Pairing.G2Point([uint256(8230963559238861955506201567559068329924686135927674499019465350557180397001), uint256(20362248643492701283016451006112187976709659846269563330453373148803158445482)], [uint256(131929125271226424497412182353060640205419290657788563309462084176117793380), uint256(422778962068941430465715462509875358292226464127073776854932323425806922026)]); + vk.gamma = Pairing.G2Point([uint256(7424726390072423090222095311088712464296868513647357437012747975036226367992), uint256(20199738536879980066767324489975712041159572285071113752454978277835699609399)], [uint256(2018997229579349317989248785728982841421138100098766188086704930179801861031), uint256(13235660154221370689371221313415080957662892474207727494676971523650861166346)]); + vk.delta = Pairing.G2Point([uint256(4530557911715599742665153887937739756249082677029908686231255634514100917662), uint256(14095324423589541055029490731989662262746627407178873693998589853082713040842)], [uint256(8859993358987903903694632489163929327502630048839855545548429327482457025421), uint256(20316889508401033651911110702151291297049807841607703227468535428967208519478)]); vk.gamma_abc = new Pairing.G1Point[](3); - vk.gamma_abc[0] = Pairing.G1Point(uint256(12174941994645993736452108908683741256598597386960162354462886506985329539098), uint256(17229206348022982564916949531496247121219085409407697442796491258711837521805)); - vk.gamma_abc[1] = Pairing.G1Point(uint256(18019694165351403779432422368588161319089235118048159912613396436357595061940), uint256(1212776829082822447015286891876234195601214475928427439028328609344343634762)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(2930232800117937744666966066364644873238929118209633871556876438652528620473), uint256(5984491430675216143188597428234276982818650818981787372181415725551138926341)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(14796349507331874527258287799826463819790533189845565069693078084686797772667), uint256(10606116633033366329348352792591157031514797223481455697908932993078211946166)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(8584671564822225893276271088767010023935592071944919369619906910056929104667), uint256(15718970982588709431604436935544624196227257365890343871036848665502529538120)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(17567218296845033770969159993519342716625014384466128576891818709268526072947), uint256(407034824806782950319727681335763277921118187322175269320210991695792773757)); } function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index e67d0153..d3dcaa0a 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -154,10 +154,9 @@ contract VerifierTest is Test { uint8[40] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ data[i] = dataU[i]; - console.log("--user data[i=%d], value:%s", i, dataU[i]); } - uint256 returnNum = verifier.verifyUserData(userdata, rootb, roota); + uint256 returnNum = verifier.verifyUserData(data, rootb, roota); assert(returnNum == input[0]); diff --git a/contracts/verifier/snark_proof_with_public_inputs.json b/contracts/verifier/snark_proof_with_public_inputs.json index 85400eb2..3843da23 100644 --- a/contracts/verifier/snark_proof_with_public_inputs.json +++ b/contracts/verifier/snark_proof_with_public_inputs.json @@ -1 +1 @@ -{"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 +{"Proof":{"Ar":{"X":"8174031545225483383141418079564090791231490145781531926335276336175308863707","Y":"3834171856059451751436869467231246873520343021451160163408402538567178474219"},"Krs":{"X":"13586733060922750289506280282556626924213814817587389232592841141519550546903","Y":"7971169649759305355784664598000388441999417324143312928531542368266818198801"},"Bs":{"X":{"A0":"16244654992473864068434685396879843024965338706161139584337673486694680249857","A1":"6265690638221588196510439213899713108168752331944685238458496370594808062420"},"Y":{"A0":"12712710855225608154900745325328982591451728027910889754119239166082381964965","A1":"15879521449010124364490571648506817452456921365860115408511389594204873958337"}},"Commitments":[{"X":"10897131706311829408130705881694553087082057910633496700258666134136872262230","Y":"458953713510288618015750230832000155046368787668572283656604498671917216865"}],"CommitmentPok":{"X":"4415924133992939061388980411431402645937939123603889508394326920377549767070","Y":"7258264583926976292105251464579645788723690411202733258248780672362649452811"}},"PublicWitness":["13423602751184986938789540733258245323887402178343424404801648229902985960977","17447406357559998420445886886299190665098387339890379864001066407842671637468"]} \ No newline at end of file diff --git a/sdk/src/local/libsnark/contract.go b/sdk/src/local/libsnark/contract.go index 54aba21d..d2862142 100644 --- a/sdk/src/local/libsnark/contract.go +++ b/sdk/src/local/libsnark/contract.go @@ -217,11 +217,14 @@ contract Verifier { } function verifyUserData( - bytes memory _userData, + uint8[32] memory _userData, uint32[8] memory _memRootBefore, uint32[8] memory _memRootAfter ) public pure returns (uint256) { - bytes32 userData = sha256(_userData); + uint256 userData = 0; + for (uint256 i = 0; i < 32; i++) { + userData |= uint256(_userData[i]) << (8 * (31 - i)); + } uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { From 4f60b6af64299d81bab9cf82b2d86e3a5b0d7990 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 09:39:46 +0800 Subject: [PATCH 39/76] update host --- host-program/src/bin/zkm-prove.rs | 42 +++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index d7702bd2..43a23c6f 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -1,5 +1,6 @@ use common::file; use serde::{Deserialize, Serialize}; +use serde_json; use sha2::{Digest, Sha256}; use std::env; use std::path::Path; @@ -80,8 +81,7 @@ async fn main() -> Result<(), Box> { //public inputs let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); - //replace the user data with the bincode of the public_input - + // let proof_result_path = output_path.join("public_inputs.json"); let mut f = file::new(&proof_result_path.to_string_lossy()); match f.write(prover_result.public_values.as_slice()) { @@ -93,6 +93,11 @@ async fn main() -> Result<(), Box> { return Err("public_inputs: failed to write to file".into()); } } + //check the userdata = hash(bing(public_input)) + if !check_public_input(&input.public_inputstream, &proof_result_path){ + log::info!("public_inputs check false."); + return Err("public_inputs check false.".into()); + } //contract let output_dir = "../contracts/src".to_string(); let output_path = Path::new(&output_dir); @@ -286,3 +291,36 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result, +} + +#[derive(Serialize, Deserialize, Debug)] +struct Roots { + root: Vec, +} + +fn check_public_input(public_inputstream: Vec, file: &Path) -> bool { + let mut hasher = Sha256::new(); + hasher.update(&input.public_inputstream); + let result_hs = hasher.finalize(); + let output_hs: [u8; 32] = result_hs.into(); + + let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); + + let public_inputs: PublicInputs = serde_json::from_str(&file_contents) + .expect("Failed to parse JSON"); + + let userdata = public_inputs.userdata; + + if userdata == output_hs { + return true; + } else { + log::info!("public inputs is different. the file's is: {:?}, host's is :{:?} ", userdata, output_hs); + return false; + } +} From 47a454bc7b107e63aade558e86a4eb4f0e277991 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 09:42:00 +0800 Subject: [PATCH 40/76] update host --- host-program/src/bin/zkm-prove.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 43a23c6f..d00794d2 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -304,9 +304,9 @@ struct Roots { root: Vec, } -fn check_public_input(public_inputstream: Vec, file: &Path) -> bool { +fn check_public_input(public_inputstream: &Vec, file: &Path) -> bool { let mut hasher = Sha256::new(); - hasher.update(&input.public_inputstream); + hasher.update(&public_inputstream); let result_hs = hasher.finalize(); let output_hs: [u8; 32] = result_hs.into(); From d063380a18f691de369abfeda975194d949b30ce Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 10:04:39 +0800 Subject: [PATCH 41/76] update host --- contracts/src/verifier.sol | 21 ++++++++++++--------- contracts/test/verifier.t.sol | 4 ++-- host-program/src/bin/zkm-prove.rs | 14 ++++++++------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index cdcbce47..d3332031 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -169,14 +169,14 @@ contract Verifier { Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point(uint256(12213467821920820614707148851046325635862298693838622950893582979895118929487), uint256(4059718347818326615319162910259532091818465155501291697278660304496107954649)); - vk.beta = Pairing.G2Point([uint256(8230963559238861955506201567559068329924686135927674499019465350557180397001), uint256(20362248643492701283016451006112187976709659846269563330453373148803158445482)], [uint256(131929125271226424497412182353060640205419290657788563309462084176117793380), uint256(422778962068941430465715462509875358292226464127073776854932323425806922026)]); - vk.gamma = Pairing.G2Point([uint256(7424726390072423090222095311088712464296868513647357437012747975036226367992), uint256(20199738536879980066767324489975712041159572285071113752454978277835699609399)], [uint256(2018997229579349317989248785728982841421138100098766188086704930179801861031), uint256(13235660154221370689371221313415080957662892474207727494676971523650861166346)]); - vk.delta = Pairing.G2Point([uint256(4530557911715599742665153887937739756249082677029908686231255634514100917662), uint256(14095324423589541055029490731989662262746627407178873693998589853082713040842)], [uint256(8859993358987903903694632489163929327502630048839855545548429327482457025421), uint256(20316889508401033651911110702151291297049807841607703227468535428967208519478)]); + vk.alpha = Pairing.G1Point(uint256(17893901124021210272638516546159505340327529763852882834791797783715120736836), uint256(13231467695658339900650768414253718422865673742208133343863421143503462113168)); + vk.beta = Pairing.G2Point([uint256(17887881584875332850937101588175122783613013000081586029290713558009208876939), uint256(5173405259660942311812575171887960300813259764306641231158685689101871053226)], [uint256(15421617451342223732374977604635144339106949166377272589278289042372210038178), uint256(4017392132424093353152388503575757440680696483308117139782333575602591537707)]); + vk.gamma = Pairing.G2Point([uint256(1290588609385786732164658248069445973512624381080875973387694511433780759175), uint256(12976305822780276357100851134930711058729218483183625693066553997404593228345)], [uint256(18550887073089422994810671436018260406270744572816708595449262508502728046582), uint256(9330344219985993709191598687338119285767947902106034962837030239408815004050)]); + vk.delta = Pairing.G2Point([uint256(2993044589807208422766651638941259454777038307900585129916365748825379882932), uint256(13132866685093967953405556380011883248024322097351146455602465105530729440852)], [uint256(9287962342478396674958016817894657236291812832698868341924590133380732632404), uint256(18863937240777198026935865424217363800063264366248119243760988932201425732600)]); vk.gamma_abc = new Pairing.G1Point[](3); - vk.gamma_abc[0] = Pairing.G1Point(uint256(14796349507331874527258287799826463819790533189845565069693078084686797772667), uint256(10606116633033366329348352792591157031514797223481455697908932993078211946166)); - vk.gamma_abc[1] = Pairing.G1Point(uint256(8584671564822225893276271088767010023935592071944919369619906910056929104667), uint256(15718970982588709431604436935544624196227257365890343871036848665502529538120)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(17567218296845033770969159993519342716625014384466128576891818709268526072947), uint256(407034824806782950319727681335763277921118187322175269320210991695792773757)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(19701327339338910895804292089025911598468663156478648178422701350645841255447), uint256(13110634886897473934651042825240895861319233163917095215468572038313498764478)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(1494470184684746103182008386385284997483007826087210853641973278095564194395), uint256(15287893920487755265590292730618597525230562373919215600034902709720476794414)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(21118373401230054986312559037776481326385579291282972713217534432519345349176), uint256(5928106153795853981148997491594430501835099096792005950029918271482575875827)); } function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { @@ -219,11 +219,14 @@ contract Verifier { } function verifyUserData( - bytes memory _userData, + uint8[32] memory _userData, uint32[8] memory _memRootBefore, uint32[8] memory _memRootAfter ) public pure returns (uint256) { - bytes32 userData = sha256(_userData); + uint256 userData = 0; + for (uint256 i = 0; i < 32; i++) { + userData |= uint256(_userData[i]) << (8 * (31 - i)); + } uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index d3dcaa0a..e7de3fea 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -120,7 +120,7 @@ contract VerifierTest is Test { } - function test_ValidPublicInputs() public { + function test_ValidPublicInputs() public view { string memory root = vm.projectRoot(); string memory path1 = string.concat(root, "/verifier/snark_proof_with_public_inputs.json"); string memory json1 = vm.readFile(path1); @@ -151,7 +151,7 @@ contract VerifierTest is Test { bytes memory userdata = json.parseRaw(".userdata"); uint8[] memory dataU = abi.decode(userdata, ( uint8[])); - uint8[40] memory data; + uint8[32] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ data[i] = dataU[i]; } diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index d00794d2..6d8d906c 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -94,7 +94,7 @@ async fn main() -> Result<(), Box> { } } //check the userdata = hash(bing(public_input)) - if !check_public_input(&input.public_inputstream, &proof_result_path){ + if !check_public_inputs(&input.public_inputstream, &proof_result_path){ log::info!("public_inputs check false."); return Err("public_inputs check false.".into()); } @@ -261,20 +261,22 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Result { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); + //let mut buf = Vec::new(); + //bincode::serialize_into(&mut buf, &"0".into()).expect("serialization failed"); let input = ProverInput { elf: read(elf_path).unwrap(), - public_inputstream: "".into(), + public_inputstream: "0".into(), private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, }; - + log::info!("set_mem_alloc_vec_input, bincode(pulic_input): {:?} ", &input.public_inputstream); Ok(input) } @@ -288,7 +290,7 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result, } -fn check_public_input(public_inputstream: &Vec, file: &Path) -> bool { +fn check_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); From d6ea7374382f6c5cfd0a47621cb2c06a5996f51d Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 12:04:26 +0800 Subject: [PATCH 42/76] add sdk readme --- contracts/src/verifier.sol | 15 ++++---- contracts/verifier/public_inputs.json | 1 + .../snark_proof_with_public_inputs.json | 2 +- sdk/README.md | 37 +++++++++++++++++++ 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 contracts/verifier/public_inputs.json create mode 100644 sdk/README.md diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index d3332031..c077066c 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -169,14 +169,14 @@ contract Verifier { Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point(uint256(17893901124021210272638516546159505340327529763852882834791797783715120736836), uint256(13231467695658339900650768414253718422865673742208133343863421143503462113168)); - vk.beta = Pairing.G2Point([uint256(17887881584875332850937101588175122783613013000081586029290713558009208876939), uint256(5173405259660942311812575171887960300813259764306641231158685689101871053226)], [uint256(15421617451342223732374977604635144339106949166377272589278289042372210038178), uint256(4017392132424093353152388503575757440680696483308117139782333575602591537707)]); - vk.gamma = Pairing.G2Point([uint256(1290588609385786732164658248069445973512624381080875973387694511433780759175), uint256(12976305822780276357100851134930711058729218483183625693066553997404593228345)], [uint256(18550887073089422994810671436018260406270744572816708595449262508502728046582), uint256(9330344219985993709191598687338119285767947902106034962837030239408815004050)]); - vk.delta = Pairing.G2Point([uint256(2993044589807208422766651638941259454777038307900585129916365748825379882932), uint256(13132866685093967953405556380011883248024322097351146455602465105530729440852)], [uint256(9287962342478396674958016817894657236291812832698868341924590133380732632404), uint256(18863937240777198026935865424217363800063264366248119243760988932201425732600)]); + vk.alpha = Pairing.G1Point(uint256(17884030049888402582930522634680045980218895157770014621228517930841398898320), uint256(2350978535899614821646964216257925583253276541463344259269542551158701289458)); + vk.beta = Pairing.G2Point([uint256(1613759499451718303109587803609230044450728271243901381991450140652237262237), uint256(13176152694427271392344565871597696964044099349335530419158779717146828135094)], [uint256(3587653900410715185752588317605019576903016822498886862601803291780076308774), uint256(20601287378194133768066523789115568200465979402035628035404977132432467357789)]); + vk.gamma = Pairing.G2Point([uint256(9367952242408234285221783729033471524940989283006962053225244149428455161273), uint256(7341692840578528899339734093016329097643365248755296865998285247731552899784)], [uint256(5083545727173617485781990103162757481866716430312684043489529868657338510162), uint256(251757250577192221172663597492175466361299979636670411953274889875873821649)]); + vk.delta = Pairing.G2Point([uint256(11321077334858993603431720421195051874886836186661036445267064194511452530572), uint256(21099203371033535710339183927532133319646558665839856693481984766763470884236)], [uint256(10379415104622054804131240163245968089931503228849058778417768370707907929329), uint256(7040519220923418672760448075965385566691907473046025926221085987998536756747)]); vk.gamma_abc = new Pairing.G1Point[](3); - vk.gamma_abc[0] = Pairing.G1Point(uint256(19701327339338910895804292089025911598468663156478648178422701350645841255447), uint256(13110634886897473934651042825240895861319233163917095215468572038313498764478)); - vk.gamma_abc[1] = Pairing.G1Point(uint256(1494470184684746103182008386385284997483007826087210853641973278095564194395), uint256(15287893920487755265590292730618597525230562373919215600034902709720476794414)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(21118373401230054986312559037776481326385579291282972713217534432519345349176), uint256(5928106153795853981148997491594430501835099096792005950029918271482575875827)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(11590282650484566301715500144746794453025561559197120910037949363903919921012), uint256(7240186011313163626428393969053597927325358427138856190449585551638717303978)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(16851701958735909727177002414313170429493070482367808819516835198831546341851), uint256(4671176006742483473688334226493308749494496891643522400555743578691957865339)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(10297488576560864147262501047860710947275459565465770032438548255715216908509), uint256(8066008377168733143615531753854933596773707140958982340013362294116524105255)); } function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { @@ -227,7 +227,6 @@ contract Verifier { for (uint256 i = 0; i < 32; i++) { userData |= uint256(_userData[i]) << (8 * (31 - i)); } - uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); diff --git a/contracts/verifier/public_inputs.json b/contracts/verifier/public_inputs.json new file mode 100644 index 00000000..2fcd7ec0 --- /dev/null +++ b/contracts/verifier/public_inputs.json @@ -0,0 +1 @@ +{"roots_before":{"root":[3180437513,2269175112,3980585591,1336417138,3868846496,1961193041,1556548794,1348838317]},"roots_after":{"root":[2706326830,1994368865,3808172320,3128503797,3298745588,256064002,3617964268,2235077062]},"userdata":[205,238,119,148,81,114,187,79,174,93,76,42,82,63,163,67,203,194,239,73,4,34,143,215,115,199,136,187,186,216,30,187]} \ No newline at end of file diff --git a/contracts/verifier/snark_proof_with_public_inputs.json b/contracts/verifier/snark_proof_with_public_inputs.json index 3843da23..967a280d 100644 --- a/contracts/verifier/snark_proof_with_public_inputs.json +++ b/contracts/verifier/snark_proof_with_public_inputs.json @@ -1 +1 @@ -{"Proof":{"Ar":{"X":"8174031545225483383141418079564090791231490145781531926335276336175308863707","Y":"3834171856059451751436869467231246873520343021451160163408402538567178474219"},"Krs":{"X":"13586733060922750289506280282556626924213814817587389232592841141519550546903","Y":"7971169649759305355784664598000388441999417324143312928531542368266818198801"},"Bs":{"X":{"A0":"16244654992473864068434685396879843024965338706161139584337673486694680249857","A1":"6265690638221588196510439213899713108168752331944685238458496370594808062420"},"Y":{"A0":"12712710855225608154900745325328982591451728027910889754119239166082381964965","A1":"15879521449010124364490571648506817452456921365860115408511389594204873958337"}},"Commitments":[{"X":"10897131706311829408130705881694553087082057910633496700258666134136872262230","Y":"458953713510288618015750230832000155046368787668572283656604498671917216865"}],"CommitmentPok":{"X":"4415924133992939061388980411431402645937939123603889508394326920377549767070","Y":"7258264583926976292105251464579645788723690411202733258248780672362649452811"}},"PublicWitness":["13423602751184986938789540733258245323887402178343424404801648229902985960977","17447406357559998420445886886299190665098387339890379864001066407842671637468"]} \ No newline at end of file +{"Proof":{"Ar":{"X":"380389139670664228346429835363397936959266262575561256733130854616421745343","Y":"19156209365003795806898729186305313341923844860516109573197779111789090688116"},"Krs":{"X":"14096368118421777611601341046598132306944045905130776100298144347949233892781","Y":"3818850049370442866892652464549355809402800329540563878277586796825882789673"},"Bs":{"X":{"A0":"16635573058162699027400727829509385631463535654198496530586000395110937681885","A1":"15590919684665263177652713966060646609042846336004625358311643526176785454513"},"Y":{"A0":"13908723843963626809004421663016965388523262128563042640563065169680339331263","A1":"16483453801075872834471231751142165074139967714394404376201074823617106318783"}},"Commitments":[{"X":"10707343050040720365204172192469008186769210899621958825306536343450190258917","Y":"17330567961847116140801937850445531229459593799087641998548221271243376516135"}],"CommitmentPok":{"X":"8461861831056560312326076376135998305188728605913829393822715584293592182836","Y":"6376718635830295732500647064940352641237277939711867221765787087563093295931"}},"PublicWitness":["5281439405489989899303957828569599236164595298153787119569151355395055400401","15101336571963630961903841844730580325522246658124550161157693696374426144067"]} \ No newline at end of file diff --git a/sdk/README.md b/sdk/README.md new file mode 100644 index 00000000..f46cc049 --- /dev/null +++ b/sdk/README.md @@ -0,0 +1,37 @@ +# ZKM SDK usage + +## Use the libsnark + +1. The compile.sh in the path sdk/src/local/libsnark only supports X86_64 linux. + +``` +cd zkm-project-template/sdk/src/local/libsnark +./compile.sh +``` +If successful, it will generate the libsnark.so in sdk/src/local/libsnark/ + +2. To instruct your Rust environment on the location of the libsnark.so , you can set the LD_LIBRARY_PATH environment variable. For example: + +``` +export LD_LIBRARY_PATH=Your BASEDIR/zkm-project-template/sdk/src/local/libsnark:$LD_LIBRARY_PATH +``` + +3. Import the SDK + +Add the following dependency to your Cargo.toml file. +``` +[dependencies] +zkm-sdk = { path = "../sdk", features = ["snark"] } +``` + +## Don't use the libsnark + +1. Set the environment variable `NO_USE_SNARK=true` . + +2. Import the SDK + +Add the following dependency to your Cargo.toml file. +``` +[dependencies] +zkm-sdk = { path = "../sdk"} +``` From 49be14e19922d61cf5c8c817a8d759c791f465a9 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 12:11:52 +0800 Subject: [PATCH 43/76] update sdk's readme --- sdk/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/README.md b/sdk/README.md index f46cc049..e7099c48 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -8,7 +8,7 @@ cd zkm-project-template/sdk/src/local/libsnark ./compile.sh ``` -If successful, it will generate the libsnark.so in sdk/src/local/libsnark/ + If successful, it will generate the libsnark.so in sdk/src/local/libsnark/ 2. To instruct your Rust environment on the location of the libsnark.so , you can set the LD_LIBRARY_PATH environment variable. For example: @@ -18,10 +18,10 @@ export LD_LIBRARY_PATH=Your BASEDIR/zkm-project-template/sdk/src/local/libsnark: 3. Import the SDK -Add the following dependency to your Cargo.toml file. + Add the following dependency to your Cargo.toml file. ``` [dependencies] -zkm-sdk = { path = "../sdk", features = ["snark"] } +zkm-sdk = { git = "https://github.com/zkMIPS/zkm-project-template", branch = "main", features = ["snark"] } ``` ## Don't use the libsnark @@ -30,8 +30,8 @@ zkm-sdk = { path = "../sdk", features = ["snark"] } 2. Import the SDK -Add the following dependency to your Cargo.toml file. + Add the following dependency to your Cargo.toml file. ``` [dependencies] -zkm-sdk = { path = "../sdk"} +zkm-sdk = { git = "https://github.com/zkMIPS/zkm-project-template", branch = "main" } ``` From 4dca9a8a1f089de82e3a6ad044e8d0a47f333bb0 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 12:15:23 +0800 Subject: [PATCH 44/76] update sdk's readme --- sdk/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/README.md b/sdk/README.md index e7099c48..7b5828fb 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -8,7 +8,7 @@ cd zkm-project-template/sdk/src/local/libsnark ./compile.sh ``` - If successful, it will generate the libsnark.so in sdk/src/local/libsnark/ + If successful, it will generate the libsnark.so in sdk/src/local/libsnark/ 2. To instruct your Rust environment on the location of the libsnark.so , you can set the LD_LIBRARY_PATH environment variable. For example: @@ -18,8 +18,8 @@ export LD_LIBRARY_PATH=Your BASEDIR/zkm-project-template/sdk/src/local/libsnark: 3. Import the SDK - Add the following dependency to your Cargo.toml file. ``` +// Cargo.toml [dependencies] zkm-sdk = { git = "https://github.com/zkMIPS/zkm-project-template", branch = "main", features = ["snark"] } ``` @@ -30,8 +30,8 @@ zkm-sdk = { git = "https://github.com/zkMIPS/zkm-project-template", branch = "ma 2. Import the SDK - Add the following dependency to your Cargo.toml file. ``` +// Cargo.toml [dependencies] zkm-sdk = { git = "https://github.com/zkMIPS/zkm-project-template", branch = "main" } ``` From 313afed5c1a353f842c553797dba31b2ba273f44 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 16:40:31 +0800 Subject: [PATCH 45/76] add log for hash(bincong(public_inputs)) --- host-program/src/bin/zkm-prove.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 6d8d906c..5a36529a 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -320,6 +320,7 @@ fn check_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { let userdata = public_inputs.userdata; if userdata == output_hs { + log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); return true; } else { log::info!("public inputs is different. the file's is: {:?}, host's is :{:?} ", userdata, output_hs); From dfe8199016f18337c5c10dd7f1bd48c65fd5a864 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 18:31:33 +0800 Subject: [PATCH 46/76] update sdk --- contracts/test/verifier.t.sol | 3 ++- sdk/src/local/libsnark/contract.go | 9 +++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index e7de3fea..c385aad3 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -154,9 +154,10 @@ contract VerifierTest is Test { uint8[32] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ data[i] = dataU[i]; + console.log("--data[i=%d], value:%s", i, data[i]); } - uint256 returnNum = verifier.verifyUserData(data, rootb, roota); + uint256 returnNum = verifier.calculatePublicInput(userdata, rootb, roota); assert(returnNum == input[0]); diff --git a/sdk/src/local/libsnark/contract.go b/sdk/src/local/libsnark/contract.go index d2862142..2bdb6014 100644 --- a/sdk/src/local/libsnark/contract.go +++ b/sdk/src/local/libsnark/contract.go @@ -216,15 +216,12 @@ contract Verifier { } - function verifyUserData( - uint8[32] memory _userData, + function calculatePublicInput( + bytes memory _userData, uint32[8] memory _memRootBefore, uint32[8] memory _memRootAfter ) public pure returns (uint256) { - uint256 userData = 0; - for (uint256 i = 0; i < 32; i++) { - userData |= uint256(_userData[i]) << (8 * (31 - i)); - } + bytes32 userData = sha256(_userData); uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { From 59c4427b3271ab265947b51fd66cf0a7c9867bda Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 18:46:53 +0800 Subject: [PATCH 47/76] update contract --- contracts/test/verifier.t.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index c385aad3..d9b0cd36 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -150,12 +150,13 @@ contract VerifierTest is Test { } bytes memory userdata = json.parseRaw(".userdata"); - uint8[] memory dataU = abi.decode(userdata, ( uint8[])); + console.log("--userdata.len:%d", userdata.length) + /*uint8[] memory dataU = abi.decode(userdata, ( uint8[])); uint8[32] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ data[i] = dataU[i]; console.log("--data[i=%d], value:%s", i, data[i]); - } + }*/ uint256 returnNum = verifier.calculatePublicInput(userdata, rootb, roota); From 7125ff87b7bfa57d7c5177a5fcbbc954dd0fed51 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 18:49:40 +0800 Subject: [PATCH 48/76] update contract --- contracts/test/verifier.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index d9b0cd36..28a1ae97 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -150,7 +150,7 @@ contract VerifierTest is Test { } bytes memory userdata = json.parseRaw(".userdata"); - console.log("--userdata.len:%d", userdata.length) + console.log("--userdata.len:%d", userdata.length); /*uint8[] memory dataU = abi.decode(userdata, ( uint8[])); uint8[32] memory data; for (uint256 i = 0; i < dataU.length; i++ ){ From c29e4f36843d59b5c8fd6cf80ba9334a06965fa8 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 23:08:14 +0800 Subject: [PATCH 49/76] update contract --- contracts/test/verifier.t.sol | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index 28a1ae97..13a7085c 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -149,17 +149,17 @@ contract VerifierTest is Test { roota[i] = rootAf[i]; } - bytes memory userdata = json.parseRaw(".userdata"); - console.log("--userdata.len:%d", userdata.length); - /*uint8[] memory dataU = abi.decode(userdata, ( uint8[])); - uint8[32] memory data; - for (uint256 i = 0; i < dataU.length; i++ ){ - data[i] = dataU[i]; - console.log("--data[i=%d], value:%s", i, data[i]); - }*/ - - uint256 returnNum = verifier.calculatePublicInput(userdata, rootb, roota); + bytes userdata = json.parseRaw(".userdata"); + uint8[] memory dataU = abi.decode(userdata, ( uint8[])); + //bytes memory data = abi.encodePacked(dataU); + bytes memory data = new bytes(dataU.length); + for (uint256 i = 0; i < data.length; i++) { + data[i] = bytes1(dataU[i]); + //console.log("--data[i=%d], value:%s", i, uint8(data[i])); + } + + uint256 returnNum = verifier.calculatePublicInput(data, rootb, roota); assert(returnNum == input[0]); } From 4e4c11445abf0d9e860e0ce45ff02915fc06a2a5 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 23:09:46 +0800 Subject: [PATCH 50/76] update contract --- contracts/test/verifier.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index 13a7085c..da4ffff4 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -149,7 +149,7 @@ contract VerifierTest is Test { roota[i] = rootAf[i]; } - bytes userdata = json.parseRaw(".userdata"); + bytes memory userdata = json.parseRaw(".userdata"); uint8[] memory dataU = abi.decode(userdata, ( uint8[])); //bytes memory data = abi.encodePacked(dataU); bytes memory data = new bytes(dataU.length); From dd0cd6c4c8a367850b116abfb25725447cafc97e Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 23:28:07 +0800 Subject: [PATCH 51/76] update the host --- host-program/src/bin/zkm-prove.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 5a36529a..c25f9ae8 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -94,7 +94,7 @@ async fn main() -> Result<(), Box> { } } //check the userdata = hash(bing(public_input)) - if !check_public_inputs(&input.public_inputstream, &proof_result_path){ + if !replace_public_inputs(&input.public_inputstream, &proof_result_path){ log::info!("public_inputs check false."); return Err("public_inputs check false.".into()); } @@ -306,7 +306,7 @@ struct Roots { root: Vec, } -fn check_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { +fn replace_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); @@ -314,16 +314,30 @@ fn check_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - let public_inputs: PublicInputs = serde_json::from_str(&file_contents) + let mut public_inputs: PublicInputs = serde_json::from_str(&file_contents) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; if userdata == output_hs { log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); - return true; + } else { log::info!("public inputs is different. the file's is: {:?}, host's is :{:?} ", userdata, output_hs); return false; } + + // edit the userdata + if let Some(userdata) = public_inputs["userdata"].as_array_mut() { + *userdata = public_inputstream.as_array().unwrap().clone(); + } else { + panic!("userdata is not an array"); + } + + let mut fp = File::create(file).expect("Unable to create file"); + + // save the new contents + to_writer(&mut fp, &public_inputs) + .expect("Unable to write to public input file"); + return true; } From e54f1f9ed5ef49e0523c2e456df359fc4daa26c9 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Sun, 27 Oct 2024 23:35:29 +0800 Subject: [PATCH 52/76] update the host --- host-program/src/bin/zkm-prove.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index c25f9ae8..d002ba1e 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -8,6 +8,8 @@ use std::time::Instant; use zkm_sdk::{prover::ProverInput, ProverClient}; use hex; use std::fs::read; +use serde_json::{json, to_writer}; +use std::fs::File; #[tokio::main] async fn main() -> Result<(), Box> { @@ -328,8 +330,9 @@ fn replace_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { } // edit the userdata + let new_userdata = json!(public_inputstream); if let Some(userdata) = public_inputs["userdata"].as_array_mut() { - *userdata = public_inputstream.as_array().unwrap().clone(); + *userdata = new_userdata.as_array().unwrap().clone(); } else { panic!("userdata is not an array"); } From 37c93936a3e66ef5bd3308736d64fe4aebf4e814 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 08:51:39 +0800 Subject: [PATCH 53/76] update host --- host-program/src/bin/zkm-prove.rs | 41 +++++++++---------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index d002ba1e..908b1d46 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -81,22 +81,7 @@ async fn main() -> Result<(), Box> { } } //public inputs - let output_dir = "../contracts/verifier".to_string(); - let output_path = Path::new(&output_dir); - // - let proof_result_path = output_path.join("public_inputs.json"); - let mut f = file::new(&proof_result_path.to_string_lossy()); - match f.write(prover_result.public_values.as_slice()) { - Ok(bytes_written) => { - log::info!("public_inputs: successfully written {} bytes.", bytes_written); - } - Err(e) => { - log::info!("public_inputs: failed to write to file: {}", e); - return Err("public_inputs: failed to write to file".into()); - } - } - //check the userdata = hash(bing(public_input)) - if !replace_public_inputs(&input.public_inputstream, &proof_result_path){ + if !replace_public_inputs(&input.public_inputstream, &prover_result.public_values) { log::info!("public_inputs check false."); return Err("public_inputs check false.".into()); } @@ -308,15 +293,19 @@ struct Roots { root: Vec, } -fn replace_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { +fn replace_public_inputs(public_inputstream: &Vec, proof_public_inputs: &Vec) -> bool { + let output_dir = "../contracts/verifier".to_string(); + let output_path = Path::new(&output_dir); + let proof_result_path = output_path.join("public_inputs.json"); + let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); let output_hs: [u8; 32] = result_hs.into(); - let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); + //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - let mut public_inputs: PublicInputs = serde_json::from_str(&file_contents) + let mut public_inputs: PublicInputs = serde_json::from_str(&proof_public_inputs) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; @@ -329,16 +318,10 @@ fn replace_public_inputs(public_inputstream: &Vec, file: &Path) -> bool { return false; } - // edit the userdata - let new_userdata = json!(public_inputstream); - if let Some(userdata) = public_inputs["userdata"].as_array_mut() { - *userdata = new_userdata.as_array().unwrap().clone(); - } else { - panic!("userdata is not an array"); - } - - let mut fp = File::create(file).expect("Unable to create file"); - + //update userdata + public_inputs.userdata = public_inputstream; + let mut fp = File::create(proof_result_path).expect("Unable to create file"); + //let block_public_inputs = serde_json::json!({"public_inputs": block_proof.public_inputs,}); // save the new contents to_writer(&mut fp, &public_inputs) .expect("Unable to write to public input file"); From 886c61904f7932993056c7753d8552895ad53fa7 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 08:57:40 +0800 Subject: [PATCH 54/76] update host --- host-program/src/bin/zkm-prove.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 908b1d46..b4ba43ee 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -293,7 +293,7 @@ struct Roots { root: Vec, } -fn replace_public_inputs(public_inputstream: &Vec, proof_public_inputs: &Vec) -> bool { +fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec) -> bool { let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); let proof_result_path = output_path.join("public_inputs.json"); @@ -305,7 +305,7 @@ fn replace_public_inputs(public_inputstream: &Vec, proof_public_inputs: &Vec //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - let mut public_inputs: PublicInputs = serde_json::from_str(&proof_public_inputs) + let mut public_inputs: PublicInputs = serde_json::from_str(proof_public_inputs.as_slice()) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; From b63eb7d334429ecf8f5d244f6886d0a62e22e5b9 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 08:59:40 +0800 Subject: [PATCH 55/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index b4ba43ee..17526947 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -81,7 +81,7 @@ async fn main() -> Result<(), Box> { } } //public inputs - if !replace_public_inputs(&input.public_inputstream, &prover_result.public_values) { + if !replace_public_inputs(input.public_inputstream, &prover_result.public_values) { log::info!("public_inputs check false."); return Err("public_inputs check false.".into()); } From ce17046879253cded55047a527ad0faf05512550 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 09:16:01 +0800 Subject: [PATCH 56/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 17526947..ee15e441 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -305,7 +305,7 @@ fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec< //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - let mut public_inputs: PublicInputs = serde_json::from_str(proof_public_inputs.as_slice()) + let mut public_inputs: PublicInputs = serde_json::from_slice(proof_public_inputs.as_bytes()) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; From 926f7d62c43d54d0a56c1fd711954e38dc55239f Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 09:19:32 +0800 Subject: [PATCH 57/76] update host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index ee15e441..cd31a37f 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -305,7 +305,7 @@ fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec< //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - let mut public_inputs: PublicInputs = serde_json::from_slice(proof_public_inputs.as_bytes()) + let mut public_inputs: PublicInputs = serde_json::from_slice(proof_public_inputs.as_slice().as_bytes()) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; From c05a962a205c314be7fa8b12ce6c28dc668b268c Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 09:26:16 +0800 Subject: [PATCH 58/76] update host --- host-program/src/bin/zkm-prove.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index cd31a37f..454d581d 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -304,8 +304,8 @@ fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec< let output_hs: [u8; 32] = result_hs.into(); //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); - - let mut public_inputs: PublicInputs = serde_json::from_slice(proof_public_inputs.as_slice().as_bytes()) + let slice_bt: &[u8] = &proof_public_inputs; + let mut public_inputs: PublicInputs = serde_json::from_slice(slice_bt) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; From b8801af917add5c5f958639ac4b0ee10a6334656 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 10:15:40 +0800 Subject: [PATCH 59/76] update host --- contracts/src/verifier.sol | 24 +++++++++---------- contracts/verifier/public_inputs.json | 2 +- .../snark_proof_with_public_inputs.json | 2 +- host-program/src/bin/zkm-prove.rs | 12 ++++------ 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index c077066c..fc2a7349 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -169,14 +169,14 @@ contract Verifier { Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point(uint256(17884030049888402582930522634680045980218895157770014621228517930841398898320), uint256(2350978535899614821646964216257925583253276541463344259269542551158701289458)); - vk.beta = Pairing.G2Point([uint256(1613759499451718303109587803609230044450728271243901381991450140652237262237), uint256(13176152694427271392344565871597696964044099349335530419158779717146828135094)], [uint256(3587653900410715185752588317605019576903016822498886862601803291780076308774), uint256(20601287378194133768066523789115568200465979402035628035404977132432467357789)]); - vk.gamma = Pairing.G2Point([uint256(9367952242408234285221783729033471524940989283006962053225244149428455161273), uint256(7341692840578528899339734093016329097643365248755296865998285247731552899784)], [uint256(5083545727173617485781990103162757481866716430312684043489529868657338510162), uint256(251757250577192221172663597492175466361299979636670411953274889875873821649)]); - vk.delta = Pairing.G2Point([uint256(11321077334858993603431720421195051874886836186661036445267064194511452530572), uint256(21099203371033535710339183927532133319646558665839856693481984766763470884236)], [uint256(10379415104622054804131240163245968089931503228849058778417768370707907929329), uint256(7040519220923418672760448075965385566691907473046025926221085987998536756747)]); + vk.alpha = Pairing.G1Point(uint256(3822932099788692543893615615066972300406373947723030294258252002713585496403), uint256(8173193854899062432314554250991981779941095778853153816262363990613415549438)); + vk.beta = Pairing.G2Point([uint256(638097608883083313836943541679313429707282911426954640699276722576387081317), uint256(3751723829816740730360448481115251256231372311662960756727455782541092751897)], [uint256(11410702092984585592593659204564657716505484111280606030738425543269372552414), uint256(6736124019075283073386797712805716195791904848505873580520536986689099483753)]); + vk.gamma = Pairing.G2Point([uint256(5630985036407536091973365882144041701080856810089086912097199130278505300476), uint256(141269356882722294339606379867452136343405487604647939844944847450816904784)], [uint256(18809802681208979768328764137493742888362092447107132123188937200472443571395), uint256(19449249420300963896022532575435844361654327173103650152659708471111966573222)]); + vk.delta = Pairing.G2Point([uint256(10743744079075485890832820142653878510090993325927655607276366849545485999117), uint256(20563041352001208858608214828504098903816500023729058205172300285213774279156)], [uint256(21710866298309228313317221133542251571423139001776712080498468905281372930623), uint256(19858325399274673578321878556365008726898844025397576551348176974037027887616)]); vk.gamma_abc = new Pairing.G1Point[](3); - vk.gamma_abc[0] = Pairing.G1Point(uint256(11590282650484566301715500144746794453025561559197120910037949363903919921012), uint256(7240186011313163626428393969053597927325358427138856190449585551638717303978)); - vk.gamma_abc[1] = Pairing.G1Point(uint256(16851701958735909727177002414313170429493070482367808819516835198831546341851), uint256(4671176006742483473688334226493308749494496891643522400555743578691957865339)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(10297488576560864147262501047860710947275459565465770032438548255715216908509), uint256(8066008377168733143615531753854933596773707140958982340013362294116524105255)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(2993240122707847476044610966790373125918126867295952817555283201864988081296), uint256(9392632542050338609286308498693880942976264842132074203992101574948415893432)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(20299414895746854112827167089405448748114636668365933695934345955174767233644), uint256(4190649257444976759979904796132450365543389494669187575482379914398880386318)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(19603685552297693441087963249855316257621121917070011387955799042838432008674), uint256(17931964434033440369121316382740485127350295117622696658665726645932862034275)); } function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { @@ -218,15 +218,13 @@ contract Verifier { } - function verifyUserData( - uint8[32] memory _userData, + function calculatePublicInput( + bytes memory _userData, uint32[8] memory _memRootBefore, uint32[8] memory _memRootAfter ) public pure returns (uint256) { - uint256 userData = 0; - for (uint256 i = 0; i < 32; i++) { - userData |= uint256(_userData[i]) << (8 * (31 - i)); - } + bytes32 userData = sha256(_userData); + uint256 memRootBefore = 0; for (uint256 i = 0; i < 8; i++) { memRootBefore |= uint256(_memRootBefore[i]) << (32 * (7 - i)); diff --git a/contracts/verifier/public_inputs.json b/contracts/verifier/public_inputs.json index 2fcd7ec0..bb735bb4 100644 --- a/contracts/verifier/public_inputs.json +++ b/contracts/verifier/public_inputs.json @@ -1 +1 @@ -{"roots_before":{"root":[3180437513,2269175112,3980585591,1336417138,3868846496,1961193041,1556548794,1348838317]},"roots_after":{"root":[2706326830,1994368865,3808172320,3128503797,3298745588,256064002,3617964268,2235077062]},"userdata":[205,238,119,148,81,114,187,79,174,93,76,42,82,63,163,67,203,194,239,73,4,34,143,215,115,199,136,187,186,216,30,187]} \ No newline at end of file +{"roots_before":{"root":[3267150326,197883508,687930418,1173067998,1371010302,2022666653,3458911669,3998222321]},"roots_after":{"root":[587761475,2912322362,3375527962,649710565,1661363813,3237642605,3403841378,2784312502]},"userdata":[1,2,3,4,5,6,7,8,9,10,17,255,34,17,255,255,51,34,17,0,255,255,255,255,85,68,51,34,17,0,0,0,255,255,255,255,255,255,255,255,32,0,0,0,0,0,0,0,113,30,150,9,51,158,146,176,61,220,10,33,24,39,219,164,33,243,143,158,216,185,216,6,225,255,221,140,21,255,160,61,2,0,0,0,6,0,0,0,0,0,0,0,119,111,114,108,100,33]} \ No newline at end of file diff --git a/contracts/verifier/snark_proof_with_public_inputs.json b/contracts/verifier/snark_proof_with_public_inputs.json index 967a280d..c8a9419d 100644 --- a/contracts/verifier/snark_proof_with_public_inputs.json +++ b/contracts/verifier/snark_proof_with_public_inputs.json @@ -1 +1 @@ -{"Proof":{"Ar":{"X":"380389139670664228346429835363397936959266262575561256733130854616421745343","Y":"19156209365003795806898729186305313341923844860516109573197779111789090688116"},"Krs":{"X":"14096368118421777611601341046598132306944045905130776100298144347949233892781","Y":"3818850049370442866892652464549355809402800329540563878277586796825882789673"},"Bs":{"X":{"A0":"16635573058162699027400727829509385631463535654198496530586000395110937681885","A1":"15590919684665263177652713966060646609042846336004625358311643526176785454513"},"Y":{"A0":"13908723843963626809004421663016965388523262128563042640563065169680339331263","A1":"16483453801075872834471231751142165074139967714394404376201074823617106318783"}},"Commitments":[{"X":"10707343050040720365204172192469008186769210899621958825306536343450190258917","Y":"17330567961847116140801937850445531229459593799087641998548221271243376516135"}],"CommitmentPok":{"X":"8461861831056560312326076376135998305188728605913829393822715584293592182836","Y":"6376718635830295732500647064940352641237277939711867221765787087563093295931"}},"PublicWitness":["5281439405489989899303957828569599236164595298153787119569151355395055400401","15101336571963630961903841844730580325522246658124550161157693696374426144067"]} \ No newline at end of file +{"Proof":{"Ar":{"X":"7152265182515544811404200942955825289871480326788808634721833619683429072600","Y":"421910172777853342725655560572148790240445475350859621197904025975889476258"},"Krs":{"X":"18903274688905334984006626550012050148972988984776811060040395095097371801106","Y":"1892438252513241782886357191917101565595850033458115510425395577012656154261"},"Bs":{"X":{"A0":"18828379993314989082808439842142936064599486299276536667074181265362564015000","A1":"19511528122623550345884585273362980032596984796248419403601567289875881625634"},"Y":{"A0":"15651257158815572031823779177417104081001104341486638393020009168193604582053","A1":"2332887965337842780775638782964937902784175526912287925303669202690177815700"}},"Commitments":[{"X":"13820925056344096846974471839515748846317210075276109141187919870767461357114","Y":"1709261015561631143008291383655296057555729137911606288713788357143462112689"}],"CommitmentPok":{"X":"14574155334723611097387245430848033035970851381089677856130096815552993903675","Y":"17805018758484818579639056365465149897695653165411044435874436467063017387206"}},"PublicWitness":["7984930538653253192668885850965508449366240307702777824111767427942369281486","19023164871427544894963908473273260002810082077794178830358020405286267935921"]} \ No newline at end of file diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 454d581d..f361680b 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -8,7 +8,7 @@ use std::time::Instant; use zkm_sdk::{prover::ProverInput, ProverClient}; use hex; use std::fs::read; -use serde_json::{json, to_writer}; +use serde_json::to_writer; use std::fs::File; #[tokio::main] @@ -303,25 +303,21 @@ fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec< let result_hs = hasher.finalize(); let output_hs: [u8; 32] = result_hs.into(); - //let file_contents = std::fs::read_to_string(file).expect("Failed to read file"); let slice_bt: &[u8] = &proof_public_inputs; let mut public_inputs: PublicInputs = serde_json::from_slice(slice_bt) .expect("Failed to parse JSON"); let userdata = public_inputs.userdata; - if userdata == output_hs { - log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); - + log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); } else { - log::info!("public inputs is different. the file's is: {:?}, host's is :{:?} ", userdata, output_hs); + log::info!("public inputs is different. the sdk's is: {:?}, host's is :{:?} ", userdata, output_hs); return false; } - //update userdata + //update userdata with bincode(public_inputs). The old userdata is hash(bincode(pulic_inputs)). public_inputs.userdata = public_inputstream; let mut fp = File::create(proof_result_path).expect("Unable to create file"); - //let block_public_inputs = serde_json::json!({"public_inputs": block_proof.public_inputs,}); // save the new contents to_writer(&mut fp, &public_inputs) .expect("Unable to write to public input file"); From 6ecdf6e8b0f9bf81ffbdf73c70525bcf15e22aef Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 10:22:44 +0800 Subject: [PATCH 60/76] update contract --- contracts/src/verifier.sol | 14 +++++++------- contracts/verifier/public_inputs.json | 2 +- .../verifier/snark_proof_with_public_inputs.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/src/verifier.sol b/contracts/src/verifier.sol index fc2a7349..271232cd 100644 --- a/contracts/src/verifier.sol +++ b/contracts/src/verifier.sol @@ -169,14 +169,14 @@ contract Verifier { Pairing.G1Point c; } function verifyingKey() pure internal returns (VerifyingKey memory vk) { - vk.alpha = Pairing.G1Point(uint256(3822932099788692543893615615066972300406373947723030294258252002713585496403), uint256(8173193854899062432314554250991981779941095778853153816262363990613415549438)); - vk.beta = Pairing.G2Point([uint256(638097608883083313836943541679313429707282911426954640699276722576387081317), uint256(3751723829816740730360448481115251256231372311662960756727455782541092751897)], [uint256(11410702092984585592593659204564657716505484111280606030738425543269372552414), uint256(6736124019075283073386797712805716195791904848505873580520536986689099483753)]); - vk.gamma = Pairing.G2Point([uint256(5630985036407536091973365882144041701080856810089086912097199130278505300476), uint256(141269356882722294339606379867452136343405487604647939844944847450816904784)], [uint256(18809802681208979768328764137493742888362092447107132123188937200472443571395), uint256(19449249420300963896022532575435844361654327173103650152659708471111966573222)]); - vk.delta = Pairing.G2Point([uint256(10743744079075485890832820142653878510090993325927655607276366849545485999117), uint256(20563041352001208858608214828504098903816500023729058205172300285213774279156)], [uint256(21710866298309228313317221133542251571423139001776712080498468905281372930623), uint256(19858325399274673578321878556365008726898844025397576551348176974037027887616)]); + vk.alpha = Pairing.G1Point(uint256(4982840985977509480812696446748055946734515908139415073223361588673361263746), uint256(8712150437299664934793155504800743464028309588231747319088718049163058504378)); + vk.beta = Pairing.G2Point([uint256(14644847491060364109480040637526345932168168023485200270880573723303997401430), uint256(6775420302490570505302079782352798472495089461815319569111358423389688511258)], [uint256(14768767847587643701090831715662879751600874573066085500496772126134950836854), uint256(240437156120544871266428605167538236442109586608972230783814470863300438827)]); + vk.gamma = Pairing.G2Point([uint256(7262210795700233819627126529411641564304118216220826693309624003838669118557), uint256(17968365474447290552942753834244607172785018962023497431395501373062750769088)], [uint256(20236512178005743879072938503706731346788846934024160084859279719418824582905), uint256(21482178354445909306461993182701005930359221724872119005518668975905484364020)]); + vk.delta = Pairing.G2Point([uint256(1429989016433393031270355281347460626641102112248176356804534174800828327904), uint256(8658924682454037491287711579335854754277930237325482595536833570576568002546)], [uint256(6666320311792476308297678246169834119738677804991811576981285704354255423405), uint256(17225692118650814592746316412064011994785803400777901648568774307469594781516)]); vk.gamma_abc = new Pairing.G1Point[](3); - vk.gamma_abc[0] = Pairing.G1Point(uint256(2993240122707847476044610966790373125918126867295952817555283201864988081296), uint256(9392632542050338609286308498693880942976264842132074203992101574948415893432)); - vk.gamma_abc[1] = Pairing.G1Point(uint256(20299414895746854112827167089405448748114636668365933695934345955174767233644), uint256(4190649257444976759979904796132450365543389494669187575482379914398880386318)); - vk.gamma_abc[2] = Pairing.G1Point(uint256(19603685552297693441087963249855316257621121917070011387955799042838432008674), uint256(17931964434033440369121316382740485127350295117622696658665726645932862034275)); + vk.gamma_abc[0] = Pairing.G1Point(uint256(658688055398885923946239798304191006171364785528162641914664772011403932614), uint256(16627251529150893079116090723842709162209343897331682563491207763133727729949)); + vk.gamma_abc[1] = Pairing.G1Point(uint256(17533717551801610611963983357502065482287525214778437011670149217505961606170), uint256(6945694641534038337990336721326850919360048910929658061971010569206119522556)); + vk.gamma_abc[2] = Pairing.G1Point(uint256(13525035671734603893608933762469327796081853408677259439579741391855268192799), uint256(10191107221118371707862430176671506476078342119397430622030687829974682775664)); } function verify(uint[2] memory input, Proof memory proof, uint[2] memory proof_commitment) public view returns (uint) { diff --git a/contracts/verifier/public_inputs.json b/contracts/verifier/public_inputs.json index bb735bb4..46d8fb89 100644 --- a/contracts/verifier/public_inputs.json +++ b/contracts/verifier/public_inputs.json @@ -1 +1 @@ -{"roots_before":{"root":[3267150326,197883508,687930418,1173067998,1371010302,2022666653,3458911669,3998222321]},"roots_after":{"root":[587761475,2912322362,3375527962,649710565,1661363813,3237642605,3403841378,2784312502]},"userdata":[1,2,3,4,5,6,7,8,9,10,17,255,34,17,255,255,51,34,17,0,255,255,255,255,85,68,51,34,17,0,0,0,255,255,255,255,255,255,255,255,32,0,0,0,0,0,0,0,113,30,150,9,51,158,146,176,61,220,10,33,24,39,219,164,33,243,143,158,216,185,216,6,225,255,221,140,21,255,160,61,2,0,0,0,6,0,0,0,0,0,0,0,119,111,114,108,100,33]} \ No newline at end of file +{"roots_before":{"root":[3180437513,2269175112,3980585591,1336417138,3868846496,1961193041,1556548794,1348838317]},"roots_after":{"root":[2706326830,1994368865,3808172320,3128503797,3298745588,256064002,3617964268,2235077062]},"userdata":[210,10,0,0,0,0,0,0,123,34,95,105,110,102,111,34,58,110,117,108,108,44,34,99,104,97,105,110,95,105,100,34,58,51,50,51,56,50,44,34,101,110,118,34,58,123,34,99,117,114,114,101,110,116,67,111,105,110,98,97,115,101,34,58,34,48,120,57,49,51,57,101,101,53,52,53,48,102,100,51,98,48,100,48,55,101,51,97,54,50,51,101,101,53,101,51,100,102,48,49,51,98,102,100,101,97,55,34,44,34,99,117,114,114,101,110,116,68,105,102,102,105,99,117,108,116,121,34,58,34,48,120,48,34,44,34,99,117,114,114,101,110,116,71,97,115,76,105,109,105,116,34,58,34,48,120,49,99,57,99,51,56,48,34,44,34,99,117,114,114,101,110,116,78,117,109,98,101,114,34,58,34,48,120,51,48,99,48,100,34,44,34,99,117,114,114,101,110,116,84,105,109,101,115,116,97,109,112,34,58,34,48,120,54,54,101,102,51,101,52,101,34,44,34,99,117,114,114,101,110,116,66,97,115,101,70,101,101,34,58,34,48,120,55,34,44,34,112,114,101,118,105,111,117,115,72,97,115,104,34,58,34,48,120,54,102,99,99,56,51,54,50,55,49,100,100,55,101,49,54,49,97,55,53,57,97,50,102,50,102,49,54,99,55,48,57,100,52,49,49,50,101,52,101,53,56,51,53,55,98,99,97,98,52,49,55,102,54,54,54,102,102,56,55,53,55,51,57,34,44,34,99,117,114,114,101,110,116,82,97,110,100,111,109,34,58,34,48,120,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,34,44,34,99,117,114,114,101,110,116,66,101,97,99,111,110,82,111,111,116,34,58,34,48,120,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,34,44,34,99,117,114,114,101,110,116,87,105,116,104,100,114,97,119,97,108,115,82,111,111,116,34,58,34,48,120,53,54,101,56,49,102,49,55,49,98,99,99,53,53,97,54,102,102,56,51,52,53,101,54,57,50,99,48,102,56,54,101,53,98,52,56,101,48,49,98,57,57,54,99,97,100,99,48,48,49,54,50,50,102,98,53,101,51,54,51,98,52,50,49,34,44,34,112,97,114,101,110,116,66,108,111,98,71,97,115,85,115,101,100,34,58,34,48,120,53,50,48,56,34,44,34,112,97,114,101,110,116,69,120,99,101,115,115,66,108,111,98,71,97,115,34,58,34,48,120,53,50,48,56,34,125,44,34,112,114,101,34,58,123,34,48,120,49,56,100,98,55,100,102,53,102,51,48,97,97,102,54,98,52,102,49,53,98,50,52,50,99,48,55,99,101,54,50,102,102,97,50,102,51,51,49,49,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,49,99,57,100,49,50,48,49,48,53,54,48,48,48,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,48,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,44,34,48,120,57,49,51,57,101,101,53,52,53,48,102,100,51,98,48,100,48,55,101,51,97,54,50,51,101,101,53,101,51,100,102,48,49,51,98,102,100,101,97,55,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,50,98,48,55,55,101,98,97,99,53,102,50,53,50,100,49,101,99,99,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,102,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,44,34,48,120,97,52,56,50,97,101,55,100,55,53,51,98,51,51,99,98,48,54,102,100,54,56,55,50,52,52,51,101,57,48,50,99,98,54,98,101,101,53,57,50,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,49,48,57,56,51,100,54,101,48,97,48,102,57,51,52,56,99,53,48,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,49,97,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,125,44,34,112,111,115,116,34,58,123,34,83,104,97,110,103,104,97,105,34,58,91,123,34,101,120,112,101,99,116,69,120,99,101,112,116,105,111,110,34,58,110,117,108,108,44,34,105,110,100,101,120,101,115,34,58,123,34,100,97,116,97,34,58,48,44,34,103,97,115,34,58,48,44,34,118,97,108,117,101,34,58,48,125,44,34,104,97,115,104,34,58,34,48,120,99,102,48,54,54,53,99,98,102,97,50,56,57,48,97,57,102,101,48,53,100,101,99,48,50,55,102,102,99,97,53,48,102,50,50,57,97,52,49,52,99,54,49,48,51,48,48,52,97,49,49,55,102,49,100,102,101,57,53,50,99,52,53,98,34,44,34,112,111,115,116,83,116,97,116,101,34,58,123,34,48,120,49,56,100,98,55,100,102,53,102,51,48,97,97,102,54,98,52,102,49,53,98,50,52,50,99,48,55,99,101,54,50,102,102,97,50,102,51,51,49,49,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,49,100,53,101,99,97,101,101,53,98,98,48,48,48,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,48,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,44,34,48,120,97,52,56,50,97,101,55,100,55,53,51,98,51,51,99,98,48,54,102,100,54,56,55,50,52,52,51,101,57,48,50,99,98,54,98,101,101,53,57,50,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,49,48,57,56,51,100,53,102,56,98,53,50,56,99,55,53,101,49,56,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,49,98,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,44,34,48,120,57,49,51,57,101,101,53,52,53,48,102,100,51,98,48,100,48,55,101,51,97,54,50,51,101,101,53,101,51,100,102,48,49,51,98,102,100,101,97,55,34,58,123,34,98,97,108,97,110,99,101,34,58,34,48,120,50,98,48,55,55,101,98,100,50,57,50,48,56,52,49,98,101,99,99,34,44,34,99,111,100,101,34,58,34,48,120,34,44,34,110,111,110,99,101,34,58,34,48,120,102,34,44,34,115,116,111,114,97,103,101,34,58,123,125,125,125,44,34,108,111,103,115,34,58,34,48,120,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,34,44,34,116,120,98,121,116,101,115,34,58,34,48,120,55,98,50,50,54,51,54,49,54,99,54,99,54,53,55,50,50,50,51,97,50,50,51,48,55,56,54,49,51,52,51,56,51,50,54,49,54,53,51,55,54,52,51,55,51,53,51,51,54,50,51,51,51,51,54,51,54,50,51,48,51,54,54,54,54,52,51,54,51,56,51,55,51,50,51,52,51,52,51,51,54,53,51,57,51,48,51,50,54,51,54,50,51,54,54,50,54,53,54,53,51,53,51,57,51,50,50,50,50,99,50,50,54,55,54,49,55,51,53,102,54,99,54,57,54,100,54,57,55,52,50,50,51,97,51,50,51,49,51,48,51,48,51,48,50,99,50,50,54,55,54,49,55,51,53,102,55,48,55,50,54,57,54,51,54,53,50,50,51,97,50,50,51,48,55,56,51,55,51,55,51,51,51,53,51,57,51,52,51,48,51,55,50,50,50,99,50,50,55,52,55,50,54,49,54,101,55,51,54,49,54,51,55,52,53,102,55,52,54,102,50,50,51,97,50,50,51,48,55,56,51,49,51,56,54,52,54,50,51,55,54,52,54,54,51,53,54,54,51,51,51,48,54,49,54,49,54,54,51,54,54,50,51,52,54,54,51,49,51,53,54,50,51,50,51,52,51,50,54,51,51,48,51,55,54,51,54,53,51,54,51,50,54,54,54,54,54,49,51,50,54,54,51,51,51,51,51,49,51,49,50,50,50,99,50,50,55,54,54,49,54,99,55,53,54,53,50,50,51,97,50,50,51,48,55,56,54,51,51,49,54,50,51,56,54,53,54,52,51,53,51,54,51,53,51,48,51,48,51,48,50,50,50,99,50,50,54,52,54,49,55,52,54,49,50,50,51,97,50,50,51,48,55,56,50,50,50,99,50,50,54,101,54,102,54,101,54,51,54,53,50,50,51,97,51,50,51,54,50,99,50,50,54,51,54,56,54,49,54,57,54,101,53,102,54,57,54,52,50,50,51,97,51,51,51,50,51,51,51,56,51,50,50,99,50,50,54,49,54,51,54,51,54,53,55,51,55,51,53,102,54,99,54,57,55,51,55,52,50,50,51,97,53,98,53,100,50,99,50,50,54,55,54,49,55,51,53,102,55,48,55,50,54,57,54,102,55,50,54,57,55,52,55,57,53,102,54,54,54,53,54,53,50,50,51,97,50,50,51,48,55,56,51,55,51,55,51,51,51,53,51,57,51,52,51,48,51,48,50,50,50,99,50,50,54,50,54,99,54,102,54,50,53,102,54,56,54,49,55,51,54,56,54,53,55,51,50,50,51,97,53,98,53,100,50,99,50,50,54,100,54,49,55,56,53,102,54,54,54,53,54,53,53,102,55,48,54,53,55,50,53,102,54,50,54,99,54,102,54,50,53,102,54,55,54,49,55,51,50,50,51,97,54,101,55,53,54,99,54,99,50,99,50,50,54,49,55,53,55,52,54,56,54,102,55,50,54,57,55,97,54,49,55,52,54,57,54,102,54,101,53,102,54,99,54,57,55,51,55,52,50,50,51,97,54,101,55,53,54,99,54,99,55,100,34,125,93,125,44,34,116,114,97,110,115,97,99,116,105,111,110,34,58,123,34,100,97,116,97,34,58,91,34,48,120,34,93,44,34,103,97,115,76,105,109,105,116,34,58,91,34,48,120,53,50,48,56,34,93,44,34,103,97,115,80,114,105,99,101,34,58,34,48,120,55,55,51,53,57,52,48,55,34,44,34,110,111,110,99,101,34,58,34,48,120,49,97,34,44,34,115,101,99,114,101,116,75,101,121,34,58,34,48,120,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,34,44,34,115,101,110,100,101,114,34,58,34,48,120,97,52,56,50,97,101,55,100,55,53,51,98,51,51,99,98,48,54,102,100,54,56,55,50,52,52,51,101,57,48,50,99,98,54,98,101,101,53,57,50,34,44,34,116,111,34,58,34,48,120,49,56,100,98,55,100,102,53,102,51,48,97,97,102,54,98,52,102,49,53,98,50,52,50,99,48,55,99,101,54,50,102,102,97,50,102,51,51,49,49,34,44,34,118,97,108,117,101,34,58,91,34,48,120,99,49,98,56,101,100,53,54,53,48,48,48,34,93,44,34,109,97,120,70,101,101,80,101,114,71,97,115,34,58,34,48,120,49,55,52,56,55,54,101,56,48,48,34,44,34,109,97,120,80,114,105,111,114,105,116,121,70,101,101,80,101,114,71,97,115,34,58,34,48,120,55,55,51,53,57,52,48,48,34,44,34,97,99,99,101,115,115,76,105,115,116,115,34,58,91,91,93,93,44,34,98,108,111,98,86,101,114,115,105,111,110,101,100,72,97,115,104,101,115,34,58,91,93,44,34,109,97,120,70,101,101,80,101,114,66,108,111,98,71,97,115,34,58,110,117,108,108,125,44,34,111,117,116,34,58,110,117,108,108,125]} \ No newline at end of file diff --git a/contracts/verifier/snark_proof_with_public_inputs.json b/contracts/verifier/snark_proof_with_public_inputs.json index c8a9419d..68050c48 100644 --- a/contracts/verifier/snark_proof_with_public_inputs.json +++ b/contracts/verifier/snark_proof_with_public_inputs.json @@ -1 +1 @@ -{"Proof":{"Ar":{"X":"7152265182515544811404200942955825289871480326788808634721833619683429072600","Y":"421910172777853342725655560572148790240445475350859621197904025975889476258"},"Krs":{"X":"18903274688905334984006626550012050148972988984776811060040395095097371801106","Y":"1892438252513241782886357191917101565595850033458115510425395577012656154261"},"Bs":{"X":{"A0":"18828379993314989082808439842142936064599486299276536667074181265362564015000","A1":"19511528122623550345884585273362980032596984796248419403601567289875881625634"},"Y":{"A0":"15651257158815572031823779177417104081001104341486638393020009168193604582053","A1":"2332887965337842780775638782964937902784175526912287925303669202690177815700"}},"Commitments":[{"X":"13820925056344096846974471839515748846317210075276109141187919870767461357114","Y":"1709261015561631143008291383655296057555729137911606288713788357143462112689"}],"CommitmentPok":{"X":"14574155334723611097387245430848033035970851381089677856130096815552993903675","Y":"17805018758484818579639056365465149897695653165411044435874436467063017387206"}},"PublicWitness":["7984930538653253192668885850965508449366240307702777824111767427942369281486","19023164871427544894963908473273260002810082077794178830358020405286267935921"]} \ No newline at end of file +{"Proof":{"Ar":{"X":"8728298070179409622671565494854414149764009252234770122552245295145748114760","Y":"13858057945550283848621204904214782713729779258955795681562397624675504543985"},"Krs":{"X":"20997660049140304173225637533332884179740846142089972818360000401204903854894","Y":"20753437672678271153299806809320762161121619327521260957221119302819973168817"},"Bs":{"X":{"A0":"3248938711780232333037827081553026435955782797126789609156631417061676001531","A1":"17503500096532406105577853167534849291027064713124894782037534009784190363382"},"Y":{"A0":"7651501675424767975396069876156119281192510645562446873886439818347912540845","A1":"1172973244327967912276078042774112866438916148782280438085955943730115670883"}},"Commitments":[{"X":"5560971376371596839091076966141222584791168032387407288654728348722054525189","Y":"11224943278815009758323907625780507294123965417950750137549104992567683189940"}],"CommitmentPok":{"X":"7613071802064773139672851697311193558206731447564551045030626068345706240434","Y":"17945904483608948640532351521618851519437605249090407298449250485970632473583"}},"PublicWitness":["5281439405489989899303957828569599236164595298153787119569151355395055400401","11970360391326087476580906000059380021614438020894280187376697902994770669641"]} \ No newline at end of file From 5d9e08a40f90d69d7e0e8e30edbc641e50db5803 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 18:26:32 +0800 Subject: [PATCH 61/76] update the readme and host --- README.md | 5 ++-- contracts/test/verifier.t.sol | 3 --- host-program/src/bin/zkm-prove.rs | 45 +++++++++++++++---------------- sdk/README.md | 2 +- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 6f5bd1d5..709d9bf1 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,7 @@ cd zkm-project-template/host-program > 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 four guest programs(sha2-rust, sha2-go, mem-alloc-vec,revme), each capable of generating a SNARK proof on a machine -equipped with an AMD EPYC 7R13 processor and 246GB of memory. The following will use sha2-rust and revme as an example to demonstrate local and network proofs. +> 3. There are four guest programs(sha2-rust, sha2-go, mem-alloc-vec,revme). The following will use sha2-rust and revme 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. @@ -298,7 +297,7 @@ The revme guest program takes a block data as input and its running is as same a #### Generating the public input about a specific block > [!NOTE] -> The local node is the [GOAT](https://goat.network) test chain in the following example. You must use the Eth-Compatible local node. +> The local node connects ZKM test chain in the following example. You must use the Eth-Compatible local node. ```sh cd ~ diff --git a/contracts/test/verifier.t.sol b/contracts/test/verifier.t.sol index da4ffff4..d4b5ae6b 100644 --- a/contracts/test/verifier.t.sol +++ b/contracts/test/verifier.t.sol @@ -138,7 +138,6 @@ contract VerifierTest is Test { uint32[] memory rootBe = abi.decode(rootBefore, ( uint32[])); uint32[8] memory rootb; for (uint256 i = 0; i < rootBe.length; i++ ){ - //console.log("--before[i=%d], value:%s", i, rootBe[i]); rootb[i] = rootBe[i]; } @@ -151,11 +150,9 @@ contract VerifierTest is Test { bytes memory userdata = json.parseRaw(".userdata"); uint8[] memory dataU = abi.decode(userdata, ( uint8[])); - //bytes memory data = abi.encodePacked(dataU); bytes memory data = new bytes(dataU.length); for (uint256 i = 0; i < data.length; i++) { data[i] = bytes1(dataU[i]); - //console.log("--data[i=%d], value:%s", i, uint8(data[i])); } diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index f361680b..b35607ef 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -34,15 +34,17 @@ async fn main() -> Result<(), Box> { 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").expect("ELF PATH is missed"); + let args_parameter = env::var("ARGS").unwrap_or("data-to-hash".to_string()); let input: ProverInput = match args[1].as_str() { "sha2-rust" => { - set_sha2_rust_input(seg_size2, execute_only2).expect("set sha2-rust input error") + set_sha2_rust_input(seg_size2, execute_only2, elf_path).expect("set sha2-rust input error") } - "sha2-go" => set_sha2_go_input(seg_size2, execute_only2).expect("set sha2-go input error"), - "mem-alloc-vec" => set_mem_alloc_vec_input(seg_size2, execute_only2) + "sha2-go" => set_sha2_go_input(seg_size2, execute_only2, elf_path, args_parameter).expect("set sha2-go input error"), + "mem-alloc-vec" => set_mem_alloc_vec_input(seg_size2, execute_only2, elf_path) .expect("set mem-alloc-vec input error"), - "revme" => set_revme_input(seg_size2, execute_only2).expect("set revme input error"), + "revme" => set_revme_input(seg_size2, execute_only2, elf_path).expect("set revme input error"), _ => { helper(); ProverInput { @@ -80,10 +82,10 @@ async fn main() -> Result<(), Box> { return Err("Proof: failed to write to file".into()); } } - //public inputs - if !replace_public_inputs(input.public_inputstream, &prover_result.public_values) { - log::info!("public_inputs check false."); - return Err("public_inputs check false.".into()); + //Handle the public inputs + if !update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values) { + log::info!("the proof's public_inputs has something wrong."); + return Err("the proof's public_inputs has something wrong.".into()); } //contract let output_dir = "../contracts/src".to_string(); @@ -151,8 +153,7 @@ async fn main() -> Result<(), Box> { Ok(()) } -fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { - let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); +fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: string) -> anyhow::Result { 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(); @@ -229,11 +230,9 @@ impl Data { } } -fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { - let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); - let args1 = env::var("ARGS").unwrap_or("data-to-hash".to_string()); +fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: string, args: string) -> anyhow::Result { // assume the arg[0] is the hash(input)(which is a public input), and the arg[1] is the input. - let args: Vec<&str> = args1.split_whitespace().collect(); + let args: Vec<&str> = args.split_whitespace().collect(); assert_eq!(args.len(), 2); let mut data = Data::new(); // Fill in the input data @@ -252,13 +251,13 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result anyhow::Result { +fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: string) -> anyhow::Result { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); //let mut buf = Vec::new(); //bincode::serialize_into(&mut buf, &"0".into()).expect("serialization failed"); let input = ProverInput { elf: read(elf_path).unwrap(), - public_inputstream: "0".into(), + public_inputstream: "0".into(), //if the public input is empty, please using "0" private_inputstream: "".into(), seg_size: seg_size_u, execute_only: execute_only_b, @@ -267,9 +266,7 @@ fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Res Ok(input) } -fn set_revme_input(seg_size_u: u32, execute_only_b: bool) -> anyhow::Result { - let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); - let json_path = env::var("JSON_PATH").expect("JSON PATH is missing"); +fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: string, json_path: string) -> anyhow::Result { let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: read(json_path).unwrap(), @@ -293,7 +290,7 @@ struct Roots { root: Vec, } -fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec) -> bool { +fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> bool { let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); let proof_result_path = output_path.join("public_inputs.json"); @@ -307,18 +304,20 @@ fn replace_public_inputs(public_inputstream: Vec, proof_public_inputs: &Vec< let mut public_inputs: PublicInputs = serde_json::from_slice(slice_bt) .expect("Failed to parse JSON"); + //1,check the userdata (from sdk' proof) = hash(bincode(host's public_inputs)) ? let userdata = public_inputs.userdata; if userdata == output_hs { log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); } else { - log::info!("public inputs is different. the sdk's is: {:?}, host's is :{:?} ", userdata, output_hs); + log::info!("public inputs's hash is different. the sdk's is: {:?}, host's is :{:?} ", userdata, output_hs); return false; } - //update userdata with bincode(public_inputs). The old userdata is hash(bincode(pulic_inputs)). + //2, update userdata with bincode(host's public_inputs). public_inputs.userdata = public_inputstream; let mut fp = File::create(proof_result_path).expect("Unable to create file"); - // save the new contents + + //3, save the json file to_writer(&mut fp, &public_inputs) .expect("Unable to write to public input file"); return true; diff --git a/sdk/README.md b/sdk/README.md index 7b5828fb..492fc019 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -2,7 +2,7 @@ ## Use the libsnark -1. The compile.sh in the path sdk/src/local/libsnark only supports X86_64 linux. +1. The compile.sh in the path sdk/src/local/libsnark only supports X86_64 linux.For MacOS, there is a [Dockerfile](../Dockerfile) in the template. ``` cd zkm-project-template/sdk/src/local/libsnark From 28cacf4d936e14e8dde1492ba7e4e93975b1500e Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 18:31:18 +0800 Subject: [PATCH 62/76] update the readme and host --- host-program/src/bin/zkm-prove.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index b35607ef..9d00a1f7 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -36,6 +36,7 @@ async fn main() -> Result<(), Box> { let execute_only2 = execute_only.parse::().unwrap_or(false); let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); let args_parameter = env::var("ARGS").unwrap_or("data-to-hash".to_string()); + let json_path = env::var("JSON_PATH").expect("JSON PATH is missing"); let input: ProverInput = match args[1].as_str() { "sha2-rust" => { @@ -44,7 +45,7 @@ async fn main() -> Result<(), Box> { "sha2-go" => set_sha2_go_input(seg_size2, execute_only2, elf_path, args_parameter).expect("set sha2-go input error"), "mem-alloc-vec" => set_mem_alloc_vec_input(seg_size2, execute_only2, elf_path) .expect("set mem-alloc-vec input error"), - "revme" => set_revme_input(seg_size2, execute_only2, elf_path).expect("set revme input error"), + "revme" => set_revme_input(seg_size2, execute_only2, elf_path, json_path).expect("set revme input error"), _ => { helper(); ProverInput { @@ -153,7 +154,7 @@ async fn main() -> Result<(), Box> { Ok(()) } -fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: string) -> anyhow::Result { +fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { 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(); @@ -230,7 +231,7 @@ impl Data { } } -fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: string, args: string) -> anyhow::Result { +fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, args: String) -> anyhow::Result { // 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); @@ -251,7 +252,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: string, ar Ok(input) } -fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: string) -> anyhow::Result { +fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); //let mut buf = Vec::new(); //bincode::serialize_into(&mut buf, &"0".into()).expect("serialization failed"); @@ -266,7 +267,7 @@ fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: stri Ok(input) } -fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: string, json_path: string) -> anyhow::Result { +fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, json_path: String) -> anyhow::Result { let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: read(json_path).unwrap(), From b9cb2c2915fcb9c9e5b09c445bf9f8f1136c10a3 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 18:32:50 +0800 Subject: [PATCH 63/76] update the readme and host --- host-program/src/bin/zkm-prove.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 9d00a1f7..d49a8580 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -253,9 +253,6 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, ar } fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { - let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); - //let mut buf = Vec::new(); - //bincode::serialize_into(&mut buf, &"0".into()).expect("serialization failed"); let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: "0".into(), //if the public input is empty, please using "0" From 717eb421fb032bf08e2596db0d5b4d9c1c628d1b Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 19:22:31 +0800 Subject: [PATCH 64/76] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 709d9bf1..f7ca296e 100644 --- a/README.md +++ b/README.md @@ -314,4 +314,4 @@ cp test-vectors/244.json zkm-project-template/host-program/test-vectors/ Next, you need to edit the `JSON_PATH` variable in the [`run-local-proving.sh`](host-program/run-local-proving.sh) or [`run-network-proving.sh`](host-program/run-network-proving.sh) to match the name of the JSON file mentioned above. -Then, you can execute the run-xxx-proving.sh by following the steps outlined in `Example 1: sha2-rust`. \ No newline at end of file +Then, you can execute the run-xxx-proving.sh by following the steps outlined in [`Example 1: sha2-rust`](### Example 1 : `sha2-rust`). \ No newline at end of file From 2bee916d356fa97b0a884baf4d9f0cfc03cdfad4 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 19:27:41 +0800 Subject: [PATCH 65/76] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7ca296e..709d9bf1 100644 --- a/README.md +++ b/README.md @@ -314,4 +314,4 @@ cp test-vectors/244.json zkm-project-template/host-program/test-vectors/ Next, you need to edit the `JSON_PATH` variable in the [`run-local-proving.sh`](host-program/run-local-proving.sh) or [`run-network-proving.sh`](host-program/run-network-proving.sh) to match the name of the JSON file mentioned above. -Then, you can execute the run-xxx-proving.sh by following the steps outlined in [`Example 1: sha2-rust`](### Example 1 : `sha2-rust`). \ No newline at end of file +Then, you can execute the run-xxx-proving.sh by following the steps outlined in `Example 1: sha2-rust`. \ No newline at end of file From 4fa8235715e1b2a9b3b1b7d2970cf29b9e989f81 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Mon, 28 Oct 2024 23:24:54 +0800 Subject: [PATCH 66/76] perfect the host --- host-program/src/bin/zkm-prove.rs | 48 +++++++++++++++++-------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index d49a8580..ee9ef635 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -70,6 +70,7 @@ async fn main() -> Result<(), Box> { ); return Err("SEG_SIZE is excessively large".into()); } + //1.snark proof 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"); @@ -83,12 +84,26 @@ async fn main() -> Result<(), Box> { return Err("Proof: failed to write to file".into()); } } - //Handle the public inputs - if !update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values) { - log::info!("the proof's public_inputs has something wrong."); - return Err("the proof's public_inputs has something wrong.".into()); + + //2.handle the public inputs + match update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values) { + Ok(public_inputs) => { + //println!("Updated public inputs: {:?}", public_inputs); + let output_dir = "../contracts/verifier".to_string(); + let output_path = Path::new(&output_dir); + let public_inputs_path = output_path.join("public_inputs.json"); + let mut fp = File::create(public_inputs_path).expect("Unable to create file"); + + //save the json file + to_writer(&mut fp, &public_inputs) + .expect("Unable to write to public input file"); + }, + Err(e) => { + return Err("Error updating public inputs".into()); + } } - //contract + + //3.contract let output_dir = "../contracts/src".to_string(); let output_path = Path::new(&output_dir); let contract_path = output_path.join("verifier.sol"); @@ -155,14 +170,14 @@ async fn main() -> Result<(), Box> { } fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { - let num_bytes: usize = 1024; //Notice! : if this value is small, it will not generate the snark proof. + let num_bytes: usize = 1024; //Notice! : if this value is small, it will not generate the 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. + // assume the arg[0] = hash(public input), and the arg[1] = public input. let public_input = output.to_vec(); let mut pub_buf = Vec::new(); bincode::serialize_into(&mut pub_buf, &public_input) @@ -288,11 +303,7 @@ struct Roots { root: Vec, } -fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> bool { - let output_dir = "../contracts/verifier".to_string(); - let output_path = Path::new(&output_dir); - let proof_result_path = output_path.join("public_inputs.json"); - +fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> Result { let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); @@ -302,21 +313,16 @@ fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_i let mut public_inputs: PublicInputs = serde_json::from_slice(slice_bt) .expect("Failed to parse JSON"); - //1,check the userdata (from sdk' proof) = hash(bincode(host's public_inputs)) ? + //1.check the userdata (from the proof) = hash(bincode(host's public_inputs)) ? let userdata = public_inputs.userdata; if userdata == output_hs { log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); } else { - log::info!("public inputs's hash is different. the sdk's is: {:?}, host's is :{:?} ", userdata, output_hs); - return false; + log::info!("public inputs's hash is different. the proof's is: {:?}, host's is :{:?} ", userdata, output_hs); + return Err(Error::msg("Public inputs's hash does not match the proof's userdata")); } //2, update userdata with bincode(host's public_inputs). public_inputs.userdata = public_inputstream; - let mut fp = File::create(proof_result_path).expect("Unable to create file"); - - //3, save the json file - to_writer(&mut fp, &public_inputs) - .expect("Unable to write to public input file"); - return true; + Ok(public_inputs) } From 0a1bd247637cfe09d11948c96bfbf6d46140183c Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 00:05:19 +0800 Subject: [PATCH 67/76] perfect the host --- host-program/src/bin/zkm-prove.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index ee9ef635..a79f9029 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -84,25 +84,28 @@ async fn main() -> Result<(), Box> { return Err("Proof: failed to write to file".into()); } } - + //2.handle the public inputs - match update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values) { - Ok(public_inputs) => { - //println!("Updated public inputs: {:?}", public_inputs); + let public_inputs = update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values); + match public_inputs { + Ok(Some(inputs)) => { let output_dir = "../contracts/verifier".to_string(); let output_path = Path::new(&output_dir); let public_inputs_path = output_path.join("public_inputs.json"); let mut fp = File::create(public_inputs_path).expect("Unable to create file"); - //save the json file - to_writer(&mut fp, &public_inputs) - .expect("Unable to write to public input file"); - }, + to_writer(&mut fp, &inputs).expect("Unable to write to public input file"); + } + Ok(None) => { + log::info!("Failed to update the public inputs."); + return Err("Failed to update the public inputs.".into()); + } Err(e) => { - return Err("Error updating public inputs".into()); + log::info!("Failed to update the public inputs. error: {}", e); + return Err("Failed to update the public inputs.".into()); } } - + //3.contract let output_dir = "../contracts/src".to_string(); let output_path = Path::new(&output_dir); @@ -303,7 +306,7 @@ struct Roots { root: Vec, } -fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> Result { +fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> anyhow::Result> { let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); @@ -319,10 +322,10 @@ fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_i log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); } else { log::info!("public inputs's hash is different. the proof's is: {:?}, host's is :{:?} ", userdata, output_hs); - return Err(Error::msg("Public inputs's hash does not match the proof's userdata")); + return Err(anyhow::anyhow!("Public inputs's hash does not match the proof's userdata.")); } //2, update userdata with bincode(host's public_inputs). public_inputs.userdata = public_inputstream; - Ok(public_inputs) + Ok(Some(public_inputs)) } From 8e5c67d8ef393a9b11ea556dcea8ccde9af00993 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 10:13:25 +0800 Subject: [PATCH 68/76] perfect the host --- host-program/run-local-proving.sh | 1 + host-program/run-network-proving.sh | 1 + host-program/src/bin/zkm-prove.rs | 7 ++++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/host-program/run-local-proving.sh b/host-program/run-local-proving.sh index 0e64c25f..76ecb36a 100755 --- a/host-program/run-local-proving.sh +++ b/host-program/run-local-proving.sh @@ -13,6 +13,7 @@ export SEG_SIZE=262144 export ARGS="711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d world!" export ELF_PATH=${BASEDIR}/../guest-program/$program/target/mips-unknown-linux-musl/release/$program export JSON_PATH=${BASEDIR}/test-vectors/test.json +export PROOF_RESULTS_PATH=${BASEDIR}/../contracts export EXECUTE_ONLY=false echo "Compile guest-program ${program}" diff --git a/host-program/run-network-proving.sh b/host-program/run-network-proving.sh index 7a9bb29a..d5eb469b 100755 --- a/host-program/run-network-proving.sh +++ b/host-program/run-network-proving.sh @@ -16,6 +16,7 @@ export SEG_SIZE=262144 export ARGS="711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d world!" export ELF_PATH=${BASEDIR}/../guest-program/$program/target/mips-unknown-linux-musl/release/$program export JSON_PATH=${BASEDIR}/test-vectors/test.json +export PROOF_RESULTS_PATH=${BASEDIR}/../contracts export EXECUTE_ONLY=false echo "Compile guest-program ${program}" diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index a79f9029..e9539f51 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -37,6 +37,7 @@ async fn main() -> Result<(), Box> { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); let args_parameter = env::var("ARGS").unwrap_or("data-to-hash".to_string()); let json_path = env::var("JSON_PATH").expect("JSON PATH is missing"); + let proof_results_path = env::var("PROOF_RESULTS_PATH").expect("PROOF RESULTS PATH is missing"); let input: ProverInput = match args[1].as_str() { "sha2-rust" => { @@ -71,7 +72,7 @@ async fn main() -> Result<(), Box> { return Err("SEG_SIZE is excessively large".into()); } //1.snark proof - let output_dir = "../contracts/verifier".to_string(); + let output_dir = format!("{}/verifier", proof_results_path); let output_path = Path::new(&output_dir); let proof_result_path = output_path.join("snark_proof_with_public_inputs.json"); let mut f = file::new(&proof_result_path.to_string_lossy()); @@ -89,7 +90,7 @@ async fn main() -> Result<(), Box> { let public_inputs = update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values); match public_inputs { Ok(Some(inputs)) => { - let output_dir = "../contracts/verifier".to_string(); + let output_dir = format!("{}/verifier", proof_results_path); let output_path = Path::new(&output_dir); let public_inputs_path = output_path.join("public_inputs.json"); let mut fp = File::create(public_inputs_path).expect("Unable to create file"); @@ -107,7 +108,7 @@ async fn main() -> Result<(), Box> { } //3.contract - let output_dir = "../contracts/src".to_string(); + let output_dir = format!("{}/src", proof_results_path); let output_path = Path::new(&output_dir); let contract_path = output_path.join("verifier.sol"); let mut f = file::new(&contract_path.to_string_lossy()); From 5f299ab6e92423ef53937c90fb2455e2f356a4cc Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 10:31:22 +0800 Subject: [PATCH 69/76] perfect the host --- host-program/src/bin/zkm-prove.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index e9539f51..f31acd11 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -26,10 +26,6 @@ async fn main() -> Result<(), Box> { helper(); } - log::info!("new prover client."); - let prover_client = ProverClient::new().await; - log::info!("new prover client,ok."); - 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()); @@ -37,7 +33,11 @@ async fn main() -> Result<(), Box> { let elf_path = env::var("ELF_PATH").expect("ELF PATH is missed"); let args_parameter = env::var("ARGS").unwrap_or("data-to-hash".to_string()); let json_path = env::var("JSON_PATH").expect("JSON PATH is missing"); - let proof_results_path = env::var("PROOF_RESULTS_PATH").expect("PROOF RESULTS PATH is missing"); + let proof_results_path = env::var("PROOF_RESULTS_PATH").unwrap_or("../contracts".to_string()); + + log::info!("new prover client."); + let prover_client = ProverClient::new().await; + log::info!("new prover client,ok."); let input: ProverInput = match args[1].as_str() { "sha2-rust" => { @@ -73,6 +73,7 @@ async fn main() -> Result<(), Box> { } //1.snark proof let output_dir = format!("{}/verifier", proof_results_path); + tokio::fs::create_dir_all(&output_dir).await?; let output_path = Path::new(&output_dir); let proof_result_path = output_path.join("snark_proof_with_public_inputs.json"); let mut f = file::new(&proof_result_path.to_string_lossy()); @@ -91,6 +92,7 @@ async fn main() -> Result<(), Box> { match public_inputs { Ok(Some(inputs)) => { let output_dir = format!("{}/verifier", proof_results_path); + tokio::fs::create_dir_all(&output_dir).await?; let output_path = Path::new(&output_dir); let public_inputs_path = output_path.join("public_inputs.json"); let mut fp = File::create(public_inputs_path).expect("Unable to create file"); @@ -109,6 +111,7 @@ async fn main() -> Result<(), Box> { //3.contract let output_dir = format!("{}/src", proof_results_path); + tokio::fs::create_dir_all(&output_dir).await?; let output_path = Path::new(&output_dir); let contract_path = output_path.join("verifier.sol"); let mut f = file::new(&contract_path.to_string_lossy()); @@ -121,7 +124,7 @@ async fn main() -> Result<(), Box> { return Err("Contract: failed to write to file".into()); } } - log::info!("Generating proof successfully .The proof file and verifier contract are in the the path contracts/verifier and contracts/src ."); + log::info!("Generating proof successfully .The proof file and verifier contract are in the the path {}/{verifier,src} .", proof_results_path); } else { match args[1].as_str() { "sha2-rust" => { From 3eb4d0abef29b3756e0aa405c5b3b7d220097d27 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 10:36:55 +0800 Subject: [PATCH 70/76] perfect the host --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index f31acd11..201dfd98 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -124,7 +124,7 @@ async fn main() -> Result<(), Box> { return Err("Contract: failed to write to file".into()); } } - log::info!("Generating proof successfully .The proof file and verifier contract are in the the path {}/{verifier,src} .", proof_results_path); + log::info!("Generating proof successfully .The proof file and verifier contract are in the the path {}/{{verifier,src}} .", proof_results_path); } else { match args[1].as_str() { "sha2-rust" => { From c803d33b1beef7e72e341db22ad2675db7d030fc Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 10:50:49 +0800 Subject: [PATCH 71/76] perfect the host --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 709d9bf1..7b28e77b 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,9 @@ generation of proofs on the local machine and over the proof network, respective > 3. There are four guest programs(sha2-rust, sha2-go, mem-alloc-vec,revme). The following will use sha2-rust and revme as an example to demonstrate local and network proofs. +> 4. If the environmental variable `PROOF_RESULTS_PATH` is not set, the proof results file will be saved in zkm-project-template/contracts/{src, verifier}; if the environmental variable `PROOF_RESULTS_PATH` is set, after the proof is completed, the proof results file needs to be copied to the corresponding zkm-project-template/contracts/{src, verifier}. + + > [!WARNING] > The environmental variable `SEG_SIZE` in the run-xxx_proving.sh affects the final proof generation. From 2a2aa261cc00336a8c473b161a558e2a2cb0e18b Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 10:55:53 +0800 Subject: [PATCH 72/76] perfect the readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b28e77b..c77fba00 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ generation of proofs on the local machine and over the proof network, respective > 3. There are four guest programs(sha2-rust, sha2-go, mem-alloc-vec,revme). The following will use sha2-rust and revme as an example to demonstrate local and network proofs. -> 4. If the environmental variable `PROOF_RESULTS_PATH` is not set, the proof results file will be saved in zkm-project-template/contracts/{src, verifier}; if the environmental variable `PROOF_RESULTS_PATH` is set, after the proof is completed, the proof results file needs to be copied to the corresponding zkm-project-template/contracts/{src, verifier}. +> 4. If the environmental variable `PROOF_RESULTS_PATH` is not set, the proof results file will be saved in zkm-project-template/contracts/{src, verifier}; if the environmental variable `PROOF_RESULTS_PATH` is set, after the proof is completed, the proof results file needs to be copied from from 'PROOF_RESULTS_PATH'/{src, verifier} to the corresponding zkm-project-template/contracts/{src, verifier}. > [!WARNING] From 44e8d6b5d701981720d0ef82f8937d7001c6d1bf Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 17:08:25 +0800 Subject: [PATCH 73/76] update the host --- host-program/src/bin/zkm-prove.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 201dfd98..b84431aa 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -266,7 +266,7 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, ar let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: buf, - private_inputstream: "".into(), + private_inputstream: "".into(), //the private input is empty seg_size: seg_size_u, execute_only: execute_only_b, }; @@ -277,8 +277,8 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, ar fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { let input = ProverInput { elf: read(elf_path).unwrap(), - public_inputstream: "0".into(), //if the public input is empty, please using "0" - private_inputstream: "".into(), + public_inputstream: "".into(), //the public input is empty + private_inputstream: "".into(), //the private input is empty seg_size: seg_size_u, execute_only: execute_only_b, }; @@ -290,7 +290,7 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, json let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: read(json_path).unwrap(), - private_inputstream: "".into(), + private_inputstream: "".into(), //the private input is empty seg_size: seg_size_u, execute_only: execute_only_b, }; From 3da1d3df441ea263b2077f5b36a0d3ad48ab55ea Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 17:52:54 +0800 Subject: [PATCH 74/76] fix CI/CD error --- host-program/src/bin/zkm-prove.rs | 110 +++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 33 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index bfb514e5..cbc47186 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -1,15 +1,15 @@ use common::file; +//use hex; use serde::{Deserialize, Serialize}; -use serde_json; +//use serde_json; +use serde_json::to_writer; use sha2::{Digest, Sha256}; use std::env; +use std::fs::read; +use std::fs::File; use std::path::Path; use std::time::Instant; use zkm_sdk::{prover::ProverInput, ProverClient}; -use hex; -use std::fs::read; -use serde_json::to_writer; -use std::fs::File; #[tokio::main] async fn main() -> Result<(), Box> { @@ -40,13 +40,14 @@ async fn main() -> Result<(), Box> { log::info!("new prover client,ok."); let input: ProverInput = match args[1].as_str() { - "sha2-rust" => { - set_sha2_rust_input(seg_size2, execute_only2, elf_path).expect("set sha2-rust input error") - } - "sha2-go" => set_sha2_go_input(seg_size2, execute_only2, elf_path, args_parameter).expect("set sha2-go input error"), + "sha2-rust" => set_sha2_rust_input(seg_size2, execute_only2, elf_path) + .expect("set sha2-rust input error"), + "sha2-go" => set_sha2_go_input(seg_size2, execute_only2, elf_path, args_parameter) + .expect("set sha2-go input error"), "mem-alloc-vec" => set_mem_alloc_vec_input(seg_size2, execute_only2, elf_path) .expect("set mem-alloc-vec input error"), - "revme" => set_revme_input(seg_size2, execute_only2, elf_path, json_path).expect("set revme input error"), + "revme" => set_revme_input(seg_size2, execute_only2, elf_path, json_path) + .expect("set revme input error"), _ => { helper(); ProverInput { @@ -88,15 +89,19 @@ async fn main() -> Result<(), Box> { } } - //2.handle the public inputs - let public_inputs = update_public_inputs_with_bincode(input.public_inputstream, &prover_result.public_values); + //2.handle the public inputs + let public_inputs = update_public_inputs_with_bincode( + input.public_inputstream, + &prover_result.public_values, + ); match public_inputs { Ok(Some(inputs)) => { let output_dir = format!("{}/verifier", proof_results_path); tokio::fs::create_dir_all(&output_dir).await?; let output_path = Path::new(&output_dir); let public_inputs_path = output_path.join("public_inputs.json"); - let mut fp = File::create(public_inputs_path).expect("Unable to create file"); + let mut fp = + File::create(public_inputs_path).expect("Unable to create file"); //save the json file to_writer(&mut fp, &inputs).expect("Unable to write to public input file"); } @@ -109,7 +114,7 @@ async fn main() -> Result<(), Box> { return Err("Failed to update the public inputs.".into()); } } - + //3.contract let output_dir = format!("{}/src", proof_results_path); tokio::fs::create_dir_all(&output_dir).await?; @@ -177,23 +182,27 @@ async fn main() -> Result<(), Box> { Ok(()) } -fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { +fn set_sha2_rust_input( + seg_size_u: u32, + execute_only_b: bool, + elf_path: String, +) -> anyhow::Result { let num_bytes: usize = 1024; //Notice! : if this value is small, it will not generate the 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] = hash(public input), and the arg[1] = public input. 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, &pri_input).expect("private_input serialization failed"); - + let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: pub_buf, @@ -202,7 +211,10 @@ fn set_sha2_rust_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) execute_only: execute_only_b, args: "".into(), }; - log::info!("sha2_rust, bincode(pulic_input): {:?} ", &input.public_inputstream); + log::info!( + "sha2_rust, bincode(pulic_input): {:?} ", + &input.public_inputstream + ); Ok(input) } @@ -255,7 +267,12 @@ impl Data { } } -fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, args: String) -> anyhow::Result { +fn set_sha2_go_input( + seg_size_u: u32, + execute_only_b: bool, + elf_path: String, + args: String, +) -> anyhow::Result { // 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); @@ -273,11 +290,18 @@ fn set_sha2_go_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, ar execute_only: execute_only_b, args: "".into(), }; - log::info!("sha2_go, bincode(pulic_input): {:?} ", &input.public_inputstream); + log::info!( + "sha2_go, bincode(pulic_input): {:?} ", + &input.public_inputstream + ); Ok(input) } -fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: String) -> anyhow::Result { +fn set_mem_alloc_vec_input( + seg_size_u: u32, + execute_only_b: bool, + elf_path: String, +) -> anyhow::Result { let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: "".into(), //the public input is empty @@ -286,11 +310,19 @@ fn set_mem_alloc_vec_input(seg_size_u: u32, execute_only_b: bool, elf_path: Stri execute_only: execute_only_b, args: "".into(), }; - log::info!("set_mem_alloc_vec_input, bincode(pulic_input): {:?} ", &input.public_inputstream); + log::info!( + "set_mem_alloc_vec_input, bincode(pulic_input): {:?} ", + &input.public_inputstream + ); Ok(input) } -fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, json_path: String) -> anyhow::Result { +fn set_revme_input( + seg_size_u: u32, + execute_only_b: bool, + elf_path: String, + json_path: String, +) -> anyhow::Result { let input = ProverInput { elf: read(elf_path).unwrap(), public_inputstream: read(json_path).unwrap(), @@ -299,7 +331,10 @@ fn set_revme_input(seg_size_u: u32, execute_only_b: bool, elf_path: String, json execute_only: execute_only_b, args: "".into(), }; - log::info!("revme, bincode(pulic_input): {:?} ", &input.public_inputstream); + log::info!( + "revme, bincode(pulic_input): {:?} ", + &input.public_inputstream + ); Ok(input) } @@ -315,26 +350,35 @@ struct Roots { root: Vec, } -fn update_public_inputs_with_bincode(public_inputstream: Vec, proof_public_inputs: &Vec) -> anyhow::Result> { +fn update_public_inputs_with_bincode( + public_inputstream: Vec, + proof_public_inputs: &Vec, +) -> anyhow::Result> { let mut hasher = Sha256::new(); hasher.update(&public_inputstream); let result_hs = hasher.finalize(); let output_hs: [u8; 32] = result_hs.into(); - let slice_bt: &[u8] = &proof_public_inputs; - let mut public_inputs: PublicInputs = serde_json::from_slice(slice_bt) - .expect("Failed to parse JSON"); + let slice_bt: &[u8] = proof_public_inputs; + let mut public_inputs: PublicInputs = + serde_json::from_slice(slice_bt).expect("Failed to parse JSON"); //1.check the userdata (from the proof) = hash(bincode(host's public_inputs)) ? let userdata = public_inputs.userdata; if userdata == output_hs { - log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); + log::info!(" hash(bincode(pulic_input)): {:?} ", &output_hs); } else { - log::info!("public inputs's hash is different. the proof's is: {:?}, host's is :{:?} ", userdata, output_hs); - return Err(anyhow::anyhow!("Public inputs's hash does not match the proof's userdata.")); + log::info!( + "public inputs's hash is different. the proof's is: {:?}, host's is :{:?} ", + userdata, + output_hs + ); + return Err(anyhow::anyhow!( + "Public inputs's hash does not match the proof's userdata." + )); } //2, update userdata with bincode(host's public_inputs). public_inputs.userdata = public_inputstream; Ok(Some(public_inputs)) -} +} \ No newline at end of file From 82e1c0deda12f4f8c67e60be853f6b59a52181f2 Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 17:56:34 +0800 Subject: [PATCH 75/76] fix CI/CD error --- host-program/src/bin/zkm-prove.rs | 2 +- sdk/src/local/util.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index cbc47186..3dd3604d 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -381,4 +381,4 @@ fn update_public_inputs_with_bincode( //2, update userdata with bincode(host's public_inputs). public_inputs.userdata = public_inputstream; Ok(Some(public_inputs)) -} \ No newline at end of file +} diff --git a/sdk/src/local/util.rs b/sdk/src/local/util.rs index 0f0e4827..cc74edab 100644 --- a/sdk/src/local/util.rs +++ b/sdk/src/local/util.rs @@ -217,14 +217,12 @@ pub fn prove_multi_seg_common( let wrapped_proof = wrapped_circuit.prove(&block_proof)?; wrapped_proof.save(outdir)?; - let block_public_inputs = serde_json::json!({ "public_inputs": block_proof.public_inputs, }); let outdir_path = std::path::Path::new(outdir); let public_values_file = File::create(outdir_path.join("public_values.json"))?; serde_json::to_writer(&public_values_file, &updated_agg_public_values)?; - let block_public_inputs_file = File::create(outdir_path.join("block_public_inputs.json"))?; serde_json::to_writer(&block_public_inputs_file, &block_public_inputs)?; From 960334e8215d0ad161311b2169b3f6065cd25aac Mon Sep 17 00:00:00 2001 From: "[gavin-ygy]" <[gavin.ygy@gmail.com]> Date: Tue, 29 Oct 2024 18:04:01 +0800 Subject: [PATCH 76/76] fix CI/CD error --- host-program/src/bin/zkm-prove.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host-program/src/bin/zkm-prove.rs b/host-program/src/bin/zkm-prove.rs index 3dd3604d..a4c6df66 100644 --- a/host-program/src/bin/zkm-prove.rs +++ b/host-program/src/bin/zkm-prove.rs @@ -352,7 +352,7 @@ struct Roots { fn update_public_inputs_with_bincode( public_inputstream: Vec, - proof_public_inputs: &Vec, + proof_public_inputs: &[u8], ) -> anyhow::Result> { let mut hasher = Sha256::new(); hasher.update(&public_inputstream);