import { Aes128 } from '@aztec/circuits.js/barretenberg';
import { deriveAESSecret } from './shared_secret_derivation.js';
/**
 * Encrypts the plaintext using the secret key and public key
 *
 * @param plaintext - The plaintext buffer
 * @param secret - The secret key used to derive the AES secret
 * @param publicKey - Public key used to derived the AES secret
 * @param deriveSecret - Function to derive the AES secret from the ephemeral secret key and public key
 * @returns The ciphertext
 */
export function encrypt(plaintext, secret, publicKey, deriveSecret = deriveAESSecret) {
    const aesSecret = deriveSecret(secret, publicKey);
    const key = aesSecret.subarray(0, 16);
    const iv = aesSecret.subarray(16, 32);
    const aes128 = new Aes128();
    return aes128.encryptBufferCBC(plaintext, iv, key);
}
/**
 * Decrypts the ciphertext using the secret key and public key
 * @param ciphertext - The ciphertext buffer
 * @param secret - The secret key used to derive the AES secret
 * @param publicKey - The public key used to derive the AES secret
 * @param deriveSecret - Function to derive the AES secret from the ephemeral secret key and public key
 * @returns
 */
export function decrypt(ciphertext, secret, publicKey, deriveSecret = deriveAESSecret) {
    const aesSecret = deriveSecret(secret, publicKey);
    const key = aesSecret.subarray(0, 16);
    const iv = aesSecret.subarray(16, 32);
    const aes128 = new Aes128();
    return aes128.decryptBufferCBC(ciphertext, iv, key);
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW5jcnlwdGlvbl91dGlsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2xvZ3MvbDFfcGF5bG9hZC9lbmNyeXB0aW9uX3V0aWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLGlDQUFpQyxDQUFDO0FBRXpELE9BQU8sRUFBRSxlQUFlLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUVoRTs7Ozs7Ozs7R0FRRztBQUNILE1BQU0sVUFBVSxPQUFPLENBQ3JCLFNBQWlCLEVBQ2pCLE1BQXNCLEVBQ3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