@aztec/foundation 0.14.2 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dest/abi/decoder.js +3 -3
  2. package/dest/abi/encoder.d.ts +1 -1
  3. package/dest/abi/encoder.d.ts.map +1 -1
  4. package/dest/abi/encoder.js +3 -3
  5. package/dest/abi/function_selector.d.ts +2 -1
  6. package/dest/abi/function_selector.d.ts.map +1 -1
  7. package/dest/abi/function_selector.js +6 -5
  8. package/dest/aztec-address/index.d.ts +10 -108
  9. package/dest/aztec-address/index.d.ts.map +1 -1
  10. package/dest/aztec-address/index.js +18 -139
  11. package/dest/bigint-buffer/index.d.ts.map +1 -1
  12. package/dest/bigint-buffer/index.js +7 -4
  13. package/dest/collection/array.d.ts.map +1 -1
  14. package/dest/collection/array.js +7 -4
  15. package/dest/crypto/index.d.ts +1 -0
  16. package/dest/crypto/index.d.ts.map +1 -1
  17. package/dest/crypto/index.js +2 -1
  18. package/dest/crypto/pedersen/index.d.ts +1 -9
  19. package/dest/crypto/pedersen/index.d.ts.map +1 -1
  20. package/dest/crypto/pedersen/index.js +2 -298
  21. package/dest/crypto/pedersen/pedersen.elliptic.d.ts +12 -0
  22. package/dest/crypto/pedersen/pedersen.elliptic.d.ts.map +1 -0
  23. package/dest/crypto/pedersen/pedersen.elliptic.js +575 -0
  24. package/dest/crypto/pedersen/pedersen.noble.d.ts +10 -0
  25. package/dest/crypto/pedersen/pedersen.noble.d.ts.map +1 -0
  26. package/dest/crypto/pedersen/pedersen.noble.js +560 -0
  27. package/dest/crypto/pedersen/pedersen.wasm.d.ts +16 -0
  28. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -0
  29. package/dest/crypto/pedersen/pedersen.wasm.js +38 -0
  30. package/dest/crypto/random/index.d.ts.map +1 -1
  31. package/dest/crypto/random/index.js +5 -3
  32. package/dest/crypto/sha256/index.d.ts +0 -8
  33. package/dest/crypto/sha256/index.d.ts.map +1 -1
  34. package/dest/crypto/sha256/index.js +1 -12
  35. package/dest/fields/fields.d.ts +69 -160
  36. package/dest/fields/fields.d.ts.map +1 -1
  37. package/dest/fields/fields.js +142 -185
  38. package/dest/fields/index.d.ts +0 -1
  39. package/dest/fields/index.d.ts.map +1 -1
  40. package/dest/fields/index.js +1 -2
  41. package/dest/fields/point.d.ts.map +1 -1
  42. package/dest/fields/point.js +5 -4
  43. package/dest/json-rpc/class_converter.d.ts.map +1 -1
  44. package/dest/json-rpc/class_converter.js +7 -5
  45. package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -1
  46. package/dest/json-rpc/client/json_rpc_client.js +3 -2
  47. package/dest/log/console.d.ts +1 -1
  48. package/dest/log/console.d.ts.map +1 -1
  49. package/dest/log/debug.d.ts +1 -1
  50. package/dest/log/debug.d.ts.map +1 -1
  51. package/dest/log/index.d.ts +1 -4
  52. package/dest/log/index.d.ts.map +1 -1
  53. package/dest/log/index.js +2 -1
  54. package/dest/log/log_fn.d.ts +5 -0
  55. package/dest/log/log_fn.d.ts.map +1 -0
  56. package/dest/log/log_fn.js +2 -0
  57. package/dest/log/logger.d.ts +1 -1
  58. package/dest/log/logger.d.ts.map +1 -1
  59. package/dest/log/logger.js +7 -4
  60. package/dest/serialize/buffer_reader.d.ts +0 -16
  61. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  62. package/dest/serialize/buffer_reader.js +1 -21
  63. package/dest/serialize/free_funcs.d.ts +6 -0
  64. package/dest/serialize/free_funcs.d.ts.map +1 -1
  65. package/dest/serialize/free_funcs.js +11 -1
  66. package/dest/timer/timer.d.ts +4 -0
  67. package/dest/timer/timer.d.ts.map +1 -1
  68. package/dest/timer/timer.js +10 -4
  69. package/dest/transport/interface/transferable.d.ts +1 -1
  70. package/dest/transport/interface/transferable.d.ts.map +1 -1
  71. package/dest/transport/interface/transferable.js +5 -3
  72. package/dest/wasm/empty_wasi_sdk.d.ts +1 -1
  73. package/dest/wasm/wasm_module.d.ts.map +1 -1
  74. package/dest/wasm/wasm_module.js +4 -3
  75. package/package.json +8 -3
  76. package/src/abi/decoder.ts +2 -2
  77. package/src/abi/encoder.ts +3 -2
  78. package/src/abi/function_selector.ts +5 -4
  79. package/src/aztec-address/index.ts +17 -149
  80. package/src/bigint-buffer/index.ts +9 -3
  81. package/src/collection/array.ts +9 -3
  82. package/src/crypto/index.ts +1 -0
  83. package/src/crypto/pedersen/index.ts +1 -309
  84. package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
  85. package/src/crypto/pedersen/pedersen.noble.ts +573 -0
  86. package/src/crypto/pedersen/pedersen.wasm.ts +46 -0
  87. package/src/crypto/random/index.ts +6 -2
  88. package/src/crypto/sha256/index.ts +0 -13
  89. package/src/fields/fields.ts +197 -197
  90. package/src/fields/index.ts +0 -1
  91. package/src/fields/point.ts +5 -3
  92. package/src/json-rpc/class_converter.ts +8 -4
  93. package/src/json-rpc/client/json_rpc_client.ts +3 -1
  94. package/src/log/console.ts +1 -1
  95. package/src/log/debug.ts +1 -1
  96. package/src/log/index.ts +1 -6
  97. package/src/log/log_fn.ts +5 -0
  98. package/src/log/logger.ts +10 -4
  99. package/src/serialize/buffer_reader.ts +0 -22
  100. package/src/serialize/free_funcs.ts +11 -0
  101. package/src/timer/timer.ts +10 -3
  102. package/src/transport/interface/transferable.ts +7 -3
  103. package/src/wasm/wasm_module.ts +3 -1
  104. package/dest/fields/grumpkin_scalar.d.ts +0 -96
  105. package/dest/fields/grumpkin_scalar.d.ts.map +0 -1
  106. package/dest/fields/grumpkin_scalar.js +0 -125
  107. package/src/fields/grumpkin_scalar.ts +0 -138
