dss icon indicating copy to clipboard operation
dss copied to clipboard

Optimize rpow(x,n,base)

Open k06a opened this issue 5 years ago • 5 comments

  1. Optimize rpow(x,n,base) for the case x != 0 && n == 0 (291 gas vs 383 gas)

    switch x case 0 {switch n case 0 {z := b} default {z := 0}}
    default {
       ...
    }
    

    =>

    switch n case 0 { z := b }
    default {
        switch x case 0 { z := 0 }
        default {
            ...
        }
    }
    
  2. Simplify overflow check for squaring in rpow(x,n,base) (2355 gas vs 2388 gas for x = 2, n = 255)

    let xx := mul(x, x)
    if iszero(eq(div(xx, x), x)) { revert(0,0) }
    

    =>

    let xx := mul(x, x)
    if shr(128, x) { revert(0,0) }
    

k06a avatar Oct 19 '19 18:10 k06a

@kmbarry1 can you get these optimisations to pass the spec? 😛

livnev avatar Oct 20 '19 10:10 livnev

I will give it a try today!

kmbarry1 avatar Oct 21 '19 14:10 kmbarry1

Also, just to give "official" comment on this: looks good, but there are two things I want to check prior to merging:

  1. gas for x != 0 && n != 0 is not worsened (I don't think it is, looks like it should be at least the same, maybe better, but want to check explicitly)
  2. must successfully formally verify this

kmbarry1 avatar Oct 21 '19 16:10 kmbarry1

@kmbarry1 do we have any update on this regarding FV?

gbalabasquer avatar Oct 29 '19 19:10 gbalabasquer

Am getting through the backlog of changes that are definitely going in before analyzing this.

kmbarry1 avatar Oct 29 '19 19:10 kmbarry1