mirror of
https://github.com/tornadocash/tornado-core.git
synced 2024-11-22 01:37:07 +01:00
code style
This commit is contained in:
parent
a7fedcd7ea
commit
7eaa5fcb4f
@ -1,99 +1,99 @@
|
||||
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
|
||||
this.totalElements = defaultElements.length
|
||||
defaultElements.forEach((element, i) => {
|
||||
this.storage.put(MerkleTree.index_to_key(prefix, level, i), element)
|
||||
})
|
||||
level++
|
||||
let numberOfElementInRow = Math.ceil(defaultElements.length / 2)
|
||||
for (level; level <= this.n_levels; level++) {
|
||||
for(let i = 0; i < numberOfElementInRow; i++) {
|
||||
const leftKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i)
|
||||
const rightKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i + 1)
|
||||
let level = 0
|
||||
this.totalElements = defaultElements.length
|
||||
defaultElements.forEach((element, i) => {
|
||||
this.storage.put(MerkleTree.index_to_key(prefix, level, i), element)
|
||||
})
|
||||
level++
|
||||
let numberOfElementInRow = Math.ceil(defaultElements.length / 2)
|
||||
for (level; level <= this.n_levels; level++) {
|
||||
for(let i = 0; i < numberOfElementInRow; i++) {
|
||||
const leftKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i)
|
||||
const rightKey = MerkleTree.index_to_key(prefix, level - 1, 2 * i + 1)
|
||||
|
||||
const left = this.storage.get(leftKey)
|
||||
const right = this.storage.get_or_element(rightKey, this.zero_values[level - 1])
|
||||
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);
|
||||
this.storage.put(MerkleTree.index_to_key(prefix, level, i), subRoot)
|
||||
}
|
||||
numberOfElementInRow = Math.max(Math.ceil(numberOfElementInRow / 2), 1)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,17 +146,17 @@ 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)
|
||||
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;
|
||||
if (current_index % 2 == 0) {
|
||||
sibling_index += 1;
|
||||
} else {
|
||||
sibling_index -= 1;
|
||||
}
|
||||
await handler.handle_index(i, current_index, sibling_index);
|
||||
current_index = Math.floor(current_index / 2);
|
||||
let sibling_index = current_index
|
||||
if (current_index % 2 == 0) {
|
||||
sibling_index += 1
|
||||
} else {
|
||||
sibling_index -= 1
|
||||
}
|
||||
await handler.handle_index(i, current_index, sibling_index)
|
||||
current_index = Math.floor(current_index / 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MerkleTree;
|
||||
module.exports = MerkleTree
|
||||
|
16
lib/MiMC.js
16
lib/MiMC.js
@ -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();
|
||||
}
|
||||
hash(level, left, right) {
|
||||
return mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MimcSpongeHasher;
|
||||
module.exports = MimcSpongeHasher
|
||||
|
@ -2,38 +2,38 @@
|
||||
|
||||
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
|
||||
return element
|
||||
}
|
||||
}
|
||||
|
||||
put(key, value) {
|
||||
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) {
|
||||
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
|
||||
|
@ -1,5 +1,5 @@
|
||||
const Migrations = artifacts.require("Migrations");
|
||||
const Migrations = artifacts.require('Migrations')
|
||||
|
||||
module.exports = function(deployer) {
|
||||
deployer.deploy(Migrations);
|
||||
};
|
||||
deployer.deploy(Migrations)
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
};
|
||||
}
|
||||
|
@ -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,19 +25,19 @@ module.exports = {
|
||||
// options below to some value.
|
||||
|
||||
development: {
|
||||
host: "127.0.0.1", // Localhost (default: none)
|
||||
port: 8545, // Standard Ethereum port (default: none)
|
||||
network_id: "*", // Any network (default: none)
|
||||
host: '127.0.0.1', // Localhost (default: none)
|
||||
port: 8545, // Standard Ethereum port (default: none)
|
||||
network_id: '*', // Any network (default: none)
|
||||
},
|
||||
|
||||
// Another network with more advanced options...
|
||||
// advanced: {
|
||||
// port: 8777, // Custom port
|
||||
// network_id: 1342, // Custom network
|
||||
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
|
||||
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
|
||||
// from: <address>, // Account to send txs from (default: accounts[0])
|
||||
// websockets: true // Enable EventEmitter interface for web3 (default: false)
|
||||
// port: 8777, // Custom port
|
||||
// network_id: 1342, // Custom network
|
||||
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
|
||||
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
|
||||
// from: <address>, // Account to send txs from (default: accounts[0])
|
||||
// websockets: true // Enable EventEmitter interface for web3 (default: false)
|
||||
// },
|
||||
|
||||
// Useful for deploying to a public network.
|
||||
@ -54,9 +54,9 @@ module.exports = {
|
||||
|
||||
// Useful for private networks
|
||||
// private: {
|
||||
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
|
||||
// network_id: 2111, // This network is yours, in the cloud.
|
||||
// production: true // Treats this network as if it was a public net. (default: false)
|
||||
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
|
||||
// network_id: 2111, // This network is yours, in the cloud.
|
||||
// production: true // Treats this network as if it was a public net. (default: false)
|
||||
// }
|
||||
},
|
||||
|
||||
@ -68,14 +68,14 @@ 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: {
|
||||
enabled: false,
|
||||
runs: 200
|
||||
},
|
||||
// evmVersion: "byzantium"
|
||||
optimizer: {
|
||||
enabled: false,
|
||||
runs: 200
|
||||
},
|
||||
// evmVersion: "byzantium"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user