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.
100 lines (99 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Point = void 0;
const blsct_1 = require("./blsct");
const managedObj_1 = require("./managedObj");
/** Represents an element in the BLS12-381 G1 curve group.
* A wrapper of [MclG1Point](https://github.com/nav-io/navio-core/blob/master/src/blsct/arith/mcl/mcl_g1point.h)in navio-core.
*
* Instantiating a Point object without a parameter returns a random point.
* Examples:
* ```ts
* const { Point, Scalar } = require('navio-blsct')
* const p1 = new Point() // random point
* const p2 = Point.random()
* const s = new Scalar()
* const p3 = Point.fromScalar(s)
* p3.isValid() // true
* const p4 = Point.base()
* const p5 = Point.base()
* p4.equals(p5) // true
* const p6 = Point.deserialize(p4.serialize())
* p6.equals(p4) // true
* const ser = p1.serialize()
* const deser = Point.deserialize(ser)
* ser === deser.serialize() // true
* ```
*/
class Point extends managedObj_1.ManagedObj {
/** Constructs a new random `Point` instance.
*/
constructor(obj) {
if (typeof obj === 'object') {
super(obj);
}
else
(super((0, blsct_1.genRandomPoint)().value));
}
value() {
return (0, blsct_1.castToPoint)(this.obj);
}
/** Returns a random point.
* * @returns A random point on the BLS12-381 G1 curve.
*/
static random() {
const rv = (0, blsct_1.genRandomPoint)();
const x = Point.fromObj(rv.value);
(0, blsct_1.freeObj)(rv);
return x;
}
/** Returns the base point of the BLS12-381 G1 curve.
* @returns The base point of the BLS12-381 G1 curve.
*/
static base() {
const rv = (0, blsct_1.genBasePoint)();
const x = Point.fromObj(rv.value);
(0, blsct_1.freeObj)(rv);
return x;
}
/**
* Computes the product of the BLS12-381 G1 base point and the given scalar.
*
* @param scalar - The scalar by which to multiply the base point.
* @returns The point on the BLS12-381 G1 curve obtained by multiplying the base point by the scalar.
*/
static fromScalar(scalar) {
const obj = (0, blsct_1.pointFromScalar)(scalar.value());
return Point.fromObj(obj);
}
/** Checks if the point is valid.
* @returns `true` if the point is valid, `false` otherwise.
*/
isValid() {
return (0, blsct_1.isValidPoint)(this.value());
}
toString() {
const s = (0, blsct_1.pointToStr)(this.value());
return `Point(${s})`;
}
/** Returns if the point is equal to the provided point.
* @param other - The point to compare with.
* @returns `true` if the points are equal, `false` otherwise.
*/
equals(other) {
return (0, blsct_1.arePointEqual)(this.value(), other.value());
}
serialize() {
return (0, blsct_1.serializePoint)(this.value());
}
/**
* Deserializes a hexadecimal string into a Point instance.
*
* @param hex - The hexadecimal string to convert.
* @returns The `Point` instance represented by the input string.
*/
static deserialize(hex) {
return Point._deserialize(hex, blsct_1.deserializePoint);
}
}
exports.Point = Point;