Skip to content

Commit

Permalink
final tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
DavePearce committed Feb 24, 2025
1 parent 756f252 commit 50d569e
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 151 deletions.
1 change: 0 additions & 1 deletion pkg/air/gadgets/lexicographic_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
// ensure it is positive. The delta column is constrained to a given bitwidth,
// with constraints added as necessary to ensure this.
func ApplyLexicographicSortingGadget(prefix string, columns []uint, signs []bool, bitwidth uint, schema *air.Schema) {
fmt.Printf("Columns: source (%d) vs signs (%d), bitwidth: %d\n", len(columns), len(signs), bitwidth)
// Check preconditions
if len(columns) < len(signs) {
panic("Inconsistent number of columns and signs for lexicographic sort.")
Expand Down
3 changes: 2 additions & 1 deletion pkg/hir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func (p *Schema) AddRangeConstraint(handle string, context trace.Context, expr E
}

// AddSortedConstraint appends a new sorted constraint.
func (p *Schema) AddSortedConstraint(handle string, context trace.Context, bitwidth uint, sources []UnitExpr, signs []bool) {
func (p *Schema) AddSortedConstraint(handle string, context trace.Context, bitwidth uint, sources []UnitExpr,
signs []bool) {
// Finally add constraint
p.constraints = append(p.constraints,
constraint.NewSortedConstraint(handle, context, bitwidth, sources, signs))
Expand Down
6 changes: 3 additions & 3 deletions pkg/mir/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ func lowerSortedConstraintToAir(c SortedConstraint, mirSchema *Schema, airSchema
sources[i] = air_gadgets.Expand(c.Context, sourceBitwidth, source, airSchema)
}
// Determine number of ordered columns
ncols := len(c.Signs)
numSignedCols := len(c.Signs)
// finally add the constraint
if ncols == 1 {
if numSignedCols == 1 {
// For a single column sort, its actually a bit easier because we don't
// need to implement a multiplexor (i.e. to determine which column is
// differs, etc). Instead, we just need a delta column which ensures
Expand All @@ -231,7 +231,7 @@ func lowerSortedConstraintToAir(c SortedConstraint, mirSchema *Schema, airSchema
// Sanity check bitwidth
bitwidth := uint(0)

for i := 0; i < ncols; i++ {
for i := 0; i < numSignedCols; i++ {
// Extract bitwidth of ith column
ith := mirSchema.Columns().Nth(sources[i]).DataType.AsUint().BitWidth()
if ith > bitwidth {
Expand Down
3 changes: 2 additions & 1 deletion pkg/mir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (p *Schema) AddRangeConstraint(handle string, casenum uint, context trace.C
}

// AddSortedConstraint appends a new sorted constraint.
func (p *Schema) AddSortedConstraint(handle string, context trace.Context, bitwidth uint, sources []Expr, signs []bool) {
func (p *Schema) AddSortedConstraint(handle string, context trace.Context, bitwidth uint, sources []Expr,
signs []bool) {
// Finally add constraint
p.constraints = append(p.constraints,
constraint.NewSortedConstraint(handle, context, bitwidth, sources, signs))
Expand Down
145 changes: 0 additions & 145 deletions pkg/util/collection/set/sortedset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,156 +13,11 @@
package set

import (
"fmt"
"strings"
"testing"

"github.com/consensys/go-corset/pkg/util"
)

const N = 2

type entry struct {
values [N]uint
}

func (lhs entry) LessEq(rhs entry) bool {
for i := len(lhs.values); i > 0; i-- {
//sign := (i == len(lhs.values)) || (i == 1)
sign := true
//
if lhs.values[i-1] < rhs.values[i-1] {
return sign
} else if lhs.values[i-1] > rhs.values[i-1] {
return !sign
}
}
//
return true
}

func zip(items [N][]uint) []entry {
var entries []entry

for i := range items[0] {
var row [N]uint
for j := range items {
row[j] = items[j][i]
}

entries = append(entries, entry{row})
}
//
return entries
}

func unzip(entries []entry) [N][]uint {
var items [N][]uint
//
for _, e := range entries {
for i := 0; i < N; i++ {
items[i] = append(items[i], e.values[i])
}
}
//
return items
}

func initWords(n uint, widths []uint) [N][]uint {
var words [N][]uint
//
for k := 0; k < N; k++ {
if widths[k] != 0 {
words[k] = util.GenerateRandomUints(n, widths[k])
} else {
words[k] = make([]uint, n)
}
}
//
return words
}

func sortWords(words [N][]uint) [N][]uint {
// Sort it
aset := NewAnySortedSet[entry]()
//
for _, v := range zip(words) {
aset.Insert(v)
}
// Unzip it
return unzip(aset.ToArray())
}

func areSorted(words [N][]uint) bool {
ws := zip(words)
//
for i := range ws {
if i > 0 && !ws[i-1].LessEq(ws[i]) {
return false
}
}
//
return true
}

func hasDeltaOverflow(words [N][]uint, bounds []uint) bool {
nrows := len(words[0])
for k := 0; k < nrows; k++ {
if k == 0 {
continue
}
//
for i := N; i > 0; i-- {
items := words[i-1]
// NOTE: assumes positive sign here.
delta := items[k] - items[k-1]
//
if delta >= bounds[i-1] {
return true
} else if delta != 0 {
break
}
}
}
//
return false
}

func Test_SortedSet(t *testing.T) {
// Bounds determined by bitwidth of columns.
bounds := []uint{262144, 262144}
init := []uint{300000, 1024}
//init := []uint{256, 256}
// //
for i := 3; i < 10; i++ {
for j := 0; j < 100000; j++ {
// Create words
words := initWords(uint(i), init)
// Sort words
words = sortWords(words)
//
//if words[N-1][0] != 0 {
//if !areSorted(words) {
if hasDeltaOverflow(words, bounds) {
// Print words
fmt.Printf("{ ")

for k := 0; k < N; k++ {
if k != 0 {
fmt.Print(", ")
}

kth := fmt.Sprintf("%v", words[k])
kth = strings.ReplaceAll(kth, " ", ",")
fmt.Printf("\"W%d\": %s", k, kth)
}

fmt.Printf("}\n")
}
}
}
}

func Test_SortedSet_00(t *testing.T) {
check_SortedSet_Insert(t, 5, 10)
check_SortedSet_InsertSorted(t, 5, 10)
Expand Down

0 comments on commit 50d569e

Please sign in to comment.