Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]
))
))
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
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
}
}
hash (values: Any | Array): BigInt {
if (Array.isArray(values)) {
return BigInt(multiHash(values.map((x: Any): BigInt => bigInt(x))))
}
return bigInt(multiHash([BigInt(values)]))
}
const hashLeftRight = (left: BigInt, right: BigInt): BigInt => {
return bigInt(multiHash([bigInt(left), bigInt(right)]))
}
return ret.map((x: Any): BigInt => bigInt(x))
}
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
}
},
return BigInt(multiHash(values.map((x: Any): BigInt => bigInt(x))))
}