How to use circomlib - 10 common examples

To help you get started, we’ve selected a few circomlib 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 / generate_circuit_input.js View on Github external
const txHash = mimc7.multiHash(
  [tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)

// update Alice account
const newAlice = {
  pubkey: tx.updated_pubkey,
  detail: BigInt(tx.detail)
}
const newAliceHash = mimc7.multiHash(
  [newAlice.pubkey[0], newAlice.pubkey[1], BigInt(newAlice.detail)]
)

// update root
const final_root = mimc7.multiHash([newAliceHash, bobHash])

// console.log('tree_root: ' + tree_root.toString())
// console.log('final_root: ' + final_root.toString())
// console.log('accounts_pubkeys Alice.pubkey[0]: ' + Alice.pubkey[0].toString())
// console.log('accounts_pubkeys Alice.pubkey[1]: ' + Alice.pubkey[1].toString())
// console.log('accounts_detail: ' + final_root.toString())
// console.log('sender_detail: ' + Alice.detail.toString())
// console.log('sender_updated_pubkey[0]: ' + newAlice.pubkey[0].toString())
// console.log('sender_updated_pubkey[1]: ' + newAlice.pubkey[1].toString())
// console.log('sender_updated_detail: ' + newAlice.detail.toString())
// console.log("signature['R8'][0]: " + signature.R8[0].toString())
// console.log("signature['R8'][1]: " + signature.R8[1].toString())
// console.log("signature['S']: " + signature.S.toString())
// console.log('aliceHash: ' + aliceHash.toString())
// console.log('bobHash: ' + bobHash.toString())
// console.log('newAliceHash: ' + newAliceHash.toString())
github barryWhiteHat / maci / app / interact.js View on Github external
console.log('Submitted initial message')

  // Wait 10 seconds
  console.log('Sleeping 10 seconds...')
  await sleep(10000)
  console.log('Woken up')

  // Construct 2nd message
  const userSecondMessage = [
    ...userPosition,
    ...userPubKey,
    1n // New position
  ]

  const userSecondMessageHash = mimc7.multiHash(userSecondMessage)

  const secondSignature: MiMicSignature = eddsa.signMiMC(
    userPrvKey.toString(),
    userSecondMessageHash
  )

  // Insert signature into tx
  const userSecondMessage2 = [
    ...userSecondMessage,
    secondSignature.R8[0],
    secondSignature.R8[1],
    secondSignature.S
  ]

  const userSecondEncryptedMessage = encrypt(
    userSecondMessage2,
github barryWhiteHat / maci / app / utils / generate_circuit_input.js View on Github external
detail: 0,
  updated_pubkey: Alice.pubkey
}

// Alice sign tx
const txHash = mimc7.multiHash(
  [tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)

// update Alice account
const newAlice = {
  pubkey: tx.updated_pubkey,
  detail: BigInt(tx.detail)
}
const newAliceHash = mimc7.multiHash(
  [newAlice.pubkey[0], newAlice.pubkey[1], BigInt(newAlice.detail)]
)

// update root
const final_root = mimc7.multiHash([newAliceHash, bobHash])

// console.log('tree_root: ' + tree_root.toString())
// console.log('final_root: ' + final_root.toString())
// console.log('accounts_pubkeys Alice.pubkey[0]: ' + Alice.pubkey[0].toString())
// console.log('accounts_pubkeys Alice.pubkey[1]: ' + Alice.pubkey[1].toString())
// console.log('accounts_detail: ' + final_root.toString())
// console.log('sender_detail: ' + Alice.detail.toString())
// console.log('sender_updated_pubkey[0]: ' + newAlice.pubkey[0].toString())
// console.log('sender_updated_pubkey[1]: ' + newAlice.pubkey[1].toString())
// console.log('sender_updated_detail: ' + newAlice.detail.toString())
// console.log("signature['R8'][0]: " + signature.R8[0].toString())
github barryWhiteHat / maci / app / utils / generate_circuit_input.js View on Github external
)

const tree_root = mimc7.multiHash([aliceHash, bobHash])

// transaction
const tx = {
  from: Alice.pubkey,
  detail: 0,
  updated_pubkey: Alice.pubkey
}

// Alice sign tx
const txHash = mimc7.multiHash(
  [tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)

// update Alice account
const newAlice = {
  pubkey: tx.updated_pubkey,
  detail: BigInt(tx.detail)
}
const newAliceHash = mimc7.multiHash(
  [newAlice.pubkey[0], newAlice.pubkey[1], BigInt(newAlice.detail)]
)

// update root
const final_root = mimc7.multiHash([newAliceHash, bobHash])

// console.log('tree_root: ' + tree_root.toString())
// console.log('final_root: ' + final_root.toString())
// console.log('accounts_pubkeys Alice.pubkey[0]: ' + Alice.pubkey[0].toString())
github barryWhiteHat / maci / app / interact.js View on Github external
// Wait 10 seconds
  console.log('Sleeping 10 seconds...')
  await sleep(10000)
  console.log('Woken up')

  // Construct 2nd message
  const userSecondMessage = [
    ...userPosition,
    ...userPubKey,
    1n // New position
  ]

  const userSecondMessageHash = mimc7.multiHash(userSecondMessage)

  const secondSignature: MiMicSignature = eddsa.signMiMC(
    userPrvKey.toString(),
    userSecondMessageHash
  )

  // Insert signature into tx
  const userSecondMessage2 = [
    ...userSecondMessage,
    secondSignature.R8[0],
    secondSignature.R8[1],
    secondSignature.S
  ]

  const userSecondEncryptedMessage = encrypt(
    userSecondMessage2,
    userPrvKey,
    coordinatorPublicKey
github barryWhiteHat / maci / boilerplate / crypto / ts / index.ts View on Github external
const sign = (
    privKey: PrivKey,
    plaintext: Plaintext,
): Signature => {

    // TODO: make these intermediate variables have more meaningful names
    const h1 = bigInt2Buffer(mimcspongeHashOne(privKey))

    // TODO: document these steps
    const sBuff = eddsa.pruneBuffer(h1.slice(0, 32))
    const s = snarkjs.bigInt.leBuff2int(sBuff)
    const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3))

    debugger
    const msgBuff = snarkjs.bigInt.leInt2Buff(
        plaintext,
        32
    )

    const rBuff = bigInt2Buffer(
        mimcspongeHashOne(
            buffer2BigInt(Buffer.concat(
                [h1.slice(32, 64), msgBuff]
            ))
        )
    )

    let r = snarkjs.bigInt.leBuff2int(rBuff)
github barryWhiteHat / maci / boilerplate / crypto / ts / index.ts View on Github external
const genPubKey = (privKey: PrivKey): PubKey => {
    // Check whether privKey is a field element
    assert(privKey < SNARK_FIELD_SIZE)

    // TODO: check whether privKey is valid (i.e. that the prune buffer step
    // worked)

    const pubKey = babyJub.mulPointEscalar(
        babyJub.Base8,
        formatPrivKeyForBabyJub(privKey),
    )

    // TODO: assert that pubKey is valid
    // TODO: figure out how to check if pubKey is valid

    return pubKey
}
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])
  const S = r.add(hm.mul(s)).mod(babyJub.subOrder)
