circomlib/test/babyjub_js.js
test/babyjub_js.jsRaw
1const chai = require("chai");
2const babyjub = require("../src/babyjub.js");
3const Scalar = require("ffjavascript").Scalar;
4
5const assert = chai.assert;
6
7// const bigInt = require("big-integer");
8
9
10describe("Baby Jub js test", function () {
11
12 this.timeout(100000);
13
14 it("Should add point (0,1) and (0,1)", () => {
15
16 const p1 = [
17 babyjub.F.e(0),
18 babyjub.F.e(1)];
19 const p2 = [
20 babyjub.F.e(0),
21 babyjub.F.e(1)
22 ];
23
24 const out = babyjub.addPoint(p1, p2);
25 assert(babyjub.F.eq(out[0], babyjub.F.zero));
26 assert(babyjub.F.eq(out[1], babyjub.F.one));
27 });
28
29 it("Should base be 8*generator", () => {
30 let res;
31 res = babyjub.addPoint(babyjub.Generator, babyjub.Generator);
32 res = babyjub.addPoint(res, res);
33 res = babyjub.addPoint(res, res);
34
35 assert(babyjub.F.eq(res[0], babyjub.Base8[0]));
36 assert(babyjub.F.eq(res[1], babyjub.Base8[1]));
37 });
38
39 it("Should add 2 same numbers", () => {
40
41 const p1 = [
42 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
43 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
44 ];
45 const p2 = [
46 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
47 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
48 ];
49
50 const out = babyjub.addPoint(p1, p2);
51 assert(babyjub.F.eq(out[0], babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
52 assert(babyjub.F.eq(out[1], babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
53 });
54
55 it("Should add 2 different numbers", () => {
56
57 const p1 = [
58 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
59 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
60 ];
61 const p2 = [
62 babyjub.F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
63 babyjub.F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
64 ];
65
66 const out = babyjub.addPoint(p1, p2);
67 assert(babyjub.F.eq(out[0], babyjub.F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
68 assert(babyjub.F.eq(out[1], babyjub.F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
69
70 });
71
72 it("should mulPointEscalar 0", () => {
73 const p = [
74 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
75 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
76 ];
77
78 const r = babyjub.mulPointEscalar(p, 3);
79 let r2 = babyjub.addPoint(p, p);
80 r2 = babyjub.addPoint(r2, p);
81 assert.equal(r2[0].toString(), r[0].toString());
82 assert.equal(r2[1].toString(), r[1].toString());
83 assert.equal(r[0].toString(), "19372461775513343691590086534037741906533799473648040012278229434133483800898");
84 assert.equal(r[1].toString(), "9458658722007214007257525444427903161243386465067105737478306991484593958249");
85 });
86
87 it("should mulPointEscalar 1", () => {
88 const p = [
89 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
90 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
91 ];
92
93 const r = babyjub.mulPointEscalar(p, Scalar.fromString("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
94 assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
95 assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
96 });
97
98 it("should mulPointEscalar 2", () => {
99 const p = [
100 babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
101 babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
102 ];
103
104 const r = babyjub.mulPointEscalar(p, Scalar.fromString("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
105 assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
106 assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
107 });
108
109 it("should inCurve 1", () => {
110 const p = [
111 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
112 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
113 ];
114 assert(babyjub.inCurve(p));
115 });
116
117 it("should inCurve 2", () => {
118 const p = [
119 babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
120 babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
121 ];
122 assert(babyjub.inCurve(p));
123 });
124
125 it("should inSubgroup 1", () => {
126 const p = [
127 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
128 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
129 ];
130 assert(babyjub.inSubgroup(p));
131 });
132
133 it("should inSubgroup 2", () => {
134 const p = [
135 babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
136 babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
137 ];
138 assert(babyjub.inSubgroup(p));
139 });
140
141 it("should packPoint - unpackPoint 1", () => {
142 const p = [
143 babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
144 babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
145 ];
146 const buf = babyjub.packPoint(p);
147 assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
148 const p2 = babyjub.unpackPoint(buf);
149 assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
150 assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
151 });
152
153 it("should packPoint - unpackPoint 2", () => {
154 const p = [
155 babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
156 babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
157 ];
158 const buf = babyjub.packPoint(p);
159 assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");
160 const p2 = babyjub.unpackPoint(buf);
161 assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
162 assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
163 });
164});