Skip to content

Commit

Permalink
Notes for chapter 1 (#1)
Browse files Browse the repository at this point in the history
1. Notes for chapter 1
2. Impl the IP case
  • Loading branch information
SuccinctPaul authored Jul 13, 2023
1 parent 2af1e99 commit 199adc1
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/gh-page.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# CI jobs to be run upon the code lands to the main branch or GitHub Action test branches.

name: ci-post-land

on:
push:
branches: [main, gha-test-*]

jobs:
generate-documentation:
name: Generate the Book using mdBook and deploy it to gh-page(deploy-gh-page)
runs-on: ubuntu-20.04
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
steps:
- uses: actions/checkout@v3
with:
ref: main

- name: Setup mdBook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: '0.4.10'
# mdbook-version: 'latest'

- name: install mdbook-katex
run: "cargo install mdbook-katex"

- run: mdbook build book

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./book/book/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.idea
.vscode
10 changes: 10 additions & 0 deletions 1-ip/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ip"
version = "0.1.0"
edition = "2021"
description = "The interactive proofs case(1.2.1) in chapter 1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
rand_core = { version = "0.6.4", default-features = false, features = ["std"] }
86 changes: 86 additions & 0 deletions 1-ip/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/// The interactive proofs case(1.2.1) in chapter 1
mod prover;
mod utils;
mod verify;

use rand_core::{OsRng, RngCore};
use std::iter;
use std::iter::{once, Sum};

use crate::prover::Prover;
use crate::utils::{calculate_hash, ComputeType};
use crate::verify::Verify;

pub(crate) struct Data {
name: String,
index: u64,
data: Vec<u64>,
}

// Completeness of the IP means that if the cloud correctly runs the program on the data and follows
// the prescribed protocol, then the user will be convinced to accept the answer as valid.
#[test]
fn completeness() {
println!("storage data");

// init business and clouder provider
let mut business = Prover::default();
let mut cloud_provider = Verify::default();

// send & receive data
let data = business.send();
cloud_provider.receive(data.clone());
println!("received data\n");

// challenge and response about data
let challenge = OsRng.next_u64() % 100;
calculate_hash(&data);
assert_eq!(
business.summary + challenge,
cloud_provider.summary(challenge)
);
println!("verified equal");

fn sum(datas: &Vec<u64>) -> u64 {
datas.iter().sum()
}

assert_eq!(sum(&data), cloud_provider.compute(sum));
println!("completeness");
}

// Soundness of the IP means that if the cloud returns the wrong output, then the user will reject the answer
// as invalid with high probability no matter how hard the cloud works to trick the user into accepting the answer as valid.
#[test]
fn soundness() {
println!("storage datas");

// init business and clouder provider
let mut business = Prover::default();
let mut cloud_provider = Verify::default();

// send & receive data
let raw_data = business.send();

let mut data = raw_data.clone();
println!("Add the mess info");
data.push(OsRng.next_u64());
cloud_provider.receive(data.clone());
println!("received datas\n");

// challenge and response about data
let challenge = OsRng.next_u64() % 100;
calculate_hash(&data);
assert_ne!(
business.summary + challenge,
cloud_provider.summary(challenge)
);
println!("verified non equal");

fn sum(datas: &Vec<u64>) -> u64 {
datas.iter().sum()
}

assert_ne!(sum(&raw_data), cloud_provider.compute(sum));
println!("Soundness");
}
26 changes: 26 additions & 0 deletions 1-ip/src/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::utils::calculate_hash;
use rand_core::{OsRng, RngCore};

#[derive(Default)]
pub struct Prover {
// hash key of data
pub(crate) summary: u64,
}

impl Prover {
pub(crate) fn send(&mut self) -> Vec<u64> {
// generate data
let data = Self::generate_data();
println!("data.size:{:?}", data.len());
// obtain key by hash the data
self.summary = calculate_hash(&data);
// send data
data
}

fn generate_data() -> Vec<u64> {
let mut rng = OsRng;
let size = 5 + rng.next_u32() % 15;
(0..size).map(|_| rng.next_u32() as u64).collect::<Vec<_>>()
}
}
13 changes: 13 additions & 0 deletions 1-ip/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// calculate the hash of the data
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}

