Skip to content
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

Implemented unit tests for rotateComplex and LINEEQUATION #26

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface WRAPPER {
function expZ(int256, int256) external returns (int256, int256);

function pow(int256, int256, int256) external returns (int256, int256);

function rotateComplexz(int256, int256, int256) external returns (int256, int256);

function lineequationz(int256, int256, int256, int256) external returns (int256, int256);
}

contract Deploy is Script {
Expand Down
40 changes: 39 additions & 1 deletion src/ComplexHuff/Complex.huff
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define constant VALUE_LOCATION = FREE_STORAGE_POINTER()

#define macro ADD_Z() = takes (0) returns (0) {
#define macro ADD_Z() = takes (4) returns (2) {
add // [Re(A)+Re(B),Im(A),Im(B)]
swap2 // [Im(B),Im(A),Re(A)+Re(B)]
add // [Im(B)+Im(A),Re(A)+Re(B)]
Expand Down Expand Up @@ -393,4 +393,42 @@
swap2
sdiv //[r**n*cos(x)/1e18,r**n*sin(x)/1e18]

}

// to rotate a complex number by any angle
#define macro rotateComplex() = takes(3) returns(2) {
// INPUT STACK => [Re(A), Im(A), angle]

TO_POLAR() // [r, theta,angle]
swap2 // [angle,theta,r]

// Add the rotation angle
add // [theta + angle, r]

FROM_POLAR() // [Re(A) after rotation, Im(A) after rotation]
}

// to find the the equation of a line passing through two complex numbers
#define macro LINEEQUATION() = takes(4) returns(2) {
//INPUT STACK => [RE(a),RE(b),IM(a),IM(b)]
swap1 //[rb,ra,ia,ib]
swap2 //[ia,ra,rb,ib]
swap1 //[ra,ia,rb,ib]
dup3 //[rb,ra,ia,rb,ib]
sub //[rb-ra,ia,rb,ib]
swap1 //[ia,rb-ra,rb,ib]
dup4 //[ib,ia,rb-ra,rb,ib]
sub //[ib-ia,rb-ra,rb,ib]
[X3] //[1e18,ib-ia,rb-ra,rb,ib]
mul //[1e18*ib-ia,rb-ra,rb,ib]
sdiv //[1e18*(ib-ia)/rb-ra,rb,ib]
dup1 //[m,m,rb,ib]
swap2 //[rb,m,m,ib]
mul //[rb*m,m,ib]
swap1 //[m,rb*m,ib]
swap2 //[ib,rb*m,m]
[X3] //[1e18,ib,rb*m,m]
mul //[1e18*ib,rb*m,m]
sub //[1e18*ib-rb*m,m]
swap1 //[m,c]
}
40 changes: 39 additions & 1 deletion src/ComplexHuff/WRAPPER.huff
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define function sqrt(int256,int256) view returns (int256,int256)
#define function expZ(int256,int256) view returns (int256,int256)
#define function pow(int256,int256,int256) view returns (int256,int256)
#define function rotateComplexz(int256,int256,int256) view returns (int256,int256)
#define function lineequationz(int256,int256,int256,int256) view returns (int256,int256)


#define macro SUB_Z_LOCAL() = takes (0) returns (0) {
Expand Down Expand Up @@ -148,6 +150,35 @@
}


#define macro rotateComplex_LOCAL() = takes(0) returns(0){
0x04 calldataload
0x24 calldataload
0x44 calldataload
rotateComplex()
0x00 mstore
0x20 mstore
0x40 0x00 return
}



#define macro LINEEQUATION_LOCAL() = takes (0) returns (0) {
// Load value from storage.
0x04 calldataload //[r1]
0x24 calldataload //[r2]
0x44 calldataload //[i1]
0x64 calldataload //[i2, i1, r2, r1]
LINEEQUATION()
// Store value in memory.
0x00 mstore
0x20 mstore
// Return value
0x40 0x00 return
}





#define macro MAIN() = takes (0) returns (0) {
// Identify which function is being called.
Expand All @@ -164,7 +195,10 @@
dup1 __FUNC_SIG(ln) eq ln_Z jumpi
dup1 __FUNC_SIG(sqrt) eq sqrt_jump jumpi
dup1 __FUNC_SIG(expZ) eq exp_Z jumpi
dup1 __FUNC_SIG(pow) eq pow_Z jumpi
dup1 __FUNC_SIG(pow) eq pow_Z jumpi
dup1 __FUNC_SIG(rotateComplexz) eq rotateComplex_Z jumpi
dup1 __FUNC_SIG(lineequationz) eq lineequation_Z jumpi


0x00 0x00 revert

Expand Down Expand Up @@ -207,5 +241,9 @@
pow_Z:
POW_LOCAL()

rotateComplex_Z:
rotateComplex_LOCAL()

lineequation_Z:
LINEEQUATION_LOCAL()
}
25 changes: 25 additions & 0 deletions src/complexMath/Complex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,29 @@ contract Num_Complex {

return a;
}

/// @notice COMPLEX RotatesComplexNumber
/// @param a Complex number
/// @param T Theta
/// @return Complex number
function rotateComplex(Complex memory a, int256 T) public pure returns (Complex memory) {
(int256 r, int256 _T) = toPolar(a);
_T = _T + T;
a = fromPolar(r, _T);
return a;
}

/// @notice COMPLEX equationofaline
/// @param a Complex number
/// @param a Complex number
/// @return int slope
/// @return int intercept
function LINE_EQUATION(Complex memory a, Complex memory b) public pure returns (int256, int256) {
int256 slnum = b.im - a.im;
int256 sldno = b.re - a.re;
int256 slope = (1e18 * slnum) / (sldno);
int256 intercept = (1e18 * b.im - slope * b.re);

return (slope, intercept);
}
}
16 changes: 16 additions & 0 deletions test/Complex.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ contract ComplexTest is Test {
int256 r = complex.atan1to1(9 * 1e17);
assertEq((r * 100) / scale, 73);
}

function testrotateComplex() public {
(int256 r, int256 i) = complex.rotateComplexz(267116799542721300, 3 * scale, 4 * scale);
assertApproxEqAbs(r, 3 * scale, 1e17);
assertApproxEqAbs(i, 4 * scale, 1e17);
}

function testLINE_EQUATION() public {
(int256 m, int256 c) = complex.lineequationz(19 * scale, 15 * scale, 7 * scale, 5 * scale);
assertEq(m / scale, 2);
assertEq(c / scale ** 2, 5);
}
}

interface WRAPPER {
Expand Down Expand Up @@ -116,4 +128,8 @@ interface WRAPPER {
function expZ(int256, int256) external returns (int256, int256);

function pow(int256, int256, int256) external returns (int256, int256);

function rotateComplexz(int256, int256, int256) external returns (int256, int256);

function lineequationz(int256, int256, int256, int256) external returns (int256, int256);
}
Loading