Structs and enums in Rust
Whenever a Rust circuit is compiled, rustc
applies various optimizations to reduce its memory usage.
Among these memory optimizations is reordering fields in structs and enums to avoid unnecessary 'paddings' in circuit IRs. Consider the following example:
use ark_curve25519::{EdwardsAffine, Fr};
use ark_pallas::Fq;
type BlockType = [Fq; 2];
type EdDSAMessageBlockType = [Fq; 4];
#[derive(Copy, Clone)]
pub struct BlockDataType {
prev_block_hash: BlockType,
data: BlockType,
validators_signatures: [EdDSASignatureType; 4],
validators_keys: [EdwardsAffine; 4],
}
#[derive(Copy, Clone)]
pub struct EdDSASignatureType {
r: EdwardsAffine,
s: Fr,
}
The public input representation of the BlockDataType
struct would look as follows:
"struct": [
{
"array": [{"field": 1}, {"field": 1}]
},
{
"array": [{"field": 3}, {"field": 1}]
},
{
"array": [
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
{"struct": [{"curve": [4, 5]}, {"field": 8}]},
{"struct": [{"curve": [4, 5]}, {"field": 8}]}
]
},
{
"array": [
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]},
{"curve": ["0x4f043d481c8f09de646b1aa05de7ebfab126fc8bbb74f42532378c4dec6e76ec", "0x58719b60b26bd8b8b76de1a886ed82aa11692b4dc5494fe96d5b31f1c63f36a8"]}
]
}
]
When compiling the BlockDataType
struct, rustc
will reorder its fields.
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.
To avoid this problem, use the #[repr(C)]
directive:
#[derive(Copy, Clone)]
pub struct BlockDataType {
prev_block_hash: BlockType,
data: BlockType,
validators_signatures: [EdDSASignatureType; 4],
validators_keys: [EdwardsAffine; 4],
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct EdDSASignatureType {
r: EdwardsAffine,
s: Fr,
}
If this directive is included, Rust will treat structs and enums as C-like types, meaning that rustc
will never reorder fields in them.