UNPKG

@bitgo/bls

Version:

Implementation of bls signature verification for ethereum 2.0

85 lines (61 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Signature = void 0; var _assert = _interopRequireDefault(require("assert")); var _constants = require("./constants"); var _context = require("./context"); var _utils = require("./helpers/utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class Signature { constructor(value) { _defineProperty(this, "value", void 0); this.value = value; (0, _assert.default)(this.value.isValidOrder()); } static fromCompressedBytes(value) { (0, _assert.default)(value.length === 2 * _constants.FP_POINT_LENGTH, "Signature must have ".concat(2 * _constants.FP_POINT_LENGTH, " bytes")); const context = (0, _context.getContext)(); const signature = new context.Signature(); if (!_utils.EMPTY_SIGNATURE.equals(value)) { signature.deserialize(value); } return new Signature(signature); } static fromValue(signature) { return new Signature(signature); } static aggregate(signatures) { const context = (0, _context.getContext)(); const signature = new context.Signature(); signature.aggregate(signatures.map(sig => sig.getValue())); return new Signature(signature); } add(other) { const agg = this.value.clone(); agg.add(other.value); return new Signature(agg); } getValue() { return this.value; } verifyAggregate(publicKeys, message) { return this.value.fastAggregateVerify(publicKeys.map(key => key.getValue()), message); } verifyMultiple(publicKeys, messages, fast = false) { const msgs = Buffer.concat(messages); if (!fast && !(0, _context.getContext)().areAllMsgDifferent(msgs)) { return false; } return this.value.aggregateVerifyNoCheck(publicKeys.map(key => key.getValue()), msgs); } toBytesCompressed() { return Buffer.from(this.value.serialize()); } toHex() { return "0x" + this.value.serializeToHexStr(); } } exports.Signature = Signature;