Skip to content

Commit

Permalink
Merge pull request #48 from NilFoundation/zkllvm-outdated-info
Browse files Browse the repository at this point in the history
Zkllvm outdated info
  • Loading branch information
khannanov-nil authored May 23, 2024
2 parents 2c31d80 + 5b81f1d commit d380210
Show file tree
Hide file tree
Showing 14 changed files with 1,613 additions and 658 deletions.
72 changes: 34 additions & 38 deletions sidebar-zkllvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
type: 'category',
label: 'Getting started',
collapsible: true,
collapsed: false,
collapsed: true,
items: [
{
type: 'doc',
Expand Down Expand Up @@ -121,20 +121,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 @@ -144,24 +136,38 @@ export default {
items: [
{
type: 'doc',
label: 'First circuit with hashes',
id: 'tutorials/hashes'
},
{
type: 'doc',
label: 'EsDSA signature verifications',
id: 'tutorials/eddsa'
},
{
type: 'doc',
label: 'Merkle tree commitment schemes',
id: 'tutorials/merkle-tree'
label: 'Primer',
id: 'use-cases/primer'
},
{
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 @@ -170,21 +176,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
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

0 comments on commit d380210

Please sign in to comment.