@aztec/foundation 0.15.0 → 0.16.0

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 (85) 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 +1 -0
  28. package/dest/fields/point.d.ts.map +1 -1
  29. package/dest/fields/point.js +8 -4
  30. package/dest/json-rpc/class_converter.d.ts.map +1 -1
  31. package/dest/json-rpc/class_converter.js +7 -5
  32. package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -1
  33. package/dest/json-rpc/client/json_rpc_client.js +3 -2
  34. package/dest/log/console.d.ts +1 -1
  35. package/dest/log/console.d.ts.map +1 -1
  36. package/dest/log/debug.d.ts +1 -1
  37. package/dest/log/debug.d.ts.map +1 -1
  38. package/dest/log/index.d.ts +1 -4
  39. package/dest/log/index.d.ts.map +1 -1
  40. package/dest/log/index.js +2 -1
  41. package/dest/log/log_fn.d.ts +5 -0
  42. package/dest/log/log_fn.d.ts.map +1 -0
  43. package/dest/log/log_fn.js +2 -0
  44. package/dest/log/logger.d.ts +1 -1
  45. package/dest/log/logger.d.ts.map +1 -1
  46. package/dest/log/logger.js +7 -4
  47. package/dest/serialize/buffer_reader.d.ts +0 -16
  48. package/dest/serialize/buffer_reader.d.ts.map +1 -1
  49. package/dest/serialize/buffer_reader.js +1 -21
  50. package/dest/serialize/free_funcs.d.ts +6 -0
  51. package/dest/serialize/free_funcs.d.ts.map +1 -1
  52. package/dest/serialize/free_funcs.js +11 -2
  53. package/dest/transport/interface/transferable.d.ts.map +1 -1
  54. package/dest/transport/interface/transferable.js +5 -3
  55. package/dest/wasm/empty_wasi_sdk.d.ts +1 -1
  56. package/dest/wasm/wasm_module.d.ts.map +1 -1
  57. package/dest/wasm/wasm_module.js +4 -3
  58. package/package.json +3 -4
  59. package/src/abi/decoder.ts +2 -2
  60. package/src/abi/encoder.ts +3 -2
  61. package/src/abi/function_selector.ts +2 -2
  62. package/src/aztec-address/index.ts +17 -149
  63. package/src/bigint-buffer/index.ts +9 -3
  64. package/src/collection/array.ts +9 -3
  65. package/src/crypto/pedersen/pedersen.wasm.ts +23 -5
  66. package/src/crypto/random/index.ts +6 -2
  67. package/src/crypto/sha256/index.ts +0 -13
  68. package/src/fields/fields.ts +197 -197
  69. package/src/fields/index.ts +0 -1
  70. package/src/fields/point.ts +9 -3
  71. package/src/json-rpc/class_converter.ts +8 -4
  72. package/src/json-rpc/client/json_rpc_client.ts +3 -1
  73. package/src/log/console.ts +1 -1
  74. package/src/log/debug.ts +1 -1
  75. package/src/log/index.ts +1 -6
  76. package/src/log/log_fn.ts +5 -0
  77. package/src/log/logger.ts +10 -4
  78. package/src/serialize/buffer_reader.ts +0 -22
  79. package/src/serialize/free_funcs.ts +11 -1
  80. package/src/transport/interface/transferable.ts +6 -2
  81. package/src/wasm/wasm_module.ts +3 -1
  82. package/dest/fields/grumpkin_scalar.d.ts +0 -96
  83. package/dest/fields/grumpkin_scalar.d.ts.map +0 -1
  84. package/dest/fields/grumpkin_scalar.js +0 -125
  85. package/src/fields/grumpkin_scalar.ts +0 -138
@@ -1,162 +1,30 @@
1
- import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
2
1
  import { Fr } from '../fields/index.js';
3
- import { BufferReader } from '../serialize/buffer_reader.js';
4
2
 
5
3
  /**
6
- * AztecAddress represents a 32-byte address in the Aztec Protocol. It provides methods to create, manipulate, and
7
- * compare addresses. The maximum value of an address is determined by the field modulus and all instances of AztecAddress
8
- * should have a value less than or equal to this max value. This class also provides helper functions to convert
9
- * addresses from strings, buffers, and other formats.
4
+ * AztecAddress represents a 32-byte address in the Aztec Protocol.
5
+ * It provides methods to create, manipulate, and compare addresses.
6
+ * The maximum value of an address is determined by the field modulus and all instances of AztecAddress.
7
+ * It should have a value less than or equal to this max value.
8
+ * This class also provides helper functions to convert addresses from strings, buffers, and other formats.
10
9
  */