github iden3 / iden3js / src / crypto / mimc7.js View on Github external
function multiHash(arr) {
  // TODO check bigints inside finite field
  return mimc7.multiHash(arr);
}
github barryWhiteHat / maci / app / utils / generate_circuit_input.js View on Github external
const { Circuit } = require('snarkjs')
const zkSnark = require('snarkjs').original
const { unstringifyBigInts } = require('snarkjs/src/stringifybigint')

const alicePrvKey = Buffer.from('1'.toString().padStart(64, '0'), 'hex')
const alicePubKey = eddsa.prv2pub(alicePrvKey)
const bobPrvKey = Buffer.from('2'.toString().padStart(64, '0'), 'hex')
const bobPubKey = eddsa.prv2pub(bobPrvKey)

// accounts (1 = Yes, 0 = No)
const Alice = {
  pubkey: alicePubKey,
  detail: 1
}

const aliceHash = mimc7.multiHash(
  [Alice.pubkey[0], Alice.pubkey[1], BigInt(Alice.detail)]
)

const Bob = {
  pubkey: bobPubKey,
  detail: 0
}
const bobHash = mimc7.multiHash(
  [Bob.pubkey[0], Bob.pubkey[1], BigInt(Bob.detail)]
)

const tree_root = mimc7.multiHash([aliceHash, bobHash])

// transaction
const tx = {
  from: Alice.pubkey,