Skip to content
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

feat: Add div mod hint #127

Merged
merged 10 commits into from
Oct 7, 2024
17 changes: 17 additions & 0 deletions src/hints/math/divMod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ describe('DivMod', () => {
);
}
);

test('should throw an error when rhs is zero', () => {
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(0n);

vm.memory.assertEq(vm.ap, lhsValue);
vm.memory.assertEq(vm.ap.add(1), rhsValue);

expect(() => {
divMod(vm, hint.lhs, hint.rhs, hint.quotient, hint.remainder);
});
coxmars marked this conversation as resolved.
Show resolved Hide resolved
});
});
5 changes: 2 additions & 3 deletions src/hints/math/divMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'hints/hintParamsSchema';
import { HintName } from 'hints/hintName';
import { Felt } from 'primitives/felt';
import { CannotDivideByZero } from 'errors/primitives';

/** Zod object to parse DivMod hint */
export const divModParser = z
Expand Down Expand Up @@ -54,9 +55,7 @@ export const divMod = (
const lhsValue = vm.getResOperandValue(lhs).toBigInt();
const rhsValue = vm.getResOperandValue(rhs).toBigInt();

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

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