-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add lessThanOrEqualAddress cairo hint (#132)
* feat: add lessThanOrEqualAdress hint * fix: fix typo in comment * fix: fix typos, tests and methods documentation * fix: fix tests * style: run trunk * doc: improve JSDocs * refactor: rename cairo programs * style: format
- Loading branch information
1 parent
41f3337
commit be4c9d0
Showing
8 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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() { | ||
let mut span = array![1, 2, 3].span(); | ||
let _ = span.multi_pop_back::<2>(); | ||
} |
This file contains 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() { | ||
let mut span = array![1, 2, 3].span(); | ||
let _ = span.multi_pop_front::<2>(); | ||
} |
This file contains 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 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 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 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 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,85 @@ | ||
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 { testLessThanOrEqualAddressParser } from './testLessThanOrEqualAddress'; | ||
import { Relocatable } from 'primitives/relocatable'; | ||
|
||
const TEST_LESS_THAN_OR_EQUAL_ADDRESS = { | ||
TestLessThanOrEqualAddress: { | ||
lhs: { | ||
Deref: { | ||
register: 'AP', | ||
offset: 0, | ||
}, | ||
}, | ||
rhs: { | ||
Deref: { | ||
register: 'AP', | ||
offset: 1, | ||
}, | ||
}, | ||
dst: { | ||
register: 'AP', | ||
offset: 2, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('TestLessThanOrEqualAddress', () => { | ||
test('should properly parse TestLessThanOrEqualAddress hint', () => { | ||
const hint = testLessThanOrEqualAddressParser.parse( | ||
TEST_LESS_THAN_OR_EQUAL_ADDRESS | ||
); | ||
|
||
expect(hint).toEqual({ | ||
type: HintName.TestLessThanOrEqualAddress, | ||
lhs: { | ||
type: OpType.Deref, | ||
cell: { | ||
register: Register.Ap, | ||
offset: 0, | ||
}, | ||
}, | ||
rhs: { | ||
type: OpType.Deref, | ||
cell: { | ||
register: Register.Ap, | ||
offset: 1, | ||
}, | ||
}, | ||
dst: { | ||
register: Register.Ap, | ||
offset: 2, | ||
}, | ||
}); | ||
}); | ||
|
||
test.each([ | ||
[new Relocatable(1, 0), new Relocatable(1, 1), new Felt(1n)], | ||
[new Relocatable(1, 1), new Relocatable(1, 0), new Felt(0n)], | ||
[new Relocatable(1, 1), new Relocatable(1, 1), new Felt(1n)], | ||
[new Relocatable(0, 0), new Relocatable(1, 0), new Felt(1n)], | ||
])( | ||
'should properly execute TestLessThanOrEqualAddress hint', | ||
(lhs, rhs, expected) => { | ||
const hint = testLessThanOrEqualAddressParser.parse( | ||
TEST_LESS_THAN_OR_EQUAL_ADDRESS | ||
); | ||
const vm = new VirtualMachine(); | ||
vm.memory.addSegment(); | ||
vm.memory.addSegment(); | ||
vm.memory.assertEq(vm.ap, lhs); | ||
vm.memory.assertEq(vm.ap.add(1), rhs); | ||
|
||
vm.executeHint(hint); | ||
expect(vm.memory.get(vm.cellRefToRelocatable(hint.dst))).toEqual( | ||
expected | ||
); | ||
} | ||
); | ||
}); |
This file contains 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,63 @@ | ||
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 TestLessThanOrEqualAddress hint */ | ||
export const testLessThanOrEqualAddressParser = z | ||
.object({ | ||
TestLessThanOrEqualAddress: z.object({ | ||
lhs: resOperand, | ||
rhs: resOperand, | ||
dst: cellRef, | ||
}), | ||
}) | ||
.transform(({ TestLessThanOrEqualAddress: { lhs, rhs, dst } }) => ({ | ||
type: HintName.TestLessThanOrEqualAddress, | ||
lhs, | ||
rhs, | ||
dst, | ||
})); | ||
|
||
/** | ||
* TestLessThanOrEqualAddress hint | ||
* | ||
* Check whether the Relocatable value at `lhs` is inferior or equal to the value at `rhs` | ||
*/ | ||
export type TestLessThanOrEqualAddress = z.infer< | ||
typeof testLessThanOrEqualAddressParser | ||
>; | ||
|
||
/** | ||
* TestLessThanOrEqualAddress hint | ||
* | ||
* Check whether the Relocatable value at `lhs` is inferior or equal to the value at `rhs` | ||
* | ||
* @param {VirtualMachine} vm - The virtual machine instance. | ||
* @param {ResOperand} lhs - The left-hand side operand. | ||
* @param {ResOperand} rhs - The right-hand side operand. | ||
* @param {CellRef} dst - The address where the result of the comparison will be stored. | ||
*/ | ||
export const testLessThanOrEqualAddress = ( | ||
vm: VirtualMachine, | ||
lhs: ResOperand, | ||
rhs: ResOperand, | ||
dst: CellRef | ||
) => { | ||
const lhsValue = vm.getResOperandRelocatable(lhs); | ||
const rhsValue = vm.getResOperandRelocatable(rhs); | ||
|
||
const isLessThanOrEqual = lhsValue <= rhsValue; | ||
|
||
const result = new Felt(isLessThanOrEqual ? 1n : 0n); | ||
|
||
vm.memory.assertEq(vm.cellRefToRelocatable(dst), result); | ||
}; |