remove deps, add simple hashFunction for example and tests.

This commit is contained in:
Sergei SMART 2022-02-24 13:21:14 +10:00
parent 107217f74d
commit 5f036748a9
6 changed files with 2449 additions and 4995 deletions

2359
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "fixed-merkle-tree",
"version": "0.6.1",
"version": "0.7.0",
"description": "Fixed depth merkle tree implementation with sequential inserts",
"repository": "https://github.com/tornadocash/fixed-merkle-tree.git",
"main": "lib/index.js",
@ -8,7 +8,9 @@
"scripts": {
"test": "ts-mocha 'test/*.spec.ts'",
"build": "tsc",
"lint": "eslint ."
"clean": "rm -rf lib/",
"prepare": "npm run clean && npm run build",
"lint": "eslint src"
},
"keywords": [
"merkle",
@ -20,9 +22,7 @@
"files": [
"src/*"
],
"dependencies": {
"circomlib": "git+https://github.com/tornadocash/circomlib.git#5beb6aee94923052faeecea40135d45b6ce6172c"
},
"dependencies": {},
"devDependencies": {
"@types/expect": "^24.3.0",
"@types/mocha": "^9.1.0",

View File

@ -1,9 +1,6 @@
import { mimcsponge } from 'circomlib'
import simpleHash from './simpleHash'
// keccak256("tornado") % BN254_FIELD_SIZE
const DEFAULT_ZERO = '21663839004416932945382355908790599225266501822907911457504978515578255421292'
const defaultHash = (left: Element, right: Element): string => mimcsponge.multiHash([BigInt(left), BigInt(right)]).toString()
const defaultHash = (left: Element, right: Element): string => simpleHash([left, right])
export default class MerkleTree {
get layers(): Array<Element[]> {
@ -23,7 +20,7 @@ export default class MerkleTree {
constructor(levels: number, elements: Element[] = [], {
hashFunction = defaultHash,
zeroElement = DEFAULT_ZERO,
zeroElement = 0,
}: MerkleTreeOptions = {}) {
this.levels = levels
this.capacity = 2 ** levels
@ -131,30 +128,30 @@ export default class MerkleTree {
* @param {number} index Leaf index to generate path for
* @returns {{pathElements: Object[], pathIndex: number[]}} An object containing adjacent elements and left-right index
*/
path(index: Element) {
path(index: Element): ProofPath {
if (isNaN(Number(index)) || index < 0 || index >= this._layers[0].length) {
throw new Error('Index out of bounds: ' + index)
}
let elIndex = +index
const pathElements: Element[] = []
const pathIndices: number[] = []
const layerIndices: number[] = []
const pathPositions: number [] = []
for (let level = 0; level < this.levels; level++) {
pathIndices[level] = elIndex % 2
const leafIndex = elIndex ^ 1
if (leafIndex < this._layers[level].length) {
pathElements[level] = this._layers[level][leafIndex]
layerIndices[level] = leafIndex
pathPositions[level] = leafIndex
} else {
pathElements[level] = this._zeros[level]
layerIndices[level] = 0
pathPositions[level] = 0
}
elIndex >>= 1
}
return {
pathElements,
pathIndices,
layerIndices,
pathPositions,
}
}
@ -268,6 +265,9 @@ export type SerializedTreeState = {
_zeros: Array<Element>,
_layers: Array<Element[]>
}
export type Mimcsponge = {
multiHash: (arr: BigInt[], key?: Element, numOutputs?) => string
export type ProofPath = {
pathElements: Element[],
pathIndices: number[],
pathPositions: number[],
}

20
src/simpleHash.ts Normal file
View File

@ -0,0 +1,20 @@
/***
* This is insecure hash function, just for example only
* @param data
* @param seed
* @param hashLength
*/
function simpleHash<T>(data: T[], seed?: number, hashLength = 40): string {
const str = data.join('')
let i, l,
hval = seed ?? 0x811c9dcc5
for (i = 0, l = str.length; i < l; i++) {
hval ^= str.charCodeAt(i)
hval += (hval << 1) + (hval << 4) + (hval << 6) + (hval << 8) + (hval << 24)
}
const hash = (hval >>> 0).toString(16)
return BigInt('0x' + hash.padEnd(hashLength - (hash.length - 1), '0')).toString(10)
}
export default simpleHash

View File

@ -8,22 +8,22 @@ describe('MerkleTree', () => {
it('should have correct zero root', () => {
const tree = new MerkleTree(10, [])
return should().equal(tree.root(), '14030416097908897320437553787826300082392928432242046897689557706485311282736')
return should().equal(tree.root(), '3060353338620102847451617558650138132480')
})
it('should have correct 1 element root', () => {
const tree = new MerkleTree(10, [1])
should().equal(tree.root(), '8423266420989796135179818298985240707844287090553672312129988553683991994663')
should().equal(tree.root(), '4059654748770657324723044385589999697920')
})
it('should have correct even elements root', () => {
const tree = new MerkleTree(10, [1, 2])
should().equal(tree.root(), '6632020347849276860492323008882350357301732786233864934344775324188835172576')
should().equal(tree.root(), '3715471817149864798706576217905179918336')
})
it('should have correct odd elements root', () => {
const tree = new MerkleTree(10, [1, 2, 3])
should().equal(tree.root(), '13605252518346649016266481317890801910232739395710162921320863289825142055129')
should().equal(tree.root(), '5199180210167621115778229238102210117632')
})
it('should be able to create a full tree', () => {
@ -40,19 +40,19 @@ describe('MerkleTree', () => {
it('should insert into empty tree', () => {
const tree = new MerkleTree(10)
tree.insert(42)
should().equal(tree.root(), '5305397050004975530787056746976521882221645950652996479084366175595194436378')
should().equal(tree.root(), '750572848877730275626358141391262973952')
})
it('should insert into odd tree', () => {
const tree = new MerkleTree(10, [1])
tree.insert(42)
should().equal(tree.root(), '4732716818150428188641303198013632061441036732749853605989871103991103096471')
should().equal(tree.root(), '5008383558940708447763798816817296703488')
})
it('should insert into even tree', () => {
const tree = new MerkleTree(10, [1, 2])
tree.insert(42)
should().equal(tree.root(), '6204016789747878948181936326719724987136198810274146408545977300318734508764')
should().equal(tree.root(), '5005864318873356880627322373636156817408')
})
it('should insert last element', () => {
@ -71,7 +71,7 @@ describe('MerkleTree', () => {
it('should work', () => {
const tree = new MerkleTree(10, [1, 2, 3])
tree.bulkInsert([4, 5, 6])
should().equal(tree.root(), '10132905325673518287563057607527946096399700874345297651940963130460267058606')
should().equal(tree.root(), '4066635800770511602067209448381558554624')
})
it('should give the same result as sequental inserts', () => {
@ -116,31 +116,31 @@ describe('MerkleTree', () => {
it('should update first element', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
tree.update(0, 42)
should().equal(tree.root(), '153077538697962715163231177553585573790587443799974092612333826693999310199')
should().equal(tree.root(), '3884161948856565981263417078389340635136')
})
it('should update last element', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
tree.update(4, 42)
should().equal(tree.root(), '1955192134603843666100093417117434845771298375724087600313714421260719033775')
should().equal(tree.root(), '3564959811529894228734180300843252711424')
})
it('should update odd element', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
tree.update(1, 42)
should().equal(tree.root(), '6642888742811380760154112624880866754768235565211186414088321870395007150538')
should().equal(tree.root(), '4576704573778433422699674477203122290688')
})
it('should update even element', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4, 5])
tree.update(2, 42)
should().equal(tree.root(), '11739358667442647096377238675718917508981868161724701476635082606510350785683')
should().equal(tree.root(), '1807994110952186123819489133812038762496')
})
it('should update extra element', () => {
const tree = new MerkleTree(10, [1, 2, 3, 4])
tree.update(4, 5)
should().equal(tree.root(), '6341751103515285836339987888606244815365572869367801108789753151704260302930')
should().equal(tree.root(), '1099080610107164849381389194938128793600')
})
it('should fail to update incorrect index', () => {
@ -177,15 +177,16 @@ describe('MerkleTree', () => {
assert.deepEqual(path.pathIndices, [0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
assert.deepEqual(path.pathElements, [
4,
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
'21305827034995891902714687670641862055126514524916463201449278400604999416145',
'14506027710748750947258687001455876266559341618222612722926156490737302846427',
'4766583705360062980279572762279781527342845808161105063909171241304075622345',
'16640205414190175414380077665118269450294358858897019640557533278896634808665',
'13024477302430254842915163302704885770955784224100349847438808884122720088412',
'11345696205391376769769683860277269518617256738724086786512014734609753488820',
'17235543131546745471991808272245772046758360534180976603221801364506032471936',
'155962837046691114236524362966874066300454611955781275944230309195800494087',
'4027992409016347597424110157229339967488',
'3591172241203040147397382471352592629760',
'938972308169430750202858820582946897920',
'3743880566844110745576746962917825445888',
'2074434463882483178614385966084599578624',
'2808856778596740691845240322870189490176',
'4986731814143931240516913804278285467648',
'1918547053077726613961101558405545328640',
'5444383861051812288142814494928935059456',
])
})
@ -195,15 +196,16 @@ describe('MerkleTree', () => {
assert.deepEqual(path.pathIndices, [1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
assert.deepEqual(path.pathElements, [
3,
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
'21305827034995891902714687670641862055126514524916463201449278400604999416145',
'14506027710748750947258687001455876266559341618222612722926156490737302846427',
'4766583705360062980279572762279781527342845808161105063909171241304075622345',
'16640205414190175414380077665118269450294358858897019640557533278896634808665',
'13024477302430254842915163302704885770955784224100349847438808884122720088412',
'11345696205391376769769683860277269518617256738724086786512014734609753488820',
'17235543131546745471991808272245772046758360534180976603221801364506032471936',
'155962837046691114236524362966874066300454611955781275944230309195800494087',
'4027992409016347597424110157229339967488',
'3591172241203040147397382471352592629760',
'938972308169430750202858820582946897920',
'3743880566844110745576746962917825445888',
'2074434463882483178614385966084599578624',
'2808856778596740691845240322870189490176',
'4986731814143931240516913804278285467648',
'1918547053077726613961101558405545328640',
'5444383861051812288142814494928935059456',
])
})
@ -220,15 +222,16 @@ describe('MerkleTree', () => {
assert.deepEqual(path.pathIndices, [0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
assert.deepEqual(path.pathElements, [
4,
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
'21305827034995891902714687670641862055126514524916463201449278400604999416145',
'14506027710748750947258687001455876266559341618222612722926156490737302846427',
'4766583705360062980279572762279781527342845808161105063909171241304075622345',
'16640205414190175414380077665118269450294358858897019640557533278896634808665',
'13024477302430254842915163302704885770955784224100349847438808884122720088412',
'11345696205391376769769683860277269518617256738724086786512014734609753488820',
'17235543131546745471991808272245772046758360534180976603221801364506032471936',
'155962837046691114236524362966874066300454611955781275944230309195800494087',
'4027992409016347597424110157229339967488',
'3591172241203040147397382471352592629760',
'938972308169430750202858820582946897920',
'3743880566844110745576746962917825445888',
'2074434463882483178614385966084599578624',
'2808856778596740691845240322870189490176',
'4986731814143931240516913804278285467648',
'1918547053077726613961101558405545328640',
'5444383861051812288142814494928935059456',
])
})
})
@ -238,16 +241,18 @@ describe('MerkleTree', () => {
const layers = [
[1, 2, 3, 4, 5],
[
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
'9256022917525827637821171443533757190340579068025270193352322268529570863974',
'21652272025144185891702495507858700052653521882982711576347377471507927142323',
'4027992409016347597424110157229339967488',
'923221781152860005594997320673730232320',
'752191049236692618445397735417537626112',
],
[
'6464294476958346139385024074008223400825166653076969388043746597957512245037',
'21305827034995891902714687670641862055126514524916463201449278400604999416145',
'81822854828781486047086122479545722339328',
'3591172241203040147397382471352592629760',
],
['13482485030738390475684870940688026655293260583748749623167469686138055064771'],
['5734482689596254215043546232260442114954448597263096309106433719494772338840'],
['2729943778107054496417267081388406865920'],
['4562739390655416913642128116127918718976'],
]
it('should return same elements in array', () => {

4930
yarn.lock

File diff suppressed because it is too large Load Diff