Skip to content

Commit

Permalink
feat: use blake2 for variable hashcode (Consensys#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivokub authored Jul 12, 2024
1 parent 065027a commit 378dd33
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions frontend/cs/r1cs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type builder struct {
kvstore.Store

// map for recording boolean constrained variables (to not constrain them twice)
mtBooleans map[uint64][]expr.LinearExpression
mtBooleans map[[16]byte][]expr.LinearExpression

tOne constraint.Element
eZero, eOne expr.LinearExpression
Expand All @@ -82,7 +82,7 @@ func newBuilder(field *big.Int, config frontend.CompileConfig) *builder {
macCapacity = config.CompressThreshold
}
builder := builder{
mtBooleans: make(map[uint64][]expr.LinearExpression, config.Capacity/10),
mtBooleans: make(map[[16]byte][]expr.LinearExpression, config.Capacity/10),
config: config,
heap: make(minHeap, 0, 100),
mbuf1: make(expr.LinearExpression, 0, macCapacity),
Expand Down
19 changes: 12 additions & 7 deletions frontend/internal/expr/linear_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expr

import (
"github.com/consensys/gnark/constraint"
"golang.org/x/crypto/blake2b"
)

type LinearExpression []Term
Expand Down Expand Up @@ -52,12 +53,16 @@ func (l LinearExpression) Less(i, j int) bool {
return iID < jID
}

// HashCode returns a fast-to-compute but NOT collision resistant hash code identifier for the linear
// expression
func (l LinearExpression) HashCode() uint64 {
h := uint64(17)
for _, val := range l {
h = h*23 + val.HashCode() // TODO @gbotrel revisit
// HashCode returns a collision-resistant identifier of the linear expression. It is constructed from the hash codes of the terms.
func (l LinearExpression) HashCode() [16]byte {
h, err := blake2b.New256(nil)
if err != nil {
panic(err)
}
return h
for i := range l {
termHash := l[i].HashCode()
h.Write(termHash[:])
}
crc := h.Sum(nil)
return [16]byte(crc[:16])
}
21 changes: 18 additions & 3 deletions frontend/internal/expr/term.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package expr

import "github.com/consensys/gnark/constraint"
import (
"encoding/binary"

"github.com/consensys/gnark/constraint"
"golang.org/x/crypto/blake2b"
)

type Term struct {
VID int
Expand All @@ -20,6 +25,16 @@ func (t Term) WireID() int {
return t.VID
}

func (t Term) HashCode() uint64 {
return t.Coeff[0]*29 + uint64(t.VID<<12)
// HashCode returns a collision resistant hash code identifier for the term.
func (t Term) HashCode() [16]byte {
h, err := blake2b.New256(nil)
if err != nil {
panic(err)
}
h.Write(binary.BigEndian.AppendUint64(nil, uint64(t.VID)))
for i := range t.Coeff {
h.Write(binary.BigEndian.AppendUint64(nil, uint64(t.Coeff[i])))
}
crc := h.Sum(nil)
return [16]byte(crc[:16])
}
6 changes: 3 additions & 3 deletions std/math/emulated/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Field[T FieldParams] struct {

log zerolog.Logger

constrainedLimbs map[uint64]struct{}
constrainedLimbs map[[16]byte]struct{}
checker frontend.Rangechecker

mulChecks []mulCheck[T]
Expand All @@ -69,7 +69,7 @@ func NewField[T FieldParams](native frontend.API) (*Field[T], error) {
f := &Field[T]{
api: native,
log: logger.Logger(),
constrainedLimbs: make(map[uint64]struct{}),
constrainedLimbs: make(map[[16]byte]struct{}),
checker: rangecheck.New(native),
}

Expand Down Expand Up @@ -216,7 +216,7 @@ func (f *Field[T]) enforceWidthConditional(a *Element[T]) (didConstrain bool) {
}
continue
}
if vv, ok := a.Limbs[i].(interface{ HashCode() uint64 }); ok {
if vv, ok := a.Limbs[i].(interface{ HashCode() [16]byte }); ok {
// okay, this is a canonical variable and it has a hashcode. We use
// it to see if the limb is already constrained.
h := vv.HashCode()
Expand Down

0 comments on commit 378dd33

Please sign in to comment.