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
5 changes: 5 additions & 0 deletions src/hints/hintHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
shouldSkipSquashLoop,
} from './dict/shouldSkipSquashLoop';
import { TestLessThan, testLessThan } from './math/testLessThan';
import { DivMod, divMod } from './math/divMod';

/**
* Map hint names to the function executing their logic.
Expand Down Expand Up @@ -125,4 +126,8 @@ export const handlers: HintHandler = {
const h = hint as TestLessThan;
testLessThan(vm, h.lhs, h.rhs, h.dst);
},
[HintName.DivMod]: (vm, hint) => {
const h = hint as DivMod;
divMod(vm, h.leftHandSide, h.rightHandSide, h.quotientAddress, h.remainderAddress);
},
};
1 change: 1 addition & 0 deletions src/hints/hintName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export enum HintName {
ShouldContinueSquashLoop = 'ShouldContinueSquashLoop',
ShouldSkipSquashLoop = 'ShouldSkipSquashLoop',
TestLessThan = 'TestLessThan',
DivMod = 'DivMod',
}
2 changes: 2 additions & 0 deletions src/hints/hintSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { initSquashDataParser } from './dict/initSquashData';
import { shouldContinueSquashLoopParser } from './dict/shouldContinueSquashLoop';
import { shouldSkipSquashLoopParser } from './dict/shouldSkipSquashLoop';
import { testLessThanParser } from './math/testLessThan';
import { divModParser } from './math/divMod';

/** Zod object to parse any implemented hints */
const hint = z.union([
Expand All @@ -33,6 +34,7 @@ const hint = z.union([
shouldContinueSquashLoopParser,
shouldSkipSquashLoopParser,
testLessThanParser,
divModParser
]);

/** Zod object to parse an array of hints grouped on a given PC */
Expand Down
58 changes: 58 additions & 0 deletions src/hints/math/divMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { z } from 'zod';

import { VirtualMachine } from 'vm/virtualMachine';

import { resOperand, ResOperand, cellRef, CellRef, } from 'hints/hintParamsSchema';
import { HintName } from 'hints/hintName';
import { Felt } from 'primitives/felt';

/** Zod object to parse DivMod hint */
export const divModParser = z
.object({
DivMod: z.object({
lhs: resOperand,
rhs: resOperand,
quotient: cellRef,
remainder: cellRef,
}),
})
.transform(({ DivMod: { lhs, rhs, quotient, remainder } }) => ({
type: HintName.DivMod,
leftHandSide: lhs,
rightHandSide: rhs,
quotientAddress: quotient,
remainderAddress: remainder,
coxmars marked this conversation as resolved.
Show resolved Hide resolved
}));


/**
* DivMod hint type
*/
export type DivMod = z.infer<typeof divModParser>;

/**
* Perform division and modulus operations.
*
* @param {VirtualMachine} vm - The virtual machine instance
* @param {ResOperand} leftHandSide - The left-hand side operand
* @param {ResOperand} rightHandSide - The right-hand side operand
* @param {CellRef} quotientAddress - The address to store the quotient
* @param {CellRef} remainderAddress - The address to store the remainder
*/

export const divMod = (
vm: VirtualMachine,
leftHandSide: ResOperand,
rightHandSide: ResOperand,
quotientAddress: CellRef,
remainderAddress: CellRef
) => {
const lhsValue = vm.getResOperandValue(leftHandSide).toBigInt();
const rhsValue = vm.getResOperandValue(rightHandSide).toBigInt();

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

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