UNPKG

@beraji/wallet-sdk

Version:

Beraji: Distributed Secret Sharing.

126 lines 5.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.EdTSS = exports.EdCurve = void 0; const ed25519_1 = require("@noble/ed25519"); const ed25519_2 = require("@noble/curves/ed25519"); const sha512_1 = require("@noble/hashes/sha512"); const sha3_1 = require("@noble/hashes/sha3"); const bn_js_1 = __importDefault(require("bn.js")); const sss_1 = require("./sss"); const ff_1 = require("./ff"); const utils_1 = require("@noble/hashes/utils"); const utils_2 = require("./utils"); class EdCurve { } exports.EdCurve = EdCurve; _a = EdCurve; EdCurve.ff = ff_1.FiniteField.fromBigInt(ed25519_2.ed25519.CURVE.n, 'le'); EdCurve.ZERO = ed25519_1.Point.ZERO.toRawBytes(); EdCurve.validate = (point) => { try { ed25519_1.Point.fromHex(point); return true; } catch (er) { return false; } }; EdCurve.baseMul = (r) => { if (_a.ff.ZERO.eq(_a.ff.encode(r))) return ed25519_1.Point.ZERO.toRawBytes(); const b = BigInt(new bn_js_1.default(r, 16, _a.ff.en).toString()); return ed25519_1.Point.BASE.multiply(b).toRawBytes(); }; EdCurve.negPoint = (point) => { const a = ed25519_1.Point.fromHex(point); return a.negate().toRawBytes(); }; EdCurve.addPoint = (pointA, pointB) => { if ((0, utils_2.equal)([pointA, ed25519_1.Point.ZERO.toRawBytes()])) return pointB; if ((0, utils_2.equal)([pointB, ed25519_1.Point.ZERO.toRawBytes()])) return pointA; const a = ed25519_1.Point.fromHex(pointA); const b = ed25519_1.Point.fromHex(pointB); return a.add(b).toRawBytes(); }; EdCurve.mulScalar = (point, scalar) => { if ((0, utils_2.equal)([point, ed25519_1.Point.ZERO.toRawBytes()]) || _a.ff.ZERO.eq(_a.ff.encode(scalar))) return ed25519_1.Point.ZERO.toRawBytes(); const p = ed25519_1.Point.fromHex(point); const s = BigInt(new bn_js_1.default(scalar, 16, _a.ff.en).toString()); return p.multiply(s).toRawBytes(); }; EdCurve.getDerivedKey = (privateKey) => { const derivedKey = (0, sha512_1.sha512)(privateKey.subarray(0, 32)).subarray(0, 32); derivedKey[0] &= 248; derivedKey[31] &= 127; derivedKey[31] |= 64; return _a.ff.norm(derivedKey); }; EdCurve.getPublicKey = (privateKey, derived = false) => { if (!derived) privateKey = _a.getDerivedKey(privateKey); const pubkey = _a.baseMul(privateKey); return pubkey; }; class EdTSS { } exports.EdTSS = EdTSS; _b = EdTSS; EdTSS.ff = ff_1.FiniteField.fromBigInt(ed25519_2.ed25519.CURVE.n, 'le'); EdTSS.signatureLength = 64; EdTSS.privateKeyLength = 32; EdTSS.publicKeyLength = 32; EdTSS.randomnessLength = 32; EdTSS.shareRandomness = (t, n, indice, seed) => { const r = _b.ff.norm(!seed ? (0, utils_1.randomBytes)(_b.randomnessLength) : (0, sha3_1.keccak_256)(seed)); const secretSharing = new sss_1.SecretSharing(_b.ff); const { shares, zkp } = secretSharing.share(r, t, n, { indice, ec: EdCurve, }); const R = EdCurve.baseMul(r); return { shares, R, r, zkp }; }; EdTSS.addSig = (sigs) => { const rs = sigs.map((sig) => sig.subarray(0, 32)); const ss = sigs.map((sig) => sig.subarray(32)); const R = rs.reduce((sum, r) => EdCurve.addPoint(sum, r), ed25519_1.Point.ZERO.toRawBytes()); const S = ss.reduce((sum, s) => _b.ff.add(sum, s), _b.ff.decode(new bn_js_1.default(0))); return (0, utils_1.concatBytes)(R, S); }; EdTSS.sign = (msg, R, publicKey, r, derivedKey) => { if (r.length !== _b.randomnessLength) throw new Error('bad randomness size'); if (derivedKey.length !== _b.privateKeyLength) throw new Error('bad private key size'); if (publicKey.length !== _b.publicKeyLength) throw new Error('bad public key size'); const h = (0, sha512_1.sha512)((0, utils_1.concatBytes)(R, publicKey, msg)); const s = _b.ff.add(_b.ff.mul(h, derivedKey), r); const rG = EdCurve.baseMul(r); return (0, utils_1.concatBytes)(rG, s); }; EdTSS.verify = (msg, R, publicKey, index, sig, pzkp, rzkp) => { if (publicKey.length !== _b.publicKeyLength) throw new Error('bad public key size'); if (pzkp.length !== rzkp.length) throw new Error('bad proofs size'); const x = _b.ff.decode(new bn_js_1.default(index, 8, _b.ff.en)); const h = (0, sha512_1.sha512)((0, utils_1.concatBytes)(R, publicKey, msg)); const rG = sig.subarray(0, _b.publicKeyLength); const s = sig.subarray(_b.publicKeyLength, _b.signatureLength); const _rG = rzkp.reduce((sum, co, i) => EdCurve.addPoint(sum, EdCurve.mulScalar(co, _b.ff.pow(x, i))), EdCurve.ZERO); if (!(0, utils_2.equal)([_rG, rG])) return false; const sG = EdCurve.baseMul(s); const _sG = EdCurve.addPoint(rG, EdCurve.mulScalar(pzkp.reduce((sum, co, i) => EdCurve.addPoint(sum, EdCurve.mulScalar(co, _b.ff.pow(x, i))), EdCurve.ZERO), _b.ff.norm(h))); return (0, utils_2.equal)([_sG, sG]); }; //# sourceMappingURL=edtss.js.map