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 = (
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)
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
}
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)
const ecdh = (priv: BigInt, pub: Tuple): BigInt => {
const s = babyJubJubPrivateKey(priv)
return babyJub.mulPointEscalar(
pub,
s
)[0]
}
const privateToPublicKey = (sk: BigInt): [BigInt, BigInt] => {
const s = babyJubJubPrivateKey(sk)
return babyJub.mulPointEscalar(
babyJub.Base8,
s
)
}
const genEcdhSharedKey = (
privKey: PrivKey,
pubKey: PubKey,
): EcdhSharedKey => {
return babyJub.mulPointEscalar(pubKey, formatPrivKeyForBabyJub(privKey))[0]
}
function privToPub(privKey: Buffer, compress: boolean): Buffer {
if (privKey.length !== 32) {
throw new Error(`Input Error: Buffer has ${privKey.length} bytes. It should be 32 bytes`);
}
const scalar = privToScalar(privKey);
const pubKey = babyJub.mulPointEscalar(baseBabyJub, scalar);
const pubKeyX = utils.bigIntToBufferBE(pubKey[0]);
const pubKeyY = utils.bigIntToBufferBE(pubKey[1]);
if (!babyJub.inSubgroup(pubKey)) {
throw new Error('Point generated not in babyjub subgroup');
}
if (!compress) {
return Buffer.concat([pubKeyX, pubKeyY]);
}
return compressPoint(pubKeyX, pubKeyY);
}