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.

76 lines
1.9 KiB

  1. package babyjub
  2. import (
  3. "crypto"
  4. "crypto/rand"
  5. "math/big"
  6. "testing"
  7. "github.com/iden3/go-iden3-crypto/poseidon"
  8. "github.com/stretchr/testify/require"
  9. )
  10. // https://pkg.go.dev/crypto#PrivateKey
  11. type shadowPrivateKey interface {
  12. Public() crypto.PublicKey
  13. Equal(x crypto.PrivateKey) bool
  14. }
  15. // https://pkg.go.dev/crypto#PublicKey
  16. type shadowPublicKey interface {
  17. Equal(x crypto.PublicKey) bool
  18. }
  19. func TestBjjWrappedPrivateKeyInterfaceImpl(t *testing.T) {
  20. require.Implements(t, (*crypto.Signer)(nil), new(BjjWrappedPrivateKey))
  21. require.Implements(t, (*shadowPrivateKey)(nil), new(BjjWrappedPrivateKey))
  22. }
  23. func TestBjjWrappedPrivateKey(t *testing.T) {
  24. pk := RandomBjjWrappedKey()
  25. hasher := poseidon.New()
  26. hasher.Write([]byte("test"))
  27. digest := hasher.Sum(nil)
  28. sig, err := pk.Sign(rand.Reader, digest, crypto.Hash(0))
  29. require.NoError(t, err)
  30. pub, ok := pk.Public().(*BjjWrappedPublicKey)
  31. require.True(t, ok)
  32. decomrpessSig, err := decomrpessSig(sig)
  33. require.NoError(t, err)
  34. digestBI := big.NewInt(0).SetBytes(digest)
  35. pub.pubKey.VerifyPoseidon(digestBI, decomrpessSig)
  36. }
  37. func TestBjjWrappedPrivateKeyEqual(t *testing.T) {
  38. x1 := RandomBjjWrappedKey()
  39. require.True(t, x1.Equal(x1))
  40. x2 := RandomBjjWrappedKey()
  41. require.False(t, x1.Equal(x2))
  42. }
  43. func TestBjjWrappedPublicKeyInterfaceImpl(t *testing.T) {
  44. require.Implements(t, (*shadowPublicKey)(nil), new(BjjWrappedPublicKey))
  45. }
  46. func TestBjjWrappedPublicKeyEqual(t *testing.T) {
  47. x1 := RandomBjjWrappedKey().Public().(*BjjWrappedPublicKey)
  48. require.True(t, x1.Equal(x1))
  49. x2 := RandomBjjWrappedKey().Public()
  50. require.False(t, x1.Equal(x2))
  51. }
  52. func decomrpessSig(commpresedSig []byte) (*Signature, error) {
  53. poseidonComSig := &SignatureComp{}
  54. if err := poseidonComSig.UnmarshalText(commpresedSig); err != nil {
  55. return nil, err
  56. }
  57. poseidonDecSig, err := poseidonComSig.Decompress()
  58. if err != nil {
  59. return nil, err
  60. }
  61. return poseidonDecSig, nil
  62. }