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.

82 lines
2.9 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/iden3/go-merkletree"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestBJJFromStringWithChecksum(t *testing.T) {
  10. s := "21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7"
  11. pkComp, err := BJJFromStringWithChecksum(s)
  12. assert.NoError(t, err)
  13. sBytes, err := hex.DecodeString(s)
  14. assert.NoError(t, err)
  15. assert.Equal(t, hex.EncodeToString(SwapEndianness(sBytes)), pkComp.String())
  16. pk, err := pkComp.Decompress()
  17. assert.NoError(t, err)
  18. // expected values computed with js implementation
  19. assert.Equal(t,
  20. "2492816973395423007340226948038371729989170225696553239457870892535792679622",
  21. pk.X.String())
  22. assert.Equal(t,
  23. "15238403086306505038849621710779816852318505119327426213168494964113886299863",
  24. pk.Y.String())
  25. }
  26. func TestRmEndingZeroes(t *testing.T) {
  27. s0, err :=
  28. merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000000")
  29. require.NoError(t, err)
  30. s1, err :=
  31. merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000001")
  32. require.NoError(t, err)
  33. s2, err :=
  34. merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000002")
  35. require.NoError(t, err)
  36. // expect cropped last zeroes
  37. circomSiblings := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0}
  38. siblings := RmEndingZeroes(circomSiblings)
  39. expected := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2}
  40. assert.Equal(t, expected, siblings)
  41. // expect empty array when input is an empty array
  42. siblings = RmEndingZeroes([]*merkletree.Hash{})
  43. assert.Equal(t, []*merkletree.Hash{}, siblings)
  44. // expect nil when input is nil
  45. siblings = RmEndingZeroes(nil)
  46. assert.Nil(t, siblings)
  47. // cases when inputs are [x], [x,0], [0,x]
  48. circomSiblings = []*merkletree.Hash{s1}
  49. siblings = RmEndingZeroes(circomSiblings)
  50. assert.Equal(t, []*merkletree.Hash{s1}, siblings)
  51. circomSiblings = []*merkletree.Hash{s1, s0}
  52. siblings = RmEndingZeroes(circomSiblings)
  53. assert.Equal(t, []*merkletree.Hash{s1}, siblings)
  54. circomSiblings = []*merkletree.Hash{s0, s1}
  55. siblings = RmEndingZeroes(circomSiblings)
  56. assert.Equal(t, []*merkletree.Hash{s0, s1}, siblings)
  57. // expect empty array when input is all zeroes
  58. circomSiblings = []*merkletree.Hash{s0}
  59. siblings = RmEndingZeroes(circomSiblings)
  60. assert.Equal(t, []*merkletree.Hash{}, siblings)
  61. circomSiblings = []*merkletree.Hash{s0, s0}
  62. siblings = RmEndingZeroes(circomSiblings)
  63. assert.Equal(t, []*merkletree.Hash{}, siblings)
  64. circomSiblings = []*merkletree.Hash{s0, s0, s0, s0, s0}
  65. siblings = RmEndingZeroes(circomSiblings)
  66. assert.Equal(t, []*merkletree.Hash{}, siblings)
  67. // expect input equal to output when last element!=0
  68. circomSiblings = []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0, s2}
  69. siblings = RmEndingZeroes(circomSiblings)
  70. assert.Equal(t, circomSiblings, siblings)
  71. }