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.

114 lines
2.9 KiB

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