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.

69 lines
1.9 KiB

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