escalarmul.js
1 const chai = require("chai"); 2 const path = require("path"); 3 const wasm_tester = require("circom_tester").wasm; 4 const buildBabyjub = require("circomlibjs").buildBabyjub; 5 6 const Scalar = require("ffjavascript").Scalar; 7 8 const assert = chai.assert; 9 10 function print(circuit, w, s) { 11 console.log(s + ": " + w[circuit.getSignalIdx(s)]); 12 } 13 14 describe("Exponentioation test", function () { 15 let babyJub; 16 let Fr; 17 this.timeout(100000); 18 19 before( async () => { 20 babyJub = await buildBabyjub(); 21 Fr = babyJub.F; 22 }); 23 24 it("Should generate the Exponentiation table in k=0", async () => { 25 26 const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom")); 27 28 const w = await circuit.calculateWitness({in: 1}); 29 30 await circuit.checkConstraints(w); 31 32 let g = [ 33 Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), 34 Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") 35 ]; 36 37 let dbl= [Fr.e("0"), Fr.e("1")]; 38 39 const expectedOut = []; 40 41 for (let i=0; i<16; i++) { 42 43 expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]); 44 dbl = babyJub.addPoint(dbl,g); 45 } 46 47 await circuit.assertOut(w, {out: expectedOut}); 48 49 }); 50 51 it("Should generate the Exponentiation table in k=3", async () => { 52 53 const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom")); 54 55 const w = await circuit.calculateWitness({in: 1}); 56 57 await circuit.checkConstraints(w); 58 59 let g = [ 60 Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), 61 Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") 62 ]; 63 64 for (let i=0; i<12;i++) { 65 g = babyJub.addPoint(g,g); 66 } 67 68 let dbl= [Fr.e("0"), Fr.e("1")]; 69 70 const expectedOut = []; 71 72 for (let i=0; i<16; i++) { 73 expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]); 74 75 dbl = babyJub.addPoint(dbl,g); 76 } 77 78 await circuit.assertOut(w, {out: expectedOut}); 79 80 }); 81 82 it("Should exponentiate g^31", async () => { 83 84 const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test.circom")); 85 86 const w = await circuit.calculateWitness({"in": 31}); 87 88 await circuit.checkConstraints(w); 89 90 let g = [ 91 Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), 92 Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") 93 ]; 94 95 let c = [Fr.e(0), Fr.e(1)]; 96 97 for (let i=0; i<31;i++) { 98 c = babyJub.addPoint(c,g); 99 } 100 101 await circuit.assertOut(w, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] }); 102 103 const w2 = await circuit.calculateWitness({"in": Scalar.add(Scalar.shl(Scalar.e(1), 252),Scalar.e(1))}); 104 105 c = [g[0], g[1]]; 106 for (let i=0; i<252;i++) { 107 c = babyJub.addPoint(c,c); 108 } 109 c = babyJub.addPoint(c,g); 110 111 await circuit.assertOut(w2, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] }); 112 113 }).timeout(10000000); 114 115 it("Number of constrains for 256 bits", async () => { 116 117 const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom")); 118 119 }).timeout(10000000); 120 121 });