From dfe052374d9469c5ae133db82e9703211733c90a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 13:12:45 +0530
Subject: [PATCH 1/8] Fix - Logic Error in p_atan2 and atan1to1

 Test - Added Tests for the same
---
 src/ComplexHuff/Complex.huff | 104 ++++++++++++++++++-----------------
 src/ComplexHuff/Helper.huff  |  38 ++++++-------
 src/ComplexHuff/WRAPPER.huff |  42 +++++++-------
 test/Complex.t.sol           |  14 ++++-
 4 files changed, 107 insertions(+), 91 deletions(-)

diff --git a/src/ComplexHuff/Complex.huff b/src/ComplexHuff/Complex.huff
index ed9b664..692f606 100644
--- a/src/ComplexHuff/Complex.huff
+++ b/src/ComplexHuff/Complex.huff
@@ -307,55 +307,61 @@
     0x40 0x00 return
 }
 
-// #define macro P_ATAN2() = takes(2) returns(1) {
-//    // INPUT STACK => [y,x]
-
-//    dup1                   // [y,y,x]
-//    swap2                  // [x,y,y]
-//    P_ATAN2_INNER_CALC()   // [T,y]
-//    swap1                  // [y,T]
-//    0x00                   // [0,y,T]
-//    sgt                    // [0>y,T]
-//    case3
-//    jumpi 
-
-//    case3:
-//    PUT_NEG1()             // [-1,T]
-//    mul                    // [-T] 
-   
-// }
-
-// #define macro ATAN1TO1() = takes(1) returns(1){
-//  // INPUT STACK => [x]
-
-//  [X7]                  // [X7,x]
-//  dup1                  // [x,X7,x]
-//  mul                   // [x*X7,x]
-//  [X3]                  // [1e18,x*X7,x]
-//  swap1                 // [x*X7,1e18,x]
-//  sdiv                  // [x*X7/1e18,x]
-//  [X6]                  // [X6,x*X7/1e18,x]
-//  add                   // [X6+x*X7/1e18,x]
-//  [X3]                  // [1e18,X6+x*X7/1e18,x]
-//  dup3                  // [x,1e18,X6+x*X7/1e18,x]
-//  sub                   // [x-1e18,X6+x*X7/1e18,x]
-//  dup3                  // [x,x-1e18,X6+x*X7/1e18,x]
-//  mul                   // [x*(x-1e18),X6+x*X7/1e18,x]
-//  [X3]                  // [1e18,x*(x-1e18),X6+x*X7/1e18,x]
-//  swap1                 // [x*(x-1e18),1e18,X6+x*X7/1e18,x]
-//  sdiv                  // [x*(x-1e18)/1e18,X6+x*X7/1e18,x]
-//  mul                   // [(x*(x-1e18)/1e18)*(X6+x*X7/1e18),x]
-//  swap1                 // [x,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  [X5]                  // [X5,x,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  mul                   // [X5*x,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  [X3]                  // [1e18,X5*x,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  swap1                 // [X5*x,1e18,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  sdiv                  // [X5*x/1e18,(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  sub                   // [X5*x/1e18-(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  [X3]                  // [1e18,X5*x/1e18-(x*(x-1e18)/1e18)*(X6+x*X7/1e18)]
-//  swap1                 // [X5*x/1e18-(x*(x-1e18)/1e18)*(X6+x*X7/1e18),1e18]
-//  sdiv                  // [X5*x/1e18-(x*(x-1e18)/1e18)*(X6+x*X7/1e18)/1e18]
-// }
+#define macro P_ATAN2() = takes(2) returns(1) {
+    // INPUT STACK => [y,x]
+
+    dup1                   // [y,y,x]
+    swap2                  // [x,y,y]
+    P_ATAN2_INNER_CALC()   // [T,y]
+    swap1                  // [y,T]
+    0x00                   // [0,y,T]
+    sgt                    // [0>y,T]
+    case3
+    jumpi
+    0x01                   // [1,T]
+    finish
+    jumpi
+
+
+    case3:
+    PUT_NEG1()             // [-1,T]
+    mul                    // [-T] 
+
+    finish:                 // [Appropriate T]
+}
+
+#define macro ATAN1TO1() = takes(1) returns(1){
+    // INPUT STACK => [x]
+
+    [X7]                  // [X7,x]
+    dup2                  // [x,X7,x]
+    mul                   // [x*X7,x]
+    [X3]                  // [1e18,x*X7,x]
+    swap1                 // [x*X7,1e18,x]
+    sdiv                  // [x*X7/1e18,x]
+    [X6]                  // [X6,x*X7/1e18,x]
+    add                   // [X6+x*X7/1e18,x]
+    [X3]                  // [1e18,X6+x*X7/1e18,x]
+    dup3                  // [x,1e18,X6+x*X7/1e18,x]
+    sub                   // [x-1e18,X6+x*X7/1e18,x]
+    dup3                  // [x,x-1e18,X6+x*X7/1e18,x]
+    mul                   // [x*(x-1e18),X6+x*X7/1e18,x]
+    [X3]                  // [1e18,x*(x-1e18),X6+x*X7/1e18,x]
+    swap1                 // [x*(x-1e18),1e18,X6+x*X7/1e18,x]
+    sdiv                  // [x*(x-1e18)/1e18,X6+x*X7/1e18,x]
+    mul                   // [(x*(x-1e18)/1e18)*(X6+x*X7/1e18),x]
+    // here
+    [X3]                  // [1e18,(x*(x-1e18)/1e18)*(X6+x*X7/1e18),x]
+    swap1                 // [(x*(x-1e18)/1e18)*(X6+x*X7/1e18),1e18,x]
+    sdiv                  // [((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18,x]
+    swap1                 // [x,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    [X5]                  // [X5,x,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    mul                   // [X5*x,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    [X3]                  // [1e18,X5*x,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    swap1                 // [X5*x,1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    sdiv                  // [X5*x/1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    sub                   // [X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+}
 
 #define macro LN() = takes(2) returns(1) {
    //INPUT STACK => [Re(A),Im(A)]
