Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zkllvm outdated info #48

Merged
merged 11 commits into from
May 23, 2024
74 changes: 35 additions & 39 deletions sidebar-zkllvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
type: 'category',
label: 'Getting started',
collapsible: true,
collapsed: false,
collapsed: true,
items: [
{
type: 'doc',
Expand Down Expand Up @@ -86,7 +86,7 @@ export default {
collapsed: true,
items: [
{

type: 'doc',
label: 'Refactoring if/else statements',
id: 'best-practices-limitations/avoiding-if-else-statements'
Expand Down Expand Up @@ -116,20 +116,12 @@ export default {
label: 'Unrolling loops',
id: 'best-practices-limitations/unrolling-loops'
},
]
},
{
type: 'category',
label: 'Circuit development',
collapsible: true,
collapsed: true,
items: [
{
type: 'doc',
label: 'Standard library',
id: 'circuit-development/standalone-clang',
},
],
label: 'Structs and enums in Rust',
id: 'best-practices-limitations/rust-derive'
}
]
},
{
type: 'category',
Expand All @@ -139,24 +131,38 @@ export default {
items: [
{
type: 'doc',
label: 'First circuit with hashes',
id: 'tutorials/hashes'
},
{
type: 'doc',
label: 'EsDSA signature verifications',
id: 'tutorials/eddsa'
label: 'Primer',
id: 'use-cases/primer'
},
{
type: 'doc',
label: 'Merkle tree commitment schemes',
id: 'tutorials/merkle-tree'
},
{
type: 'doc',
label: 'Constructing a zkBridge',
id: 'tutorials/zkbridge'
type: 'category',
label: 'Construct a zkBridge',
collapsible: true,
collapsed: true,
items: [
{
type: 'doc',
label: 'Write a circuit with hashes',
id: 'use-cases/zk-bridge/hashes'
},
{
type: 'doc',
label: 'Verify EdDSA signatures',
id: 'use-cases/zk-bridge/eddsa'
},
{
type: 'doc',
label: 'Create a Merkle tree commitment scheme',
id: 'use-cases/zk-bridge/merkle-tree'
},
{
type: 'doc',
label: 'Write an algorithm for state-proof verification',
id: 'use-cases/zk-bridge/zkbridge'
},
]
},

]
},
{
Expand All @@ -165,21 +171,11 @@ export default {
collapsible: true,
collapsed: true,
items: [
{
type: 'doc',
label: 'Contributing',
id: 'misc/contributing'
},
{
type: 'doc',
label: 'Code of conduct',
id: 'misc/code-of-conduct'
},
{
type: 'doc',
label: 'Contact',
id: 'misc/contact'
},
]
},
],
Expand Down
60 changes: 60 additions & 0 deletions zkllvm/best-practices-limitations/rust-derive.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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**](https://doc.rust-lang.org/nomicon/repr-rust.html). Consider the following example:

```rust

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, but maybe for demonstration purposes we can construct smaller example? This one is a bit too big than needed to illustrate the issue and probably it will be a bit harder for users to get the point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've simplified the example

use ark_pallas::Fq;

type BlockType = [Fq; 2];

pub struct BlockDataType {
prev_block_hash: BlockType,
data: BlockType,
validators_signature: i32,
validators_key: u64,
}
```

The public input representation of the `BlockDataType` struct would look as follows:

```json
"struct": [
{
"array": [{"field": 1}, {"field": 1}]
},
{
"array": [{"field": 3}, {"field": 1}]
},
{
"int": 1
},
{
"int": 1
}
]
```

When compiling the `BlockDataType` struct, `rustc` may reorder its fields.

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.

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

```rust

use ark_pallas::Fq;

type BlockType = [Fq; 2];

#[repr(C)]
pub struct BlockDataType {
prev_block_hash: BlockType,
data: BlockType,
validators_signature: i32,
validators_key: u64,
}
```

If this directive is included, Rust will treat structs and enums as C-like types, meaning that `rustc` will never reorder fields in them.
25 changes: 0 additions & 25 deletions zkllvm/circuit-development/standalone-clang.md

This file was deleted.

5 changes: 0 additions & 5 deletions zkllvm/misc/contact.md

This file was deleted.

3 changes: 0 additions & 3 deletions zkllvm/misc/contributing.md

This file was deleted.

183 changes: 0 additions & 183 deletions zkllvm/tutorials/01-hashes.md

This file was deleted.

Loading