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

View File

@ -1,13 +1,13 @@
const circomlib = require('circomlib'); const circomlib = require('circomlib')
const mimcsponge = circomlib.mimcsponge; const mimcsponge = circomlib.mimcsponge
const snarkjs = require('snarkjs'); const snarkjs = require('snarkjs')
const bigInt = snarkjs.bigInt; const bigInt = snarkjs.bigInt
class MimcSpongeHasher { class MimcSpongeHasher {
hash(level, left, right) { 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 { class JsStorage {
constructor() { constructor() {
this.db = {}; this.db = {}
} }
get(key) { get(key) {
return this.db[key]; return this.db[key]
} }
get_or_element(key, defaultElement) { get_or_element(key, defaultElement) {
const element = this.db[key]; const element = this.db[key]
if (element === undefined) { if (element === undefined) {
return defaultElement; return defaultElement
} else { } else {
return element return element
} }
@ -22,18 +22,18 @@ class JsStorage {
if (key === undefined || value === undefined) { if (key === undefined || value === undefined) {
throw Error('key or value is undefined') throw Error('key or value is undefined')
} }
this.db[key] = value; this.db[key] = value
} }
del(key) { del(key) {
delete this.db[key]; delete this.db[key]
} }
put_batch(key_values) { put_batch(key_values) {
key_values.forEach(element => { 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) { 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 mimcGenContract = require('circomlib/src/mimcsponge_gencontract.js')
const Artifactor = require('truffle-artifactor'); const Artifactor = require('truffle-artifactor')
const SEED = 'mimcsponge'; const SEED = 'mimcsponge'
module.exports = function(deployer) { module.exports = function(deployer) {
return deployer.then( async () => { return deployer.then( async () => {
const contractsDir = path.join(__dirname, '..', 'build/contracts'); const contractsDir = path.join(__dirname, '..', 'build/contracts')
let artifactor = new Artifactor(contractsDir); let artifactor = new Artifactor(contractsDir)
let mimcContractName = 'MiMC'; let mimcContractName = 'MiMC'
await artifactor.save({ await artifactor.save({
contractName: mimcContractName, contractName: mimcContractName,
abi: mimcGenContract.abi, abi: mimcGenContract.abi,
unlinked_binary: mimcGenContract.createCode(SEED, 220), 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() require('dotenv').config()
const Verifier = artifacts.require("Verifier"); const Verifier = artifacts.require('Verifier')
module.exports = function(deployer) { module.exports = function(deployer) {
deployer.deploy(Verifier); deployer.deploy(Verifier)
}; }

View File

@ -1,7 +1,7 @@
require('dotenv').config({ path: '../.env' }) require('dotenv').config({ path: '../.env' })
const Mixer = artifacts.require("Mixer"); const Mixer = artifacts.require('Mixer')
const Verifier = artifacts.require("Verifier"); const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require("MiMC"); const MiMC = artifacts.require('MiMC')
module.exports = function(deployer) { module.exports = function(deployer) {
@ -11,6 +11,6 @@ module.exports = function(deployer) {
const miMC = await MiMC.deployed() const miMC = await MiMC.deployed()
await Mixer.link(MiMC, miMC.address) await Mixer.link(MiMC, miMC.address)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT) 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() require('dotenv').config()
const HDWalletProvider = require('truffle-hdwallet-provider'); const HDWalletProvider = require('truffle-hdwallet-provider')
const utils = require('web3-utils') const utils = require('web3-utils')
// const infuraKey = "fj4jll3k....."; // const infuraKey = "fj4jll3k.....";
// //
@ -25,9 +25,9 @@ module.exports = {
// options below to some value. // options below to some value.
development: { development: {
host: "127.0.0.1", // Localhost (default: none) host: '127.0.0.1', // Localhost (default: none)
port: 8545, // Standard Ethereum port (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... // Another network with more advanced options...
@ -68,7 +68,7 @@ module.exports = {
// Configure your compilers // Configure your compilers
compilers: { compilers: {
solc: { 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) // 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 settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: { optimizer: {