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.

65 lines
1.7 KiB

  1. package bn128
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBN128(t *testing.T) {
  8. bn128, err := NewBn128()
  9. assert.Nil(t, err)
  10. big40 := big.NewInt(int64(40))
  11. big75 := big.NewInt(int64(75))
  12. g1a := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(big40))
  13. g2a := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(big75))
  14. g1b := bn128.G1.MulScalar(bn128.G1.G, bn128.Fq1.Copy(big75))
  15. g2b := bn128.G2.MulScalar(bn128.G2.G, bn128.Fq1.Copy(big40))
  16. pre1a := bn128.PreComputeG1(g1a)
  17. pre2a, err := bn128.PreComputeG2(g2a)
  18. assert.Nil(t, err)
  19. pre1b := bn128.PreComputeG1(g1b)
  20. pre2b, err := bn128.PreComputeG2(g2b)
  21. r1 := bn128.MillerLoop(pre1a, pre2a)
  22. r2 := bn128.MillerLoop(pre1b, pre2b)
  23. rbe := bn128.Fq12.Mul(r1, bn128.Fq12.Inverse(r2))
  24. res := bn128.FinalExponentiation(rbe)
  25. a := bn128.Fq12.Affine(res)
  26. b := bn128.Fq12.Affine(bn128.Fq12.One())
  27. assert.True(t, bn128.Fq12.Equal(a, b))
  28. assert.True(t, bn128.Fq12.Equal(res, bn128.Fq12.One()))
  29. }
  30. func TestBN128_PairingFunction(t *testing.T) {
  31. bn128, err := NewBn128()
  32. assert.Nil(t, err)
  33. big25 := big.NewInt(int64(25))
  34. big30 := big.NewInt(int64(30))
  35. g1a := bn128.G1.MulScalar(bn128.G1.G, big25)
  36. g2a := bn128.G2.MulScalar(bn128.G2.G, big30)
  37. g1b := bn128.G1.MulScalar(bn128.G1.G, big30)
  38. g2b := bn128.G2.MulScalar(bn128.G2.G, big25)
  39. pA, err := bn128.Pairing(g1a, g2a)
  40. assert.Nil(t, err)
  41. pB, err := bn128.Pairing(g1b, g2b)
  42. assert.Nil(t, err)
  43. assert.True(t, bn128.Fq12.Equal(pA, pB))
  44. assert.Equal(t, pA[0][0][0].String(), "73680848340331011700282047627232219336104151861349893575958589557226556635706")
  45. assert.Equal(t, bn128.Fq12.Affine(pA)[0][0][0].String(), "8016119724813186033542830391460394070015218389456422587891475873290878009957")
  46. }