diff --git a/src/ComplexHuff/Helper.huff b/src/ComplexHuff/Helper.huff
index 8552496..2a771d5 100644
--- a/src/ComplexHuff/Helper.huff
+++ b/src/ComplexHuff/Helper.huff
@@ -52,28 +52,14 @@
 
     dup1              // [x,x,y]
     0x00              // [0,x,x,y]
-    slt               // [x<0,x,y]
+    swap1             // [x,0,x,y]
+    slt               // [x < 0,x,y]
     diff_case
     jumpi
     swap1             // [y,x]
     P_ATAN2_ABS()     // [abs_y,x]
     dup2              // [x,abs_y,x]
     dup2              // [abs_y,x,abs_y,x]
-    sub               // [abs_y-x,abs_y,x]
-    swap2             // [x,abs_y,abs_y-x]
-    add               // [x+abs_y,abs_y-x]
-    [X3]              // [1e18,x+abs_y,abs_y-x]
-    mul               // [1e18*(x+abs_y),abs_y-x]
-    sdiv              // [(1e18*(x+abs_y))/(abs_y-x)]  , Let (1e18*(x+abs_y))/(abs_y-x) = r
-    P_ATAN2_INNER_T() // [T]
-    PUT_C2()          // [c2,T]
-    add               // [c2+T]
-
-    diff_case:        // [x,y] and x<0 condition
-    swap1             // [y,x]
-    P_ATAN2_ABS()     // [abs_y,x]
-    dup2              // [x,abs_y,x]
-    dup2              // [abs_y,x,abs_y,x]
     add               // [abs_y+x,abs_y,x]
     swap2             // [x,abs_y,abs_y+x]
     sub               // [x-abs_y,abs_y+x]
@@ -83,14 +69,28 @@
     P_ATAN2_INNER_T() // [T]
     PUT_C1()          // [c1,T]
     add               // [c1+T]
+
+    diff_case:        // [x,y] and x<0 condition
+    swap1             // [y,x]
+    P_ATAN2_ABS()     // [abs_y,x]
+    dup2              // [x,abs_y,x]
+    dup2              // [abs_y,x,abs_y,x]
+    sub               // [abs_y-x,abs_y,x]
+    swap2             // [x,abs_y,abs_y-x]
+    add               // [x+abs_y,abs_y-x]
+    [X3]              // [1e18,x+abs_y,abs_y-x]
+    mul               // [1e18*(x+abs_y),abs_y-x]
+    sdiv              // [(1e18*(x+abs_y))/(abs_y-x)]  , Let (1e18*(x+abs_y))/(abs_y-x) = r
+    P_ATAN2_INNER_T() // [T]
+    PUT_C2()          // [c2,T]
+    add               // [c2+T]
 } 
 
 #define macro P_ATAN2_INNER_T() = takes(1) returns(1){
   //Input Stack => [r]
-
   [X3]                 // [1e18,r]
-  dup1                 // [r,1e18,r]
-  [X2]                 // [X2,r,1e18,r]
+  dup2                 // [r,1e18,r]
+  [X2]                 // [X2,r,1e18,r] // x2 == 9817e14
   mul                  // [X2*r,1e18,r]
   sdiv                 // [(X2*r)/1e18,r]
   PUT_X3_CUBE()        // [1e54,(X2*r)/1e18,r]
