Skip to content

Commit

Permalink
Integration with wasmsnark, sqrtF2, formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaylina committed May 15, 2020
1 parent 18cab5b commit efa1935
Show file tree
Hide file tree
Showing 19 changed files with 1,789 additions and 28 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ exports.EC = require("./src/ec");
exports.bn128 = require("./src/bn128.js");

exports.utils = require("./src/utils");
exports.ChaCha = require("./src/chacha");


39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
},
"homepage": "https://github.com/iden3/ffjs#readme",
"dependencies": {
"big-integer": "^1.6.48"
"big-integer": "^1.6.48",
"wasmsnark": "0.0.10"
},
"devDependencies": {
"blake2b": "^2.1.3",
"chai": "^4.2.0",
"eslint": "^6.8.0"
}
Expand Down
67 changes: 63 additions & 4 deletions src/bn128.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ const F1Field = require("./f1field");
const F2Field = require("./f2field");
const F3Field = require("./f3field");
const EC = require("./ec.js");
const buildEngine = require("./engine");
const bn128_wasm = require("wasmsnark").bn128_wasm;


let engine = null;


class BN128 {

constructor() {

this.name = "bn128";
this.q = Scalar.fromString("21888242871839275222246405745257275088696311157297823662689037894645226208583");
this.r = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");

Expand All @@ -53,6 +60,10 @@ class BN128 {
this.G1 = new EC(this.F1, this.g1);
this.G2 = new EC(this.F2, this.g2);

this.G1.b = this.F1.e(3);
this.G2.b = this.F2.div([this.F1.e(3),this.F1.e(0)], [this.F1.e(9), this.F1.e(1)]);
this.G2.cofactor = Scalar.e("0x30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d");

this.nonResidueF6 = [ this.F1.e("9"), this.F1.e("1") ];

this.F6 = new F3Field(this.F2, this.nonResidueF6);
Expand All @@ -68,6 +79,54 @@ class BN128 {

this._preparePairing();

this.G1.batchApplyKey = this.batchApplyKeyG1.bind(this);
this.G2.batchApplyKey = this.batchApplyKeyG2.bind(this);
this.G1.batchLEMtoU = this.batchLEMtoUG1.bind(this);
this.G2.batchLEMtoU = this.batchLEMtoUG2.bind(this);
this.G1.batchLEMtoC = this.batchLEMtoCG1.bind(this);
this.G2.batchLEMtoC = this.batchLEMtoCG2.bind(this);
}

async loadEngine() {
if (!engine) {
engine = await buildEngine(this, bn128_wasm, true);
}
}

async batchApplyKeyG1(buff, first, inc) {
await this.loadEngine();
const res = await engine.batchApplyKey("G1", buff, first, inc);
return res;
}

async batchApplyKeyG2(buff, first, inc) {
await this.loadEngine();
const res = await engine.batchApplyKey("G2", buff, first, inc);
return res;
}

async batchLEMtoUG1(buff) {
await this.loadEngine();
const res = await engine.batchConvert("G1", "LEM", "U", buff );
return res;
}

async batchLEMtoUG2(buff) {
await this.loadEngine();
const res = await engine.batchConvert("G2", "LEM", "U",buff);
return res;
}

async batchLEMtoCG1(buff) {
await this.loadEngine();
const res = await engine.batchConvert("G1", "LEM", "C", buff);
return res;
}

async batchLEMtoCG2(buff) {
await this.loadEngine();
const res = await engine.batchConvert("G2", "LEM", "C", buff);
return res;
}

_preparePairing() {
Expand Down Expand Up @@ -253,7 +312,7 @@ class BN128 {
finalExponentiation(elt) {
// TODO: There is an optimization in FF

const res = this.F12.exp(elt,this.final_exponent);
const res = this.F12.pow(elt,this.final_exponent);

return res;
}
Expand Down Expand Up @@ -307,7 +366,7 @@ class BN128 {

const D = this.F2.sub( X1, this.F2.mul(x2,Z1) ); // D = X1 - X2*Z1

// console.log("Y: "+ A[0].affine(this.q).toString(16));
// console.log("Y: "+ A[0].affine(this.q).toString(16));

const E = this.F2.sub( Y1, this.F2.mul(y2,Z1) ); // E = Y1 - Y2*Z1
const F = this.F2.square(D); // F = D^2
Expand Down Expand Up @@ -342,14 +401,14 @@ class BN128 {
_mul_by_024(a, ell_0, ell_VW, ell_VV) {

// Old implementation
/*
/*
const b = [
[ell_0, this.F2.zero, ell_VV],
[this.F2.zero, ell_VW, this.F2.zero]
];
return this.F12.mul(a,b);
*/
*/

// This is a new implementation,
// But it does not look worthy
Expand Down
96 changes: 96 additions & 0 deletions src/chacha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@


const Scalar = require("./scalar");


function quarterRound(st, a, b, c, d) {

st[a] = (st[a] + st[b]) >>> 0;
st[d] = (st[d] ^ st[a]) >>> 0;
st[d] = ((st[d] << 16) | ((st[d]>>>16) & 0xFFFF)) >>> 0;

st[c] = (st[c] + st[d]) >>> 0;
st[b] = (st[b] ^ st[c]) >>> 0;
st[b] = ((st[b] << 12) | ((st[b]>>>20) & 0xFFF)) >>> 0;

st[a] = (st[a] + st[b]) >>> 0;
st[d] = (st[d] ^ st[a]) >>> 0;
st[d] = ((st[d] << 8) | ((st[d]>>>24) & 0xFF)) >>> 0;

st[c] = (st[c] + st[d]) >>> 0;
st[b] = (st[b] ^ st[c]) >>> 0;
st[b] = ((st[b] << 7) | ((st[b]>>>25) & 0x7F)) >>> 0;
}

function doubleRound(st) {
quarterRound(st, 0, 4, 8,12);
quarterRound(st, 1, 5, 9,13);
quarterRound(st, 2, 6,10,14);
quarterRound(st, 3, 7,11,15);

quarterRound(st, 0, 5,10,15);
quarterRound(st, 1, 6,11,12);
quarterRound(st, 2, 7, 8,13);
quarterRound(st, 3, 4, 9,14);
}

module.exports = class ChaCha {

constructor(seed) {
seed = seed || [0,0,0,0,0,0,0,0];
this.state = [
0x61707865,
0x3320646E,
0x79622D32,
0x6B206574,
seed[0],
seed[1],
seed[2],
seed[3],
seed[4],
seed[5],
seed[6],
seed[7],
0,
0,
0,
0
];
this.idx = 16;
this.buff = new Array(16);
}

nextU32() {
if (this.idx == 16) this.update();
return this.buff[this.idx++];
}

nextU64() {
return Scalar.add(Scalar.mul(this.nextU32(), 0x100000000), this.nextU32());
}

nextBool() {
return (this.nextU32() & 1) == 1;
}

update() {
// Copy the state
for (let i=0; i<16; i++) this.buff[i] = this.state[i];

// Apply the rounds
for (let i=0; i<10; i++) doubleRound(this.buff);

// Add to the initial
for (let i=0; i<16; i++) this.buff[i] = (this.buff[i] + this.state[i]) >>> 0;

this.idx = 0;

this.state[12] = (this.state[12] + 1) >>> 0;
if (this.state[12] != 0) return;
this.state[13] = (this.state[13] + 1) >>> 0;
if (this.state[13] != 0) return;
this.state[14] = (this.state[14] + 1) >>> 0;
if (this.state[14] != 0) return;
this.state[15] = (this.state[15] + 1) >>> 0;
}
};
Loading

0 comments on commit efa1935

Please sign in to comment.