-
Notifications
You must be signed in to change notification settings - Fork 519
feat: add a partition selector #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4a8b3be
feat: add a partition selector
aybehrouz 9de6bdb
Merge remote-tracking branch 'origin/develop' into feat/slicer
aybehrouz 5ef5b13
fix: handle pivot points at the end of input
aybehrouz 7608209
docs: add missing docs
aybehrouz bc54b6b
test: add more tests
aybehrouz ecdf54b
fix: reduce mask size
aybehrouz a3c1451
docs: fix some comments
aybehrouz ee2b579
Merge branch 'develop' into feat/slicer
aybehrouz 95d53f6
fix: use the new hint registration
aybehrouz 01ef749
fix: handle errors when calling hints
aybehrouz 1f8391b
refactor: make hints unexported
aybehrouz 267a614
docs: add a missing doc
aybehrouz 0a5d3cd
refactor: use per file hint registration
aybehrouz 6ea6117
refactor: use one `init` per package
aybehrouz 42d5369
refactor: add GetHints for getting all hints
ivokub a762c5d
chore: remove empty line in import
ivokub c64dec3
feat: add `Slice` function
aybehrouz 3caf3d1
feat: handle full array selection
aybehrouz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package selector_test | ||
|
|
||
| import "github.com/consensys/gnark/frontend" | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/consensys/gnark-crypto/ecc" | ||
| "github.com/consensys/gnark/backend/groth16" | ||
| "github.com/consensys/gnark/frontend/cs/r1cs" | ||
| "github.com/consensys/gnark/std/selector" | ||
| ) | ||
|
|
||
| // adderCircuit adds first Count number of its input array In. | ||
| type adderCircuit struct { | ||
| Count frontend.Variable | ||
| In [10]frontend.Variable | ||
| ExpectedSum frontend.Variable | ||
| } | ||
|
|
||
| // Define defines the arithmetic circuit. | ||
| func (c *adderCircuit) Define(api frontend.API) error { | ||
| selectedPart := selector.Partition(api, c.Count, false, c.In[:]) | ||
| sum := api.Add(selectedPart[0], selectedPart[1], selectedPart[2:]...) | ||
| api.AssertIsEqual(sum, c.ExpectedSum) | ||
| return nil | ||
| } | ||
|
|
||
| // ExamplePartition gives an example on how to use selector.Partition to make a circuit that accepts a Count and an | ||
| // input array In, and then calculates the sum of first Count numbers of the input array. | ||
| func ExamplePartition() { | ||
| circuit := adderCircuit{} | ||
| witness := adderCircuit{ | ||
| Count: 6, | ||
| In: [10]frontend.Variable{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}, | ||
| ExpectedSum: 30, | ||
| } | ||
| ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit) | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("compiled") | ||
| } | ||
| pk, vk, err := groth16.Setup(ccs) | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("setup done") | ||
| } | ||
| secretWitness, err := frontend.NewWitness(&witness, ecc.BN254.ScalarField()) | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("secret witness") | ||
| } | ||
| publicWitness, err := secretWitness.Public() | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("public witness") | ||
| } | ||
| proof, err := groth16.Prove(ccs, pk, secretWitness) | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("proof") | ||
| } | ||
| err = groth16.Verify(proof, vk, publicWitness) | ||
| if err != nil { | ||
| panic(err) | ||
| } else { | ||
| fmt.Println("verify") | ||
| } | ||
| // Output: compiled | ||
| // setup done | ||
| // secret witness | ||
| // public witness | ||
| // proof | ||
| // verify | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package selector | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/consensys/gnark/frontend" | ||
| "math/big" | ||
| ) | ||
|
|
||
| // Slice selects a slice of the input array at indices [start, end), and zeroes the array at other | ||
| // indices. More precisely, for each i we have: | ||
| // | ||
| // if i >= start and i < end | ||
| // out[i] = input[i] | ||
| // else | ||
| // out[i] = 0 | ||
| // | ||
| // We must have start >= 0 and end <= len(input), otherwise a proof cannot be generated. | ||
| func Slice(api frontend.API, start, end frontend.Variable, input []frontend.Variable) []frontend.Variable { | ||
| // it appears that this is the most efficient implementation. There is also another implementation | ||
| // which creates the mask by adding two stepMask outputs, however that would not work correctly when | ||
| // end < start. | ||
| out := Partition(api, end, false, input) | ||
| out = Partition(api, start, true, out) | ||
| return out | ||
| } | ||
|
|
||
| // Partition selects left or right side of the input array, with respect to the pivotPosition. | ||
| // More precisely when rightSide is false, for each i we have: | ||
| // | ||
| // if i < pivotPosition | ||
| // out[i] = input[i] | ||
| // else | ||
| // out[i] = 0 | ||
| // | ||
| // and when rightSide is true, for each i we have: | ||
| // | ||
| // if i >= pivotPosition | ||
| // out[i] = input[i] | ||
| // else | ||
| // out[i] = 0 | ||
| // | ||
| // We must have pivotPosition >= 0 and pivotPosition <= len(input), otherwise a proof cannot be generated. | ||
| func Partition(api frontend.API, pivotPosition frontend.Variable, rightSide bool, | ||
| input []frontend.Variable) (out []frontend.Variable) { | ||
| out = make([]frontend.Variable, len(input)) | ||
| var mask []frontend.Variable | ||
| // we create a bit mask to multiply with the input. | ||
| if rightSide { | ||
| mask = stepMask(api, len(input), pivotPosition, 0, 1) | ||
| } else { | ||
| mask = stepMask(api, len(input), pivotPosition, 1, 0) | ||
| } | ||
| for i := 0; i < len(out); i++ { | ||
| out[i] = api.Mul(mask[i], input[i]) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // stepMask generates a step like function into an output array of a given length. | ||
| // The output is an array of length outputLen, | ||
| // such that its first stepPosition elements are equal to startValue and the remaining elements are equal to | ||
| // endValue. Note that outputLen cannot be a circuit variable. | ||
| // | ||
| // We must have stepPosition >= 0 and stepPosition <= outputLen, otherwise a proof cannot be generated. | ||
| // This function panics when outputLen is less than 2. | ||
| func stepMask(api frontend.API, outputLen int, | ||
| stepPosition, startValue, endValue frontend.Variable) []frontend.Variable { | ||
| if outputLen < 2 { | ||
| panic("the output len of StepMask must be >= 2") | ||
| } | ||
| // get the output as a hint | ||
| out, err := api.Compiler().NewHint(stepOutput, outputLen, stepPosition, startValue, endValue) | ||
| if err != nil { | ||
| panic(fmt.Sprintf("error in calling StepMask hint: %v", err)) | ||
| } | ||
|
|
||
| // add the boundary constraints: | ||
| // (out[0] - startValue) * stepPosition == 0 | ||
| api.AssertIsEqual(api.Mul(api.Sub(out[0], startValue), stepPosition), 0) | ||
| // (out[len(out)-1] - endValue) * (len(out) - stepPosition) == 0 | ||
| api.AssertIsEqual(api.Mul(api.Sub(out[len(out)-1], endValue), api.Sub(len(out), stepPosition)), 0) | ||
|
ivokub marked this conversation as resolved.
|
||
|
|
||
| // add constraints for the correct form of a step function that steps at the stepPosition | ||
| for i := 1; i < len(out); i++ { | ||
| // (out[i] - out[i-1]) * (i - stepPosition) == 0 | ||
| api.AssertIsEqual(api.Mul(api.Sub(out[i], out[i-1]), api.Sub(i, stepPosition)), 0) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // stepOutput is a hint function used within [StepMask] function. It must be | ||
| // provided to the prover when circuit uses it. | ||
| func stepOutput(_ *big.Int, inputs, results []*big.Int) error { | ||
| stepPos := inputs[0] | ||
|
ivokub marked this conversation as resolved.
|
||
| startValue := inputs[1] | ||
| endValue := inputs[2] | ||
| for i := 0; i < len(results); i++ { | ||
| if i < int(stepPos.Int64()) { | ||
| results[i].Set(startValue) | ||
| } else { | ||
| results[i].Set(endValue) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.