Skip to content

Commit

Permalink
Merge testnet 14 oct (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauroToscano authored Oct 14, 2024
2 parents ae8d8f3 + ab37cfc commit 5143a3e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
89 changes: 89 additions & 0 deletions docs/3_guides/2.2_modify_zkquiz_questions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# 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 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)
{% 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. 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", "Paris", "Madrid"];
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

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:

```
SHA3-256(bab)
```

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

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
!= [
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 this, ensure that the [SP1 Rust toolchain](https://docs.succinct.xyz/introduction.html) is installed. Run:

```
make compile_elf
```

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)
2 changes: 1 addition & 1 deletion docs/3_guides/2_build_your_first_aligned_application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
Expand Down
3 changes: 3 additions & 0 deletions examples/zkquiz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ answer_quiz_local:
--batcher-url ws://localhost:8080 \
--network devnet \
--verifier-contract-address $(CONTRACT_ADDRESS)

compile_elf:
cd quiz/program && cargo prove build

0 comments on commit 5143a3e

Please sign in to comment.