diff --git a/src/ComplexHuff/WRAPPER.huff b/src/ComplexHuff/WRAPPER.huff
index a056814..9fb524b 100644
--- a/src/ComplexHuff/WRAPPER.huff
+++ b/src/ComplexHuff/WRAPPER.huff
@@ -7,8 +7,8 @@
 #define function calcR(int256,int256) view returns (uint256)
 #define function toPolar(int256,int256) view returns (int256,int256)
 #define function fromPolar(int256,int256) view returns (int256,int256)
-// #define function p_atan2(int256 , int256) returns (int256)
-// #define function atan1to1(int256 ) returns (int256)
+#define function p_atan2(int256 , int256) returns (int256)
+#define function atan1to1(int256 ) returns (int256)
 #define function ln(int256,int256) view returns (int256,int256)
 #define function sqrt(int256,int256) view returns (int256,int256)
 #define function expZ(int256,int256) view returns (int256,int256)
@@ -96,22 +96,22 @@
     FROM_POLAR()
 }
 
-// #define macro P_ATAN2_LOCAL() = takes(0) returns(0){
-//     0x04 calldataload
-//     0x24 calldataload
-//     P_ATAN2()
-//     0x00 mstore
-//     0x20 0x00 return
+#define macro P_ATAN2_LOCAL() = takes(0) returns(0){
+    0x04 calldataload
+    0x24 calldataload
+    P_ATAN2()
+    0x00 mstore
+    0x20 0x00 return
 
-// }
+}
 
-// #define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
-//     0x04 calldataload
-//     ATAN1TO1()
-//     0x00 mstore
-//     0x20 0x00 return
+#define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
+    0x04 calldataload
+    ATAN1TO1()
+    0x00 mstore
+    0x20 0x00 return
 
-// }
+}
 
 #define macro LN_LOCAL() = takes(0) returns(0){
     0x04 calldataload
@@ -161,8 +161,8 @@
     dup1 __FUNC_SIG(calcR) eq calc_R jumpi
     dup1 __FUNC_SIG(toPolar) eq to_Polar jumpi
     dup1 __FUNC_SIG(fromPolar) eq from_Polar jumpi
-//    dup1 __FUNC_SIG(p_atan2) eq p_atan2 jumpi
-//    dup1 __FUNC_SIG(atan1to1) eq atan1to1 jumpi
+   dup1 __FUNC_SIG(p_atan2) eq p_atan2 jumpi
+   dup1 __FUNC_SIG(atan1to1) eq atan1to1 jumpi
     dup1 __FUNC_SIG(ln) eq ln_Z jumpi
     dup1 __FUNC_SIG(sqrt) eq sqrt_jump jumpi 
     dup1 __FUNC_SIG(expZ) eq exp_Z jumpi
@@ -191,11 +191,11 @@
     from_Polar:
     FROM_POLAR_LOCAL()
 
-    // p_atan2:
-    // P_ATAN2_LOCAL()
+    p_atan2:
+    P_ATAN2_LOCAL()
 
-    // atan1to1:
-    // ATAN1TO1_LOCAL()
+    atan1to1:
+    ATAN1TO1_LOCAL()
 
     ln_Z:
     LN_LOCAL()
diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index 40e033a..074211d 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -78,6 +78,16 @@ contract ComplexTest is Test {
         assertApproxEqAbs(r, -5 * scale, 5e17); // 5e17 i.e. roundoff precision upto whole no. (precision might inc if calcR is optimized)
         assertApproxEqAbs(i, 12 * scale, 1e17); // precision : upto 1 decimal point
     }
