@aztec/stdlib 0.82.3 → 0.83.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.
- package/dest/avm/avm.d.ts +3889 -382
- package/dest/avm/avm.d.ts.map +1 -1
- package/dest/avm/avm.js +64 -18
- package/dest/avm/avm_proving_request.d.ts +1610 -66
- package/dest/avm/avm_proving_request.d.ts.map +1 -1
- package/dest/block/l2_block_downloader/l2_block_stream.d.ts +9 -12
- package/dest/block/l2_block_downloader/l2_block_stream.d.ts.map +1 -1
- package/dest/block/l2_block_downloader/l2_block_stream.js +39 -11
- package/dest/config/config.d.ts +2 -2
- package/dest/config/config.d.ts.map +1 -1
- package/dest/config/config.js +4 -5
- package/dest/contract/interfaces/node-info.d.ts +2 -2
- package/dest/contract/interfaces/node-info.d.ts.map +1 -1
- package/dest/contract/interfaces/node-info.js +1 -1
- package/dest/interfaces/prover-client.d.ts +3 -3
- package/dest/interfaces/prover-client.d.ts.map +1 -1
- package/dest/interfaces/prover-client.js +6 -4
- package/dest/interfaces/proving-job.d.ts +1610 -66
- package/dest/interfaces/proving-job.d.ts.map +1 -1
- package/dest/interfaces/pxe.d.ts +7 -6
- package/dest/interfaces/pxe.d.ts.map +1 -1
- package/dest/interfaces/pxe.js +1 -1
- package/dest/keys/derivation.d.ts +1 -1
- package/dest/keys/derivation.d.ts.map +1 -1
- package/dest/keys/derivation.js +10 -2
- package/dest/logs/index.d.ts +2 -1
- package/dest/logs/index.d.ts.map +1 -1
- package/dest/logs/index.js +2 -1
- package/dest/logs/pending_tagged_log.d.ts +17 -0
- package/dest/logs/pending_tagged_log.d.ts.map +1 -0
- package/dest/logs/pending_tagged_log.js +45 -0
- package/dest/logs/{l1_payload/shared_secret_derivation.d.ts → shared_secret_derivation.d.ts} +4 -3
- package/dest/logs/shared_secret_derivation.d.ts.map +1 -0
- package/dest/logs/{l1_payload/shared_secret_derivation.js → shared_secret_derivation.js} +3 -5
- package/dest/logs/tx_scoped_l2_log.d.ts +6 -1
- package/dest/logs/tx_scoped_l2_log.d.ts.map +1 -1
- package/dest/logs/tx_scoped_l2_log.js +12 -4
- package/dest/note/note.d.ts +45 -4
- package/dest/note/note.d.ts.map +1 -1
- package/dest/note/note.js +51 -4
- package/dest/proofs/proof.d.ts.map +1 -1
- package/dest/proofs/proof.js +33 -7
- package/dest/snapshots/download.js +1 -1
- package/dest/snapshots/types.d.ts +4 -4
- package/dest/snapshots/types.d.ts.map +1 -1
- package/dest/snapshots/types.js +1 -1
- package/dest/snapshots/upload.d.ts +1 -1
- package/dest/snapshots/upload.d.ts.map +1 -1
- package/dest/snapshots/upload.js +1 -1
- package/dest/tests/factories.d.ts +10 -2
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +42 -6
- package/dest/tests/mocks.d.ts +2 -1
- package/dest/tests/mocks.d.ts.map +1 -1
- package/dest/tests/mocks.js +5 -1
- package/dest/trees/nullifier_leaf.d.ts +46 -21
- package/dest/trees/nullifier_leaf.d.ts.map +1 -1
- package/dest/trees/nullifier_leaf.js +48 -30
- package/dest/trees/nullifier_membership_witness.d.ts +28 -12
- package/dest/trees/nullifier_membership_witness.d.ts.map +1 -1
- package/dest/trees/protocol_contract_leaf.d.ts +0 -1
- package/dest/trees/protocol_contract_leaf.d.ts.map +1 -1
- package/dest/trees/protocol_contract_leaf.js +0 -3
- package/dest/trees/public_data_leaf.d.ts +46 -25
- package/dest/trees/public_data_leaf.d.ts.map +1 -1
- package/dest/trees/public_data_leaf.js +35 -30
- package/dest/trees/public_data_witness.d.ts +36 -18
- package/dest/trees/public_data_witness.d.ts.map +1 -1
- package/dest/trees/public_data_witness.js +6 -6
- package/dest/versioning/versioning.d.ts +1 -1
- package/dest/versioning/versioning.d.ts.map +1 -1
- package/dest/versioning/versioning.js +6 -6
- package/package.json +6 -7
- package/src/avm/avm.ts +74 -20
- package/src/block/l2_block_downloader/l2_block_stream.ts +58 -29
- package/src/config/config.ts +6 -6
- package/src/contract/interfaces/node-info.ts +3 -3
- package/src/interfaces/prover-client.ts +9 -7
- package/src/interfaces/pxe.ts +14 -7
- package/src/keys/derivation.ts +12 -6
- package/src/logs/index.ts +2 -1
- package/src/logs/pending_tagged_log.ts +43 -0
- package/src/logs/{l1_payload/shared_secret_derivation.ts → shared_secret_derivation.ts} +4 -11
- package/src/logs/tx_scoped_l2_log.ts +13 -4
- package/src/note/note.ts +61 -5
- package/src/proofs/proof.ts +39 -5
- package/src/snapshots/download.ts +1 -1
- package/src/snapshots/types.ts +2 -2
- package/src/snapshots/upload.ts +5 -3
- package/src/tests/factories.ts +72 -8
- package/src/tests/mocks.ts +7 -0
- package/src/trees/nullifier_leaf.ts +49 -26
- package/src/trees/protocol_contract_leaf.ts +0 -4
- package/src/trees/public_data_leaf.ts +31 -29
- package/src/trees/public_data_witness.ts +6 -6
- package/src/versioning/versioning.ts +8 -14
- package/dest/event/event.d.ts +0 -24
- package/dest/event/event.d.ts.map +0 -1
- package/dest/event/event.js +0 -13
- package/dest/event/event_metadata.d.ts +0 -38
- package/dest/event/event_metadata.d.ts.map +0 -1
- package/dest/event/event_metadata.js +0 -45
- package/dest/event/index.d.ts +0 -4
- package/dest/event/index.d.ts.map +0 -1
- package/dest/event/index.js +0 -3
- package/dest/event/l1_event_payload.d.ts +0 -52
- package/dest/event/l1_event_payload.d.ts.map +0 -1
- package/dest/event/l1_event_payload.js +0 -64
- package/dest/logs/l1_payload/encrypted_log_payload.d.ts +0 -50
- package/dest/logs/l1_payload/encrypted_log_payload.d.ts.map +0 -1
- package/dest/logs/l1_payload/encrypted_log_payload.js +0 -140
- package/dest/logs/l1_payload/encryption_util.d.ts +0 -24
- package/dest/logs/l1_payload/encryption_util.d.ts.map +0 -1
- package/dest/logs/l1_payload/encryption_util.js +0 -46
- package/dest/logs/l1_payload/index.d.ts +0 -3
- package/dest/logs/l1_payload/index.d.ts.map +0 -1
- package/dest/logs/l1_payload/index.js +0 -2
- package/dest/logs/l1_payload/payload.d.ts +0 -60
- package/dest/logs/l1_payload/payload.d.ts.map +0 -1
- package/dest/logs/l1_payload/payload.js +0 -61
- package/dest/logs/l1_payload/shared_secret_derivation.d.ts.map +0 -1
- package/src/event/event.ts +0 -16
- package/src/event/event_metadata.ts +0 -56
- package/src/event/index.ts +0 -3
- package/src/event/l1_event_payload.ts +0 -87
- package/src/logs/l1_payload/encrypted_log_payload.ts +0 -202
- package/src/logs/l1_payload/encryption_util.ts +0 -54
- package/src/logs/l1_payload/index.ts +0 -2
- package/src/logs/l1_payload/payload.ts +0 -73
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import { PRIVATE_LOG_SIZE_IN_FIELDS } from '@aztec/constants';
|
|
2
|
-
import { randomBytes } from '@aztec/foundation/crypto';
|
|
3
|
-
import { Fr, type GrumpkinScalar, NotOnCurveError, Point } from '@aztec/foundation/fields';
|
|
4
|
-
import { BufferReader, type Tuple, numToUInt16BE, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
|
-
|
|
6
|
-
import { AztecAddress } from '../../aztec-address/index.js';
|
|
7
|
-
import { derivePublicKeyFromSecretKey } from '../../keys/derivation.js';
|
|
8
|
-
import { PrivateLog } from '../private_log.js';
|
|
9
|
-
import {
|
|
10
|
-
aes128Decrypt,
|
|
11
|
-
aes128Encrypt,
|
|
12
|
-
deriveAesSymmetricKeyAndIvFromEcdhSharedSecretUsingSha256,
|
|
13
|
-
} from './encryption_util.js';
|
|
14
|
-
import { deriveEcdhSharedSecret, deriveEcdhSharedSecretUsingAztecAddress } from './shared_secret_derivation.js';
|
|
15
|
-
|
|
16
|
-
// Below constants should match the values defined in aztec-nr/aztec/src/encrypted_logs/log_assembly_strategies/default_aes128/note.nr.
|
|
17
|
-
// Note: we will soon be 'abstracting' log processing: apps will process their own logs, instead of the PXE processing all apps' logs. Therefore, this file will imminently change considerably.
|
|
18
|
-
|
|
19
|
-
const TAG_SIZE_IN_FIELDS = 1;
|
|
20
|
-
const EPK_SIZE_IN_FIELDS = 1;
|
|
21
|
-
|
|
22
|
-
const USABLE_PRIVATE_LOG_SIZE_IN_FIELDS = PRIVATE_LOG_SIZE_IN_FIELDS - TAG_SIZE_IN_FIELDS - EPK_SIZE_IN_FIELDS;
|
|
23
|
-
const USABLE_PRIVATE_LOG_SIZE_IN_BYTES = ((USABLE_PRIVATE_LOG_SIZE_IN_FIELDS * 31) / 16) * 16;
|
|
24
|
-
|
|
25
|
-
// The incoming header ciphertext is 48 bytes
|
|
26
|
-
// 32 bytes for the address, and 16 bytes padding to follow PKCS#7
|
|
27
|
-
const HEADER_CIPHERTEXT_SIZE_IN_BYTES = 48;
|
|
28
|
-
const USABLE_PLAINTEXT_SIZE_IN_BYTES = USABLE_PRIVATE_LOG_SIZE_IN_BYTES - HEADER_CIPHERTEXT_SIZE_IN_BYTES;
|
|
29
|
-
|
|
30
|
-
const CONTRACT_ADDRESS_SIZE_IN_BYTES = 32;
|
|
31
|
-
|
|
32
|
-
const SIZE_OF_ENCODING_OF_CIPHERTEXT_SIZE_IN_BYTES = 2;
|
|
33
|
-
|
|
34
|
-
function beBytes31ToFields(bytes: Buffer): Fr[] {
|
|
35
|
-
const fields = [];
|
|
36
|
-
const numFields = Math.ceil(bytes.length / 31);
|
|
37
|
-
for (let i = 0; i < numFields; i++) {
|
|
38
|
-
fields.push(new Fr(bytes.subarray(i * 31, (i + 1) * 31)));
|
|
39
|
-
}
|
|
40
|
-
return fields;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function fieldsToBEBytes31(fields: Fr[]) {
|
|
44
|
-
return Buffer.concat(fields.map(f => f.toBuffer().subarray(1)));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Encrypted log payload with a tag used for retrieval by clients.
|
|
49
|
-
*/
|
|
50
|
-
export class EncryptedLogPayload {
|
|
51
|
-
constructor(
|
|
52
|
-
/**
|
|
53
|
-
* Note discovery tag.
|
|
54
|
-
*/
|
|
55
|
-
public readonly tag: Fr,
|
|
56
|
-
/**
|
|
57
|
-
* Address of a contract that emitted the log.
|
|
58
|
-
*/
|
|
59
|
-
public readonly contractAddress: AztecAddress,
|
|
60
|
-
/**
|
|
61
|
-
* Decrypted incoming body.
|
|
62
|
-
*/
|
|
63
|
-
public readonly incomingBodyPlaintext: Buffer,
|
|
64
|
-
) {}
|
|
65
|
-
|
|
66
|
-
// NB: Only appears to be used in tests
|
|
67
|
-
// See noir-projects/aztec-nr/aztec/src/encrypted_logs/log_assembly_strategies/default_aes128/note.nr
|
|
68
|
-
public async generatePayload(
|
|
69
|
-
ephSk: GrumpkinScalar,
|
|
70
|
-
recipient: AztecAddress,
|
|
71
|
-
rand: (len: number) => Buffer = randomBytes,
|
|
72
|
-
): Promise<PrivateLog> {
|
|
73
|
-
const ephPk = await derivePublicKeyFromSecretKey(ephSk);
|
|
74
|
-
const [ephPkX, ephPkSignBool] = ephPk.toXAndSign();
|
|
75
|
-
const ephPkSignU8 = Buffer.from([Number(ephPkSignBool)]);
|
|
76
|
-
|
|
77
|
-
const ciphertextSharedSecret = await deriveEcdhSharedSecretUsingAztecAddress(ephSk, recipient); // not to be confused with the tagging shared secret
|
|
78
|
-
|
|
79
|
-
const [symKey, iv] = deriveAesSymmetricKeyAndIvFromEcdhSharedSecretUsingSha256(ciphertextSharedSecret);
|
|
80
|
-
|
|
81
|
-
if (this.incomingBodyPlaintext.length > USABLE_PLAINTEXT_SIZE_IN_BYTES) {
|
|
82
|
-
throw new Error(`Incoming body plaintext cannot be more than ${USABLE_PLAINTEXT_SIZE_IN_BYTES} bytes.`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const finalPlaintext = this.incomingBodyPlaintext;
|
|
86
|
-
|
|
87
|
-
const ciphertextBytes = await aes128Encrypt(finalPlaintext, iv, symKey);
|
|
88
|
-
|
|
89
|
-
const headerPlaintext = serializeToBuffer(this.contractAddress.toBuffer(), numToUInt16BE(ciphertextBytes.length));
|
|
90
|
-
|
|
91
|
-
// TODO: it is unsafe to re-use the same iv and symKey. We'll need to do something cleverer.
|
|
92
|
-
const headerCiphertextBytes = await aes128Encrypt(headerPlaintext, iv, symKey);
|
|
93
|
-
|
|
94
|
-
if (headerCiphertextBytes.length !== HEADER_CIPHERTEXT_SIZE_IN_BYTES) {
|
|
95
|
-
throw new Error(`Invalid header ciphertext size: ${headerCiphertextBytes.length}`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const properLogBytesLength = 1 /* ephPkSignU8 */ + HEADER_CIPHERTEXT_SIZE_IN_BYTES + ciphertextBytes.length;
|
|
99
|
-
|
|
100
|
-
const logBytesPaddingToMult31 = rand(31 * Math.ceil(properLogBytesLength / 31) - properLogBytesLength);
|
|
101
|
-
|
|
102
|
-
const logBytes = serializeToBuffer(ephPkSignU8, headerCiphertextBytes, ciphertextBytes, logBytesPaddingToMult31);
|
|
103
|
-
|
|
104
|
-
if (logBytes.length % 31 !== 0) {
|
|
105
|
-
throw new Error(`logBytes.length should be divisible by 31, got: ${logBytes.length}`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const fieldsPadding = Array.from({ length: USABLE_PRIVATE_LOG_SIZE_IN_FIELDS - logBytes.length / 31 }, () =>
|
|
109
|
-
Fr.fromBuffer(rand(32)),
|
|
110
|
-
); // we use the randomBytes function instead of `Fr.random()`, so that we can use deterministic randomness in tests, through the rand() function.
|
|
111
|
-
|
|
112
|
-
const logFields = [this.tag, ephPkX, ...beBytes31ToFields(logBytes), ...fieldsPadding] as Tuple<
|
|
113
|
-
Fr,
|
|
114
|
-
typeof PRIVATE_LOG_SIZE_IN_FIELDS
|
|
115
|
-
>;
|
|
116
|
-
|
|
117
|
-
if (logFields.length !== PRIVATE_LOG_SIZE_IN_FIELDS) {
|
|
118
|
-
throw new Error(
|
|
119
|
-
`Expected private log payload to have ${PRIVATE_LOG_SIZE_IN_FIELDS} fields. Got ${logFields.length}.`,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return new PrivateLog(logFields);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Decrypts a ciphertext as an incoming log.
|
|
128
|
-
*
|
|
129
|
-
* This is executable by the recipient of the note, and uses the addressSecret to decrypt the payload.
|
|
130
|
-
*
|
|
131
|
-
* @param payload - The payload for the log
|
|
132
|
-
* @param addressSecret - The address secret, used to decrypt the logs
|
|
133
|
-
* @param ciphertextLength - Optionally supply the ciphertext length (see trimCiphertext())
|
|
134
|
-
* @returns The decrypted log payload
|
|
135
|
-
*/
|
|
136
|
-
public static async decryptAsIncoming(
|
|
137
|
-
payload: Fr[],
|
|
138
|
-
addressSecret: GrumpkinScalar,
|
|
139
|
-
): Promise<EncryptedLogPayload | undefined> {
|
|
140
|
-
try {
|
|
141
|
-
const logFields = payload;
|
|
142
|
-
|
|
143
|
-
const tag = logFields[0];
|
|
144
|
-
const ephPkX = logFields[1];
|
|
145
|
-
|
|
146
|
-
const reader = BufferReader.asReader(fieldsToBEBytes31(logFields.slice(TAG_SIZE_IN_FIELDS + EPK_SIZE_IN_FIELDS)));
|
|
147
|
-
|
|
148
|
-
const ephPkSigBuf = reader.readBytes(1);
|
|
149
|
-
const ephPkSignBool = !!ephPkSigBuf[0];
|
|
150
|
-
const ephPk = await Point.fromXAndSign(ephPkX, ephPkSignBool);
|
|
151
|
-
|
|
152
|
-
const headerCiphertextBytes = reader.readBytes(HEADER_CIPHERTEXT_SIZE_IN_BYTES);
|
|
153
|
-
|
|
154
|
-
let contractAddress = AztecAddress.ZERO;
|
|
155
|
-
if (!addressSecret) {
|
|
156
|
-
throw new Error('Cannot decrypt without an address secret.');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const ciphertextSharedSecret = await deriveEcdhSharedSecret(addressSecret, ephPk);
|
|
160
|
-
|
|
161
|
-
const [symKey, iv] = deriveAesSymmetricKeyAndIvFromEcdhSharedSecretUsingSha256(ciphertextSharedSecret);
|
|
162
|
-
|
|
163
|
-
const headerPlaintextBytes = await aes128Decrypt(headerCiphertextBytes, iv, symKey);
|
|
164
|
-
|
|
165
|
-
const headerReader = BufferReader.asReader(headerPlaintextBytes);
|
|
166
|
-
|
|
167
|
-
const contractAddressBuf = headerReader.readBytes(CONTRACT_ADDRESS_SIZE_IN_BYTES);
|
|
168
|
-
contractAddress = AztecAddress.fromBuffer(contractAddressBuf);
|
|
169
|
-
|
|
170
|
-
const ciphertextBytesLengthBuf = headerReader.readBytes(SIZE_OF_ENCODING_OF_CIPHERTEXT_SIZE_IN_BYTES);
|
|
171
|
-
const ciphertextBytesLength = (ciphertextBytesLengthBuf[0] << 8) + ciphertextBytesLengthBuf[1];
|
|
172
|
-
|
|
173
|
-
const ciphertextBytes = reader.readBytes(ciphertextBytesLength);
|
|
174
|
-
|
|
175
|
-
const plaintextBytes = await aes128Decrypt(ciphertextBytes, iv, symKey);
|
|
176
|
-
|
|
177
|
-
return new EncryptedLogPayload(tag, contractAddress, plaintextBytes);
|
|
178
|
-
} catch (e: any) {
|
|
179
|
-
// Following error messages are expected to occur when decryption fails
|
|
180
|
-
if (!this.isAcceptableError(e)) {
|
|
181
|
-
// If we encounter an unexpected error, we rethrow it
|
|
182
|
-
throw e;
|
|
183
|
-
}
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private static isAcceptableError(e: any) {
|
|
189
|
-
return (
|
|
190
|
-
e instanceof NotOnCurveError ||
|
|
191
|
-
e.message.endsWith('is greater or equal to field modulus.') ||
|
|
192
|
-
e.message.startsWith('Invalid AztecAddress length') ||
|
|
193
|
-
e.message.startsWith('Selector must fit in') ||
|
|
194
|
-
e.message.startsWith('Attempted to read beyond buffer length') ||
|
|
195
|
-
e.message.startsWith('RangeError [ERR_BUFFER_OUT_OF_BOUNDS]:')
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
public toBuffer() {
|
|
200
|
-
return serializeToBuffer(this.tag, this.contractAddress.toBuffer(), this.incomingBodyPlaintext);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { GeneratorIndex } from '@aztec/constants';
|
|
2
|
-
import { Aes128, sha256 } from '@aztec/foundation/crypto';
|
|
3
|
-
import type { Point } from '@aztec/foundation/fields';
|
|
4
|
-
import { numToUInt8 } from '@aztec/foundation/serialize';
|
|
5
|
-
|
|
6
|
-
function extractCloseToUniformlyRandom256BitsFromEcdhSharedSecretUsingSha256(sharedSecret: Point): Buffer {
|
|
7
|
-
const secretBuffer = Buffer.concat([sharedSecret.toCompressedBuffer(), numToUInt8(GeneratorIndex.SYMMETRIC_KEY)]);
|
|
8
|
-
const hash = sha256(secretBuffer);
|
|
9
|
-
return hash;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function deriveAesSymmetricKeyAndIvFromEcdhSharedSecret(
|
|
13
|
-
sharedSecret: Point,
|
|
14
|
-
randomnessExtractionFunction: (sharedSecret: Point) => Buffer,
|
|
15
|
-
): [Buffer, Buffer] {
|
|
16
|
-
const random256Bits = randomnessExtractionFunction(sharedSecret);
|
|
17
|
-
const symKey = random256Bits.subarray(0, 16);
|
|
18
|
-
const iv = random256Bits.subarray(16, 32);
|
|
19
|
-
return [symKey, iv];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function deriveAesSymmetricKeyAndIvFromEcdhSharedSecretUsingSha256(sharedSecret: Point): [Buffer, Buffer] {
|
|
23
|
-
return deriveAesSymmetricKeyAndIvFromEcdhSharedSecret(
|
|
24
|
-
sharedSecret,
|
|
25
|
-
extractCloseToUniformlyRandom256BitsFromEcdhSharedSecretUsingSha256,
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Encrypts the plaintext using the secret key and public key
|
|
31
|
-
*
|
|
32
|
-
* @param plaintext - The plaintext buffer
|
|
33
|
-
* @param secret - The secret key used to derive the AES secret
|
|
34
|
-
* @param publicKey - Public key used to derived the AES secret
|
|
35
|
-
* @param deriveSecret - Function to derive the AES secret from the ephemeral secret key and public key
|
|
36
|
-
* @returns The ciphertext
|
|
37
|
-
*/
|
|
38
|
-
export function aes128Encrypt(plaintext: Buffer, iv: Buffer, symKey: Buffer): Promise<Buffer> {
|
|
39
|
-
const aes128 = new Aes128();
|
|
40
|
-
return aes128.encryptBufferCBC(plaintext, iv, symKey);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Decrypts the ciphertext using the secret key and public key
|
|
45
|
-
* @param ciphertext - The ciphertext buffer
|
|
46
|
-
* @param secret - The secret key used to derive the AES secret
|
|
47
|
-
* @param publicKey - The public key used to derive the AES secret
|
|
48
|
-
* @param deriveSecret - Function to derive the AES secret from the ephemeral secret key and public key
|
|
49
|
-
* @returns
|
|
50
|
-
*/
|
|
51
|
-
export function aes128Decrypt(ciphertext: Buffer, iv: Buffer, symKey: Buffer): Promise<Buffer> {
|
|
52
|
-
const aes128 = new Aes128();
|
|
53
|
-
return aes128.decryptBufferCBC(ciphertext, iv, symKey);
|
|
54
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { randomInt } from '@aztec/foundation/crypto';
|
|
2
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
4
|
-
import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
|
|
5
|
-
|
|
6
|
-
import { schemas } from '../../schemas/index.js';
|
|
7
|
-
import { Vector } from '../../types/index.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The Note class represents a Note emitted from a Noir contract as a vector of Fr (finite field) elements.
|
|
11
|
-
* This data also represents a preimage to a note hash. This class extends the Vector class, which allows for
|
|
12
|
-
* additional operations on the underlying field elements.
|
|
13
|
-
*/
|
|
14
|
-
export class Payload extends Vector<Fr> {
|
|
15
|
-
toJSON() {
|
|
16
|
-
return this.toBuffer();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
static get schema() {
|
|
20
|
-
return schemas.Buffer.transform(Payload.fromBuffer);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Create a Note instance from a Buffer or BufferReader.
|
|
25
|
-
* The input 'buffer' can be either a Buffer containing the serialized Fr elements or a BufferReader instance.
|
|
26
|
-
* This function reads the Fr elements in the buffer and constructs a Note with them.
|
|
27
|
-
*
|
|
28
|
-
* @param buffer - The Buffer or BufferReader containing the serialized Fr elements.
|
|
29
|
-
* @returns A Note instance containing the deserialized Fr elements.
|
|
30
|
-
*/
|
|
31
|
-
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
32
|
-
const reader = BufferReader.asReader(buffer);
|
|
33
|
-
return new Payload(reader.readVector(Fr));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Generates a random Note instance with a variable number of items.
|
|
38
|
-
* The number of items is determined by a random value between 1 and 10 (inclusive).
|
|
39
|
-
* Each item in the Note is generated using the Fr.random() method.
|
|
40
|
-
*
|
|
41
|
-
* @returns A randomly generated Note instance.
|
|
42
|
-
*/
|
|
43
|
-
static random() {
|
|
44
|
-
const numItems = randomInt(10) + 1;
|
|
45
|
-
const items = Array.from({ length: numItems }, () => Fr.random());
|
|
46
|
-
return new Payload(items);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Returns a hex representation of the note.
|
|
51
|
-
* @returns A hex string with the vector length as first element.
|
|
52
|
-
*/
|
|
53
|
-
override toString() {
|
|
54
|
-
return bufferToHex(this.toBuffer());
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates a new Note instance from a hex string.
|
|
59
|
-
* @param str - Hex representation.
|
|
60
|
-
* @returns A Note instance.
|
|
61
|
-
*/
|
|
62
|
-
static fromString(str: string) {
|
|
63
|
-
return Payload.fromBuffer(hexToBuffer(str));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get length() {
|
|
67
|
-
return this.items.length;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
equals(other: Payload) {
|
|
71
|
-
return this.items.every((item, index) => item.equals(other.items[index]));
|
|
72
|
-
}
|
|
73
|
-
}
|