Skip to content
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

Refac/arya todos #1107

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions constraint/bls12-377/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bls12-381/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bls24-315/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bls24-317/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bn254/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bw6-633/gkr.go

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

4 changes: 2 additions & 2 deletions constraint/bw6-761/gkr.go

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

12 changes: 7 additions & 5 deletions constraint/gkr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type InputDependency struct {
}

type GkrWire struct {
Gate string // TODO: Change to description
GateName string
Inputs []int
Dependencies []InputDependency // nil for input wires
NbUniqueOutputs int
Expand Down Expand Up @@ -49,7 +49,9 @@ func (w GkrWire) IsOutput() bool {
return w.NbUniqueOutputs == 0
}

// AssignmentOffsets returns the index of the first value assigned to a wire TODO: Explain clearly
// AssignmentOffsets returns the index of the first value assigned to a wire in the Solve/Prove GKR hint.
// For any GKR circuit wire, the explicitly assigned values (if any) across all instances are contiguous as hint inputs.
// Thus, the indexes of all the assignments to the i'th wire are [d.AssignmentOffsets()[i], d.AssignmentOffsets()[i+1])
func (d *GkrInfo) AssignmentOffsets() []int {
c := d.Circuit
res := make([]int, len(c)+1)
Expand Down Expand Up @@ -96,7 +98,7 @@ func (d *GkrInfo) Compile(nbInstances int) (GkrPermutations, error) {
p.SortedWires, uniqueOuts = algo_utils.TopologicalSort(inputs)
p.WiresPermutation = algo_utils.InvertPermutation(p.SortedWires)
wirePermutationAt := algo_utils.SliceAt(p.WiresPermutation)
sorted := make([]GkrWire, len(d.Circuit)) // TODO: Directly manipulate d.Circuit instead
sorted := make([]GkrWire, len(d.Circuit)) // perf-TODO: Directly manipulate d.Circuit instead
for newI, oldI := range p.SortedWires {
oldW := d.Circuit[oldI]

Expand All @@ -117,10 +119,10 @@ func (d *GkrInfo) Compile(nbInstances int) (GkrPermutations, error) {
if oldW.Dependencies[i].InputInstance == oldW.Dependencies[i-1].InputInstance {
return p, fmt.Errorf("an input wire can only have one dependency per instance")
}
} // TODO: Check that dependencies and explicit assignments cover all instances
}

sorted[newI] = GkrWire{
Gate: oldW.Gate,
GateName: oldW.GateName,
Inputs: algo_utils.Map(oldW.Inputs, wirePermutationAt),
Dependencies: oldW.Dependencies,
NbUniqueOutputs: len(uniqueOuts[oldI]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func convertCircuit(noPtr constraint.GkrCircuit) (gkr.Circuit, error) {
resCircuit := make(gkr.Circuit, len(noPtr))
var found bool
for i := range noPtr {
if resCircuit[i].Gate, found = gkr.Gates[noPtr[i].Gate]; !found && noPtr[i].Gate != "" {
return nil, fmt.Errorf("gate \"%s\" not found", noPtr[i].Gate)
if resCircuit[i].Gate, found = gkr.Gates[noPtr[i].GateName]; !found && noPtr[i].GateName != "" {
return nil, fmt.Errorf("gate \"%s\" not found", noPtr[i].GateName)
}
resCircuit[i].Inputs = algo_utils.Map(noPtr[i].Inputs, algo_utils.SlicePtrAt(resCircuit))
}
Expand Down
45 changes: 1 addition & 44 deletions std/compress/io.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package compress

import (
"errors"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/compress/internal/plonk"
"github.com/consensys/gnark/std/hash/mimc"
Expand Down Expand Up @@ -71,48 +70,6 @@ func ChecksumPaddedBytes(b []byte, validLength int, hsh hash.Hash, fieldNbBits i
return hsh.Sum(nil)
}

// UnpackIntoBytes construes every element in packed as consisting of bytesPerElem bytes, returning those bytes
// it DOES NOT prove that the elements in unpacked are actually bytes
// nbBytes is the number of "valid" bytes according to the padding scheme in https://github.com/Consensys/zkevm-monorepo/blob/main/prover/lib/compressor/blob/blob_maker.go#L299
// TODO @tabaie @gbotrel move the padding/packing code to gnark or compress
// the very last non-zero byte in the unpacked stream is meant to encode the number of unused bytes in the last field element used.
// though UnpackIntoBytes includes that last byte in unpacked, it is not counted in nbBytes
func UnpackIntoBytes(api frontend.API, bytesPerElem int, packed []frontend.Variable) (unpacked []frontend.Variable, nbBytes frontend.Variable, err error) {
if unpacked, err = api.Compiler().NewHint(UnpackIntoBytesHint, bytesPerElem*len(packed), packed...); err != nil {
return
}
found := frontend.Variable(0)
nbBytes = frontend.Variable(0)
for i := len(unpacked) - 1; i >= 0; i-- {

z := api.IsZero(unpacked[i])

lastNonZero := plonk.EvaluateExpression(api, z, found, -1, -1, 1, 1) // nz - found
nbBytes = api.Add(nbBytes, api.Mul(lastNonZero, frontend.Variable(i))) // the last nonzero byte itself is useless

//api.AssertIsEqual(api.Mul(api.Sub(bytesPerElem-i%bytesPerElem, unpacked[i]), lastNonZero), 0) // sanity check, technically unnecessary TODO @Tabaie make sure it's one constraint only or better yet, remove

found = plonk.EvaluateExpression(api, z, found, -1, 0, 1, 1) // found ? 1 : nz = nz + found (1 - nz) = 1 - z + found z
}
return
}

func UnpackIntoBytesHint(_ *big.Int, ins, outs []*big.Int) error {
bytesPerElem := len(outs) / len(ins)
if len(ins)*bytesPerElem != len(outs) {
return errors.New("in length must divide out length")
}
_256 := big.NewInt(256)
var v big.Int
for i := range ins {
v.Set(ins[i])
for j := bytesPerElem - 1; j >= 0; j-- {
v.DivMod(&v, _256, outs[i*bytesPerElem+j])
}
}
return nil
}

// ReadNum reads the slice c as a big endian number in base radix
func ReadNum(api frontend.API, c []frontend.Variable, radix *big.Int) frontend.Variable {
if len(c) == 0 {
Expand Down Expand Up @@ -177,7 +134,7 @@ func NewNumReader(api frontend.API, toRead []frontend.Variable, numNbBits, wordN
}

func (nr *NumReader) SetNumNbBits(numNbBits int) {
wordNbBits := nr.radix.BitLen() - 1 // TODO check
wordNbBits := nr.radix.BitLen() - 1
wordsPerNum := numNbBits / wordNbBits
if wordsPerNum*wordNbBits != numNbBits {
panic("numNbBits must be divisible by wordNbBits")
Expand Down
1 change: 0 additions & 1 deletion std/compress/lzss/snark.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func RegisterHints() {
hint.RegisterHint(internal.BreakUpBytesIntoBitsHint)
hint.RegisterHint(internal.BreakUpBytesIntoCrumbsHint)
hint.RegisterHint(internal.BreakUpBytesIntoHalfHint)
hint.RegisterHint(compress.UnpackIntoBytesHint)
}

// options and other auxiliary input
Expand Down
4 changes: 2 additions & 2 deletions std/gkr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func frontendVarToInt(a constraint.GkrVariable) int {

func (api *API) NamedGate(gate string, in ...constraint.GkrVariable) constraint.GkrVariable {
api.toStore.Circuit = append(api.toStore.Circuit, constraint.GkrWire{
Gate: gate,
Inputs: algo_utils.Map(in, frontendVarToInt),
GateName: gate,
Inputs: algo_utils.Map(in, frontendVarToInt),
})
api.assignments = append(api.assignments, nil)
return constraint.GkrVariable(len(api.toStore.Circuit) - 1)
Expand Down
8 changes: 4 additions & 4 deletions std/gkr/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ func (c *benchMiMCMerkleTreeCircuit) Define(api frontend.API) error {

// cheat{
gkr.toStore.Circuit = append(gkr.toStore.Circuit, constraint.GkrWire{
Gate: "mimc",
Inputs: []int{int(x), int(y)},
GateName: "mimc",
Inputs: []int{int(x), int(y)},
})
gkr.assignments = append(gkr.assignments, nil)
z := constraint.GkrVariable(2)
Expand Down Expand Up @@ -583,8 +583,8 @@ func (c *mimcNoDepCircuit) Define(api frontend.API) error {
z = y
for i := 0; i < c.mimcDepth; i++ {
_gkr.toStore.Circuit = append(_gkr.toStore.Circuit, constraint.GkrWire{
Gate: "mimc",
Inputs: []int{int(x), int(z)},
GateName: "mimc",
Inputs: []int{int(x), int(z)},
})
_gkr.assignments = append(_gkr.assignments, nil)
z = constraint.GkrVariable(len(_gkr.toStore.Circuit) - 1)
Expand Down
2 changes: 1 addition & 1 deletion std/gkr/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func newCircuitDataForSnark(info constraint.GkrInfo, assignment assignment) circ
for i := range circuit {
w := info.Circuit[i]
circuit[i] = Wire{
Gate: ite(w.IsInput(), Gates[w.Gate], Gate(IdentityGate{})),
Gate: ite(w.IsInput(), Gates[w.GateName], Gate(IdentityGate{})),
Inputs: algo_utils.Map(w.Inputs, circuitAt),
nbUniqueOutputs: w.NbUniqueOutputs,
}
Expand Down
Loading