Skip to content
Open
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
31 changes: 29 additions & 2 deletions std/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func BytesToNative(api frontend.API, b []uints.U8, opts ...Option) (frontend.Var
}
// check that the input was in range of the field modulus. Omit if cfg.allowOverflow is set.
if !cfg.allowOverflow {
assertBytesLeq(api, b, api.Compiler().Field())
assertBytesLt(api, b, api.Compiler().Field())
}
return res, nil
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func NativeToBytes(api frontend.API, v frontend.Variable, opts ...Option) ([]uin
// check if we don't care about the uniqueness (in case later when composing
// back to native element the check is done there).
if !cfg.allowOverflow {
assertBytesLeq(api, resU8, api.Compiler().Field())
assertBytesLt(api, resU8, api.Compiler().Field())
}
return resU8, nil
}
Expand Down Expand Up @@ -337,3 +337,30 @@ func assertBytesLeq(api frontend.API, b []uints.U8, bound *big.Int) error {
}
return nil
}

func assertBytesLt(api frontend.API, b []uints.U8, bound *big.Int) error {
bapi, err := uints.NewBytes(api)
if err != nil {
return err
}
mBytes := bound.Bytes()
if len(b) < len(mBytes) {
return nil
}
for i := 0; i < len(b)-len(mBytes); i++ {
api.AssertIsEqual(bapi.ValueUnchecked(b[i]), 0)
}
bb := b[len(b)-len(mBytes):]
rchecker := rangecheck.New(api)
var eq_i frontend.Variable = 1
for i := range mBytes {
diff := api.Sub(mBytes[i], bapi.Value(bb[i]))
nbBits := bits.Len8(mBytes[i])
rchecker.Check(api.Mul(eq_i, diff), nbBits)
isEq := api.IsZero(diff)
eq_i = api.Mul(eq_i, isEq)
}
// when lengths are comparable, disallow equality
api.AssertIsEqual(eq_i, 0)
return nil
}
14 changes: 14 additions & 0 deletions std/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,17 @@ func TestAssertBytesLeq(t *testing.T) {
tc(assert, []byte{253, 253, 253}, []byte{254, 252}, true)
tc(assert, []byte{253, 253, 253}, []byte{0, 254, 252}, true)
}

func TestBytesToNative_EqualModulus(t *testing.T) {
assert := test.NewAssert(t)
assert.Run(func(assert *test.Assert) {
m := fr_bn254.Modulus()
sbytes := m.Bytes()
// Expect invalid since strict < modulus must hold
assert.CheckCircuit(
&BytesToNativeCircuit{In: make([]uints.U8, len(sbytes))},
test.WithInvalidAssignment(&BytesToNativeCircuit{In: uints.NewU8Array(sbytes), Expected: big.NewInt(0)}),
test.WithCurves(ecc.BN254),
)
}, "equal-modulus")
}