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

Commit e0cb199

Browse files
committed
changed final Rust circuit to use refs in most functions, removed some directives
1 parent e86539c commit e0cb199

File tree

1 file changed

+42
-57
lines changed

1 file changed

+42
-57
lines changed

zkllvm/use-cases/zk-bridge/zkbridge.mdx

+42-57
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Read the following tutorials before proceeding further.
7171
type EdDSAMessageBlockType = [Fq; 4];
7272

7373
#[repr(C)]
74-
#[derive(Copy, Clone)]
7574
pub struct BlockDataType {
7675
prev_block_hash: BlockType,
7776
data: BlockType,
@@ -80,7 +79,6 @@ Read the following tutorials before proceeding further.
8079
}
8180

8281
#[repr(C)]
83-
#[derive(Copy, Clone)]
8482
pub struct EdDSASignatureType {
8583
r: EdwardsAffine,
8684
s: Fr,
@@ -91,8 +89,6 @@ Read the following tutorials before proceeding further.
9189

9290
:::info[Rust directives]
9391

94-
The `#[derive(Copy, Clone)]` makes it so functions do not take ownership of the variables belonging to custom structs if these variables are passed by value.
95-
9692
To learn more about the `#[derive(C)]` directive, [**click here**](../../best-practices-limitations/rust-derive).
9793

9894
:::
@@ -133,22 +129,22 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
133129
</TabItem>
134130
<TabItem value='rust' label='Rust'>
135131
```rust
136-
pub fn hash_512(r: EdwardsAffine, pk: EdwardsAffine, m: EdDSAMessageBlockType) -> Fr {
132+
pub fn hash_512(r: &EdwardsAffine, pk: &EdwardsAffine, m: &EdDSAMessageBlockType) -> Fr {
137133
assigner_sha2_512(r.0, pk.0, [m[0].0, m[1].0, m[2].0, m[3].0]).into()
138134
}
139135

140-
pub fn hash_256(block1: BlockType, block2: BlockType) -> BlockType {
136+
pub fn hash_256(block1: &BlockType, block2: &BlockType) -> BlockType {
141137
let sha = assigner_sha2_256([block1[0].0, block1[1].0], [block2[0].0, block2[1].0]);
142138
[sha[0].into(), sha[1].into()]
143139
}
144140

145141
pub fn verify_eddsa_signature(
146-
input: EdDSASignatureType,
147-
pk: EdwardsAffine,
148-
m: EdDSAMessageBlockType,
142+
input: &EdDSASignatureType,
143+
pk: &EdwardsAffine,
144+
m: &EdDSAMessageBlockType,
149145
) -> bool {
150146
let b = EdwardsAffine::one();
151-
let k = hash_512(input.r, pk, m);
147+
let k = hash_512(&input.r, pk, m);
152148
b * input.s == input.r + (pk * k)
153149
}
154150

@@ -157,7 +153,7 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
157153
}
158154

159155
#[unroll_for_loops]
160-
pub fn verify_signature(unconfirmed_block: BlockDataType) -> bool {
156+
pub fn verify_signature(unconfirmed_block: &BlockDataType) -> bool {
161157
let mut is_verified: bool = true;
162158
let message: EdDSAMessageBlockType = [
163159
unconfirmed_block.prev_block_hash[0],
@@ -169,9 +165,9 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
169165
for i in 0..4 {
170166
is_verified = is_verified
171167
&& verify_eddsa_signature(
172-
unconfirmed_block.validators_signatures[i],
173-
unconfirmed_block.validators_keys[i],
174-
message,
168+
&unconfirmed_block.validators_signatures[i],
169+
&unconfirmed_block.validators_keys[i],
170+
&message,
175171
);
176172
}
177173

@@ -186,30 +182,21 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
186182
<Tabs groupId='language'>
187183
<TabItem value='cpp' label='C++'>
188184
```cpp
189-
#[circuit]
190-
#[unroll_for_loops]
191-
pub fn verify_protocol_state_proof(
192-
last_confirmed_block_hash: BlockType,
193-
unconfirmed_blocks: [BlockDataType; 2],
194-
) -> bool {
195-
let mut is_correct = is_same(
196-
unconfirmed_blocks[0].prev_block_hash,
197-
last_confirmed_block_hash,
198-
);
199-
is_correct = is_correct && verify_signature(unconfirmed_blocks[0]);
200-
201-
for i in 1..2 {
202-
let evaluated_block_hash: BlockType = hash_256(
203-
unconfirmed_blocks[i - 1].prev_block_hash,
204-
unconfirmed_blocks[i - 1].data,
205-
);
206-
207-
is_correct =
208-
is_correct && is_same(unconfirmed_blocks[i].prev_block_hash, evaluated_block_hash);
209-
is_correct = is_correct && verify_signature(unconfirmed_blocks[i]);
185+
[[circuit]] bool verify_protocol_state_proof (
186+
typename sha2<256>::block_type last_confirmed_block_hash,
187+
std::array<block_data_type, 2> unconfirmed_blocks) {
188+
bool res = true;
189+
if (!is_same(unconfirmed_blocks[0].prev_block_hash, last_confirmed_block_hash)) {
190+
return false;
210191
}
211-
212-
is_correct
192+
for (int i = 1; i < 2; i++) {
193+
typename sha2<256>::block_type evaluated_block_hash =
194+
hash<sha2<256>>(
195+
unconfirmed_blocks[i-1].prev_block_hash,
196+
unconfirmed_blocks[i-1].data);
197+
res = res & is_same(unconfirmed_blocks[i].prev_block_hash, evaluated_block_hash);
198+
}
199+
return res;
213200
}
214201
```
215202
</TabItem>
@@ -225,17 +212,17 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
225212
unconfirmed_blocks[0].prev_block_hash,
226213
last_confirmed_block_hash,
227214
);
228-
is_correct = is_correct && verify_signature(unconfirmed_blocks[0]);
215+
is_correct = is_correct && verify_signature(&unconfirmed_blocks[0]);
229216

230217
for i in 1..2 {
231218
let evaluated_block_hash: BlockType = hash_256(
232-
unconfirmed_blocks[i - 1].prev_block_hash,
233-
unconfirmed_blocks[i - 1].data,
219+
&unconfirmed_blocks[i - 1].prev_block_hash,
220+
&unconfirmed_blocks[i - 1].data,
234221
);
235222

236223
is_correct =
237224
is_correct && is_same(unconfirmed_blocks[i].prev_block_hash, evaluated_block_hash);
238-
is_correct = is_correct && verify_signature(unconfirmed_blocks[i]);
225+
is_correct = is_correct && verify_signature(&unconfirmed_blocks[i]);
239226
}
240227

241228
is_correct
@@ -338,7 +325,6 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
338325
type EdDSAMessageBlockType = [Fq; 4];
339326

340327
#[repr(C)]
341-
#[derive(Copy, Clone)]
342328
pub struct BlockDataType {
343329
prev_block_hash: BlockType,
344330
data: BlockType,
@@ -347,28 +333,27 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
347333
}
348334

349335
#[repr(C)]
350-
#[derive(Copy, Clone)]
351336
pub struct EdDSASignatureType {
352337
r: EdwardsAffine,
353338
s: Fr,
354339
}
355340

356-
pub fn hash_512(r: EdwardsAffine, pk: EdwardsAffine, m: EdDSAMessageBlockType) -> Fr {
341+
pub fn hash_512(r: &EdwardsAffine, pk: &EdwardsAffine, m: &EdDSAMessageBlockType) -> Fr {
357342
assigner_sha2_512(r.0, pk.0, [m[0].0, m[1].0, m[2].0, m[3].0]).into()
358343
}
359344

360-
pub fn hash_256(block1: BlockType, block2: BlockType) -> BlockType {
345+
pub fn hash_256(block1: &BlockType, block2: &BlockType) -> BlockType {
361346
let sha = assigner_sha2_256([block1[0].0, block1[1].0], [block2[0].0, block2[1].0]);
362347
[sha[0].into(), sha[1].into()]
363348
}
364349

365350
pub fn verify_eddsa_signature(
366-
input: EdDSASignatureType,
367-
pk: EdwardsAffine,
368-
m: EdDSAMessageBlockType,
351+
input: &EdDSASignatureType,
352+
pk: &EdwardsAffine,
353+
m: &EdDSAMessageBlockType,
369354
) -> bool {
370355
let b = EdwardsAffine::one();
371-
let k = hash_512(input.r, pk, m);
356+
let k = hash_512(&input.r, pk, m);
372357
b * input.s == input.r + (pk * k)
373358
}
374359

@@ -377,7 +362,7 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
377362
}
378363

379364
#[unroll_for_loops]
380-
pub fn verify_signature(unconfirmed_block: BlockDataType) -> bool {
365+
pub fn verify_signature(unconfirmed_block: &BlockDataType) -> bool {
381366
let mut is_verified: bool = true;
382367
let message: EdDSAMessageBlockType = [
383368
unconfirmed_block.prev_block_hash[0],
@@ -389,9 +374,9 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
389374
for i in 0..4 {
390375
is_verified = is_verified
391376
&& verify_eddsa_signature(
392-
unconfirmed_block.validators_signatures[i],
393-
unconfirmed_block.validators_keys[i],
394-
message,
377+
&unconfirmed_block.validators_signatures[i],
378+
&unconfirmed_block.validators_keys[i],
379+
&message,
395380
);
396381
}
397382

@@ -408,17 +393,17 @@ To learn more about the `#[derive(C)]` directive, [**click here**](../../best-pr
408393
unconfirmed_blocks[0].prev_block_hash,
409394
last_confirmed_block_hash,
410395
);
411-
is_correct = is_correct && verify_signature(unconfirmed_blocks[0]);
396+
is_correct = is_correct && verify_signature(&unconfirmed_blocks[0]);
412397

413398
for i in 1..2 {
414399
let evaluated_block_hash: BlockType = hash_256(
415-
unconfirmed_blocks[i - 1].prev_block_hash,
416-
unconfirmed_blocks[i - 1].data,
400+
&unconfirmed_blocks[i - 1].prev_block_hash,
401+
&unconfirmed_blocks[i - 1].data,
417402
);
418403

419404
is_correct =
420405
is_correct && is_same(unconfirmed_blocks[i].prev_block_hash, evaluated_block_hash);
421-
is_correct = is_correct && verify_signature(unconfirmed_blocks[i]);
406+
is_correct = is_correct && verify_signature(&unconfirmed_blocks[i]);
422407
}
423408

424409
is_correct

0 commit comments

Comments
 (0)