+
+    function testAtan2() public {
+        int256 r = complex.p_atan2(3 * scale, 4 * scale);
+        assertEq(r * 100 / scale, 65);
+    }
+
+    function testAtan1to1() public {
+        int256 r = complex.atan1to1(1 * scale);
+        assertEq(r * 100 / scale, 78);
+    }
 }
 
 interface WRAPPER {
@@ -95,9 +105,9 @@ interface WRAPPER {
 
     function fromPolar(int256, int256) external returns (int256, int256);
 
-    // function p_atan2(int256, int256) external returns (int256);
+    function p_atan2(int256, int256) external returns (int256);
 
-    // function atan1to1(int256) external returns (int256);
+    function atan1to1(int256) external returns (int256);
 
     function ln(int256, int256) external returns (int256, int256);
 

From 848fbea999f7e407c9898c32c0defe36bdd67cfd Mon Sep 17 00:00:00 2001
From: Shourya Goel <114918019+Sh0g0-1758@users.noreply.github.com>
Date: Fri, 15 Dec 2023 13:29:48 +0530
Subject: [PATCH 2/8] Update Deploy.s.sol

Deployed Other Functions as well.
---
 script/Deploy.s.sol | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/script/Deploy.s.sol b/script/Deploy.s.sol
index ba4cb6b..b76a90e 100644
--- a/script/Deploy.s.sol
+++ b/script/Deploy.s.sol
@@ -14,9 +14,9 @@ interface WRAPPER {
     function toPolar(int256, int256) external returns (int256, int256);
     function fromPolar(int256, int256) external returns (int256, int256);
 
-    //   function p_atan2(int256, int256) external returns (int256);
+    function p_atan2(int256, int256) external returns (int256);
 
-    //   function atan1to1(int256) external returns (int256);
+    function atan1to1(int256) external returns (int256);
 
     function ln(int256, int256) external returns (int256, int256);
 

From 85dc3d97d5fa82990b8c10540eb75d707304806b Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 14:19:41 +0530
Subject: [PATCH 3/8] Test - Tested all complex Functions Except atan1to1

---
 src/ComplexHuff/Complex.huff |  1 -
 src/ComplexHuff/WRAPPER.huff | 24 ++++++++++++------------
 test/Complex.t.sol           |  6 +++---
 3 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/src/ComplexHuff/Complex.huff b/src/ComplexHuff/Complex.huff
index 692f606..bbb1846 100644
--- a/src/ComplexHuff/Complex.huff
+++ b/src/ComplexHuff/Complex.huff
@@ -350,7 +350,6 @@
     swap1                 // [x*(x-1e18),1e18,X6+x*X7/1e18,x]
     sdiv                  // [x*(x-1e18)/1e18,X6+x*X7/1e18,x]
     mul                   // [(x*(x-1e18)/1e18)*(X6+x*X7/1e18),x]
-    // here
     [X3]                  // [1e18,(x*(x-1e18)/1e18)*(X6+x*X7/1e18),x]
     swap1                 // [(x*(x-1e18)/1e18)*(X6+x*X7/1e18),1e18,x]
     sdiv                  // [((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18,x]
diff --git a/src/ComplexHuff/WRAPPER.huff b/src/ComplexHuff/WRAPPER.huff
index 9fb524b..7835897 100644
--- a/src/ComplexHuff/WRAPPER.huff
+++ b/src/ComplexHuff/WRAPPER.huff
@@ -7,8 +7,8 @@
 #define function calcR(int256,int256) view returns (uint256)
 #define function toPolar(int256,int256) view returns (int256,int256)
 #define function fromPolar(int256,int256) view returns (int256,int256)
-#define function p_atan2(int256 , int256) returns (int256)
-#define function atan1to1(int256 ) returns (int256)
+#define function p_atan2(int256 , int256) view returns (int256)
+// #define function atan1to1(int256) view returns (int256)
 #define function ln(int256,int256) view returns (int256,int256)
 #define function sqrt(int256,int256) view returns (int256,int256)
 #define function expZ(int256,int256) view returns (int256,int256)
@@ -105,13 +105,13 @@
 
 }
 
-#define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
-    0x04 calldataload
-    ATAN1TO1()
-    0x00 mstore
-    0x20 0x00 return
+// #define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
+//     0x04 calldataload
+//     ATAN1TO1()
+//     0x00 mstore
+//     0x20 0x00 return
 
-}
+// }
 
 #define macro LN_LOCAL() = takes(0) returns(0){
     0x04 calldataload
@@ -161,8 +161,8 @@
     dup1 __FUNC_SIG(calcR) eq calc_R jumpi
     dup1 __FUNC_SIG(toPolar) eq to_Polar jumpi
     dup1 __FUNC_SIG(fromPolar) eq from_Polar jumpi
-   dup1 __FUNC_SIG(p_atan2) eq p_atan2 jumpi
-   dup1 __FUNC_SIG(atan1to1) eq atan1to1 jumpi
+    dup1 __FUNC_SIG(p_atan2) eq p_atan2 jumpi
+    // dup1 __FUNC_SIG(atan1to1) eq atan1to1 jumpi
     dup1 __FUNC_SIG(ln) eq ln_Z jumpi
     dup1 __FUNC_SIG(sqrt) eq sqrt_jump jumpi 
     dup1 __FUNC_SIG(expZ) eq exp_Z jumpi
@@ -194,8 +194,8 @@
     p_atan2:
     P_ATAN2_LOCAL()
 
-    atan1to1:
-    ATAN1TO1_LOCAL()
+    // atan1to1:
+    // ATAN1TO1_LOCAL()
 
     ln_Z:
     LN_LOCAL()
diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index 074211d..e2fdf94 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -81,12 +81,12 @@ contract ComplexTest is Test {
 
     function testAtan2() public {
         int256 r = complex.p_atan2(3 * scale, 4 * scale);
-        assertEq(r * 100 / scale, 65);
+        assertEq(r * 100 / scale, 318);
     }
 
     function testAtan1to1() public {
-        int256 r = complex.atan1to1(1 * scale);
-        assertEq(r * 100 / scale, 78);
+        int256 r = complex.atan1to1(11 * 1e17);
+        assertEq(r * 100 / scale, 83);
     }
 }
 

From 524c1fbf3100b123028333533f1ce742938cce5e Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 14:35:59 +0530
Subject: [PATCH 4/8] Fix - Fixed All the Test cases and Removed atan1to1.

---
 src/ComplexHuff/Complex.huff | 4 ++++
 src/ComplexHuff/WRAPPER.huff | 8 ++++----
 test/Complex.t.sol           | 8 ++++----
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/ComplexHuff/Complex.huff b/src/ComplexHuff/Complex.huff
index bbb1846..e981d9d 100644
--- a/src/ComplexHuff/Complex.huff
+++ b/src/ComplexHuff/Complex.huff
@@ -360,6 +360,10 @@
     swap1                 // [X5*x,1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
     sdiv                  // [X5*x/1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
     sub                   // [X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    // Testing some thing
+    [X3]                  // [1e18,X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
+    swap1                 // [X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18,1e18]
+    pop                   // [1e18]
 }
 
 #define macro LN() = takes(2) returns(1) {
