From 7dde918f2cbda8f266fcdf46e910b66ed6c0a9c9 Mon Sep 17 00:00:00 2001 From: Xuyang Song Date: Thu, 14 Nov 2024 16:13:54 +0800 Subject: [PATCH] add circuit benchmark --- .formatter.exs | 2 +- README.md | 27 ++++++++---------- bench/compliance_circuit_bench.exs | 42 ++++++++++++++++++++++++++++ bench/logic_circuit_bench.exs | 44 ++++++++++++++++++++++++++++++ mix.exs | 3 +- mix.lock | 3 ++ 6 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 bench/compliance_circuit_bench.exs create mode 100644 bench/logic_circuit_bench.exs diff --git a/.formatter.exs b/.formatter.exs index 97ae23d..d68ef22 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,5 +1,5 @@ # Used by "mix format" [ - inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], + inputs: ["{mix,.formatter}.exs", "{config,lib,test,bench}/**/*.{ex,exs}"], line_length: 78 ] diff --git a/README.md b/README.md index 56e8788..2b6aa3f 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,19 @@ -# Cairo +**TODO: Add description** + +# Cairo API Doc [CairoProver Doc](./native/cairo_prover/README.md) [CairoVM Doc](./native/cairo_vm/README.md) -**TODO: Add description** - -## Installation - -If [available in Hex](https://hex.pm/docs/publish), the package can be installed -by adding `cairo` to your list of dependencies in `mix.exs`: +# Benchmark -```elixir -def deps do - [ - {:cairo, "~> 0.1.0"} - ] -end +## Complaince circuit +```bash +mix run bench/compliance_circuit_bench.exs ``` -Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) -and published on [HexDocs](https://hexdocs.pm). Once published, the docs can -be found at . +## Logic circuit +```shell +mix run bench/logic_circuit_bench.exs +``` diff --git a/bench/compliance_circuit_bench.exs b/bench/compliance_circuit_bench.exs new file mode 100644 index 0000000..b3491e4 --- /dev/null +++ b/bench/compliance_circuit_bench.exs @@ -0,0 +1,42 @@ +{:ok, program} = File.read("./native/cairo_vm/compliance.json") +{:ok, input} = File.read("./native/cairo_vm/compliance_input.json") + +{_output, trace, memory, public_inputs} = + Cairo.cairo_vm_runner( + program, + input + ) + +# Prove and verify +{proof, public_input} = Cairo.prove(trace, memory, public_inputs) +# Cairo.verify(proof, public_input) + +Benchee.run( + %{ + "compliance circuit: run cairo vm" => fn -> + Cairo.cairo_vm_runner(program, input) + end + }, + warmup: 1, + time: 10 +) + +Benchee.run( + %{ + "compliance circuit: prover" => fn -> + Cairo.prove(trace, memory, public_inputs) + end + }, + warmup: 1, + time: 10 +) + +Benchee.run( + %{ + "compliance circuit: verifier" => fn -> + Cairo.verify(proof, public_input) + end + }, + warmup: 1, + time: 10 +) diff --git a/bench/logic_circuit_bench.exs b/bench/logic_circuit_bench.exs new file mode 100644 index 0000000..3c7a0df --- /dev/null +++ b/bench/logic_circuit_bench.exs @@ -0,0 +1,44 @@ +{:ok, program} = File.read("./native/cairo_vm/trivial_resource_logic.json") + +{:ok, input} = + File.read("./native/cairo_vm/trivial_resource_logic_input.json") + +{_output, trace, memory, public_inputs} = + Cairo.cairo_vm_runner( + program, + input + ) + +# Prove and verify +{proof, public_input} = Cairo.prove(trace, memory, public_inputs) +# Cairo.verify(proof, public_input) + +Benchee.run( + %{ + "logic circuit: run cairo vm" => fn -> + Cairo.cairo_vm_runner(program, input) + end + }, + warmup: 1, + time: 10 +) + +Benchee.run( + %{ + "logic circuit: prover" => fn -> + Cairo.prove(trace, memory, public_inputs) + end + }, + warmup: 1, + time: 10 +) + +Benchee.run( + %{ + "logic circuit: verifier" => fn -> + Cairo.verify(proof, public_input) + end + }, + warmup: 1, + time: 10 +) diff --git a/mix.exs b/mix.exs index b13ca2e..583d3fa 100644 --- a/mix.exs +++ b/mix.exs @@ -25,7 +25,8 @@ defmodule Cairo.MixProject do {:dialyxir, "~> 1.3", only: [:dev], runtime: false}, {:typed_struct, "~> 0.3.0"}, {:ex_doc, "~> 0.31", only: [:dev], runtime: false}, - {:rustler, "~> 0.31.0"} + {:rustler, "~> 0.31.0"}, + {:benchee, "~> 1.0", only: :dev} # {:dep_from_hexpm, "~> 0.3.0"}, # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} ] diff --git a/mix.lock b/mix.lock index e4f8ff8..e5f6eee 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,6 @@ %{ + "benchee": {:hex, :benchee, "1.3.1", "c786e6a76321121a44229dde3988fc772bca73ea75170a73fd5f4ddf1af95ccf", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "76224c58ea1d0391c8309a8ecbfe27d71062878f59bd41a390266bf4ac1cc56d"}, + "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"}, "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, @@ -9,6 +11,7 @@ "makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, "rustler": {:hex, :rustler, "0.31.0", "7e5eefe61e6e6f8901e5aa3de60073d360c6320d9ec363027b0197297b80c46a", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "99e378459bfb9c3bda6d3548b2b3bc6f9ad97f728f76bdbae7bf5c770a4f8abd"}, + "statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"}, "toml": {:hex, :toml, "0.7.0", "fbcd773caa937d0c7a02c301a1feea25612720ac3fa1ccb8bfd9d30d822911de", [:mix], [], "hexpm", "0690246a2478c1defd100b0c9b89b4ea280a22be9a7b313a8a058a2408a2fa70"}, "typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"}, }