fix integration test, update book
This commit is contained in:
parent
5257d309c5
commit
2e9c66a5c1
24 changed files with 96 additions and 58 deletions
|
@ -80,6 +80,40 @@ impl<T: Field> Witness<T> {
|
|||
serde_json::to_writer_pretty(writer, &map)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_json<R: Read>(reader: R) -> io::Result<Self> {
|
||||
let json: serde_json::Value = serde_json::from_reader(reader)?;
|
||||
let object = json
|
||||
.as_object()
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Witness must be an object"))?;
|
||||
|
||||
let mut witness = Witness::empty();
|
||||
for (k, v) in object {
|
||||
let variable = Variable::try_from_human_readable(k).map_err(|why| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Invalid variable in witness: {}", why),
|
||||
)
|
||||
})?;
|
||||
|
||||
let value = v
|
||||
.as_str()
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::Other, "Witness value must be a string")
|
||||
})
|
||||
.and_then(|v| {
|
||||
T::try_from_dec_str(v).map_err(|_| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Invalid value in witness: {}", v),
|
||||
)
|
||||
})
|
||||
})?;
|
||||
|
||||
witness.insert(variable, value);
|
||||
}
|
||||
Ok(witness)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> fmt::Display for Witness<T> {
|
||||
|
|
|
@ -43,20 +43,14 @@ As a next step we can create a witness file using the following command:
|
|||
|
||||
Using the flag `-a` we pass arguments to the program. Recall that our goal is to compute the hash for the number `5`. Consequently we set `a`, `b` and `c` to `0` and `d` to `5`.
|
||||
|
||||
Still here? Great! At this point, we can check the `witness` file for the return values:
|
||||
Still here? Great! At this point we can check the return values. We should see the following output:
|
||||
|
||||
```
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:13}}
|
||||
Witness:
|
||||
["263561599766550617289250058199814760685","65303172752238645975888084098459749904"]
|
||||
```
|
||||
|
||||
which should lead to the following output:
|
||||
|
||||
```sh
|
||||
~out_0 263561599766550617289250058199814760685
|
||||
~out_1 65303172752238645975888084098459749904
|
||||
```
|
||||
|
||||
Hence, by concatenating the outputs as 128 bit numbers, we arrive at the following value as the hash for our selected pre-image :
|
||||
By concatenating the outputs as 128 bit numbers, we arrive at the following value as the hash for our selected pre-image :
|
||||
`0xc6481e22c5ff4164af680b8cfaa5e8ed3120eeff89c4f307c4a6faaae059ce10`
|
||||
|
||||
## Prove knowledge of pre-image
|
||||
|
@ -78,13 +72,13 @@ Note that we now compare the result of `sha256packed` with the hard-coded correc
|
|||
So, having defined the program, Victor is now ready to compile the code:
|
||||
|
||||
```
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:17}}
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:15}}
|
||||
```
|
||||
|
||||
Based on that Victor can run the setup phase and export a verifier smart contract as a Solidity file:
|
||||
|
||||
```
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:18:19}}
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:16:17}}
|
||||
```
|
||||
|
||||
`setup` creates a `verification.key` file and a `proving.key` file. Victor gives the proving key to Peggy.
|
||||
|
@ -94,13 +88,13 @@ Based on that Victor can run the setup phase and export a verifier smart contrac
|
|||
Peggy provides the correct pre-image as an argument to the program.
|
||||
|
||||
```
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:20}}
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:18}}
|
||||
```
|
||||
|
||||
Finally, Peggy can run the command to construct the proof:
|
||||
|
||||
```
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:21}}
|
||||
{{#include ../../../zokrates_cli/examples/book/sha256_tutorial/test.sh:19}}
|
||||
```
|
||||
|
||||
As the inputs were declared as private in the program, they do not appear in the proof thanks to the zero-knowledge property of the protocol.
|
||||
|
|
|
@ -8,9 +8,7 @@ function zokrates() {
|
|||
}
|
||||
|
||||
zokrates compile -i hashexample.zok
|
||||
zokrates compute-witness -a 0 0 0 5
|
||||
|
||||
grep '~out' witness
|
||||
zokrates compute-witness -a 0 0 0 5 --verbose
|
||||
|
||||
cp -f hashexample_updated.zok hashexample.zok
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
~out_0 12
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "12"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 0
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "0"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 1
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "1"
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
~out_0 0
|
||||
~out_1 0
|
||||
~out_2 0
|
||||
~out_3 42
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"~out_0": "0",
|
||||
"~out_1": "0",
|
||||
"~out_2": "0",
|
||||
"~out_3": "42"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 5
|
3
zokrates_cli/tests/code/n_choose_k.expected.witness.json
Normal file
3
zokrates_cli/tests/code/n_choose_k.expected.witness.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "5"
|
||||
}
|
1
zokrates_cli/tests/code/no_return.expected.witness.json
Normal file
1
zokrates_cli/tests/code/no_return.expected.witness.json
Normal file
|
@ -0,0 +1 @@
|
|||
{}
|
|
@ -1,8 +0,0 @@
|
|||
~out_0 2
|
||||
~out_1 1
|
||||
~out_2 1
|
||||
~out_3 1
|
||||
~out_4 3
|
||||
~out_5 3
|
||||
~out_6 3
|
||||
~out_7 3
|
10
zokrates_cli/tests/code/return_array.expected.witness.json
Normal file
10
zokrates_cli/tests/code/return_array.expected.witness.json
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"~out_0": "2",
|
||||
"~out_1": "1",
|
||||
"~out_2": "1",
|
||||
"~out_3": "1",
|
||||
"~out_4": "3",
|
||||
"~out_5": "3",
|
||||
"~out_6": "3",
|
||||
"~out_7": "3"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 3
|
3
zokrates_cli/tests/code/simple_add.expected.witness.json
Normal file
3
zokrates_cli/tests/code/simple_add.expected.witness.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "3"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 24
|
3
zokrates_cli/tests/code/simple_mul.expected.witness.json
Normal file
3
zokrates_cli/tests/code/simple_mul.expected.witness.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "24"
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
~out_0 0
|
3
zokrates_cli/tests/code/taxation.expected.witness.json
Normal file
3
zokrates_cli/tests/code/taxation.expected.witness.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"~out_0": "0"
|
||||
}
|
|
@ -16,11 +16,11 @@ mod integration {
|
|||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read, Write};
|
||||
use std::panic;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use tempdir::TempDir;
|
||||
use zokrates_abi::{parse_strict, Encode};
|
||||
use zokrates_ast::ir::Witness;
|
||||
use zokrates_ast::typed::abi::Abi;
|
||||
use zokrates_field::Bn128Field;
|
||||
use zokrates_proof_systems::{
|
||||
|
@ -101,7 +101,9 @@ mod integration {
|
|||
let program_name =
|
||||
Path::new(Path::new(path.file_stem().unwrap()).file_stem().unwrap());
|
||||
let prog = dir.join(program_name).with_extension("zok");
|
||||
let witness = dir.join(program_name).with_extension("expected.witness");
|
||||
let witness = dir
|
||||
.join(program_name)
|
||||
.with_extension("expected.witness.json");
|
||||
let json_input = dir.join(program_name).with_extension("arguments.json");
|
||||
|
||||
test_compile_and_witness(
|
||||
|
@ -250,33 +252,24 @@ mod integration {
|
|||
.unwrap();
|
||||
|
||||
// load the expected witness
|
||||
let mut expected_witness_file = File::open(&expected_witness_path).unwrap();
|
||||
let mut expected_witness = String::new();
|
||||
expected_witness_file
|
||||
.read_to_string(&mut expected_witness)
|
||||
.unwrap();
|
||||
let expected_witness_file = File::open(&expected_witness_path).unwrap();
|
||||
let expected_witness =
|
||||
Witness::<zokrates_field::Bn128Field>::read_json(expected_witness_file).unwrap();
|
||||
|
||||
// load the actual witness
|
||||
let mut witness_file = File::open(&witness_path).unwrap();
|
||||
let mut witness = String::new();
|
||||
witness_file.read_to_string(&mut witness).unwrap();
|
||||
let witness_file = File::open(&witness_path).unwrap();
|
||||
let witness = Witness::<zokrates_field::Bn128Field>::read(witness_file).unwrap();
|
||||
|
||||
// load the actual inline witness
|
||||
let mut inline_witness_file = File::open(&inline_witness_path).unwrap();
|
||||
let mut inline_witness = String::new();
|
||||
inline_witness_file
|
||||
.read_to_string(&mut inline_witness)
|
||||
.unwrap();
|
||||
let inline_witness_file = File::open(&inline_witness_path).unwrap();
|
||||
let inline_witness =
|
||||
Witness::<zokrates_field::Bn128Field>::read(inline_witness_file).unwrap();
|
||||
|
||||
assert_eq!(inline_witness, witness);
|
||||
|
||||
for line in expected_witness.as_str().split('\n') {
|
||||
assert!(
|
||||
witness.contains(line),
|
||||
"Witness generation failed for {}\n\nLine \"{}\" not found in witness",
|
||||
program_path.to_str().unwrap(),
|
||||
line
|
||||
);
|
||||
for (k, v) in expected_witness.0 {
|
||||
let value = witness.0.get(&k).expect("should contain key");
|
||||
assert!(v.eq(value));
|
||||
}
|
||||
|
||||
let backends = map! {
|
||||
|
|
Loading…
Reference in a new issue