UNPKG

navio-blsct

Version:

TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.

183 lines (182 loc) 7.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RangeProof = void 0; const blsct_1 = require("./blsct"); const amountRecoveryRes_1 = require("./amountRecoveryRes"); const managedObj_1 = require("./managedObj"); const point_1 = require("./point"); const scalar_1 = require("./scalar"); const tokenId_1 = require("./tokenId"); /** Represents a (possibly aggregated) range proof for one or more confidential transaction amounts. * * Examples: * ```ts * const { RangeProof, AmountRecoveryReq, AmountRecoveryRes, Point, TokenId } = require('navio-blsct') * const nonce = Point.random() * const tokenId = TokenId.default() * const rp = RangeProof.generate([456], nonce, 'navio', tokenId) * RangeProof.verifyProofs([rp]) // true * const req = new AmountRecoveryReq(rp, nonce) * const res = RangeProof.recoverAmounts([req1]) * res[0].isSucc // true * res[0].amount // 456 * res[0].message // 'navio' * rp.get_A() // Point object representing A * rp.get_A_wip() // Point object representing A_wip * rp.get_B() // Point object representing B * rp.get_r_prime() // Scalar object representing r' * rp.get_s_prime() // Scalar object representing s' * rp.get_delta_prime() // Scalar object representing delta' * rp.get_alpha_hat() // Scalar object representing alpha_hat * rp.get_tau_x() // Scalar object representing tau_x * const ser = rp.serialize() * const deser = RangeProof.deserialize(ser) * ser === deser.serialize() // true * ``` */ class RangeProof extends managedObj_1.ManagedObj { constructor(obj) { super(obj); } /** Generates a range proof for the given amounts, nonce, and message. * @param amounts - An array of amounts to be included in the range proof. * @param nonce - A nonce used to generate the range proof. * @param msg - A message associated with the range proof. * @param tokenId - An optional token ID. If not provided, a default token ID is used. * @returns A new `RangeProof` instance containing the generated range proof. */ static generate(amounts, nonce, msg, tokenId) { tokenId = tokenId ?? tokenId_1.TokenId.default(); const vec = (0, blsct_1.createUint64Vec)(); for (const amount of amounts) { (0, blsct_1.addToUint64Vec)(vec, amount); } const rv = (0, blsct_1.buildRangeProof)(vec, nonce.value(), msg, tokenId.value()); (0, blsct_1.deleteUint64Vec)(vec); if (rv.result !== 0) { const msg = `Building range proof failed. Error code = ${rv.result}`; (0, blsct_1.freeObj)(rv); throw new Error(msg); } const x = RangeProof.fromObjAndSize(rv.value, rv.value_size); (0, blsct_1.freeObj)(rv); return x; } /** Verifies a list of range proofs. * @param proofs - An array of `RangeProof` instances to be verified. * @returns `true` if all proofs are valid, `false` otherwise. */ static verifyProofs(proofs) { const vec = (0, blsct_1.createRangeProofVec)(); for (const proof of proofs) { (0, blsct_1.addToRangeProofVec)(vec, proof.value(), proof.size()); } const rv = (0, blsct_1.verifyRangeProofs)(vec); if (rv.result !== 0) { const msg = `Verifying range proofs failed. Error code = ${rv.result}`; (0, blsct_1.freeObj)(rv); throw new Error(msg); } (0, blsct_1.deleteRangeProofVec)(vec); return rv.value; } /** Recovers amounts from a list of `AmountRecoveryReq` instances. * @param reqs - An array of `AmountRecoveryReq` instances containing range proofs and nonces. * @returns An array of `AmountRecoveryRes` instances containing the recovery results. */ recoverAmounts(reqs) { const reqVec = (0, blsct_1.createAmountRecoveryReqVec)(); for (const req of reqs) { const blsctReq = (0, blsct_1.genAmountRecoveryReq)(req.rangeProof.value(), req.rangeProof.size(), req.nonce.value()); (0, blsct_1.addToAmountRecoveryReqVec)(reqVec, blsctReq); } const rv = (0, blsct_1.recoverAmount)(reqVec); (0, blsct_1.deleteAmountRecoveryReqVec)(reqVec); if (rv.result !== 0) { const msg = `Recovering amount failed. Error code = ${rv.result}`; (0, blsct_1.deleteAmountsRetVal)(rv); throw new Error(msg); } let results = []; const size = (0, blsct_1.getAmountRecoveryResultSize)(rv.value); for (let i = 0; i < size; ++i) { const isSucc = (0, blsct_1.getAmountRecoveryResultIsSucc)(rv.value, i); const amount = (0, blsct_1.getAmountRecoveryResultAmount)(rv.value, i); const msg = (0, blsct_1.getAmountRecoveryResultMsg)(rv.value, i); const x = new amountRecoveryRes_1.AmountRecoveryRes(isSucc, amount, msg); results.push(x); } (0, blsct_1.deleteAmountsRetVal)(rv); return results; } value() { return (0, blsct_1.castToRangeProof)(this.obj); } serialize() { return (0, blsct_1.serializeRangeProof)(this.value(), this.size()); } /** Deserializes a hexadecimal string into a `RangeProof` instance. * @param hex - A hexadecimal string representing the serialized range proof. * @returns A new `RangeProof` instance containing the deserialized data. */ static deserialize(hex) { return RangeProof._deserialize(hex, blsct_1.deserializeRangeProof); } /** Returns the A point of the range proof. * @returns A `Point` object representing the A point. */ get_A() { const obj = (0, blsct_1.getRangeProof_A)(this.value(), this.size()); return point_1.Point.fromObj(obj); } /** Returns the A_wip point of the range proof. * @returns A `Point` object representing the A_wip point. */ get_A_wip() { const obj = (0, blsct_1.getRangeProof_A_wip)(this.value(), this.size()); return point_1.Point.fromObj(obj); } /** Returns the B point of the range proof. * @returns A `Point` object representing the B point. */ get_B() { const obj = (0, blsct_1.getRangeProof_B)(this.value(), this.size()); return point_1.Point.fromObj(obj); } /** Returns the r' scalar of the range proof. * @returns A `Scalar` object representing the r' scalar. */ get_r_prime() { const obj = (0, blsct_1.getRangeProof_r_prime)(this.value(), this.size()); return scalar_1.Scalar.fromObj(obj); } /** Returns the s' scalar of the range proof. * @returns A `Scalar` object representing the s' scalar. */ get_s_prime() { const obj = (0, blsct_1.getRangeProof_s_prime)(this.value(), this.size()); return scalar_1.Scalar.fromObj(obj); } /** Returns the tau_x scalar of the range proof. * @returns A `Scalar` object representing the tau_x scalar. */ get_delta_prime() { const obj = (0, blsct_1.getRangeProof_delta_prime)(this.value(), this.size()); return scalar_1.Scalar.fromObj(obj); } /** Returns the alpha_hat scalar of the range proof. * @returns A `Scalar` object representing the alpha_hat scalar. */ get_alpha_hat() { const obj = (0, blsct_1.getRangeProof_alpha_hat)(this.value(), this.size()); return scalar_1.Scalar.fromObj(obj); } /** Returns the tau_x scalar of the range proof. * @returns A `Scalar` object representing the t_aux scalar. */ get_tau_x() { const obj = (0, blsct_1.getRangeProof_tau_x)(this.value(), this.size()); return scalar_1.Scalar.fromObj(obj); } } exports.RangeProof = RangeProof;