Commit Graph

237 Commits

Author SHA1 Message Date
Alex Ozdemir
4c5dafee95 A Waksman-based RAM permutation argument (#171)
This allows folks to use the RAM machinery while sticking with (non-interactive) R1CS output.

We're going to need this anyway when we benchmark our new approach.
2023-09-19 02:39:24 -07:00
Alex Ozdemir
64dcc18175 Improve RAM: oblivious & volatile (#170)
* Improve the oblivious RAM pass by killing the hack where we treat selects as arrays.
* Fix a bug where the volatile RAM pass would not place selects before stores against the same array
* Improve that volatile RAM pass by placing selects against the same array literal in the same RAM. Before, they would each end up in different RAMs, which sucks. This is especially bad for ROMs.
2023-09-18 11:07:19 -07:00
Alex Ozdemir
fb198eeadd ram structure example (#169) 2023-09-14 13:34:27 -07:00
Alex Ozdemir
a586f7f95d GC after each optimization (#167)
Flag: `--ir-frequent-gc true`

Reduces memory usage on Jess & Eli's pgm from 398MB to 285MB.
2023-08-11 16:37:46 -07:00
Alex Ozdemir
d7217e559e Optimize GC (#166)
Previously, a GC call would scan the HC table, identifying dead nodes.

Now, whenever a Node is destroyed, if it's ready to be GC'd, it gets added to a list. That list is used at GC time instead of the scan.

This substantially decreases the cost of running GC many times (as the Z# FE does).
2023-07-09 14:45:14 -07:00
Alex Ozdemir
bf3d9c601e Remove dead r1cs vars after opt (#164)
We were already removing those that are explicitly eliminated, but they
can become dead in other ways too.
2023-06-22 19:44:35 -07:00
Alex Ozdemir
079eea1ae9 r1cs: fix boolean majority (#163)
* r1cs: fix boolean majority

for m = majority(a,b,c),
we were lowering

    (-ab + bc + ac - 2abc)

instead of

    (ab + bc + ac - 2abc)

Whoops! This is a functional correctness bug: we were lowering
soundly and completely, for the wrong specification.

Credit to Anna for raising the issue!

* add test_sha256 (commented out b/c it is slow)

* fmt + lint
2023-06-14 15:14:05 -07:00
Edward Chen
bd9cec31fb Replaced third party dependencies with binaries to reduce CI build times (#162)
To reduce CI build time:
- Replaced ABY dependency with corresponding binary.
- Removed dependencies on KaHIP and KaHyPar for now because these dependencies aren't used upstream.
Minor updates:
- Updated ABY source to Public branch
Note: 
- The aby_interpreter binary will only work on Linux. We can rebuild the binary from this repo.
2023-06-14 14:58:39 -04:00
Alex Ozdemir
961fda6fb3 Fix bug in r1cs lowering for bv cmps (#161)
We called the wrong function...
This is a soundness bug.

Credit to Anna for reporting this.
2023-06-14 09:53:10 -07:00
Alex Ozdemir
e404c13468 Access array without it cloning it. (#160)
All credits to Evan for finding this.

Co-authored-by: Evan Laufer <evan.m.laufer@gmail.com>
2023-05-26 15:33:05 -07:00
Alex Ozdemir
18990d079e Inline small elements in FieldV. (#156)
reduces memory usage during R1CS lowering by ~40%.
2023-03-20 08:59:23 -07:00
Edward Chen
2e2bf85399 Linker Optimization Pass Documentation clean-up (#155) 2023-03-15 22:27:25 -04:00
Alex Ozdemir
706405fd4f Committed witnesses & randomness in Z# (& tests) (#154)
A basic implementation of committed witnesses & volatile RAM extraction in the Z# front-end.

The passes in question are still a bit brittle, so I left them behind a flag.
2023-03-15 16:28:19 -07:00
Edward Chen
a49e03abfb Linker Optimization Pass clean-up (#153) 2023-03-15 15:21:19 -04:00
Edward Chen
06a34c8cc5 Linker Optimization Pass (#152) 2023-03-15 15:03:51 -04:00
Alex Ozdemir
cd1be1ea1c Optimizations for interaction, randomness and RAM (#151)
List of optimizations:
* challenge skolemization
* condition store parsing/expansion
* volatile RAM extraction
* persistent RAM extraction
* RAM checking (for both)
2023-03-14 11:15:42 -07:00
Alex Ozdemir
450c37b896 Add extension operators and new operators (#150)
modifies some opt passes w/ new ops
2023-03-14 01:09:46 -07:00
Alex Ozdemir
155794c2bf Committed witnesses, CP link implementation (#149) 2023-03-13 14:39:35 -07:00
Alex Ozdemir
ad11eb311e Update dependencies (#148)
Clears sec advisory on a transitive dep: remove_dir_all. It was
(transitively) dead anyways.
2023-03-09 15:54:24 -08:00
Edward Chen
bb8e6e3673 KaHIP and kahypar binaries for MPC backend (#147)
- Upgraded ci pipeline to [v3](https://github.com/actions/cache/blob/main/README.md)
- Included installation and build scripts  for KaHIP and kahypar in driver.py
- Used absolute paths for caching in ci pipeline (relative paths don't work). 

Average ci time brought down from 15 minutes to 8 minutes!
2023-02-27 12:31:35 -05:00
Alex Ozdemir
77e7040047 Mirage, variable rounds, randomness, zk opt (#146)
Adds:

    an implementation of the Mirage proof system
        generalized to multiple round of interaction
    a notion of rounds for variables
    a notion of randomness for variables
    to the R1CS layer:
        committed witnesses
        rounds
    new witness computation machinery (to support multiple rounds)
2023-02-06 09:58:19 -08:00
Alex Ozdemir
c900b79afb challenge operator (#145) 2023-01-29 21:09:34 -08:00
Alex Ozdemir
e4d19b358a Operator statistics (#144) 2023-01-29 19:39:02 -08:00
Alex Ozdemir
63f950a795 eval_op (#143)
separate evaluation from terms

its now a fn(operator, child values) -> value
2023-01-29 19:38:51 -08:00
Alex Ozdemir
8608e03e90 Option to hide all fields when printing IR (#142) 2023-01-29 19:16:42 -08:00
Edward Chen
c06e938c41 Introducing SV Comp-style tests for C Frontend (#140)
Integrating SV comp tests for C Frontend

---------

Co-authored-by: Alex Ozdemir <aozdemir@hmc.edu>
2023-01-29 21:30:55 -05:00
Edward Chen
20b91a6601 Clippy: uninlined_format_args (#141) 2023-01-29 21:05:15 -05:00
Alex Ozdemir
5e36d4ba03 features: add bellman, spartan, aby (#139)
Also, add ./driver --check-all, which checks every single-feature build.
2023-01-19 00:13:09 -08:00
Alex Ozdemir
d11b92ae0f Add rounds & challenges to Computation (#138) 2023-01-18 12:03:26 -08:00
Alex Ozdemir
d15db74f04 change metadata serialization & representation (#137)
In the new approach, all variable metadata is stored per-variable.

This makes it easier to add new kinds of metadata, and to serialize that metadata.
2023-01-18 11:22:43 -08:00
Alex Ozdemir
b28123115b lazy debitification in r1cs::trans (#136)
Before, whenever we constructed a bit-wise encoding of a BV, we also
debitified it into a UINT encoding.

Now, we do the debitification on-demand. This saves ~10-15% of compilation
time.

While debitification is free from the perspective of R1CS, it is costly
for compilation time.

I measured compile time for the sha2 round function.

```terminal
Benchmark #1: ./circ-old third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs
  Time (mean ± σ):      1.219 s ±  0.019 s    [User: 1.145 s, System: 0.071 s]
  Range (min … max):    1.180 s …  1.240 s    10 runs

Benchmark #2: ./circ-new third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs
  Time (mean ± σ):      1.055 s ±  0.024 s    [User: 987.1 ms, System: 65.9 ms]
  Range (min … max):    1.026 s …  1.094 s    10 runs

Summary
  './circ-new third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs' ran
    1.15 ± 0.03 times faster than './circ-old third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs'
```
2023-01-17 10:58:41 -08:00
Alex Ozdemir
62ee97b240 missed a datalog feature (#135) 2023-01-17 09:49:42 -08:00
Alex Ozdemir
5f3d2b95fe Split hash-consing into its own crate & reimplement (#134)
I split the hash-consing implementation into its own crate and re-implemented it.

In the new implementation, the table is thread-local. Terms are not Send, but linear terms are.

Decreased used of atomics gives a non-trivial speed-up. I tested on Z#'s sha2 round function, with an exit after IR optimization:

Benchmark #1: ./circ-old third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs
  Time (mean ± σ):     236.2 ms ±  16.1 ms    [User: 223.3 ms, System: 12.4 ms]
  Range (min … max):   221.1 ms … 264.1 ms    11 runs

Benchmark #2: ./circ-new third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs
  Time (mean ± σ):     141.8 ms ±  13.1 ms    [User: 131.3 ms, System: 10.0 ms]
  Range (min … max):   125.4 ms … 160.4 ms    18 runs

Summary
  './circ-new third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs' ran
    1.67 ± 0.19 times faster than './circ-old third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/sha256/shaRound.zok r1cs'
2023-01-17 02:17:58 -08:00
Alex Ozdemir
c4cfa50cf9 retarget bellman dependency to our fork (mirage impl) (#132) 2023-01-12 12:13:21 -08:00
Alex Ozdemir
500b9157f4 Upload Braun's thesis & fix link (#133) 2023-01-12 12:04:01 -08:00
Alex Ozdemir
838b8a6791 Include Anna's test cases and fix (#131)
The first bug is a disagreement between our bellman circuit and our bellman
verify function. The circuit omit unused variables (public or private). The
verify function includes unused public variables.

Now, the circuit includes unused public variables.

The second bug is related to large BV comparisons. The fix is to emit a
bitwise comparator. We could optimize further in the future.

closes #125
2023-01-09 23:00:35 -08:00
Alex Ozdemir
ebfd6853d3 Avoid R1CS copy (#130)
Co-authored-by: Riad S. Wahby <rsw@jfet.org>
2023-01-06 09:11:27 -08:00
Alex Ozdemir
1a1cdc5858 Add option to panic on OOB PfToBv (#129) 2023-01-05 19:57:04 -08:00
Alex Ozdemir
f9bedf0787 Alternative formatter (#128)
* Configuration system. Kill DFL_T

* add circ::cfg::CircCfg that holds cfg info
   * it's constructible from circ_opt::CircOpt
      * implements clap::Args, so you can set it from your compiler's
        CLI/envvars
      * defined in external crate to keep clap out of our main build
      * organized by circ module, but not feature gated
         *  no point: the build wouldn't meaningfully change
      * includes a way to set the default field
* added circ::cfg::set and circ::cfg::cfg
   * also circ::cfg::set_default and circ::cfg::set_cfg
   * access a sync::once_cell, static configuration
* killed DFL_T

* workflows
   * unit-tested component probably need to not read circ::cfg::cfg.
   * compilers need to call circ::cfg::set or circ::cfg::set_default.

* rm dead features

* WIP

* Alternate formatting machinery.

* fmt & lint

* rm Letified

* ilp
2023-01-03 21:51:28 -08:00
Alex Ozdemir
be8741c615 Configuration system. Kill DFL_T (#127)
* Configuration system. Kill DFL_T

* add circ::cfg::CircCfg that holds cfg info
   * it's constructible from circ_opt::CircOpt
      * implements clap::Args, so you can set it from your compiler's
        CLI/envvars
      * defined in external crate to keep clap out of our main build
      * organized by circ module, but not feature gated
         *  no point: the build wouldn't meaningfully change
      * includes a way to set the default field
* added circ::cfg::set and circ::cfg::cfg
   * also circ::cfg::set_default and circ::cfg::set_cfg
   * access a sync::once_cell, static configuration
* killed DFL_T

* workflows
   * unit-tested component probably need to not read circ::cfg::cfg.
   * compilers need to call circ::cfg::set or circ::cfg::set_default.

* rm dead features
2022-12-25 20:53:27 -08:00
Alex Ozdemir
05b793d565 Bugfixes: R1CS lowering (includes a soundness bugfix) (#124)
Fixes 4 bugs in R1CS lowering:
* division-by-zero in finite fields: previously, this always caused
  incompleteness. Now, the behavior depends on the CIRC_RELAXATION
  envvar:
  * CIRC_RELAXATION=incomplete: divide-by-0 causes incompletenes
  * CIRC_RELAXATION=nondet: divide-by-0 allows the output to take any value
  * CIRC_RELAXATION=det: divide-by-0 forces the output to 0
* bit-vector overshift: previously, this cause incompleteness. Now,
  we follow the semantics of SMT-LIB (the result saturates).
* bit-vector udiv-by-zero: previously, the output value was an
  unconstrained bit-vector. Now, it is MAX (following SMT-LIB).
* bit-vector comparisons: previously, the prover could lie, claiming
  that x < y when actually x >= y. All bit-vector comparisons are
  affected, including those in udiv and urem.
  * soundness bug!
2022-12-19 09:35:57 -08:00
Alex Ozdemir
65e8bd1de9 Better serde impl for bellman keys (#122)
* Better serde impl for bellman keys

Bellman keys (pk and vk) expose non-trait read and write functions for
interacting with `io::{Read,Write}`. They don't expose serde impls.
We need to serde them.

Previously, we used the io interface to convert from/to a `Vec<u8>`,
whose serde impl we used.

This was a bad idea b/c the serde impl for `Vec<u8>` is bad. It gets
stuck with the generic serde impl for `Vec`, rather than using
`(de)serialize_bytes`.

Now, we have serde-impl'd wrapper types for pk and vk that do the right
thing.

By my measurements, this decreases the cost of serializing a pk by about
84%.

* serde_bytes
2022-12-10 16:55:18 -08:00
Alex Ozdemir
052fff8f24 Slightly better serde implementation for Term (#123)
* Slightly better serde implementation for Term

Also, for data-structures thereof.

Crediting Colin, b/c this is based on his PR.

Co-Authored-By: Collin Zhang <collinzrj@gmail.com>
2022-12-10 16:29:32 -08:00
Riad S. Wahby
78c5d10fb2 bump deps to avoid yanked packages ; fix some clippy lints in circ_fields (#121) 2022-11-17 11:21:19 -08:00
Alex Ozdemir
051809a554 Better serde for Computation, PreComp, R1cs (#120)
The previous implementation for all 3 structures would separately serialize each term in a precomputation. This was very bad, because precomputations can be quite large, and the different terms typically share large numbers of sub-terms. This can be quadratic blowup.

Change list:

    add input/output type metadata to PreComp (this makes the type of variables in a PreComp clear)
    add textual serialization and parsing for PreComp (and include this in Computation)
    add serde implementations for Computation and PreCop based on the text formats.
    modify the R1cs serde derive so that the Vec<Term> is tuplized before serde

This reduces our prover-key size for verifyEddsa.zok from wildly large (>450GB) to 171MB.

Collin and Anna first observed this bug. Collin's PR #118 works around it (among other improvements).
2022-11-08 15:47:51 -08:00
Jess Woods
25773910e5 Spartan Backend (#70) 2022-11-07 10:11:58 -08:00
Edward Chen
90f6b2ff35 Unsigned (#115)
Support `unsigned` in the C frontend
2022-11-01 23:46:08 -04:00
Edward Chen
1290ad09f8 Function Abstraction, end-to-end testing with MPC backend (#114)
Updated MPC unit tests to use function abstractions. Most of these updates are made to the C frontend and MPC lowering pass. Additional optimization and ILP solving passes need to be updated.

The assignment and cost model passes now need to factor in Array and Tuple semantics. This requires changes to the CostModel assignments. This also requires the the def-use-graph implementation for constant-index propagation.

Work-around:
- disabled HyCC benchmark tests
- disabled tests that use Op::Select / Op::Store
2022-10-31 15:33:36 -04:00
Alex Ozdemir
a8f512aff3 Improve conditional matrix mult with 3 opts (#116)
We received a benchmark from Lef which essentially checked the
following:

   (cond ? A*B : A*C)[i] == v

where A, B, C are 4 by 4 matrices, cond is a bool, i is an index, and v
a value.

The expected number of constraints is n^3: order 64 , but compile time
and constraint count were in the thousands.

I investigated finding a number of things:
* the Z# front-end entered a conditional branch for the ternary, causing
  the MM to happen in a conditional context
* while the MM has an oblivious access pattern, since `i` is variable,
  the oblivious pass wasn't really helping
* there were some weird non-constant indices floating around like
  (ite cond 0 0)

The three optimizations fix these problems:
* If the Z# frontend isn't in assertion isolation mode, don't enter a
  conditional branch for an ITE
* Constant folding: add the rewrite (ite cond a a) -> a
* Linear-scan array eliminator: for each operation, check if the index
  is constant. If so, skip the scan (for that access only)
2022-10-27 13:02:44 -07:00
Edward Chen
f1dbab65ab Function Abstractions (#113)
Initial commit for support function abstractions.

`Computations` are maps of function names `str` to computation `Computation`. 

Updates:
- Frontend `Gen` returns `Computations` with only a single main `Computation`.
- Optimizations take in `Computations` instead of a single `Computation`. Optimizations are applied to each `Computation`. 
- Backends take in only the main `Computation`.
2022-10-18 21:27:58 -04:00