From b8f2137e4fb843adee8b481661aae5f0455089b2 Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Fri, 10 Feb 2023 17:01:51 -0500 Subject: [PATCH 1/3] refactor: replace MapHash with ConstHash in sumcheck test vectors --- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../fr/test_vector_utils/test_vector_utils.go | 234 ++----------- .../generator/sumcheck/test_vectors/hash.json | 13 - .../generator/sumcheck/test_vectors/main.go | 37 +-- .../sumcheck/test_vectors/vectors.json | 4 +- .../test_vector_utils/test_vector_utils.go | 279 ++-------------- .../template/test_vector_utils.go.tmpl | 307 ++---------------- 14 files changed, 228 insertions(+), 2518 deletions(-) delete mode 100644 internal/generator/sumcheck/test_vectors/hash.json diff --git a/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go index aaa5f41e1b..afd66b06c1 100644 --- a/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go index de86845066..3d1c78f6fb 100644 --- a/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bls12-378/fr" "github.com/consensys/gnark-crypto/ecc/bls12-378/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go index 4bfd2a7907..0872c428c5 100644 --- a/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go b/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go index 17f7ca0cea..3d4db2d0a1 100644 --- a/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bls24-315/fr" "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go b/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go index a488ebc5a8..d5ede49eda 100644 --- a/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bls24-317/fr" "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bn254/fr/test_vector_utils/test_vector_utils.go b/ecc/bn254/fr/test_vector_utils/test_vector_utils.go index f39f6ae47b..6dccd71b58 100644 --- a/ecc/bn254/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bn254/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark-crypto/ecc/bn254/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go index 503f4cb4c0..893cdabc97 100644 --- a/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go index cf55446791..c805ba53e3 100644 --- a/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bw6-756/fr" "github.com/consensys/gnark-crypto/ecc/bw6-756/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go index 00b75ce227..794d3588cb 100644 --- a/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go @@ -17,247 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/ecc/bw6-761/fr" "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/polynomial" "hash" - - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 fr.Element - key2 fr.Element - key2Present bool - value fr.Element - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *fr.Element { + var res fr.Element + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := SetElement(&entry.value, v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := SetElement(&entry.key2, key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := SetElement(&entry.key1, key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state fr.Element - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x fr.Element - for i := 0; i < len(p); i += fr.Bytes { - x.SetBytes(p[i:min(len(p), i+fr.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return fr.Bytes -} - -func (m *MapHash) BlockSize() int { - return fr.Bytes -} - -func (m *MapHash) write(x fr.Element) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (fr.Element, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return fr.Element{}, fmt.Errorf(sb.String()) -} - -func (m *ElementMap) FindPair(x *fr.Element, y *fr.Element) (fr.Element, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *fr.Element { - var res fr.Element - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/fr.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/fr.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res fr.Element res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -277,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/internal/generator/sumcheck/test_vectors/hash.json b/internal/generator/sumcheck/test_vectors/hash.json deleted file mode 100644 index a3f147a260..0000000000 --- a/internal/generator/sumcheck/test_vectors/hash.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "-14,3":-4, - "-9,1":-4, - "-4,-2":3, - "-2,-4":1, - "3,-3":-2, - "4,-3":-3, - "26,-3":-2, - "27,-3":-2, - "482434100784":-3, - "482434100785":-4, - "482434100786":-2 -} \ No newline at end of file diff --git a/internal/generator/sumcheck/test_vectors/main.go b/internal/generator/sumcheck/test_vectors/main.go index 591042039f..798f5a4f3f 100644 --- a/internal/generator/sumcheck/test_vectors/main.go +++ b/internal/generator/sumcheck/test_vectors/main.go @@ -8,12 +8,13 @@ import ( "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/polynomial" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/sumcheck" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/test_vector_utils" + "hash" "math/bits" "os" "path/filepath" ) -func runMultilin(dir string, testCaseInfo *TestCaseInfo) error { +func runMultilin(testCaseInfo *TestCaseInfo) error { var poly polynomial.MultiLin if v, err := test_vector_utils.SliceToElementSlice(testCaseInfo.Values); err == nil { @@ -22,14 +23,14 @@ func runMultilin(dir string, testCaseInfo *TestCaseInfo) error { return err } - var mp *test_vector_utils.ElementMap + var hsh hash.Hash var err error - if mp, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, testCaseInfo.Hash)); err != nil { + if hsh, err = test_vector_utils.HashFromDescription(testCaseInfo.Hash); err != nil { return err } proof, err := sumcheck.Prove( - &singleMultilinClaim{poly}, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: mp})) + &singleMultilinClaim{poly}, fiatshamir.WithHash(hsh)) if err != nil { return err } @@ -46,21 +47,21 @@ func runMultilin(dir string, testCaseInfo *TestCaseInfo) error { return err } - if err = sumcheck.Verify(singleMultilinLazyClaim{g: poly, claimedSum: claimedSum}, proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: mp})); err != nil { + if err = sumcheck.Verify(singleMultilinLazyClaim{g: poly, claimedSum: claimedSum}, proof, fiatshamir.WithHash(hsh)); err != nil { return fmt.Errorf("proof rejected: %v", err) } proof.PartialSumPolys[0][0].Add(&proof.PartialSumPolys[0][0], test_vector_utils.ToElement(1)) - if err = sumcheck.Verify(singleMultilinLazyClaim{g: poly, claimedSum: claimedSum}, proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: mp})); err == nil { + if err = sumcheck.Verify(singleMultilinLazyClaim{g: poly, claimedSum: claimedSum}, proof, fiatshamir.WithHash(hsh)); err == nil { return fmt.Errorf("bad proof accepted") } return nil } -func run(dir string, testCaseInfo *TestCaseInfo) error { +func run(testCaseInfo *TestCaseInfo) error { switch testCaseInfo.Type { case "multilin": - return runMultilin(dir, testCaseInfo) + return runMultilin(testCaseInfo) default: return fmt.Errorf("type \"%s\" unrecognized", testCaseInfo.Type) } @@ -73,8 +74,6 @@ func runAll(relPath string) error { return err } - dir := filepath.Dir(filename) - var bytes []byte if bytes, err = os.ReadFile(filename); err != nil { @@ -88,7 +87,7 @@ func runAll(relPath string) error { failed := false for name, testCase := range testCasesInfo { - if err = run(dir, testCase); err != nil { + if err = run(testCase); err != nil { fmt.Println(name, ":", err) failed = true } @@ -102,10 +101,6 @@ func runAll(relPath string) error { return err } - if err = test_vector_utils.SaveUsedHashEntries(); err != nil { - return err - } - return os.WriteFile(filename, bytes, 0) } @@ -119,12 +114,12 @@ func main() { type TestCasesInfo map[string]*TestCaseInfo type TestCaseInfo struct { - Type string `json:"type"` - Hash string `json:"hash"` - Values []interface{} `json:"values"` - Description string `json:"description"` - Proof PrintableProof `json:"proof"` - ClaimedSum interface{} `json:"claimedSum"` + Type string `json:"type"` + Hash test_vector_utils.HashDescription `json:"hash"` + Values []interface{} `json:"values"` + Description string `json:"description"` + Proof PrintableProof `json:"proof"` + ClaimedSum interface{} `json:"claimedSum"` } type PrintableProof struct { diff --git a/internal/generator/sumcheck/test_vectors/vectors.json b/internal/generator/sumcheck/test_vectors/vectors.json index 8b1820e174..1550c82ba2 100644 --- a/internal/generator/sumcheck/test_vectors/vectors.json +++ b/internal/generator/sumcheck/test_vectors/vectors.json @@ -1,7 +1,7 @@ { "linear_univariate_single_claim": { "type": "multilin", - "hash": "hash.json", + "hash": {"type": "const", "val": -1}, "values": [ 1, 3 @@ -19,7 +19,7 @@ }, "trilinear_single_claim": { "type": "multilin", - "hash": "hash.json", + "hash": {"type": "const", "val": -1}, "values": [ 1, 2, diff --git a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go b/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go index 00eb39b48e..866b301871 100644 --- a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go +++ b/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go @@ -17,292 +17,51 @@ package test_vector_utils import ( - "encoding/json" "fmt" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/polynomial" "hash" - "math/rand" - "os" - "path/filepath" "reflect" - "sort" "strconv" "strings" ) -type ElementTriplet struct { - key1 small_rational.SmallRational - key2 small_rational.SmallRational - key2Present bool - value small_rational.SmallRational - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *small_rational.SmallRational { + var res small_rational.SmallRational + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) - - for k, v := range rawMap { - var entry ElementTriplet - if _, err := entry.value.SetInterface(v); err != nil { - return nil, err - } +type HashDescription map[string]interface{} - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := entry.key2.SetInterface(key[1]); err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } - if _, err := entry.key1.SetInterface(key[0]); err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state small_rational.SmallRational - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x small_rational.SmallRational - for i := 0; i < len(p); i += small_rational.Bytes { - x.SetBytes(p[i:min(len(p), i+small_rational.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return small_rational.Bytes -} - -func (m *MapHash) BlockSize() int { - return small_rational.Bytes -} - -func (m *MapHash) write(x small_rational.SmallRational) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} - -func SaveUsedHashEntries() error { - for path, hash := range MapCache { - if err := hash.SaveUsedEntries(path); err != nil { - return err - } - } - return nil -} - -func (t *ElementTriplet) writeKeyValue(sb *strings.Builder) error { - t.writeKey(sb) - sb.WriteRune(':') - - if valueBytes, err := json.Marshal(ElementToInterface(&t.value)); err == nil { - sb.WriteString(string(valueBytes)) - return nil - } else { - return err - } -} - -func (m *ElementMap) serializedUsedEntries() (string, error) { - var sb strings.Builder - sb.WriteRune('{') - - first := true - - for _, element := range *m { - if !element.used { - continue - } - if !first { - sb.WriteRune(',') - } - first = false - sb.WriteString("\n\t") - if err := element.writeKeyValue(&sb); err != nil { - return "", err - } - } - - sb.WriteString("\n}") - - return sb.String(), nil -} - -func (m *ElementMap) SaveUsedEntries(path string) error { - - if s, err := m.serializedUsedEntries(); err != nil { - return err - } else { - return os.WriteFile(path, []byte(s), 0) - } -} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) (small_rational.SmallRational, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - // if not found, add it: - if _, err := toFind.value.SetInterface(rand.Int63n(11) - 5); err != nil { - panic(err.Error()) - } - toFind.used = true - *m = append(*m, toFind) - m.sort() //Inefficient, but it's okay. This is only run when a new test case is introduced - - return toFind.value, nil -} - -func (m *ElementMap) FindPair(x *small_rational.SmallRational, y *small_rational.SmallRational) (small_rational.SmallRational, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *small_rational.SmallRational { - var res small_rational.SmallRational - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/small_rational.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/small_rational.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res small_rational.SmallRational res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -322,7 +81,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } diff --git a/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl b/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl index 12b63bfb95..96561c0a5e 100644 --- a/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl +++ b/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl @@ -1,317 +1,50 @@ import ( - "encoding/json" "fmt" "{{.FieldPackagePath}}" "{{.FieldPackagePath}}/polynomial" "hash" - {{if .RandomizeMissingHashEntries}}"math/rand"{{end}} - "os" - "path/filepath" - "sort" "strings" "strconv" "reflect" + "reflect" ) -type ElementTriplet struct { - key1 {{.ElementType}} - key2 {{.ElementType}} - key2Present bool - value {{.ElementType}} - used bool -} - -func (t *ElementTriplet) CmpKey(o *ElementTriplet) int { - if cmp1 := t.key1.Cmp(&o.key1); cmp1 != 0 { - return cmp1 - } - - if t.key2Present { - if o.key2Present { - return t.key2.Cmp(&o.key2) - } - return 1 - } else { - if o.key2Present { - return -1 - } - return 0 - } -} - -var MapCache = make(map[string]*ElementMap) - -func ElementMapFromFile(path string) (*ElementMap, error) { - path, err := filepath.Abs(path) - if err != nil { - return nil, err - } - if h, ok := MapCache[path]; ok { - return h, nil - } - var bytes []byte - if bytes, err = os.ReadFile(path); err == nil { - var asMap map[string]interface{} - if err = json.Unmarshal(bytes, &asMap); err != nil { - return nil, err - } - - var h ElementMap - if h, err = CreateElementMap(asMap); err == nil { - MapCache[path] = &h - } - - return &h, err - - } else { - return nil, err - } +func ToElement(i int64) *{{.ElementType}} { + var res {{.ElementType}} + res.SetInt64(i) + return &res } -func CreateElementMap(rawMap map[string]interface{}) (ElementMap, error) { - res := make(ElementMap, 0, len(rawMap)) +type HashDescription map[string]interface{} - for k, v := range rawMap { - var entry ElementTriplet - if _, err := {{setElement "entry.value" "v" .ElementType }}; err != nil { - return nil, err - } - - key := strings.Split(k, ",") - switch len(key) { - case 1: - entry.key2Present = false - case 2: - entry.key2Present = true - if _, err := {{setElement "entry.key2" "key[1]" .ElementType}}; err != nil { - return nil, err - } +func HashFromDescription(d HashDescription) (hash.Hash, error) { + if _type, ok := d["type"]; ok { + switch _type { + case "const": + startState := int64(d["val"].(int)) + return &MessageCounter {startState: startState, step: 0, state: startState}, nil default: - return nil, fmt.Errorf("cannot parse %T as one or two field elements", v) - } - if _, err := {{setElement "entry.key1" "key[0]" .ElementType }}; err != nil { - return nil, err - } - - res = append(res, &entry) - } - - res.sort() - return res, nil -} - -type ElementMap []*ElementTriplet - -type MapHash struct { - Map *ElementMap - state {{.ElementType}} - stateValid bool -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func (m *MapHash) Write(p []byte) (n int, err error) { - var x {{.ElementType}} - for i := 0; i < len(p); i += {{.FieldPackageName}}.Bytes { - x.SetBytes(p[i:min(len(p), i+{{.FieldPackageName}}.Bytes)]) - if err = m.write(x); err != nil { - return - } - } - n = len(p) - return -} - -func (m *MapHash) Sum(b []byte) []byte { - mP := *m - if _, err := mP.Write(b); err != nil { - panic(err) - } - bytes := mP.state.Bytes() - return bytes[:] -} - -func (m *MapHash) Reset() { - m.stateValid = false -} - -func (m *MapHash) Size() int { - return {{.FieldPackageName}}.Bytes -} - -func (m *MapHash) BlockSize() int { - return {{.FieldPackageName}}.Bytes -} - -func (m *MapHash) write(x {{.ElementType}}) error { - X := &x - Y := &m.state - if !m.stateValid { - Y = nil - } - var err error - if m.state, err = m.Map.FindPair(X, Y); err == nil { - m.stateValid = true - } - return err -} - -func (t *ElementTriplet) writeKey(sb *strings.Builder) { - sb.WriteRune('"') - sb.WriteString(t.key1.String()) - if t.key2Present { - sb.WriteRune(',') - sb.WriteString(t.key2.String()) - } - sb.WriteRune('"') -} - -{{- if .RandomizeMissingHashEntries}} - -func SaveUsedHashEntries() error { - for path, hash := range MapCache { - if err := hash.SaveUsedEntries(path); err != nil { - return err - } - } - return nil -} - -func (t *ElementTriplet) writeKeyValue(sb *strings.Builder) error { - t.writeKey(sb) - sb.WriteRune(':') - - if valueBytes, err := json.Marshal(ElementToInterface(&t.value)); err == nil { - sb.WriteString(string(valueBytes)) - return nil - } else { - return err - } -} - -func (m *ElementMap) serializedUsedEntries() (string, error) { - var sb strings.Builder - sb.WriteRune('{') - - first := true - - for _, element := range *m { - if !element.used { - continue - } - if !first { - sb.WriteRune(',') - } - first = false - sb.WriteString("\n\t") - if err := element.writeKeyValue(&sb); err != nil { - return "", err - } - } - - sb.WriteString("\n}") - - return sb.String(), nil -} - -func (m *ElementMap) SaveUsedEntries(path string) error { - - if s, err := m.serializedUsedEntries(); err != nil { - return err - } else { - return os.WriteFile(path, []byte(s), 0) - } -} -{{- else}} -func (m *ElementMap) UnusedEntries() []interface{} { - unused := make([]interface{}, 0) - for _, v := range *m { - if !v.used { - var vInterface interface{} - if v.key2Present { - vInterface = []interface{}{ElementToInterface(&v.key1), ElementToInterface(&v.key2)} - } else { - vInterface = ElementToInterface(&v.key1) - } - unused = append(unused, vInterface) + return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) } } - return unused -} -{{- end}} - -func (m *ElementMap) sort() { - sort.Slice(*m, func(i, j int) bool { - return (*m)[i].CmpKey((*m)[j]) <= 0 - }) -} - -func (m *ElementMap) find(toFind *ElementTriplet) ({{.ElementType}}, error) { - i := sort.Search(len(*m), func(i int) bool { return (*m)[i].CmpKey(toFind) >= 0 }) - - if i < len(*m) && (*m)[i].CmpKey(toFind) == 0 { - (*m)[i].used = true - return (*m)[i].value, nil - } - - {{- if .RandomizeMissingHashEntries}} - // if not found, add it: - if _, err := toFind.value.SetInterface(rand.Int63n(11) - 5); err != nil { - panic(err.Error()) - } - toFind.used = true - *m = append(*m, toFind) - m.sort() //Inefficient, but it's okay. This is only run when a new test case is introduced - - return toFind.value, nil - {{- else}} - var sb strings.Builder - sb.WriteString("no value available for input ") - toFind.writeKey(&sb) - return {{.ElementType}}{}, fmt.Errorf(sb.String()) - {{- end}} -} - -func (m *ElementMap) FindPair(x *{{.ElementType}}, y *{{.ElementType}}) ({{.ElementType}}, error) { - - toFind := ElementTriplet{ - key1: *x, - key2Present: y != nil, - } - - if y != nil { - toFind.key2 = *y - } - - return m.find(&toFind) -} - -func ToElement(i int64) *{{.ElementType}} { - var res {{.ElementType}} - res.SetInt64(i) - return &res + return nil, fmt.Errorf("hash description missing type") } type MessageCounter struct { - startState uint64 - state uint64 - step uint64 + startState int64 + state int64 + step int64 } func (m *MessageCounter) Write(p []byte) (n int, err error) { inputBlockSize := (len(p)-1)/{{.FieldPackageName}}.Bytes + 1 - m.state += uint64(inputBlockSize) * m.step + m.state += int64(inputBlockSize) * m.step return len(p), nil } func (m *MessageCounter) Sum(b []byte) []byte { inputBlockSize := (len(b)-1)/{{.FieldPackageName}}.Bytes + 1 - resI := m.state + uint64(inputBlockSize)*m.step + resI := m.state + int64(inputBlockSize)*m.step var res {{.ElementType}} res.SetInt64(int64(resI)) resBytes := res.Bytes() @@ -331,7 +64,7 @@ func (m *MessageCounter) BlockSize() int { } func NewMessageCounter(startState, step int) hash.Hash { - transcript := &MessageCounter{startState: uint64(startState), state: uint64(startState), step: uint64(step)} + transcript := &MessageCounter{startState: int64(startState), state: int64(startState), step: int64(step)} return transcript } From 0adf1286f696eb0d661a01529bdeb9eaae9c9a9d Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Fri, 10 Feb 2023 17:17:34 -0500 Subject: [PATCH 2/3] fix: _wip gkr tests to use const hash --- .../gkr/template/gkr.test.vectors.gen.go.tmpl | 7 +---- .../gkr/template/gkr.test.vectors.go.tmpl | 2 +- internal/generator/gkr/test_vectors/main.go | 29 +++++++++---------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl b/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl index 4f8fe15245..5ac02d1b9e 100644 --- a/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl @@ -13,12 +13,7 @@ import ( ) func main() { - if err := func() error { - if err := GenerateVectors(); err != nil { - return err - } - return test_vector_utils.SaveUsedHashEntries() - }(); err != nil { + if err := GenerateVectors(); err != nil { fmt.Println(err.Error()) os.Exit(-1) } diff --git a/internal/generator/gkr/template/gkr.test.vectors.go.tmpl b/internal/generator/gkr/template/gkr.test.vectors.go.tmpl index 253b90910f..7706024ff1 100644 --- a/internal/generator/gkr/template/gkr.test.vectors.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.vectors.go.tmpl @@ -126,7 +126,7 @@ func unmarshalProof(printable PrintableProof) ({{$Proof}}, error) { type TestCase struct { Circuit {{$Circuit}} - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof {{$Proof}} FullAssignment {{$WireAssignment}} InOutAssignment {{$WireAssignment}} diff --git a/internal/generator/gkr/test_vectors/main.go b/internal/generator/gkr/test_vectors/main.go index 1515bef8dd..22743f57ab 100644 --- a/internal/generator/gkr/test_vectors/main.go +++ b/internal/generator/gkr/test_vectors/main.go @@ -25,6 +25,7 @@ import ( "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/polynomial" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/sumcheck" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/test_vector_utils" + "hash" "os" "path/filepath" "reflect" @@ -76,8 +77,10 @@ func run(absPath string) error { return err } + transcriptSetting := fiatshamir.WithHash(testCase.Hash) + var proof gkr.Proof - proof, err = gkr.Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err = gkr.Prove(testCase.Circuit, testCase.FullAssignment, transcriptSetting) if err != nil { return err } @@ -99,7 +102,7 @@ func run(absPath string) error { return err } - err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, testCase.transcriptSetting()) + err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, transcriptSetting) if err != nil { return err } @@ -109,7 +112,7 @@ func run(absPath string) error { return err } - err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, testCase.transcriptSetting([]byte{0, 1})) + err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) if err == nil { return fmt.Errorf("bad proof accepted") } @@ -250,7 +253,7 @@ func unmarshalProof(printable PrintableProof) (gkr.Proof, error) { type TestCase struct { Circuit gkr.Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof gkr.Proof FullAssignment gkr.WireAssignment InOutAssignment gkr.WireAssignment @@ -258,11 +261,11 @@ type TestCase struct { } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -288,8 +291,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof gkr.Proof @@ -359,10 +362,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...small_rational.SmallRational) (result small_rational.SmallRational) { From 1f32c1f1fc2d94f90148c8a05eafdf9b19fcecfe Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Fri, 10 Feb 2023 17:46:20 -0500 Subject: [PATCH 3/3] refactor: gkr using const hash instead of map hash --- ecc/bls12-377/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bls12-378/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bls12-381/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bls24-315/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bls24-317/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bn254/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- .../test_vector_utils_test.go | 23 ---- ecc/bw6-633/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bw6-756/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- ecc/bw6-761/fr/gkr/gkr_test.go | 27 +++-- .../fr/test_vector_utils/test_vector_utils.go | 2 +- .../generator/gkr/template/gkr.test.go.tmpl | 7 +- .../gkr/template/gkr.test.vectors.gen.go.tmpl | 9 +- .../gkr/template/gkr.test.vectors.go.tmpl | 18 ++-- internal/generator/gkr/test_vectors/main.go | 7 +- .../mimc_five_levels_two_instances._json | 2 +- .../gkr/test_vectors/resources/hash.json | 100 ------------------ .../single_identity_gate_two_instances.json | 11 +- ...nput_two_identity_gates_two_instances.json | 21 ++-- .../single_input_two_outs_two_instances.json | 23 ++-- .../single_mimc_gate_four_instances.json | 41 +++---- .../single_mimc_gate_two_instances.json | 23 ++-- .../single_mul_gate_two_instances.json | 15 +-- ...s_composed_single_input_two_instances.json | 13 ++- ...uts_select-input-3_gate_two_instances.json | 13 ++- .../sumcheck/test_vectors/vectors.json | 14 ++- .../test_vector_utils/test_vector_utils.go | 2 +- .../test_vector_utils_test.go | 76 ------------- .../template/test_vector_utils.go.tmpl | 2 +- 37 files changed, 240 insertions(+), 441 deletions(-) delete mode 100644 internal/generator/gkr/test_vectors/resources/hash.json delete mode 100644 internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils_test.go diff --git a/ecc/bls12-377/fr/gkr/gkr_test.go b/ecc/bls12-377/fr/gkr/gkr_test.go index 445a7dce41..5e0721ebf0 100644 --- a/ecc/bls12-377/fr/gkr/gkr_test.go +++ b/ecc/bls12-377/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go index afd66b06c1..b189399a6a 100644 --- a/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bls12-378/fr/gkr/gkr_test.go b/ecc/bls12-378/fr/gkr/gkr_test.go index 482026e48c..278a8f92f0 100644 --- a/ecc/bls12-378/fr/gkr/gkr_test.go +++ b/ecc/bls12-378/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go index 3d1c78f6fb..1ab9dbf18f 100644 --- a/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bls12-381/fr/gkr/gkr_test.go b/ecc/bls12-381/fr/gkr/gkr_test.go index 39d399cb56..091293b58d 100644 --- a/ecc/bls12-381/fr/gkr/gkr_test.go +++ b/ecc/bls12-381/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go b/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go index 0872c428c5..5565a78d3c 100644 --- a/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bls24-315/fr/gkr/gkr_test.go b/ecc/bls24-315/fr/gkr/gkr_test.go index cb48506554..32fe102615 100644 --- a/ecc/bls24-315/fr/gkr/gkr_test.go +++ b/ecc/bls24-315/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go b/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go index 3d4db2d0a1..a6be6d45e6 100644 --- a/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bls24-317/fr/gkr/gkr_test.go b/ecc/bls24-317/fr/gkr/gkr_test.go index 4449c65a09..de72508f9c 100644 --- a/ecc/bls24-317/fr/gkr/gkr_test.go +++ b/ecc/bls24-317/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go b/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go index d5ede49eda..2981462aa6 100644 --- a/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bn254/fr/gkr/gkr_test.go b/ecc/bn254/fr/gkr/gkr_test.go index 8b6735cd1b..6be5844fe4 100644 --- a/ecc/bn254/fr/gkr/gkr_test.go +++ b/ecc/bn254/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bn254/fr/test_vector_utils/test_vector_utils.go b/ecc/bn254/fr/test_vector_utils/test_vector_utils.go index 6dccd71b58..073fe7a0e0 100644 --- a/ecc/bn254/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bn254/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bn254/fr/test_vector_utils/test_vector_utils_test.go b/ecc/bn254/fr/test_vector_utils/test_vector_utils_test.go index 9ecbab36bd..dc14edf510 100644 --- a/ecc/bn254/fr/test_vector_utils/test_vector_utils_test.go +++ b/ecc/bn254/fr/test_vector_utils/test_vector_utils_test.go @@ -1,35 +1,12 @@ package test_vector_utils import ( - "github.com/consensys/gnark-crypto/ecc/bn254/fr" fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational/test_vector_utils" "github.com/stretchr/testify/assert" - "strconv" "testing" ) -func TestTranscript(t *testing.T) { - - mp, err := CreateElementMap(map[string]interface{}{ - strconv.Itoa('0'): 2, - "3,2": 5, - }) - assert.NoError(t, err) - - hsh := MapHash{Map: &mp} - transcript := fiatshamir.NewTranscript(&hsh, "0", "1") - bytes := ToElement(3).Bytes() - err = transcript.Bind("0", bytes[:]) - assert.NoError(t, err) - var cBytes []byte - cBytes, err = transcript.ComputeChallenge("0") - assert.NoError(t, err) - var res fr.Element - res.SetBytes(cBytes) - assert.True(t, ToElement(5).Equal(&res)) -} - func TestCounterTranscriptInequality(t *testing.T) { const challengeName = "fC.0" t1 := fiatshamir.NewTranscript(test_vector_utils.NewMessageCounter(1, 1), challengeName) diff --git a/ecc/bw6-633/fr/gkr/gkr_test.go b/ecc/bw6-633/fr/gkr/gkr_test.go index db6d243d9a..3271cdc072 100644 --- a/ecc/bw6-633/fr/gkr/gkr_test.go +++ b/ecc/bw6-633/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go index 893cdabc97..bdf2b832f1 100644 --- a/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bw6-756/fr/gkr/gkr_test.go b/ecc/bw6-756/fr/gkr/gkr_test.go index c80a7f3e7e..b11a3e48d6 100644 --- a/ecc/bw6-756/fr/gkr/gkr_test.go +++ b/ecc/bw6-756/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go index c805ba53e3..1ff699ea56 100644 --- a/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/ecc/bw6-761/fr/gkr/gkr_test.go b/ecc/bw6-761/fr/gkr/gkr_test.go index 23b78cd80d..5477887c2c 100644 --- a/ecc/bw6-761/fr/gkr/gkr_test.go +++ b/ecc/bw6-761/fr/gkr/gkr_test.go @@ -27,6 +27,7 @@ import ( fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" + "hash" "os" "path/filepath" "reflect" @@ -363,7 +364,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -373,11 +374,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } @@ -609,18 +610,18 @@ func unmarshalProof(printable PrintableProof) (Proof, error) { type TestCase struct { Circuit Circuit - Hash *test_vector_utils.ElementMap + Hash hash.Hash Proof Proof FullAssignment WireAssignment InOutAssignment WireAssignment } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -646,8 +647,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof Proof @@ -716,10 +717,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...fr.Element) (result fr.Element) { diff --git a/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go b/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go index 794d3588cb..7405907e57 100644 --- a/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go +++ b/ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/internal/generator/gkr/template/gkr.test.go.tmpl b/internal/generator/gkr/template/gkr.test.go.tmpl index 2b46751416..088a7246c6 100644 --- a/internal/generator/gkr/template/gkr.test.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.go.tmpl @@ -9,6 +9,7 @@ import ( "github.com/consensys/gnark-crypto/utils" "github.com/stretchr/testify/assert" "fmt" + "hash" "os" "strconv" "testing" @@ -353,7 +354,7 @@ func generateTestProver(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - proof, err := Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err := Prove(testCase.Circuit, testCase.FullAssignment, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err) assert.NoError(t, proofEquals(testCase.Proof, proof)) } @@ -363,11 +364,11 @@ func generateTestVerifier(path string) func(t *testing.T) { return func(t *testing.T) { testCase, err := newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, testCase.transcriptSetting()) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(testCase.Hash)) assert.NoError(t, err, "proof rejected") testCase, err = newTestCase(path) assert.NoError(t, err) - err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(&test_vector_utils.MapHash{Map: testCase.Hash}, []byte{1})) + err = Verify(testCase.Circuit, testCase.InOutAssignment, testCase.Proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) assert.NotNil(t, err, "bad proof accepted") } } diff --git a/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl b/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl index 5ac02d1b9e..baf031567a 100644 --- a/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.vectors.gen.go.tmpl @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "reflect" + "hash" ) func main() { @@ -53,8 +54,10 @@ func run(absPath string) error { return err } + transcriptSetting := fiatshamir.WithHash(testCase.Hash) + var proof gkr.Proof - proof, err = gkr.Prove(testCase.Circuit, testCase.FullAssignment, testCase.transcriptSetting()) + proof, err = gkr.Prove(testCase.Circuit, testCase.FullAssignment, transcriptSetting) if err != nil { return err } @@ -76,7 +79,7 @@ func run(absPath string) error { return err } - err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, testCase.transcriptSetting()) + err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, transcriptSetting) if err != nil { return err } @@ -86,7 +89,7 @@ func run(absPath string) error { return err } - err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, testCase.transcriptSetting([]byte{0, 1})) + err = gkr.Verify(testCase.Circuit, testCase.InOutAssignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(2, 0))) if err == nil { return fmt.Errorf("bad proof accepted") } diff --git a/internal/generator/gkr/template/gkr.test.vectors.go.tmpl b/internal/generator/gkr/template/gkr.test.vectors.go.tmpl index 7706024ff1..60fb16b523 100644 --- a/internal/generator/gkr/template/gkr.test.vectors.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.vectors.go.tmpl @@ -134,11 +134,11 @@ type TestCase struct { } type TestCaseInfo struct { - Hash string `json:"hash"` - Circuit string `json:"circuit"` - Input [][]interface{} `json:"input"` - Output [][]interface{} `json:"output"` - Proof PrintableProof `json:"proof"` + Hash test_vector_utils.HashDescription `json:"hash"` + Circuit string `json:"circuit"` + Input [][]interface{} `json:"input"` + Output [][]interface{} `json:"output"` + Proof PrintableProof `json:"proof"` } var testCases = make(map[string]*TestCase) @@ -164,8 +164,8 @@ func newTestCase(path string) (*TestCase, error) { if circuit, err = getCircuit(filepath.Join(dir, info.Circuit)); err != nil { return nil, err } - var _hash *test_vector_utils.ElementMap - if _hash, err = test_vector_utils.ElementMapFromFile(filepath.Join(dir, info.Hash)); err != nil { + var _hash hash.Hash + if _hash, err = test_vector_utils.HashFromDescription(info.Hash); err != nil { return nil, err } var proof {{$Proof}} @@ -241,10 +241,6 @@ func newTestCase(path string) (*TestCase, error) { return tCase, nil } -func (c *TestCase) transcriptSetting(initialChallenge ...[]byte) fiatshamir.Settings { - return fiatshamir.WithHash(&test_vector_utils.MapHash{Map: c.Hash}, initialChallenge...) -} - type mulGate struct{} func (g mulGate) Evaluate(element ...{{.ElementType}}) (result {{.ElementType}}) { diff --git a/internal/generator/gkr/test_vectors/main.go b/internal/generator/gkr/test_vectors/main.go index 22743f57ab..3822aa3de9 100644 --- a/internal/generator/gkr/test_vectors/main.go +++ b/internal/generator/gkr/test_vectors/main.go @@ -32,12 +32,7 @@ import ( ) func main() { - if err := func() error { - if err := GenerateVectors(); err != nil { - return err - } - return test_vector_utils.SaveUsedHashEntries() - }(); err != nil { + if err := GenerateVectors(); err != nil { fmt.Println(err.Error()) os.Exit(-1) } diff --git a/internal/generator/gkr/test_vectors/mimc_five_levels_two_instances._json b/internal/generator/gkr/test_vectors/mimc_five_levels_two_instances._json index 94f45f4a92..446d23fdb2 100644 --- a/internal/generator/gkr/test_vectors/mimc_five_levels_two_instances._json +++ b/internal/generator/gkr/test_vectors/mimc_five_levels_two_instances._json @@ -1,5 +1,5 @@ { - "hash": "resources/hash.json", + "hash": {"type": "const", "val": -1}, "circuit": "resources/mimc_five_levels.json", "input": [[1, 3], [1, 3], [1, 3], [1, 3], [1, 3], [1, 3]], "output": [[4, 3]], diff --git a/internal/generator/gkr/test_vectors/resources/hash.json b/internal/generator/gkr/test_vectors/resources/hash.json deleted file mode 100644 index 9a96e4aaa5..0000000000 --- a/internal/generator/gkr/test_vectors/resources/hash.json +++ /dev/null @@ -1,100 +0,0 @@ -{ - "-1145901219840000000,5":1, - "-364381509670404096,1":5, - "-95630089550561280,-5":1, - "-19168501451194368,5":-5, - "-2559554567012352,-1":5, - "-2559554567012352,1":5, - "-172523520000000,-1":1, - "-172523520000000,2":-1, - "-2861958168576,-2":-1, - "-2861958168576,0":2, - "-191032962484,-4":4, - "-191032962484,4":-5, - "-72481259520,4":-4, - "-72481259520,5":4, - "-24034694442,0":4, - "-24034694442,3":5, - "-6664736128,-5":0, - "-6664736128,1":3, - "-1440000120,-5":-5, - "-1440000120,2":1, - "-520000000,0":5, - "-408944640,-5":0, - "-408944640,4":-2, - "-215233605,-4":0, - "-215233605,1":0, - "-213909504,-3":-5, - "-213909504,-1":2, - "-79691776,-4":-4, - "-79691776,-1":1, - "-25529833,-1":-4, - "-25529833,4":-1, - "-16796110,-5":-3, - "-16796110,-1":-1, - "-6718464,-4":-1, - "-6718464,3":4, - "-1328125,1":-4, - "-1328125,2":3, - "-292992,-2":-1, - "-292992,1":-5, - "-163840,1":2, - "-163840,2":1, - "-6561,-3":1, - "-6561,1":2, - "-272,0":0, - "-128,-3":5, - "-90,-3":0, - "-85,0":5, - "-85,1":0, - "-80,-3":1, - "-80,-2":0, - "-40,-4":-5, - "-40,-1":3, - "-40,4":-4, - "-33,-2":-2, - "-33,0":-3, - "-30,-2":-5, - "-30,0":-3, - "-27,-3":-2, - "-27,1":-3, - "-20,-4":1, - "-20,-2":3, - "-12,-2":-3, - "-9,-5":-1, - "-9,-4":-2, - "-9,-3":4, - "-9,-2":-4, - "-9,1":-4, - "-6,-3":-2, - "-6,1":0, - "-5,-4":-2, - "-5,-2":-1, - "-5,4":-5, - "-4,-4":-3, - "-4,-2":-4, - "-4,-1":-3, - "-3,-4":1, - "-3,-3":-3, - "-3,-2":-2, - "-3,-1":-5, - "-3,1":2, - "-2,-2":-2, - "-2,5":0, - "0,-3":-2, - "0,-2":-4, - "0,2":5, - "1,-3":-4, - "1,-2":1, - "1,5":-3, - "3,-3":-2, - "4,4":4, - "5,-2":-2, - "1715678768":-3, - "1715678769":-1, - "33548498023443810":-3, - "8588415549364514352":5, - "8588697024341225008":-2, - "8588978499317935664":-4, - "8588978499317935665":4 -} \ No newline at end of file diff --git a/internal/generator/gkr/test_vectors/single_identity_gate_two_instances.json b/internal/generator/gkr/test_vectors/single_identity_gate_two_instances.json index 01cf5b7c46..ce326d0a63 100644 --- a/internal/generator/gkr/test_vectors/single_identity_gate_two_instances.json +++ b/internal/generator/gkr/test_vectors/single_identity_gate_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_identity_gate.json", "input": [ [ @@ -20,12 +23,12 @@ }, { "finalEvalProof": [ - 3 + 5 ], "partialSumPolys": [ [ - -9, - -20 + -3, + -8 ] ] } diff --git a/internal/generator/gkr/test_vectors/single_input_two_identity_gates_two_instances.json b/internal/generator/gkr/test_vectors/single_input_two_identity_gates_two_instances.json index d926fc2b66..2c95f044f2 100644 --- a/internal/generator/gkr/test_vectors/single_input_two_identity_gates_two_instances.json +++ b/internal/generator/gkr/test_vectors/single_input_two_identity_gates_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_input_two_identity_gates.json", "input": [ [ @@ -22,30 +25,30 @@ "finalEvalProof": [], "partialSumPolys": [ [ - -33, - -128 + 0, + 0 ] ] }, { "finalEvalProof": [ - 5 + 1 ], "partialSumPolys": [ [ - -9, - -40 + -3, + -16 ] ] }, { "finalEvalProof": [ - -3 + 1 ], "partialSumPolys": [ [ - -9, - -40 + -3, + -16 ] ] } diff --git a/internal/generator/gkr/test_vectors/single_input_two_outs_two_instances.json b/internal/generator/gkr/test_vectors/single_input_two_outs_two_instances.json index 39ad0f0ac5..d348303d0e 100644 --- a/internal/generator/gkr/test_vectors/single_input_two_outs_two_instances.json +++ b/internal/generator/gkr/test_vectors/single_input_two_outs_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_input_two_outs.json", "input": [ [ @@ -22,31 +25,31 @@ "finalEvalProof": [], "partialSumPolys": [ [ - -6, - -33 + 0, + 0 ] ] }, { "finalEvalProof": [ - 1 + 0 ], "partialSumPolys": [ [ - -12, - -90, - -272 + -4, + -36, + -112 ] ] }, { "finalEvalProof": [ - -2 + 0 ], "partialSumPolys": [ [ - -6, - -30 + -2, + -12 ] ] } diff --git a/internal/generator/gkr/test_vectors/single_mimc_gate_four_instances.json b/internal/generator/gkr/test_vectors/single_mimc_gate_four_instances.json index 8c50ed09a2..525459ecb1 100644 --- a/internal/generator/gkr/test_vectors/single_mimc_gate_four_instances.json +++ b/internal/generator/gkr/test_vectors/single_mimc_gate_four_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_mimc_gate.json", "input": [ [ @@ -34,29 +37,29 @@ }, { "finalEvalProof": [ - 1, - 7 + -1, + -3 ], "partialSumPolys": [ [ - -292992, - -16796110, - "-213909504", - "-1440000120", - "-6664736128", - "-24034694442", - "-72481259520", - "-191032962484" + -32640, + -2239484, + -29360128, + "-200000010", + "-931628672", + "-3373267120", + "-10200858624", + "-26939400158" ], [ - "-408944640", - "-2861958168576", - "-172523520000000", - "-2559554567012352", - "-19168501451194368", - "-95630089550561280", - "-364381509670404096", - "-1145901219840000000" + -81920, + -41943040, + "-1254113280", + "-13421772800", + "-83200000000", + "-366917713920", + "-1281828208640", + "-3779571220480" ] ] } diff --git a/internal/generator/gkr/test_vectors/single_mimc_gate_two_instances.json b/internal/generator/gkr/test_vectors/single_mimc_gate_two_instances.json index 488c9aa24e..7fa23ce4b1 100644 --- a/internal/generator/gkr/test_vectors/single_mimc_gate_two_instances.json +++ b/internal/generator/gkr/test_vectors/single_mimc_gate_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_mimc_gate.json", "input": [ [ @@ -29,18 +32,18 @@ { "finalEvalProof": [ 1, - 6 + 0 ], "partialSumPolys": [ [ - -6561, - -163840, - -1328125, - -6718464, - -25529833, - -79691776, - "-215233605", - "-520000000" + -2187, + -65536, + -546875, + -2799360, + -10706059, + -33554432, + -90876411, + "-220000000" ] ] } diff --git a/internal/generator/gkr/test_vectors/single_mul_gate_two_instances.json b/internal/generator/gkr/test_vectors/single_mul_gate_two_instances.json index bc01efd879..75c1d59c3d 100644 --- a/internal/generator/gkr/test_vectors/single_mul_gate_two_instances.json +++ b/internal/generator/gkr/test_vectors/single_mul_gate_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/single_mul_gate.json", "input": [ [ @@ -28,14 +31,14 @@ }, { "finalEvalProof": [ - 4, - 2 + 5, + 1 ], "partialSumPolys": [ [ - -27, - -80, - -85 + -9, + -32, + -35 ] ] } diff --git a/internal/generator/gkr/test_vectors/two_identity_gates_composed_single_input_two_instances.json b/internal/generator/gkr/test_vectors/two_identity_gates_composed_single_input_two_instances.json index 4daede5890..10e5f1ff3c 100644 --- a/internal/generator/gkr/test_vectors/two_identity_gates_composed_single_input_two_instances.json +++ b/internal/generator/gkr/test_vectors/two_identity_gates_composed_single_input_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/two_identity_gates_composed_single_input.json", "input": [ [ @@ -20,22 +23,22 @@ }, { "finalEvalProof": [ - 6 + 3 ], "partialSumPolys": [ [ - 5, + -1, 0 ] ] }, { "finalEvalProof": [ - -3 + 3 ], "partialSumPolys": [ [ - -3, + -1, 0 ] ] diff --git a/internal/generator/gkr/test_vectors/two_inputs_select-input-3_gate_two_instances.json b/internal/generator/gkr/test_vectors/two_inputs_select-input-3_gate_two_instances.json index ed4fc4f181..19e127df71 100644 --- a/internal/generator/gkr/test_vectors/two_inputs_select-input-3_gate_two_instances.json +++ b/internal/generator/gkr/test_vectors/two_inputs_select-input-3_gate_two_instances.json @@ -1,5 +1,8 @@ { - "hash": "resources/hash.json", + "hash": { + "type": "const", + "val": -1 + }, "circuit": "resources/two_inputs_select-input-3_gate.json", "input": [ [ @@ -28,13 +31,13 @@ }, { "finalEvalProof": [ - -5, - -3 + -1, + 1 ], "partialSumPolys": [ [ - -9, - -40 + -3, + -16 ] ] } diff --git a/internal/generator/sumcheck/test_vectors/vectors.json b/internal/generator/sumcheck/test_vectors/vectors.json index 1550c82ba2..64b8e3fb2d 100644 --- a/internal/generator/sumcheck/test_vectors/vectors.json +++ b/internal/generator/sumcheck/test_vectors/vectors.json @@ -1,7 +1,10 @@ { "linear_univariate_single_claim": { "type": "multilin", - "hash": {"type": "const", "val": -1}, + "hash": { + "type": "const", + "val": -1 + }, "values": [ 1, 3 @@ -19,7 +22,10 @@ }, "trilinear_single_claim": { "type": "multilin", - "hash": {"type": "const", "val": -1}, + "hash": { + "type": "const", + "val": -1 + }, "values": [ 1, 2, @@ -37,10 +43,10 @@ 26 ], [ - -9 + -1 ], [ - -14 + -4 ] ], "finalEvalProof": {} diff --git a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go b/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go index 866b301871..e93d43b666 100644 --- a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go +++ b/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils.go @@ -38,7 +38,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter{startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type) diff --git a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils_test.go b/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils_test.go deleted file mode 100644 index 9fb77e99a6..0000000000 --- a/internal/generator/test_vector_utils/small_rational/test_vector_utils/test_vector_utils_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package test_vector_utils - -import ( - fiatshamir "github.com/consensys/gnark-crypto/fiat-shamir" - "github.com/consensys/gnark-crypto/internal/generator/test_vector_utils/small_rational" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestHashNewElementSaved(t *testing.T) { - var hash ElementMap - - var fortyFour small_rational.SmallRational - fortyFour.SetInt64(44) - - expected, err := hash.FindPair(&fortyFour, nil) - assert.NoError(t, err) - for i := 0; i < 10; i++ { - seen, err := hash.FindPair(&fortyFour, nil) - assert.NoError(t, err) - if !expected.Equal(&seen) { - t.Errorf("expected %s saw %s", expected.String(), seen.String()) - } - } -} - -func TestHashConsistency(t *testing.T) { - var one small_rational.SmallRational - var mp ElementMap - one.SetOne() - bytes := one.Bytes() - - t1 := fiatshamir.NewTranscript(&MapHash{Map: &mp}, "0") - assert.NoError(t, t1.Bind("0", bytes[:])) - c1, err := t1.ComputeChallenge("0") - assert.NoError(t, err) - - t2 := fiatshamir.NewTranscript(&MapHash{Map: &mp}, "0") - assert.NoError(t, t2.Bind("0", bytes[:])) - c2, err := t2.ComputeChallenge("0") - assert.NoError(t, err) - - assert.Equal(t, c1, c2) -} - -func TestSaveHash(t *testing.T) { - - var one, two, three small_rational.SmallRational - one.SetInt64(1) - two.SetInt64(2) - three.SetInt64(3) - - hash := ElementMap{{ - key1: one, - key2: small_rational.SmallRational{}, - key2Present: false, - value: two, - used: true, - }, { - key1: one, - key2: one, - key2Present: true, - value: three, - used: true, - }, { - key1: two, - key2: one, - key2Present: true, - value: two, - used: false, - }} - - serialized, err := hash.serializedUsedEntries() - assert.NoError(t, err) - assert.Equal(t, "{\n\t\"1\":2,\n\t\"1,1\":3\n}", serialized) -} diff --git a/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl b/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl index 96561c0a5e..4ce3fa20ba 100644 --- a/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl +++ b/internal/generator/test_vector_utils/template/test_vector_utils.go.tmpl @@ -21,7 +21,7 @@ func HashFromDescription(d HashDescription) (hash.Hash, error) { if _type, ok := d["type"]; ok { switch _type { case "const": - startState := int64(d["val"].(int)) + startState := int64(d["val"].(float64)) return &MessageCounter {startState: startState, step: 0, state: startState}, nil default: return nil, fmt.Errorf("unknown fake hash type \"%s\"", _type)