Skip to content

Commit

Permalink
Refactor/gkr test vectors (#425)
Browse files Browse the repository at this point in the history
* feat: smallRational.BigInt()

* refactor: simpler ElementToInterface
  • Loading branch information
Tabaie authored Aug 18, 2023
1 parent 99cfa43 commit 190c5bf
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 116 deletions.
12 changes: 3 additions & 9 deletions ecc/bls12-377/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bls12-378/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bls12-381/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bls24-315/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bls24-317/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bn254/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bw6-633/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bw6-756/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions ecc/bw6-761/fr/test_vector_utils/test_vector_utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@
-32640,
-2239484,
-29360128,
"-200000010",
"-931628672",
"-3373267120",
"-10200858624",
"-26939400158"
-200000010,
-931628672,
-3373267120,
-10200858624,
-26939400158
],
[
-81920,
-41943040,
"-1254113280",
"-13421772800",
"-83200000000",
"-366917713920",
"-1281828208640",
"-3779571220480"
-1254113280,
-13421772800,
-83200000000,
-366917713920,
-1281828208640,
-3779571220480
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
-10706059,
-33554432,
-90876411,
"-220000000"
-220000000
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (z *SmallRational) simplify() {
}
}

if bigDivides(&den, &num) {
num.Div(&num, &den)
den.SetInt64(1)
}

z.numerator = num
z.denominator = den

Expand Down Expand Up @@ -371,8 +376,19 @@ func bytesToBigIntSigned(src []byte) big.Int {
return res
}

func (z *SmallRational) BigInt(*big.Int) *big.Int {
panic("not implemented")
// BigInt returns sets dst to the value of z if it is an integer.
// if z is not an integer, nil is returned.
// if the given dst is nil, the address of the numerator is returned.
// if the given dst is non-nil, it is returned.
func (z *SmallRational) BigInt(dst *big.Int) *big.Int {
if z.denominator.Cmp(big.NewInt(1)) != 0 {
return nil
}
if dst == nil {
return &z.numerator
}
dst.Set(&z.numerator)
return dst
}

func (z *SmallRational) SetBytes(b []byte) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package small_rational

import (
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)

func TestBigDivides(t *testing.T) {
assert.True(t, bigDivides(big.NewInt(-1), big.NewInt(4)))
assert.False(t, bigDivides(big.NewInt(-3), big.NewInt(4)))
}

func TestCmp(t *testing.T) {

cases := make([]SmallRational, 36)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import (
"{{.FieldPackagePath}}"
"{{.FieldPackagePath}}/polynomial"
"hash"
"strings"
"strconv"
"reflect"
"reflect"
{{if eq .ElementType "fr.Element"}}"strings"{{- end}}
)

func ToElement(i int64) *{{.ElementType}} {
Expand Down Expand Up @@ -185,15 +183,10 @@ func PolynomialSliceEquals(a []polynomial.Polynomial, b []polynomial.Polynomial)
}

func ElementToInterface(x *{{.ElementType}}) interface{} {
text := x.Text(10)
if len(text) < 10 && !strings.Contains(text, "/") {
if i, err := strconv.Atoi(text); err != nil {
panic(err.Error())
} else {
return i
}
if i := x.BigInt(nil); i != nil {
return i
}
return text
return x.Text(10)
}

func ElementSliceToInterfaceSlice(x interface{}) []interface{} {
Expand Down

0 comments on commit 190c5bf

Please sign in to comment.