Skip to content

Conversation

@aybehrouz
Copy link
Contributor

@aybehrouz aybehrouz commented Feb 22, 2023

This is a simple partition selector. It selects the first count number of the input array, keeps them and zeros all other entries or vice versa. By cascading these gadgets a slice can be selected. It works using a step function as a mask.

I just had this idea and tried to implement it. I'm not sure if my implementation is good or if this is useful. Currently some tests are failing for a specific curve. I have no idea why, and I don't know how I can fix that!

@aybehrouz aybehrouz marked this pull request as draft February 22, 2023 22:05
@aybehrouz
Copy link
Contributor Author

tests that fail:

=== RUN   TestPartition/bls24_317/plonkFRI#03
=== PAUSE TestPartition/bls24_317/plonkFRI#03
=== CONT  TestPartition/bls24_317/plonkFRI#03
    assert.go:517: 
        	Error Trace:	/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:517
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:227
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:269
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:74
        	Error:      	did not error (but should have) plonkFRI(bls24_317)
        	            	witness:{"Pivot":6,"In":[10,20,30,40,50,60],"Left":[10,20,30,40,50,60],"Right":[0,0,0,0,0,0]}
        	Test:       	TestPartition/bls24_317/plonkFRI#03
    --- FAIL: TestPartition/bls24_317/plonkFRI#03 (0.63s)
=== RUN   TestPartition/bls24_317/plonkFRI#04
=== PAUSE TestPartition/bls24_317/plonkFRI#04
=== CONT  TestPartition/bls24_317/plonkFRI#04
    assert.go:517: 
        	Error Trace:	/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:517
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:227
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:269
        	            				/home/aybehrouz/GolandProjects/gnark/std/selector/assert.go:74
        	Error:      	did not error (but should have) plonkFRI(bls24_317)
        	            	witness:{"Pivot":0,"In":[10,20,30,40,50,60],"Left":[0,0,0,0,0,0],"Right":[10,20,30,40,50,60]}
        	Test:       	TestPartition/bls24_317/plonkFRI#04
    --- FAIL: TestPartition/bls24_317/plonkFRI#04 (1.02s)
    ```

We want to handle the case when `Partition` needs to select the whole input.
@aybehrouz aybehrouz marked this pull request as ready for review February 23, 2023 20:56
@aybehrouz aybehrouz marked this pull request as draft February 24, 2023 09:18
It seems that handling pivot points at the end of the array was not a good idea. That will introduce the overhead of one more nonlinear constraint, while in many cased it is not needed.
@aybehrouz aybehrouz marked this pull request as ready for review February 24, 2023 21:08
@CLAassistant
Copy link

CLAassistant commented Feb 27, 2023

CLA assistant check
All committers have signed the CLA.

@aybehrouz
Copy link
Contributor Author

I'll close this for now. Later it could be reopened, if needed.

@aybehrouz aybehrouz closed this Mar 2, 2023
@ivokub
Copy link
Collaborator

ivokub commented Mar 3, 2023

Sorry for not reviewing the PR in time. We definitely appreciate the contribution. Skimming through the implementation it seems to be of high quality and I wouldn't mind merging. I was just postponing the review after other tasks :)

From my side, it can stay open because imo otherwise the PR will be buried into other closed (and merged) PRs.

@aybehrouz
Copy link
Contributor Author

aybehrouz commented Mar 3, 2023

No worries, actually I realized that you guys were busy and because it wasn't something high priority, I didn't want to keep the PR list crowded, so I closed it to reopen later.

@aybehrouz aybehrouz reopened this Mar 3, 2023
Also removes a todo about a failing test, since that test is not failing anymore.
Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I think that the gadget is useful and implementation seems very good.

I have some few inline comments though. I think error should be handled but I'm not sure if would be good to add new methods.

@aybehrouz aybehrouz requested a review from ivokub March 8, 2023 13:24
Signed-off-by: aybehrouz <[email protected]>
Apparently, Go runs all the init functions of a package when the package is imported. So, there is no point in having multiple init functions in different source files.
@ivokub
Copy link
Collaborator

ivokub commented Mar 13, 2023

Suggested edit:

diff --git a/std/hints.go b/std/hints.go
index a5a2a9bc..a570d762 100644
--- a/std/hints.go
+++ b/std/hints.go
@@ -31,6 +31,6 @@ func registerHints() {
 	solver.RegisterHint(bits.NNAF)
 	solver.RegisterHint(bits.IthBit)
 	solver.RegisterHint(bits.NBits)
-	selector.RegisterAllHints()
+	solver.RegisterHint(selector.GetHints()...)
 	solver.RegisterHint(emulated.GetHints()...)
 }
diff --git a/std/selector/multiplexer.go b/std/selector/multiplexer.go
index 25a8b149..c9611a0f 100644
--- a/std/selector/multiplexer.go
+++ b/std/selector/multiplexer.go
@@ -14,23 +14,22 @@ package selector
 
 import (
 	"fmt"
-	"github.com/consensys/gnark/constraint/solver"
 	"math/big"
 
+	"github.com/consensys/gnark/constraint/solver"
+
 	"github.com/consensys/gnark/frontend"
 )
 
 func init() {
 	// register hints
-	RegisterAllHints()
+	solver.RegisterHint(GetHints()...)
 }
 
-// RegisterAllHints registers all the hint functions that are used by this package by calling
-// solver.RegisterHint.
-func RegisterAllHints() {
-	solver.RegisterHint(stepOutput)
-	solver.RegisterHint(muxIndicators)
-	solver.RegisterHint(mapIndicators)
+// GetHints returns all hint functions used in this package. This method is
+// useful for registering all hints in the solver.
+func GetHints() []solver.Hint {
+	return []solver.Hint{stepOutput, muxIndicators, mapIndicators}
 }
 
 // Map is a key value associative array: the output will be values[i] such that keys[i] == queryKey. If keys does not

Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes wrt. hints. See also the comment vs Slice/Partition.

@aybehrouz
Copy link
Contributor Author

I agree with changes wrt. hints. Let's all packages have the same method for handling hints.

ivokub and others added 3 commits March 13, 2023 18:42
This commit adds a `Slice` function which is a wrapper for a cascade of two `Partition` calls. It also makes StepMask unexported.
@aybehrouz aybehrouz requested a review from ivokub March 13, 2023 19:24
Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think it almost looks perfect. How many constraints we add when we also want to handle pivot at beginning of end of the slice? If it is two contstraints per partition then I would definitely add (just add boundary cases).

Now we handle the case when pivotPosition is at the start or end of the input array.

Signed-off-by: aybehrouz <[email protected]>
@aybehrouz aybehrouz requested a review from ivokub March 14, 2023 12:34
Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Merge ahead!

@ivokub ivokub merged commit 8699eba into Consensys:develop Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants