-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
yarn-project/circuit-types/src/logs/l1_payload/encryption_util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { type GrumpkinScalar, type PublicKey } from '@aztec/circuits.js'; | ||
import { Aes128 } from '@aztec/circuits.js/barretenberg'; | ||
|
||
import { deriveDiffieHellmanAESSecret } 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: Buffer, | ||
secret: GrumpkinScalar, | ||
publicKey: PublicKey, | ||
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveDiffieHellmanAESSecret, | ||
): Buffer { | ||
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: Buffer, | ||
secret: GrumpkinScalar, | ||
publicKey: PublicKey, | ||
deriveSecret: (secret: GrumpkinScalar, publicKey: PublicKey) => Buffer = deriveDiffieHellmanAESSecret, | ||
): Buffer { | ||
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); | ||
} |