modular.d.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. export declare function mod(a: bigint, b: bigint): bigint;
  2. /**
  3. * Efficiently raise num to power and do modular division.
  4. * Unsafe in some contexts: uses ladder, so can expose bigint bits.
  5. * @example
  6. * pow(2n, 6n, 11n) // 64n % 11n == 9n
  7. */
  8. export declare function pow(num: bigint, power: bigint, modulo: bigint): bigint;
  9. export declare function pow2(x: bigint, power: bigint, modulo: bigint): bigint;
  10. export declare function invert(number: bigint, modulo: bigint): bigint;
  11. /**
  12. * Tonelli-Shanks square root search algorithm.
  13. * 1. https://eprint.iacr.org/2012/685.pdf (page 12)
  14. * 2. Square Roots from 1; 24, 51, 10 to Dan Shanks
  15. * Will start an infinite loop if field order P is not prime.
  16. * @param P field order
  17. * @returns function that takes field Fp (created from P) and number n
  18. */
  19. export declare function tonelliShanks(P: bigint): <T>(Fp: IField<T>, n: T) => T;
  20. export declare function FpSqrt(P: bigint): <T>(Fp: IField<T>, n: T) => T;
  21. export declare const isNegativeLE: (num: bigint, modulo: bigint) => boolean;
  22. export interface IField<T> {
  23. ORDER: bigint;
  24. BYTES: number;
  25. BITS: number;
  26. MASK: bigint;
  27. ZERO: T;
  28. ONE: T;
  29. create: (num: T) => T;
  30. isValid: (num: T) => boolean;
  31. is0: (num: T) => boolean;
  32. neg(num: T): T;
  33. inv(num: T): T;
  34. sqrt(num: T): T;
  35. sqr(num: T): T;
  36. eql(lhs: T, rhs: T): boolean;
  37. add(lhs: T, rhs: T): T;
  38. sub(lhs: T, rhs: T): T;
  39. mul(lhs: T, rhs: T | bigint): T;
  40. pow(lhs: T, power: bigint): T;
  41. div(lhs: T, rhs: T | bigint): T;
  42. addN(lhs: T, rhs: T): T;
  43. subN(lhs: T, rhs: T): T;
  44. mulN(lhs: T, rhs: T | bigint): T;
  45. sqrN(num: T): T;
  46. isOdd?(num: T): boolean;
  47. pow(lhs: T, power: bigint): T;
  48. invertBatch: (lst: T[]) => T[];
  49. toBytes(num: T): Uint8Array;
  50. fromBytes(bytes: Uint8Array): T;
  51. cmov(a: T, b: T, c: boolean): T;
  52. }
  53. export declare function validateField<T>(field: IField<T>): IField<T>;
  54. /**
  55. * Same as `pow` but for Fp: non-constant-time.
  56. * Unsafe in some contexts: uses ladder, so can expose bigint bits.
  57. */
  58. export declare function FpPow<T>(f: IField<T>, num: T, power: bigint): T;
  59. /**
  60. * Efficiently invert an array of Field elements.
  61. * `inv(0)` will return `undefined` here: make sure to throw an error.
  62. */
  63. export declare function FpInvertBatch<T>(f: IField<T>, nums: T[]): T[];
  64. export declare function FpDiv<T>(f: IField<T>, lhs: T, rhs: T | bigint): T;
  65. export declare function FpIsSquare<T>(f: IField<T>): (x: T) => boolean;
  66. export declare function nLength(n: bigint, nBitLength?: number): {
  67. nBitLength: number;
  68. nByteLength: number;
  69. };
  70. type FpField = IField<bigint> & Required<Pick<IField<bigint>, 'isOdd'>>;
  71. /**
  72. * Initializes a finite field over prime. **Non-primes are not supported.**
  73. * Do not init in loop: slow. Very fragile: always run a benchmark on a change.
  74. * Major performance optimizations:
  75. * * a) denormalized operations like mulN instead of mul
  76. * * b) same object shape: never add or remove keys
  77. * * c) Object.freeze
  78. * @param ORDER prime positive bigint
  79. * @param bitLen how many bits the field consumes
  80. * @param isLE (def: false) if encoding / decoding should be in little-endian
  81. * @param redef optional faster redefinitions of sqrt and other methods
  82. */
  83. export declare function Field(ORDER: bigint, bitLen?: number, isLE?: boolean, redef?: Partial<IField<bigint>>): Readonly<FpField>;
  84. export declare function FpSqrtOdd<T>(Fp: IField<T>, elm: T): T;
  85. export declare function FpSqrtEven<T>(Fp: IField<T>, elm: T): T;
  86. /**
  87. * "Constant-time" private key generation utility.
  88. * Same as mapKeyToField, but accepts less bytes (40 instead of 48 for 32-byte field).
  89. * Which makes it slightly more biased, less secure.
  90. * @deprecated use mapKeyToField instead
  91. */
  92. export declare function hashToPrivateScalar(hash: string | Uint8Array, groupOrder: bigint, isLE?: boolean): bigint;
  93. /**
  94. * Returns total number of bytes consumed by the field element.
  95. * For example, 32 bytes for usual 256-bit weierstrass curve.
  96. * @param fieldOrder number of field elements, usually CURVE.n
  97. * @returns byte length of field
  98. */
  99. export declare function getFieldBytesLength(fieldOrder: bigint): number;
  100. /**
  101. * Returns minimal amount of bytes that can be safely reduced
  102. * by field order.
  103. * Should be 2^-128 for 128-bit curve such as P256.
  104. * @param fieldOrder number of field elements, usually CURVE.n
  105. * @returns byte length of target hash
  106. */
  107. export declare function getMinHashLength(fieldOrder: bigint): number;
  108. /**
  109. * "Constant-time" private key generation utility.
  110. * Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF
  111. * and convert them into private scalar, with the modulo bias being negligible.
  112. * Needs at least 48 bytes of input for 32-byte private key.
  113. * https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/
  114. * FIPS 186-5, A.2 https://csrc.nist.gov/publications/detail/fips/186/5/final
  115. * RFC 9380, https://www.rfc-editor.org/rfc/rfc9380#section-5
  116. * @param hash hash output from SHA3 or a similar function
  117. * @param groupOrder size of subgroup - (e.g. secp256k1.CURVE.n)
  118. * @param isLE interpret hash bytes as LE num
  119. * @returns valid private scalar
  120. */
  121. export declare function mapHashToField(key: Uint8Array, fieldOrder: bigint, isLE?: boolean): Uint8Array;
  122. export {};
  123. //# sourceMappingURL=modular.d.ts.map