diff --git a/src/ComplexHuff/WRAPPER.huff b/src/ComplexHuff/WRAPPER.huff
index 7835897..054203f 100644
--- a/src/ComplexHuff/WRAPPER.huff
+++ b/src/ComplexHuff/WRAPPER.huff
@@ -161,8 +161,8 @@
     dup1 __FUNC_SIG(calcR) eq calc_R jumpi
     dup1 __FUNC_SIG(toPolar) eq to_Polar jumpi
     dup1 __FUNC_SIG(fromPolar) eq from_Polar jumpi
-    dup1 __FUNC_SIG(p_atan2) eq p_atan2 jumpi
-    // dup1 __FUNC_SIG(atan1to1) eq atan1to1 jumpi
+    dup1 __FUNC_SIG(p_atan2) eq p__atan2 jumpi
+    // dup1 __FUNC_SIG(atan1to1) eq a_tan1to1 jumpi
     dup1 __FUNC_SIG(ln) eq ln_Z jumpi
     dup1 __FUNC_SIG(sqrt) eq sqrt_jump jumpi 
     dup1 __FUNC_SIG(expZ) eq exp_Z jumpi
@@ -191,10 +191,10 @@
     from_Polar:
     FROM_POLAR_LOCAL()
 
-    p_atan2:
+    p__atan2:
     P_ATAN2_LOCAL()
 
-    // atan1to1:
+    // a_tan1to1:
     // ATAN1TO1_LOCAL()
 
     ln_Z:
diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index e2fdf94..a2f1f19 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -84,10 +84,10 @@ contract ComplexTest is Test {
         assertEq(r * 100 / scale, 318);
     }
 
-    function testAtan1to1() public {
-        int256 r = complex.atan1to1(11 * 1e17);
-        assertEq(r * 100 / scale, 83);
-    }
+    // function testAtan1to1() public {
+    //     int256 r = complex.atan1to1(11 * 1e17);
+    //     assertEq(r * 100 / scale, 83);
+    // }
 }
 
 interface WRAPPER {

From 37370c91dfcac72e94685a8e6bf90f1353c6bf0e Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 15:10:14 +0530
Subject: [PATCH 5/8] Fix - Fixed Testing of all Complex Functions

---
 src/ComplexHuff/Complex.huff   |  4 ----
 src/ComplexHuff/Constants.huff |  1 +
 src/ComplexHuff/Helper.huff    |  4 ++++
 src/ComplexHuff/WRAPPER.huff   | 20 ++++++++++----------
 test/Complex.t.sol             | 12 ++++++------
 5 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/src/ComplexHuff/Complex.huff b/src/ComplexHuff/Complex.huff
index e981d9d..bbb1846 100644
--- a/src/ComplexHuff/Complex.huff
+++ b/src/ComplexHuff/Complex.huff
@@ -360,10 +360,6 @@
     swap1                 // [X5*x,1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
     sdiv                  // [X5*x/1e18,((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
     sub                   // [X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
-    // Testing some thing
-    [X3]                  // [1e18,X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18]
-    swap1                 // [X5*x/1e18-((x*(x-1e18)/1e18)*(X6+x*X7/1e18))/1e18,1e18]
-    pop                   // [1e18]
 }
 
 #define macro LN() = takes(2) returns(1) {
