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.

75 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, "2492816973395423007340226948038371729989170225696553239457870892535792679622", pk.X.String())
  20. assert.Equal(t, "15238403086306505038849621710779816852318505119327426213168494964113886299863", pk.Y.String())
  21. }
  22. func TestRmEndingZeroes(t *testing.T) {
  23. s0, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000000")
  24. require.NoError(t, err)
  25. s1, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000001")
  26. require.NoError(t, err)
  27. s2, err := merkletree.NewHashFromHex("0x0000000000000000000000000000000000000000000000000000000000000002")
  28. require.NoError(t, err)
  29. // expect cropped last zeroes
  30. circomSiblings := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0}
  31. siblings := RmEndingZeroes(circomSiblings)
  32. expected := []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2}
  33. assert.Equal(t, expected, siblings)
  34. // expect empty array when input is an empty array
  35. siblings = RmEndingZeroes([]*merkletree.Hash{})
  36. assert.Equal(t, []*merkletree.Hash{}, siblings)
  37. // expect nil when input is nil
  38. siblings = RmEndingZeroes(nil)
  39. assert.Nil(t, siblings)
  40. // cases when inputs are [x], [x,0], [0,x]
  41. circomSiblings = []*merkletree.Hash{s1}
  42. siblings = RmEndingZeroes(circomSiblings)
  43. assert.Equal(t, []*merkletree.Hash{s1}, siblings)
  44. circomSiblings = []*merkletree.Hash{s1, s0}
  45. siblings = RmEndingZeroes(circomSiblings)
  46. assert.Equal(t, []*merkletree.Hash{s1}, siblings)
  47. circomSiblings = []*merkletree.Hash{s0, s1}
  48. siblings = RmEndingZeroes(circomSiblings)
  49. assert.Equal(t, []*merkletree.Hash{s0, s1}, siblings)
  50. // expect empty array when input is all zeroes
  51. circomSiblings = []*merkletree.Hash{s0}
  52. siblings = RmEndingZeroes(circomSiblings)
  53. assert.Equal(t, []*merkletree.Hash{}, siblings)
  54. circomSiblings = []*merkletree.Hash{s0, s0}
  55. siblings = RmEndingZeroes(circomSiblings)
  56. assert.Equal(t, []*merkletree.Hash{}, siblings)
  57. circomSiblings = []*merkletree.Hash{s0, s0, s0, s0, s0}
  58. siblings = RmEndingZeroes(circomSiblings)
  59. assert.Equal(t, []*merkletree.Hash{}, siblings)
  60. // expect input equal to output when last element!=0
  61. circomSiblings = []*merkletree.Hash{s0, s1, s0, s1, s1, s1, s2, s0, s0, s0, s0, s2}
  62. siblings = RmEndingZeroes(circomSiblings)
  63. assert.Equal(t, circomSiblings, siblings)
  64. }