@@ -14,7 +14,14 @@ export class Timer {
14
14
  private start: number;
15
15
 
16
16
  constructor() {
17
- this.start = new Date().getTime();
17
+ this.start = performance.now();
18
+ }
19
+
20
+ /**
21
+ * Return microseconds.
22
+ */
23
+ public us() {
24
+ return this.ms() * 1000;
18
25
  }
19
26
 
20
27
  /**
@@ -25,7 +32,7 @@ export class Timer {
25
32
  * @returns The elapsed time in milliseconds.
26
33
  */
27
34
  public ms() {
28
- return new Date().getTime() - this.start;
35
+ return performance.now() - this.start;
29
36
  }
30
37
 
31
38
  /**
@@ -36,6 +43,6 @@ export class Timer {
36
43
  * @returns The elapsed time in seconds.
37
44
  */
38
45
  public s() {
39
- return (new Date().getTime() - this.start) / 1000;
46
+ return this.ms() / 1000;
40
47
  }
41
48
  }
@@ -36,7 +36,9 @@ export interface TransferDescriptor<T = any> {
36
36
  * @returns A boolean indicating whether the object is transferable.
37
37
  */
38
38
  function isTransferable(thing: any): thing is Transferable {
39
- if (!thing || typeof thing !== 'object') return false;
39
+ if (!thing || typeof thing !== 'object') {
40
+ return false;
41
+ }
40
42
  // Don't check too thoroughly, since the list of transferable things in JS might grow over time
41
43
  return true;
42
44
  }
@@ -77,7 +79,7 @@ export function Transfer<T>(transferable: Transferable): TransferDescriptor<T>;
77
79
  * Mark transferable objects within an arbitrary object or array as
78
80
  * being a transferable object. They will then not be serialized
79
81
  * and deserialized on messaging with the main thread, but ownership
80
- * of them will be tranferred to the receiving thread.
82
+ * of them will be transferred to the receiving thread.
81
83
  *
82
84
  * Only array buffers, message ports and few more special types of
83
85
  * objects can be transferred, but it's much faster than serializing and
@@ -109,7 +111,9 @@ export function Transfer<T>(payload: T, transferables: Transferable[]): Transfer
109
111
  */
110
112
  export function Transfer<T>(payload: T, transferables?: Transferable[]): TransferDescriptor<T> {
111
113
  if (!transferables) {
112
- if (!isTransferable(payload)) throw Error();
114
+ if (!isTransferable(payload)) {
115
+ throw Error();
116
+ }
113
117
  transferables = [payload];
114
118
  }
115
119
 
@@ -232,7 +232,9 @@ export class WasmModule implements IWasmModule {
232
232
  addr = addr >>> 0;
233
233
  const m = this.getMemory();
234
234
  let i = addr;
235
- for (; m[i] !== 0; ++i);
235
+ while (m[i] !== 0) {
236
+ ++i;
237
+ }
236
238
  return Buffer.from(m.slice(addr, i)).toString('ascii');
237
239
  }
238
240
 
@@ -1,96 +0,0 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- import { BufferReader } from '../serialize/buffer_reader.js';
3
- import { Fr } from './fields.js';
4
- /**
5
- * Represents a field element in a prime finite field with modulus defined by the constant MODULUS.
6
- * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
7
- * the Aztec Yellow Paper.
8
- */
9
- export declare class GrumpkinScalar {
10
- /**
11
- * The element's value as a bigint in the finite field.
12
- */
13
- readonly value: bigint;
14
- static MODULUS: bigint;
15
- static MAX_VALUE: bigint;
16
- static SIZE_IN_BYTES: number;
17
- private static HIGH_SHIFT;
18
- private static LOW_MASK;
19
- constructor(
20
- /**
21
- * The element's value as a bigint in the finite field.
22
- */
23
- value: bigint);
24
- get low(): Fr;
25
- get high(): Fr;
26
- /**
27
- * Deserialize a grumpkin scalar serialized in 2 Fr.
28
- * @param high - The high Fr element.
29
- * @param low - The low Fr element.
30
- * @returns A GrumpkinScalar instance with the value of the two Fr elements.
31
- */
32
- static fromHighLow(high: Fr, low: Fr): GrumpkinScalar;
33
- /**
34
- * Generates a random GrumpkinScalar.
35
- *
36
- * @returns A new GrumpkinScalar instance with a randomly generated value.
37
- */
38
- static random(): GrumpkinScalar;
39
- /**
40
- * Create an instance of GrumpkinScalar from a given buffer or BufferReader.
41
- * @remarks Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
42
- * with that value. Throws an error if the value is out of range for the calling class.
43
- *
44
- * @param buffer - The input buffer or BufferReader containing the bytes representing the value.
45
- * @returns A GrumpkinScalar instance.
46
- */
47
- static fromBuffer(buffer: Buffer | BufferReader): GrumpkinScalar;
48
- /**
49
- * Like fromBuffer, but reduces the value modulo MODULUS.
50
- *
51
- * @param buffer - The Buffer or BufferReader containing the bytes representing the value.
52
- * @returns GrumpkinScalar with the decoded value.
53
- */
54
- static fromBufferWithReduction(buffer: Buffer | BufferReader): GrumpkinScalar;
55
- /**
56
- * Create a GrumpkinScalar instance from a hex-encoded string.
57
- * The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
58
- * Throws an error if the input length is invalid or the address value is out of range.
59
- *
60
- * @param address - The hex-encoded string representing the field element.
61
- * @returns A GrumpkinScalar instance.
62
- */
63
- static fromString(address: string): GrumpkinScalar;
64
- /**
65
- * Converts the bigint value of the instance to a Buffer representation.
66
- * The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
67
- *
68
- * @returns A Buffer containing the byte representation of the instance's value.
69
- */
70
- toBuffer(): Buffer;
71
- /**
72
- * Converts the GrumpkinScalar value to a hexadecimal string representation.
73
- * The resulting string is prefixed with '0x' and contains the exact number of hex characters required
74
- * to represent the numeric value of this instance.
75
- *
76
- * @param padTo32 - Whether to pad the resulting string to 32 bytes.
77
- * @returns A hexadecimal string representing the GrumpkinScalar value.
78
- */
79
- toString(padTo32?: boolean): `0x${string}`;
80
- /**
81
- * Check if the value of the current instance is zero.
82
- * This function compares the internal 'value' property with 0n (BigInt representation of zero).
83
- * Returns true if the value is zero, otherwise returns false.
84
- *
85
- * @returns A boolean indicating whether the value is zero or not.
86
- */
87
- isZero(): boolean;
88
- /**
89
- * Converts the value of the GrumpkinScalar instance to a friendly JSON format.
90
- * The output is a hexadecimal string representation of the value with '0x' prefix.
91
- *
92
- * @returns A string representing the value in the JSON format.
93
- */
94
- toFriendlyJSON(): `0x${string}`;
95
- }
96
- //# sourceMappingURL=grumpkin_scalar.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"grumpkin_scalar.d.ts","sourceRoot":"","sources":["../../src/fields/grumpkin_scalar.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEjC;;;;GAIG;AACH,qBAAa,cAAc;IAUvB;;OAEG;aACa,KAAK,EAAE,MAAM;IAZ/B,MAAM,CAAC,OAAO,SAAuE;IACrF,MAAM,CAAC,SAAS,SAA+B;IAC/C,MAAM,CAAC,aAAa,SAAM;IAG1B,OAAO,CAAC,MAAM,CAAC,UAAU,CAAkD;IAC3E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA0C;;IAG/D;;OAEG;IACa,KAAK,EAAE,MAAM;IAO/B,IAAI,GAAG,IAAI,EAAE,CAEZ;IAED,IAAI,IAAI,IAAI,EAAE,CAEb;IAED;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,cAAc;IAIrD;;;;OAIG;IACH,MAAM,CAAC,MAAM;IAKb;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAK/C;;;;;OAKG;IACH,MAAM,CAAC,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAM5D;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM;IAIjC;;;;;OAKG;IACH,QAAQ;IAIR;;;;;;;OAOG;IACH,QAAQ,CAAC,OAAO,UAAQ;IAIxB;;;;;;OAMG;IACH,MAAM;IAIN;;;;;OAKG;IACH,cAAc;CAGf"}
@@ -1,125 +0,0 @@
1
- import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
2
- import { randomBytes } from '../crypto/index.js';
3
- import { BufferReader } from '../serialize/buffer_reader.js';
4
- import { Fr } from './fields.js';
5
- /**
6
- * Represents a field element in a prime finite field with modulus defined by the constant MODULUS.
7
- * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
8
- * the Aztec Yellow Paper.
9
- */
10
- export class GrumpkinScalar {
11
- constructor(
12
- /**
13
- * The element's value as a bigint in the finite field.
14
- */
15
- value) {
16
- this.value = value;
17
- if (value > GrumpkinScalar.MAX_VALUE) {
18
- throw new Error(`GrumpkinScalar out of range ${value}.`);
19
- }
20
- }
21
- get low() {
22
- return new Fr(this.value & GrumpkinScalar.LOW_MASK);
23
- }
24
- get high() {
25
- return new Fr(this.value >> GrumpkinScalar.HIGH_SHIFT);
26
- }
27
- /**
28
- * Deserialize a grumpkin scalar serialized in 2 Fr.
29
- * @param high - The high Fr element.
30
- * @param low - The low Fr element.
31
- * @returns A GrumpkinScalar instance with the value of the two Fr elements.
32
- */
33
- static fromHighLow(high, low) {
34
- return new GrumpkinScalar((high.value << GrumpkinScalar.HIGH_SHIFT) + low.value);
35
- }
36
- /**
37
- * Generates a random GrumpkinScalar.
38
- *
39
- * @returns A new GrumpkinScalar instance with a randomly generated value.
40
- */
41
- static random() {
42
- const r = toBigIntBE(randomBytes(64)) % GrumpkinScalar.MODULUS;
43
- return new this(r);
44
- }
45
- /**
46
- * Create an instance of GrumpkinScalar from a given buffer or BufferReader.
47
- * @remarks Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
48
- * with that value. Throws an error if the value is out of range for the calling class.
49
- *
50
- * @param buffer - The input buffer or BufferReader containing the bytes representing the value.
51
- * @returns A GrumpkinScalar instance.
52
- */
53
- static fromBuffer(buffer) {
54
- const reader = BufferReader.asReader(buffer);
55
- return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
56
- }
57
- /**
58
- * Like fromBuffer, but reduces the value modulo MODULUS.
59
- *
60
- * @param buffer - The Buffer or BufferReader containing the bytes representing the value.
61
- * @returns GrumpkinScalar with the decoded value.
62
- */
63
- static fromBufferWithReduction(buffer) {
64
- const reader = BufferReader.asReader(buffer);
65
- const value = toBigIntBE(reader.readBytes(GrumpkinScalar.SIZE_IN_BYTES)) % GrumpkinScalar.MODULUS;
66
- return new GrumpkinScalar(value);
67
- }
68
- /**
69
- * Create a GrumpkinScalar instance from a hex-encoded string.
70
- * The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
71
- * Throws an error if the input length is invalid or the address value is out of range.
72
- *
73
- * @param address - The hex-encoded string representing the field element.
74
- * @returns A GrumpkinScalar instance.
75
- */
76
- static fromString(address) {
77
- return GrumpkinScalar.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
78
- }
79
- /**
80
- * Converts the bigint value of the instance to a Buffer representation.
81
- * The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
82
- *
83
- * @returns A Buffer containing the byte representation of the instance's value.
84
- */
85
- toBuffer() {
86
- return toBufferBE(this.value, GrumpkinScalar.SIZE_IN_BYTES);
87
- }
88
- /**
89
- * Converts the GrumpkinScalar value to a hexadecimal string representation.
90
- * The resulting string is prefixed with '0x' and contains the exact number of hex characters required
91
- * to represent the numeric value of this instance.
92
- *
93
- * @param padTo32 - Whether to pad the resulting string to 32 bytes.
94
- * @returns A hexadecimal string representing the GrumpkinScalar value.
95
- */
96
- toString(padTo32 = false) {
97
- return toHex(this.value, padTo32);
98
- }
99
- /**
100
- * Check if the value of the current instance is zero.
101
- * This function compares the internal 'value' property with 0n (BigInt representation of zero).
102
- * Returns true if the value is zero, otherwise returns false.
103
- *
104
- * @returns A boolean indicating whether the value is zero or not.
105
- */
106
- isZero() {
107
- return this.value === 0n;
108
- }
109
- /**
110
- * Converts the value of the GrumpkinScalar instance to a friendly JSON format.
111
- * The output is a hexadecimal string representation of the value with '0x' prefix.
112
- *
113
- * @returns A string representing the value in the JSON format.
114
- */
115
- toFriendlyJSON() {
116
- return this.toString();
117
- }
118
- }
119
- GrumpkinScalar.MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
120
- GrumpkinScalar.MAX_VALUE = GrumpkinScalar.MODULUS - 1n;
121
- GrumpkinScalar.SIZE_IN_BYTES = 32;
122
- // The following constants are used to split a GrumpkinScalar into two Fr elements.
123
- GrumpkinScalar.HIGH_SHIFT = BigInt((GrumpkinScalar.SIZE_IN_BYTES / 2) * 8);
124
- GrumpkinScalar.LOW_MASK = (1n << GrumpkinScalar.HIGH_SHIFT) - 1n;
125
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ3J1bXBra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BTyxDQUFDLE9BQU8sQ0FBQyxNQUFNLEVBQUUsRUFBRSxDQUFDLEVBQUUsS0FBSyxDQUFDLENBQUMsQ0FBQztJQUNwRixDQUFDO0lBRUQ7Ozs7O09BS0c7SUFDSCxRQUFRO1FBQ04sT0FBTyxVQUFVLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxjQUFjLENBQUMsYUFBYSxDQUFDLENBQUM7SUFDOUQsQ0FBQztJQUVEOzs7Ozs7O09BT0c7SUFDSCxRQUFRLENBQUMsT0FBTyxHQUFHLEtBQUs7UUFDdEIsT0FBTyxLQUFLLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxPQUFPLENBQUMsQ0FBQztJQUNwQyxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsTUFBTTtRQUNKLE9BQU8sSUFBSSxDQUFDLEtBQUssS0FBSyxFQUFFLENBQUM7SUFDM0IsQ0FBQztJQUVEOzs7OztPQUtHO0lBQ0gsY0FBYztRQUNaLE9BQU8sSUFBSSxDQUFDLFFBQVEsRUFBRSxDQUFDO0lBQ3pCLENBQUM7O0FBN0hNLHNCQUFPLEdBQUcsbUVBQW1FLENBQUM7QUFDOUUsd0JBQVMsR0FBRyxjQUFjLENBQUMsT0FBTyxHQUFHLEVBQUUsQ0FBQztBQUN4Qyw0QkFBYSxHQUFHLEVBQUUsQ0FBQztBQUUxQixtRkFBbUY7QUFDcEUseUJBQVUsR0FBRyxNQUFNLENBQUMsQ0FBQyxjQUFjLENBQUMsYUFBYSxHQUFHLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO0FBQzVELHVCQUFRLEdBQUcsQ0FBQyxFQUFFLElBQUksY0FBYyxDQUFDLFVBQVUsQ0FBQyxHQUFHLEVBQUUsQ0FBQyJ9
@@ -1,138 +0,0 @@
1
- import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
2
- import { randomBytes } from '../crypto/index.js';
3
- import { BufferReader } from '../serialize/buffer_reader.js';
4
- import { Fr } from './fields.js';
5
-
6
- /**
7
- * Represents a field element in a prime finite field with modulus defined by the constant MODULUS.
8
- * @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
9
- * the Aztec Yellow Paper.
10
- */
11
- export class GrumpkinScalar {
12
- static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
13
- static MAX_VALUE = GrumpkinScalar.MODULUS - 1n;
14
- static SIZE_IN_BYTES = 32;
15
-
16
- // The following constants are used to split a GrumpkinScalar into two Fr elements.
17
- private static HIGH_SHIFT = BigInt((GrumpkinScalar.SIZE_IN_BYTES / 2) * 8);
18
- private static LOW_MASK = (1n << GrumpkinScalar.HIGH_SHIFT) - 1n;
19
-
20
- constructor(
21
- /**
22
- * The element's value as a bigint in the finite field.
23
- */
24
- public readonly value: bigint,
25
- ) {
26
- if (value > GrumpkinScalar.MAX_VALUE) {
27
- throw new Error(`GrumpkinScalar out of range ${value}.`);
28
- }
29
- }
30
-
31
- get low(): Fr {
32
- return new Fr(this.value & GrumpkinScalar.LOW_MASK);
33
- }
34
-
35
- get high(): Fr {
36
- return new Fr(this.value >> GrumpkinScalar.HIGH_SHIFT);
37
- }
38
-
39
- /**
40
- * Deserialize a grumpkin scalar serialized in 2 Fr.
41
- * @param high - The high Fr element.
42
- * @param low - The low Fr element.
43
- * @returns A GrumpkinScalar instance with the value of the two Fr elements.
44
- */
45
- static fromHighLow(high: Fr, low: Fr): GrumpkinScalar {
46
- return new GrumpkinScalar((high.value << GrumpkinScalar.HIGH_SHIFT) + low.value);
47
- }
48
-
49
- /**
50
- * Generates a random GrumpkinScalar.
51
- *
52
- * @returns A new GrumpkinScalar instance with a randomly generated value.
53
- */
54
- static random() {
55
- const r = toBigIntBE(randomBytes(64)) % GrumpkinScalar.MODULUS;
56
- return new this(r);
57
- }
58
-
59
- /**
60
- * Create an instance of GrumpkinScalar from a given buffer or BufferReader.
61
- * @remarks Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
62
- * with that value. Throws an error if the value is out of range for the calling class.
63
- *
64
- * @param buffer - The input buffer or BufferReader containing the bytes representing the value.
65
- * @returns A GrumpkinScalar instance.
66
- */
67
- static fromBuffer(buffer: Buffer | BufferReader) {
68
- const reader = BufferReader.asReader(buffer);
69
- return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
70
- }
71
-
72
- /**
73
- * Like fromBuffer, but reduces the value modulo MODULUS.
74
- *
75
- * @param buffer - The Buffer or BufferReader containing the bytes representing the value.
76
- * @returns GrumpkinScalar with the decoded value.
77
- */
78
- static fromBufferWithReduction(buffer: Buffer | BufferReader) {
79
- const reader = BufferReader.asReader(buffer);
80
- const value = toBigIntBE(reader.readBytes(GrumpkinScalar.SIZE_IN_BYTES)) % GrumpkinScalar.MODULUS;
81
- return new GrumpkinScalar(value);
82
- }
83
-
84
- /**
85
- * Create a GrumpkinScalar instance from a hex-encoded string.
86
- * The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
87
- * Throws an error if the input length is invalid or the address value is out of range.
88
- *
89
- * @param address - The hex-encoded string representing the field element.
90
- * @returns A GrumpkinScalar instance.
91
- */
92
- static fromString(address: string) {
93
- return GrumpkinScalar.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
94
- }
95
-
96
- /**
97
- * Converts the bigint value of the instance to a Buffer representation.
98
- * The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
99
- *
100
- * @returns A Buffer containing the byte representation of the instance's value.
101
- */
102
- toBuffer() {
103
- return toBufferBE(this.value, GrumpkinScalar.SIZE_IN_BYTES);
104
- }
105
-
106
- /**
107
- * Converts the GrumpkinScalar value to a hexadecimal string representation.
108
- * The resulting string is prefixed with '0x' and contains the exact number of hex characters required
109
- * to represent the numeric value of this instance.
110
- *
111
- * @param padTo32 - Whether to pad the resulting string to 32 bytes.
112
- * @returns A hexadecimal string representing the GrumpkinScalar value.
113
- */
114
- toString(padTo32 = false) {
115
- return toHex(this.value, padTo32);
116
- }
117
-
118
- /**
119
- * Check if the value of the current instance is zero.
120
- * This function compares the internal 'value' property with 0n (BigInt representation of zero).
121
- * Returns true if the value is zero, otherwise returns false.
122
- *
123
- * @returns A boolean indicating whether the value is zero or not.
124
- */
125
- isZero() {
126
- return this.value === 0n;
127
- }
128
-
129
- /**
130
- * Converts the value of the GrumpkinScalar instance to a friendly JSON format.
131
- * The output is a hexadecimal string representation of the value with '0x' prefix.
132
- *
133
- * @returns A string representing the value in the JSON format.
134
- */
135
- toFriendlyJSON() {
136
- return this.toString();
137
- }
138
- }