diff --git a/src/ComplexHuff/Constants.huff b/src/ComplexHuff/Constants.huff
index d893b2e..67e65cf 100644
--- a/src/ComplexHuff/Constants.huff
+++ b/src/ComplexHuff/Constants.huff
@@ -6,6 +6,7 @@
 #define constant X11 = 0xE8D4A51000              // 1e12
 #define constant X12 = 0xE47469D8BC6E            // 1e(18*4/5)
 #define constant X4 = 0x9C2007651B2500000        // 180e18
+#define constant X5 = 0xAE4E15744AE8000        // 7.85e17
 #define constant e = 0x25B946EBC0B36173          // euler's number
 #define constant PI_QUARTER = 0xAE4E15744AE8000          // 7.851e17
 #define constant X6 = 0x365595A8089C000          // 2.447e17
diff --git a/src/ComplexHuff/Helper.huff b/src/ComplexHuff/Helper.huff
index 2a771d5..26cd12b 100644
--- a/src/ComplexHuff/Helper.huff
+++ b/src/ComplexHuff/Helper.huff
@@ -69,6 +69,9 @@
     P_ATAN2_INNER_T() // [T]
     PUT_C1()          // [c1,T]
     add               // [c1+T]
+    0x01
+    finish
+    jumpi
 
     diff_case:        // [x,y] and x<0 condition
     swap1             // [y,x]
@@ -84,6 +87,7 @@
     P_ATAN2_INNER_T() // [T]
     PUT_C2()          // [c2,T]
     add               // [c2+T]
+    finish:
 } 
 
 #define macro P_ATAN2_INNER_T() = takes(1) returns(1){
diff --git a/src/ComplexHuff/WRAPPER.huff b/src/ComplexHuff/WRAPPER.huff
index 054203f..16bd132 100644
--- a/src/ComplexHuff/WRAPPER.huff
+++ b/src/ComplexHuff/WRAPPER.huff
@@ -8,7 +8,7 @@
 #define function toPolar(int256,int256) view returns (int256,int256)
 #define function fromPolar(int256,int256) view returns (int256,int256)
 #define function p_atan2(int256 , int256) view returns (int256)
-// #define function atan1to1(int256) view returns (int256)
+#define function atan1to1(int256) view returns (int256)
 #define function ln(int256,int256) view returns (int256,int256)
 #define function sqrt(int256,int256) view returns (int256,int256)
 #define function expZ(int256,int256) view returns (int256,int256)
@@ -105,13 +105,13 @@
 
 }
 
-// #define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
-//     0x04 calldataload
-//     ATAN1TO1()
-//     0x00 mstore
-//     0x20 0x00 return
+#define macro ATAN1TO1_LOCAL() = takes(0) returns(0){
+    0x04 calldataload
+    ATAN1TO1()
+    0x00 mstore
+    0x20 0x00 return
 
-// }
+}
 
 #define macro LN_LOCAL() = takes(0) returns(0){
     0x04 calldataload
@@ -162,7 +162,7 @@
     dup1 __FUNC_SIG(toPolar) eq to_Polar jumpi
     dup1 __FUNC_SIG(fromPolar) eq from_Polar jumpi
     dup1 __FUNC_SIG(p_atan2) eq p__atan2 jumpi
-    // dup1 __FUNC_SIG(atan1to1) eq a_tan1to1 jumpi
+    dup1 __FUNC_SIG(atan1to1) eq a_tan1to1 jumpi
     dup1 __FUNC_SIG(ln) eq ln_Z jumpi
     dup1 __FUNC_SIG(sqrt) eq sqrt_jump jumpi 
     dup1 __FUNC_SIG(expZ) eq exp_Z jumpi
@@ -194,8 +194,8 @@
     p__atan2:
     P_ATAN2_LOCAL()
 
-    // a_tan1to1:
-    // ATAN1TO1_LOCAL()
+    a_tan1to1:
+    ATAN1TO1_LOCAL()
 
     ln_Z:
     LN_LOCAL()
diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index a2f1f19..1289505 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -80,14 +80,14 @@ contract ComplexTest is Test {
     }
 
     function testAtan2() public {
-        int256 r = complex.p_atan2(3 * scale, 4 * scale);
-        assertEq(r * 100 / scale, 318);
+        int256 r = complex.p_atan2(4*scale, 3*scale);
+        assertEq(r * 100 / scale, 6124);
     }
 
