Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function verifyMultiple (pubkeys, messageHashes, signature, domain) {
assert.equal(pubkeys.length, messageHashes.length,
'number of pubkeys must equal number of message hashes')
const pubkeyG1s = pubkeys.map((pub) => mclPubkey(pub))
const signatureG2 = mclSignature(signature)
const ePH = Array.from({length: pubkeys.length}, (_, i) => i)
// create a pairing for each pubkey, messageHash pair
.map((i) => toG2AndPairing(pubkeyG1s[i], messageHashes[i], domain))
// accumulate into a single mcl.GT
.reduce((acc, val) => mcl.mul(acc, val))
return ePH.isEqual(mcl.pairing(g1(), signatureG2))
}
async function init () {
await mcl.init(mcl.BLS12_381)
}
function hashToG2 (messageHash, domain) {
assert.equal(messageHash.length, 32, 'messageHash must be 32 bytes long')
assert.equal(domain.length, 8, 'domain must be 8 bytes long')
const xReal = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([1]),
]))
const xRealFp = new mcl.Fp()
xRealFp.setLittleEndian(xReal)
const xImag = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([2]),
]))
const xImagFp = new mcl.Fp()
xImagFp.setLittleEndian(xImag)
const xCoordinate = new mcl.Fp2()
xCoordinate.set_a(xRealFp)
xCoordinate.set_b(xImagFp)
return xCoordinate.mapToG2()
}
function hashToG2 (messageHash, domain) {
assert.equal(messageHash.length, 32, 'messageHash must be 32 bytes long')
assert.equal(domain.length, 8, 'domain must be 8 bytes long')
const xReal = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([1]),
]))
const xRealFp = new mcl.Fp()
xRealFp.setLittleEndian(xReal)
const xImag = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([2]),
]))
const xImagFp = new mcl.Fp()
xImagFp.setLittleEndian(xImag)
const xCoordinate = new mcl.Fp2()
xCoordinate.set_a(xRealFp)
xCoordinate.set_b(xImagFp)
return xCoordinate.mapToG2()
}
assert.equal(domain.length, 8, 'domain must be 8 bytes long')
const xReal = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([1]),
]))
const xRealFp = new mcl.Fp()
xRealFp.setLittleEndian(xReal)
const xImag = keccak256(Buffer.concat([
messageHash,
domain,
Buffer.from([2]),
]))
const xImagFp = new mcl.Fp()
xImagFp.setLittleEndian(xImag)
const xCoordinate = new mcl.Fp2()
xCoordinate.set_a(xRealFp)
xCoordinate.set_b(xImagFp)
return xCoordinate.mapToG2()
}
function toG2AndPairing (pubkey, messageHash, domain) {
return mcl.pairing(pubkey, hashToG2(messageHash, domain))
}
function verify (pubkey, messageHash, signature, domain) {
const pubkeyG1 = mclPubkey(pubkey)
const signatureG2 = mclSignature(signature)
return toG2AndPairing(pubkeyG1, messageHash, domain).isEqual(mcl.pairing(g1(), signatureG2))
}
function g1() {
const g = new mcl.G1()
g.setStr('1 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569')
return g
}
.reduce((acc, val) => mcl.mul(acc, val))
return ePH.isEqual(mcl.pairing(g1(), signatureG2))
function genPublic (secretKey) {
const s = mclSecretKey(secretKey)
const q = g1()
const key = toBuffer(mcl.mul(q, s));
key[0] |= 0xa0;
return key
}