-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: registering PublicDataWitness in JsonRpcServer (#6243)
- Loading branch information
Showing
7 changed files
with
114 additions
and
44 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
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
42 changes: 0 additions & 42 deletions
42
yarn-project/circuit-types/src/interfaces/public_data_tree.ts
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
yarn-project/circuit-types/src/public_data_witness.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,31 @@ | ||
import { PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; | ||
import { fr } from '@aztec/circuits.js/testing'; | ||
import { toBufferBE } from '@aztec/foundation/bigint-buffer'; | ||
import { randomInt } from '@aztec/foundation/crypto'; | ||
|
||
import { PublicDataWitness } from './public_data_witness.js'; | ||
import { SiblingPath } from './sibling_path/sibling_path.js'; | ||
|
||
describe('contract_artifact', () => { | ||
it('serializes and deserializes an instance', () => { | ||
const witness = makePublicDataWitness(randomInt(1000000)); | ||
|
||
const deserialized = PublicDataWitness.fromBuffer(witness.toBuffer()); | ||
expect(deserialized).toEqual(witness); | ||
}); | ||
}); | ||
|
||
/** | ||
* Factory function to create a PublicDataWitness based on given seed. | ||
* @param seed - A seed used to derive all parameters. | ||
* @returns A new instance of PublicDataWitness. | ||
*/ | ||
function makePublicDataWitness(seed: number): PublicDataWitness { | ||
const leafPreimage = new PublicDataTreeLeafPreimage(fr(seed + 1), fr(seed + 2), fr(seed + 3), BigInt(seed + 4)); | ||
const siblingPath = new SiblingPath( | ||
PUBLIC_DATA_TREE_HEIGHT, | ||
Array.from({ length: PUBLIC_DATA_TREE_HEIGHT }, (_, i) => toBufferBE(BigInt(seed + i + 6), 32)), | ||
); | ||
|
||
return new PublicDataWitness(BigInt(seed + 5), leafPreimage, siblingPath); | ||
} |
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,79 @@ | ||
import { Fr, PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage } from '@aztec/circuits.js'; | ||
import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; | ||
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; | ||
|
||
import { SiblingPath } from './sibling_path/sibling_path.js'; | ||
|
||
/** | ||
* Public data witness. | ||
* @remarks This allows to prove either: | ||
* - That a slot in the public data tree is empty (0 value) if it falls within the range of the leaf. | ||
* - The current value of a slot in the public data tree if it matches exactly the slot of the leaf. | ||
*/ | ||
export class PublicDataWitness { | ||
constructor( | ||
/** | ||
* The index of the leaf in the public data tree. | ||
*/ | ||
public readonly index: bigint, | ||
/** | ||
* Preimage of a low leaf. All the slots in the range of the leaf are empty, and the current value of the | ||
* leaf slot is stored in the leaf. | ||
*/ | ||
public readonly leafPreimage: PublicDataTreeLeafPreimage, | ||
/** | ||
* Sibling path to prove membership of the leaf. | ||
*/ | ||
public readonly siblingPath: SiblingPath<typeof PUBLIC_DATA_TREE_HEIGHT>, | ||
) {} | ||
|
||
/** | ||
* Returns a field array representation of a public data witness. | ||
* @returns A field array representation of a public data witness. | ||
*/ | ||
public toFields(): Fr[] { | ||
return [ | ||
new Fr(this.index), | ||
new Fr(this.leafPreimage.slot), | ||
new Fr(this.leafPreimage.value), | ||
new Fr(this.leafPreimage.nextIndex), | ||
new Fr(this.leafPreimage.nextSlot), | ||
...this.siblingPath.toFields(), | ||
]; | ||
} | ||
|
||
toBuffer(): Buffer { | ||
return serializeToBuffer([this.index, this.leafPreimage, this.siblingPath]); | ||
} | ||
|
||
/** | ||
* Returns a string representation of the TxEffect object. | ||
*/ | ||
toString(): string { | ||
return this.toBuffer().toString('hex'); | ||
} | ||
|
||
/** | ||
* Deserializes an PublicDataWitness object from a buffer. | ||
* @param buf - Buffer or BufferReader to deserialize. | ||
* @returns An instance of PublicDataWitness. | ||
*/ | ||
static fromBuffer(buffer: Buffer | BufferReader): PublicDataWitness { | ||
const reader = BufferReader.asReader(buffer); | ||
|
||
return new PublicDataWitness( | ||
toBigIntBE(reader.readBytes(32)), | ||
reader.readObject(PublicDataTreeLeafPreimage), | ||
SiblingPath.fromBuffer(reader.readBytes(4 + 32 * PUBLIC_DATA_TREE_HEIGHT)), | ||
); | ||
} | ||
|
||
/** | ||
* Deserializes an PublicDataWitness object from a string. | ||
* @param str - String to deserialize. | ||
* @returns An instance of PublicDataWitness. | ||
*/ | ||
static fromString(str: string) { | ||
return PublicDataWitness.fromBuffer(Buffer.from(str, 'hex')); | ||
} | ||
} |