code style

This commit is contained in:
poma 2019-07-16 20:27:20 +03:00
parent a7fedcd7ea
commit 7eaa5fcb4f
8 changed files with 137 additions and 138 deletions

View File

@ -1,23 +1,23 @@
const jsStorage = require("./Storage");
const mimcHasher = require("./MiMC");
const jsStorage = require('./Storage')
const mimcHasher = require('./MiMC')
class MerkleTree {
constructor(n_levels, zero_value, defaultElements, prefix, storage, hasher) {
this.prefix = prefix;
this.storage = storage || new jsStorage();
this.hasher = hasher || new mimcHasher();
this.n_levels = n_levels;
this.zero_values = [];
this.totalElements = 0;
this.prefix = prefix
this.storage = storage || new jsStorage()
this.hasher = hasher || new mimcHasher()
this.n_levels = n_levels
this.zero_values = []
this.totalElements = 0
let current_zero_value = zero_value || 0;
this.zero_values.push(current_zero_value);
let current_zero_value = zero_value || 0
this.zero_values.push(current_zero_value)
for (let i = 0; i < n_levels; i++) {
current_zero_value = this.hasher.hash(i, current_zero_value, current_zero_value);
current_zero_value = this.hasher.hash(i, current_zero_value, current_zero_value)
this.zero_values.push(
current_zero_value.toString(),
);
)
}
if (defaultElements) {
let level = 0
@ -35,7 +35,7 @@ class MerkleTree {
const left = this.storage.get(leftKey)
const right = this.storage.get_or_element(rightKey, this.zero_values[level - 1])
const subRoot = this.hasher.hash(null, left, right);
const subRoot = this.hasher.hash(null, left, right)
this.storage.put(MerkleTree.index_to_key(prefix, level, i), subRoot)
}
numberOfElementInRow = Math.max(Math.ceil(numberOfElementInRow / 2), 1)
@ -44,56 +44,56 @@ class MerkleTree {
}
static index_to_key(prefix, level, index) {
const key = `${prefix}_tree_${level}_${index}`;
return key;
const key = `${prefix}_tree_${level}_${index}`
return key
}
async root() {
let root = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
this.zero_values[this.n_levels],
);
)
return root;
return root
}
async path(index) {
class PathTraverser {
constructor(prefix, storage, zero_values) {
this.prefix = prefix;
this.storage = storage;
this.zero_values = zero_values;
this.path_elements = [];
this.path_index = [];
this.prefix = prefix
this.storage = storage
this.zero_values = zero_values
this.path_elements = []
this.path_index = []
}
async handle_index(level, element_index, sibling_index) {
const sibling = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, level, sibling_index),
this.zero_values[level],
);
this.path_elements.push(sibling);
this.path_index.push(element_index % 2);
)
this.path_elements.push(sibling)
this.path_index.push(element_index % 2)
}
}
let traverser = new PathTraverser(this.prefix, this.storage, this.zero_values);
let traverser = new PathTraverser(this.prefix, this.storage, this.zero_values)
const root = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
this.zero_values[this.n_levels],
);
)
const element = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, 0, index),
this.zero_values[0],
);
)
await this.traverse(index, traverser);
await this.traverse(index, traverser)
return {
root,
path_elements: traverser.path_elements,
path_index: traverser.path_index,
element
};
}
}
async update(index, element, insert = false) {
@ -105,12 +105,12 @@ class MerkleTree {
try {
class UpdateTraverser {
constructor(prefix, storage, hasher, element, zero_values) {
this.prefix = prefix;
this.current_element = element;
this.zero_values = zero_values;
this.storage = storage;
this.hasher = hasher;
this.key_values_to_put = [];
this.prefix = prefix
this.current_element = element
this.zero_values = zero_values
this.storage = storage
this.hasher = hasher
this.key_values_to_put = []
}
async handle_index(level, element_index, sibling_index) {
@ -118,26 +118,26 @@ class MerkleTree {
this.original_element = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, level, element_index),
this.zero_values[level],
);
)
}
const sibling = await this.storage.get_or_element(
MerkleTree.index_to_key(this.prefix, level, sibling_index),
this.zero_values[level],
);
let left, right;
)
let left, right
if (element_index % 2 == 0) {
left = this.current_element;
right = sibling;
left = this.current_element
right = sibling
} else {
left = sibling;
right = this.current_element;
left = sibling
right = this.current_element
}
this.key_values_to_put.push({
key: MerkleTree.index_to_key(this.prefix, level, element_index),
value: this.current_element,
});
this.current_element = this.hasher.hash(level, left, right);
})
this.current_element = this.hasher.hash(level, left, right)
}
}
let traverser = new UpdateTraverser(
@ -146,15 +146,15 @@ class MerkleTree {
this.hasher,
element,
this.zero_values
);
)
await this.traverse(index, traverser);
await this.traverse(index, traverser)
traverser.key_values_to_put.push({
key: MerkleTree.index_to_key(this.prefix, this.n_levels, 0),
value: traverser.current_element,
});
})
await this.storage.put_batch(traverser.key_values_to_put);
await this.storage.put_batch(traverser.key_values_to_put)
} catch(e) {
console.error(e)
}
@ -167,18 +167,18 @@ class MerkleTree {
}
async traverse(index, handler) {
let current_index = index;
let current_index = index
for (let i = 0; i < this.n_levels; i++) {
let sibling_index = current_index;
let sibling_index = current_index
if (current_index % 2 == 0) {
sibling_index += 1;
sibling_index += 1
} else {
sibling_index -= 1;
sibling_index -= 1
}
await handler.handle_index(i, current_index, sibling_index);
current_index = Math.floor(current_index / 2);
await handler.handle_index(i, current_index, sibling_index)
current_index = Math.floor(current_index / 2)
}
}
}
module.exports = MerkleTree;
module.exports = MerkleTree

