Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
}
const formatPrivKeyForBabyJub = (privKey: PrivKey) => {
// TODO: clarify this explanation
// 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
const sBuff = eddsa.pruneBuffer(
bigInt2Buffer(
mimcspongeHashOne(privKey)
).slice(0, 32)
)
return snarkjs.bigInt.leBuff2int(sBuff).shr(3)
}
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;
}
toPrivScalar(): bigInt {
const h1 = createBlakeHash('blake512').update(this.sk).digest();
const sBuff = eddsa.pruneBuffer(h1.slice(0, 32));
return (bigInt.leBuff2int(sBuff)).shr(3);
}