forked from visoftsolutions/noir_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:
PublicCircuitPublicInputs
and `PrivateCircuitPublicInputs…
…` cleanup (AztecProtocol#4360) Cleanup of `PublicCircuitPublicInputs` serialization + more tests
- Loading branch information
Showing
10 changed files
with
274 additions
and
302 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
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
21 changes: 21 additions & 0 deletions
21
yarn-project/circuits.js/src/structs/contract_storage_read.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,21 @@ | ||
import { makeContractStorageRead } from '../tests/factories.js'; | ||
import { ContractStorageRead } from './contract_storage_read.js'; | ||
|
||
describe('ContractStorageRead', () => { | ||
it('serializes to buffer and deserializes it back', () => { | ||
const randomInt = Math.floor(Math.random() * 1000); | ||
const expected = makeContractStorageRead(randomInt); | ||
const buffer = expected.toBuffer(); | ||
const res = ContractStorageRead.fromBuffer(buffer); | ||
expect(res).toEqual(expected); | ||
}); | ||
|
||
it('serializes to field array and deserializes it back', () => { | ||
const randomInt = Math.floor(Math.random() * 1000); | ||
const expected = makeContractStorageRead(randomInt); | ||
|
||
const fieldArray = expected.toFields(); | ||
const res = ContractStorageRead.fromFields(fieldArray); | ||
expect(res).toEqual(expected); | ||
}); | ||
}); |
77 changes: 77 additions & 0 deletions
77
yarn-project/circuits.js/src/structs/contract_storage_read.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,77 @@ | ||
import { Fr } from '@aztec/foundation/fields'; | ||
import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize'; | ||
|
||
/** | ||
* Contract storage read operation on a specific contract. | ||
* | ||
* Note: Similar to `PublicDataRead` but it's from the POV of contract storage so we are not working with public data | ||
* tree leaf index but storage slot index. | ||
*/ | ||
export class ContractStorageRead { | ||
constructor( | ||
/** | ||
* Storage slot we are reading from. | ||
*/ | ||
public readonly storageSlot: Fr, | ||
/** | ||
* Value read from the storage slot. | ||
*/ | ||
public readonly currentValue: Fr, | ||
/** | ||
* Optional side effect counter tracking position of this event in tx execution. | ||
* Note: Not serialized | ||
*/ | ||
public readonly sideEffectCounter?: number, | ||
) {} | ||
|
||
static from(args: { | ||
/** | ||
* Storage slot we are reading from. | ||
*/ | ||
storageSlot: Fr; | ||
/** | ||
* Value read from the storage slot. | ||
*/ | ||
currentValue: Fr; | ||
/** | ||
* Optional side effect counter tracking position of this event in tx execution. | ||
*/ | ||
sideEffectCounter?: number; | ||
}) { | ||
return new ContractStorageRead(args.storageSlot, args.currentValue, args.sideEffectCounter); | ||
} | ||
|
||
toBuffer() { | ||
return serializeToBuffer(this.storageSlot, this.currentValue); | ||
} | ||
|
||
static fromBuffer(buffer: Buffer | BufferReader) { | ||
const reader = BufferReader.asReader(buffer); | ||
return new ContractStorageRead(Fr.fromBuffer(reader), Fr.fromBuffer(reader)); | ||
} | ||
|
||
static empty() { | ||
return new ContractStorageRead(Fr.ZERO, Fr.ZERO); | ||
} | ||
|
||
isEmpty() { | ||
return this.storageSlot.isZero() && this.currentValue.isZero(); | ||
} | ||
|
||
toFriendlyJSON() { | ||
return `Slot=${this.storageSlot.toFriendlyJSON()}: ${this.currentValue.toFriendlyJSON()}`; | ||
} | ||
|
||
toFields(): Fr[] { | ||
return [this.storageSlot, this.currentValue]; | ||
} | ||
|
||
static fromFields(fields: Fr[] | FieldReader): ContractStorageRead { | ||
const reader = FieldReader.asReader(fields); | ||
|
||
const storageSlot = reader.readField(); | ||
const currentValue = reader.readField(); | ||
|
||
return new ContractStorageRead(storageSlot, currentValue); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
yarn-project/circuits.js/src/structs/contract_storage_update_request.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,21 @@ | ||
import { makeContractStorageUpdateRequest } from '../tests/factories.js'; | ||
import { ContractStorageUpdateRequest } from './contract_storage_update_request.js'; | ||
|
||
describe('ContractStorageUpdateRequest', () => { | ||
it('serializes to buffer and deserializes it back', () => { | ||
const randomInt = Math.floor(Math.random() * 1000); | ||
const expected = makeContractStorageUpdateRequest(randomInt); | ||
const buffer = expected.toBuffer(); | ||
const res = ContractStorageUpdateRequest.fromBuffer(buffer); | ||
expect(res).toEqual(expected); | ||
}); | ||
|
||
it('serializes to field array and deserializes it back', () => { | ||
const randomInt = Math.floor(Math.random() * 1000); | ||
const expected = makeContractStorageUpdateRequest(randomInt); | ||
|
||
const fieldArray = expected.toFields(); | ||
const res = ContractStorageUpdateRequest.fromFields(fieldArray); | ||
expect(res).toEqual(expected); | ||
}); | ||
}); |
Oops, something went wrong.