View File

@ -1,13 +1,13 @@
const circomlib = require('circomlib');
const mimcsponge = circomlib.mimcsponge;
const snarkjs = require('snarkjs');
const circomlib = require('circomlib')
const mimcsponge = circomlib.mimcsponge
const snarkjs = require('snarkjs')
const bigInt = snarkjs.bigInt;
const bigInt = snarkjs.bigInt
class MimcSpongeHasher {
hash(level, left, right) {
return mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString();
return mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString()
}
}
module.exports = MimcSpongeHasher;
module.exports = MimcSpongeHasher

View File

@ -2,17 +2,17 @@
class JsStorage {
constructor() {
this.db = {};
this.db = {}
}
get(key) {
return this.db[key];
return this.db[key]
}
get_or_element(key, defaultElement) {
const element = this.db[key];
const element = this.db[key]
if (element === undefined) {
return defaultElement;
return defaultElement
} else {
return element
}
@ -22,18 +22,18 @@ class JsStorage {
if (key === undefined || value === undefined) {
throw Error('key or value is undefined')
}
this.db[key] = value;
this.db[key] = value
}
del(key) {
delete this.db[key];
delete this.db[key]
}
put_batch(key_values) {
key_values.forEach(element => {
this.db[element.key] = element.value;
});
this.db[element.key] = element.value
})
}
}
module.exports = JsStorage;
module.exports = JsStorage

View File

@ -1,5 +1,5 @@
const Migrations = artifacts.require("Migrations");
const Migrations = artifacts.require('Migrations')
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
deployer.deploy(Migrations)
}

View File

@ -1,24 +1,23 @@
const path = require('path');
const path = require('path')
const mimcGenContract = require('circomlib/src/mimcsponge_gencontract.js');
const Artifactor = require('truffle-artifactor');
const mimcGenContract = require('circomlib/src/mimcsponge_gencontract.js')
const Artifactor = require('truffle-artifactor')
const SEED = 'mimcsponge';
const SEED = 'mimcsponge'
module.exports = function(deployer) {
return deployer.then( async () => {
const contractsDir = path.join(__dirname, '..', 'build/contracts');
let artifactor = new Artifactor(contractsDir);
let mimcContractName = 'MiMC';
const contractsDir = path.join(__dirname, '..', 'build/contracts')
let artifactor = new Artifactor(contractsDir)
let mimcContractName = 'MiMC'
await artifactor.save({
contractName: mimcContractName,
abi: mimcGenContract.abi,
unlinked_binary: mimcGenContract.createCode(SEED, 220),
}).then(async () => {
const MiMC = artifacts.require(mimcContractName)
await deployer.deploy(MiMC)
})
.then(async () => {
const MiMC = artifacts.require(mimcContractName);
await deployer.deploy(MiMC);
});
});
};
})
}

View File

@ -1,6 +1,6 @@
require('dotenv').config()
const Verifier = artifacts.require("Verifier");
const Verifier = artifacts.require('Verifier')
module.exports = function(deployer) {
deployer.deploy(Verifier);
};
deployer.deploy(Verifier)
}

View File

@ -1,7 +1,7 @@
require('dotenv').config({ path: '../.env' })
const Mixer = artifacts.require("Mixer");
const Verifier = artifacts.require("Verifier");
const MiMC = artifacts.require("MiMC");
const Mixer = artifacts.require('Mixer')
const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require('MiMC')
module.exports = function(deployer) {
@ -11,6 +11,6 @@ module.exports = function(deployer) {
const miMC = await MiMC.deployed()
await Mixer.link(MiMC, miMC.address)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT)
console.log("Mixer's address ", mixer.address)
console.log('Mixer\'s address ', mixer.address)
})
};
}

View File

@ -1,5 +1,5 @@
require('dotenv').config()
const HDWalletProvider = require('truffle-hdwallet-provider');
const HDWalletProvider = require('truffle-hdwallet-provider')
const utils = require('web3-utils')
// const infuraKey = "fj4jll3k.....";
//
@ -25,9 +25,9 @@ module.exports = {
// options below to some value.
development: {
host: "127.0.0.1", // Localhost (default: none)
host: '127.0.0.1', // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
network_id: '*', // Any network (default: none)
},
// Another network with more advanced options...
@ -68,7 +68,7 @@ module.exports = {
// Configure your compilers
compilers: {
solc: {
version: "0.5.8", // Fetch exact version from solc-bin (default: truffle's version)
version: '0.5.8', // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {