diff --git a/PLONK/noir/.dockerignore b/PLONK/noir/.dockerignore new file mode 100644 index 0000000..5e31262 --- /dev/null +++ b/PLONK/noir/.dockerignore @@ -0,0 +1,3 @@ +hash/sha256/target +hello_world/target +matmult/target diff --git a/PLONK/noir/Dockerfile b/PLONK/noir/Dockerfile index b12c8b0..fa08367 100644 --- a/PLONK/noir/Dockerfile +++ b/PLONK/noir/Dockerfile @@ -1,28 +1,22 @@ -FROM ubuntu:22.04 as base +FROM ubuntu:24.04 AS base RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install \ - -y build-essential cmake git libgmp3-dev libprocps-dev libboost-all-dev libssl-dev libsodium-dev nano wget clang lld libomp-dev curl bash xxd + -y build-essential cmake git nano wget curl bash xxd jq python3 -RUN curl https://sh.rustup.rs -sSf | bash -s -- -y -ENV PATH="/root/.cargo/bin:${PATH}" -RUN git clone https://github.com/noir-lang/noir.git +RUN curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash -WORKDIR noir +ENV PATH="$PATH:/root/.nargo/bin" -RUN git checkout tags/v0.3.2 +RUN noirup -v 1.0.0-beta.6 -RUN cargo install --locked --path=crates/nargo --no-default-features --features plonk_bn254 - -# RUN curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash - -# RUN cd /root/.nargo/bin/ && rm noirup && wget https://raw.githubusercontent.com/noir-lang/noirup/main/noirup && chmod +x noirup +RUN nargo --version -# ENV PATH="${PATH}:/root/.nargo/bin" +RUN curl -L https://raw.githubusercontent.com/AztecProtocol/aztec-packages/refs/heads/master/barretenberg/bbup/install | bash -# RUN noirup +ENV PATH="$PATH:/root/.bb" -RUN nargo --version +RUN bbup -v 0.87.0 # If you want to avoid downloading fresh Ignite SRS everytime, download it from http://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/sealed/transcript00.dat and inject into container. # COPY transcript00.dat /root/noir_cache/ignition/transcript00.dat diff --git a/PLONK/noir/hash/sha256/Nargo.toml b/PLONK/noir/hash/sha256/Nargo.toml index e0b467c..49a79dd 100644 --- a/PLONK/noir/hash/sha256/Nargo.toml +++ b/PLONK/noir/hash/sha256/Nargo.toml @@ -1,5 +1,8 @@ [package] +name = "sha256" +type = "bin" authors = [""] -compiler_version = "0.1" +compiler_version = ">=1.0.0" -[dependencies] \ No newline at end of file +[dependencies] +sha256 = { tag = "v0.1.4", git = "https://github.com/noir-lang/sha256" } diff --git a/PLONK/noir/hash/sha256/run.py b/PLONK/noir/hash/sha256/run.py index b2200a0..2148fbb 100755 --- a/PLONK/noir/hash/sha256/run.py +++ b/PLONK/noir/hash/sha256/run.py @@ -30,13 +30,26 @@ def echo(*string): def main(): print('Running benchmarks for sha256') + setup_time = 0 proof_time = 0 proof_size = 0 verify_time = 0 + + echo("Running preliminary checks...") + subprocess.check_call(["nargo", "check"]) + echo("Done") + + echo("Compiling circuit...") + subprocess.check_call(["nargo", "compile"]) + echo("Done") + for _ in range(BENCHMARK_ROUNDS): - echo("Running preliminary checks...") - subprocess.check_call(["nargo", "check"]) + echo("Generating vk...") + time_start = time.time() + subprocess.check_call(["bb", "write_vk", "-b", "./target/sha256.json", "-o", "./target"]) + time_duration = time.time() - time_start echo("Done") + setup_time += time_duration echo("Generating Prover.toml file with random circuit inputs...") msg = gen_random_bytes(HASH_INPUT_LEN // 8) @@ -48,26 +61,36 @@ def main(): open(prover_toml_path, "w").write(prover_toml) echo("Done") + echo("Performing witness generation...") + time_start = time.time() + subprocess.check_call(["nargo", "execute"]) + time_duration = time.time() - time_start + echo("Done") + proof_time += time_duration + echo("Generating proof...") time_start = time.time() - subprocess.check_call(["nargo", "prove", "p"]) + subprocess.check_call(["bb", "prove", "-b", "./target/sha256.json", "-w", "./target/sha256.gz", "-o", "./target"]) time_duration = time.time() - time_start echo("Done") proof_time += time_duration - proof_path = get_project_dir() / "proofs" / "p.proof" - proof_size = proof_path.stat().st_size + proof_path = get_project_dir() / "target" / "proof" + public_inputs_path = get_project_dir() / "target" / "public_inputs" + proof_size = proof_path.stat().st_size + public_inputs_path.stat().st_size echo("Verifying proof...") time_start = time.time() - subprocess.check_call(["nargo", "verify", "p"]) + subprocess.check_call(["bb", "verify", "-k", "./target/vk", "-p", "./target/proof", "-i", "./target/public_inputs"]) time_duration = time.time() - time_start echo("Done") verify_time += time_duration + avg_setup_time = (setup_time * 10**6) / BENCHMARK_ROUNDS avg_proof_time = (proof_time * 10**6) / BENCHMARK_ROUNDS avg_verify_time = (verify_time * 10**6) / BENCHMARK_ROUNDS - echo(f"(Setup +) Proof time: {avg_proof_time:.0f} us") + echo(f"Setup time: {avg_setup_time:.0f} us") + echo(f"Proof time: {avg_proof_time:.0f} us") echo(f"Proof size: {proof_size} B") echo(f"Verify time: {avg_verify_time:.0f} us") diff --git a/PLONK/noir/hash/sha256/src/main.nr b/PLONK/noir/hash/sha256/src/main.nr index 49a1417..229254d 100644 --- a/PLONK/noir/hash/sha256/src/main.nr +++ b/PLONK/noir/hash/sha256/src/main.nr @@ -1,6 +1,4 @@ -use dep::std; - fn main(msg: [u8; 64], known_hash: pub [u8; 32]) { - let calc_hash = std::hash::sha256(msg); - constrain calc_hash == known_hash; + let calc_hash = sha256::digest(msg); + assert_eq(calc_hash, known_hash); } diff --git a/PLONK/noir/matmult/Nargo.toml b/PLONK/noir/matmult/Nargo.toml index e0b467c..1c23aff 100644 --- a/PLONK/noir/matmult/Nargo.toml +++ b/PLONK/noir/matmult/Nargo.toml @@ -1,5 +1,7 @@ [package] +name = "matmult" +type = "bin" authors = [""] -compiler_version = "0.1" +compiler_version = ">=1.0.0" -[dependencies] \ No newline at end of file +[dependencies] diff --git a/PLONK/noir/matmult/out/vk b/PLONK/noir/matmult/out/vk new file mode 100644 index 0000000..6ef70ba Binary files /dev/null and b/PLONK/noir/matmult/out/vk differ diff --git a/PLONK/noir/matmult/run.py b/PLONK/noir/matmult/run.py index 6dd66ad..6630e4f 100755 --- a/PLONK/noir/matmult/run.py +++ b/PLONK/noir/matmult/run.py @@ -29,40 +29,63 @@ def echo(*string): def main(): print('Running benchmarks for matmult') + setup_time = 0 proof_time = 0 proof_size = 0 verify_time = 0 - for _ in range(BENCHMARK_ROUNDS): - echo("Running preliminary checks...") - subprocess.check_call(["nargo", "check"]) - echo("Done") + echo("Running preliminary checks...") + subprocess.check_call(["nargo", "check"]) + echo("Done") + + echo("Compiling circuit...") + subprocess.check_call(["nargo", "compile"]) + echo("Done") + + for _ in range(BENCHMARK_ROUNDS): # echo("Generating Prover.toml file with random circuit inputs...") # prover_toml = f"a = {array_a}\nb = {array_b}\nc = {array_c}\n" # prover_toml_path = get_project_dir() / "Prover.toml" # open(prover_toml_path, "w").write(prover_toml) # echo("Done") + echo("Generating vk...") + time_start = time.time() + subprocess.check_call(["bb", "write_vk", "-b", "./target/matmult.json", "-o", "./target"]) + time_duration = time.time() - time_start + echo("Done") + setup_time += time_duration + + echo("Performing witness generation...") + time_start = time.time() + subprocess.check_call(["nargo", "execute"]) + time_duration = time.time() - time_start + echo("Done") + proof_time += time_duration + echo("Generating proof...") time_start = time.time() - subprocess.check_call(["nargo", "prove", "p"]) + subprocess.check_call(["bb", "prove", "-b", "./target/matmult.json", "-w", "./target/matmult.gz", "-o", "./target"]) time_duration = time.time() - time_start echo("Done") proof_time += time_duration - proof_path = get_project_dir() / "proofs" / "p.proof" - proof_size = proof_path.stat().st_size + proof_path = get_project_dir() / "target" / "proof" + public_inputs_path = get_project_dir() / "target" / "public_inputs" + proof_size = proof_path.stat().st_size + public_inputs_path.stat().st_size echo("Verifying proof...") time_start = time.time() - subprocess.check_call(["nargo", "verify", "p"]) + subprocess.check_call(["bb", "verify", "-k", "./target/vk", "-p", "./target/proof", "-i", "./target/public_inputs"]) time_duration = time.time() - time_start echo("Done") verify_time += time_duration + avg_setup_time = (setup_time * 10**6) / BENCHMARK_ROUNDS avg_proof_time = (proof_time * 10**6) / BENCHMARK_ROUNDS avg_verify_time = (verify_time * 10**6) / BENCHMARK_ROUNDS - echo(f"(Setup +) Proof time: {avg_proof_time:.0f} us") + echo(f"Setup time: {avg_setup_time:.0f} us") + echo(f"Proof time: {avg_proof_time:.0f} us") echo(f"Proof size: {proof_size} B") echo(f"Verify time: {avg_verify_time:.0f} us") diff --git a/PLONK/noir/matmult/src/main.nr b/PLONK/noir/matmult/src/main.nr index bba5f74..60d7f29 100644 --- a/PLONK/noir/matmult/src/main.nr +++ b/PLONK/noir/matmult/src/main.nr @@ -1,14 +1,14 @@ -fn main(a : [Field; 1024], b : [Field; 1024], c : pub [Field; 1024]) { +fn main(a: [Field; 1024], b: [Field; 1024], c: pub [Field; 1024]) { let mut temp_field = [0; 1024]; for i in 0..32 { for j in 0..32 { for k in 0..32 { - temp_field[i*32 + j] += temp_field[i*32 + j] + a[i * 32 + k] * b[k * 32 + j]; + temp_field[i * 32 + j] += temp_field[i * 32 + j] + a[i * 32 + k] * b[k * 32 + j]; } } } for i in 0..1024 { - constrain c[i] == temp_field[i]; + assert_eq(c[i], temp_field[i]); } -} \ No newline at end of file +}