-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Charlie Lye <[email protected]>
- Loading branch information
1 parent
48544e9
commit 9598d54
Showing
14 changed files
with
131 additions
and
82 deletions.
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
yarn-project/aztec-rpc/src/aztec_rpc_server/note_preimage/index.ts
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
yarn-project/aztec-rpc/src/aztec_rpc_server/note_preimage/note_preimage.test.ts
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
yarn-project/aztec-rpc/src/aztec_rpc_server/note_preimage/note_preimage.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/encrypt_buffer.test.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,34 @@ | ||
import { Grumpkin } from '@aztec/barretenberg.js/crypto'; | ||
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; | ||
import { AztecAddress } from '@aztec/foundation/aztec-address'; | ||
import { randomBytes } from '@aztec/foundation/crypto'; | ||
import { decryptBuffer, encryptBuffer } from './encrypt_buffer.js'; | ||
|
||
describe('encrypt buffer', () => { | ||
it('convert to and from encrypted buffer', async () => { | ||
const grumpkin = new Grumpkin(await BarretenbergWasm.new()); | ||
const data = randomBytes(253); | ||
const ownerPrivKey = randomBytes(32); | ||
const ownerPubKey = AztecAddress.fromBuffer(grumpkin.mul(Grumpkin.generator, ownerPrivKey)); | ||
const ephPrivKey = randomBytes(32); | ||
const encrypted = encryptBuffer(data, ownerPubKey, ephPrivKey, grumpkin); | ||
const decrypted = decryptBuffer(encrypted, ownerPrivKey, grumpkin); | ||
expect(decrypted).not.toBeUndefined(); | ||
expect(decrypted).toEqual(data); | ||
}); | ||
|
||
it('decrypting gibberish returns undefined', async () => { | ||
const grumpkin = new Grumpkin(await BarretenbergWasm.new()); | ||
const data = randomBytes(253); | ||
const ownerPrivKey = randomBytes(32); | ||
const ephPrivKey = randomBytes(32); | ||
const ownerPubKey = AztecAddress.fromBuffer(grumpkin.mul(Grumpkin.generator, ownerPrivKey)); | ||
const encrypted = encryptBuffer(data, ownerPubKey, ephPrivKey, grumpkin); | ||
|
||
// Introduce gibberish. | ||
const gibberish = Buffer.concat([randomBytes(8), encrypted.subarray(8)]); | ||
|
||
const decrypted = decryptBuffer(gibberish, ownerPrivKey, grumpkin); | ||
expect(decrypted).toBeUndefined(); | ||
}); | ||
}); |
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
2 changes: 2 additions & 0 deletions
2
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/index.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,2 @@ | ||
export * from './note_preimage.js'; | ||
export * from './encrypt_buffer.js'; |
11 changes: 11 additions & 0 deletions
11
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/note_preimage.test.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,11 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { NotePreimage } from './note_preimage.js'; | ||
|
||
describe('note_preimage', () => { | ||
it('convert to and from buffer', () => { | ||
const fields = Array.from({ length: 5 }).map(() => Fr.random()); | ||
const notePreImage = new NotePreimage(fields); | ||
const buf = notePreImage.toBuffer(); | ||
expect(NotePreimage.fromBuffer(buf)).toEqual(notePreImage); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/note_preimage.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,10 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { Vector } from '@aztec/circuits.js'; | ||
import { BufferReader } from '@aztec/foundation/serialize'; | ||
|
||
export class NotePreimage extends Vector<Fr> { | ||
static fromBuffer(buffer: Buffer | BufferReader) { | ||
const reader = BufferReader.asReader(buffer); | ||
return new NotePreimage(reader.readVector(Fr)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/tx_aux_data.test.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,34 @@ | ||
import { Grumpkin } from '@aztec/barretenberg.js/crypto'; | ||
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; | ||
import { AztecAddress, randomBytes } from '@aztec/foundation'; | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { NotePreimage } from './note_preimage.js'; | ||
import { TxAuxData } from './tx_aux_data.js'; | ||
|
||
const randomTxAuxData = () => { | ||
const fields = Array.from({ length: 5 }).map(() => Fr.random()); | ||
const notePreImage = new NotePreimage(fields); | ||
const contractAddress = AztecAddress.random(); | ||
const storageSlot = Fr.random(); | ||
return new TxAuxData(notePreImage, contractAddress, storageSlot); | ||
}; | ||
|
||
describe('tx_aux_data', () => { | ||
it('convert to and from buffer', () => { | ||
const txAuxData = randomTxAuxData(); | ||
const buf = txAuxData.toBuffer(); | ||
expect(TxAuxData.fromBuffer(buf)).toEqual(txAuxData); | ||
}); | ||
|
||
it('convert to and from encrypted buffer', async () => { | ||
const grumpkin = new Grumpkin(await BarretenbergWasm.new()); | ||
const txAuxData = randomTxAuxData(); | ||
const ownerPrivKey = randomBytes(32); | ||
const ownerPubKey = AztecAddress.fromBuffer(grumpkin.mul(Grumpkin.generator, ownerPrivKey)); | ||
const ephPrivKey = randomBytes(32); | ||
const encrypted = txAuxData.toEncryptedBuffer(ownerPubKey, ephPrivKey, grumpkin); | ||
const decrypted = TxAuxData.fromEncryptedBuffer(encrypted, ownerPrivKey, grumpkin); | ||
expect(decrypted).not.toBeUndefined(); | ||
expect(decrypted).toEqual(txAuxData); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
yarn-project/aztec-rpc/src/aztec_rpc_server/tx_aux_data/tx_aux_data.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,32 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { AztecAddress } from '@aztec/circuits.js'; | ||
import { BufferReader } from '@aztec/foundation/serialize'; | ||
import { NotePreimage } from './note_preimage.js'; | ||
import { serializeToBuffer } from '@aztec/circuits.js/utils'; | ||
import { decryptBuffer, encryptBuffer } from './encrypt_buffer.js'; | ||
import { Grumpkin } from '@aztec/barretenberg.js/crypto'; | ||
|
||
export class TxAuxData { | ||
constructor(public notePreImage: NotePreimage, public contractAddress: AztecAddress, public storageSlot: Fr) {} | ||
|
||
static fromBuffer(buffer: Buffer | BufferReader) { | ||
const reader = BufferReader.asReader(buffer); | ||
return new TxAuxData(reader.readObject(NotePreimage), reader.readObject(AztecAddress), reader.readFr()); | ||
} | ||
|
||
toBuffer() { | ||
return serializeToBuffer([this.notePreImage, this.contractAddress, this.storageSlot]); | ||
} | ||
|
||
public toEncryptedBuffer(ownerPubKey: AztecAddress, ephPrivKey: Buffer, grumpkin: Grumpkin) { | ||
return encryptBuffer(this.toBuffer(), ownerPubKey, ephPrivKey, grumpkin); | ||
} | ||
|
||
static fromEncryptedBuffer(data: Buffer, ownerPrivKey: Buffer, grumpkin: Grumpkin) { | ||
const buf = decryptBuffer(data, ownerPrivKey, grumpkin); | ||
if (!buf) { | ||
return; | ||
} | ||
return TxAuxData.fromBuffer(buf); | ||
} | ||
} |
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
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
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