Skip to content

Commit

Permalink
Merge pull request #20 from blueh4mster/main
Browse files Browse the repository at this point in the history
optimized calcR
  • Loading branch information
0xpanicError authored Dec 15, 2023
2 parents 109ada5 + 88edee3 commit 1f4841a
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 267 deletions.
91 changes: 11 additions & 80 deletions src/ComplexHuff/Complex.huff
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
//INPUT STACK => [RE(a),RE(b),IM(a),IM(b)]

0x02 // [2,Re(A),Re(B),Im(A),Im(B)]
dup2 // [Re(B),2,Re(A),Re(B),Im(A),Im(B)]
dup3 // [Re(B),2,Re(A),Re(B),Im(A),Im(B)]
exp // [Re(B)**2 ,Re(A),Re(B),Im(A),Im(B)]
0x02 // [2,Re(B)**2 ,Re(A),Re(B),Im(A),Im(B)]
dup5 // [Im(B),2,Re(B)**2,Re(A),Re(B),Im(A),Im(B)]
dup6 // [Im(B),2,Re(B)**2,Re(A),Re(B),Im(A),Im(B)]
exp // [Im(B)**2,Re(B)**2,Re(A),Re(B),Im(A),Im(B)]
add // [Im(B)**2+Re(B)**2,Re(A),Re(B),Im(A),Im(B)] , Let Im(B)**2+Re(B)**2 be X
swap4 // [Im(B),Re(A),Re(B),Im(A),X]
Expand Down Expand Up @@ -94,60 +94,7 @@
dup1 // [b,b,a*a]
mul // [b*b,a*a]
add // [b*b+a*a]
SQRT_NEW() // [sqrt]
}

#define macro SQRT_NEW() = takes(1) returns(1) {
//[b*b+a*a] <= x**2
//push in memory
[VALUE_LOCATION]
sstore //[]
0x01 //[0x01]
loop: //get x**2 from memory and push in stack
[VALUE_LOCATION] //[ptr,0x01(i)]
sload //[x*x,i]
0x2710
mul
dup2 //[i,x2,i]
0x01 //[0x01,i,x2,i]
swap1 //[i,0x01,x2,i]
sub //[i-1,x2,i]
swap2 //[i,x2,i-1]
dup1
mul //[i*i,x*x,i-1]
eq ans jumpi //[i*i==x**2?,i-1] //[i-1]
0x01
add //[i]
[VALUE_LOCATION] //[ptr,i]
sload //[x**2,i]
0x2710
mul
dup2 //[i,x2,i]
0x01
swap1
sub //[i-1,x2,i]
swap2 //[i,x2,i-1]
dup1
mul //[i*i,x*x,i-1]
gt exit jumpi //[i-1]
0x02
add //[i+1]
loop
jump

ans:
0x01
add // [i]
result jump

exit:
result jump

result:
[X15]
mul
[VALUE_LOCATION]
sstore
SQRT_PRB() // [sqrt]
}

/// @notice
Expand All @@ -158,9 +105,7 @@

dup2 //[i,r,i]
dup2 //[r,i,r,i]
CALC_R() // [r,i]
[VALUE_LOCATION]
sload // [r,Re(a),IM(a)]
CALC_R()
swap2 // [Im(a),Re(a),r]
[X3]
mul
Expand Down Expand Up @@ -375,41 +320,27 @@
dup2
dup2 // [re,im,re,im]
CALC_R()
[VALUE_LOCATION]
sload // [r,re,im]
dup2 // [re,r,re,im]
[X3]
mul
dup2 // [r,re*1e18,r,re,im]
swap3
[X3]
mul // [re*1e18,re*1e18,r,r,im]
swap3 // [r,re*1e18,r,re*1e18,im]
dup2 // [r,re,r,re,im]
add // [r+re,r,re,im]
0x02
swap1
sdiv // [re+r/2,r,re,im]
[X3]
mul
swap2 // [re,r,re+r/2,im]
swap1
sub // [r-re,re+r/2,im]
0x02
swap1
sdiv // [r-re/2,re+r/2,im]
[X3]
mul
swap2
pop // [x**2,y**2]
[X15]
swap1
sdiv // [x**2/1e16,y**2]
SQRT_NEW()
[VALUE_LOCATION]
sload // [x,y**2]
swap1
[X15]
SQRT_PRB()
swap1
sdiv
SQRT_NEW()
[VALUE_LOCATION]
sload
SQRT_PRB()
swap1 // [x,y]
}

