Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/util/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,33 @@ func asInt64(x any) (int64, bool) {

return 0, false
}

// True errors if condition is false.
func True(t *testing.T, condition bool, msg ...any) {
if condition {
return
}

t.Errorf("condition is false")

if len(msg) != 0 {
t.Errorf(msg[0].(string), msg[1:]...)
}
Comment thread
Tabaie marked this conversation as resolved.

t.FailNow()
}

// False errors if condition is true.
func False(t *testing.T, condition bool, msg ...any) {
if !condition {
return
}

t.Errorf("condition is true")

if len(msg) != 0 {
t.Errorf(msg[0].(string), msg[1:]...)
}

t.FailNow()
}
23 changes: 16 additions & 7 deletions pkg/util/field/gf251/element.go

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

31 changes: 23 additions & 8 deletions pkg/util/field/gf251/element_test.go

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

23 changes: 16 additions & 7 deletions pkg/util/field/gf8209/element.go

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

31 changes: 23 additions & 8 deletions pkg/util/field/gf8209/element_test.go

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

2 changes: 2 additions & 0 deletions pkg/util/field/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type Element[Operand any] interface {
AddBytes([]byte) Operand // AddBytes adds Element to the given big-endian value, with a strict length requirement.
fmt.Stringer
Text(base int) string // Text returns the numerical value of x in the given base.
IsZero() bool
IsOne() bool
}
12 changes: 12 additions & 0 deletions pkg/util/field/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package field

import (
bls12_377 "github.com/consensys/go-corset/pkg/util/field/bls12-377"
"github.com/consensys/go-corset/pkg/util/field/koalabear"
)

func init() {
// make sure the interface is adhered to.
_ = Element[koalabear.Element](koalabear.Element{})
_ = Element[bls12_377.Element](bls12_377.Element{})
}
11 changes: 7 additions & 4 deletions pkg/util/field/internal/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ type fieldSpecs struct {

type fieldConfig struct {
fieldSpecs
RSq uint32
RSqModM uint32
RModM uint32
NegModulusInvModR uint32
}

Expand All @@ -83,11 +84,13 @@ func (f fieldSpecs) config() (*fieldConfig, error) {

var x big.Int

x.Mod(r, m).
Mul(&x, &x).
x.Mod(r, m)
specs.RModM = uint32(x.Uint64())

x.Mul(&x, &x).
Mod(&x, m)

specs.RSq = uint32(x.Uint64())
specs.RSqModM = uint32(x.Uint64())

x.ModInverse(m, r)
specs.NegModulusInvModR = uint32(R - x.Uint64())
Expand Down
23 changes: 16 additions & 7 deletions pkg/util/field/internal/generator/templates/element.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type Element [1]uint32 // defined as an array to prevent mistaken use of arithme
const (
r = 1 << 32 // register size
modulus = {{ .Modulus }}
rSq = {{ .RSq }} // r² (mod m)
rModM = {{ .RModM }}
rSqModM = {{ .RSqModM }} // r² (mod m)
negModulusInvModR = {{ .NegModulusInvModR }} // -modulus⁻¹ (mod r), used for Montgomery reduction
nbBytes = {{ $nbBytes }}
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func montgomeryReduce(x uint64) Element {

// AddUint32 x + y
func (x Element) AddUint32(y uint32) Element {
return x.Add(NewElement(y))
return x.Add(New(y))
}

// ToUint32 returns the numerical (non-Montgomery)
Expand All @@ -71,9 +72,9 @@ func (x Element) Mul(y Element) Element {
return montgomeryReduce(uint64(x[0]) * uint64(y[0]))
}

// NewElement returns an element of the field f corresponding to the natural number x.
func NewElement(x uint32) Element {
return Element{uint32(uint64(x) << 32 % modulus)}
// New returns an element of the field f corresponding to the natural number x.
func New(x uint32) Element {
return Element{x}.Mul(Element{rSqModM})
}

// Cmp compares the numerical values of x and y.
Expand Down Expand Up @@ -107,7 +108,7 @@ func (x Element) Inverse() Element {

var c Element
// Since x actually contains x.R, we have to multiply the result by R² to get x⁻¹R⁻¹R² = x⁻¹R.
b := Element{rSq}
b := Element{rSqModM}

for (u != 1) && (v != 1) {
for u%2 == 0 {
Expand Down Expand Up @@ -167,5 +168,13 @@ func (x Element) AddBytes(b []byte) Element {
for i := range nbBytes {
v |= uint32(b[i]) << ((nbBytes - 1 - i)*8)
}
return x.Add(NewElement(v))
return x.Add(New(v))
}

func (x Element) IsZero() bool {
return x[0] == 0
}

func (x Element) IsOne() bool {
return x[0] == rModM
}
Loading