diff --git a/cairo_native/libfuncs/felt252/index.html b/cairo_native/libfuncs/felt252/index.html index 059e81109..7b5fb27e3 100644 --- a/cairo_native/libfuncs/felt252/index.html +++ b/cairo_native/libfuncs/felt252/index.html @@ -1 +1 @@ -cairo_native::libfuncs::felt252 - Rust

Module cairo_native::libfuncs::felt252

source ·
Expand description

Functions§

  • Select and call the correct libfunc builder function from the selector.
  • Generate MLIR operations for the following libfuncs:
  • Generate MLIR operations for the felt252_const libfunc.
  • Generate MLIR operations for the felt252_is_zero libfunc.
\ No newline at end of file +cairo_native::libfuncs::felt252 - Rust

Module cairo_native::libfuncs::felt252

source ·
Expand description

Functions§

  • Select and call the correct libfunc builder function from the selector.
  • Generate MLIR operations for the following libfuncs:
  • Generate MLIR operations for the felt252_const libfunc.
  • Generate MLIR operations for the felt252_is_zero libfunc.
\ No newline at end of file diff --git a/src/cairo_native/libfuncs/felt252.rs.html b/src/cairo_native/libfuncs/felt252.rs.html index aeb37cfc6..99a2249ca 100644 --- a/src/cairo_native/libfuncs/felt252.rs.html +++ b/src/cairo_native/libfuncs/felt252.rs.html @@ -698,6 +698,106 @@ 698 699 700 +701 +702 +703 +704 +705 +706 +707 +708 +709 +710 +711 +712 +713 +714 +715 +716 +717 +718 +719 +720 +721 +722 +723 +724 +725 +726 +727 +728 +729 +730 +731 +732 +733 +734 +735 +736 +737 +738 +739 +740 +741 +742 +743 +744 +745 +746 +747 +748 +749 +750 +751 +752 +753 +754 +755 +756 +757 +758 +759 +760 +761 +762 +763 +764 +765 +766 +767 +768 +769 +770 +771 +772 +773 +774 +775 +776 +777 +778 +779 +780 +781 +782 +783 +784 +785 +786 +787 +788 +789 +790 +791 +792 +793 +794 +795 +796 +797 +798 +799 +800
//! # `Felt`-related libfuncs
 
 use super::LibfuncHelper;
@@ -1119,9 +1219,13 @@
             }
         };
 
-        // TODO: Add test program for `felt252_div`.
+        static ref FELT252_DIV: (String, Program) = load_cairo! {
+            fn run_test(lhs: felt252, rhs: felt252) -> felt252 {
+                felt252_div(lhs, rhs.try_into().unwrap())
+            }
+        };
 
-        // TODO: Add test program for `felt252_add_const`.
+        // TODO: Add test program for `felt252_add_const`.
         // TODO: Add test program for `felt252_sub_const`.
         // TODO: Add test program for `felt252_mul_const`.
         // TODO: Add test program for `felt252_div_const`.
@@ -1372,6 +1476,102 @@
         );
     }
 
+    #[test]
+    fn felt252_div() {
+        // Helper function to run the test and extract the return value.
+        let run_test = |lhs, rhs| run_program(&FELT252_DIV, "run_test", &[lhs, rhs]).return_value;
+
+        // Helper function to extract the result struct field from the return value.
+        let extract_struct_field = |result| match result {
+            JitValue::Enum { value, .. } => match *value {
+                JitValue::Struct { fields, .. } => fields[0].clone(),
+                _ => panic!("Expected Struct"),
+            },
+            _ => panic!("Expected Enum"),
+        };
+
+        // Helper function to assert that a division panics.
+        let assert_panics = |lhs, rhs| match run_test(lhs, rhs) {
+            JitValue::Enum { debug_name, .. } => {
+                assert_eq!(
+                    debug_name,
+                    Some("core::panics::PanicResult::<(core::felt252,)>".into())
+                );
+            }
+            _ => panic!("division by 0 is expected to panic"),
+        };
+
+        // Division by zero is expected to panic.
+        assert_panics(JitValue::felt_str("0"), JitValue::felt_str("0"));
+        assert_panics(JitValue::felt_str("1"), JitValue::felt_str("0"));
+        assert_panics(JitValue::felt_str("-2"), JitValue::felt_str("0"));
+
+        // Test cases for valid division results.
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("0"), JitValue::felt_str("1"))),
+            JitValue::felt_str("0")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("0"), JitValue::felt_str("-2"))),
+            JitValue::felt_str("0")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("0"), JitValue::felt_str("-1"))),
+            JitValue::felt_str("0")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("1"), JitValue::felt_str("1"))),
+            JitValue::felt_str("1")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("1"), JitValue::felt_str("-2"))),
+            JitValue::felt_str(
+                "1809251394333065606848661391547535052811553607665798349986546028067936010240"
+            )
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("1"), JitValue::felt_str("-1"))),
+            JitValue::felt_str("-1")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-2"), JitValue::felt_str("1"))),
+            JitValue::felt_str("-2")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-2"), JitValue::felt_str("-2"))),
+            JitValue::felt_str("1")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-2"), JitValue::felt_str("-1"))),
+            JitValue::felt_str("2")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-1"), JitValue::felt_str("1"))),
+            JitValue::felt_str("-1")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-1"), JitValue::felt_str("-2"))),
+            JitValue::felt_str(
+                "1809251394333065606848661391547535052811553607665798349986546028067936010241"
+            )
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("-1"), JitValue::felt_str("-1"))),
+            JitValue::felt_str("1")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(JitValue::felt_str("6"), JitValue::felt_str("2"))),
+            JitValue::felt_str("3")
+        );
+        assert_eq!(
+            extract_struct_field(run_test(
+                JitValue::felt_str("1000"),
+                JitValue::felt_str("2")
+            )),
+            JitValue::felt_str("500")
+        );
+    }
+
     #[test]
     fn felt252_const() {
         assert_eq!(