From 0ba730a80f6bd15660850b43ca06d16a5e57e43c Mon Sep 17 00:00:00 2001 From: Wanseob Lim Date: Sun, 3 May 2020 15:36:40 +0900 Subject: [PATCH 1/3] Add matrix multiplication example for array input usage --- TUTORIAL.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/TUTORIAL.md b/TUTORIAL.md index 647f16b..a068eb7 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -239,6 +239,59 @@ The `===` operator adds a constraint without assigning any value to a signal. The circuit also has another problem: the operation works in `Z_r`, so we need to guarantee the multiplication does not overflow. This can be done by converting the inputs to binary and checking the ranges, but we will reserve it for future tutorials. +## Bonus track 2 + +You can also write a complex circuit using array or nested array. For example, you want to prove that you have two private matrices A, B and reveal its multiplication result AB. To make a flexible template, we'll asusme that A is a m x n matrix and B is a n x p matrix. + +``` +template Multiplier() { + signal input a; + signal input b; + signal output c; + c <== a*b; +} + +template MatrixMultiplier(m, n, p) { + signal input a[m][n]; + signal input b[n][p]; + signal input ab[m][p]; + component intermediates[m][p][n]; + for(var row = 0; row < m; row++) { + for(var col = 0; col < p; col++) { + var sum = 0; + for(var i = 0; i < n; i++) { + intermediates[row][col][i] = Multiplier(); + intermediates[row][col][i].a <== a[row][i]; + intermediates[row][col][i].b <== b[i][col]; + sum = sum + intermediates[row][col][i].c + } + ab[row][col] === sum; + } + } +} + +component main = MatrixMultiplier(2, 3, 4); +``` + +Using this template, you should make different circuits when m, n, and p values vary. Here, we'll make a circuit which proves the matrix multiplication resultlt of 2x3 matrix and 3x4 matrix. With the circuir we will prove +``` +┌ ┐ ┌ ┐ ┌ ┐ +│ 0 -1 3 │ x │ -2 11 12 13 │ = │ 4 9 4 1 │ +│ 2 1 4 │ │ -4 -3 -1 14 │ │ -8 27 27 60 │ +└ ┘ │ 0 2 1 5 │ └ ┘ + └ ┘ +``` + +And then you can create an `input.json` file. +```json +{ + "a": [[0, -1, 3], [2, 1, 4]], + "b": [[-2, 11, 12, 13], [-4, -3, -1, 14], [0, 2, 1, 5]], + "ab": [[4, 9, 4, 1], [-8, 27, 27, 60]] +} +``` + + ## Where to go from here You may want to read the [README](https://github.com/iden3/circom) to learn more features about `circom`. From 4909b1c657fd21d92946711da5da6432d741ac75 Mon Sep 17 00:00:00 2001 From: Wanseob Lim Date: Sun, 3 May 2020 15:38:50 +0900 Subject: [PATCH 2/3] fix typo --- TUTORIAL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index a068eb7..a660d81 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -273,7 +273,7 @@ template MatrixMultiplier(m, n, p) { component main = MatrixMultiplier(2, 3, 4); ``` -Using this template, you should make different circuits when m, n, and p values vary. Here, we'll make a circuit which proves the matrix multiplication resultlt of 2x3 matrix and 3x4 matrix. With the circuir we will prove +Using this template, you should make different circuits when m, n, and p values vary. Here, we'll make a circuit which proves the matrix multiplication result of 2x3 matrix and 3x4 matrix. With the circuir we will prove ``` ┌ ┐ ┌ ┐ ┌ ┐ │ 0 -1 3 │ x │ -2 11 12 13 │ = │ 4 9 4 1 │ From 207a69674d7b1e790b6b797174e88dc3166669d7 Mon Sep 17 00:00:00 2001 From: Wanseob Lim Date: Sun, 3 May 2020 15:40:27 +0900 Subject: [PATCH 3/3] fix typo --- TUTORIAL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index a660d81..b38b9f0 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -273,7 +273,7 @@ template MatrixMultiplier(m, n, p) { component main = MatrixMultiplier(2, 3, 4); ``` -Using this template, you should make different circuits when m, n, and p values vary. Here, we'll make a circuit which proves the matrix multiplication result of 2x3 matrix and 3x4 matrix. With the circuir we will prove +Using this template, you should make different circuits when m, n, and p values vary. Here, we'll make a circuit which proves the matrix multiplication result of 2x3 matrix and 3x4 matrix. With the circuit we will prove ``` ┌ ┐ ┌ ┐ ┌ ┐ │ 0 -1 3 │ x │ -2 11 12 13 │ = │ 4 9 4 1 │