gnark icon indicating copy to clipboard operation
gnark copied to clipboard

Can not passed XOR boolean check, even though I only use Binary Operations.

Open ZhAnGeek opened this issue 3 years ago • 2 comments

I have tried a lot of ways to do the xor process, however it wont work. Here's my code.

the first part is about change the variables to binary, for this function we use the api XOR/OR/AND function as an parameter f

func UInt64BitOperation(api *frontend.API, a, b frontend.Variable, f func(a, b frontend.Variable) frontend.Variable) frontend.Variable {
	cs := *api
	aBits := cs.ToBinary(a, 64)
	bBits := cs.ToBinary(b, 64)
	var resBits []frontend.Variable = make([]frontend.Variable, 64)

And this below which is straight thinking but won't work

	for i := 0; i < 64; i++ {
		resBits[i] = f(aBits[i], bBits[i])
	}

And I have tried another way.

	for i := 0; i < 64; i++ {
		aC := (*api).Select(aBits[i], One, Zero)
		bC := (*api).Select(bBits[i], One, Zero)
		resBits[i] = f(aC, bC)
	}

I just check the value in hint function, so weird that aC, bC is all 1, but the XOR result returns a overflow number. And the error keep telling constraint #13307 is not satisfied: [assertIsBoolean] -1 == (0|1)

I have seen there's saying boolean operations results will always be boolean, but it seems like not the situation. How should I do?thanks.

ZhAnGeek avatar Jul 18 '22 00:07 ZhAnGeek

func UInt64BitOperation(api frontend.API, a, b frontend.Variable, f func(a, b frontend.Variable) frontend.Variable) frontend.Variable {
	aBits := api.ToBinary(a, 64)
	bBits := api.ToBinary(b, 64)
	var resBits []frontend.Variable = make([]frontend.Variable, 64)
	for i := 0; i < 64; i++ {
		resBits[i] = f(aBits[i], bBits[i])
		api.AssertIsBoolean(aBits[i])
		api.AssertIsBoolean(bBits[i])
		api.AssertIsBoolean(f(aBits[i], bBits[i])) // _<------------ it only stucks here_
	}
	return api.FromBinary(resBits...)
}

ZhAnGeek avatar Jul 19 '22 08:07 ZhAnGeek

Hi @ZhAnGeek - can you give a full compilable circuit which causes the error? Particularly, what is the implementation of the function f you are using?

ivokub avatar Jul 19 '22 09:07 ivokub