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.

113 lines
2.8 KiB

  1. package fields
  2. import (
  3. "bytes"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestTranspose(t *testing.T) {
  9. b0 := big.NewInt(int64(0))
  10. b1 := big.NewInt(int64(1))
  11. bFive := big.NewInt(int64(5))
  12. a := [][]*big.Int{
  13. []*big.Int{b0, b1, b0, b0, b0, b0},
  14. []*big.Int{b0, b0, b0, b1, b0, b0},
  15. []*big.Int{b0, b1, b0, b0, b1, b0},
  16. []*big.Int{bFive, b0, b0, b0, b0, b1},
  17. }
  18. aT := Transpose(a)
  19. assert.Equal(t, aT, [][]*big.Int{
  20. []*big.Int{b0, b0, b0, bFive},
  21. []*big.Int{b1, b0, b1, b0},
  22. []*big.Int{b0, b0, b0, b0},
  23. []*big.Int{b0, b1, b0, b0},
  24. []*big.Int{b0, b0, b1, b0},
  25. []*big.Int{b0, b0, b0, b1},
  26. })
  27. }
  28. func neg(a *big.Int) *big.Int {
  29. return new(big.Int).Neg(a)
  30. }
  31. func TestPol(t *testing.T) {
  32. b0 := big.NewInt(int64(0))
  33. b1 := big.NewInt(int64(1))
  34. b2 := big.NewInt(int64(2))
  35. b3 := big.NewInt(int64(3))
  36. b4 := big.NewInt(int64(4))
  37. b5 := big.NewInt(int64(5))
  38. b6 := big.NewInt(int64(6))
  39. b16 := big.NewInt(int64(16))
  40. a := []*big.Int{b1, b0, b5}
  41. b := []*big.Int{b3, b0, b1}
  42. // new Finite Field
  43. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  44. assert.True(nil, ok)
  45. f := NewFq(r)
  46. // new Polynomial Field
  47. pf := NewPF(f)
  48. // polynomial multiplication
  49. o := pf.Mul(a, b)
  50. assert.Equal(t, o, []*big.Int{b3, b0, b16, b0, b5})
  51. // polynomial division
  52. quo, rem := pf.Div(a, b)
  53. assert.Equal(t, quo[0].Int64(), int64(5))
  54. assert.Equal(t, new(big.Int).Sub(rem[0], r).Int64(), int64(-14)) // check the rem result without modulo
  55. c := []*big.Int{neg(b4), b0, neg(b2), b1}
  56. d := []*big.Int{neg(b3), b1}
  57. quo2, rem2 := pf.Div(c, d)
  58. assert.Equal(t, quo2, []*big.Int{b3, b1, b1})
  59. assert.Equal(t, rem2[0].Int64(), int64(5))
  60. // polynomial addition
  61. o = pf.Add(a, b)
  62. assert.Equal(t, o, []*big.Int{b4, b0, b6})
  63. // polynomial subtraction
  64. o1 := pf.Sub(a, b)
  65. o2 := pf.Sub(b, a)
  66. o = pf.Add(o1, o2)
  67. assert.True(t, bytes.Equal(b0.Bytes(), o[0].Bytes()))
  68. assert.True(t, bytes.Equal(b0.Bytes(), o[1].Bytes()))
  69. assert.True(t, bytes.Equal(b0.Bytes(), o[2].Bytes()))
  70. c = []*big.Int{b5, b6, b1}
  71. d = []*big.Int{b1, b3}
  72. o = pf.Sub(c, d)
  73. assert.Equal(t, o, []*big.Int{b4, b3, b1})
  74. // NewPolZeroAt
  75. o = pf.NewPolZeroAt(3, 4, b4)
  76. assert.Equal(t, pf.Eval(o, big.NewInt(3)), b4)
  77. o = pf.NewPolZeroAt(2, 4, b3)
  78. assert.Equal(t, pf.Eval(o, big.NewInt(2)), b3)
  79. }
  80. func TestLagrangeInterpolation(t *testing.T) {
  81. // new Finite Field
  82. r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  83. assert.True(nil, ok)
  84. f := NewFq(r)
  85. // new Polynomial Field
  86. pf := NewPF(f)
  87. b0 := big.NewInt(int64(0))
  88. b5 := big.NewInt(int64(5))
  89. a := []*big.Int{b0, b0, b0, b5}
  90. alpha := pf.LagrangeInterpolation(a)
  91. assert.Equal(t, pf.Eval(alpha, big.NewInt(int64(4))), b5)
  92. aux := pf.Eval(alpha, big.NewInt(int64(3))).Int64()
  93. assert.Equal(t, aux, int64(0))
  94. }