snarkjs/src/gcurve.js
src/gcurve.jsRaw
1const fUtils = require("./futils.js");
2
3class GCurve {
4
5 constructor(F, g) {
6 this.F = F;
7 this.g = [F.copy(g[0]), F.copy(g[1])];
8 if (this.g.length == 2) this.g[2] = this.F.one;
9 this.zero = [this.F.zero, this.F.one, this.F.zero];
10 }
11
12 isZero(p) {
13 return this.F.isZero(p[2]);
14 }
15
16 add(p1, p2) {
17
18 if (this.isZero(p1)) return p2;
19 if (this.isZero(p2)) return p1;
20
21 const res = new Array(3);
22
23 const Z1Z1 = this.F.square( p1[2] );
24 const Z2Z2 = this.F.square( p2[2] );
25
26 const U1 = this.F.mul( p1[0] , Z2Z2 ); // U1 = X1 * Z2Z2
27 const U2 = this.F.mul( p2[0] , Z1Z1 ); // U2 = X2 * Z1Z1
28
29 const Z1_cubed = this.F.mul( p1[2] , Z1Z1);
30 const Z2_cubed = this.F.mul( p2[2] , Z2Z2);
31
32 const S1 = this.F.mul( p1[1] , Z2_cubed); // S1 = Y1 * Z2 * Z2Z2
33 const S2 = this.F.mul( p2[1] , Z1_cubed); // S2 = Y2 * Z1 * Z1Z1
34
35 if (this.F.equals(U1,U2) && this.F.equals(S1,S2)) {
36 return this.double(p1);
37 }
38
39 const H = this.F.sub( U2 , U1 ); // H = U2-U1
40
41 const S2_minus_S1 = this.F.sub( S2 , S1 );
42
43 const I = this.F.square( this.F.add(H,H) ); // I = (2 * H)^2
44 const J = this.F.mul( H , I ); // J = H * I
45
46 const r = this.F.add( S2_minus_S1 , S2_minus_S1 ); // r = 2 * (S2-S1)
47 const V = this.F.mul( U1 , I ); // V = U1 * I
48
49 res[0] =
50 this.F.sub(
51 this.F.sub( this.F.square(r) , J ),
52 this.F.add( V , V )); // X3 = r^2 - J - 2 * V
53
54 const S1_J = this.F.mul( S1 , J );
55
56 res[1] =
57 this.F.sub(
58 this.F.mul( r , this.F.sub(V,res[0])),
59 this.F.add( S1_J,S1_J )); // Y3 = r * (V-X3)-2 S1 J
60
61 res[2] =
62 this.F.mul(
63 H,
64 this.F.sub(
65 this.F.square( this.F.add(p1[2],p2[2]) ),
66 this.F.add( Z1Z1 , Z2Z2 ))); // Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
67
68 return res;
69 }
70
71 neg(p) {
72 return [p[0], this.F.neg(p[1]), p[2]];
73 }
74
75 sub(a, b) {
76 return this.add(a, this.neg(b));
77 }
78
79 double(p) {
80 const res = new Array(3);
81
82 if (this.isZero(p)) return p;
83
84 const A = this.F.square( p[0] ); // A = X1^2
85 const B = this.F.square( p[1] ); // B = Y1^2
86 const C = this.F.square( B ); // C = B^2
87
88 let D =
89 this.F.sub(
90 this.F.square( this.F.add(p[0] , B )),
91 this.F.add( A , C));
92 D = this.F.add(D,D); // D = 2 * ((X1 + B)^2 - A - C)
93
94 const E = this.F.add( this.F.add(A,A), A); // E = 3 * A
95 const F = this.F.square( E ); // F = E^2
96
97 res[0] = this.F.sub( F , this.F.add(D,D) ); // X3 = F - 2 D
98
99 let eightC = this.F.add( C , C );
100 eightC = this.F.add( eightC , eightC );
101 eightC = this.F.add( eightC , eightC );
102
103 res[1] =
104 this.F.sub(
105 this.F.mul(
106 E,
107 this.F.sub( D, res[0] )),
108 eightC); // Y3 = E * (D - X3) - 8 * C
109
110 const Y1Z1 = this.F.mul( p[1] , p[2] );
111 res[2] = this.F.add( Y1Z1 , Y1Z1 ); // Z3 = 2 * Y1 * Z1
112
113 return res;
114 }
115
116 mulScalar(base, e) {
117 return fUtils.mulScalar(this, base, e);
118 }
119
120 affine(p) {
121 if (this.isZero(p)) {
122 return this.zero;
123 } else {
124 const Z_inv = this.F.inverse(p[2]);
125 const Z2_inv = this.F.square(Z_inv);
126 const Z3_inv = this.F.mul(Z2_inv, Z_inv);
127
128 const res = new Array(3);
129 res[0] = this.F.affine( this.F.mul(p[0],Z2_inv));
130 res[1] = this.F.affine( this.F.mul(p[1],Z3_inv));
131 res[2] = this.F.one;
132
133 return res;
134 }
135 }
136
137 equals(p1, p2) {
138 if (this.isZero(p1)) return this.isZero(p2);
139 if (this.isZero(p2)) return this.isZero(p1);
140
141 const Z1Z1 = this.F.square( p1[2] );
142 const Z2Z2 = this.F.square( p2[2] );
143
144 const U1 = this.F.mul( p1[0] , Z2Z2 );
145 const U2 = this.F.mul( p2[0] , Z1Z1 );
146
147 const Z1_cubed = this.F.mul( p1[2] , Z1Z1);
148 const Z2_cubed = this.F.mul( p2[2] , Z2Z2);
149
150 const S1 = this.F.mul( p1[1] , Z2_cubed);
151 const S2 = this.F.mul( p2[1] , Z1_cubed);
152
153 return (this.F.equals(U1,U2) && this.F.equals(S1,S2));
154 }
155
156 toString(p) {
157 const cp = this.affine(p);
158 return `[ ${this.F.toString(cp[0])} , ${this.F.toString(cp[1])} ]`;
159 }
160
161}
162
163module.exports = GCurve;
164