227 Commits

Author SHA1 Message Date
Alex Ozdemir
4865ce6b93 Special-case multiply-by-constant in R1CS lowering
Previously, to lower z = x * y, we always generated a fresh variable for
z. If a or b were constant, then R1CS optimization killed the fresh
variable.

Now, if a or b are constant, we skip the fresh variable, and just scale
the LC.
2023-03-27 16:11:07 -07:00
Alex Ozdemir
274d817de7 Inline small elements in FieldV. (#156)
reduces memory usage during R1CS lowering by ~40%.
2023-03-21 11:16:28 -07:00
algoidan
f4245ebd0e convert array::store -> tuple::field (#157) 2023-03-21 11:06:02 -07:00
Alex Ozdemir
ba4043f05d add fn: dump_op_stats
slightly different than on master b/c of the different HC
implementation.
2023-03-16 10:59:39 -07:00
Alex Ozdemir
70bd9f18a7 cache zsharp::term::unwrap_array_ir 2023-02-28 09:46:45 -08:00
Alex Ozdemir
1fd95f0313 Cache pf_lit_ir 2023-02-28 09:46:45 -08:00
Alex Ozdemir
e71cac11a6 Op::Array 2023-02-28 09:46:40 -08:00
Alex Ozdemir
1cfbf87cfa Optimize garbage collection 2023-02-23 16:36:36 -08:00
Alex Ozdemir
33747dde61 Add zxc & zxe example 2023-02-08 14:24:01 -08:00
Alex Ozdemir
006fe80520 Merge branch 'master' into zsharp 2023-01-06 09:33:32 -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
c95b92f60e Revert "Bugfixes: R1CS lowering (includes a soundness bugfix) (#124)"
This reverts commit 05b793d565.

Right now Z#'s represents field comparisons and remainders
using 255-bit bit-vectors. Our field-blaster is not intended to be
correct or sound for bit-vectors that are this wide. Previously, a bug
in the field-blaster essentially allowed compilation and proving to
succeed anyway.

With the (reverted) bugfix, the field-blaster crashes.

Ultimately, we want Z# to handle these operations using
range-checks---not bit-vectors. So, I'm reverting the bugfix for now.
2022-12-20 10:00:53 -08:00
Alex Ozdemir
f9bd0d2191 add tests that detect the issue 2022-12-20 10:00:45 -08:00
Alex Ozdemir
3804dfd431 trace: print computation between opts 2022-12-19 19:59:32 -08:00
Alex Ozdemir
093000b868 Merge branch 'master' into zsharp-master-merge 2022-12-19 09:36:51 -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
466dc9586c Merge branch 'master' into zsharp-try-merge 2022-12-13 09:57:18 -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
kwantam
ca80064002 documentation on pub members 2022-10-27 12:39:17 -04:00
kwantam
d5c4381c1b ZoK: DecimalNumber should store value as String 2022-10-27 12:39:17 -04:00
kwantam
3add8910ed make R1cs and Lc members pub 2022-10-27 12:39:17 -04:00
kwantam
1cf941cc9f zxe: write output as a value map 2022-10-27 12:39:17 -04:00
kwantam
e1866ab9b7 fmt 2022-10-27 12:39:16 -04:00
kwantam
a4dab252b4 zxe: evaluate precomp and print results 2022-10-27 12:38:18 -04:00
kwantam
df84fa78e0 zxc: write both prover_data and verifier_data 2022-10-27 12:38:17 -04:00
kwantam
466e04ed48 to_r1cs memory savings, zxc cleanup
to_r1cs used to return two copies of the r1cs, one inside ProverData and one directly.
This wastes memory with essentially no upside, as far as I can tell. I modified to_r1cs
to return just the ProverData and the VerifierData.

I also pulled the common code from R1cs::prover_data and R1cs::verifier_data into a
function that both of those functions now call.

Finally, cleaned up zxc to actually write the r1cs out (either as bincode or json).
2022-10-27 12:37:33 -04:00
kwantam
4cbad187cf note 2022-10-27 12:28:18 -04:00
kwantam
30f2bce615 add ability to dump value map from consts 2022-10-27 12:28:18 -04:00
kwantam
439973f241 tiny cleanup 2022-10-27 12:28:18 -04: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
Edward Chen
fbbef279e0 Refactored C Frontend to use helper functions to interact with Circify methods (#112)
- Wrapped Circify behind a RefCell
- refactored methods to Circify
2022-10-18 13:04:21 -04:00
Edward Chen
a6bedd4f2d Boilerplate for C AstWalker (#109) 2022-10-18 10:14:03 -04:00
Evan Laufer
84b2f33e5f Update pest parsers to use PrattParser (#108)
pest::prec_climber has been deprecated.
also, fix some clippy lints
2022-10-13 11:25:46 -07:00
Edward Chen
e2edbadd0d Clone audit of the C frontend (#106) 2022-10-12 11:55:44 -07:00
Edward Chen
30c88d55f3 Rotation IR intrinsic for SIMD FHE operations (#107) 2022-10-09 00:11:17 -04:00
Alex Ozdemir
c1bac11486 Broader SHA bitwise ITE optimization (#104)
It detects bitwise BV ITEs expressed using and XOR/OR of ANDs with a
NOT. Now it triggers more often.

This cuts a few percentage points (~10%) worth of constraints on ZoK's
shaRound circuit.
2022-09-12 17:20:20 +02:00
Alex Ozdemir
f6c37b505b R1CS scalability bugfix (#102)
Reduce Restrict::restrict_to_inputs from n*n to n

It needs to compute the set of terms which only on inputs in S.
It did this via *separate* containment checks for each term.
But, of course, there is a simple graph traversal that does O(1) work
per term.
I'm taking this patch now b/c I need it. All credits to Evan.
It passes our ZK tests, and a wrote a few unit tests.

Co-authored-by: Evan Laufer <evan.m.laufer@gmail.com>
2022-09-12 16:56:19 +02:00
Alex Ozdemir
15eadb6110 Fix Linter Warnings (#103) 2022-09-12 16:04:23 +02:00
Alex Ozdemir
4686854248 fmt (#98) 2022-08-12 07:32:57 -04:00
Alex Ozdemir
05ca4eda11 bv uext in smt output (#97) 2022-08-12 07:07:34 -04:00