bls.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. import { AffinePoint } from './curve.js';
  3. import { IField } from './modular.js';
  4. import { Hex, PrivKey, CHash } from './utils.js';
  5. import { MapToCurve, Opts as HTFOpts, htfBasicOpts, createHasher } from './hash-to-curve.js';
  6. import { CurvePointsType, ProjPointType as ProjPointType, CurvePointsRes } from './weierstrass.js';
  7. /**
  8. * BLS (Barreto-Lynn-Scott) family of pairing-friendly curves.
  9. * Implements BLS (Boneh-Lynn-Shacham) signatures.
  10. * Consists of two curves: G1 and G2:
  11. * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
  12. * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
  13. * - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
  14. * Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
  15. * Pairing is used to aggregate and verify signatures.
  16. * We are using Fp for private keys (shorter) and Fp₂ for signatures (longer).
  17. * Some projects may prefer to swap this relation, it is not supported for now.
  18. **/
  19. type Fp = bigint;
  20. export type ShortSignatureCoder<Fp> = {
  21. fromHex(hex: Hex): ProjPointType<Fp>;
  22. toRawBytes(point: ProjPointType<Fp>): Uint8Array;
  23. toHex(point: ProjPointType<Fp>): string;
  24. };
  25. export type SignatureCoder<Fp2> = {
  26. fromHex(hex: Hex): ProjPointType<Fp2>;
  27. toRawBytes(point: ProjPointType<Fp2>): Uint8Array;
  28. toHex(point: ProjPointType<Fp2>): string;
  29. };
  30. type Fp2Bls<Fp, Fp2> = IField<Fp2> & {
  31. reim: (num: Fp2) => {
  32. re: Fp;
  33. im: Fp;
  34. };
  35. multiplyByB: (num: Fp2) => Fp2;
  36. frobeniusMap(num: Fp2, power: number): Fp2;
  37. };
  38. type Fp12Bls<Fp2, Fp12> = IField<Fp12> & {
  39. frobeniusMap(num: Fp12, power: number): Fp12;
  40. multiplyBy014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
  41. conjugate(num: Fp12): Fp12;
  42. finalExponentiate(num: Fp12): Fp12;
  43. };
  44. export type CurveType<Fp, Fp2, Fp6, Fp12> = {
  45. G1: Omit<CurvePointsType<Fp>, 'n'> & {
  46. ShortSignature: SignatureCoder<Fp>;
  47. mapToCurve: MapToCurve<Fp>;
  48. htfDefaults: HTFOpts;
  49. };
  50. G2: Omit<CurvePointsType<Fp2>, 'n'> & {
  51. Signature: SignatureCoder<Fp2>;
  52. mapToCurve: MapToCurve<Fp2>;
  53. htfDefaults: HTFOpts;
  54. };
  55. fields: {
  56. Fp: IField<Fp>;
  57. Fr: IField<bigint>;
  58. Fp2: Fp2Bls<Fp, Fp2>;
  59. Fp6: IField<Fp6>;
  60. Fp12: Fp12Bls<Fp2, Fp12>;
  61. };
  62. params: {
  63. x: bigint;
  64. r: bigint;
  65. };
  66. htfDefaults: HTFOpts;
  67. hash: CHash;
  68. randomBytes: (bytesLength?: number) => Uint8Array;
  69. };
  70. export type CurveFn<Fp, Fp2, Fp6, Fp12> = {
  71. getPublicKey: (privateKey: PrivKey) => Uint8Array;
  72. getPublicKeyForShortSignatures: (privateKey: PrivKey) => Uint8Array;
  73. sign: {
  74. (message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
  75. (message: ProjPointType<Fp2>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp2>;
  76. };
  77. signShortSignature: {
  78. (message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
  79. (message: ProjPointType<Fp>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp>;
  80. };
  81. verify: (signature: Hex | ProjPointType<Fp2>, message: Hex | ProjPointType<Fp2>, publicKey: Hex | ProjPointType<Fp>, htfOpts?: htfBasicOpts) => boolean;
  82. verifyShortSignature: (signature: Hex | ProjPointType<Fp>, message: Hex | ProjPointType<Fp>, publicKey: Hex | ProjPointType<Fp2>, htfOpts?: htfBasicOpts) => boolean;
  83. verifyBatch: (signature: Hex | ProjPointType<Fp2>, messages: (Hex | ProjPointType<Fp2>)[], publicKeys: (Hex | ProjPointType<Fp>)[], htfOpts?: htfBasicOpts) => boolean;
  84. aggregatePublicKeys: {
  85. (publicKeys: Hex[]): Uint8Array;
  86. (publicKeys: ProjPointType<Fp>[]): ProjPointType<Fp>;
  87. };
  88. aggregateSignatures: {
  89. (signatures: Hex[]): Uint8Array;
  90. (signatures: ProjPointType<Fp2>[]): ProjPointType<Fp2>;
  91. };
  92. aggregateShortSignatures: {
  93. (signatures: Hex[]): Uint8Array;
  94. (signatures: ProjPointType<Fp>[]): ProjPointType<Fp>;
  95. };
  96. millerLoop: (ell: [Fp2, Fp2, Fp2][], g1: [Fp, Fp]) => Fp12;
  97. pairing: (P: ProjPointType<Fp>, Q: ProjPointType<Fp2>, withFinalExponent?: boolean) => Fp12;
  98. G1: CurvePointsRes<Fp> & ReturnType<typeof createHasher<Fp>>;
  99. G2: CurvePointsRes<Fp2> & ReturnType<typeof createHasher<Fp2>>;
  100. Signature: SignatureCoder<Fp2>;
  101. ShortSignature: ShortSignatureCoder<Fp>;
  102. params: {
  103. x: bigint;
  104. r: bigint;
  105. G1b: bigint;
  106. G2b: Fp2;
  107. };
  108. fields: {
  109. Fp: IField<Fp>;
  110. Fp2: Fp2Bls<Fp, Fp2>;
  111. Fp6: IField<Fp6>;
  112. Fp12: Fp12Bls<Fp2, Fp12>;
  113. Fr: IField<bigint>;
  114. };
  115. utils: {
  116. randomPrivateKey: () => Uint8Array;
  117. calcPairingPrecomputes: (p: AffinePoint<Fp2>) => [Fp2, Fp2, Fp2][];
  118. };
  119. };
  120. export declare function bls<Fp2, Fp6, Fp12>(CURVE: CurveType<Fp, Fp2, Fp6, Fp12>): CurveFn<Fp, Fp2, Fp6, Fp12>;
  121. export {};
  122. //# sourceMappingURL=bls.d.ts.map