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 WideMul128 hint #126

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
WideMul128 = 'WideMul128',
}
3 changes: 2 additions & 1 deletion src/hints/hintSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { initSquashDataParser } from './dict/initSquashData';
import { shouldContinueSquashLoopParser } from './dict/shouldContinueSquashLoop';
import { shouldSkipSquashLoopParser } from './dict/shouldSkipSquashLoop';
import { testLessThanParser } from './math/testLessThan';

import { wideMul128 } from './WideMul128';
/** Zod object to parse any implemented hints */
const hint = z.union([
allocFelt252DictParser,
Expand All @@ -33,6 +33,7 @@ const hint = z.union([
shouldContinueSquashLoopParser,
shouldSkipSquashLoopParser,
testLessThanParser,
wideMul128,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hint object expects the zod object you've defined to parse the hint from the compilation artifacts: wideMul128Parser

]);

/** Zod object to parse an array of hints grouped on a given PC */
Expand Down
55 changes: 55 additions & 0 deletions src/hints/wideMul128.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { z } from 'zod';
import { HintName } from 'hints/hintName';
import { cellRef, CellRef } from 'hints/hintParamsSchema';
import { resOperand, ResOperand } from 'hints/hintParamsSchema';
import { VirtualMachine } from 'vm/virtualMachine';
import { Felt } from 'primitives/felt';
export const wideMul128Parser = z.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add JSDoc

object({
WideMul128: z.object({
lhs: resOperand,
rhs: resOperand,
high: cellRef,
low: cellRef
}),
})
.transform(({ WideMul128: { lhs, rhs, high, low } }) => ({
type: HintName.WideMul128,
lhs: lhs,
rhs: rhs,
high: high,
low: low
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the property names are unchanged, you can directly use the variable:

Suggested change
lhs: lhs,
rhs: rhs,
high: high,
low: low
lhs,
rhs,
high,
low

}));

export const wideMul128 = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a well-formatted JSDoc, take example on the AllocFelt252Dict one

vm: VirtualMachine,
lhs: ResOperand,
rhs: ResOperand,
high: CellRef,
low: CellRef
): void => {
try {
const mask128 = (BigInt(1) << BigInt(128)) - BigInt(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use mask rather than mask128

Use the short notation for Bigint when possible, it is more readable: 128n instead of BigInt(128)

Suggested change
const mask128 = (BigInt(1) << BigInt(128)) - BigInt(1);
const mask = (1n << 128n) - 1n


//Gets the operands' values
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add comments, the code is self-explanatory here (same // Do multiplication) etc.

const lhsVal = vm.getResOperandValue(lhs).toBigInt();
const rhsVal = vm.getResOperandValue(rhs).toBigInt();

// Do multiplication
const prod = lhsVal * rhsVal;

//Calc and storage high
const highVal = prod >> BigInt(128);
const highRef = vm.cellRefToRelocatable(high);
vm.memory.assertEq(highRef, new Felt(highVal));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly create highVal as a Felt

No need to declare a variable for the address (i.e. highRef), directly call cellRefToRelocatable in assertEq()


//Calc and storage low
const lowVal = prod & mask128;
const lowRef = vm.cellRefToRelocatable(low);
vm.memory.assertEq(lowRef, new Felt(lowVal));

} catch (error: any) {
throw new Error(error.message);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't wrap the function in a try catch statement

};