How to use the snarkjs.bigInt.leBuff2int 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 iden3 / iden3js / src / crypto / mimc7.js View on Github external
function hashBuffer(msg) { // msg is a Buffer
  const n = 31;
  const msgArray = [];
  const fullParts = Math.floor(msg.length / n);
  for (let i = 0; i < fullParts; i++) {
    const v = bigInt.leBuff2int(msg.slice(n * i, n * (i + 1)));
    msgArray.push(v);
  }
  if (msg.length % n !== 0) {
    const v = bigInt.leBuff2int(msg.slice(fullParts * n));
    msgArray.push(v);
  }
  return mimc7.multiHash(msgArray);
}
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]
    ))
  ))
  let r = bigInt.leBuff2int(rBuff)
  r = r.mod(babyJub.subOrder)
  const R8 = babyJub.mulPointEscalar(babyJub.Base8, r)
  const hm = multiHash([R8[0], R8[1], A[0], A[1], msg])
github weijiekoh / zkmm / mastermind / src / utils.ts View on Github external
const genSalt = (): bigInt.BigInteger => {
    // the maximum integer supported by Solidity is (2 ^ 256), which is 32
    // bytes long
    const buf = crypto.randomBytes(30)
    const salt = bigInt.leBuff2int(buf).sub(bigInt(340))

    // 4 * (4^3) + 4 * (4^2) + 4 * (4^1) + 4 * (4^0) = 340
    // Only return values greater than the largest possible solution
    if (salt.lt(340)) {
        return genSalt()
    }

    return salt
}
github barryWhiteHat / maci / app / utils / crypto.js View on Github external
const babyJubJubPrivateKey = (priv: BigInt): BigInt => {
  // Formats private key to be babyJubJub compatiable

  // https://tools.ietf.org/html/rfc8032
  // Because of the "buff[0] & 0xF8" part which makes sure you have a point with order that 8 divides
  // (^ pruneBuffer)
  // Every point in babyjubjub is of the form: aP + bH, where H has order 8 and P has a big large prime order
  // Guaranteeing that any low order points in babyjubjub get deleted
  // ^From Kobi
  const sBuff = eddsa.pruneBuffer(
    bigInt2Buffer(hash(priv))
      .slice(0, 32)
  )

  return bigInt.leBuff2int(sBuff).shr(3)
}
github iden3 / iden3js / src / crypto / babyjub-utils.js View on Github external
function privToScalar(privKey: Buffer): bigInt {
  const h1 = createBlakeHash('blake512').update(privKey).digest();
  const sBuff = eddsa.pruneBuffer(h1.slice(0, 32));
  const scalar = (bigInt.leBuff2int(sBuff)).shr(3);
  if (scalar >= babyJub.p) {
    throw new Error('scalar generated larger than subgroup');
  }
  return scalar;
}
github barryWhiteHat / maci / app / utils / crypto.js View on Github external
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]
    ))
  ))
  let r = bigInt.leBuff2int(rBuff)
  r = r.mod(babyJub.subOrder)
  const R8 = babyJub.mulPointEscalar(babyJub.Base8, r)
  const hm = multiHash([R8[0], R8[1], A[0], A[1], msg])
  const S = r.add(hm.mul(s)).mod(babyJub.subOrder)
  return {
    R8: R8,
    S: S
  }
}
github iden3 / iden3js / src / crypto / poseidon.js View on Github external
function hashBuffer(msgBuff) {
  const n = 31;
  const msgArray = [];
  const fullParts = Math.floor(msgBuff.length / n);
  for (let i = 0; i < fullParts; i++) {
    const v = bigInt.leBuff2int(msgBuff.slice(n * i, n * (i + 1)));
    msgArray.push(v);
  }
  if (msgBuff.length % n !== 0) {
    const v = bigInt.leBuff2int(msgBuff.slice(fullParts * n));
    msgArray.push(v);
  }
  return multiHash(msgArray);
}
github iden3 / iden3js / src / crypto / eddsa-babyjub.js View on Github external
toPrivScalar(): bigInt {
    const h1 = createBlakeHash('blake512').update(this.sk).digest();
    const sBuff = eddsa.pruneBuffer(h1.slice(0, 32));
    return (bigInt.leBuff2int(sBuff)).shr(3);
  }