@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
38 lines • 1.31 kB
JavaScript
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
import { bls12_381 } from "@noble/curves/bls12-381.js";
export const G1_SIZE = 48;
export const G2_SIZE = 96;
/** Evaluates the short Weierstrass curve equation `x³ + a·x + b` at `x` over the field of `p`. */
export function weierstrassEquation(x, p) {
const x2 = p.Fp.sqr(x);
const x3 = p.Fp.mul(x2, x);
return p.Fp.add(p.Fp.add(x3, p.Fp.mul(x, p.CURVE().a)), p.CURVE().b);
}
export function g1ToBytes(p) {
return p.toBytes(true);
}
/**
* Compressed-G1 deserializer with prime-order subgroup check, matching aptos-core `ts-batch-encrypt`.
*/
export function bytesToG1(bytes) {
const p = bls12_381.G1.Point.fromBytes(bytes);
if (!p.isTorsionFree()) {
throw new Error("Tried to deserialize invalid group element: not torsion free");
}
return p;
}
export function g2ToBytes(p) {
return p.toBytes(true);
}
/**
* Compressed-G2 deserializer with prime-order subgroup check, matching aptos-core `ts-batch-encrypt`.
*/
export function bytesToG2(bytes) {
const p = bls12_381.G2.Point.fromBytes(bytes);
if (!p.isTorsionFree()) {
throw new Error("Tried to deserialize invalid group element: not torsion free");
}
return p;
}
//# sourceMappingURL=curveSerialization.js.map