arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/circuits/babyjub.circom
circom/node_modules/circomlib/circuits/babyjub.circomRaw
  1/*
  2    Copyright 2018 0KIMS association.
  3
  4    This file is part of circom (Zero Knowledge Circuit Compiler).
  5
  6    circom is a free software: you can redistribute it and/or modify it
  7    under the terms of the GNU General Public License as published by
  8    the Free Software Foundation, either version 3 of the License, or
  9    (at your option) any later version.
 10
 11    circom is distributed in the hope that it will be useful, but WITHOUT
 12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 13    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 14    License for more details.
 15
 16    You should have received a copy of the GNU General Public License
 17    along with circom. If not, see <https://www.gnu.org/licenses/>.
 18*/
 19pragma circom 2.0.0;
 20
 21include "bitify.circom";
 22include "escalarmulfix.circom";
 23
 24template BabyAdd() {
 25    signal input x1;
 26    signal input y1;
 27    signal input x2;
 28    signal input y2;
 29    signal output xout;
 30    signal output yout;
 31
 32    signal beta;
 33    signal gamma;
 34    signal delta;
 35    signal tau;
 36
 37    var a = 168700;
 38    var d = 168696;
 39
 40    beta <== x1*y2;
 41    gamma <== y1*x2;
 42    delta <== (-a*x1+y1)*(x2 + y2);
 43    tau <== beta * gamma;
 44
 45    xout <-- (beta + gamma) / (1+ d*tau);
 46    (1+ d*tau) * xout === (beta + gamma);
 47
 48    yout <-- (delta + a*beta - gamma) / (1-d*tau);
 49    (1-d*tau)*yout === (delta + a*beta - gamma);
 50}
 51
 52template BabyDbl() {
 53    signal input x;
 54    signal input y;
 55    signal output xout;
 56    signal output yout;
 57
 58    component adder = BabyAdd();
 59    adder.x1 <== x;
 60    adder.y1 <== y;
 61    adder.x2 <== x;
 62    adder.y2 <== y;
 63
 64    adder.xout ==> xout;
 65    adder.yout ==> yout;
 66}
 67
 68
 69template BabyCheck() {
 70    signal input x;
 71    signal input y;
 72
 73    signal x2;
 74    signal y2;
 75
 76    var a = 168700;
 77    var d = 168696;
 78
 79    x2 <== x*x;
 80    y2 <== y*y;
 81
 82    a*x2 + y2 === 1 + d*x2*y2;
 83}
 84
 85// Extracts the public key from private key
 86template BabyPbk() {
 87    signal input  in;
 88    signal output Ax;
 89    signal output Ay;
 90
 91    var BASE8[2] = [
 92        5299619240641551281634865583518297030282874472190772894086521144482721001553,
 93        16950150798460657717958625567821834550301663161624707787222815936182638968203
 94    ];
 95
 96    component pvkBits = Num2Bits(253);
 97    pvkBits.in <== in;
 98
 99    component mulFix = EscalarMulFix(253, BASE8);
100
101    var i;
102    for (i=0; i<253; i++) {
103        mulFix.e[i] <== pvkBits.out[i];
104    }
105    Ax  <== mulFix.out[0];
106    Ay  <== mulFix.out[1];
107}