Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 0fc4c84

Browse files
committed
addressed comments in Rust structs
1 parent 46ac51e commit 0fc4c84

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

zkllvm/best-practices-limitations/rust-derive.mdx

+8-25
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
Whenever a Rust circuit is compiled, `rustc` applies various optimizations to reduce its memory usage.
44

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:
66

77
```rust
8-
use ark_curve25519::{EdwardsAffine, Fr};
98
use ark_pallas::Fq;
109

1110
type BlockType = [Fq; 2];
12-
type EdDSAMessageBlockType = [Fq; 4];
1311

1412
#[derive(Copy, Clone)]
1513
pub struct BlockDataType {
1614
prev_block_hash: BlockType,
1715
data: BlockType,
18-
validators_signatures: [EdDSASignatureType; 4],
19-
validators_keys: [EdwardsAffine; 4],
20-
}
21-
22-
#[derive(Copy, Clone)]
23-
pub struct EdDSASignatureType {
24-
r: EdwardsAffine,
25-
s: Fr,
16+
validators_signature: i32,
17+
validators_key: u64,
2618
}
2719
```
2820

@@ -37,31 +29,22 @@ The public input representation of the `BlockDataType` struct would look as foll
3729
"array": [{"field": 3}, {"field": 1}]
3830
},
3931
{
40-
"array": [
41-
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
42-
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
43-
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
44-
{"struct": [{"curve": [4, 5]}, {"field": 8}]}
45-
]
32+
"int": 1
4633
},
4734
{
48-
"array": [
49-
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
50-
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
51-
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
52-
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]}
53-
]
35+
"int": 1
5436
}
5537
]
5638
```
5739

58-
When compiling the `BlockDataType` struct, `rustc` will reorder its fields.
40+
When compiling the `BlockDataType` struct, `rustc` may reorder its fields.
5941

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.
6143

6244
To avoid this problem, use the `#[repr(C)]` directive:
6345

6446
```rust
47+
#[repr(C)]
6548
#[derive(Copy, Clone)]
6649
pub struct BlockDataType {
6750
prev_block_hash: BlockType,

0 commit comments

Comments
 (0)