-
Notifications
You must be signed in to change notification settings - Fork 592
chore: AVM TS - move tag validation outside of instruction constructors #13038
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { strict as assert } from 'assert'; | ||
|
|
||
| import { TaggedMemory } from '../avm_memory_types.js'; | ||
| import { BufferCursor } from './buffer_cursor.js'; | ||
|
|
||
| /** | ||
|
|
@@ -103,8 +104,12 @@ export enum OperandType { | |
| UINT64, | ||
| UINT128, | ||
| FF, | ||
| TAG, | ||
| } | ||
|
|
||
| // Define a type that represents the possible types of the deserialized values. | ||
| type DeserializedValue = number | bigint; | ||
|
|
||
| type OperandNativeType = number | bigint; | ||
| type OperandWriter = (value: any) => void; | ||
|
|
||
|
|
@@ -116,6 +121,7 @@ const OPERAND_SPEC = new Map<OperandType, [number, (offset: number) => OperandNa | |
| [OperandType.UINT64, [8, Buffer.prototype.readBigInt64BE, Buffer.prototype.writeBigInt64BE]], | ||
| [OperandType.UINT128, [16, readBigInt128BE, writeBigInt128BE]], | ||
| [OperandType.FF, [32, readBigInt254BE, writeBigInt254BE]], | ||
| [OperandType.TAG, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]], | ||
| ]); | ||
|
|
||
| function readBigInt254BE(this: Buffer, offset: number): bigint { | ||
|
|
@@ -160,19 +166,30 @@ function writeBigInt128BE(this: Buffer, value: bigint): void { | |
| * @param operands Specification of the operand types. | ||
| * @returns An array as big as {@code operands}, with the converted TS values. | ||
| */ | ||
| export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): (number | bigint)[] { | ||
| const argValues = []; | ||
| export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): DeserializedValue[] { | ||
| const argValues: DeserializedValue[] = []; | ||
| if (Buffer.isBuffer(cursor)) { | ||
| cursor = new BufferCursor(cursor); | ||
| } | ||
|
|
||
| for (const op of operands) { | ||
| const opType = op; | ||
| for (const opType of operands) { | ||
| const [sizeBytes, reader, _writer] = OPERAND_SPEC.get(opType)!; | ||
| argValues.push(reader.call(cursor.buffer(), cursor.position())); | ||
| const value = reader.call(cursor.buffer(), cursor.position()); | ||
| argValues.push(value); | ||
| cursor.advance(sizeBytes); | ||
| } | ||
|
|
||
| // We first want to detect other parsing errors (e.g., instruction size | ||
| // is longer than remaining bytes) first and therefore tag validation is done after completion | ||
| // of parsing above. Order of errors need to match with circuit. | ||
| for (let i = 0; i < operands.length; i++) { | ||
| if (operands[i] === OperandType.TAG) { | ||
| // We parsed a single byte (readUInt8()) and therefore value is of number type (not bigint) | ||
| // We need to cast to number because checkIsValidTag expects a number. | ||
| TaggedMemory.checkIsValidTag(Number(argValues[i]) ?? 0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this defensive expression is not really helping. You might be calling Number with undefined, I think I had written |
||
| } | ||
| } | ||
|
|
||
| return argValues; | ||
| } | ||
|
|
||
|
|
||
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.
[Re: lines +96 to +99]
arg, missed this
See this comment inline on Graphite.