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.

120 lines
3.2 KiB

  1. package arbo
  2. import (
  3. "crypto/sha256"
  4. "math/big"
  5. "github.com/iden3/go-iden3-crypto/poseidon"
  6. "golang.org/x/crypto/blake2b"
  7. )
  8. var (
  9. // TypeHashSha256 represents the label for the HashFunction of Sha256
  10. TypeHashSha256 = []byte("sha256")
  11. // TypeHashPoseidon represents the label for the HashFunction of
  12. // Poseidon
  13. TypeHashPoseidon = []byte("poseidon")
  14. // TypeHashBlake2b represents the label for the HashFunction of Blake2b
  15. TypeHashBlake2b = []byte("blake2b")
  16. // HashFunctionSha256 contains the HashSha256 struct which implements
  17. // the HashFunction interface
  18. HashFunctionSha256 HashSha256
  19. // HashFunctionPoseidon contains the HashPoseidon struct which implements
  20. // the HashFunction interface
  21. HashFunctionPoseidon HashPoseidon
  22. // HashFunctionBlake2b contains the HashBlake2b struct which implements
  23. // the HashFunction interface
  24. HashFunctionBlake2b HashBlake2b
  25. )
  26. // Once Generics are at Go, this will be updated (August 2021
  27. // https://blog.golang.org/generics-next-step)
  28. // HashFunction defines the interface that is expected for a hash function to be
  29. // used in a generic way in the Tree.
  30. type HashFunction interface {
  31. Type() []byte
  32. Len() int
  33. Hash(...[]byte) ([]byte, error)
  34. // CheckInputs checks if the inputs are valid without computing the hash
  35. // CheckInputs(...[]byte) error
  36. }
  37. // HashSha256 implements the HashFunction interface for the Sha256 hash
  38. type HashSha256 struct{}
  39. // Type returns the type of HashFunction for the HashSha256
  40. func (f HashSha256) Type() []byte {
  41. return TypeHashSha256
  42. }
  43. // Len returns the length of the Hash output
  44. func (f HashSha256) Len() int {
  45. return 32 //nolint:gomnd
  46. }
  47. // Hash implements the hash method for the HashFunction HashSha256
  48. func (f HashSha256) Hash(b ...[]byte) ([]byte, error) {
  49. var toHash []byte
  50. for i := 0; i < len(b); i++ {
  51. toHash = append(toHash, b[i]...)
  52. }
  53. h := sha256.Sum256(toHash)
  54. return h[:], nil
  55. }
  56. // HashPoseidon implements the HashFunction interface for the Poseidon hash
  57. type HashPoseidon struct{}
  58. // Type returns the type of HashFunction for the HashPoseidon
  59. func (f HashPoseidon) Type() []byte {
  60. return TypeHashPoseidon
  61. }
  62. // Len returns the length of the Hash output
  63. func (f HashPoseidon) Len() int {
  64. return 32 //nolint:gomnd
  65. }
  66. // Hash implements the hash method for the HashFunction HashPoseidon
  67. func (f HashPoseidon) Hash(b ...[]byte) ([]byte, error) {
  68. var toHash []*big.Int
  69. for i := 0; i < len(b); i++ {
  70. bi := BytesToBigInt(b[i])
  71. toHash = append(toHash, bi)
  72. }
  73. h, err := poseidon.Hash(toHash)
  74. if err != nil {
  75. return nil, err
  76. }
  77. hB := BigIntToBytes(h)
  78. return hB, nil
  79. }
  80. // HashBlake2b implements the HashFunction interface for the Blake2b hash
  81. type HashBlake2b struct{}
  82. // Type returns the type of HashFunction for the HashBlake2b
  83. func (f HashBlake2b) Type() []byte {
  84. return TypeHashBlake2b
  85. }
  86. // Len returns the length of the Hash output
  87. func (f HashBlake2b) Len() int {
  88. return 32 //nolint:gomnd
  89. }
  90. // Hash implements the hash method for the HashFunction HashBlake2b
  91. func (f HashBlake2b) Hash(b ...[]byte) ([]byte, error) {
  92. hasher, err := blake2b.New256(nil)
  93. if err != nil {
  94. return nil, err
  95. }
  96. for i := 0; i < len(b); i++ {
  97. if _, err = hasher.Write(b[i]); err != nil {
  98. return nil, err
  99. }
  100. }
  101. return hasher.Sum(nil), nil
  102. }