@aztec/txe 0.86.0-starknet.1 → 0.87.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.
@@ -1,3 +1,4 @@
1
+ import type { EthAddress } from '@aztec/foundation/eth-address';
1
2
  import { Fr } from '@aztec/foundation/fields';
2
3
  import { hexToBuffer } from '@aztec/foundation/string';
3
4
  import { type ContractArtifact, ContractArtifactSchema } from '@aztec/stdlib/abi';
@@ -59,8 +60,22 @@ export function fromUintBoundedVec(storage: ForeignCallArray, length: ForeignCal
59
60
  return Buffer.concat(boundedStorage.map(str => hexToBuffer(str).slice(-uintByteSize)));
60
61
  }
61
62
 
62
- export function toSingle(obj: Fr | AztecAddress): ForeignCallSingle {
63
- return obj.toString().slice(2);
63
+ // Just like toACVMField in yarn-project/simulator/src/private/acvm/serialize.ts but returns a ForeignCallSingle
64
+ // instead of an ACVMField.
65
+ export function toSingle(
66
+ value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint,
67
+ ): ForeignCallSingle {
68
+ let valueAsField;
69
+ if (Buffer.isBuffer(value)) {
70
+ valueAsField = Fr.fromBuffer(value);
71
+ } else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
72
+ valueAsField = new Fr(value);
73
+ } else if (typeof value === 'string') {
74
+ valueAsField = Fr.fromHexString(value);
75
+ } else {
76
+ valueAsField = value;
77
+ }
78
+ return valueAsField.toString().slice(2);
64
79
  }
65
80
 
66
81
  export function toArray(objs: Fr[]): ForeignCallArray {
@@ -100,6 +115,40 @@ export function arrayToBoundedVec(
100
115
  return [storage, len];
101
116
  }
102
117
 
118
+ /**
119
+ * Converts an array of arrays representing Noir BoundedVec of nested arrays into its Noir serialized form.
120
+ * @param bVecStorage - The array underlying the BoundedVec.
121
+ * @param maxLen - The max length of the BoundedVec (max num of the nested arrays in the BoundedVec).
122
+ * @param nestedArrayLength - The length of the nested arrays (each nested array has to have the same length).
123
+ * @returns Serialized BoundedVec following Noir intrinsic serialization.
124
+ */
125
+ export function arrayOfArraysToBoundedVecOfArrays(
126
+ bVecStorage: ForeignCallArray[],
127
+ maxLen: number,
128
+ nestedArrayLength: number,
129
+ ): [ForeignCallArray, ForeignCallSingle] {
130
+ if (bVecStorage.length > maxLen) {
131
+ throw new Error(`Array of length ${bVecStorage.length} larger than maxLen ${maxLen}`);
132
+ }
133
+
134
+ // Check that all nested arrays have length nestedArrayLength
135
+ if (!bVecStorage.every(nestedArray => nestedArray.length === nestedArrayLength)) {
136
+ throw new Error(
137
+ `Nested array length passed in from Noir does not correspond to the length obtained in TS: ${nestedArrayLength} !== ${bVecStorage[0].length}`,
138
+ );
139
+ }
140
+
141
+ const flattenedStorage = bVecStorage.flat();
142
+
143
+ const numFieldsToPad = maxLen * nestedArrayLength - flattenedStorage.length;
144
+
145
+ const flattenedStorageWithPadding = flattenedStorage.concat(Array(numFieldsToPad).fill(new Fr(0)));
146
+
147
+ // At last we get the actual length of the BoundedVec and return the values.
148
+ const len = toSingle(new Fr(bVecStorage.length));
149
+ return [flattenedStorageWithPadding, len];
150
+ }
151
+
103
152
  export function toForeignCallResult(obj: (ForeignCallSingle | ForeignCallArray)[]) {
104
153
  return { values: obj };
105
154
  }