From 50be20e8ba27911eff1bd8982b938ba109e80a3a Mon Sep 17 00:00:00 2001 From: schaeff Date: Mon, 13 Jul 2020 17:05:59 +0200 Subject: [PATCH] complete changes --- .../examples/functions/lt_comparison.zok | 16 +--- .../merkleTree/pedersenPathProof3.zok | 6 +- .../examples/merkleTree/sha256PathProof3.zok | 7 +- .../runtime_errors/lt_overflow_max_plus_1.zok | 4 +- .../lt_overflow_p_minus_one.zok | 4 +- .../examples/sudoku/sudoku_checker.zok | 94 +++++++++---------- zokrates_cli/examples/test_lt_max_value.zok | 4 +- 7 files changed, 67 insertions(+), 68 deletions(-) diff --git a/zokrates_cli/examples/functions/lt_comparison.zok b/zokrates_cli/examples/functions/lt_comparison.zok index edeb29d9..8de0ca28 100644 --- a/zokrates_cli/examples/functions/lt_comparison.zok +++ b/zokrates_cli/examples/functions/lt_comparison.zok @@ -1,23 +1,17 @@ -def lt(field a,field b) -> (field): - return if a < b then 1 else 0 fi +def lt(field a,field b) -> (bool): + return a < b def cutoff() -> (field): return 31337 def getThing(field index) -> (field): - field result = 3 - result = if index == 0 then 13 else result fi - result = if index == 1 then 23 else result fi - result = if index == 2 then 43 else result fi - result = if index == 3 then 53 else result fi - result = if index == 4 then 73 else result fi - result = if index == 5 then 83 else result fi - return result + field[6] a = [13, 23, 43, 53, 73, 83] + return a[index] def cubeThing(field thing) -> (field): return thing**3 -def main(field index) -> (field): +def main(field index) -> (bool): field thing = getThing(index) thing = cubeThing(thing) return lt(cutoff(), thing) diff --git a/zokrates_cli/examples/merkleTree/pedersenPathProof3.zok b/zokrates_cli/examples/merkleTree/pedersenPathProof3.zok index 1d1a419b..c4d91429 100644 --- a/zokrates_cli/examples/merkleTree/pedersenPathProof3.zok +++ b/zokrates_cli/examples/merkleTree/pedersenPathProof3.zok @@ -6,7 +6,7 @@ import "hashes/utils/256bitsDirectionHelper" as multiplex // Merke-Tree inclusion proof for tree depth 3 using SNARK efficient pedersen hashes // directionSelector=> 1/true if current digest is on the rhs of the hash -def main(bool[256] rootDigest, private bool[256] leafDigest, private bool[3] directionSelector, bool[256] PathDigest0, private bool[256] PathDigest1, private bool[256] PathDigest2) -> (bool): +def main(bool[256] rootDigest, private bool[256] leafDigest, private bool[3] directionSelector, bool[256] PathDigest0, private bool[256] PathDigest1, private bool[256] PathDigest2) -> (): BabyJubJubParams context = context() //Setup @@ -22,5 +22,7 @@ def main(bool[256] rootDigest, private bool[256] leafDigest, private bool[3] dir preimage = multiplex(directionSelector[2], currentDigest, PathDigest2) currentDigest = hash(preimage) - return rootDigest == currentDigest + rootDigest == currentDigest + + return diff --git a/zokrates_cli/examples/merkleTree/sha256PathProof3.zok b/zokrates_cli/examples/merkleTree/sha256PathProof3.zok index 04cc5eb4..4cdea787 100644 --- a/zokrates_cli/examples/merkleTree/sha256PathProof3.zok +++ b/zokrates_cli/examples/merkleTree/sha256PathProof3.zok @@ -3,7 +3,7 @@ import "utils/multiplexer/256bit" as multiplex // Merkle-Tree inclusion proof for tree depth 3 -def main(field treeDepth, bool[256] rootDigest, private bool[256] leafDigest, private bool[2] directionSelector, bool[256] PathDigest0, private bool[256] PathDigest1) -> (field): +def main(field treeDepth, bool[256] rootDigest, private bool[256] leafDigest, private bool[2] directionSelector, bool[256] PathDigest0, private bool[256] PathDigest1) -> (): //Setup bool[256] currentDigest = leafDigest @@ -23,4 +23,7 @@ def main(field treeDepth, bool[256] rootDigest, private bool[256] leafDigest, pr currentDigest = sha256(lhs, rhs) counter = counter + 1 - return counter == treeDepth && rootDigest == currentDigest \ No newline at end of file + counter == treeDepth + rootDigest == currentDigest + + return \ No newline at end of file diff --git a/zokrates_cli/examples/runtime_errors/lt_overflow_max_plus_1.zok b/zokrates_cli/examples/runtime_errors/lt_overflow_max_plus_1.zok index 0c1b91eb..41dc0299 100644 --- a/zokrates_cli/examples/runtime_errors/lt_overflow_max_plus_1.zok +++ b/zokrates_cli/examples/runtime_errors/lt_overflow_max_plus_1.zok @@ -4,9 +4,9 @@ // It should not work for the maxvalue = 2^(pbits - 2) - 1 augmented by one // /!\ should be called with a = 0 -def main(field a) -> (field): +def main(field a) -> (bool): field pbits = 254 // maxvalue = 2**252 - 1 field maxvalue = a + 7237005577332262213973186563042994240829374041602535252466099000494570602496 - 1 // we added a = 0 to prevent the condition to be evaluated at compile time - return if 0 < (maxvalue + 1) then 1 else 0 fi \ No newline at end of file + return 0 < (maxvalue + 1) \ No newline at end of file diff --git a/zokrates_cli/examples/runtime_errors/lt_overflow_p_minus_one.zok b/zokrates_cli/examples/runtime_errors/lt_overflow_p_minus_one.zok index 49700543..5ee8ca0d 100644 --- a/zokrates_cli/examples/runtime_errors/lt_overflow_p_minus_one.zok +++ b/zokrates_cli/examples/runtime_errors/lt_overflow_p_minus_one.zok @@ -1,7 +1,7 @@ // as p - 1 is greater than p/2, comparing to it should fail // /!\ should be called with a = 0 -def main(field a) -> (field): +def main(field a) -> (bool): field p = 21888242871839275222246405745257275088548364400416034343698204186575808495617 + a // we added a = 0 to prevent the condition to be evaluated at compile time - return if 0 < p - 1 then 1 else 0 fi \ No newline at end of file + return 0 < p - 1 \ No newline at end of file diff --git a/zokrates_cli/examples/sudoku/sudoku_checker.zok b/zokrates_cli/examples/sudoku/sudoku_checker.zok index 8e5ae015..fa692c9f 100644 --- a/zokrates_cli/examples/sudoku/sudoku_checker.zok +++ b/zokrates_cli/examples/sudoku/sudoku_checker.zok @@ -8,68 +8,68 @@ // -------------------------- // | c21 | c22 || d21 | d22 | -def checkEquality(field e11,field e12,field e21,field e22) -> (field): -field counter = if e11 == e12 then 1 else 0 fi -counter = counter + if e11 == e21 then 1 else 0 fi -counter = counter + if e11 == e22 then 1 else 0 fi -counter = counter + if e12 == e21 then 1 else 0 fi -counter = counter + if e12 == e21 then 1 else 0 fi -counter = counter + if e21 == e22 then 1 else 0 fi -return counter +// We use a naive encoding of the values as `[1, 2, 3, 4]` and rely on if-else statements to detect duplicates + +def countDuplicates(field e11,field e12,field e21,field e22) -> (field): + field duplicates = if e11 == e12 then 1 else 0 fi + duplicates = duplicates + if e11 == e21 then 1 else 0 fi + duplicates = duplicates + if e11 == e22 then 1 else 0 fi + duplicates = duplicates + if e12 == e21 then 1 else 0 fi + duplicates = duplicates + if e12 == e21 then 1 else 0 fi + duplicates = duplicates + if e21 == e22 then 1 else 0 fi + return duplicates // returns 0 for x in (1..4) def validateInput(field x) -> (field): -return (x-1)*(x-2)*(x-3)*(x-4) + return (x-1)*(x-2)*(x-3)*(x-4) // variables naming: box'row''column' -def main(field a21, field b11, field b22, field c11, field c22, field d21, private field a11, private field a12, private field a22, private field b12, private field b21, private field c12, private field c21, private field d11, private field d12, private field d22) -> (field): +def main(field a21, field b11, field b22, field c11, field c22, field d21, private field a11, private field a12, private field a22, private field b12, private field b21, private field c12, private field c21, private field d11, private field d12, private field d22) -> (bool): -// validate inputs -0 == validateInput(a11) -0 == validateInput(a12) -0 == validateInput(a21) -0 == validateInput(a22) + // validate inputs + 0 == validateInput(a11) + 0 == validateInput(a12) + 0 == validateInput(a21) + 0 == validateInput(a22) -0 == validateInput(b11) -0 == validateInput(b12) -0 == validateInput(b21) -0 == validateInput(b22) + 0 == validateInput(b11) + 0 == validateInput(b12) + 0 == validateInput(b21) + 0 == validateInput(b22) -0 == validateInput(c11) -0 == validateInput(c12) -0 == validateInput(c21) -0 == validateInput(c22) + 0 == validateInput(c11) + 0 == validateInput(c12) + 0 == validateInput(c21) + 0 == validateInput(c22) -0 == validateInput(d11) -0 == validateInput(d12) -0 == validateInput(d21) -0 == validateInput(d22) + 0 == validateInput(d11) + 0 == validateInput(d12) + 0 == validateInput(d21) + 0 == validateInput(d22) -field counter = 0 // globally counts duplicate entries in boxes, rows and columns + field duplicates = 0 // globally counts duplicate entries in boxes, rows and columns -// check box correctness + // check box correctness // no duplicates - counter = counter + checkEquality(a11,a12,a21,a22) - counter = counter + checkEquality(b11,b12,b21,b22) - counter = counter + checkEquality(c11,c12,c21,c22) - counter = counter + checkEquality(d11,d12,d21,d22) + duplicates = duplicates + countDuplicates(a11,a12,a21,a22) + duplicates = duplicates + countDuplicates(b11,b12,b21,b22) + duplicates = duplicates + countDuplicates(c11,c12,c21,c22) + duplicates = duplicates + countDuplicates(d11,d12,d21,d22) -// check row correctness + // check row correctness - counter = counter + checkEquality(a11,a12,b11,b12) - counter = counter + checkEquality(a21,a22,b21,b22) - counter = counter + checkEquality(c11,c12,d11,d12) - counter = counter + checkEquality(c21,c22,d21,d22) + duplicates = duplicates + countDuplicates(a11,a12,b11,b12) + duplicates = duplicates + countDuplicates(a21,a22,b21,b22) + duplicates = duplicates + countDuplicates(c11,c12,d11,d12) + duplicates = duplicates + countDuplicates(c21,c22,d21,d22) -// check column correctness + // check column correctness - counter = counter + checkEquality(a11,a21,c11,c21) - counter = counter + checkEquality(a12,a22,c12,c22) - counter = counter + checkEquality(b11,b21,d11,d21) - counter = counter + checkEquality(b12,b22,d12,d22) + duplicates = duplicates + countDuplicates(a11,a21,c11,c21) + duplicates = duplicates + countDuplicates(a12,a22,c12,c22) + duplicates = duplicates + countDuplicates(b11,b21,d11,d21) + duplicates = duplicates + countDuplicates(b12,b22,d12,d22) -// assert counter is 0 -counter == 0 - -return 1 + // the solution is correct if and only if there are no duplicates + return duplicates == 0 diff --git a/zokrates_cli/examples/test_lt_max_value.zok b/zokrates_cli/examples/test_lt_max_value.zok index 20fdbdef..8860cd40 100644 --- a/zokrates_cli/examples/test_lt_max_value.zok +++ b/zokrates_cli/examples/test_lt_max_value.zok @@ -3,8 +3,8 @@ // we can compare numbers up to 2^(pbits - 2) - 1, ie any number which fits in (pbits - 2) bits // lt should work for the maxvalue = 2^(pbits - 2) - 1 -def main(field a) -> (field): +def main(field a) -> (bool): field pbits = 254 // maxvalue = 2**252 - 1 field maxvalue = 7237005577332262213973186563042994240829374041602535252466099000494570602496 - 1 - return if 0 < maxvalue then 1 else 0 fi \ No newline at end of file + return 0 < maxvalue \ No newline at end of file