test_helpers.mjs 9.17 KB
Newer Older
John Doe's avatar
John Doe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
import { randomBytes } from 'crypto'
import { readFileSync } from 'fs';
import { Fr } from '@aztec/bb.js/dest/types/index.js';
import { MerkleTree } from './MerkleTree.mjs';
import { keccak256 } from "@ethersproject/keccak256/lib/index.js";
import fs from 'fs'

export function randomBytesFr(numBytes) {
  const bytes = randomBytes(numBytes)
  const bytesFr = Fr.fromBufferReduce(bytes)
  return bytesFr
}

export const format = (data) => {
    if (data.length === 0) return "[]"
    return `[\n    "${data.join('",\n    "')}"\n]`;
}

export const dumpToml = (data) => {
    data =
    `
            tx_in = ${format(data.tx_in)}
            amount_public_in = "${data.amount_public_in}"
            amount_public_out = "${data.amount_public_out}"
            commitment_out = ${format(data.commitment_out)}
            recipient = "${data.recipient}"
            oracle = "${data.oracle}"
            old_root = "${data.old_root}"
            new_root = "${data.new_root}"
            secrets = ${format(data.secrets)}
            utxo_in = ${format(data.utxo_in)}
            utxo_out = ${format(data.utxo_out)}
            roots = ${format(data.roots)}
            leaves = ${format(data.leaves)}
            indexes = ${format(data.indexes)}
            hash_path = ${format(data.hash_path)}
            nullifier_hashes = ${format(data.nullifiers)}
    `
    fs.writeFileSync('./Prover.toml', data);
}

export function path_to_uint8array(path) {
    let buffer = readFileSync(path);
    return new Uint8Array(buffer);
  }
  
const toFixedHex = (number, pad0x, length = 32) => {
  let hexString = number.toString(16).padStart(length * 2, '0');
  return (pad0x ? `0x` + hexString : hexString);
}

export function getSolidityHash(asset) {
  // Flatten the object
  asset = 0;
  return keccak256(asset);
}
  
export function generateHashPathInput(hash_path) {
  let hash_path_input = [];
  for (var i = 0; i < hash_path.length; i++) {
    hash_path_input.push(`0x` + hash_path[i]);
  }
  return hash_path_input;
}

export function generateUTXO(batchSize, amounts, _secrets, BarretenbergApi) {
  let utxos = []
  for (let i = 0; i < batchSize; i++) {
    let amountBN = amounts[i]
    let utxo = {
      secret: _secrets[i],
      owner: BarretenbergApi.pedersenPlookupCompress([_secrets[i]]),
      amountBN: amountBN,
      amount: Fr.fromString(toFixedHex(Number(amountBN.toString()), true)),
      assetType: Fr.fromBufferReduce(Buffer.from(getSolidityHash(0), 'hex')),
    }
    utxos.push(utxo)
  }
  return utxos
}

export function generateTreeProof(utxoIn, trees, BarretenbergApi) {
  let treeProof = []
  for (let i = 0; i < utxoIn.length; i++) {
    let commitment = BarretenbergApi.pedersenPlookupCompress(
      [utxoIn[i].owner, utxoIn[i].amount, utxoIn[i].assetType]
    ).toString()
    let proofs = {
        utxo: {
            leaf: commitment,
            index: trees.utxoTreeOld.getIndex(commitment),
            root: trees.utxoTreeOld.root(),
            hash_path: trees.utxoTreeOld.proof(
              trees.utxoTreeOld.getIndex(commitment)
            ).pathElements
        },
        tx: {
            leaf: trees.utxoTreeOld.root(),
            index: trees.txTreeOld.getIndex(trees.utxoTreeOld.root()),
            root: trees.txTreeOld.root(),
            hash_path: trees.txTreeOld.proof(
              trees.txTreeOld.getIndex(trees.utxoTreeOld.root())
            ).pathElements
        },
        batch: {
            leaf: trees.batchLeaf,
            index: trees.batch_tree.getIndex(trees.batchLeaf),
            root: trees.batch_tree.root(),
            hash_path: trees.batch_tree.proof(
              trees.batch_tree.getIndex(trees.batchLeaf)
            ).pathElements
        },
        historic: {
            leaf: trees.newHistoricRoot,
            index: trees.historic_tree.getIndex(trees.newHistoricRoot),
            root: trees.historic_tree.root(),
            hash_path: trees.historic_tree.proof(
              trees.historic_tree.getIndex(trees.newHistoricRoot)
            ).pathElements
        }
      }
    treeProof.push(proofs)
  }
  return treeProof
}

export function generateDataToml(oldRoot, newRoot, api) {
  let zeroHash = api.pedersenPlookupCompress([Fr.fromString(toFixedHex(0, true))])

  const data = {
    tx_in: new Array(16).fill('0xf35fcb490b7ea67c3ac26ed530fa5d8dfe8be344e7177ebb63fe02723fb6f725'),
    secrets: new Array(16).fill('0'),
    utxo_in: new Array(48).fill('0'),
    utxo_out: new Array(48).fill('0'),
    oracle: zeroHash.toString(),
    old_root: "0xf35fcb490b7ea67c3ac26ed530fa5d8dfe8be344e7177ebb63fe02723fb6f725",
    new_root: "0xf35fcb490b7ea67c3ac26ed530fa5d8dfe8be344e7177ebb63fe02723fb6f725",
    roots: new Array(64).fill('0'),
    leaves: new Array(64).fill('0'),
    indexes: new Array(64).fill('0'),
    hash_path: new Array(288).fill('0'),
    commitment_out: new Array(16).fill('0xf35fcb490b7ea67c3ac26ed530fa5d8dfe8be344e7177ebb63fe02723fb6f725'),
    amount_public_in: "0",
    amount_public_out: "0",
    nullifiers: new Array(16).fill('0'),
    recipient: Fr.fromString(toFixedHex(0, true)).toString()
  }

  if (oldRoot !== "0") data.old_root = oldRoot;
  if (newRoot !== "0") data.new_root = newRoot;

  return data

}

