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
{{ message }}
This repository was archived by the owner on Feb 18, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: zkllvm/best-practices-limitations/rust-derive.mdx
+8-25
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,19 @@
2
2
3
3
Whenever a Rust circuit is compiled, `rustc` applies various optimizations to reduce its memory usage.
4
4
5
-
Among these memory optimizations is [**reordering fields in structs and enums to avoid unnecessary 'paddings' in circuit IRs**](https://users.rust-lang.org/t/disable-struct-reordering-optimization-when-emitting-llvm-bitcode/74951). Consider the following example:
5
+
Among these memory optimizations is [**reordering fields in structs and enums to avoid unnecessary 'paddings' in circuit IRs**](https://doc.rust-lang.org/nomicon/repr-rust.html). Consider the following example:
6
6
7
7
```rust
8
-
useark_curve25519::{EdwardsAffine, Fr};
9
8
useark_pallas::Fq;
10
9
11
10
typeBlockType= [Fq; 2];
12
-
typeEdDSAMessageBlockType= [Fq; 4];
13
11
14
12
#[derive(Copy, Clone)]
15
13
pubstructBlockDataType {
16
14
prev_block_hash:BlockType,
17
15
data:BlockType,
18
-
validators_signatures: [EdDSASignatureType; 4],
19
-
validators_keys: [EdwardsAffine; 4],
20
-
}
21
-
22
-
#[derive(Copy, Clone)]
23
-
pubstructEdDSASignatureType {
24
-
r:EdwardsAffine,
25
-
s:Fr,
16
+
validators_signature:i32,
17
+
validators_key:u64,
26
18
}
27
19
```
28
20
@@ -37,31 +29,22 @@ The public input representation of the `BlockDataType` struct would look as foll
When compiling the `BlockDataType` struct, `rustc`will reorder its fields.
40
+
When compiling the `BlockDataType` struct, `rustc`may reorder its fields.
59
41
60
-
When `assigner` is called on a circuit with this struct, the circuit IR will conflict with the public input as the field order in the IR and the public input file will no longer match.
42
+
When `assigner` is called on a circuit with this struct, the circuit IR would conflict with the public input as the field order in the IR and the public input file would no longer match.
61
43
62
44
To avoid this problem, use the `#[repr(C)]` directive:
0 commit comments