From 6876c0c03adb61fbf67a25d87031ce41f25a9fd6 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Thu, 8 Aug 2024 05:50:53 -0600 Subject: [PATCH 1/7] feat: add TestLessThanOrEqual hint --- .../hints/test_less_than_or_equal_false.cairo | 3 + .../hints/test_less_than_or_equal_true.cairo | 4 ++ src/hints/hintHandler.ts | 8 +++ src/hints/hintName.ts | 1 + src/hints/hintSchema.ts | 2 + src/hints/math/testLessThanOrEqual.test.ts | 69 +++++++++++++++++++ src/hints/math/testLessThanOrEqual.ts | 55 +++++++++++++++ 7 files changed, 142 insertions(+) create mode 100644 cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo create mode 100644 cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo create mode 100644 src/hints/math/testLessThanOrEqual.test.ts create mode 100644 src/hints/math/testLessThanOrEqual.ts diff --git a/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo b/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo new file mode 100644 index 00000000..78dd5249 --- /dev/null +++ b/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo @@ -0,0 +1,3 @@ +fn main() -> bool { + 12_u32 <= 10_u32 +} \ No newline at end of file diff --git a/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo b/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo new file mode 100644 index 00000000..a0c1a9bd --- /dev/null +++ b/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo @@ -0,0 +1,4 @@ +fn main() -> bool { + 10_u32 <= 12_u32; + 12_u32 <= 12_u32 +} \ No newline at end of file diff --git a/src/hints/hintHandler.ts b/src/hints/hintHandler.ts index 020aeeab..10898eb0 100644 --- a/src/hints/hintHandler.ts +++ b/src/hints/hintHandler.ts @@ -48,6 +48,10 @@ import { shouldSkipSquashLoop, } from './dict/shouldSkipSquashLoop'; import { TestLessThan, testLessThan } from './math/testLessThan'; +import { + TestLessThanOrEqual, + testLessThanOrEqual, +} from './math/testLessThanOrEqual'; /** * Map hint names to the function executing their logic. @@ -125,4 +129,8 @@ export const handlers: HintHandler = { const h = hint as TestLessThan; testLessThan(vm, h.lhs, h.rhs, h.dst); }, + [HintName.TestLessThanOrEqual]: (vm, hint) => { + const h = hint as TestLessThanOrEqual; + testLessThanOrEqual(vm, h.lhs, h.rhs, h.dst); + }, }; diff --git a/src/hints/hintName.ts b/src/hints/hintName.ts index f00a024a..f16eaba0 100644 --- a/src/hints/hintName.ts +++ b/src/hints/hintName.ts @@ -15,4 +15,5 @@ export enum HintName { ShouldContinueSquashLoop = 'ShouldContinueSquashLoop', ShouldSkipSquashLoop = 'ShouldSkipSquashLoop', TestLessThan = 'TestLessThan', + TestLessThanOrEqual = 'TestLessThanOrEqual', } diff --git a/src/hints/hintSchema.ts b/src/hints/hintSchema.ts index 892737db..2914aec3 100644 --- a/src/hints/hintSchema.ts +++ b/src/hints/hintSchema.ts @@ -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 { testLessThanOrEqualParser } from './math/testLessThanOrEqual'; /** Zod object to parse any implemented hints */ const hint = z.union([ @@ -33,6 +34,7 @@ const hint = z.union([ shouldContinueSquashLoopParser, shouldSkipSquashLoopParser, testLessThanParser, + testLessThanOrEqualParser, ]); /** Zod object to parse an array of hints grouped on a given PC */ diff --git a/src/hints/math/testLessThanOrEqual.test.ts b/src/hints/math/testLessThanOrEqual.test.ts new file mode 100644 index 00000000..0a1f6fe7 --- /dev/null +++ b/src/hints/math/testLessThanOrEqual.test.ts @@ -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); + }); +}); diff --git a/src/hints/math/testLessThanOrEqual.ts b/src/hints/math/testLessThanOrEqual.ts new file mode 100644 index 00000000..eb0513ac --- /dev/null +++ b/src/hints/math/testLessThanOrEqual.ts @@ -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 + */ +export type TestLessThanOrEqual = z.infer; + +/** + * 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` + */ +export const testLessThanOrEqual = ( + 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); +}; From ed3e453738aef69255b9712b8b11d610adc82a75 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Fri, 9 Aug 2024 00:11:39 -0600 Subject: [PATCH 2/7] fix: fixed program and documentation --- .../cairo/hints/test_less_than_or_equal_true.cairo | 5 +++-- src/hints/math/testLessThanOrEqual.ts | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo b/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo index a0c1a9bd..e1549a83 100644 --- a/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo +++ b/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo @@ -1,4 +1,5 @@ fn main() -> bool { - 10_u32 <= 12_u32; - 12_u32 <= 12_u32 + let a = 10_u32; + let b = 12_u32; + a <= b && a <= a } \ No newline at end of file diff --git a/src/hints/math/testLessThanOrEqual.ts b/src/hints/math/testLessThanOrEqual.ts index eb0513ac..bb9fc977 100644 --- a/src/hints/math/testLessThanOrEqual.ts +++ b/src/hints/math/testLessThanOrEqual.ts @@ -41,6 +41,13 @@ export type TestLessThanOrEqual = z.infer; * 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` + * + * + * @param {VirtualMachine} vm - The virtual machine instance. + * @param {ResOperand} lhs - Pointer to the operand representing the left-hand side value in the comparison. + * @param {ResOperand} rhs - Pointer to the operand representing the right-hand side value in the comparison. + * @param {CellRef} dst - Pointer to where the result of the comparison (0 or 1) will be stored. + * */ export const testLessThanOrEqual = ( vm: VirtualMachine, From 0a10eaaaff854e7df9f7d106e5fc4d4edcff1621 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Fri, 9 Aug 2024 02:38:35 -0600 Subject: [PATCH 3/7] fix: fixed trunk formatting --- src/hints/math/testLessThanOrEqual.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hints/math/testLessThanOrEqual.ts b/src/hints/math/testLessThanOrEqual.ts index bb9fc977..ca08a8e6 100644 --- a/src/hints/math/testLessThanOrEqual.ts +++ b/src/hints/math/testLessThanOrEqual.ts @@ -41,13 +41,13 @@ export type TestLessThanOrEqual = z.infer; * 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` - * - * + * + * * @param {VirtualMachine} vm - The virtual machine instance. * @param {ResOperand} lhs - Pointer to the operand representing the left-hand side value in the comparison. * @param {ResOperand} rhs - Pointer to the operand representing the right-hand side value in the comparison. - * @param {CellRef} dst - Pointer to where the result of the comparison (0 or 1) will be stored. - * + * @param {CellRef} dst - Pointer to where the result of the comparison (0 or 1) will be stored. + * */ export const testLessThanOrEqual = ( vm: VirtualMachine, From 40ccc4da9b2a207a61c4b141a1719a8f8203b61d Mon Sep 17 00:00:00 2001 From: Lazarus Date: Fri, 9 Aug 2024 04:33:36 -0600 Subject: [PATCH 4/7] fix: fixed documentation --- src/hints/math/testLessThanOrEqual.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/hints/math/testLessThanOrEqual.ts b/src/hints/math/testLessThanOrEqual.ts index ca08a8e6..d3a1718d 100644 --- a/src/hints/math/testLessThanOrEqual.ts +++ b/src/hints/math/testLessThanOrEqual.ts @@ -30,23 +30,20 @@ export const testLessThanOrEqualParser = z /** * TestLessThanOrEqual hint * - * Store true at `dst` if value at `lhs` is inferior or equal to value at `rhs`. - * Store false otherwise + * Check whether the value at `lhs` is less than or equal to the value at `rhs` + * */ export type TestLessThanOrEqual = z.infer; /** * 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` - * + * Store true at dst if value at lhs is inferior or equal to value at rhs, false otherwise. * * @param {VirtualMachine} vm - The virtual machine instance. - * @param {ResOperand} lhs - Pointer to the operand representing the left-hand side value in the comparison. - * @param {ResOperand} rhs - Pointer to the operand representing the right-hand side value in the comparison. - * @param {CellRef} dst - Pointer to where the result of the comparison (0 or 1) will be stored. + * @param {ResOperand} lhs - The left-hand side operand. + * @param {ResOperand} rhs - The right-hand side operand. + * @param {CellRef} dst - Pointer to where the result will be stored. * */ export const testLessThanOrEqual = ( From 760b4867640da2922b88a6de7e1064a06b691358 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Fri, 9 Aug 2024 05:37:37 -0600 Subject: [PATCH 5/7] fix: fix documentation --- src/hints/math/testLessThanOrEqual.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hints/math/testLessThanOrEqual.ts b/src/hints/math/testLessThanOrEqual.ts index d3a1718d..fb26dc6d 100644 --- a/src/hints/math/testLessThanOrEqual.ts +++ b/src/hints/math/testLessThanOrEqual.ts @@ -38,7 +38,7 @@ export type TestLessThanOrEqual = z.infer; /** * TestLessThanOrEqual hint * - * Store true at dst if value at lhs is inferior or equal to value at rhs, false otherwise. + * Store true at `dst` if value at `lhs` is inferior or equal to value at `rhs`, false otherwise. * * @param {VirtualMachine} vm - The virtual machine instance. * @param {ResOperand} lhs - The left-hand side operand. From 4fad19f84b4424d1ed181c28adc5c57ba07ecc51 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Thu, 15 Aug 2024 04:31:49 -0600 Subject: [PATCH 6/7] fix: fixed program test --- cairo_programs/cairo/hints/span_life.cairo | 4 ++++ .../cairo/hints/test_less_than_or_equal_false.cairo | 3 --- .../cairo/hints/test_less_than_or_equal_true.cairo | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) create mode 100644 cairo_programs/cairo/hints/span_life.cairo delete mode 100644 cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo delete mode 100644 cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo diff --git a/cairo_programs/cairo/hints/span_life.cairo b/cairo_programs/cairo/hints/span_life.cairo new file mode 100644 index 00000000..5a889705 --- /dev/null +++ b/cairo_programs/cairo/hints/span_life.cairo @@ -0,0 +1,4 @@ +fn main() { + let span = array![1, 2, 3].span(); + let _slice = span.slice(0, 1); +} \ No newline at end of file diff --git a/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo b/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo deleted file mode 100644 index 78dd5249..00000000 --- a/cairo_programs/cairo/hints/test_less_than_or_equal_false.cairo +++ /dev/null @@ -1,3 +0,0 @@ -fn main() -> bool { - 12_u32 <= 10_u32 -} \ No newline at end of file diff --git a/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo b/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo deleted file mode 100644 index e1549a83..00000000 --- a/cairo_programs/cairo/hints/test_less_than_or_equal_true.cairo +++ /dev/null @@ -1,5 +0,0 @@ -fn main() -> bool { - let a = 10_u32; - let b = 12_u32; - a <= b && a <= a -} \ No newline at end of file From af6b1a25abb815f7f50e5ec5c8adc5638244d872 Mon Sep 17 00:00:00 2001 From: Lazarus Date: Tue, 27 Aug 2024 09:31:42 -0600 Subject: [PATCH 7/7] fix: filename span_life.cairo changed --- cairo_programs/cairo/hints/{span_life.cairo => span_slice.cairo} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cairo_programs/cairo/hints/{span_life.cairo => span_slice.cairo} (100%) diff --git a/cairo_programs/cairo/hints/span_life.cairo b/cairo_programs/cairo/hints/span_slice.cairo similarity index 100% rename from cairo_programs/cairo/hints/span_life.cairo rename to cairo_programs/cairo/hints/span_slice.cairo