From 9cab539698265a11d3cb6f7b8a8b3b5367db5d67 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Sun, 16 Dec 2018 11:27:29 +0100 Subject: [PATCH] Substractor --- circuits/aliascheck.circom | 18 +++++++ circuits/babyjub.circom | 17 +++++++ circuits/binsub.circom | 71 +++++++++++++++++++++++++++ circuits/comparators.circom | 19 +++++++ circuits/compconstant.circom | 18 +++++++ circuits/eddsa.circom | 19 +++++++ circuits/escalarmul.circom | 19 +++++++ circuits/escalarmulany.circom | 18 +++++++ circuits/escalarmulfix.circom | 19 +++++++ circuits/escalarmulw4table.circom | 19 +++++++ circuits/mimc.circom | 15 ++++++ circuits/montgomery.circom | 18 +++++++ circuits/multiplexer.circom | 19 +++++++ circuits/mux3.circom | 19 +++++++ circuits/mux4.circom | 15 ++++++ circuits/pedersen.circom | 19 +++++++ circuits/pedersen_old.circom | 18 +++++++ circuits/pointbits.circom | 19 +++++++ circuits/sign.circom | 19 +++++++ circuits/smt/smthash.circom | 19 +++++++ circuits/smt/smtlevins.circom | 19 +++++++ circuits/smt/smtprocessor.circom | 19 +++++++ circuits/smt/smtprocessorlevel.circom | 19 +++++++ circuits/smt/smtprocessorsm.circom | 19 +++++++ circuits/smt/smtverifier.circom | 19 +++++++ circuits/smt/smtverifierlevel.circom | 19 +++++++ circuits/smt/smtverifiersm.circom | 19 +++++++ circuits/switcher.circom | 19 +++++++ test/binsub.js | 56 +++++++++++++++++++++ test/{sum.js => binsum.js} | 0 test/circuits/binsub_test.circom | 26 ++++++++++ 31 files changed, 651 insertions(+) create mode 100644 circuits/binsub.circom create mode 100644 test/binsub.js rename test/{sum.js => binsum.js} (100%) create mode 100644 test/circuits/binsub_test.circom diff --git a/circuits/aliascheck.circom b/circuits/aliascheck.circom index e911424..c4dfad5 100644 --- a/circuits/aliascheck.circom +++ b/circuits/aliascheck.circom @@ -1,3 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ include "compconstant.circom"; diff --git a/circuits/babyjub.circom b/circuits/babyjub.circom index c7cddb1..a2e6b6b 100644 --- a/circuits/babyjub.circom +++ b/circuits/babyjub.circom @@ -1,4 +1,21 @@ +/* + Copyright 2018 0KIMS association. + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ template BabyAdd() { signal input x1; diff --git a/circuits/binsub.circom b/circuits/binsub.circom new file mode 100644 index 0000000..ec52d83 --- /dev/null +++ b/circuits/binsub.circom @@ -0,0 +1,71 @@ + /* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + +/* +This component creates a binary substraction. + + +Main Constraint: + (in[0][0] * 2^0 + in[0][1] * 2^1 + ..... + in[0][n-1] * 2^(n-1)) + + + 2^n + - (in[1][0] * 2^0 + in[1][1] * 2^1 + ..... + in[1][n-1] * 2^(n-1)) + === + out[0] * 2^0 + out[1] * 2^1 + + out[n-1] *2^(n-1) + aux + + + out[0] * (out[0] - 1) === 0 + out[1] * (out[0] - 1) === 0 + . + . + . + out[n-1] * (out[n-1] - 1) === 0 + aux * (aux-1) == 0 + +*/ + +template BinSub(n) { + signal input in[2][n]; + signal output out[n]; + + signal aux; + + var lin = 2**n; + var lout = 0; + + for (var i=0; i> i) & 1; + + // Ensure out is binary + out[i] * (out[i] - 1) === 0; + + lout = lout + out[i]*(2**i); + } + + aux <-- (lin >> n) & 1; + aux*(aux-1) === 0; + lout = lout + aux*(2**n); + + // Ensure the sum; + lin === lout; +} diff --git a/circuits/comparators.circom b/circuits/comparators.circom index 732bd6b..1a7f789 100644 --- a/circuits/comparators.circom +++ b/circuits/comparators.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "bitify.circom"; include "binsum.circom"; diff --git a/circuits/compconstant.circom b/circuits/compconstant.circom index e0422fc..96a6f1b 100644 --- a/circuits/compconstant.circom +++ b/circuits/compconstant.circom @@ -1,3 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ include "bitify.circom"; diff --git a/circuits/eddsa.circom b/circuits/eddsa.circom index f1acbfe..37e4a7e 100644 --- a/circuits/eddsa.circom +++ b/circuits/eddsa.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "compconstant.circom"; include "pointbits.circom"; include "pedersen.circom"; diff --git a/circuits/escalarmul.circom b/circuits/escalarmul.circom index d34b8cb..34b0ff2 100644 --- a/circuits/escalarmul.circom +++ b/circuits/escalarmul.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /* ┏━━━━━━━━━━━┓ diff --git a/circuits/escalarmulany.circom b/circuits/escalarmulany.circom index 9812baa..50c0c7e 100644 --- a/circuits/escalarmulany.circom +++ b/circuits/escalarmulany.circom @@ -1,3 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ include "montgomery.circom"; include "babyjub.circom"; diff --git a/circuits/escalarmulfix.circom b/circuits/escalarmulfix.circom index c99e9dc..3a1a620 100644 --- a/circuits/escalarmulfix.circom +++ b/circuits/escalarmulfix.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "mux3.circom"; include "montgomery.circom"; include "babyjub.circom"; diff --git a/circuits/escalarmulw4table.circom b/circuits/escalarmulw4table.circom index fff97da..727197a 100644 --- a/circuits/escalarmulw4table.circom +++ b/circuits/escalarmulw4table.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + function pointAdd(x1,y1,x2,y2) { var a = 168700; var d = 168696; diff --git a/circuits/mimc.circom b/circuits/mimc.circom index 1f782d2..1f76f2f 100644 --- a/circuits/mimc.circom +++ b/circuits/mimc.circom @@ -1,6 +1,21 @@ +/* + Copyright 2018 0KIMS association. + This file is part of circom (Zero Knowledge Circuit Compiler). + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ template MiMC7(nrounds) { signal input x_in; diff --git a/circuits/montgomery.circom b/circuits/montgomery.circom index 262f765..9081307 100644 --- a/circuits/montgomery.circom +++ b/circuits/montgomery.circom @@ -1,3 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ /* Source: https://en.wikipedia.org/wiki/Montgomery_curve diff --git a/circuits/multiplexer.circom b/circuits/multiplexer.circom index fc66406..0c8f594 100644 --- a/circuits/multiplexer.circom +++ b/circuits/multiplexer.circom @@ -17,6 +17,25 @@ along with circom. If not, see . */ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + // --> Assignation without constraint // <-- Assignation without constraint // === Constraint diff --git a/circuits/mux3.circom b/circuits/mux3.circom index 1f7199d..277ead2 100644 --- a/circuits/mux3.circom +++ b/circuits/mux3.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + template MultiMux3(n) { signal input c[n][8]; // Constants signal input s[3]; // Selector diff --git a/circuits/mux4.circom b/circuits/mux4.circom index 8814126..c30bb94 100644 --- a/circuits/mux4.circom +++ b/circuits/mux4.circom @@ -1,6 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ template MultiMux4(n) { signal input c[n][16]; // Constants diff --git a/circuits/pedersen.circom b/circuits/pedersen.circom index 09e9e75..d5e5726 100644 --- a/circuits/pedersen.circom +++ b/circuits/pedersen.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "montgomery.circom"; include "mux3.circom"; include "babyjub.circom"; diff --git a/circuits/pedersen_old.circom b/circuits/pedersen_old.circom index 942ea0c..35d23a5 100644 --- a/circuits/pedersen_old.circom +++ b/circuits/pedersen_old.circom @@ -1,3 +1,21 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ include "escalarmul.circom"; diff --git a/circuits/pointbits.circom b/circuits/pointbits.circom index d60c0b9..ec4d2e7 100644 --- a/circuits/pointbits.circom +++ b/circuits/pointbits.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "bitify.circom"; include "aliascheck.circom"; include "compconstant.circom"; diff --git a/circuits/sign.circom b/circuits/sign.circom index 34d8fae..57ebcc8 100644 --- a/circuits/sign.circom +++ b/circuits/sign.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "compconstant.circom"; template Sign() { diff --git a/circuits/smt/smthash.circom b/circuits/smt/smthash.circom index 75f2c46..baba85e 100644 --- a/circuits/smt/smthash.circom +++ b/circuits/smt/smthash.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + include "../mimc.circom"; diff --git a/circuits/smt/smtlevins.circom b/circuits/smt/smtlevins.circom index 2a359f0..67145b6 100644 --- a/circuits/smt/smtlevins.circom +++ b/circuits/smt/smtlevins.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /* This component finds the level where the oldInsert is done. diff --git a/circuits/smt/smtprocessor.circom b/circuits/smt/smtprocessor.circom index f0a1ca6..6c26d37 100644 --- a/circuits/smt/smtprocessor.circom +++ b/circuits/smt/smtprocessor.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /*************************************************************************************************** SMTProcessor: Sparse Merkle Tree processor is a component to verify an insert/update/delete elements diff --git a/circuits/smt/smtprocessorlevel.circom b/circuits/smt/smtprocessorlevel.circom index 7d96bc3..117671e 100644 --- a/circuits/smt/smtprocessorlevel.circom +++ b/circuits/smt/smtprocessorlevel.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /****** SMTProcessorLevel diff --git a/circuits/smt/smtprocessorsm.circom b/circuits/smt/smtprocessorsm.circom index 650e980..fac95b0 100644 --- a/circuits/smt/smtprocessorsm.circom +++ b/circuits/smt/smtprocessorsm.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /*************************************************************************************************** Each level on a SMTProcessor has a state. diff --git a/circuits/smt/smtverifier.circom b/circuits/smt/smtverifier.circom index c8f2956..eee1f3d 100644 --- a/circuits/smt/smtverifier.circom +++ b/circuits/smt/smtverifier.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /* SMTVerifier is a component to verify inclusion/exclusion of an element in the tree diff --git a/circuits/smt/smtverifierlevel.circom b/circuits/smt/smtverifierlevel.circom index 11a4905..a866dae 100644 --- a/circuits/smt/smtverifierlevel.circom +++ b/circuits/smt/smtverifierlevel.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /****** SMTVerifierLevel diff --git a/circuits/smt/smtverifiersm.circom b/circuits/smt/smtverifiersm.circom index f6ac808..f519653 100644 --- a/circuits/smt/smtverifiersm.circom +++ b/circuits/smt/smtverifiersm.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /* Each level in the SMTVerifier has a state. diff --git a/circuits/switcher.circom b/circuits/switcher.circom index 8d296c3..4d8b114 100644 --- a/circuits/switcher.circom +++ b/circuits/switcher.circom @@ -1,3 +1,22 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ + /* Assume sel is binary. diff --git a/test/binsub.js b/test/binsub.js new file mode 100644 index 0000000..21e5ad3 --- /dev/null +++ b/test/binsub.js @@ -0,0 +1,56 @@ +const chai = require("chai"); +const path = require("path"); +const snarkjs = require("snarkjs"); +const compiler = require("circom"); + +const assert = chai.assert; + +const bigInt = snarkjs.bigInt; + +function print(circuit, w, s) { + console.log(s + ": " + w[circuit.getSignalIdx(s)]); +} + +function checkSub(_a,_b, circuit) { + let a=bigInt(_a); + let b=bigInt(_b); + if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shl(16)); + if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shl(16)); + const w = circuit.calculateWitness({a: a, b: b}); + + let res = a.sub(b); + if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shl(16)); + assert( w[circuit.getSignalIdx("main.out")].equals(bigInt(res)) ); +} + +describe("BinSub test", () => { + let circuit; + before( async() => { + const cirDef = await compiler(path.join(__dirname, "circuits", "binsub_test.circom")); + + circuit = new snarkjs.Circuit(cirDef); + + console.log("NConstrains BinSub: " + circuit.nConstraints); + }); + + it("Should check variuos ege cases", async () => { + checkSub(0,0, circuit); + checkSub(1,0, circuit); + checkSub(-1,0, circuit); + checkSub(2,1, circuit); + checkSub(2,2, circuit); + checkSub(2,3, circuit); + checkSub(2,-1, circuit); + checkSub(2,-2, circuit); + checkSub(2,-3, circuit); + checkSub(-2,-3, circuit); + checkSub(-2,-2, circuit); + checkSub(-2,-1, circuit); + checkSub(-2,0, circuit); + checkSub(-2,1, circuit); + checkSub(-2,2, circuit); + checkSub(-2,3, circuit); + }); + + +}); diff --git a/test/sum.js b/test/binsum.js similarity index 100% rename from test/sum.js rename to test/binsum.js diff --git a/test/circuits/binsub_test.circom b/test/circuits/binsub_test.circom new file mode 100644 index 0000000..a633064 --- /dev/null +++ b/test/circuits/binsub_test.circom @@ -0,0 +1,26 @@ +include "../../circuits/bitify.circom" +include "../../circuits/binsub.circom" + +template A() { + signal private input a; + signal input b; + signal output out; + + component n2ba = Num2Bits(16); + component n2bb = Num2Bits(16); + component sub = BinSub(16); + component b2n = Bits2Num(16); + + n2ba.in <== a; + n2bb.in <== b; + + for (var i=0; i<16; i++) { + sub.in[0][i] <== n2ba.out[i]; + sub.in[1][i] <== n2bb.out[i]; + b2n.in[i] <== sub.out[i]; + } + + out <== b2n.out; +} + +component main = A();