From f7d1968b22ebd6565f8970b99e26640a72343c50 Mon Sep 17 00:00:00 2001 From: puma314 Date: Mon, 3 Oct 2022 21:56:27 -0700 Subject: [PATCH] Compiles --- sha512/sha512compression.go | 6 +++--- sha512/sha_test.go | 37 +++++++++++++++++++++++++++++++++++++ sha512/sigmaplus.go | 7 ++++++- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 sha512/sha_test.go diff --git a/sha512/sha512compression.go b/sha512/sha512compression.go index fd5c4a2..af3465b 100644 --- a/sha512/sha512compression.go +++ b/sha512/sha512compression.go @@ -88,8 +88,8 @@ func Sha512compression(api frontend.API, hin, inp []frontend.Variable) ([]fronte // t2[t].c[k] <== c[t][k]; // } - sume := BinSum(d[t], t1) - suma := BinSum(t1, t2) + sume := BinSum(api, d[t][:], t1) + suma := BinSum(api, t1, t2) // for (k=0; k<64; k++) { // sume[t].in[0][k] <== d[t][k]; // sume[t].in[1][k] <== t1[t].out[k]; @@ -161,7 +161,7 @@ func Sha512compression(api frontend.API, hin, inp []frontend.Variable) ([]fronte var fsum [8][]frontend.Variable for i := 0; i < 8; i++ { - fsum[i] = BinSum(fsum_in[i][0], fsum_in[i][1]) + fsum[i] = BinSum(api, fsum_in[i][0][:], fsum_in[i][1][:]) } var out [512]frontend.Variable diff --git a/sha512/sha_test.go b/sha512/sha_test.go new file mode 100644 index 0000000..fe567fb --- /dev/null +++ b/sha512/sha_test.go @@ -0,0 +1,37 @@ +package sha512 + +import ( + "math/big" + "testing" + + "github.com/consensys/gnark/frontend" + "github.com/consensys/gnark/test" +) + +type Sha512Circuit struct { + in []frontend.Variable `gnark:"in"` + out []frontend.Variable `gnark:"out"` +} + +func (circuit *Sha512Circuit) Define(api frontend.API) error { + res := Sha512(api, circuit.in) + for i := 0; i < 512; i++ { + api.AssertIsEqual(res[i], circuit.out[i]) + } + return nil +} + +func TestSha512(t *testing.T) { + assert := test.NewAssert(t) + circuit := OnCurveTest[Ed25519, Ed25519Scalars]{} + witness := OnCurveTest[Ed25519, Ed25519Scalars]{ + P: AffinePoint[Ed25519]{ + X: emulated.NewElement[Ed25519](newBigInt("216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A")), + Y: emulated.NewElement[Ed25519](newBigInt("6666666666666666666666666666666666666666666666666666666666666658")), + }, + } + err := test.IsSolved(&circuit, &witness, testCurve.ScalarField()) + assert.NoError(err) +} + +var testCurve = ecc.BN254 diff --git a/sha512/sigmaplus.go b/sha512/sigmaplus.go index 1daacac..4ac5a78 100644 --- a/sha512/sigmaplus.go +++ b/sha512/sigmaplus.go @@ -11,7 +11,12 @@ func SigmaPlus512(api frontend.API, in2, in7, in15, in16 []frontend.Variable) ([ sigma1 := SmallSigma512(api, in2, 19, 61, 6) sigma0 := SmallSigma512(api, in15, 1, 8, 7) - return BinSum(api, sigma1, in7, sigma0, in16) + inter := BinSum(api, sigma1, in7, sigma0, in16) + var out [64]frontend.Variable + for k := 0; k < 64; k++ { + out[k] = inter[k] + } + return out }