@aztec/foundation 0.15.0 → 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 (84) 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.js +3 -3
  6. package/dest/aztec-address/index.d.ts +10 -108
  7. package/dest/aztec-address/index.d.ts.map +1 -1
  8. package/dest/aztec-address/index.js +18 -139
  9. package/dest/bigint-buffer/index.d.ts.map +1 -1
  10. package/dest/bigint-buffer/index.js +7 -4
  11. package/dest/collection/array.d.ts.map +1 -1
  12. package/dest/collection/array.js +7 -4
  13. package/dest/crypto/pedersen/pedersen.wasm.d.ts +4 -0
  14. package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
  15. package/dest/crypto/pedersen/pedersen.wasm.js +18 -6
  16. package/dest/crypto/random/index.d.ts.map +1 -1
  17. package/dest/crypto/random/index.js +5 -3
  18. package/dest/crypto/sha256/index.d.ts +0 -8
  19. package/dest/crypto/sha256/index.d.ts.map +1 -1
  20. package/dest/crypto/sha256/index.js +1 -12
  21. package/dest/fields/fields.d.ts +69 -160
  22. package/dest/fields/fields.d.ts.map +1 -1
  23. package/dest/fields/fields.js +142 -185
  24. package/dest/fields/index.d.ts +0 -1
  25. package/dest/fields/index.d.ts.map +1 -1
  26. package/dest/fields/index.js +1 -2
  27. package/dest/fields/point.d.ts.map +1 -1
  28. package/dest/fields/point.js +5 -4
  29. package/dest/json-rpc/class_converter.d.ts.map +1 -1
  30. package/dest/json-rpc/class_converter.js +7 -5
  31. package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -1
  32. package/dest/json-rpc/client/json_rpc_client.js +3 -2
  33. package/dest/log/console.d.ts +1 -1
  34. package/dest/log/console.d.ts.map +1 -1
  35. package/dest/log/debug.d.ts +1 -1
  36. package/dest/log/debug.d.ts.map +1 -1
  37. package/dest/log/index.d.ts +1 -4
  38. package/dest/log/index.d.ts.map +1 -1
  39. package/dest/log/index.js +2 -1
  40. package/dest/log/log_fn.d.ts +5 -0
  41. package/dest/log/log_fn.d.ts.map +1 -0
  42. package/dest/log/log_fn.js +2 -0
  43. package/dest/log/logger.d.ts +1 -1
  44. package/dest/log/logger.d.ts.map +1 -1
  45. package/dest/log/logger.js +7 -4
  46. package/dest/serialize/buffer_reader.d.ts +0 -16
  47. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  48. package/dest/serialize/buffer_reader.js +1 -21
  49. package/dest/serialize/free_funcs.d.ts +6 -0
  50. package/dest/serialize/free_funcs.d.ts.map +1 -1
  51. package/dest/serialize/free_funcs.js +11 -1
  52. package/dest/transport/interface/transferable.d.ts.map +1 -1
  53. package/dest/transport/interface/transferable.js +5 -3
  54. package/dest/wasm/empty_wasi_sdk.d.ts +1 -1
  55. package/dest/wasm/wasm_module.d.ts.map +1 -1
  56. package/dest/wasm/wasm_module.js +4 -3
  57. package/package.json +3 -4
  58. package/src/abi/decoder.ts +2 -2
  59. package/src/abi/encoder.ts +3 -2
  60. package/src/abi/function_selector.ts +2 -2
  61. package/src/aztec-address/index.ts +17 -149
  62. package/src/bigint-buffer/index.ts +9 -3
  63. package/src/collection/array.ts +9 -3
  64. package/src/crypto/pedersen/pedersen.wasm.ts +23 -5
  65. package/src/crypto/random/index.ts +6 -2
  66. package/src/crypto/sha256/index.ts +0 -13
  67. package/src/fields/fields.ts +197 -197
  68. package/src/fields/index.ts +0 -1
  69. package/src/fields/point.ts +5 -3
  70. package/src/json-rpc/class_converter.ts +8 -4
  71. package/src/json-rpc/client/json_rpc_client.ts +3 -1
  72. package/src/log/console.ts +1 -1
  73. package/src/log/debug.ts +1 -1
  74. package/src/log/index.ts +1 -6
  75. package/src/log/log_fn.ts +5 -0
  76. package/src/log/logger.ts +10 -4
  77. package/src/serialize/buffer_reader.ts +0 -22
  78. package/src/serialize/free_funcs.ts +11 -0
  79. package/src/transport/interface/transferable.ts +6 -2
  80. package/src/wasm/wasm_module.ts +3 -1
  81. package/dest/fields/grumpkin_scalar.d.ts +0 -96
  82. package/dest/fields/grumpkin_scalar.d.ts.map +0 -1
  83. package/dest/fields/grumpkin_scalar.js +0 -125
  84. package/src/fields/grumpkin_scalar.ts +0 -138
