Catalog npm
Back to packageRELEASE DETAILS

0.0.1

npm

@0xagnish/zk-data-prep

## Structuring datasets into Merkle Sum Trees

circomcircomlibjssnarkjszero-knowledgezk
Published28 Mar 2023
Direct dependencies9
Transitive dependenciesNot calculated

README

Current package documentation

zkDataPrep

Structuring datasets into Merkle Sum Trees

This is a Typescript (backend-friendly) library used to create Merkle Sum Trees where each node entry is username -> balance. The root of this tree contains the aggregated sum of all the node entries, thereby, representing the total assets/liabilities of a firm.

But coming to the point.

What is a Merkle Sum Tree?

A merkle sum tree is a binary Merkle Tree with the following properties:

  • Each entry of the merkle tree is a struct, of username and balance
  • Each leaf node of the merkle tree contains a hash and a sum. The hash is equal to h(username, balance). The sum is equal to the balance itself, at the node level.
  • Each non-leaf node contains a hash and a sum. The hash is equal to h(left_child.hash, left_child.sum, right_child.hash, right_child.sum). The sum is therefore equal to the sum of the sum of it's children.
  • The root node represents the executed/constructed state of the tree and contains all the users'/entry balances. The merkleSumTree() is a Typescript implementation of a Merkle Sum Tree and it provides all the functions to construct the above mentioned modified Merkle Tree by just ingesting data from a csv file, picking up only the username -> balance.

This diagram is a representation of a similar Merkle Sum Tree

image

Setup

Installing the package

  • The package can be directly installed from the npm registry with the following command:
    $ npm i @0xagnish/zk-data-prep
    

For Database

  • Import your database to a csv file, for example this

API Guide

new MerkleSumTree (pathToCsv : string) : MerkleSumTree

import { MerkleSumTree } from '0xagnish/zk-data-prep';

const pathToCsv = 'test/entryPatterns/entry-16-valid.csv';

const tree = new MerkleSumTree(pathToCsv);
//constructs a tree using the given entries in the csv dataset

entries: [] Entry

The entries contain the username parsed as BigInt. This transformation is needed in order to make the entries hashing friendly and later on operate with zkSNARKs.

const entries = tree.entries
// [
//       Entry {
//         _usernameToBigInt: 7440338505707899769n,
//         _balance: 7534n,
//         _username: 'gAdsIaKy'
//       },
//       Entry {
//         _usernameToBigInt: 6008493982388733799n,
//         _balance: 2060n,
//         _username: 'SbuqOZGg'
//       },
//       ...
// ]

leaves : [] Node

const root = tree.root 
    // {
    //   hash: 5256203632563331423195629050622063453704745190370457907459595269961493651429n,
    //   sum: 84359n
    // }

indexOf (username: string, balance: bigint) : number

Returns the index of an entry in the tree. If the entry does not exist in the tree, it returns -1.

const index = tree.indexOf("gAdsIaKy", BigInt(7534)) // 0

createProof (index: number) : MerkleProof

Creates a proof of membership for an entry identified by its index. The MerkleProof contains the path from the leaf to the root.

const proof = tree.createProof(0)

verifyProof(proof: MerkleProof) : boolean

Verifies a proof and returns true or false. It verifies that a leaf is included in the tree and that the sum computed from the leaf to the root is equal to the total sum of the tree.

tree.verifyProof(proof)

Benchmarking

To construct a Merkle Sum Tree with 262144 (2**18 leaves) it takes:

Time takenOS/KernelRAM
108sUbuntu 22 LTS8 GB
154sMacbook Air M18 GB

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 9

Native requirements
9 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
big-integer
Environment
{
  "local_name": "big-integer",
  "alias": false,
  "peer_metadata": null
}
^1.6.51runtime
circomlibjs
Environment
{
  "local_name": "circomlibjs",
  "alias": false,
  "peer_metadata": null
}
^0.0.8runtime
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^29.5.0development
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^29.5.0development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
^2.8.1development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^29.0.3development
tslint
Environment
{
  "local_name": "tslint",
  "alias": false,
  "peer_metadata": null
}
^6.1.3development
tslint-config-prettier
Environment
{
  "local_name": "tslint-config-prettier",
  "alias": false,
  "peer_metadata": null
}
^1.18.0development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^4.9.4development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-IdIO6thYXJ9Jvd4T4Pd3tkWaw/tECjNrC5koKGT3oINztIC5IHVbu6xaLcod1FPDaHwt+6hr6ZwGDez8VnUJWQ==",
  "shasum": "b4fa05317884a6c3d670594555f7aafc410a4fbe",
  "tarball": "https://registry.npmjs.org/@0xagnish/zk-data-prep/-/zk-data-prep-0.0.1.tgz",
  "fileCount": 46,
  "unpackedSize": 3935591,
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIBoOGkOaNCsBThmQ0unKOm2afDpC//22KYCIyKYsryOsAiEAyityAc5u0hqCN7KIo6VQIpO6au7BHzX5pv3xoLuyEfs="
    }
  ],
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkI1m+ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmra3A/9EZXl8gex79Rap/Nq16uo1LrigzVqEMg6twug3mKv81y79ecB\r\noOFb/C7ZbIcivuJKTSm3y7nFwk/OMddOZKc+KuZq9+aB8oH6Jxhdthb8KlMB\r\n3MELDhwBoK1rrp5y8NWhOMyq0tJPxUmPabegzhmxDZUU/aq/gwAAqVMPsvAS\r\nyeCVm4s/Ad/WCIOX8OxksVxmtjS3P+2GI0QF1oN95k2BTB9GzIa3g51mTBJV\r\nLfKIw3MSmrEap4hDwYpzU3jA8fflYVcAWXpaiCjG3x+1VnMDoMgYOyZnFtpy\r\nuvivzYR3ups6vkACAL2DhlvHcY8NiOxr11fr9rxZmRjnT8sliZqxgEgQzT2s\r\nPXpbZa2BaedX79a2s9JRf+kKSTNdEVDrXvU0Y6lOAn3Wz6tupiq+m+Tvmyh1\r\nDbV2/H/KC9fw7oWyrwNSna4hAjflp1cOuWY0f61UVgsX2TeSX5hL2Cm0SSFK\r\neqWsz7gfwCPbhu3wJuTNqfN6T4VgE49JLLjOPqqj3k2zI1tMrGTzCwZm0HPW\r\ns9dRgeUA0McXubveEO78evXj8TBgNtxXGpL++zFB/H56WD07UwSgyHm2AoBf\r\n41Fm6ZaLbWdyGwTPiTGeENYs6NkVVPR+o/nGh/mOsYQrqBfcN464dICYheHg\r\nSm2SfVmfSpAoYUU1WRwFTWJ4nVltG1P+rV0=\r\n=I/jJ\r\n-----END PGP SIGNATURE-----\r\n"
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:40 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:862bcf071eacb411b10dc7696f5b25bc4e93fa2261f31e4b9b97aef024458f6b

Other collected versions

0.0.46 Jul0.0.34 Jul0.0.228 MarView all collected versions →