From e3584692a4d380e6fe58008a4d238932632d890a Mon Sep 17 00:00:00 2001 From: Hussein Ait Lahcen Date: Mon, 9 Oct 2023 11:55:31 +0200 Subject: [PATCH] feat(galoisd): sha256 test --- galoisd/pkg/sha256/api.go | 11 ----- galoisd/pkg/sha256/api_test.go | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 galoisd/pkg/sha256/api_test.go diff --git a/galoisd/pkg/sha256/api.go b/galoisd/pkg/sha256/api.go index 7751c9eca7..95ed6d132c 100644 --- a/galoisd/pkg/sha256/api.go +++ b/galoisd/pkg/sha256/api.go @@ -283,7 +283,6 @@ func padding(api frontend.API, input []frontend.Variable, size frontend.Variable // helpers inputLen := len(input) - paddingLen := inputLen % 64 // t is start index of inputBitLen encoding var t int @@ -304,16 +303,6 @@ func padding(api frontend.API, input []frontend.Variable, size frontend.Variable out[i] = 0 } - // return if no padding required - if paddingLen == 0 { - - // overwrite into fixed size slice - for i := 0; i < inputLen; i++ { - out[i] = input[i] - } - return out - } - // existing bytes into out for i := 0; i < inputLen; i++ { out[i] = input[i] diff --git a/galoisd/pkg/sha256/api_test.go b/galoisd/pkg/sha256/api_test.go new file mode 100644 index 0000000000..39467b18b9 --- /dev/null +++ b/galoisd/pkg/sha256/api_test.go @@ -0,0 +1,79 @@ +package sha256 + +import ( + "crypto/rand" + "crypto/sha256" + "testing" + + "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark/backend" + "github.com/consensys/gnark/frontend" + "github.com/consensys/gnark/test" +) + +const compressThreshold = 1000 + +const MaxPreimageLength = 64 + +const ImageLength = 32 + +type sha256Circuit struct { + PreimageLength frontend.Variable + Preimage [MaxPreimageLength]frontend.Variable + Image [ImageLength]frontend.Variable +} + +func (c *sha256Circuit) Define(api frontend.API) error { + api.AssertIsLessOrEqual(c.PreimageLength, MaxPreimageLength) + hash := NewSHA256(api) + actualPreimage := make([]frontend.Variable, MaxPreimageLength) + for i := 0; i < MaxPreimageLength; i++ { + actualPreimage[i] = c.Preimage[i] + } + image := hash.Hash(actualPreimage, c.PreimageLength) + for i := 0; i < ImageLength; i++ { + api.AssertIsEqual(image[i], c.Image[i]) + } + return nil +} + +func TestSha256(t *testing.T) { + message := make([]byte, MaxPreimageLength) + _, err := rand.Read(message) + if err != nil { + panic(err) + } + + nativeHasher := sha256.New() + nativeHasher.Write(message) + final := nativeHasher.Sum(nil) + + var preimage [MaxPreimageLength]frontend.Variable + for i := 0; i < MaxPreimageLength; i++ { + if i < len(message) { + preimage[i] = message[i] + } else { + preimage[i] = 0 + } + } + + var image [ImageLength]frontend.Variable + for i := 0; i < ImageLength; i++ { + image[i] = final[i] + } + + circuit := sha256Circuit{} + assignment := sha256Circuit{ + Preimage: preimage, + PreimageLength: len(message), + Image: image, + } + test.NewAssert(t).ProverSucceeded( + &circuit, + &assignment, + test.WithCurves(ecc.BN254), + test.NoFuzzing(), + test.WithCurves(ecc.BN254), + test.WithBackends(backend.GROTH16), + ) +}