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.

137 lines
3.6 KiB

  1. package r1csqapFloat
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestTranspose(t *testing.T) {
  9. b0 := big.NewFloat(float64(0))
  10. b1 := big.NewFloat(float64(1))
  11. bFive := big.NewFloat(float64(5))
  12. a := [][]*big.Float{
  13. []*big.Float{b0, b1, b0, b0, b0, b0},
  14. []*big.Float{b0, b0, b0, b1, b0, b0},
  15. []*big.Float{b0, b1, b0, b0, b1, b0},
  16. []*big.Float{bFive, b0, b0, b0, b0, b1},
  17. }
  18. aT := Transpose(a)
  19. assert.Equal(t, aT, [][]*big.Float{
  20. []*big.Float{b0, b0, b0, bFive},
  21. []*big.Float{b1, b0, b1, b0},
  22. []*big.Float{b0, b0, b0, b0},
  23. []*big.Float{b0, b1, b0, b0},
  24. []*big.Float{b0, b0, b1, b0},
  25. []*big.Float{b0, b0, b0, b1},
  26. })
  27. }
  28. func TestPol(t *testing.T) {
  29. b0 := big.NewFloat(float64(0))
  30. b1 := big.NewFloat(float64(1))
  31. // b1neg := big.NewFloat(float64(-1))
  32. // b2 := big.NewFloat(float64(2))
  33. b2neg := big.NewFloat(float64(-2))
  34. b3 := big.NewFloat(float64(3))
  35. b4 := big.NewFloat(float64(4))
  36. b5 := big.NewFloat(float64(5))
  37. b6 := big.NewFloat(float64(6))
  38. b16 := big.NewFloat(float64(16))
  39. a := []*big.Float{b1, b0, b5}
  40. b := []*big.Float{b3, b0, b1}
  41. // polynomial multiplication
  42. c := PolMul(a, b)
  43. assert.Equal(t, c, []*big.Float{b3, b0, b16, b0, b5})
  44. // polynomial addition
  45. c = PolAdd(a, b)
  46. assert.Equal(t, c, []*big.Float{b4, b0, b6})
  47. // polynomial subtraction
  48. c = PolSub(a, b)
  49. assert.Equal(t, c, []*big.Float{b2neg, b0, b4})
  50. // FloatPow
  51. p := FloatPow(big.NewFloat(float64(5)), 3)
  52. assert.Equal(t, p, big.NewFloat(float64(125)))
  53. p = FloatPow(big.NewFloat(float64(5)), 0)
  54. assert.Equal(t, p, big.NewFloat(float64(1)))
  55. // NewPolZeroAt
  56. r := NewPolZeroAt(3, 4, b4)
  57. assert.Equal(t, PolEval(r, big.NewFloat(3)), b4)
  58. r = NewPolZeroAt(2, 4, b3)
  59. assert.Equal(t, PolEval(r, big.NewFloat(2)), b3)
  60. }
  61. func TestLagrangeInterpolation(t *testing.T) {
  62. b0 := big.NewFloat(float64(0))
  63. b5 := big.NewFloat(float64(5))
  64. a := []*big.Float{b0, b0, b0, b5}
  65. alpha := LagrangeInterpolation(a)
  66. assert.Equal(t, PolEval(alpha, big.NewFloat(4)), b5)
  67. aux, _ := PolEval(alpha, big.NewFloat(3)).Int64()
  68. assert.Equal(t, aux, int64(0))
  69. }
  70. func TestR1CSToQAP(t *testing.T) {
  71. b0 := big.NewFloat(float64(0))
  72. b1 := big.NewFloat(float64(1))
  73. b3 := big.NewFloat(float64(3))
  74. b5 := big.NewFloat(float64(5))
  75. b9 := big.NewFloat(float64(9))
  76. b27 := big.NewFloat(float64(27))
  77. b30 := big.NewFloat(float64(30))
  78. b35 := big.NewFloat(float64(35))
  79. a := [][]*big.Float{
  80. []*big.Float{b0, b1, b0, b0, b0, b0},
  81. []*big.Float{b0, b0, b0, b1, b0, b0},
  82. []*big.Float{b0, b1, b0, b0, b1, b0},
  83. []*big.Float{b5, b0, b0, b0, b0, b1},
  84. }
  85. b := [][]*big.Float{
  86. []*big.Float{b0, b1, b0, b0, b0, b0},
  87. []*big.Float{b0, b1, b0, b0, b0, b0},
  88. []*big.Float{b1, b0, b0, b0, b0, b0},
  89. []*big.Float{b1, b0, b0, b0, b0, b0},
  90. }
  91. c := [][]*big.Float{
  92. []*big.Float{b0, b0, b0, b1, b0, b0},
  93. []*big.Float{b0, b0, b0, b0, b1, b0},
  94. []*big.Float{b0, b0, b0, b0, b0, b1},
  95. []*big.Float{b0, b0, b1, b0, b0, b0},
  96. }
  97. // alphas, betas, gammas
  98. alphas, betas, gammas, zx := R1CSToQAP(a, b, c)
  99. fmt.Println(alphas)
  100. fmt.Println(betas)
  101. fmt.Println(gammas)
  102. fmt.Print("Z(x): ")
  103. fmt.Println(zx)
  104. zexpected := []*big.Float{big.NewFloat(float64(24)), big.NewFloat(float64(-50)), big.NewFloat(float64(35)), big.NewFloat(float64(-10)), big.NewFloat(float64(1))}
  105. assert.Equal(t, zx, zexpected)
  106. // witness
  107. w := []*big.Float{b1, b3, b35, b9, b27, b30}
  108. fmt.Print("w: ")
  109. fmt.Println(w)
  110. // QAP A(x), B(x), C(x)
  111. ax, bx, cx, px := CombinePolynomials(w, alphas, betas, gammas)
  112. fmt.Print("A(x), B(x), C(x), P(x): ")
  113. fmt.Println(ax)
  114. fmt.Println(bx)
  115. fmt.Println(cx)
  116. fmt.Println(px)
  117. hx := DivisorPolinomial(px, zx)
  118. fmt.Print("H(x): ")
  119. fmt.Println(hx)
  120. }