-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: registering PublicDataWitness in JsonRpcServer #6243
Merged
benesjan
merged 4 commits into
master
from
05-07-fix_registering_publicdatawitness_in_jsonrpcserver
May 7, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the fixed num bytes here ^ is shit but SiblingPath serialization seems to be incompatible with the methods on reader and I've added a test which serializes and deserializes the witness so if there is some serialization change it should catch it.