diff --git a/ecc/bls12-377/fp/element_test.go b/ecc/bls12-377/fp/element_test.go index 2a1971116..430d42106 100644 --- a/ecc/bls12-377/fp/element_test.go +++ b/ecc/bls12-377/fp/element_test.go @@ -457,6 +457,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls12-377/fr/element_test.go b/ecc/bls12-377/fr/element_test.go index a8dc8c14a..ba4b6856a 100644 --- a/ecc/bls12-377/fr/element_test.go +++ b/ecc/bls12-377/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls12-377/fr/poseidon2/hash.go b/ecc/bls12-377/fr/poseidon2/hash.go index 631cd02da..57bf3621b 100644 --- a/ecc/bls12-377/fr/poseidon2/hash.go +++ b/ecc/bls12-377/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bls12-377/fr/poseidon2/poseidon2.go b/ecc/bls12-377/fr/poseidon2/poseidon2.go index 2a631437d..4c1208b71 100644 --- a/ecc/bls12-377/fr/poseidon2/poseidon2.go +++ b/ecc/bls12-377/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bls12-381/fp/element_test.go b/ecc/bls12-381/fp/element_test.go index d1339ef8d..c5e9cd5cd 100644 --- a/ecc/bls12-381/fp/element_test.go +++ b/ecc/bls12-381/fp/element_test.go @@ -457,6 +457,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls12-381/fr/element_test.go b/ecc/bls12-381/fr/element_test.go index 0172785b7..61a0e2602 100644 --- a/ecc/bls12-381/fr/element_test.go +++ b/ecc/bls12-381/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls12-381/fr/poseidon2/hash.go b/ecc/bls12-381/fr/poseidon2/hash.go index c5788f80d..ab568bcec 100644 --- a/ecc/bls12-381/fr/poseidon2/hash.go +++ b/ecc/bls12-381/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bls12-381/fr/poseidon2/poseidon2.go b/ecc/bls12-381/fr/poseidon2/poseidon2.go index fddff1f45..31fb07507 100644 --- a/ecc/bls12-381/fr/poseidon2/poseidon2.go +++ b/ecc/bls12-381/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bls24-315/fp/element_test.go b/ecc/bls24-315/fp/element_test.go index a4186669f..f3ffdeadf 100644 --- a/ecc/bls24-315/fp/element_test.go +++ b/ecc/bls24-315/fp/element_test.go @@ -455,6 +455,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls24-315/fr/element_test.go b/ecc/bls24-315/fr/element_test.go index 25eb19d2a..2faec8ce6 100644 --- a/ecc/bls24-315/fr/element_test.go +++ b/ecc/bls24-315/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls24-315/fr/poseidon2/hash.go b/ecc/bls24-315/fr/poseidon2/hash.go index 23285b149..d8334b99c 100644 --- a/ecc/bls24-315/fr/poseidon2/hash.go +++ b/ecc/bls24-315/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bls24-315/fr/poseidon2/poseidon2.go b/ecc/bls24-315/fr/poseidon2/poseidon2.go index 2076b7f07..ccc0b650e 100644 --- a/ecc/bls24-315/fr/poseidon2/poseidon2.go +++ b/ecc/bls24-315/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bls24-317/fp/element_test.go b/ecc/bls24-317/fp/element_test.go index e9de2cefa..b6cd95f89 100644 --- a/ecc/bls24-317/fp/element_test.go +++ b/ecc/bls24-317/fp/element_test.go @@ -455,6 +455,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls24-317/fr/element_test.go b/ecc/bls24-317/fr/element_test.go index 974e54b86..61e492dba 100644 --- a/ecc/bls24-317/fr/element_test.go +++ b/ecc/bls24-317/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bls24-317/fr/poseidon2/hash.go b/ecc/bls24-317/fr/poseidon2/hash.go index e416f0615..6d5908818 100644 --- a/ecc/bls24-317/fr/poseidon2/hash.go +++ b/ecc/bls24-317/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bls24-317/fr/poseidon2/poseidon2.go b/ecc/bls24-317/fr/poseidon2/poseidon2.go index 6801ab065..42b44b02a 100644 --- a/ecc/bls24-317/fr/poseidon2/poseidon2.go +++ b/ecc/bls24-317/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bn254/fp/element_test.go b/ecc/bn254/fp/element_test.go index c593258d7..a9a82ade6 100644 --- a/ecc/bn254/fp/element_test.go +++ b/ecc/bn254/fp/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bn254/fr/element_test.go b/ecc/bn254/fr/element_test.go index eb958912d..5784f7a73 100644 --- a/ecc/bn254/fr/element_test.go +++ b/ecc/bn254/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bn254/fr/poseidon2/hash.go b/ecc/bn254/fr/poseidon2/hash.go index 2930ce251..9a291ae86 100644 --- a/ecc/bn254/fr/poseidon2/hash.go +++ b/ecc/bn254/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bn254/fr/poseidon2/poseidon2.go b/ecc/bn254/fr/poseidon2/poseidon2.go index e9831ee66..40a49dbd2 100644 --- a/ecc/bn254/fr/poseidon2/poseidon2.go +++ b/ecc/bn254/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bw6-633/fp/element_test.go b/ecc/bw6-633/fp/element_test.go index 683bcdd0a..dd5fe6701 100644 --- a/ecc/bw6-633/fp/element_test.go +++ b/ecc/bw6-633/fp/element_test.go @@ -465,6 +465,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bw6-633/fr/element_test.go b/ecc/bw6-633/fr/element_test.go index 21e76b150..010cfbd16 100644 --- a/ecc/bw6-633/fr/element_test.go +++ b/ecc/bw6-633/fr/element_test.go @@ -455,6 +455,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bw6-633/fr/poseidon2/hash.go b/ecc/bw6-633/fr/poseidon2/hash.go index f5eb25338..6a4cfddea 100644 --- a/ecc/bw6-633/fr/poseidon2/hash.go +++ b/ecc/bw6-633/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bw6-633/fr/poseidon2/poseidon2.go b/ecc/bw6-633/fr/poseidon2/poseidon2.go index b3a90546a..51b26364b 100644 --- a/ecc/bw6-633/fr/poseidon2/poseidon2.go +++ b/ecc/bw6-633/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/bw6-761/fp/element_test.go b/ecc/bw6-761/fp/element_test.go index 4419103b6..40a7d0bd3 100644 --- a/ecc/bw6-761/fp/element_test.go +++ b/ecc/bw6-761/fp/element_test.go @@ -469,6 +469,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bw6-761/fr/element_test.go b/ecc/bw6-761/fr/element_test.go index 36859ef68..3864e60a5 100644 --- a/ecc/bw6-761/fr/element_test.go +++ b/ecc/bw6-761/fr/element_test.go @@ -457,6 +457,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/bw6-761/fr/poseidon2/hash.go b/ecc/bw6-761/fr/poseidon2/hash.go index da5d124ca..93f85eca7 100644 --- a/ecc/bw6-761/fr/poseidon2/hash.go +++ b/ecc/bw6-761/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/bw6-761/fr/poseidon2/poseidon2.go b/ecc/bw6-761/fr/poseidon2/poseidon2.go index 833fd242e..58738b1c3 100644 --- a/ecc/bw6-761/fr/poseidon2/poseidon2.go +++ b/ecc/bw6-761/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/grumpkin/fp/element_test.go b/ecc/grumpkin/fp/element_test.go index 1c858fc8a..50a7d4fec 100644 --- a/ecc/grumpkin/fp/element_test.go +++ b/ecc/grumpkin/fp/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/grumpkin/fr/element_test.go b/ecc/grumpkin/fr/element_test.go index c99df3572..4cefc59a0 100644 --- a/ecc/grumpkin/fr/element_test.go +++ b/ecc/grumpkin/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/grumpkin/fr/poseidon2/hash.go b/ecc/grumpkin/fr/poseidon2/hash.go index 5cf8077e7..758b17ccb 100644 --- a/ecc/grumpkin/fr/poseidon2/hash.go +++ b/ecc/grumpkin/fr/poseidon2/hash.go @@ -17,7 +17,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/ecc/grumpkin/fr/poseidon2/poseidon2.go b/ecc/grumpkin/fr/poseidon2/poseidon2.go index ff1ac9aa7..3e0d0b9bb 100644 --- a/ecc/grumpkin/fr/poseidon2/poseidon2.go +++ b/ecc/grumpkin/fr/poseidon2/poseidon2.go @@ -142,6 +142,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element diff --git a/ecc/secp256k1/fp/element_test.go b/ecc/secp256k1/fp/element_test.go index 074234814..d8b8bd80f 100644 --- a/ecc/secp256k1/fp/element_test.go +++ b/ecc/secp256k1/fp/element_test.go @@ -451,6 +451,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/secp256k1/fr/element_test.go b/ecc/secp256k1/fr/element_test.go index a67a52d9a..1112ffb12 100644 --- a/ecc/secp256k1/fr/element_test.go +++ b/ecc/secp256k1/fr/element_test.go @@ -451,6 +451,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/stark-curve/fp/element_test.go b/ecc/stark-curve/fp/element_test.go index 6f0419b08..d5457b524 100644 --- a/ecc/stark-curve/fp/element_test.go +++ b/ecc/stark-curve/fp/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/ecc/stark-curve/fr/element_test.go b/ecc/stark-curve/fr/element_test.go index 741adb877..6578dd4ba 100644 --- a/ecc/stark-curve/fr/element_test.go +++ b/ecc/stark-curve/fr/element_test.go @@ -453,6 +453,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/field/babybear/element_test.go b/field/babybear/element_test.go index 06fed55bf..b96929dd8 100644 --- a/field/babybear/element_test.go +++ b/field/babybear/element_test.go @@ -398,6 +398,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/field/babybear/poseidon2/hash.go b/field/babybear/poseidon2/hash.go index cf37f0c12..a7edc2eb2 100644 --- a/field/babybear/poseidon2/hash.go +++ b/field/babybear/poseidon2/hash.go @@ -16,13 +16,19 @@ import ( // NewMerkleDamgardHasher returns a Poseidon2 hasher using the Merkle-Damgard // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { - params := GetDefaultParameters() + p := NewDefaultPermutation() return gnarkHash.NewMerkleDamgardHasher( - &Permutation{params}, - make([]byte, params.Width/2*fr.Bytes), + p, + make([]byte, p.params.Width/2*fr.Bytes), ) } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this field. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. // The default parameters are, // diff --git a/field/generator/internal/templates/element/tests.go b/field/generator/internal/templates/element/tests.go index 622ff0d5f..ba0ec748c 100644 --- a/field/generator/internal/templates/element/tests.go +++ b/field/generator/internal/templates/element/tests.go @@ -475,6 +475,19 @@ func Test{{toTitle .ElementName}}Bytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPair{{.ElementName}}) bool { + var b {{.ElementName}} + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/field/generator/internal/templates/poseidon2/hash.go.tmpl b/field/generator/internal/templates/poseidon2/hash.go.tmpl index 471a4270e..dd7783ec6 100644 --- a/field/generator/internal/templates/poseidon2/hash.go.tmpl +++ b/field/generator/internal/templates/poseidon2/hash.go.tmpl @@ -8,13 +8,19 @@ import ( // NewMerkleDamgardHasher returns a Poseidon2 hasher using the Merkle-Damgard // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { - params := GetDefaultParameters() + p := NewDefaultPermutation() return gnarkHash.NewMerkleDamgardHasher( - &Permutation{params}, - make([]byte, params.Width/2*fr.Bytes), + p, + make([]byte, p.params.Width/2*fr.Bytes), ) } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this field. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. // The default parameters are, // diff --git a/field/goldilocks/element_test.go b/field/goldilocks/element_test.go index d9bfd73da..f563accdf 100644 --- a/field/goldilocks/element_test.go +++ b/field/goldilocks/element_test.go @@ -398,6 +398,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/field/goldilocks/poseidon2/hash.go b/field/goldilocks/poseidon2/hash.go index 3c4b7456a..d90063940 100644 --- a/field/goldilocks/poseidon2/hash.go +++ b/field/goldilocks/poseidon2/hash.go @@ -16,13 +16,19 @@ import ( // NewMerkleDamgardHasher returns a Poseidon2 hasher using the Merkle-Damgard // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { - params := GetDefaultParameters() + p := NewDefaultPermutation() return gnarkHash.NewMerkleDamgardHasher( - &Permutation{params}, - make([]byte, params.Width/2*fr.Bytes), + p, + make([]byte, p.params.Width/2*fr.Bytes), ) } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this field. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. // The default parameters are, // diff --git a/field/koalabear/element_test.go b/field/koalabear/element_test.go index 039781aa1..17c0239f1 100644 --- a/field/koalabear/element_test.go +++ b/field/koalabear/element_test.go @@ -398,6 +398,19 @@ func TestElementBytes(t *testing.T) { genA, )) + properties.Property("SetBytesCanonical(Bytes()) should stay constant", prop.ForAll( + func(a testPairElement) bool { + var b Element + bytes := a.element.Bytes() + if err := b.SetBytesCanonical(bytes[:]); err != nil { + t.Error(err) + return false + } + return a.element.Equal(&b) + }, + genA, + )) + properties.TestingRun(t, gopter.ConsoleReporter(false)) } diff --git a/field/koalabear/poseidon2/hash.go b/field/koalabear/poseidon2/hash.go index e37239348..c05c42017 100644 --- a/field/koalabear/poseidon2/hash.go +++ b/field/koalabear/poseidon2/hash.go @@ -16,13 +16,19 @@ import ( // NewMerkleDamgardHasher returns a Poseidon2 hasher using the Merkle-Damgard // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { - params := GetDefaultParameters() + p := NewDefaultPermutation() return gnarkHash.NewMerkleDamgardHasher( - &Permutation{params}, - make([]byte, params.Width/2*fr.Bytes), + p, + make([]byte, p.params.Width/2*fr.Bytes), ) } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this field. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. // The default parameters are, // diff --git a/field/koalabear/poseidon2/test-vectors.json b/field/koalabear/poseidon2/test-vectors.json new file mode 100644 index 000000000..4aac2a013 --- /dev/null +++ b/field/koalabear/poseidon2/test-vectors.json @@ -0,0 +1,24 @@ +[ + { "in": [], + "out": [0, 0, 0, 0, 0, 0, 0, 0] + }, + { + "in": [[0, 1, 2, 3, 4, 5, 6, 7]], + "out": [1402915949, 2083603674, 612464613, 903296317, 1327519169, 1646568100, 1786812904, 1917132066] + }, + { + "in": [ + [20, 30, 40, 50, 60, 70, 80, 90], + [120, 130, 140, 150, 160, 170, 180, 190] + ], + "out": [1735559108, 1813232198, 1226565528, 1795826654, 232717972, 522260852, 1109013755, 1232040797] + }, + { + "in": [ + [20, 30, 40, 50, 60, 70, 80, 90], + [120, 130, 140, 150, 160, 170, 180, 190], + [320, 330, 340, 350, 360, 370, 380, 390] + ], + "out": [2089473715, 1269546008, 32120401, 1497392503, 1194364877, 1059604979, 1458772903, 705747974] + } +] \ No newline at end of file diff --git a/field/koalabear/poseidon2/vectors_test.go b/field/koalabear/poseidon2/vectors_test.go new file mode 100644 index 000000000..0125406b5 --- /dev/null +++ b/field/koalabear/poseidon2/vectors_test.go @@ -0,0 +1,58 @@ +package poseidon2 + +import ( + "bytes" + "encoding/binary" + "encoding/json" + "os" + "testing" + + "github.com/consensys/gnark-crypto/field/koalabear" + "github.com/stretchr/testify/require" +) + +const width = 16 + +type testCase struct { + In [][]uint32 `json:"in"` + Out []uint32 `json:"out"` +} + +func TestVectors(t *testing.T) { + + f, err := os.Open("test-vectors.json") + require.NoError(t, err) + defer f.Close() + + var testCases []testCase + require.NoError(t, json.NewDecoder(f).Decode(&testCases)) + + hsh := NewMerkleDamgardHasher() + require.Equal(t, width, GetDefaultParameters().Width, "unexpected hash width") + require.Equal(t, width*koalabear.Bytes, 2*hsh.BlockSize(), "unexpected input block size") + + var buf [width * koalabear.Bytes / 2]byte + + for _, c := range testCases { + hsh.Reset() + require.Equal(t, width/2, len(c.Out), "unexpected output block size") + + for _, in := range c.In { + require.Equal(t, width/2, len(in), "unexpected input block size") + + for i := range in { + _, err = binary.Encode(buf[i*koalabear.Bytes:(i+1)*koalabear.Bytes], binary.BigEndian, in[i]) + require.NoError(t, err) + } + _, err = hsh.Write(buf[:]) + require.NoError(t, err) + } + + res := bytes.NewReader(hsh.Sum(nil)) + var out [width / 2]uint32 + for i := range width / 2 { + require.NoError(t, binary.Read(res, binary.BigEndian, &out[i])) + } + require.Equal(t, c.Out, out[:], "unexpected output on input %v", c.In) + } +} diff --git a/internal/generator/crypto/hash/poseidon2/template/hash.go.tmpl b/internal/generator/crypto/hash/poseidon2/template/hash.go.tmpl index 51d7ee945..d25d8229f 100644 --- a/internal/generator/crypto/hash/poseidon2/template/hash.go.tmpl +++ b/internal/generator/crypto/hash/poseidon2/template/hash.go.tmpl @@ -9,7 +9,7 @@ import ( // construction with the default parameters. func NewMerkleDamgardHasher() gnarkHash.StateStorer { return gnarkHash.NewMerkleDamgardHasher( - &Permutation{GetDefaultParameters()}, make([]byte, fr.Bytes)) + NewDefaultPermutation(), make([]byte, fr.Bytes)) } // GetDefaultParameters returns a set of parameters for the Poseidon2 permutation. diff --git a/internal/generator/crypto/hash/poseidon2/template/poseidon2.go.tmpl b/internal/generator/crypto/hash/poseidon2/template/poseidon2.go.tmpl index 94daa9d18..6ebb48dd3 100644 --- a/internal/generator/crypto/hash/poseidon2/template/poseidon2.go.tmpl +++ b/internal/generator/crypto/hash/poseidon2/template/poseidon2.go.tmpl @@ -141,6 +141,12 @@ func NewPermutationWithSeed(t, rf, rp int, seed string) *Permutation { return res } +// NewDefaultPermutation returns a Poseidon2 permutation with the default +// recommended parameters for this curve. +func NewDefaultPermutation() *Permutation { + return &Permutation{params: GetDefaultParameters()} +} + // sBox applies the sBox on buffer[index] func (h *Permutation) sBox(index int, input []fr.Element) { var tmp fr.Element