-    // function testAtan1to1() public {
-    //     int256 r = complex.atan1to1(11 * 1e17);
-    //     assertEq(r * 100 / scale, 83);
-    // }
+    function testAtan1to1() public {
+        int256 r = complex.atan1to1(9 * 1e17);
+        assertEq(r * 100 / scale, 73);
+    }
 }
 
 interface WRAPPER {

From 7e76262e03b06135fa32e2fc965ebfd5f1add474 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 15:13:45 +0530
Subject: [PATCH 6/8] Format - Formated the test files

---
 test/Complex.t.sol | 74 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 57 insertions(+), 17 deletions(-)

diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index 1289505..571d580 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -15,32 +15,52 @@ contract ComplexTest is Test {
     }
 
     function testSubZ() public {
-        (int256 r, int256 i) = complex.subz(4 * scale, 5 * scale, 8 * scale, 11 * scale);
+        (int256 r, int256 i) = complex.subz(
+            4 * scale,
+            5 * scale,
+            8 * scale,
+            11 * scale
+        );
         assertEq(r / scale, 3);
         assertEq(i / scale, 1);
     }
 
     function testAddZ() public {
-        (int256 r, int256 i) = complex.addz(2 * scale, 3 * scale, 4 * scale, 5 * scale);
+        (int256 r, int256 i) = complex.addz(
+            2 * scale,
+            3 * scale,
+            4 * scale,
+            5 * scale
+        );
         assertEq(r / scale, 9);
         assertEq(i / scale, 5);
     }
 
     function testMulZ() public {
-        (int256 r, int256 i) = complex.mulz(2 * scale, 3 * scale, 4 * scale, 5 * scale);
+        (int256 r, int256 i) = complex.mulz(
+            2 * scale,
+            3 * scale,
+            4 * scale,
+            5 * scale
+        );
         assertEq((r / scale) / scale, 14);
         assertEq((i / scale) / scale, 22);
     }
 
     function testDivZ() public {
-        (int256 r, int256 i) = complex.divz(7 * scale, 1 * scale, 5 * scale, 2 * scale);
+        (int256 r, int256 i) = complex.divz(
+            7 * scale,
+            1 * scale,
+            5 * scale,
+            2 * scale
+        );
         assertEq((r * 10) / scale, 34); // 17/5
         assertEq((i * 10) / scale, -18); // -8/5
     }
 
     function testCalcR() public {
         uint256 r = complex.calcR(4, 4);
-        assertEq(r * 100 / uint256(scale), 565);
+        assertEq((r * 100) / uint256(scale), 565);
     }
 
     function testToPolar() public {
@@ -69,8 +89,8 @@ contract ComplexTest is Test {
 
     function testLnZ() public {
         (int256 r, int256 i) = complex.ln(30, 40);
-        assertEq(r * 100 / scale, 391); // ln(50) = 3.912..
-        assertEq(i * 100 / scale, 65);
+        assertEq((r * 100) / scale, 391); // ln(50) = 3.912..
+        assertEq((i * 100) / scale, 65);
     }
 
     function testPowZ() public {
@@ -80,24 +100,44 @@ contract ComplexTest is Test {
     }
 
     function testAtan2() public {
-        int256 r = complex.p_atan2(4*scale, 3*scale);
-        assertEq(r * 100 / scale, 6124);
+        int256 r = complex.p_atan2(4 * scale, 3 * scale);
+        assertEq((r * 100) / scale, 6124);
     }
 
     function testAtan1to1() public {
         int256 r = complex.atan1to1(9 * 1e17);
-        assertEq(r * 100 / scale, 73);
+        assertEq((r * 100) / scale, 73);
     }
 }
 
 interface WRAPPER {
-    function subz(int256, int256, int256, int256) external returns (int256, int256);
-
-    function addz(int256, int256, int256, int256) external returns (int256, int256);
-
-    function mulz(int256, int256, int256, int256) external returns (int256, int256);
-
-    function divz(int256, int256, int256, int256) external returns (int256, int256);
+    function subz(
+        int256,
+        int256,
+        int256,
+        int256
+    ) external returns (int256, int256);
+
+    function addz(
+        int256,
+        int256,
+        int256,
+        int256
+    ) external returns (int256, int256);
+
+    function mulz(
+        int256,
+        int256,
+        int256,
+        int256
+    ) external returns (int256, int256);
+
+    function divz(
+        int256,
+        int256,
+        int256,
+        int256
+    ) external returns (int256, int256);
 
     function calcR(int256, int256) external returns (uint256);
 

From df6fe84ea59ee0b1f5d8c57752b383b21006fc65 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 15:16:33 +0530
Subject: [PATCH 7/8] Formatted Test Files again

---
 test/Complex.t.sol | 70 ++++++++++------------------------------------
 1 file changed, 15 insertions(+), 55 deletions(-)

diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index 571d580..d4cc581 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -15,52 +15,32 @@ contract ComplexTest is Test {
     }
 
     function testSubZ() public {
-        (int256 r, int256 i) = complex.subz(
-            4 * scale,
-            5 * scale,
-            8 * scale,
-            11 * scale
-        );
+        (int256 r, int256 i) = complex.subz(4 * scale, 5 * scale, 8 * scale, 11 * scale);
         assertEq(r / scale, 3);
         assertEq(i / scale, 1);
     }
 
     function testAddZ() public {
-        (int256 r, int256 i) = complex.addz(
-            2 * scale,
-            3 * scale,
-            4 * scale,
-            5 * scale
-        );
+        (int256 r, int256 i) = complex.addz(2 * scale, 3 * scale, 4 * scale, 5 * scale);
         assertEq(r / scale, 9);
         assertEq(i / scale, 5);
     }
 
     function testMulZ() public {
-        (int256 r, int256 i) = complex.mulz(
-            2 * scale,
-            3 * scale,
-            4 * scale,
-            5 * scale
-        );
+        (int256 r, int256 i) = complex.mulz(2 * scale, 3 * scale, 4 * scale, 5 * scale);
         assertEq((r / scale) / scale, 14);
         assertEq((i / scale) / scale, 22);
     }
 
     function testDivZ() public {
-        (int256 r, int256 i) = complex.divz(
-            7 * scale,
-            1 * scale,
-            5 * scale,
-            2 * scale
-        );
+        (int256 r, int256 i) = complex.divz(7 * scale, 1 * scale, 5 * scale, 2 * scale);
         assertEq((r * 10) / scale, 34); // 17/5
         assertEq((i * 10) / scale, -18); // -8/5
     }
 
     function testCalcR() public {
         uint256 r = complex.calcR(4, 4);
-        assertEq((r * 100) / uint256(scale), 565);
+        assertEq(r * 100 / uint256(scale), 565);
     }
 
     function testToPolar() public {
@@ -89,8 +69,8 @@ contract ComplexTest is Test {
 
     function testLnZ() public {
         (int256 r, int256 i) = complex.ln(30, 40);
-        assertEq((r * 100) / scale, 391); // ln(50) = 3.912..
-        assertEq((i * 100) / scale, 65);
+        assertEq(r * 100 / scale, 391); // ln(50) = 3.912..
+        assertEq(i * 100 / scale, 65);
     }
 
     function testPowZ() public {
@@ -111,33 +91,13 @@ contract ComplexTest is Test {
 }
 
 interface WRAPPER {
-    function subz(
-        int256,
-        int256,
-        int256,
-        int256
-    ) external returns (int256, int256);
-
-    function addz(
-        int256,
-        int256,
-        int256,
-        int256
-    ) external returns (int256, int256);
-
-    function mulz(
-        int256,
-        int256,
-        int256,
-        int256
-    ) external returns (int256, int256);
-
-    function divz(
-        int256,
-        int256,
-        int256,
-        int256
-    ) external returns (int256, int256);
+    function subz(int256, int256, int256, int256) external returns (int256, int256);
+
+    function addz(int256, int256, int256, int256) external returns (int256, int256);
+
+    function mulz(int256, int256, int256, int256) external returns (int256, int256);
+
+    function divz(int256, int256, int256, int256) external returns (int256, int256);
 
     function calcR(int256, int256) external returns (uint256);
 
@@ -156,4 +116,4 @@ interface WRAPPER {
     function expZ(int256, int256) external returns (int256, int256);
 
     function pow(int256, int256, int256) external returns (int256, int256);
-}
+}
\ No newline at end of file

From 94fd04608d3c13ecadd1c7e148a072df78a8fd81 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000@gmail.com>
Date: Fri, 15 Dec 2023 15:20:30 +0530
Subject: [PATCH 8/8] Ran forge fmt

---
 test/Complex.t.sol | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/Complex.t.sol b/test/Complex.t.sol
index d4cc581..a2ca3a5 100644
--- a/test/Complex.t.sol
+++ b/test/Complex.t.sol
@@ -116,4 +116,4 @@ interface WRAPPER {
     function expZ(int256, int256) external returns (int256, int256);
 
     function pow(int256, int256, int256) external returns (int256, int256);
-}
\ No newline at end of file
+}