You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `ColsRef` procedural macro is used in constraint generation to create column structs that have dynamic sizes.
4
+
5
+
Note: this macro was originally created for use in the SHA-2 VM extension, where we reuse the same constraint generation code for three different circuits (SHA-256, SHA-512, and SHA-384).
6
+
See the [SHA-2 VM extension](../../../../../../extensions/sha2/circuit/src/sha2_chip/air.rs) for an example of how to use the `ColsRef` macro to reuse constraint generation code over multiple circuits.
7
+
8
+
## Overview
9
+
10
+
As an illustrative example, consider the following columns struct:
11
+
```rust
12
+
structExampleCols<T, constN:usize> {
13
+
arr: [T; N],
14
+
sum:T,
15
+
}
16
+
```
17
+
Let's say we want to constrain `sum` to be the sum of the elements of `arr`, and `N` can be either 5 or 10.
18
+
We can define a trait that stores the config parameters.
19
+
```rust
20
+
pubtraitExampleConfig {
21
+
constN:usize;
22
+
}
23
+
```
24
+
and then implement it for the two different configs.
25
+
```rust
26
+
pubstructExampleConfigImplA;
27
+
implExampleConfigforExampleConfigImplA {
28
+
constN:usize=5;
29
+
}
30
+
pubstructExampleConfigImplB;
31
+
implExampleConfigforExampleConfigImplB {
32
+
constN:usize=10;
33
+
}
34
+
```
35
+
Then we can use the `ColsRef` macro like this
36
+
```rust
37
+
#[derive(ColsRef)]
38
+
#[config(ExampleConfig)]
39
+
structExampleCols<T, constN:usize> {
40
+
arr: [T; N],
41
+
sum:T,
42
+
}
43
+
```
44
+
which will generate a columns struct that uses references to the fields.
45
+
```rust
46
+
structExampleColsRef<'a, T, constN:usize> {
47
+
arr:&'a [T; N],
48
+
sum:&'aT,
49
+
}
50
+
```
51
+
The `ColsRef` macro will also generate a `from` method that takes a slice of the correct length and returns an instance of the columns struct.
52
+
The `from` method is parameterized by a struct that implements the `ExampleConfig` trait, and it uses the associated constants to determine how to split the input slice into the fields of the columns struct.
53
+
54
+
So, the constraint generation code can be written as
Annotatingastructnamed `ExampleCols` with `#[derive(ColsRef)]` and `#[config(ExampleConfig)]` producestwostructs, `ExampleColsRef` and `ExampleColsRefMut`.
-type `[T; LEN]` becomes `&ArrayView1<T>` (see [ndarray](https://docs.rs/ndarray/latest/ndarray/index.html)) where `LEN` is an associated constant in `ExampleConfig`
0 commit comments