Skip to content

Commit be4cd85

Browse files
committed
feat: support constant arrays
This adds support for encoding constant arrays, rather than just zero arrays.
1 parent bd3d8c8 commit be4cd85

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

pkg/trace/lt/legacy_reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func readWordColumnData(header legacyHeader, bytes []byte) array.MutArray[word.B
225225
arr = array.NewSmallArray[uint16, word.BigEndian](header.length, header.width*8)
226226
offset = uint(0)
227227
mx uint16
228+
zero word.BigEndian
228229
)
229230
// Assign elements
230231
for i := uint(0); i < header.length; i++ {
@@ -242,7 +243,7 @@ func readWordColumnData(header legacyHeader, bytes []byte) array.MutArray[word.B
242243
//
243244
switch {
244245
case mx == 0:
245-
return array.NewZeroArray[word.BigEndian](header.length)
246+
return array.NewConstantArray[word.BigEndian](header.length, zero)
246247
case mx < 256:
247248
return readByteColumnData(header.length, bytes, 1, 2)
248249
}

pkg/util/collection/array/builder.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ type staticArrayBuilder[T word.Word[T]] struct {
5151

5252
// NewArray constructs a new word array with a given capacity.
5353
func (p *staticArrayBuilder[T]) NewArray(height uint, bitwidth uint) MutArray[T] {
54+
var zero T
55+
//
5456
switch {
5557
case bitwidth == 0:
56-
return NewZeroArray[T](height)
58+
return NewConstantArray(height, zero)
5759
case bitwidth == 1:
5860
arr := NewBitArray[T](height)
5961
return &arr
@@ -87,9 +89,11 @@ type DynamicBuilder[T word.DynamicWord[T], P pool.Pool[uint32, T]] struct {
8789

8890
// NewArray constructs a new word array with a given capacity.
8991
func (p *DynamicBuilder[T, P]) NewArray(height uint, bitwidth uint) MutArray[T] {
92+
var zero T
93+
//
9094
switch {
9195
case bitwidth == 0:
92-
return NewZeroArray[T](height)
96+
return NewConstantArray(height, zero)
9397
case bitwidth == 1:
9498
arr := NewBitArray[T](height)
9599
return &arr
@@ -111,8 +115,8 @@ func (p *DynamicBuilder[T, P]) NewArray(height uint, bitwidth uint) MutArray[T]
111115
// when the encoding was made.
112116
func (p *DynamicBuilder[T, P]) Decode(encoding Encoding) MutArray[T] {
113117
switch encoding.OpCode() {
114-
case ENCODING_ZERO:
115-
return decode_zero[T](encoding)
118+
case ENCODING_CONSTANT:
119+
return decode_constant[T](encoding)
116120
case ENCODING_STATIC:
117121
return decode_static[T](encoding)
118122
case ENCODING_POOL:
@@ -131,8 +135,8 @@ func (p *DynamicBuilder[T, P]) Encode(array Array[T]) Encoding {
131135
//
132136
switch {
133137
case bitwidth == 0:
134-
encoding.Bytes = encode_zero(array.(*ZeroArray[T]))
135-
encoding.Set(ENCODING_ZERO, bitwidth)
138+
encoding.Bytes = encode_constant(array.(*ConstantArray[T]))
139+
encoding.Set(ENCODING_CONSTANT, bitwidth)
136140
case bitwidth == 1:
137141
encoding.Bytes = encode_bits(array.(*BitArray[T]))
138142
encoding.Set(ENCODING_STATIC, bitwidth)

pkg/util/collection/array/encoding.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/consensys/go-corset/pkg/util/word"
2020
)
2121

22-
// ENCODING_ZERO is for arrays which hold constant values.
23-
const ENCODING_ZERO = 0
22+
// ENCODING_CONSTANT is for arrays which hold constant values.
23+
const ENCODING_CONSTANT = 0
2424

2525
// ENCODING_STATIC is for arrays which hold their values explicitly.
2626
const ENCODING_STATIC = 1
@@ -64,12 +64,19 @@ func (p *Encoding) Set(opcode uint8, operand uint16) {
6464
// Constant Arrays
6565
// ============================================================================
6666

67-
func decode_zero[T word.DynamicWord[T]](encoding Encoding) MutArray[T] {
68-
var len = binary.BigEndian.Uint32(encoding.Bytes)
69-
return NewZeroArray[T](uint(len))
67+
func decode_constant[T word.DynamicWord[T]](encoding Encoding) MutArray[T] {
68+
var (
69+
value T
70+
len = binary.BigEndian.Uint32(encoding.Bytes)
71+
constant = encoding.Operand()
72+
)
73+
//
74+
value = value.SetUint64(uint64(constant))
75+
//
76+
return NewConstantArray[T](uint(len), value)
7077
}
7178

72-
func encode_zero[T word.DynamicWord[T]](array *ZeroArray[T]) []byte {
79+
func encode_constant[T word.DynamicWord[T]](array *ConstantArray[T]) []byte {
7380
var bytes [4]byte
7481
//
7582
binary.BigEndian.PutUint32(bytes[:], uint32(array.Len()))

pkg/util/collection/array/zero_array.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,70 @@ import (
2323
// Implementation
2424
// =================================================================================
2525

26-
// ZeroArray implements an array of single bit words simply using an underlying
27-
// array of packed bytes. That is, where eight bits are packed into a single
28-
// byte.
29-
type ZeroArray[T word.Word[T]] struct {
26+
// ConstantArray implements an array of a constant value.
27+
type ConstantArray[T word.Word[T]] struct {
3028
// Actual height of column
3129
height uint
30+
value T
3231
}
3332

34-
// NewZeroArray constructs a new word array with a given capacity.
35-
func NewZeroArray[T word.Word[T]](height uint) *ZeroArray[T] {
36-
return &ZeroArray[T]{height}
33+
// NewConstantArray constructs a new word array with a given capacity.
34+
func NewConstantArray[T word.Word[T]](height uint, value T) *ConstantArray[T] {
35+
return &ConstantArray[T]{height, value}
3736
}
3837

3938
// Append new word on this array
40-
func (p *ZeroArray[T]) Append(word T) {
39+
func (p *ConstantArray[T]) Append(word T) {
4140
p.Pad(0, 1, word)
4241
}
4342

4443
// Clone makes clones of this array producing an otherwise identical copy.
45-
func (p *ZeroArray[T]) Clone() MutArray[T] {
46-
return &ZeroArray[T]{p.height}
44+
func (p *ConstantArray[T]) Clone() MutArray[T] {
45+
return &ConstantArray[T]{p.height, p.value}
4746
}
4847

4948
// Len returns the number of elements in this word array.
50-
func (p *ZeroArray[T]) Len() uint {
49+
func (p *ConstantArray[T]) Len() uint {
5150
return p.height
5251
}
5352

5453
// BitWidth returns the width (in bits) of elements in this array.
55-
func (p *ZeroArray[T]) BitWidth() uint {
54+
func (p *ConstantArray[T]) BitWidth() uint {
5655
return 0
5756
}
5857

5958
// Build implementation for the array.Builder interface. This simply means that
6059
// a static array is its own builder.
61-
func (p *ZeroArray[T]) Build() Array[T] {
60+
func (p *ConstantArray[T]) Build() Array[T] {
6261
return p
6362
}
6463

6564
// Get returns the field element at the given index in this array.
66-
func (p *ZeroArray[T]) Get(index uint) T {
67-
var b T
68-
// Default is zero
69-
return b
65+
func (p *ConstantArray[T]) Get(index uint) T {
66+
return p.value
7067
}
7168

7269
// Set sets the field element at the given index in this array, overwriting the
7370
// original value.
74-
func (p *ZeroArray[T]) Set(index uint, word T) {
71+
func (p *ConstantArray[T]) Set(index uint, word T) {
7572
// do nothing
7673
}
7774

7875
// Pad implementation for MutArray interface.
79-
func (p *ZeroArray[T]) Pad(n uint, m uint, padding T) {
76+
func (p *ConstantArray[T]) Pad(n uint, m uint, padding T) {
8077
p.height += n + m
8178
}
8279

8380
// Slice out a subregion of this array.
84-
func (p *ZeroArray[T]) Slice(start uint, end uint) Array[T] {
81+
func (p *ConstantArray[T]) Slice(start uint, end uint) Array[T] {
8582
var (
8683
height = end - start
8784
)
8885
// Done
89-
return &ZeroArray[T]{height}
86+
return &ConstantArray[T]{height, p.value}
9087
}
9188

92-
func (p *ZeroArray[T]) String() string {
89+
func (p *ConstantArray[T]) String() string {
9390
var sb strings.Builder
9491

9592
sb.WriteString("[")

0 commit comments

Comments
 (0)