Expand Down
119 changes: 62 additions & 57 deletions src/ComplexHuff/PRBMath.huff
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,72 @@
#include './Constants.huff'
// #include './Helper.huff'

// #define macro SQRT_PRB() = takes(1) returns(1){
// // //Input Stack = [x]
#define macro SQRT_PRB() = takes(1) returns(1){
//Input Stack = [x]

// dup1 // [x,x]
// iszero // [x==0?,x]
// zero_found
// jumpi
// [X3] // [1e18,x]
// mul // [1e18*x]
// dup1 // [1e18*x,1e18*x]
// 0x01 // [1,1e18*x,1e18*x]
// GATE1()
// GATE2()
// GATE3()
// GATE4()
// GATE5()
// GATE6()
// GATE7() // [result,xnum,x']
// swap1 // [xnum,result,x']
// pop // [result,x']
// 0x00 // [0,result,x']
// loop
// jump

// loop: // [i,result,x']
// 0x01 // [1,i,result,x']
// add // [i+1,result,x']
// swap1 // [result,i+1,x']
// dup1 // [result,result,i+1,x']
// dup4 // [x',result,result,i+1,x']
// add // [x'+result,result,i+1,x']
// div // [(x'+result)/result,i+1,x'] , Let (x'+result)/result = result
// dup2 // [i+1,result,i+1,x']
// 0x07 // [7,i+1,result,i+1,x']
// eq // [i+1==7?,result,i+1,x']
// done
// jumpi
// loop
// jump
dup1 // [x,x]
iszero // [x==0?,x]
zero_found
jumpi
// [X3] // [1e18,x]
// mul // [1e18*x]
dup1 // [1e18*x,1e18*x]
0x01 // [1,1e18*x,1e18*x]
GATE1()
GATE2()
GATE3()
GATE4()
GATE5()
GATE6()
GATE7() // [result,xnum,x']
swap1 // [xnum,result,x']
pop // [result,x']
0x00 // [0,result,x']

loop: // [i,result,x']
0x01 // [1,i,result,x']
add // [i+1,result,x']
swap1 // [result,i+1,x']
dup1 // [result,result,i+1,x']
dup4 // [x',result,result,i+1,x']
div // [x'/result,result,i+1,x']
add // [x/result+result,i+1,x'] , Let (result + x'/result) >> 1 = result
0x01 shr // [result,i+1,x']
dup2 // [i+1,result,i+1,x']
0x07 // [7,i+1,result,i+1,x']
eq // [i+1==7?,result,i+1,x']
done
jumpi
swap1
loop
jump

// done: // [result,i+1,x']
// swap1 // [i+1,result,x']
// pop // [result,x']
// dup1 // [result,result,x']
// swap2 // [x',result,result]
// div // [x'/result,result]
// dup1 // [x'/result,x'/result,result]
// dup3 // [result,x'/result,x'/result,result]
// lt // [result < x'/result,x'/result,result]
// iszero
// final
// jumpi
// pop
done: // [result,i+1,x']
swap1 // [i+1,result,x']
pop // [result,x']
dup1 // [result,result,x']
swap2 // [x',result,result]
div // [x'/result,result]
dup1 // [x'/result,x'/result,result]
dup3 // [result,x'/result,x'/result,result]
lt // [result < x'/result,x'/result,result]
final
jumpi
swap1
pop
end jump

// final: // [x'/result,result]
// swap1 // [result,x'/result]
// pop // [x'/result]
final: // [x'/result,result]
pop // [x'/result]
end jump

zero_found: //[x]
pop // []
0x00 // [0]

end:
}

// zero_found: //[x]

// }

#define macro LOGE_PRB() = takes(1) returns(1) {
//Input stack => [x]
Expand Down
Loading

0 comments on commit 1f4841a

Please sign in to comment.