Browse Source

Overcome undefined "Web3.utils" on web3@1.0.0-beta41

feature/babypbk
Jordi 5 years ago
parent
commit
138945bfdc
3 changed files with 14 additions and 14 deletions
  1. +3
    -3
      src/evmasm.js
  2. +8
    -8
      src/mimc7.js
  3. +3
    -3
      src/mimc_gencontract.js

+ 3
- 3
src/evmasm.js

@ -3,7 +3,7 @@
// //
const Web3 = require("web3");
const Web3Utils = require("web3-utils");
const assert = require("assert"); const assert = require("assert");
class Contract { class Contract {
@ -39,7 +39,7 @@ class Contract {
genLoadedLength = C.code.length; genLoadedLength = C.code.length;
} }
return Web3.utils.bytesToHex(C.code.concat(this.code));
return Web3Utils.bytesToHex(C.code.concat(this.code));
} }
stop() { this.code.push(0x00); } stop() { this.code.push(0x00); }
@ -149,7 +149,7 @@ class Contract {
} }
push(data) { push(data) {
const d = Web3.utils.hexToBytes(Web3.utils.toHex(data));
const d = Web3Utils.hexToBytes(Web3Utils.toHex(data));
assert(d.length>0); assert(d.length>0);
assert(d.length<=32); assert(d.length<=32);
this.code = this.code.concat([0x5F + d.length], d); this.code = this.code.concat([0x5F + d.length], d);

+ 8
- 8
src/mimc7.js

@ -1,6 +1,6 @@
const bn128 = require("snarkjs").bn128; const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt; const bigInt = require("snarkjs").bigInt;
const Web3 = require("web3");
const Web3Utils = require("web3-utils");
const F = bn128.Fr; const F = bn128.Fr;
const SEED = "mimc"; const SEED = "mimc";
@ -8,8 +8,8 @@ const NROUNDS = 91;
exports.getIV = (seed) => { exports.getIV = (seed) => {
if (typeof seed === "undefined") seed = SEED; if (typeof seed === "undefined") seed = SEED;
const c = Web3.utils.keccak256(seed+"_iv");
const cn = bigInt(Web3.utils.toBN(c).toString());
const c = Web3Utils.keccak256(seed+"_iv");
const cn = bigInt(Web3Utils.toBN(c).toString());
const iv = cn.mod(F.q); const iv = cn.mod(F.q);
return iv; return iv;
}; };
@ -18,13 +18,13 @@ exports.getConstants = (seed, nRounds) => {
if (typeof seed === "undefined") seed = SEED; if (typeof seed === "undefined") seed = SEED;
if (typeof nRounds === "undefined") nRounds = NROUNDS; if (typeof nRounds === "undefined") nRounds = NROUNDS;
const cts = new Array(nRounds); const cts = new Array(nRounds);
let c = Web3.utils.keccak256(SEED);
let c = Web3Utils.keccak256(SEED);
for (let i=1; i<nRounds; i++) { for (let i=1; i<nRounds; i++) {
c = Web3.utils.keccak256(c);
c = Web3Utils.keccak256(c);
const n1 = Web3.utils.toBN(c).mod(Web3.utils.toBN(F.q.toString()));
const c2 = Web3.utils.padLeft(Web3.utils.toHex(n1), 64);
cts[i] = bigInt(Web3.utils.toBN(c2).toString());
const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.q.toString()));
const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64);
cts[i] = bigInt(Web3Utils.toBN(c2).toString());
} }
cts[0] = bigInt(0); cts[0] = bigInt(0);
return cts; return cts;

+ 3
- 3
src/mimc_gencontract.js

@ -2,13 +2,13 @@
// License: LGPL-3.0+ // License: LGPL-3.0+
// //
const Web3 = require("web3");
const Web3Utils = require("web3-utils");
const Contract = require("./evmasm"); const Contract = require("./evmasm");
function createCode(seed, n) { function createCode(seed, n) {
let ci = Web3.utils.keccak256(seed);
let ci = Web3Utils.keccak256(seed);
const C = new Contract(); const C = new Contract();
@ -51,7 +51,7 @@ function createCode(seed, n) {
C.mulmod(); // r=t^7 k q C.mulmod(); // r=t^7 k q
for (let i=0; i<n-1; i++) { for (let i=0; i<n-1; i++) {
ci = Web3.utils.keccak256(ci);
ci = Web3Utils.keccak256(ci);
C.dup(2); // q r k q C.dup(2); // q r k q
C.dup(0); // q q r k q C.dup(0); // q q r k q
C.dup(0); // q q q r k q C.dup(0); // q q q r k q

Loading…
Cancel
Save