privacy-scaling-explorations / zk-kit

A monorepo of reusable libraries for zero-knowledge technologies.
https://zkkit.pse.dev
MIT License
290 stars 76 forks source link

fix(utils): fix pow for negative exponent #249

Closed 0xbok closed 7 months ago

0xbok commented 7 months ago

pow fn comment says it handles negative exponent but it doesn't. This PR fixes it. Here's the reasoning behind the fix:

$$ a^{-e} = (a^{-1})^e \mod p $$

So pow(a, -e) == pow(inv(a), e)

0xbok commented 7 months ago

note that the pow test uses 12 as the order. A field with a non-prime order may not have an inverse for every element in the set.