|
| 1 | +// Copyright (c) Mysten Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { toHex } from '@mysten/bcs'; |
| 5 | +import type { Fp2, Fp12 } from '@noble/curves/abstract/tower'; |
| 6 | +import type { ProjPointType } from '@noble/curves/abstract/weierstrass'; |
| 7 | +import { bls12_381 } from '@noble/curves/bls12-381'; |
| 8 | + |
| 9 | +export class G1Element { |
| 10 | + point: ProjPointType<bigint>; |
| 11 | + |
| 12 | + constructor(point: ProjPointType<bigint>) { |
| 13 | + this.point = point; |
| 14 | + } |
| 15 | + |
| 16 | + static generator(): G1Element { |
| 17 | + return new G1Element(bls12_381.G1.ProjectivePoint.BASE); |
| 18 | + } |
| 19 | + |
| 20 | + static fromBytes(bytes: Uint8Array): G1Element { |
| 21 | + return new G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes))); |
| 22 | + } |
| 23 | + |
| 24 | + toBytes(): Uint8Array { |
| 25 | + return this.point.toRawBytes(); |
| 26 | + } |
| 27 | + |
| 28 | + multiply(scalar: Scalar): G1Element { |
| 29 | + return new G1Element(this.point.multiply(scalar.scalar)); |
| 30 | + } |
| 31 | + |
| 32 | + add(other: G1Element): G1Element { |
| 33 | + return new G1Element(this.point.add(other.point)); |
| 34 | + } |
| 35 | + |
| 36 | + subtract(other: G1Element): G1Element { |
| 37 | + return new G1Element(this.point.subtract(other.point)); |
| 38 | + } |
| 39 | + |
| 40 | + static hashToCurve(data: Uint8Array): G1Element { |
| 41 | + return new G1Element( |
| 42 | + bls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + pairing(other: G2Element): GTElement { |
| 47 | + return new GTElement(bls12_381.pairing(this.point, other.point)); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export class G2Element { |
| 52 | + point: ProjPointType<Fp2>; |
| 53 | + |
| 54 | + constructor(point: ProjPointType<Fp2>) { |
| 55 | + this.point = point; |
| 56 | + } |
| 57 | + |
| 58 | + static generator(): G2Element { |
| 59 | + return new G2Element(bls12_381.G2.ProjectivePoint.BASE); |
| 60 | + } |
| 61 | + |
| 62 | + static fromBytes(bytes: Uint8Array): G2Element { |
| 63 | + return new G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes))); |
| 64 | + } |
| 65 | + |
| 66 | + toBytes(): Uint8Array { |
| 67 | + return this.point.toRawBytes(); |
| 68 | + } |
| 69 | + |
| 70 | + multiply(scalar: Scalar): G2Element { |
| 71 | + return new G2Element(this.point.multiply(scalar.scalar)); |
| 72 | + } |
| 73 | + |
| 74 | + add(other: G2Element): G2Element { |
| 75 | + return new G2Element(this.point.add(other.point)); |
| 76 | + } |
| 77 | + |
| 78 | + hashToCurve(data: Uint8Array): G2Element { |
| 79 | + return new G2Element( |
| 80 | + bls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()), |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export class GTElement { |
| 86 | + element: Fp12; |
| 87 | + |
| 88 | + constructor(element: Fp12) { |
| 89 | + this.element = element; |
| 90 | + } |
| 91 | + |
| 92 | + toBytes(): Uint8Array { |
| 93 | + return bls12_381.fields.Fp12.toBytes(this.element); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export class Scalar { |
| 98 | + scalar: bigint; |
| 99 | + |
| 100 | + constructor(scalar: bigint) { |
| 101 | + this.scalar = scalar; |
| 102 | + } |
| 103 | + |
| 104 | + static random(): Scalar { |
| 105 | + return Scalar.fromBytes(bls12_381.utils.randomPrivateKey()); |
| 106 | + } |
| 107 | + |
| 108 | + toBytes(): Uint8Array { |
| 109 | + return new Uint8Array(bls12_381.fields.Fr.toBytes(this.scalar)); |
| 110 | + } |
| 111 | + |
| 112 | + static fromBytes(bytes: Uint8Array): Scalar { |
| 113 | + return new Scalar(bls12_381.fields.Fr.fromBytes(bytes)); |
| 114 | + } |
| 115 | + |
| 116 | + static fromNumber(num: number): Scalar { |
| 117 | + return new Scalar(BigInt(num)); |
| 118 | + } |
| 119 | +} |
0 commit comments