-
Notifications
You must be signed in to change notification settings - Fork 598
feat(avm-simulator): add to_radix_le instruction #6308
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
IlyasRidhuan
merged 1 commit into
master
from
05-09-feat_avm_add_to_radix_le_instruction_to_simulator
May 10, 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 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
185 changes: 116 additions & 69 deletions
185
docs/docs/protocol-specs/public-vm/gen/_instruction-set.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { type AvmContext } from '../avm_context.js'; | ||
| import { Field, type Uint8, Uint32 } from '../avm_memory_types.js'; | ||
| import { initContext } from '../fixtures/index.js'; | ||
| import { Addressing, AddressingMode } from './addressing_mode.js'; | ||
| import { ToRadixLE } from './conversion.js'; | ||
|
|
||
| describe('Conversion Opcodes', () => { | ||
| let context: AvmContext; | ||
|
|
||
| beforeEach(async () => { | ||
| context = initContext(); | ||
| }); | ||
|
|
||
| describe('To Radix LE', () => { | ||
| it('Should (de)serialize correctly', () => { | ||
| const buf = Buffer.from([ | ||
| ToRadixLE.opcode, // opcode | ||
| 1, // indirect | ||
| ...Buffer.from('12345678', 'hex'), // inputStateOffset | ||
| ...Buffer.from('23456789', 'hex'), // outputStateOffset | ||
| ...Buffer.from('00000002', 'hex'), // radix | ||
| ...Buffer.from('00000100', 'hex'), // numLimbs | ||
| ]); | ||
| const inst = new ToRadixLE( | ||
| /*indirect=*/ 1, | ||
| /*srcOffset=*/ 0x12345678, | ||
| /*dstOffset=*/ 0x23456789, | ||
| /*radix=*/ 2, | ||
| /*numLimbs=*/ 256, | ||
| ); | ||
|
|
||
| expect(ToRadixLE.deserialize(buf)).toEqual(inst); | ||
| expect(inst.serialize()).toEqual(buf); | ||
| }); | ||
|
|
||
| it('Should decompose correctly - direct', async () => { | ||
| const arg = new Field(0b1011101010100n); | ||
| const indirect = 0; | ||
| const srcOffset = 0; | ||
| const radix = 2; // Bit decomposition | ||
| const numLimbs = 10; // only the first 10 bits | ||
| const dstOffset = 20; | ||
| context.machineState.memory.set(srcOffset, arg); | ||
|
|
||
| await new ToRadixLE(indirect, srcOffset, dstOffset, radix, numLimbs).execute(context); | ||
|
|
||
| const resultBuffer: Buffer = Buffer.concat( | ||
| context.machineState.memory.getSliceAs<Uint8>(dstOffset, numLimbs).map(byte => byte.toBuffer()), | ||
| ); | ||
| // The expected result is the first 10 bits of the input, reversed | ||
| const expectedResults = '1011101010100'.split('').reverse().slice(0, numLimbs).map(Number); | ||
| for (let i = 0; i < numLimbs; i++) { | ||
| expect(resultBuffer.readUInt8(i)).toEqual(expectedResults[i]); | ||
| } | ||
| }); | ||
|
|
||
| it('Should decompose correctly - indirect', async () => { | ||
| const arg = new Field(Buffer.from('1234567890abcdef', 'hex')); | ||
| const indirect = new Addressing([ | ||
| /*srcOffset=*/ AddressingMode.INDIRECT, | ||
| /*dstOffset*/ AddressingMode.INDIRECT, | ||
| ]).toWire(); | ||
| const srcOffset = 0; | ||
| const srcOffsetReal = 10; | ||
| const dstOffset = 2; | ||
| const dstOffsetReal = 30; | ||
| context.machineState.memory.set(srcOffset, new Uint32(srcOffsetReal)); | ||
| context.machineState.memory.set(dstOffset, new Uint32(dstOffsetReal)); | ||
| context.machineState.memory.set(srcOffsetReal, arg); | ||
|
|
||
| const radix = 1 << 8; // Byte decomposition | ||
| const numLimbs = 32; // 256-bit decomposition | ||
| await new ToRadixLE(indirect, srcOffset, dstOffset, radix, numLimbs).execute(context); | ||
|
|
||
| const resultBuffer: Buffer = Buffer.concat( | ||
| context.machineState.memory.getSliceAs<Uint8>(dstOffsetReal, numLimbs).map(byte => byte.toBuffer()), | ||
| ); | ||
| // The expected result is the input (padded to 256 bits),and reversed | ||
| const expectedResults = '1234567890abcdef' | ||
| .padStart(64, '0') | ||
| .split('') | ||
| .reverse() | ||
| .map(a => parseInt(a, 16)); | ||
| // Checking the value in each byte of the buffer is correct | ||
| for (let i = 0; i < numLimbs; i++) { | ||
| expect(resultBuffer.readUInt8(i)).toEqual(expectedResults[2 * i] + expectedResults[2 * i + 1] * 16); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { assert } from '../../../../foundation/src/json-rpc/js_utils.js'; | ||
| import { type AvmContext } from '../avm_context.js'; | ||
| import { TypeTag, Uint8 } from '../avm_memory_types.js'; | ||
| import { Opcode, OperandType } from '../serialization/instruction_serialization.js'; | ||
| import { Addressing } from './addressing_mode.js'; | ||
| import { Instruction } from './instruction.js'; | ||
|
|
||
| export class ToRadixLE extends Instruction { | ||
| static type: string = 'TORADIXLE'; | ||
| static readonly opcode: Opcode = Opcode.TORADIXLE; | ||
|
|
||
| // Informs (de)serialization. See Instruction.deserialize. | ||
| static readonly wireFormat: OperandType[] = [ | ||
| OperandType.UINT8, // Opcode | ||
| OperandType.UINT8, // Indirect | ||
| OperandType.UINT32, // src memory address | ||
| OperandType.UINT32, // dst memory address | ||
| OperandType.UINT32, // radix (immediate) | ||
| OperandType.UINT32, // number of limbs (Immediate) | ||
| ]; | ||
|
|
||
| constructor( | ||
| private indirect: number, | ||
| private srcOffset: number, | ||
| private dstOffset: number, | ||
| private radix: number, | ||
| private numLimbs: number, | ||
| ) { | ||
| assert(radix <= 256, 'Radix cannot be greater than 256'); | ||
| super(); | ||
IlyasRidhuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public async execute(context: AvmContext): Promise<void> { | ||
| const memory = context.machineState.memory.track(this.type); | ||
| const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory); | ||
| const memoryOperations = { reads: 1, writes: this.numLimbs, indirect: this.indirect }; | ||
| context.machineState.consumeGas(this.gasCost(memoryOperations)); | ||
|
|
||
| // The radix gadget only takes in a Field | ||
| memory.checkTag(TypeTag.FIELD, srcOffset); | ||
|
|
||
| let value: bigint = memory.get(srcOffset).toBigInt(); | ||
| const radixBN: bigint = BigInt(this.radix); | ||
| const limbArray = []; | ||
|
|
||
| for (let i = 0; i < this.numLimbs; i++) { | ||
| const limb = value % radixBN; | ||
| limbArray.push(limb); | ||
| value /= radixBN; | ||
| } | ||
|
|
||
| const res = [...limbArray].map(byte => new Uint8(byte)); | ||
| memory.setSlice(dstOffset, res); | ||
|
|
||
| memory.assert(memoryOperations); | ||
| context.machineState.incrementPc(); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Does it support this to be indirect? brillig provides a pointer to the result
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.
Yep that is the plan. The details of how the AVM will implement storing slices is still a bit up in the air