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.

91 lines
2.1 KiB

  1. package proof
  2. import (
  3. "bytes"
  4. "math/big"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/arnaucube/go-snark/circuit"
  9. "github.com/arnaucube/go-snark/fields"
  10. )
  11. func TestGroth16MinimalFlow(t *testing.T) {
  12. code := `
  13. func main(private s0, public s1):
  14. s2 = s0 * s0
  15. s3 = s2 * s0
  16. s4 = s3 + s0
  17. s5 = s4 + 5
  18. equals(s1, s5)
  19. out = 1 * 1
  20. `
  21. parser := circuit.NewParser(strings.NewReader(code))
  22. cir, err := parser.Parse()
  23. assert.Nil(t, err)
  24. b3 := big.NewInt(int64(3))
  25. privateInputs := []*big.Int{b3}
  26. b35 := big.NewInt(int64(35))
  27. publicSignals := []*big.Int{b35}
  28. w, err := cir.CalculateWitness(privateInputs, publicSignals)
  29. assert.Nil(t, err)
  30. cir.GenerateR1CS()
  31. // TODO zxQAP is not used and is an old impl
  32. // TODO remove
  33. alphas, betas, gammas, _ := R1CSToQAP(
  34. cir.R1CS.A,
  35. cir.R1CS.B,
  36. cir.R1CS.C,
  37. )
  38. assert.Equal(t, 8, len(alphas))
  39. assert.Equal(t, 8, len(alphas))
  40. assert.Equal(t, 8, len(alphas))
  41. assert.True(t, !bytes.Equal(alphas[1][1].Bytes(), big.NewInt(int64(0)).Bytes()))
  42. ax, bx, cx, px := Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
  43. assert.Equal(t, 7, len(ax))
  44. assert.Equal(t, 7, len(bx))
  45. assert.Equal(t, 7, len(cx))
  46. assert.Equal(t, 13, len(px))
  47. setup := &Groth16Setup{}
  48. err = setup.Init(cir, alphas, betas, gammas)
  49. assert.Nil(t, err)
  50. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  51. div, rem := Utils.PF.Div(px, setup.Pk.Z)
  52. assert.Equal(t, hx, div)
  53. assert.Equal(t, rem, fields.ArrayOfBigZeros(6))
  54. // hx==px/zx so px==hx*zx
  55. assert.Equal(t, px, Utils.PF.Mul(hx, setup.Pk.Z))
  56. // check length of polynomials H(x) and Z(x)
  57. assert.Equal(t, len(hx), len(px)-len(setup.Pk.Z)+1)
  58. proof, err := setup.Generate(cir, w, px)
  59. assert.Nil(t, err)
  60. b35Verif := big.NewInt(int64(35))
  61. publicSignalsVerif := []*big.Int{b35Verif}
  62. {
  63. r, err := setup.Verify(proof, publicSignalsVerif)
  64. assert.Nil(t, err)
  65. assert.True(t, r)
  66. }
  67. bOtherWrongPublic := big.NewInt(int64(34))
  68. wrongPublicSignalsVerif := []*big.Int{bOtherWrongPublic}
  69. {
  70. r, err := setup.Verify(proof, wrongPublicSignalsVerif)
  71. assert.Nil(t, err)
  72. assert.False(t, r)
  73. }
  74. }