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.

78 lines
2.0 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, err := poseidon.New(16)
  26. require.NoError(t, err)
  27. hasher.Write([]byte("test"))
  28. digest := hasher.Sum(nil)
  29. sig, err := pk.Sign(rand.Reader, digest, crypto.Hash(0))
  30. require.NoError(t, err)
  31. pub, ok := pk.Public().(*BjjWrappedPublicKey)
  32. require.True(t, ok)
  33. decomrpessSig, err := DecompressSig(sig)
  34. require.NoError(t, err)
  35. digestBI := big.NewInt(0).SetBytes(digest)
  36. pub.pubKey.VerifyPoseidon(digestBI, decomrpessSig)
  37. }
  38. func TestBjjWrappedPrivateKeyEqual(t *testing.T) {
  39. x1 := RandomBjjWrappedKey()
  40. require.True(t, x1.Equal(x1))
  41. x2 := RandomBjjWrappedKey()
  42. require.False(t, x1.Equal(x2))
  43. }
  44. func TestBjjWrappedPublicKeyInterfaceImpl(t *testing.T) {
  45. require.Implements(t, (*shadowPublicKey)(nil), new(BjjWrappedPublicKey))
  46. }
  47. func TestBjjWrappedPublicKeyEqual(t *testing.T) {
  48. x1 := RandomBjjWrappedKey().Public().(*BjjWrappedPublicKey)
  49. require.True(t, x1.Equal(x1))
  50. x2 := RandomBjjWrappedKey().Public()
  51. require.False(t, x1.Equal(x2))
  52. }
  53. // DecompressSig decompresses a compressed signature.
  54. func DecompressSig(commpresedSig []byte) (*Signature, error) {
  55. poseidonComSig := &SignatureComp{}
  56. if err := poseidonComSig.UnmarshalText(commpresedSig); err != nil {
  57. return nil, err
  58. }
  59. poseidonDecSig, err := poseidonComSig.Decompress()
  60. if err != nil {
  61. return nil, err
  62. }
  63. return poseidonDecSig, nil
  64. }