export function generateTestTransaction(utxoIn, utxoOut, trees, treeProof, amountPublic, data, recipient, BarretenbergApi) {
  let utxoInLen = utxoIn.length
  let utxoOutLen = utxoOut.length
  
  // UTXOs being spent
  for (let i = 0; i < utxoInLen; i++) {
    let ownerHex = utxoIn[i].owner.toString();
    let amountHex = utxoIn[i].amount.toString();
    let assetTypeHex = utxoIn[i].assetType.toString();
    
    let note_commitment = BarretenbergApi.pedersenPlookupCompress([utxoIn[i].owner, utxoIn[i].amount, utxoIn[i].assetType]);
    let utxoLeaf = note_commitment.toString()

    data.secrets[i] = utxoIn[i].secret
    data.nullifiers[i] = BarretenbergApi.pedersenPlookupCompressFields(utxoIn[i].secret, utxoIn[i].secret)

    data.utxo_in[i*3 + 0] = ownerHex
    data.utxo_in[i*3 + 1] = amountHex
    data.utxo_in[i*3 + 2] = assetTypeHex

    data.leaves[i*4 + 0] = utxoLeaf
    data.leaves[i*4 + 1] = treeProof[i].tx.leaf
    data.leaves[i*4 + 2] = treeProof[i].batch.leaf
    data.leaves[i*4 + 3] = treeProof[i].historic.leaf

    data.indexes[i*4 + 0] = treeProof[i].utxo.index
    data.indexes[i*4 + 1] = treeProof[i].tx.index
    data.indexes[i*4 + 2] = treeProof[i].batch.index
    data.indexes[i*4 + 3] = treeProof[i].historic.index

    data.roots[i*4 + 0] = treeProof[i].utxo.root
    data.roots[i*4 + 1] = treeProof[i].tx.root
    data.roots[i*4 + 2] = treeProof[i].batch.root
    data.roots[i*4 + 3] = treeProof[i].historic.root
    
    let utxoPath = treeProof[i].utxo.hash_path
    let txPath = treeProof[i].tx.hash_path
    let batchPath = treeProof[i].batch.hash_path
    let historicPath = treeProof[i].historic.hash_path

    for (let j = 0; j < utxoPath.length; j++) {
      data.hash_path[i*18 + 0 + j] = utxoPath[j]
      data.hash_path[i*18 + 4 + j] = txPath[j]
    }

    for (let k = 0; k < batchPath.length; k++) {
      data.hash_path[i*18 + 8 + k] = batchPath[k]
      data.hash_path[i*18 + 13 + k] = historicPath[k]
    }
  }
  
  // UTXOs being generated
  for (let i = 0; i < utxoOutLen; i++) {
    let ownerHex = utxoOut[i].owner.toString();
    let amountHex = utxoOut[i].amount.toString();
    let assetTypeHex = utxoOut[i].assetType.toString();
    
    let note_commitment = BarretenbergApi.pedersenPlookupCompress([utxoOut[i].owner, utxoOut[i].amount, utxoOut[i].assetType]);
    let utxoLeaf = note_commitment.toString()

    trees.utxo_tree.insert(utxoLeaf)

    data.utxo_out[i*3 + 0] = ownerHex
    data.utxo_out[i*3 + 1] = amountHex
    data.utxo_out[i*3 + 2] = assetTypeHex

    data.commitment_out[i] = utxoLeaf
  }
  data.tx_in[0] = trees.utxo_tree.root()
  data.amount_public_in = toFixedHex(Number(amountPublic.amountIn.toString()), true)
  data.amount_public_out = toFixedHex(Number(amountPublic.amountOut.toString()), true)
  data.recipient = recipient
  dumpToml(data)
}

export function generateTestPublish(trees, data, api) {
  // Publish our local set of test txs

  let utxoTree = trees.utxo_tree
  let txTree = trees.tx_tree
  let batchTree = trees.batch_tree
  let historicTree = trees.historic_tree

  let utxoRoot = utxoTree.root()
  txTree.insert(utxoRoot)
  let txRoot = txTree.root()
  let txRootFr = Fr.fromBufferReduce(Buffer.from(txRoot.slice(2), 'hex'))
  let oracleFr = Fr.fromBufferReduce(toFixedHex(0, true))
  let oracleHash = api.pedersenPlookupCompress([oracleFr])
  let batch = api.pedersenPlookupCompressFields(txRootFr, oracleHash)
  let batchHex = batch.toString()
  batchTree.insert(batchHex)

  let oldHistoricRoot = Fr.fromBufferReduce(Buffer.from(data.new_root.slice(2), 'hex'))
  let newHistoricRoot = api.pedersenPlookupCompress([batch, oldHistoricRoot])

  let newHistoricRootHex = newHistoricRoot.toString()
  historicTree.insert(newHistoricRootHex)

  data.old_root = oldHistoricRoot.toString()
  data.new_root = newHistoricRootHex

  // Clearing the utxo tree for the next batch
  // Saving the old tree for proofs
  trees.utxoTreeOld = trees.utxo_tree
  trees.txTreeOld = trees.tx_tree
  trees.batchLeaf = batchHex
  trees.newHistoricRoot = newHistoricRootHex

  trees.utxo_tree = new MerkleTree(4, api)
  trees.tx_tree = new MerkleTree(4, api)

  dumpToml(data)
}