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.

88 lines
2.7 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 := bn128.PreComputeG2(g2a)
  19. assert.Nil(t, err)
  20. pre1b := bn128.PreComputeG1(g1b)
  21. pre2b := 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 := bn128.Pairing(g1a, g2a)
  42. pB := bn128.Pairing(g1b, g2b)
  43. assert.True(t, bn128.Fq12.Equal(pA, pB))
  44. assert.True(t, !bytes.Equal(pA[0][0][0].Bytes(), big.NewInt(int64(0)).Bytes()))
  45. assert.True(t, !bytes.Equal(bn128.Fq12.Affine(pA)[0][0][0].Bytes(), big.NewInt(int64(0)).Bytes()))
  46. // assert.Equal(t, pA[0][0][0].String(), "73680848340331011700282047627232219336104151861349893575958589557226556635706")
  47. // assert.Equal(t, bn128.Fq12.Affine(pA)[0][0][0].String(), "8016119724813186033542830391460394070015218389456422587891475873290878009957")
  48. }
  49. func TestBN128Pairing2(t *testing.T) {
  50. // test idea from https://bplib.readthedocs.io/en/latest/ by George Danezis
  51. bn, err := NewBn128()
  52. assert.Nil(t, err)
  53. gt := bn.Pairing(bn.G1.G, bn.G2.G)
  54. gt6 := bn.Fq12.Exp(gt, big.NewInt(int64(6)))
  55. // e(g1, g2)^6 == e(g1, 6*g2)
  56. assert.True(t, bn.Fq12.Equal(gt6, bn.Pairing(bn.G1.G, bn.G2.MulScalar(bn.G2.G, big.NewInt(int64(6))))))
  57. // e(g1, g2)^6 == e(6* g1, g2)
  58. assert.True(t, bn.Fq12.Equal(gt6, bn.Pairing(bn.G1.MulScalar(bn.G1.G, big.NewInt(int64(6))), bn.G2.G)))
  59. // e(g1, g2)^6 == e(3*g1, 2*g2)
  60. assert.True(t, bn.Fq12.Equal(gt6, bn.Pairing(bn.G1.MulScalar(bn.G1.G, big.NewInt(int64(3))), bn.G2.MulScalar(bn.G2.G, big.NewInt(int64(2))))))
  61. // e(g1, g2)^6 == e(2*g1, 3*g2)
  62. assert.True(t, bn.Fq12.Equal(gt6, bn.Pairing(bn.G1.MulScalar(bn.G1.G, big.NewInt(int64(2))), bn.G2.MulScalar(bn.G2.G, big.NewInt(int64(3))))))
  63. }