Skip to content

Commit 9553a46

Browse files
authored
1 parent a6fa349 commit 9553a46

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

yarn-project/simulator/src/public/avm/avm_memory_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function UnsignedIntegerClassFactory(bits: number) {
8181
public constructor(n: bigint | number) {
8282
super();
8383
this.n = BigInt(n);
84+
assert(n >= 0n, `${this.constructor.name} cannot handle negative values: ${n}.`);
8485
assert(n < NewUintClass.mod, `Value ${n} is too large for ${this.constructor.name}.`);
8586
}
8687

yarn-project/simulator/src/public/avm/opcodes/memory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export class Set extends Instruction {
6363
private value: bigint | number,
6464
) {
6565
super();
66-
67-
assert(this.value < Fr.MODULUS, 'Value is larger than Fr.MODULUS');
66+
assert(this.value >= 0, `Value ${this.value} is negative`);
67+
assert(this.value < Fr.MODULUS, `Value ${this.value} is larger than Fr.MODULUS`);
6868
}
6969

7070
public async execute(context: AvmContext): Promise<void> {

yarn-project/simulator/src/public/avm/serialization/instruction_serialization.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,38 @@ const OPERAND_SPEC = new Map<OperandType, [number, (offset: number) => OperandNa
118118
[OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
119119
[OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
120120
[OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
121-
[OperandType.UINT64, [8, Buffer.prototype.readBigInt64BE, Buffer.prototype.writeBigInt64BE]],
122-
[OperandType.UINT128, [16, readBigInt128BE, writeBigInt128BE]],
123-
[OperandType.FF, [32, readBigInt254BE, writeBigInt254BE]],
121+
[OperandType.UINT64, [8, readUint64BE, writeUint64BE]],
122+
[OperandType.UINT128, [16, readUint128BE, writeUint128BE]],
123+
[OperandType.FF, [32, readUint254BE, writeUint254BE]],
124124
[OperandType.TAG, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
125125
]);
126126

127-
function readBigInt254BE(this: Buffer, offset: number): bigint {
128-
const totalBytes = 32;
127+
function readUintBE(buf: Buffer, offset: number, totalBytes: number): bigint {
129128
let value: bigint = 0n;
130129
for (let i = 0; i < totalBytes; ++i) {
131130
value <<= 8n;
132-
value |= BigInt(this.readUint8(i + offset));
131+
value |= BigInt(buf.readUint8(i + offset));
133132
}
133+
return value;
134+
}
135+
136+
function writeUintBE(buf: Buffer, value: bigint, totalBytes: number): void {
137+
for (let offset = totalBytes - 1; offset >= 0; --offset) {
138+
buf.writeUint8(Number(value & 0xffn), offset);
139+
value >>= 8n;
140+
}
141+
}
142+
143+
function readUint64BE(this: Buffer, offset: number): bigint {
144+
return readUintBE(this, offset, 8);
145+
}
146+
147+
function writeUint64BE(this: Buffer, value: bigint): void {
148+
writeUintBE(this, value, 8);
149+
}
150+
151+
function readUint254BE(this: Buffer, offset: number): bigint {
152+
let value = readUintBE(this, offset, 32);
134153

135154
// In circuit, we only support values up to Fr.MODULUS and any deserialized value
136155
// would naturally undergo a modulus reduction.
@@ -141,30 +160,16 @@ function readBigInt254BE(this: Buffer, offset: number): bigint {
141160
return value;
142161
}
143162

144-
function writeBigInt254BE(this: Buffer, value: bigint): void {
145-
const totalBytes = 32;
146-
for (let offset = totalBytes - 1; offset >= 0; --offset) {
147-
this.writeUint8(Number(value & 0xffn), offset);
148-
value >>= 8n;
149-
}
163+
function writeUint254BE(this: Buffer, value: bigint): void {
164+
writeUintBE(this, value, 32);
150165
}
151166

152-
function readBigInt128BE(this: Buffer, offset: number): bigint {
153-
const totalBytes = 16;
154-
let ret: bigint = 0n;
155-
for (let i = 0; i < totalBytes; ++i) {
156-
ret <<= 8n;
157-
ret |= BigInt(this.readUint8(i + offset));
158-
}
159-
return ret;
167+
function readUint128BE(this: Buffer, offset: number): bigint {
168+
return readUintBE(this, offset, 16);
160169
}
161170

162-
function writeBigInt128BE(this: Buffer, value: bigint): void {
163-
const totalBytes = 16;
164-
for (let offset = totalBytes - 1; offset >= 0; --offset) {
165-
this.writeUint8(Number(value & 0xffn), offset);
166-
value >>= 8n;
167-
}
171+
function writeUint128BE(this: Buffer, value: bigint): void {
172+
writeUintBE(this, value, 16);
168173
}
169174

170175
/**

0 commit comments

Comments
 (0)