How to use the snarkjs.bigInt function in snarkjs

To help you get started, we’ve selected a few snarkjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github barryWhiteHat / maci / app / utils / crypto.js View on Github external
const sign = (prv: BigInt, _msg: BigInt): { R8: BigInt, S: BigInt } => {
  // Doing this as bigInt2Buffer requires a custom
  // methods 'greater' than isn't in the standard bigint
  // object (its a snarkjs custom bigint obj method)
  const msg = bigInt(_msg)

  const h1 = bigInt2Buffer(hash(prv))
  const sBuff = eddsa.pruneBuffer(h1.slice(0, 32))
  const s = bigInt.leBuff2int(sBuff)
  const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3))

  const msgBuff = bigInt.leInt2Buff(
    msg,
    32
  )

  const rBuff = bigInt2Buffer(hash(
    buffer2BigInt(Buffer.concat(
      [h1.slice(32, 64), msgBuff]
    ))
  ))
github barryWhiteHat / maci / boilerplate / crypto / ts / index.ts View on Github external
const genPrivKey: PrivKey = () => {

    // Check whether we are using the correct value for SNARK_FIELD_SIZE
    assert(SNARK_FIELD_SIZE.eq(snarkjs.bn128.r))

    // Prevent modulo bias
    const min = (
        (snarkjs.bigInt(2).pow(snarkjs.bigInt(256))) - SNARK_FIELD_SIZE
    ) % SNARK_FIELD_SIZE

    let rand: SnarkBigInt

    while (true) {
        rand = snarkjs.bigInt('0x' + crypto.randomBytes(32).toString('hex'))

        if (rand >= min) {
            break
        }
    }

    const privKey = rand % SNARK_FIELD_SIZE
    assert(privKey < SNARK_FIELD_SIZE)

    return privKey
github barryWhiteHat / maci / src / legacy / Add_New_Leaf / stringifybigint.js View on Github external
function unstringifyBigInts (o) {
  if ((typeof (o) === 'string') && (/^[0-9]+$/.test(o))) {
    return bigInt(o)
  } else if (Array.isArray(o)) {
    return o.map(unstringifyBigInts)
  } else if (typeof o === 'object') {
    const res = {}
    for (let k in o) {
      res[k] = unstringifyBigInts(o[k])
    }
    return res
  } else {
    return o
  }
}
github barryWhiteHat / maci / app / utils / merkletree.js View on Github external
hash (values: Any | Array<Any>): BigInt {
    if (Array.isArray(values)) {
      return BigInt(multiHash(values.map((x: Any): BigInt => bigInt(x))))
    }

    return bigInt(multiHash([BigInt(values)]))
  }
github barryWhiteHat / maci / app / utils / crypto.js View on Github external
const hashLeftRight = (left: BigInt, right: BigInt): BigInt => {
  return bigInt(multiHash([bigInt(left), bigInt(right)]))
}
github barryWhiteHat / maci / app / utils / crypto.js View on Github external
    return ret.map((x: Any): BigInt => bigInt(x))
  }
github barryWhiteHat / maci / src / legacy / Add_New_Leaf / MiMCMerkle.js View on Github external
rootFromLeafAndPath: function (leaf, idx, merkle_path) {
    if (merkle_path.length > 0) {
      const depth = merkle_path.length
      const merkle_path_pos = module.exports.idxToBinaryPos(idx, depth)
      var root = new Array(depth)
      left = bigInt(leaf) - bigInt(merkle_path_pos[0]) * (bigInt(leaf) - bigInt(merkle_path[0]))
      right = bigInt(merkle_path[0]) - bigInt(merkle_path_pos[0]) * (bigInt(merkle_path[0]) - bigInt(leaf))
      root[0] = mimcjs.multiHash([left, right])
      var i
      for (i = 1; i < depth; i++) {
        left = root[i - 1] - bigInt(merkle_path_pos[i]) * (root[i - 1] - bigInt(merkle_path[i]))
        right = bigInt(merkle_path[i]) - bigInt(merkle_path_pos[i]) * (bigInt(merkle_path[i]) - root[i - 1])
        root[i] = mimcjs.multiHash([left, right])
      }

      return root[depth - 1]
    } else {
      return leaf
    }
  },
github barryWhiteHat / maci / app / utils / merkletree.js View on Github external
      return BigInt(multiHash(values.map((x: Any): BigInt => bigInt(x))))
    }