From 98b6ea85adda25d0b450194761447e7052f605e6 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Wed, 9 Oct 2024 16:01:46 -0300 Subject: [PATCH 1/9] docs: change zkquiz questions guide --- docs/3_guides/2.2_modify_zkquiz_questions.md | 62 +++++++++++++++++++ .../2_build_your_first_aligned_application.md | 2 +- docs/SUMMARY.md | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 docs/3_guides/2.2_modify_zkquiz_questions.md diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md new file mode 100644 index 0000000000..8e049c74db --- /dev/null +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -0,0 +1,62 @@ +# Modify ZkQuiz Questions + +In [Build your first Aligned Application](2_build_your_first_aligned_application.md), we show how to build a trivia application, called ZkQuiz. ZKQuiz asks the user three questions, and if answered correctly, generates a ZK Proof of the correct answers, posts the proof on Aligned, and upon verification, mints an NFT via a smart contract. + +In this guide, we will show you how to replace those questions with others. + +{% hint style="warning" %} +This guide assumes you have already read [Build your first Aligned Application](2_build_your_first_aligned_application.md) +{% endhint %} + +## 1. Modify the Questions Asked + +First, we need to modify the questions presented to the user. To do this, navigate to our example in `examples/zkquiz/quiz/script/src/main.rs` and change the questions as needed: + +```Rust +let mut user_answers = "".to_string(); + +let question1 = "What is the capital of France?"; +let answers1 = ["Berlin", "Madrid", "Paris"]; +user_answers.push(ask_question(question1, &answers1)); + +let question2 = "What is the chemical symbol for gold?"; +let answers2 = ["Au", "Ag", "Fe"]; +user_answers.push(ask_question(question2, &answers2)); + +let question3 = "What is the native cryptocurrency of Ethereum?"; +let answers3 = ["Bitcoin", "Ether", "Litecoin"]; +user_answers.push(ask_question(question3, &answers3)); +``` + +## 2. Update the Program + +Now we need to update the program to be proven with the new correct answers. As we saw in [Build your first Aligned Application](2_build_your_first_aligned_application.md), the program in `examples/zkquiz/quiz/program/src/main.rs` takes the user answers as inputs and checks that the hash of these inputs matches the expected output. Therefore, we need to update the expected output with the hash of our new correct answers. The updated example looks as follows: + +```Rust +if output + != [ + 216, 11, 193, 177, 136, 178, 254, 150, 59, 128, 97, 103, 97, 128, 55, 57, 22, 242, 26, + 1, 186, 223, 215, 118, 206, 47, 12, 206, 114, 118, 220, 93, + ] + { + panic!("Answers do not match"); + } +``` + +## 3. Compile the Program + +Now we need to compile the updated Program, generating the binary file that will be run by the zkVm (ELF). +For that you can use the [SP1 Rust toolchain](https://docs.succinct.xyz/introduction.html). + +``` +cd examples/zkquiz/quiz/program +cargo prove build +``` + +which will output the compiled ELF to the file program/elf/riscv32im-succinct-zkvm-elf. + +## 4. Run the new ZkQuiz + +We are ready to run our new version of ZkQuiz. + +To do this, follow the same instructions as in the original [Build your first Aligned Application](2_build_your_first_aligned_application.md) diff --git a/docs/3_guides/2_build_your_first_aligned_application.md b/docs/3_guides/2_build_your_first_aligned_application.md index 31999d9b88..370ed5dca3 100644 --- a/docs/3_guides/2_build_your_first_aligned_application.md +++ b/docs/3_guides/2_build_your_first_aligned_application.md @@ -132,7 +132,7 @@ pub fn main() { } ``` -The program takes the user answers as inputs and checks that the hash of the inputs matches with the expected output. This is the program that will be compiled generati ng a binary file that will be ran by the zkVm and used later in the application side. In our case this file is already generated and is located on `/quiz/program/elf/riscv32im-succinct-zkvm-elf`. +The program takes the user answers as inputs and checks that the hash of the inputs matches with the expected output. This is the program that will be compiled generating a binary file that will be ran by the zkVm and used later in the application side. In our case this file is already generated and is located on `/quiz/program/elf/riscv32im-succinct-zkvm-elf`. ### Verifier Contract diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 6459a788b7..1be2057ea1 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -25,6 +25,7 @@ * [Submitting proofs](3_guides/0_submitting_proofs.md) * [Build your first Aligned Application](3_guides/2_build_your_first_aligned_application.md) +* [Modify Zk Quiz](3_guides/2.2_modify_zkquiz_questions.md) * [Validating public input](3_guides/3_validating_public_input.md) * [SDK Intro](3_guides/1_SDK_how_to.md) * [SDK API Reference](3_guides/1.2_SDK_api_reference.md) From 8a5463ae35a906296d0a3c9b56932408c4915268 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Wed, 9 Oct 2024 16:04:27 -0300 Subject: [PATCH 2/9] fix: typo --- docs/3_guides/2.2_modify_zkquiz_questions.md | 2 +- docs/SUMMARY.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md index 8e049c74db..8b8d1e98c4 100644 --- a/docs/3_guides/2.2_modify_zkquiz_questions.md +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -53,7 +53,7 @@ cd examples/zkquiz/quiz/program cargo prove build ``` -which will output the compiled ELF to the file program/elf/riscv32im-succinct-zkvm-elf. +which will output the compiled ELF to the file program/elf/riscv32im-succinct-zkvm-elf. ## 4. Run the new ZkQuiz diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 1be2057ea1..87fd98f1e8 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -25,7 +25,7 @@ * [Submitting proofs](3_guides/0_submitting_proofs.md) * [Build your first Aligned Application](3_guides/2_build_your_first_aligned_application.md) -* [Modify Zk Quiz](3_guides/2.2_modify_zkquiz_questions.md) +* [Modify ZkQuiz Questions](3_guides/2.2_modify_zkquiz_questions.md) * [Validating public input](3_guides/3_validating_public_input.md) * [SDK Intro](3_guides/1_SDK_how_to.md) * [SDK API Reference](3_guides/1.2_SDK_api_reference.md) From 37a3c1fac9b2305515fee3b6c5b0deb85aeadc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Avila=20Gast=C3=B3n?= <72628438+avilagaston9@users.noreply.github.com> Date: Thu, 10 Oct 2024 12:04:14 -0300 Subject: [PATCH 3/9] Update docs/3_guides/2.2_modify_zkquiz_questions.md Co-authored-by: Nacho Avecilla --- docs/3_guides/2.2_modify_zkquiz_questions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md index 8b8d1e98c4..266e59fa45 100644 --- a/docs/3_guides/2.2_modify_zkquiz_questions.md +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -2,7 +2,7 @@ In [Build your first Aligned Application](2_build_your_first_aligned_application.md), we show how to build a trivia application, called ZkQuiz. ZKQuiz asks the user three questions, and if answered correctly, generates a ZK Proof of the correct answers, posts the proof on Aligned, and upon verification, mints an NFT via a smart contract. -In this guide, we will show you how to replace those questions with others. +In this guide, we will show you how to replace those questions with your own custom ones. {% hint style="warning" %} This guide assumes you have already read [Build your first Aligned Application](2_build_your_first_aligned_application.md) From 1133894c67f565eac1cb318c114a76e7518ba749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Avila=20Gast=C3=B3n?= <72628438+avilagaston9@users.noreply.github.com> Date: Thu, 10 Oct 2024 12:05:07 -0300 Subject: [PATCH 4/9] Update docs/3_guides/2.2_modify_zkquiz_questions.md Co-authored-by: Nacho Avecilla --- docs/3_guides/2.2_modify_zkquiz_questions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md index 266e59fa45..331d78fb73 100644 --- a/docs/3_guides/2.2_modify_zkquiz_questions.md +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -45,7 +45,7 @@ if output ## 3. Compile the Program -Now we need to compile the updated Program, generating the binary file that will be run by the zkVm (ELF). +Now we need to compile the updated Program, generating the binary file that will be run by the zkVM (ELF). For that you can use the [SP1 Rust toolchain](https://docs.succinct.xyz/introduction.html). ``` From b8c707fda1cff4e408d34ccd57829c00a6b67817 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 10 Oct 2024 13:24:51 -0300 Subject: [PATCH 5/9] refactor: add explanation of how to get sha3 hash --- docs/3_guides/2.2_modify_zkquiz_questions.md | 34 ++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md index 331d78fb73..0eaed87e09 100644 --- a/docs/3_guides/2.2_modify_zkquiz_questions.md +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -10,13 +10,13 @@ This guide assumes you have already read [Build your first Aligned Application]( ## 1. Modify the Questions Asked -First, we need to modify the questions presented to the user. To do this, navigate to our example in `examples/zkquiz/quiz/script/src/main.rs` and change the questions as needed: +First, we need to modify the questions presented to the user. To do this, navigate to our example in `examples/zkquiz/quiz/script/src/main.rs` and change the questions as needed. Here’s a new set of questions with their respective answers: ```Rust let mut user_answers = "".to_string(); let question1 = "What is the capital of France?"; -let answers1 = ["Berlin", "Madrid", "Paris"]; +let answers1 = ["Berlin", "Paris", "Madrid"]; user_answers.push(ask_question(question1, &answers1)); let question2 = "What is the chemical symbol for gold?"; @@ -30,7 +30,35 @@ user_answers.push(ask_question(question3, &answers3)); ## 2. Update the Program -Now we need to update the program to be proven with the new correct answers. As we saw in [Build your first Aligned Application](2_build_your_first_aligned_application.md), the program in `examples/zkquiz/quiz/program/src/main.rs` takes the user answers as inputs and checks that the hash of these inputs matches the expected output. Therefore, we need to update the expected output with the hash of our new correct answers. The updated example looks as follows: +Now we need to update the program to be proven with the new correct answers. As we saw in [Build your first Aligned Application](2_build_your_first_aligned_application.md), the program in `examples/zkquiz/quiz/program/src/main.rs` takes the user answers as inputs and checks that the SHA3-256 hash of these inputs matches the expected output. Therefore, we need to update the expected output with the hash of our new correct answers. + +If we concatenate the correct answers to the questions above, we get `bab`, so we need to calculate the SHA3-256 hash of that: + +``` +SHA3-256(bab) +``` + +For that, you can use any SHA3-256 rust library or even online tools. Here we provide a python script that calculates it for you: +```python +import hashlib + +correct_answers = "bab" + +# Calculate SHA3-256 hash +hash_object = hashlib.sha3_256(correct_answers.encode()) + +# Get the hash as a list of integers (byte values) +hash_bytes = list(hash_object.digest()) + +print(hash_bytes) +``` + +After executing the script, we get: +```python +[216, 11, 193, 177, 136, 178, 254, 150, 59, 128, 97, 103, 97, 128, 55, 57, 22, 242, 26, 1, 186, 223, 215, 118, 206, 47, 12, 206, 114, 118, 220, 93] +``` + +Now we can update it in `examples/zkquiz/quiz/program/src/main.rs` as follows: ```Rust if output From 6d79688d95659507af682f58e47a7c780bf0f887 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 10 Oct 2024 13:41:38 -0300 Subject: [PATCH 6/9] refactor: add make target to compile the ELF --- docs/3_guides/2.2_modify_zkquiz_questions.md | 9 ++++----- examples/zkquiz/Makefile | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/3_guides/2.2_modify_zkquiz_questions.md b/docs/3_guides/2.2_modify_zkquiz_questions.md index 0eaed87e09..bdba32f73d 100644 --- a/docs/3_guides/2.2_modify_zkquiz_questions.md +++ b/docs/3_guides/2.2_modify_zkquiz_questions.md @@ -30,7 +30,7 @@ user_answers.push(ask_question(question3, &answers3)); ## 2. Update the Program -Now we need to update the program to be proven with the new correct answers. As we saw in [Build your first Aligned Application](2_build_your_first_aligned_application.md), the program in `examples/zkquiz/quiz/program/src/main.rs` takes the user answers as inputs and checks that the SHA3-256 hash of these inputs matches the expected output. Therefore, we need to update the expected output with the hash of our new correct answers. +Next, we need to update the program to be proven with the new correct answers. As described in [Build your first Aligned Application](2_build_your_first_aligned_application.md), the program in `examples/zkquiz/quiz/program/src/main.rs` takes the user answers as inputs and checks that the SHA3-256 hash of these inputs matches the expected output. Therefore, we need to update the expected output with the hash of our new correct answers. If we concatenate the correct answers to the questions above, we get `bab`, so we need to calculate the SHA3-256 hash of that: @@ -38,7 +38,7 @@ If we concatenate the correct answers to the questions above, we get `bab`, so w SHA3-256(bab) ``` -For that, you can use any SHA3-256 rust library or even online tools. Here we provide a python script that calculates it for you: +You can use any SHA3-256 Rust library or even online tools for this purpose. Here we provide a python script that calculates it for you: ```python import hashlib @@ -74,11 +74,10 @@ if output ## 3. Compile the Program Now we need to compile the updated Program, generating the binary file that will be run by the zkVM (ELF). -For that you can use the [SP1 Rust toolchain](https://docs.succinct.xyz/introduction.html). +For this, ensure that the [SP1 Rust toolchain](https://docs.succinct.xyz/introduction.html) is installed. Run: ``` -cd examples/zkquiz/quiz/program -cargo prove build +make compile_elf ``` which will output the compiled ELF to the file program/elf/riscv32im-succinct-zkvm-elf. diff --git a/examples/zkquiz/Makefile b/examples/zkquiz/Makefile index 24fc09157b..d3255f04c8 100644 --- a/examples/zkquiz/Makefile +++ b/examples/zkquiz/Makefile @@ -10,3 +10,6 @@ answer_quiz: --keystore-path $(KEYSTORE_PATH) \ --rpc-url $(RPC_URL) \ --verifier-contract-address $(CONTRACT_ADDRESS) + +compile_elf: + cd quiz/program && cargo prove build \ No newline at end of file From ef87016c8c330e72eb79f50a62a0ef4769fbc50b Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 11 Oct 2024 16:44:30 -0300 Subject: [PATCH 7/9] fix: conflicts --- examples/zkquiz/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/zkquiz/Makefile b/examples/zkquiz/Makefile index 3565bd96db..6797058612 100644 --- a/examples/zkquiz/Makefile +++ b/examples/zkquiz/Makefile @@ -22,4 +22,4 @@ answer_quiz_local: --verifier-contract-address $(CONTRACT_ADDRESS) compile_elf: - cd quiz/program && cargo prove build \ No newline at end of file + cd quiz/program && cargo prove build From cbfa1c0b9c91ae0ffe710aadb0b05410d4b54543 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 14 Oct 2024 10:58:20 -0300 Subject: [PATCH 8/9] refactor: add the new section as a submodule --- docs/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 87fd98f1e8..052866b8ca 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -25,7 +25,7 @@ * [Submitting proofs](3_guides/0_submitting_proofs.md) * [Build your first Aligned Application](3_guides/2_build_your_first_aligned_application.md) -* [Modify ZkQuiz Questions](3_guides/2.2_modify_zkquiz_questions.md) + * [Modify ZkQuiz Questions](3_guides/2.2_modify_zkquiz_questions.md) * [Validating public input](3_guides/3_validating_public_input.md) * [SDK Intro](3_guides/1_SDK_how_to.md) * [SDK API Reference](3_guides/1.2_SDK_api_reference.md) From 575475313ad2951724712844aeffd28f52bb6e65 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 14 Oct 2024 11:22:24 -0300 Subject: [PATCH 9/9] fix: use 4 spaces when tabulating --- docs/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 052866b8ca..0a342b2c96 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -25,7 +25,7 @@ * [Submitting proofs](3_guides/0_submitting_proofs.md) * [Build your first Aligned Application](3_guides/2_build_your_first_aligned_application.md) - * [Modify ZkQuiz Questions](3_guides/2.2_modify_zkquiz_questions.md) + * [Modify ZkQuiz Questions](3_guides/2.2_modify_zkquiz_questions.md) * [Validating public input](3_guides/3_validating_public_input.md) * [SDK Intro](3_guides/1_SDK_how_to.md) * [SDK API Reference](3_guides/1.2_SDK_api_reference.md)