From f84faaae834a9276ad820ed7e9d8561c61ade04d Mon Sep 17 00:00:00 2001 From: Ahmed Castro Date: Sat, 18 Mar 2023 20:09:28 -0600 Subject: [PATCH 1/5] Sudoku example fix **Problem** the logic behind `countDuplicates` is invalid because it's doing this: (incorrect) ``` if(duplicates + e11 == e21) duplicates = 1; else duplicates = 0; ``` Instead of this: (correct) ``` if(e11 == e21) duplicates = duplicates + 1; else duplicates = duplicates; ``` **Solution** I'm using an auxiliary variable to fix this **Alternative solution** We can also fix it with an array and a loop. But I went for the auxiliary variable for no particular reason. ``` def countDuplicates(field e11, field e12, field e21, field e22) -> u32 { u32[6] mut duplicates = [0,0,0,0,0,0]; duplicates[0] = e11 == e12 ? 1 : 0; duplicates[1] = e11 == e21 ? 1 : 0; duplicates[2] = e11 == e22 ? 1 : 0; duplicates[3] = e12 == e21 ? 1 : 0; duplicates[4] = e12 == e22 ? 1 : 0; duplicates[5] = e21 == e22 ? 1 : 0; u32 mut count = 0; for u32 i in 0..5 { count = count + duplicates[i]; } return count; } ``` And btw, I also added an assert to make sure we check for this validation. I've tested this on the playground and on chain on goerli and with this changes it works correctly. --- .../examples/sudoku/sudoku_checker.zok | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/zokrates_cli/examples/sudoku/sudoku_checker.zok b/zokrates_cli/examples/sudoku/sudoku_checker.zok index d6b596f35..2272a42e9 100644 --- a/zokrates_cli/examples/sudoku/sudoku_checker.zok +++ b/zokrates_cli/examples/sudoku/sudoku_checker.zok @@ -11,13 +11,20 @@ // 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 mut duplicates = e11 == e12 ? 1 : 0; - duplicates = duplicates + e11 == e21 ? 1 : 0; - duplicates = duplicates + e11 == e22 ? 1 : 0; - duplicates = duplicates + e12 == e21 ? 1 : 0; - duplicates = duplicates + e12 == e22 ? 1 : 0; - duplicates = duplicates + e21 == e22 ? 1 : 0; - return duplicates; + field mut duplicates = 0; + field mut duplicatesAux = e11 == e12 ? 1 : 0; + duplicates = duplicates + duplicatesAux; + duplicatesAux = e11 == e21 ? 1 : 0; + duplicates = duplicates + duplicatesAux; + duplicatesAux = e11 == e22 ? 1 : 0; + duplicates = duplicates + duplicatesAux; + duplicatesAux = e12 == e21 ? 1 : 0; + duplicates = duplicates + duplicatesAux; + duplicatesAux = e12 == e22 ? 1 : 0; + duplicates = duplicates + duplicatesAux; + duplicatesAux = e21 == e22 ? 1 : 0; + + return countDuplicates; } // returns 0 for x in (1..4) @@ -74,5 +81,6 @@ def main(field a21, field b11, field b22, field c11, field c22, field d21, priva duplicates = duplicates + countDuplicates(b12, b22, d12, d22); // the solution is correct if and only if there are no duplicates + assert(duplicates == 0); return duplicates == 0; } From 2e551b3d161c4f4cc81ee99472ad3a2d2dcad6f2 Mon Sep 17 00:00:00 2001 From: Ahmed Castro Date: Tue, 21 Mar 2023 11:53:10 -0600 Subject: [PATCH 2/5] Update sudoku_checker.zok --- .../examples/sudoku/sudoku_checker.zok | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/zokrates_cli/examples/sudoku/sudoku_checker.zok b/zokrates_cli/examples/sudoku/sudoku_checker.zok index 2272a42e9..7fc6e2252 100644 --- a/zokrates_cli/examples/sudoku/sudoku_checker.zok +++ b/zokrates_cli/examples/sudoku/sudoku_checker.zok @@ -11,20 +11,13 @@ // 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 mut duplicates = 0; - field mut duplicatesAux = e11 == e12 ? 1 : 0; - duplicates = duplicates + duplicatesAux; - duplicatesAux = e11 == e21 ? 1 : 0; - duplicates = duplicates + duplicatesAux; - duplicatesAux = e11 == e22 ? 1 : 0; - duplicates = duplicates + duplicatesAux; - duplicatesAux = e12 == e21 ? 1 : 0; - duplicates = duplicates + duplicatesAux; - duplicatesAux = e12 == e22 ? 1 : 0; - duplicates = duplicates + duplicatesAux; - duplicatesAux = e21 == e22 ? 1 : 0; - - return countDuplicates; + field mut duplicates = e11 == e12 ? 1 : 0; + duplicates = duplicates + (e11 == e21 ? 1 : 0); + duplicates = duplicates + (e11 == e22 ? 1 : 0); + duplicates = duplicates + (e12 == e21 ? 1 : 0); + duplicates = duplicates + (e12 == e22 ? 1 : 0); + duplicates = duplicates + (e21 == e22 ? 1 : 0); + return duplicates; } // returns 0 for x in (1..4) @@ -33,7 +26,7 @@ def validateInput(field x) -> bool { } // 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) -> bool { +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) { // validate inputs assert(validateInput(a11)); @@ -82,5 +75,6 @@ def main(field a21, field b11, field b22, field c11, field c22, field d21, priva // the solution is correct if and only if there are no duplicates assert(duplicates == 0); - return duplicates == 0; + + return; } From 30c9d8b8b187a019ef7c735f681b221dd3ff281d Mon Sep 17 00:00:00 2001 From: Ahmed Castro Date: Fri, 24 Mar 2023 16:36:49 -0600 Subject: [PATCH 3/5] Update sudoku_checker.zok --- zokrates_cli/examples/sudoku/sudoku_checker.zok | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/zokrates_cli/examples/sudoku/sudoku_checker.zok b/zokrates_cli/examples/sudoku/sudoku_checker.zok index 7fc6e2252..b77748f42 100644 --- a/zokrates_cli/examples/sudoku/sudoku_checker.zok +++ b/zokrates_cli/examples/sudoku/sudoku_checker.zok @@ -74,7 +74,5 @@ def main(field a21, field b11, field b22, field c11, field c22, field d21, priva duplicates = duplicates + countDuplicates(b12, b22, d12, d22); // the solution is correct if and only if there are no duplicates - assert(duplicates == 0); - - return; + return duplicates == 0; } From 48885d64a4ecced90cda65392eded9dcd3234a5b Mon Sep 17 00:00:00 2001 From: Ahmed Castro Date: Fri, 24 Mar 2023 16:37:58 -0600 Subject: [PATCH 4/5] Update sudoku_checker.zok --- zokrates_cli/examples/sudoku/sudoku_checker.zok | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zokrates_cli/examples/sudoku/sudoku_checker.zok b/zokrates_cli/examples/sudoku/sudoku_checker.zok index b77748f42..4c01df6ec 100644 --- a/zokrates_cli/examples/sudoku/sudoku_checker.zok +++ b/zokrates_cli/examples/sudoku/sudoku_checker.zok @@ -26,7 +26,7 @@ def validateInput(field x) -> bool { } // 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) { +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 assert(validateInput(a11)); From 2ba870a9756824562db9e539360ccffbb9091fc1 Mon Sep 17 00:00:00 2001 From: Ahmed Castro Date: Mon, 10 Apr 2023 20:09:01 -0600 Subject: [PATCH 5/5] Create 1287-Turupawn --- changelogs/unreleased/1287-Turupawn | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelogs/unreleased/1287-Turupawn diff --git a/changelogs/unreleased/1287-Turupawn b/changelogs/unreleased/1287-Turupawn new file mode 100644 index 000000000..7cb706c0d --- /dev/null +++ b/changelogs/unreleased/1287-Turupawn @@ -0,0 +1 @@ +Fixed precedence issue on Sudoku example.