@@ -123,7 +123,9 @@ export function createJsonRpcClient<T extends object>(
123
123
  {},
124
124
  {
125
125
  get: (target, rpcMethod: string) => {
126
- if (['then', 'catch'].includes(rpcMethod)) return Reflect.get(target, rpcMethod);
126
+ if (['then', 'catch'].includes(rpcMethod)) {
127
+ return Reflect.get(target, rpcMethod);
128
+ }
127
129
  return (...params: any[]) => {
128
130
  debug(format(`JsonRpcClient.constructor`, 'proxy', rpcMethod, '<-', params));
129
131
  return request(rpcMethod, params);
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-console */
2
- import { LogFn } from './index.js';
2
+ import { LogFn } from './log_fn.js';
3
3
 
4
4
  /**
5
5
  * ConsoleLogger is a utility class that provides customizable console logging functionality.
package/src/log/debug.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import debug from 'debug';
2
2
 
3
- import { LogFn } from './index.js';
3
+ import { LogFn } from './log_fn.js';
4
4
 
5
5
  let preLogHook: ((...args: any[]) => void) | undefined;
6
6
  let postLogHook: ((...args: any[]) => void) | undefined;
package/src/log/index.ts CHANGED
@@ -1,10 +1,5 @@
1
- /** Structured log data to include with the message. */
2
- export type LogData = Record<string, string | number | bigint | boolean>;
3
-
4
- /** A callable logger instance. */
5
- export type LogFn = (msg: string, data?: LogData) => void;
6
-
7
1
  export * from './console.js';
8
2
  export * from './debug.js';
9
3
  export * from './logger.js';
10
4
  export * from './log_history.js';
5
+ export * from './log_fn.js';
@@ -0,0 +1,5 @@
1
+ /** Structured log data to include with the message. */
2
+ export type LogData = Record<string, string | number | bigint | boolean>;
3
+
4
+ /** A callable logger instance. */
5
+ export type LogFn = (msg: string, data?: LogData) => void;
package/src/log/logger.ts CHANGED
@@ -2,7 +2,7 @@ import debug from 'debug';
2
2
  import isNode from 'detect-node';
3
3
  import { isatty } from 'tty';
4
4
 
5
- import { LogData, LogFn } from './index.js';
5
+ import { LogData, LogFn } from './log_fn.js';
6
6
 
7
7
  // Matches a subset of Winston log levels
8
8
  const LogLevels = ['silent', 'error', 'warn', 'info', 'verbose', 'debug'] as const;
@@ -39,7 +39,9 @@ export type DebugLogger = LogFn & Logger;
39
39
  */
40
40
  export function createDebugLogger(name: string): DebugLogger {
41
41
  const debugLogger = debug(name);
42
- if (currentLevel === 'debug') debugLogger.enabled = true;
42
+ if (currentLevel === 'debug') {
43
+ debugLogger.enabled = true;
44
+ }
43
45
 
44
46
  const logger = {
45
47
  silent: () => {},
@@ -78,7 +80,9 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
78
80
 
79
81
  msg = data ? `${msg} ${fmtLogData(data)}` : msg;
80
82
  if (debug.enabled) {
81
- if (level !== 'debug') msg = `${level.toUpperCase()} ${msg}`;
83
+ if (level !== 'debug') {
84
+ msg = `${level.toUpperCase()} ${msg}`;
85
+ }
82
86
  debug(msg);
83
87
  } else if (LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
84
88
  printLog(`${getPrefix(debug, level)} ${msg}`);
@@ -94,7 +98,9 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
94
98
  function getPrefix(debugLogger: debug.Debugger, level: LogLevel) {
95
99
  const levelLabel = currentLevel !== level ? ` ${level.toUpperCase()}` : '';
96
100
  const prefix = `${debugLogger.namespace.replace(/^aztec:/, '')}${levelLabel}`;
97
- if (!isNode || !isatty(process.stderr.fd)) return prefix;
101
+ if (!isNode || !isatty(process.stderr.fd)) {
102
+ return prefix;
103
+ }
98
104
  const colorIndex = debug.selectColor(debugLogger.namespace) as number;
99
105
  const colorCode = '\u001B[3' + (colorIndex < 8 ? colorIndex : '8;5;' + colorIndex);
100
106
  return ` ${colorCode};1m${prefix}\u001B[0m`;
@@ -1,4 +1,3 @@
1
- import { Fq, Fr } from '../fields/fields.js';
2
1
  import { Tuple } from './types.js';
3
2
 
4
3
  /**
@@ -83,27 +82,6 @@ export class BufferReader {
83
82
  return Buffer.from(this.buffer.subarray(this.index - n, this.index));
84
83
  }
85
84
 
86
- /**
87
- * Reads a Fr (finite field) element from the buffer using the 'fromBuffer' method of the Fr class.
88
- * The Fr class should provide a 'fromBuffer' method that takes a BufferReader instance as input.
89
- *
90
- * @returns An instance of the Fr class representing the finite field element.
91
- */
92
- public readFr(): Fr {
93
- return Fr.fromBuffer(this);
94
- }
95
-
96
- /**
97
- * Reads the next Fq element from the buffer using the Fq.fromBuffer method.
98
- * The Fq element represents a finite field in elliptic curve cryptography and is used for calculations.
99
- * Advances the internal buffer index by the number of bytes read.
100
- *
101
- * @returns An Fq instance representing the finite field element.
102
- */
103
- public readFq(): Fq {
104
- return Fq.fromBuffer(this);
105
- }
106
-
107
85
  /**
108
86
  * Reads a vector of numbers from the buffer and returns it as an array of numbers.
109
87
  * The method utilizes the 'readVector' method, passing a deserializer that reads numbers.
@@ -15,6 +15,17 @@ export function boolToByte(b: boolean) {
15
15
  return buf;
16
16
  }
17
17
 
18
+ /**
19
+ * @param n - The input number to be converted to a big-endian unsigned 16-bit integer Buffer.
20
+ * @param bufferSize - Optional, the size of the output Buffer (default is 2).
21
+ * @returns A Buffer containing the big-endian unsigned 16-bit integer representation of the input number.
22
+ */
23
+ export function numToUInt16BE(n: number, bufferSize = 2) {
24
+ const buf = Buffer.alloc(bufferSize);
25
+ buf.writeUInt16BE(n, bufferSize - 2);
26
+ return buf;
27
+ }
28
+
18
29
  /**
19
30
  * Convert a number into a 4-byte little-endian unsigned integer buffer.
20
31
  * The input number is serialized as an unsigned 32-bit integer in little-endian byte order,
@@ -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
  }
@@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ3J1bXBraW5fc2NhbGFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2ZpZWxkcy9ncnVtcGtpbl9zY2FsYXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsS0FBSyxFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFDMUUsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBQ2pELE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUM3RCxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBRWpDOzs7O0dBSUc7QUFDSCxNQUFNLE9BQU8sY0FBYztJQVN6QjtJQUNFOztPQUVHO0lBQ2EsS0FBYTtRQUFiLFVBQUssR0FBTCxLQUFLLENBQVE7UUFFN0IsSUFBSSxLQUFLLEdBQUcsY0FBYyxD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QUFDLFFBQVEsRUFBRSxDQUFDO0lBQ3pCLENBQUM7O0FBN0hNLHNCQUFPLEdBQUcsbUVBQW1FLENBQUM7QUFDOUUsd0JBQVMsR0FBRyxjQUFjLENBQUMsT0FBTyxHQUFHLEVBQUUsQ0FBQztBQUN4Qyw0QkFBYSxHQUFHLEVBQUUsQ0FBQztBQUUxQixtRkFBbUY7QUFDcEUseUJBQVUsR0FBRyxNQUFNLENBQUMsQ0FBQyxjQUFjLENBQUMsYUFBYSxHQUFHLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO0FBQzVELHVCQUFRLEdBQUcsQ0FBQyxFQUFFLElBQUksY0FBYyxDQUFDLFVBQVUsQ0FBQyxHQUFHLEVBQUUsQ0FBQyJ9
@@ -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
- }