You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1004 B

  1. package gocircomprover
  2. import (
  3. "bytes"
  4. "math/big"
  5. )
  6. func FAdd(a, b *big.Int) *big.Int {
  7. ab := new(big.Int).Add(a, b)
  8. return new(big.Int).Mod(ab, R)
  9. }
  10. func FSub(a, b *big.Int) *big.Int {
  11. ab := new(big.Int).Sub(a, b)
  12. return new(big.Int).Mod(ab, R)
  13. }
  14. func FMul(a, b *big.Int) *big.Int {
  15. ab := new(big.Int).Mul(a, b)
  16. return new(big.Int).Mod(ab, R)
  17. }
  18. func FDiv(a, b *big.Int) *big.Int {
  19. ab := new(big.Int).Mul(a, new(big.Int).ModInverse(b, R))
  20. return new(big.Int).Mod(ab, R)
  21. }
  22. func FNeg(a *big.Int) *big.Int {
  23. return new(big.Int).Mod(new(big.Int).Neg(a), R)
  24. }
  25. func FInv(a *big.Int) *big.Int {
  26. return new(big.Int).ModInverse(a, R)
  27. }
  28. func FExp(base *big.Int, e *big.Int) *big.Int {
  29. res := big.NewInt(1)
  30. rem := new(big.Int).Set(e)
  31. exp := base
  32. for !bytes.Equal(rem.Bytes(), big.NewInt(int64(0)).Bytes()) {
  33. // if BigIsOdd(rem) {
  34. if rem.Bit(0) == 1 { // .Bit(0) returns 1 when is odd
  35. res = FMul(res, exp)
  36. }
  37. exp = FMul(exp, exp)
  38. rem = new(big.Int).Rsh(rem, 1)
  39. }
  40. return res
  41. }