Skip to content

Commit

Permalink
version bump for BN.js
Browse files Browse the repository at this point in the history
updates as per fix of indutny/elliptic#191. also a few other adjustments. thanks @ibudisteanu for noticing a naming inconsistency.
  • Loading branch information
keizir committed Dec 26, 2019
1 parent b6d37f8 commit 1febb4f
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 3,151 deletions.
Binary file modified docs/AnonZether.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lerna": "3.14.0"
},
"dependencies": {
"bn.js": "^5.0.0",
"bn.js": "^5.1.1",
"elliptic": "^6.5.1",
"web3": "^1.2.4"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/anonymous.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "./lib",
"dependencies": {
"bn.js": "^5.0.0",
"bn.js": "^5.1.1",
"elliptic": "^6.5.1",
"web3": "^1.2.4"
},
Expand Down
14 changes: 7 additions & 7 deletions packages/anonymous.js/src/prover/burn.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class BurnProof {
constructor() {
this.serialize = () => { // please initialize this before calling this method...
var result = "0x";
result += bn128.representation(this.A).slice(2);
result += bn128.representation(this.S).slice(2);
result += bn128.representation(this.BA).slice(2);
result += bn128.representation(this.BS).slice(2);

result += bn128.representation(this.CLnPrime).slice(2);
result += bn128.representation(this.CRnPrime).slice(2);
Expand Down Expand Up @@ -67,11 +67,11 @@ class BurnProver {
var aL = new FieldVector(witness['bDiff'].toString(2, 32).split("").reverse().map((i) => new BN(i, 2).toRed(bn128.q)));
var aR = aL.plus(new BN(1).toRed(bn128.q).redNeg());
var alpha = bn128.randomScalar();
proof.A = params.commit(alpha, aL, aR);
proof.BA = params.commit(alpha, aL, aR);
var sL = new FieldVector(Array.from({ length: 32 }).map(bn128.randomScalar));
var sR = new FieldVector(Array.from({ length: 32 }).map(bn128.randomScalar));
var rho = bn128.randomScalar(); // already reduced
proof.S = params.commit(rho, sL, sR);
proof.BS = params.commit(rho, sL, sR);

var gammaDiff = bn128.randomScalar();
var zetaDiff = bn128.randomScalar();
Expand All @@ -86,8 +86,8 @@ class BurnProver {
'bytes32[2]',
], [
bn128.bytes(statementHash),
bn128.serialize(proof.A),
bn128.serialize(proof.S),
bn128.serialize(proof.BA),
bn128.serialize(proof.BS),
bn128.serialize(proof.CLnPrime),
bn128.serialize(proof.CRnPrime),
]));
Expand Down Expand Up @@ -157,7 +157,7 @@ class BurnProver {
var gs = params.getGs();
var hsPrime = params.getHs().hadamard(ys.invert());
var hExp = ys.times(z).add(twoTimesZs);
var Z = proof.A.add(proof.S.mul(x)).add(gs.sum().mul(z.redNeg())).add(hsPrime.commit(hExp)); // rename of P
var Z = proof.BA.add(proof.BS.mul(x)).add(gs.sum().mul(z.redNeg())).add(hsPrime.commit(hExp)); // rename of P
Z = Z.add(params.getH().mul(proof.mu.redNeg())); // Statement P of protocol 1. should this be included in the calculation of v...?

var o = utils.hash(ABICoder.encodeParameters([
Expand Down
18 changes: 9 additions & 9 deletions packages/anonymous.js/src/prover/zether.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ZetherProver {
proof.E = params.commit(r_E, new FieldVector([a.getVector()[0].redMul(a.getVector()[m]), a.getVector()[0].redMul(a.getVector()[m])]));
proof.F = params.commit(r_F, new FieldVector([a.getVector()[b.getVector()[0].toNumber() * m], a.getVector()[b.getVector()[m].toNumber() * m].redNeg()]));

var d = utils.hash(ABICoder.encodeParameters([
var v = utils.hash(ABICoder.encodeParameters([
'bytes32',
'bytes32[2]',
'bytes32[2]',
Expand Down Expand Up @@ -184,13 +184,13 @@ class ZetherProver {
proof.gG = Array.from({ length: m }).map((_, k) => params.getG().mul(sigma[k]));
proof.C_XG = Array.from({ length: m }).map((_, k) => statement['D'].mul(omega[k]));
proof.y_XG = Array.from({ length: m }).map((_, k) => params.getG().mul(omega[k]));
var dPow = new BN(1).toRed(bn128.q);
var vPow = new BN(1).toRed(bn128.q);
for (var i = 0; i < N; i++) { // could turn this into a complicated reduce, but...
var temp = params.getG().mul(witness['bTransfer'].redMul(dPow));
var temp = params.getG().mul(witness['bTransfer'].redMul(vPow));
var poly = i % 2 ? Q : P; // clunky, i know, etc. etc.
proof.C_XG = proof.C_XG.map((C_XG_k, k) => C_XG_k.add(temp.mul(poly[k].getVector()[(witness['index'][0] + N - (i - i % 2)) % N].redSub(poly[k].getVector()[(witness['index'][1] + N - (i - i % 2)) % N]))));
if (i != 0)
dPow = dPow.redMul(d);
vPow = vPow.redMul(v);
}

var w = utils.hash(ABICoder.encodeParameters([
Expand All @@ -204,7 +204,7 @@ class ZetherProver {
'bytes32[2][]',
'bytes32[2][]',
], [
bn128.bytes(d),
bn128.bytes(v),
proof.CLnG.map(bn128.serialize),
proof.CRnG.map(bn128.serialize),
proof.C_0G.map(bn128.serialize),
Expand Down Expand Up @@ -249,12 +249,12 @@ class ZetherProver {
var convolver = new Convolver();
var y_p = convolver.convolution(p, statement['y']);
var y_q = convolver.convolution(q, statement['y']);
dPow = new BN(1).toRed(bn128.q);
vPow = new BN(1).toRed(bn128.q);
for (var i = 0; i < N; i++) {
var y_poly = i % 2 ? y_q : y_p;
y_XR = y_XR.add(y_poly.getVector()[Math.floor(i / 2)].mul(dPow));
if (i != 0)
dPow = dPow.redMul(d);
y_XR = y_XR.add(y_poly.getVector()[Math.floor(i / 2)].mul(vPow));
if (i > 0)
vPow = vPow.redMul(v);
}

var gammaTransfer = bn128.randomScalar();
Expand Down
12 changes: 6 additions & 6 deletions packages/anonymous.js/src/utils/bn128.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const BN = require('bn.js')
const EC = require('elliptic')
const crypto = require('crypto')

const FIELD_MODULUS = new BN("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10);
const GROUP_MODULUS = new BN("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);
const FIELD_MODULUS = new BN("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47", 16);
const GROUP_MODULUS = new BN("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", 16);
const B_MAX = 4294967295;

const bn128 = {};
Expand All @@ -12,16 +12,16 @@ const bn128 = {};
bn128.curve = new EC.curve.short({
a: '0',
b: '3',
p: FIELD_MODULUS.toString(16),
n: GROUP_MODULUS.toString(16),
p: FIELD_MODULUS,
n: GROUP_MODULUS,
gRed: false,
g: ['77da99d806abd13c9f15ece5398525119d11e11e9836b2ee7d23f6159ad87d4', '1485efa927f2ad41bff567eec88f32fb0a0f706588b4e41a8d587d008b7f875'],
});

bn128.zero = bn128.curve.g.mul(0);

bn128.p = BN.red(new BN(bn128.curve.p.toString(16), 16)); // temporary workaround due to
bn128.q = BN.red(new BN(bn128.curve.n.toString(16), 16)); // https://github.com/indutny/elliptic/issues/191
bn128.p = BN.red(bn128.curve.p); // temporary workaround due to
bn128.q = BN.red(bn128.curve.n); // https://github.com/indutny/elliptic/issues/191

// Get a random BN in the bn128 curve group's reduction context
bn128.randomScalar = () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/contract-artifacts/artifacts/BurnVerifier.json

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions packages/contract-artifacts/artifacts/CashToken.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
"id": 2013,
"nodeType": "ImportDirective",
"scope": 2017,
"sourceUnit": 8322,
"sourceUnit": 8243,
"src": "0:71:1",
"symbolAliases": [],
"unitAlias": ""
Expand All @@ -347,10 +347,10 @@
"id": 2014,
"name": "ERC20Mintable",
"nodeType": "UserDefinedTypeName",
"referencedDeclaration": 8321,
"referencedDeclaration": 8242,
"src": "95:13:1",
"typeDescriptions": {
"typeIdentifier": "t_contract$_ERC20Mintable_$8321",
"typeIdentifier": "t_contract$_ERC20Mintable_$8242",
"typeString": "contract ERC20Mintable"
}
},
Expand All @@ -360,23 +360,23 @@
}
],
"contractDependencies": [
7513,
7701,
8293,
8321,
8390
7434,
7622,
8214,
8242,
8311
],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"id": 2016,
"linearizedBaseContracts": [
2016,
8321,
7701,
8293,
8390,
7513
8242,
7622,
8214,
8311,
7434
],
"name": "CashToken",
"nodeType": "ContractDefinition",
Expand All @@ -403,7 +403,7 @@
"id": 2013,
"nodeType": "ImportDirective",
"scope": 2017,
"sourceUnit": 8322,
"sourceUnit": 8243,
"src": "0:71:1",
"symbolAliases": [],
"unitAlias": ""
Expand All @@ -417,10 +417,10 @@
"id": 2014,
"name": "ERC20Mintable",
"nodeType": "UserDefinedTypeName",
"referencedDeclaration": 8321,
"referencedDeclaration": 8242,
"src": "95:13:1",
"typeDescriptions": {
"typeIdentifier": "t_contract$_ERC20Mintable_$8321",
"typeIdentifier": "t_contract$_ERC20Mintable_$8242",
"typeString": "contract ERC20Mintable"
}
},
Expand All @@ -430,23 +430,23 @@
}
],
"contractDependencies": [
7513,
7701,
8293,
8321,
8390
7434,
7622,
8214,
8242,
8311
],
"contractKind": "contract",
"documentation": null,
"fullyImplemented": true,
"id": 2016,
"linearizedBaseContracts": [
2016,
8321,
7701,
8293,
8390,
7513
8242,
7622,
8214,
8311,
7434
],
"name": "CashToken",
"nodeType": "ContractDefinition",
Expand All @@ -463,7 +463,7 @@
},
"networks": {},
"schemaVersion": "3.0.19",
"updatedAt": "2019-11-26T01:17:49.346Z",
"updatedAt": "2019-12-26T18:28:14.455Z",
"devdoc": {
"methods": {
"allowance(address,address)": {
Expand Down
10 changes: 5 additions & 5 deletions packages/contract-artifacts/artifacts/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 8405,
"referencedDeclaration": 8326,
"src": "158:3:2",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -282,7 +282,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 8405,
"referencedDeclaration": 8326,
"src": "209:3:2",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -847,7 +847,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 8405,
"referencedDeclaration": 8326,
"src": "158:3:2",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -931,7 +931,7 @@
"name": "msg",
"nodeType": "Identifier",
"overloadedDeclarations": [],
"referencedDeclaration": 8405,
"referencedDeclaration": 8326,
"src": "209:3:2",
"typeDescriptions": {
"typeIdentifier": "t_magic_message",
Expand Down Expand Up @@ -1375,7 +1375,7 @@
},
"networks": {},
"schemaVersion": "3.0.19",
"updatedAt": "2019-11-26T01:17:49.347Z",
"updatedAt": "2019-12-26T18:28:14.455Z",
"devdoc": {
"methods": {}
},
Expand Down
Loading

0 comments on commit 1febb4f

Please sign in to comment.