-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add TestLessThanOrEqual hint #133
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
Open
LazarusAA
wants to merge
7
commits into
kkrt-labs:main
Choose a base branch
from
LazarusAA:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6876c0c
feat: add TestLessThanOrEqual hint
LazarusAA ed3e453
fix: fixed program and documentation
LazarusAA 0a10eaa
fix: fixed trunk formatting
LazarusAA 40ccc4d
fix: fixed documentation
LazarusAA 760b486
fix: fix documentation
LazarusAA 4fad19f
fix: fixed program test
LazarusAA af6b1a2
fix: filename span_life.cairo changed
LazarusAA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fn main() -> bool { | ||
| 12_u32 <= 10_u32 | ||
| } | ||
4 changes: 4 additions & 0 deletions
4
cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| fn main() -> bool { | ||
| 10_u32 <= 12_u32; | ||
| 12_u32 <= 12_u32 | ||
LazarusAA marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
|
|
||
| import { Felt } from 'primitives/felt'; | ||
| import { Register } from 'vm/instruction'; | ||
| import { VirtualMachine } from 'vm/virtualMachine'; | ||
|
|
||
| import { OpType } from 'hints/hintParamsSchema'; | ||
| import { HintName } from 'hints/hintName'; | ||
| import { testLessThanOrEqualParser } from './testLessThanOrEqual'; | ||
|
|
||
| const TEST_LESS_THAN_OR_EQUAL = { | ||
| TestLessThanOrEqual: { | ||
| lhs: { | ||
| Deref: { | ||
| register: 'AP', | ||
| offset: 0, | ||
| }, | ||
| }, | ||
| rhs: { | ||
| Immediate: '0x100000000', | ||
| }, | ||
| dst: { | ||
| register: 'AP', | ||
| offset: -1, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| describe('TestLessThanOrEqual', () => { | ||
| test('should properly parse TestLessThanOrEqual hint', () => { | ||
| const hint = testLessThanOrEqualParser.parse(TEST_LESS_THAN_OR_EQUAL); | ||
|
|
||
| expect(hint).toEqual({ | ||
| type: HintName.TestLessThanOrEqual, | ||
| lhs: { | ||
| type: OpType.Deref, | ||
| cell: { | ||
| register: Register.Ap, | ||
| offset: 0, | ||
| }, | ||
| }, | ||
| rhs: { | ||
| type: OpType.Immediate, | ||
| value: new Felt(BigInt('0x100000000')), | ||
| }, | ||
| dst: { | ||
| register: Register.Ap, | ||
| offset: -1, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test.each([ | ||
| [new Felt(2n), new Felt(1n)], | ||
| [new Felt(BigInt('0x0ffffffff')), new Felt(1n)], | ||
| [new Felt(BigInt('0x100000000')), new Felt(1n)], | ||
| [new Felt(-2n), new Felt(0n)], | ||
| ])('should properly execute TestLessThanOrEqual hint', (lhs, expected) => { | ||
| const hint = testLessThanOrEqualParser.parse(TEST_LESS_THAN_OR_EQUAL); | ||
| const vm = new VirtualMachine(); | ||
| vm.memory.addSegment(); | ||
| vm.memory.addSegment(); | ||
| vm.ap = vm.ap.add(1); | ||
| vm.memory.assertEq(vm.ap, lhs); | ||
|
|
||
| vm.executeHint(hint); | ||
| expect(vm.memory.get(vm.cellRefToRelocatable(hint.dst))).toEqual(expected); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { Felt } from 'primitives/felt'; | ||
| import { VirtualMachine } from 'vm/virtualMachine'; | ||
|
|
||
| import { HintName } from 'hints/hintName'; | ||
| import { | ||
| cellRef, | ||
| resOperand, | ||
| CellRef, | ||
| ResOperand, | ||
| } from 'hints/hintParamsSchema'; | ||
|
|
||
| /** Zod object to parse TestLessThanOrEqual hint */ | ||
| export const testLessThanOrEqualParser = z | ||
| .object({ | ||
| TestLessThanOrEqual: z.object({ | ||
| lhs: resOperand, | ||
| rhs: resOperand, | ||
| dst: cellRef, | ||
| }), | ||
| }) | ||
| .transform(({ TestLessThanOrEqual: { lhs, rhs, dst } }) => ({ | ||
| type: HintName.TestLessThanOrEqual, | ||
| lhs, | ||
| rhs, | ||
| dst, | ||
| })); | ||
|
|
||
| /** | ||
| * TestLessThanOrEqual hint | ||
| * | ||
| * Store true at `dst` if value at `lhs` is inferior or equal to value at `rhs`. | ||
| * Store false otherwise | ||
zmalatrax marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| export type TestLessThanOrEqual = z.infer<typeof testLessThanOrEqualParser>; | ||
|
|
||
| /** | ||
| * TestLessThanOrEqual hint | ||
| * | ||
| * Check whether the value at `lhs` is less than or equal to the value at `rhs` | ||
| * | ||
| * Store the boolean result (0 or 1) at `dst` | ||
LazarusAA marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| export const testLessThanOrEqual = ( | ||
LazarusAA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vm: VirtualMachine, | ||
| lhs: ResOperand, | ||
| rhs: ResOperand, | ||
| dst: CellRef | ||
| ) => { | ||
| const lhsValue = vm.getResOperandValue(lhs); | ||
| const rhsValue = vm.getResOperandValue(rhs); | ||
| const result = new Felt(BigInt(lhsValue <= rhsValue)); | ||
| vm.memory.assertEq(vm.cellRefToRelocatable(dst), result); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.