Skip to content

Commit

Permalink
feat: add unit test and complete hint
Browse files Browse the repository at this point in the history
  • Loading branch information
coxmars committed Aug 8, 2024
1 parent fde21d7 commit 3dc46e3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/hints/math/divMod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { describe, expect, test } from 'bun:test';

import { Felt } from 'primitives/felt';
import { VirtualMachine } from 'vm/virtualMachine';
import { Relocatable } from 'primitives/relocatable';
import { HintName } from 'hints/hintName';
import { OpType, ResOperand, CellRef } from 'hints/hintParamsSchema';
import { divModParser } from './divMod';
import { divMod } from './divMod';
import { Register } from 'vm/instruction';

const DIV_MOD_HINT = {
DivMod: {
lhs: {
Deref: {
register: 'AP',
offset: 0,
},
},
rhs: {
Deref: {
register: 'AP',
offset: 1,
},
},
quotient: {
register: 'AP',
offset: 2,
},
remainder: {
register: 'AP',
offset: 3,
},
},
};

describe('DivMod', () => {
test('should properly parse DivMod hint', () => {
const hint = divModParser.parse(DIV_MOD_HINT);
expect(hint).toEqual({
type: HintName.DivMod,
leftHandSide: {
type: OpType.Deref,
cell: {
register: Register.Ap,
offset: 0,
},
},
rightHandSide: {
type: OpType.Deref,
cell: {
register: Register.Ap,
offset: 1,
},
},
quotientAddress: {
register: Register.Ap,
offset: 2,
},
remainderAddress: {
register: Register.Ap,
offset: 3,
},
});
});

test('should properly execute DivMod hint', () => {
const hint = divModParser.parse(DIV_MOD_HINT);
const vm = new VirtualMachine();
vm.memory.addSegment();
vm.memory.addSegment();

const lhsValue = new Felt(17n);
const rhsValue = new Felt(5n);
const quotientExpected = new Felt(3n);
const remainderExpected = new Felt(2n);

const lhsAddr = new Relocatable(0, 0);
const rhsAddr = new Relocatable(0, 1);
const quotientAddr = new Relocatable(0, 2);
const remainderAddr = new Relocatable(0, 3);

vm.memory.assertEq(lhsAddr, lhsValue);
vm.memory.assertEq(rhsAddr, rhsValue);
vm.ap = new Relocatable(0, 0);

divMod(vm, hint.leftHandSide, hint.rightHandSide, hint.quotientAddress, hint.remainderAddress);

expect(vm.memory.get(quotientAddr)).toEqual(quotientExpected);
expect(vm.memory.get(remainderAddr)).toEqual(remainderExpected);
});
});
7 changes: 7 additions & 0 deletions src/hints/math/divMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ export const divMod = (
const lhsValue = vm.getResOperandValue(leftHandSide).toBigInt();
const rhsValue = vm.getResOperandValue(rightHandSide).toBigInt();

if (rhsValue === 0n) {
throw new Error("Division by zero");
}

const quotientValue = new Felt(lhsValue / rhsValue);
const remainderValue = new Felt(lhsValue % rhsValue);

const quotientAddr = vm.cellRefToRelocatable(quotientAddress);
const remainderAddr = vm.cellRefToRelocatable(remainderAddress);

vm.memory.assertEq(quotientAddr, quotientValue);
vm.memory.assertEq(remainderAddr, remainderValue);
};

0 comments on commit 3dc46e3

Please sign in to comment.