-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(avm-simulator): add to_radix_le instruction (#6308)
- Loading branch information
1 parent
8cf9168
commit 6374a32
Showing
8 changed files
with
308 additions
and
69 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
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 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
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 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(); | ||
} | ||
|
||
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 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