pub enum ComputeType {
Sum,
}
20 changes: 20 additions & 0 deletions 1-ip/src/verify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::utils::{calculate_hash, ComputeType};

#[derive(Default)]
pub struct Verify {
data: Vec<u64>,
}

impl Verify {
pub(crate) fn receive(&mut self, data: Vec<u64>) {
self.data = data;
}

pub(crate) fn summary(&self, challenge: u64) -> u64 {
calculate_hash(&self.data) + challenge
}

pub(crate) fn compute(&self, f: fn(&Vec<u64>) -> u64) -> u64 {
f(&self.data)
}
}
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"1-ip",
]
2 changes: 2 additions & 0 deletions book/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/book
/target
14 changes: 14 additions & 0 deletions book/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

## Run it locally

* prepare
```rust
cargo install mdbook
cargo install mdbook-katex
cargo install mdbook-plantuml
```

* run
```rust
mdbook serve -o
```
40 changes: 40 additions & 0 deletions book/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[book]
title = "zk book notes"
description = ""
authors = ["ChengYueJia"]
language = "en"
multilingual = false
src = "src"


[output.html]
mathjax-support = true
curly-quotes = true
no-section-label = true
git-repository-url = "https://github.com/ChengYueJia/zkbook-notes"
git-repository-icon = "fa-github"

[output.html.print]
page-break = true

[output.html.playground]
editable = true
line-numbers = true

[output.html.search]
limit-results = 20
use-boolean-and = true
boost-title = 2
boost-hierarchy = 2
boost-paragraph = 1
expand = true
heading-split-level = 2

[output.html.fold]
enable = true
level = 3

# output pdf
#[output.pdf]


65 changes: 65 additions & 0 deletions book/macros.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
\sample:{\overset{?}{\ \gets \ }}
\GG:{\mathbb{G}}
\FF:{\mathbb{F}}
\language:{\mathcal{L}}
\relation:{\mathcal{R}}
\witness:{\text{w}}
\statement:{\text{x}}

\chalfold:{\alpha}
\chaleval:{\zeta}
\chalu:{u}
\chalv:{v}

\openx:{x}
\openy:{y}
\comm:{C}

\accCom:{U}
\accChal:{\chalfold}

\genOpen:{H}

\hpoly:{h}

\relAcc:{\relation_{\mathsf{Acc},\vec{G}}}
\relPCS:{\relation_{\mathsf{PCD},#1}}
\relIPA:{\relation_{\mathsf{IPA},#1}}


\langPCS:{\language_{\mathsf{PCD},#1}}

\rounds:{k}
\degree:{d}

\and:{\small \mathsf{AND}}
\xor:{\small \mathsf{XOR}}
\inv:{\small \mathsf{INV}}

\sC:{\small \mathsf{C}}
\sI:{\small \mathsf{I}}
\sH:{\small \mathsf{H}}
\sX:{\small \mathsf{X}}
\sY:{\small \mathsf{Y}}

\S:{\small S}
\G:{\small G}
\R:{\small R}
\T:{\small T}


\bF:{\small \mathbb{F}}
\bG:{\small \mathbb{G}}


\lsb:{\small \mathsf{LSB}}
\sha:{\small\mathsf{SHA}256}
\aes:{\small \mathsf{AES}}
\gc:{\small \mathcal{GC}}
\bit:{\{0,1\}}
\zero:{\color{red}{0}}
\one:{\color{blue}{1}}
\enc:{\small \mathsf{Enc}}
\gid:{\mathsf{gid}}
\counter:{\mathsf{counter}}
\prg:{\small \mathsf{PRG}}
Loading

0 comments on commit 199adc1

Please sign in to comment.