Skip to content

Commit e67a6a1

Browse files
committed
more fixes for sparse encoding.
1 parent e05d5d2 commit e67a6a1

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

arithmetization/src/main/java/net/consensys/linea/zktracer/lt/Column.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void write(byte[] bytes) {
133133
* @return
134134
*/
135135
public Encoding toEncoding() {
136-
return Encoding.of(buffer);
136+
return Encoding.of(name(),buffer);
137137
}
138138
}
139139

@@ -183,7 +183,7 @@ public void write(byte[] bytes) {
183183
* @return
184184
*/
185185
public Encoding toEncoding() {
186-
return Encoding.of(buffer, bitwidth(), heap);
186+
return Encoding.of(name(),buffer, bitwidth(), heap);
187187
}
188188
}
189189

arithmetization/src/main/java/net/consensys/linea/zktracer/lt/Encoding.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,27 @@ public record Encoding(int encoding, byte[] data) {
2121
public static final byte ENCODING_STATIC_DENSE = 1;
2222
public static final byte ENCODING_STATIC_SPARSE8 = 2;
2323
public static final byte ENCODING_STATIC_SPARSE16 = 3;
24-
public static final byte ENCODING_STATIC_SPARSE32 = 4;
25-
public static final byte ENCODING_POOL_CONSTANT = 5;
26-
public static final byte ENCODING_POOL8_DENSE = 6;
27-
public static final byte ENCODING_POOL16_DENSE = 7;
28-
public static final byte ENCODING_POOL32_DENSE = 8;
29-
public static final byte ENCODING_POOL8_SPARSE8 = 9;
30-
public static final byte ENCODING_POOL8_SPARSE16 = 10;
31-
public static final byte ENCODING_POOL8_SPARSE32 = 11;
32-
public static final byte ENCODING_POOL16_SPARSE8 = 12;
33-
public static final byte ENCODING_POOL16_SPARSE16 = 13;
34-
public static final byte ENCODING_POOL16_SPARSE32 = 14;
35-
public static final byte ENCODING_POOL32_SPARSE8 = 15;
36-
public static final byte ENCODING_POOL32_SPARSE16 = 16;
37-
public static final byte ENCODING_POOL32_SPARSE32 = 17;
24+
public static final byte ENCODING_STATIC_SPARSE24 = 4; // not supported
25+
public static final byte ENCODING_STATIC_SPARSE32 = 5;
26+
public static final byte ENCODING_POOL_CONSTANT = 6;
27+
public static final byte ENCODING_POOL1_DENSE = 7;
28+
public static final byte ENCODING_POOL2_DENSE = 8;
29+
public static final byte ENCODING_POOL4_DENSE = 9;
30+
public static final byte ENCODING_POOL8_DENSE = 10;
31+
public static final byte ENCODING_POOL16_DENSE = 11;
32+
public static final byte ENCODING_POOL32_DENSE = 12;
33+
public static final byte ENCODING_POOL8_SPARSE8 = 13;
34+
public static final byte ENCODING_POOL8_SPARSE16 = 14;
35+
public static final byte ENCODING_POOL8_SPARSE24 = 15; // not supported
36+
public static final byte ENCODING_POOL8_SPARSE32 = 16;
37+
public static final byte ENCODING_POOL16_SPARSE8 = 17;
38+
public static final byte ENCODING_POOL16_SPARSE16 = 18;
39+
public static final byte ENCODING_POOL16_SPARSE24 = 19; // not supported
40+
public static final byte ENCODING_POOL16_SPARSE32 = 20;
41+
public static final byte ENCODING_POOL32_SPARSE8 = 21;
42+
public static final byte ENCODING_POOL32_SPARSE16 = 22;
43+
public static final byte ENCODING_POOL32_SPARSE24 = 23; // not supported
44+
public static final byte ENCODING_POOL32_SPARSE32 = 24;
3845

3946
public Encoding(boolean pooled, int constant, byte[] data) {
4047
this(encodingConstant(pooled, constant), data);
@@ -84,6 +91,12 @@ private static int encodingDense(boolean pooled, int entryWidth, int bitwidth) {
8491
//
8592
if (!pooled) {
8693
opcode = ENCODING_STATIC_DENSE;
94+
} else if (entryWidth <= 1) {
95+
opcode = ENCODING_POOL1_DENSE;
96+
} else if (entryWidth <= 2) {
97+
opcode = ENCODING_POOL2_DENSE;
98+
} else if (entryWidth <= 4) {
99+
opcode = ENCODING_POOL4_DENSE;
87100
} else if (entryWidth <= 8) {
88101
opcode = ENCODING_POOL8_DENSE;
89102
} else if (entryWidth <= 16) {
@@ -116,7 +129,7 @@ private static int encodingConstant(boolean pooled, int constant) {
116129
* @param buffer Column data
117130
* @return Encoded column data
118131
*/
119-
public static Encoding of(int[] buffer) {
132+
public static Encoding of(String name, int[] buffer) {
120133
long maxValue = Util.maxValue(buffer);
121134
long minValue = Util.minValue(buffer);
122135
int numberOfBlocks = Util.countNumberOfBlocks(buffer);
@@ -145,7 +158,7 @@ public static Encoding of(int[] buffer) {
145158
* @param buffer Column data
146159
* @return Encoded column data
147160
*/
148-
public static Encoding of(int[] buffer, int bitwidth, BytesHeap heap) {
161+
public static Encoding of(String name, int[] buffer, int bitwidth, BytesHeap heap) {
149162
long maxValue = Util.maxValue(buffer);
150163
long minValue = Util.minValue(buffer);
151164
int numberOfBlocks = Util.countNumberOfBlocks(buffer);
@@ -426,7 +439,7 @@ private static byte[] encodeU8Sparse8(int numBlocks, int[] buffer) {
426439
* Encode 8bit values using a "sparse encoding" consisting of tuples (u8 value, u16 n), where each
427440
* represents n copies of the given value.
428441
*
429-
* @param buffer Contains only values in {0,1,...65534,65535}.
442+
* @param buffer Contains only values in {0,1,...254,255}.
430443
* @return byte encoding of the data
431444
*/
432445
private static byte[] encodeU8Sparse16(int numBlocks, int[] buffer) {

arithmetization/src/main/java/net/consensys/linea/zktracer/lt/Util.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ static int bitWidthOf(long value) {
2424
} else if (value < 16L) {
2525
return 4;
2626
} else if (value < 256L) {
27-
return 1;
27+
return 8;
2828
} else if (value < 65536L) {
29-
return 2;
29+
return 16;
3030
} else if (value < 4294967296L) {
31-
return 4;
31+
return 32;
3232
} else {
3333
throw new IllegalArgumentException("invalid value for byte width: " + value);
3434
}
@@ -81,6 +81,8 @@ static int determineLargestBlock(int[] data) {
8181
lastIndex = i;
8282
}
8383
}
84+
// Include final block.
85+
max = Math.max(max, data.length - lastIndex);
8486
//
8587
return max;
8688
}

0 commit comments

Comments
 (0)