11
- export class AztecAddress {
12
- static SIZE_IN_BYTES = 32;
13
- static ZERO = new AztecAddress(Buffer.alloc(AztecAddress.SIZE_IN_BYTES));
14
- static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
15
- static MAX_VALUE = AztecAddress.MODULUS - 1n;
16
-
17
- constructor(
18
- /**
19
- * The buffer field.
20
- */
21
- public readonly buffer: Buffer,
22
- ) {
23
- const value = toBigIntBE(buffer);
24
- if (value > AztecAddress.MAX_VALUE) {
25
- throw new Error(`AztecAddress out of range ${value}.`);
10
+ export class AztecAddress extends Fr {
11
+ constructor(buffer: Buffer) {
12
+ if (buffer.length !== 32) {
13
+ throw new Error(`Invalid length ${buffer.length}.`);
26
14
  }
15
+ super(buffer);
27
16
  }
28
17
 
29
- /**
30
- * Generates a random AztecAddress instance, using the Fr field (a finite field) to create a random value
31
- * within a valid range and then converting it into a Buffer of a fixed size in bytes.
32
- *
33
- * @returns A new AztecAddress instance with a random value.
34
- */
35
- static random() {
36
- return new AztecAddress(toBufferBE(Fr.random().value, AztecAddress.SIZE_IN_BYTES));
37
- }
38
-
39
- /**
40
- * Creates an AztecAddress instance from a given buffer or BufferReader.
41
- * If the input is a Buffer, it wraps it in a BufferReader before processing.
42
- * Throws an error if the input length is not equal to the expected size.
43
- *
44
- * @param buffer - The input buffer or BufferReader containing the address data.
45
- * @returns - A new AztecAddress instance with the extracted address data.
46
- */
47
- static fromBuffer(buffer: Buffer | BufferReader) {
48
- const reader = BufferReader.asReader(buffer);
49
- return new this(reader.readBytes(this.SIZE_IN_BYTES));
50
- }
51
-
52
- /**
53
- * Create an AztecAddress instance from a hex-encoded string.
54
- * The input 'address' should be prefixed with '0x' or not, and have exactly 64 hex characters.
55
- * Throws an error if the input length is invalid or address value is out of range.
56
- *
57
- * @param address - The hex-encoded string representing the Aztec address.
58
- * @returns An AztecAddress instance.
59
- */
60
- static fromString(address: string) {
61
- const buf = Buffer.from(address.replace(/^0x/i, ''), 'hex');
62
- if (buf.length !== AztecAddress.SIZE_IN_BYTES) {
63
- throw new Error(`Invalid length ${buf.length}.`);
64
- }
65
- return new AztecAddress(buf);
66
- }
67
-
68
- /**
69
- * Creates an AztecAddress from a bigint.
70
- * The provided value must be within the range of a field.
71
- * @param address - The bigint representation of the address.
72
- * @returns An AztecAddress instance.
73
- */
74
- static fromBigInt(address: bigint) {
75
- return new AztecAddress(toBufferBE(address, AztecAddress.SIZE_IN_BYTES));
76
- }
77
-
78
- /**
79
- * Converts the AztecAddress instance into a Buffer.
80
- * This method should be used when encoding the address for storage, transmission or serialization purposes.
81
- *
82
- * @returns A Buffer representation of the AztecAddress instance.
83
- */
84
- toBuffer() {
85
- return this.buffer;
86
- }
87
-
88
- /**
89
- * Convert the AztecAddress to a hexadecimal string representation, with a "0x" prefix.
90
- * The resulting string will have a length of 66 characters (including the prefix).
91
- *
92
- * @returns A hexadecimal string representation of the AztecAddress.
93
- */
94
- toString(): `0x${string}` {
95
- return `0x${this.buffer.toString('hex')}`;
96
- }
97
-
98
- /**
99
- * Returns a shortened string representation of the AztecAddress, displaying only the first 10 characters and last 4 characters.
100
- * This is useful for human-readable displays where the full address is not necessary.
101
- *
102
- * @returns A shortened string representation of the address.
103
- */
104
- toShortString() {
105
- const str = this.toString();
106
- return `${str.slice(0, 10)}...${str.slice(-4)}`;
107
- }
108
-
109
- /**
110
- * Returns this address from a Field element.
111
- * @param field - The Field element to convert.
112
- * @returns An Address Object from a Field element with the same value.
113
- */
114
- static fromField(field: Fr): AztecAddress {
115
- return new AztecAddress(toBufferBE(field.value, AztecAddress.SIZE_IN_BYTES));
116
- }
117
-
118
- /**
119
- * Returns this address as a field element.
120
- * @returns A field element with the same value as the address.
121
- */
122
- toField() {
123
- return Fr.fromBuffer(this.toBuffer());
124
- }
125
-
126
- /**
127
- * Returns this address as a bigint. Useful for creating maps indexed by addresses.
128
- * @returns A bigint with the same value as the address.
129
- */
130
- toBigInt() {
131
- return toBigIntBE(this.buffer);
132
- }
133
-
134
- /**
135
- * Determines if this AztecAddress instance is equal to the given AztecAddress instance.
136
- * Equality is based on the content of their respective buffers.
137
- *
138
- * @param other - The AztecAddress instance to compare against.
139
- * @returns True if the buffers of both instances are equal, false otherwise.
140
- */
141
- equals(other: AztecAddress) {
142
- return this.buffer.equals(other.buffer);
18
+ static fromField(fr: Fr) {
19
+ return new AztecAddress(fr.toBuffer());
143
20
  }
144
21
 
145
- /**
146
- * Checks if the AztecAddress is zero.
147
- *
148
- * @returns Returns true if the AztecAddress is equal to the zero address, otherwise returns false.
149
- */
150
- isZero() {
151
- return this.equals(AztecAddress.ZERO);
22
+ static fromBigInt(value: bigint) {
23
+ return AztecAddress.fromField(new Fr(value));
152
24
  }
153
25
 
154
- /**
155
- * Friendly representation for debugging purposes.
156
- *
157
- * @returns A hex string representing the address.
158
- */
159
- toFriendlyJSON() {
160
- return this.toString();
26
+ static fromString(buf: string) {
27
+ const buffer = Buffer.from(buf.replace(/^0x/i, ''), 'hex');
28
+ return new AztecAddress(buffer);
161
29
  }
162
30
  }
@@ -33,7 +33,9 @@ export function toBigIntBE(buf: Buffer): bigint {
33
33
  * @returns A little-endian buffer representation of num.
34
34
  */
35
35
  export function toBufferLE(num: bigint, width: number): Buffer {
36
- if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`);
36
+ if (num < BigInt(0)) {
37
+ throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`);
38
+ }
37
39
  const hex = num.toString(16);
38
40
  const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
39
41
  buffer.reverse();
@@ -47,10 +49,14 @@ export function toBufferLE(num: bigint, width: number): Buffer {
47
49
  * @returns A big-endian buffer representation of num.
48
50
  */
49
51
  export function toBufferBE(num: bigint, width: number): Buffer {
50
- if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`);
52
+ if (num < BigInt(0)) {
53
+ throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`);
54
+ }
51
55
  const hex = num.toString(16);
52
56
  const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
53
- if (buffer.length > width) throw new Error(`Number ${num.toString(16)} does not fit in ${width}`);
57
+ if (buffer.length > width) {
58
+ throw new Error(`Number ${num.toString(16)} does not fit in ${width}`);
59
+ }
54
60
  return buffer;
55
61
  }
56
62
 
@@ -8,7 +8,9 @@ import { Tuple } from '../serialize/types.js';
8
8
  * @returns A new padded array.
9
9
  */
10
10
  export function padArrayEnd<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
11
- if (arr.length > length) throw new Error(`Array size exceeds target length`);
11
+ if (arr.length > length) {
12
+ throw new Error(`Array size exceeds target length`);
13
+ }
12
14
  // Since typescript cannot always deduce that something is a tuple, we cast
13
15
  return [...arr, ...Array(length - arr.length).fill(elem)] as Tuple<T, N>;
14
16
  }
@@ -21,7 +23,9 @@ export function padArrayEnd<T, N extends number>(arr: T[], elem: T, length: N):
21
23
  * @returns A new padded array.
22
24
  */
23
25
  export function padArrayStart<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
24
- if (arr.length > length) throw new Error(`Array size exceeds target length`);
26
+ if (arr.length > length) {
27
+ throw new Error(`Array size exceeds target length`);
28
+ }
25
29
  // Since typescript cannot always deduce that something is a tuple, we cast
26
30
  return [...Array(length - arr.length).fill(elem), ...arr] as Tuple<T, N>;
27
31
  }
@@ -33,7 +37,9 @@ export function padArrayStart<T, N extends number>(arr: T[], elem: T, length: N)
33
37
  */
34
38
  export function isArrayEmpty<T>(arr: T[], isEmpty: (item: T) => boolean): boolean {
35
39
  for (const item of arr) {
36
- if (!isEmpty(item)) return false;
40
+ if (!isEmpty(item)) {
41
+ return false;
42
+ }
37
43
  }
38
44
  return true;
39
45
  }
@@ -1,6 +1,8 @@
1
- import { Pedersen } from '@aztec/bb.js';
1
+ import { BarretenbergSync, Fr } from '@aztec/bb.js';
2
2
 
3
- const pedersen = await Pedersen.new();
3
+ // Get the singleton. This constructs (if not already) the barretenberg sync api within bb.js itself.
4
+ // This can be called from multiple other modules as needed, and it ensures it's only constructed once.
5
+ const api = await BarretenbergSync.getSingleton();
4
6
 
5
7
  /**
6
8
  * Create a pedersen commitment (point) from an array of input fields.
@@ -11,8 +13,10 @@ export function pedersenCommit(input: Buffer[]) {
11
13
  throw new Error('All input buffers must be <= 32 bytes.');
12
14
  }
13
15
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
14
- const [x, y] = pedersen.pedersenCommit(input);
15
- return [Buffer.from(x), Buffer.from(y)];
16
+ const point = api.pedersenCommit(input.map(i => new Fr(i)));
17
+ // toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
18
+ // TODO: rename toTypedArray()?
19
+ return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
16
20
  }
17
21
 
18
22
  /**
@@ -24,5 +28,19 @@ export function pedersenHash(input: Buffer[], index = 0) {
24
28
  throw new Error('All input buffers must be <= 32 bytes.');
25
29
  }
26
30
  input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
27
- return Buffer.from(pedersen.pedersenHash(input, index));
31
+ return Buffer.from(
32
+ api
33
+ .pedersenHash(
34
+ input.map(i => new Fr(i)),
35
+ index,
36
+ )
37
+ .toBuffer(),
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Create a pedersen hash from an arbitrary length buffer.
43
+ */
44
+ export function pedersenHashBuffer(input: Buffer, index = 0) {
45
+ return Buffer.from(api.pedersenHashBuffer(input, index).toBuffer());
28
46
  }
@@ -6,8 +6,12 @@ import isNode from 'detect-node';
6
6
  const MAX_BYTES = 65536;
7
7
 
8
8
  const getWebCrypto = () => {
9
- if (typeof window !== 'undefined' && window.crypto) return window.crypto;
10
- if (typeof self !== 'undefined' && self.crypto) return self.crypto;
9
+ if (typeof window !== 'undefined' && window.crypto) {
10
+ return window.crypto;
11
+ }
12
+ if (typeof self !== 'undefined' && self.crypto) {
13
+ return self.crypto;
14
+ }
11
15
  return undefined;
12
16
  };
13
17
 
@@ -1,16 +1,3 @@
1
1
  import { default as hash } from 'hash.js';
2
2
 
3
- import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
4
- import { Fr } from '../../fields/fields.js';
5
-
6
3
  export const sha256 = (data: Buffer) => Buffer.from(hash.sha256().update(data).digest());
7
-
8
- /**
9
- * Squashes the output of sha256 into a field element.
10
- * WARNING: if you have not thought about why you are using this, or talked to somebody who has do not use it.
11
- * @param buf - Input buffer
12
- * @returns Returns a sha256 output squashed into a field element.
13
- */
14
- export const sha256ToField = (buf: Buffer): Fr => {
15
- return Fr.fromBuffer(toBufferBE(toBigIntBE(sha256(buf)) % Fr.MODULUS, 32));
16
- };