Skip to content

Commit

Permalink
feat: AES oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 24, 2024
1 parent b3fdb3b commit 4375f5f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
19 changes: 19 additions & 0 deletions yarn-project/simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventSelector, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { to2Fields } from '@aztec/foundation/serialize';

import { type ACVMField } from '../acvm_types.js';
import { frToBoolean, frToNumber, fromACVMField } from '../deserialize.js';
Expand Down Expand Up @@ -378,4 +379,22 @@ export class Oracle {
);
return toAcvmEnqueuePublicFunctionResult(enqueuedRequest);
}

encrypt([symmetricKey]: ACVMField[], [initializationVector]: ACVMField[], plaintext: ACVMField[]): ACVMField[] {
// Symmetric key and initialization vector (IV) are 16 bytes and we store them as big endian in Fr
const processedSK = fromACVMField(symmetricKey).toBuffer().subarray(0, 16);
const processedIV = fromACVMField(initializationVector).toBuffer().subarray(0, 16);
// TODO(benesjan): we could save some info here by not including the 2 empty bits at the end of each serialized
// field --> this could be valuable as the ciphertext will go on-chain
const processedPlaintext = Buffer.concat(plaintext.map(fromACVMField).map(f => f.toBuffer()));
const ciphertext = this.typedOracle.encrypt(processedSK, processedIV, processedPlaintext);
// Chunk the ciphertext buffer to 32 bytes and on each chunk call to2Fields function
const ciphertextFields: Fr[] = [];
for (let i = 0; i < ciphertext.length; i += Fr.SIZE_IN_BYTES) {
const chunk = ciphertext.subarray(i, i + Fr.SIZE_IN_BYTES);
ciphertextFields.push(...to2Fields(chunk));
}

return ciphertextFields.map(toACVMField);
}
}
4 changes: 4 additions & 0 deletions yarn-project/simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,8 @@ export abstract class TypedOracle {
): Promise<PublicCallRequest> {
throw new OracleMethodNotAvailableError('enqueuePublicFunctionCall');
}

encrypt(_symmetricKey: Buffer, _initializationVector: Buffer, _plaintext: Buffer): Buffer {
throw new OracleMethodNotAvailableError('encrypt');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
type SideEffect,
type TxContext,
} from '@aztec/circuits.js';
import { type Grumpkin } from '@aztec/circuits.js/barretenberg';
import { Aes128, type Grumpkin } from '@aztec/circuits.js/barretenberg';
import { computePublicDataTreeLeafSlot, computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash';
import { type FunctionAbi, type FunctionArtifact, countArgumentsSize } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
Expand Down Expand Up @@ -522,4 +522,9 @@ export class ClientExecutionContext extends ViewDataOracle {
}
return values;
}

public override encrypt(symmetricKey: Buffer, initializationVector: Buffer, plaintext: Buffer): Buffer {
const aes128 = new Aes128();
return aes128.encryptBufferCBC(plaintext, initializationVector, symmetricKey);
}
}

0 comments on commit 4375f5f

Please sign in to comment.