diff --git a/ecc/bls12-377/fr/mimc/mimc.go b/ecc/bls12-377/fr/mimc/mimc.go index d393f0d74..ea08f1174 100644 --- a/ecc/bls12-377/fr/mimc/mimc.go +++ b/ecc/bls12-377/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bls12-377/fr/mimc/options.go b/ecc/bls12-377/fr/mimc/options.go new file mode 100644 index 000000000..be68e4b58 --- /dev/null +++ b/ecc/bls12-377/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bls12-378/fr/mimc/mimc.go b/ecc/bls12-378/fr/mimc/mimc.go index 38cde3905..c351124ae 100644 --- a/ecc/bls12-378/fr/mimc/mimc.go +++ b/ecc/bls12-378/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bls12-378/fr/mimc/options.go b/ecc/bls12-378/fr/mimc/options.go new file mode 100644 index 000000000..c5ae735e1 --- /dev/null +++ b/ecc/bls12-378/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bls12-378/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bls12-381/fr/mimc/mimc.go b/ecc/bls12-381/fr/mimc/mimc.go index 716a655b4..f492cadf9 100644 --- a/ecc/bls12-381/fr/mimc/mimc.go +++ b/ecc/bls12-381/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bls12-381/fr/mimc/options.go b/ecc/bls12-381/fr/mimc/options.go new file mode 100644 index 000000000..20d1dafba --- /dev/null +++ b/ecc/bls12-381/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bls24-315/fr/mimc/mimc.go b/ecc/bls24-315/fr/mimc/mimc.go index 1912d66df..289944edc 100644 --- a/ecc/bls24-315/fr/mimc/mimc.go +++ b/ecc/bls24-315/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bls24-315/fr/mimc/options.go b/ecc/bls24-315/fr/mimc/options.go new file mode 100644 index 000000000..e25f3cce2 --- /dev/null +++ b/ecc/bls24-315/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bls24-315/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bls24-317/fr/mimc/mimc.go b/ecc/bls24-317/fr/mimc/mimc.go index c0050e056..b9024e7e5 100644 --- a/ecc/bls24-317/fr/mimc/mimc.go +++ b/ecc/bls24-317/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bls24-317/fr/mimc/options.go b/ecc/bls24-317/fr/mimc/options.go new file mode 100644 index 000000000..2ef5de3f5 --- /dev/null +++ b/ecc/bls24-317/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bls24-317/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bn254/fr/mimc/mimc.go b/ecc/bn254/fr/mimc/mimc.go index 719ed4e8a..c0158fc3a 100644 --- a/ecc/bn254/fr/mimc/mimc.go +++ b/ecc/bn254/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bn254/fr/mimc/mimc_test.go b/ecc/bn254/fr/mimc/mimc_test.go index dbd9740ad..73042f6c3 100644 --- a/ecc/bn254/fr/mimc/mimc_test.go +++ b/ecc/bn254/fr/mimc/mimc_test.go @@ -3,9 +3,11 @@ package mimc_test import ( "testing" + "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc" fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMiMCFiatShamir(t *testing.T) { @@ -16,3 +18,56 @@ func TestMiMCFiatShamir(t *testing.T) { _, err = fs.ComputeChallenge("c0") assert.NoError(t, err) } + +func TestByteOrder(t *testing.T) { + assert := require.New(t) + + var buf [fr.Bytes]byte + // if the 31 first bytes are FF, it's a valid FF in little endian, but not in big endian + for i := 0; i < fr.Bytes-1; i++ { + buf[i] = 0xFF + } + _, err := fr.BigEndian.Element(&buf) + assert.Error(err) + _, err = fr.LittleEndian.Element(&buf) + assert.NoError(err) + + { + // hashing buf with big endian should fail + mimcHash := mimc.NewMiMC(mimc.WithByteOrder(fr.BigEndian)) + _, err := mimcHash.Write(buf[:]) + assert.Error(err) + } + + { + // hashing buf with little endian should succeed + mimcHash := mimc.NewMiMC(mimc.WithByteOrder(fr.LittleEndian)) + _, err := mimcHash.Write(buf[:]) + assert.NoError(err) + } + + buf = [fr.Bytes]byte{} + // if the 31 bytes are FF, it's a valid FF in big endian, but not in little endian + for i := 1; i < fr.Bytes; i++ { + buf[i] = 0xFF + } + _, err = fr.BigEndian.Element(&buf) + assert.NoError(err) + _, err = fr.LittleEndian.Element(&buf) + assert.Error(err) + + { + // hashing buf with big endian should succeed + mimcHash := mimc.NewMiMC(mimc.WithByteOrder(fr.BigEndian)) + _, err := mimcHash.Write(buf[:]) + assert.NoError(err) + } + + { + // hashing buf with little endian should fail + mimcHash := mimc.NewMiMC(mimc.WithByteOrder(fr.LittleEndian)) + _, err := mimcHash.Write(buf[:]) + assert.Error(err) + } + +} diff --git a/ecc/bn254/fr/mimc/options.go b/ecc/bn254/fr/mimc/options.go new file mode 100644 index 000000000..ae82317a1 --- /dev/null +++ b/ecc/bn254/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bn254/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bw6-633/fr/mimc/mimc.go b/ecc/bw6-633/fr/mimc/mimc.go index 9cce55e36..bbf620172 100644 --- a/ecc/bw6-633/fr/mimc/mimc.go +++ b/ecc/bw6-633/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bw6-633/fr/mimc/options.go b/ecc/bw6-633/fr/mimc/options.go new file mode 100644 index 000000000..420ddbd06 --- /dev/null +++ b/ecc/bw6-633/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bw6-756/fr/mimc/mimc.go b/ecc/bw6-756/fr/mimc/mimc.go index 8078aa6c3..d53a5b0b2 100644 --- a/ecc/bw6-756/fr/mimc/mimc.go +++ b/ecc/bw6-756/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bw6-756/fr/mimc/options.go b/ecc/bw6-756/fr/mimc/options.go new file mode 100644 index 000000000..7fbe2e2e1 --- /dev/null +++ b/ecc/bw6-756/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bw6-756/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/ecc/bw6-761/fr/mimc/mimc.go b/ecc/bw6-761/fr/mimc/mimc.go index d93fe32f2..b53d52abf 100644 --- a/ecc/bw6-761/fr/mimc/mimc.go +++ b/ecc/bw6-761/fr/mimc/mimc.go @@ -41,8 +41,9 @@ var ( // digest represents the partial evaluation of the checksum // along with the params of the mimc function type digest struct { - h fr.Element - data []fr.Element // data to hash + h fr.Element + data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +57,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +114,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start : start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/ecc/bw6-761/fr/mimc/options.go b/ecc/bw6-761/fr/mimc/options.go new file mode 100644 index 000000000..996bb8862 --- /dev/null +++ b/ecc/bw6-761/fr/mimc/options.go @@ -0,0 +1,50 @@ +// Copyright 2020 Consensys Software Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package mimc + +import ( + "github.com/consensys/gnark-crypto/ecc/bw6-761/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +} diff --git a/internal/generator/crypto/hash/mimc/generate.go b/internal/generator/crypto/hash/mimc/generate.go index 9780bb79e..3783931ba 100644 --- a/internal/generator/crypto/hash/mimc/generate.go +++ b/internal/generator/crypto/hash/mimc/generate.go @@ -14,6 +14,7 @@ func Generate(conf config.Curve, baseDir string, bgen *bavard.BatchGenerator) er entries := []bavard.Entry{ {File: filepath.Join(baseDir, "doc.go"), Templates: []string{"doc.go.tmpl"}}, {File: filepath.Join(baseDir, "mimc.go"), Templates: []string{"mimc.go.tmpl"}}, + {File: filepath.Join(baseDir, "options.go"), Templates: []string{"options.go.tmpl"}}, } os.Remove(filepath.Join(baseDir, "utils.go")) os.Remove(filepath.Join(baseDir, "utils_test.go")) diff --git a/internal/generator/crypto/hash/mimc/template/mimc.go.tmpl b/internal/generator/crypto/hash/mimc/template/mimc.go.tmpl index a752a3bcc..ad1f77bdb 100644 --- a/internal/generator/crypto/hash/mimc/template/mimc.go.tmpl +++ b/internal/generator/crypto/hash/mimc/template/mimc.go.tmpl @@ -8,8 +8,6 @@ import ( "sync" ) - - const ( {{ if eq .Name "bn254" }} mimcNbRounds = 110 @@ -43,6 +41,7 @@ var ( type digest struct { h fr.Element data []fr.Element // data to hash + byteOrder fr.ByteOrder } // GetConstants exposed to be used in gnark @@ -56,9 +55,11 @@ func GetConstants() []big.Int { } // NewMiMC returns a MiMCImpl object, pure-go reference implementation -func NewMiMC() hash.Hash { +func NewMiMC(opts ...Option) hash.Hash { d := new(digest) d.Reset() + cfg := mimcOptions(opts...) + d.byteOrder = cfg.byteOrder return d } @@ -111,7 +112,7 @@ func (d *digest) Write(p []byte) (int, error) { var start int for start = 0; start < len(p); start += BlockSize { - if elem, err := fr.BigEndian.Element((*[BlockSize]byte)(p[start:start+BlockSize])); err == nil { + if elem, err := d.byteOrder.Element((*[BlockSize]byte)(p[start:start+BlockSize])); err == nil { d.data = append(d.data, elem) } else { return 0, err diff --git a/internal/generator/crypto/hash/mimc/template/options.go.tmpl b/internal/generator/crypto/hash/mimc/template/options.go.tmpl new file mode 100644 index 000000000..8194914cc --- /dev/null +++ b/internal/generator/crypto/hash/mimc/template/options.go.tmpl @@ -0,0 +1,32 @@ +import ( + "github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr" +) + +// Option defines option for altering the behavior of the MiMC hasher. +// See the descriptions of functions returning instances of this type for +// particular options. +type Option func(*mimcConfig) + +type mimcConfig struct { + byteOrder fr.ByteOrder +} + +// default options +func mimcOptions(opts ...Option) mimcConfig { + // apply options + opt := mimcConfig{ + byteOrder: fr.BigEndian, + } + for _, option := range opts { + option(&opt) + } + return opt +} + +// WithByteOrder sets the byte order used to decode the input +// in the Write method. Default is BigEndian. +func WithByteOrder(byteOrder fr.ByteOrder) Option { + return func(opt *mimcConfig) { + opt.byteOrder = byteOrder + } +}