Skip to content

Commit

Permalink
Merge pull request #17 from cy4n1d3-p1x3l/new
Browse files Browse the repository at this point in the history
Made huff code dynamic and unit testing done.
  • Loading branch information
0xpanicError authored Dec 11, 2023
2 parents a95dc57 + 87df7e4 commit 0a5d30c
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 301 deletions.
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ auto_detect_solc = false
optimizer = true
optimizer_runs = 200 # Default amount
ffi = true
fuzz_runs = 1_000
[fuzz]
runs = 100000
remappings = [
"forge-std=lib/forge-std/src/",
"foundry-huff=lib/foundry-huff/src/",
Expand Down
2 changes: 1 addition & 1 deletion src/ComplexHuff/Helper.huff
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}

#define macro PUT_X3_CUBE() = takes(0) returns(1){
0x03 //[2]
0x03 //[3]
[X3] //[1e18,3]
exp //[1e54]
}
Expand Down
43 changes: 17 additions & 26 deletions src/ComplexHuff/PRBMath.huff
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// #include './Helper.huff'

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

// dup1 // [x,x]
// iszero // [x==0?,x]
// jumpi
// zero_found
// jumpi
// [X3] // [1e18,x]
// mul // [1e18*x]
// dup1 // [1e18*x,1e18*x]
Expand Down Expand Up @@ -61,9 +61,7 @@
// pop // [x'/result]

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


// }

#define macro LOGE_PRB() = takes(1) returns(1) {
Expand All @@ -79,14 +77,14 @@

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

dup1 // [x,x]
PUT_SIGN() // [sign,x]
swap1 // [x,sign]
[X3] // [1e18,x,sign]
dup2 // [x,1e18,x,sign]
div // [x/1e18,x,sign]
0x00 // [0,x/1e18,x,sign]
MOST_SIGNIFICANT_BIT() // [msb,x,sign]
MOST_SIGNIFICANT_BIT() // [msb,x,sign]
dup1 // [msb,msb,x,sign]
[X3] // [1e18,msb,msb,x,sign]
mul // [1e18*msb,msb,x,sign]
Expand All @@ -98,35 +96,28 @@
eq // [1==x>>msb?,x>>msb,1e18*msb,sign]
fractional_zero
jumpi // [x>>msb,1e18*msb,sign]

[HALF_SCALE] // [1e18,x>>msb,1e18*msb,sign] => [delta,y,res,sign]
[HALF_SCALE] // [5e17,x>>msb,1e18*msb,sign]
looper
jump

looper: // [delta,y,res,sign]
swap1 // [y,delta>>1,res,sign]
dup1 // [y,y,delta>>1,res,sign]
mul // [y*y,delta>>1,res,sign]
[X3] // [1e18,y*y,delta>>1,res,sign]
swap1 // [y*y,1e18,delta>>1,res,sign]
sdiv // [y*y/1e18,delta>>1,res,sign]
HELPIT() // [y',delta>>1,res',sign]
swap1 // [y,delta,res,sign]
dup1 // [y,y,delta,res,sign]
mul // [y*y,delta,res,sign]
[X3] // [1e18,y*y,delta,res,sign]
swap1 // [y*y,1e18,delta,res,sign]
sdiv // [y*y/1e18,delta,res,sign]
HELPIT() // [y',delta,res',sign]
swap1 // [delta,y',res',sign]
0x01
shr // [delta>>1,y',res',sign]
0x01 // [1,delta,y,res',sign]
shr // [delta>>1,y,res',sign]
dup1 // [delta>>1,delta>>1,y',res',sign]
0x00 // [0,delta>>1,delta>>1,y',res',sign]
lt // [0<(delta>>1),delta>>1,y',res',sign]
looper
jumpi // [delta>>1,y',res',sign]
pop // [y',res',sign]
fractional_zero: // [x>>msb,1e18*msb,sign] or [y',res',sign]
pop // [res',sign]
mul // [res'*sign]
finish jump

fractional_zero: // [x>>msb,1e18*msb,sign]
pop // [1e18*msb,sign]
mul // [1e18*msb*sign]

finish:
}
}
Loading

0 comments on commit 0a5d30c

Please sign in to comment.