Commit Graph

118 Commits

Author SHA1 Message Date
Jack Grigg bb1ed8288a Set edition to 2021
We also set `resolver = "2"` on the workspace; this is the default for
the root package in Rust 2021, but as we use a virtual workspace we need
to explicitly set it instead.
2022-04-27 12:28:19 +00:00
Jack Grigg 7688c371f6 Bump MSRV to 1.56.1
Closes zcash/halo2#482.
2022-04-27 12:24:57 +00:00
parazyd a6d7785ddc
plonk: Derive Clone for VerifyingKey and ProvingKey.
Signed-off-by: parazyd <parazyd@dyne.org>
2022-04-26 14:08:11 +02:00
str4d 66b2b3ba7e
Merge pull request #414 from zcash/constraints-helper
Add a `Constraints` helper
2022-04-22 11:52:06 +02:00
Sean Bowe a02b9e2e7e
Add benchmark for various FFT sizes. 2022-04-20 13:09:58 -06:00
Jack Grigg d93846f8fd Note that `Constraints::with_selector` accepts arrays from 1.53 2022-04-20 10:55:55 +00:00
Jack Grigg 78de8a5c94 Add a `Constraints` helper
There are two existing patterns for constructing a gate from a set of
constraints with a common selector:

- Create an iterator of constraints, where each constraint includes the
  selector:
  ```
  vec![
      ("foo", selector.clone() * foo),
      ("bar", selector.clone() * bar),
      ("baz", selector * bar),
  ]
  ```
  This requires the user to write O(n) `selector.clone()` calls.

- Create an iterator of constraints, and then map the selector in:
  ```
  vec![
      ("foo", foo),
      ("bar", bar),
      ("baz", bar),
  ].into_iter().map(move |(name, poly)| (name, selector.clone() * poly))
  ```
  This looks cleaner overall, but the API is not as intuitive, and it
  is messier when the constraints are named.

The `Constraints` struct provides a third, clearer API:
```
Constraints::with_selector(
    selector,
    vec![
        ("foo", foo),
        ("bar", bar),
        ("baz", bar),
    ],
)
```
This focuses on the structure of the constraints, and handles the
selector application for the user.
2022-04-20 10:55:50 +00:00
str4d 46ba444169
Merge pull request #480 from zcash/477-mockprover-pretty-failures
Add `MockProver::assert_satisfied` with pretty-printed failures
2022-04-20 12:53:16 +02:00
str4d 69c138c25c
Clarify some comments and messages
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2022-04-19 16:04:19 +02:00
str4d 606afb8349
Merge pull request #445 from daira/mockprover-regression
Fix mock prover performance regression for lookup arguments
2022-04-19 14:17:17 +02:00
Daira Hopwood 424a2748d1
Clarify a comment 2022-04-19 12:36:02 +01:00
str4d 221766986b
Merge pull request #532 from zcash/bench-lookup-mockprover
Bench heavily padded lookup in MockProver.
2022-04-18 12:45:30 +02:00
str4d 19b2b3b7e2
Fix clippy lints 2022-04-18 12:25:58 +02:00
ying tong 066bd15d7e
cost-model.rs: Correct lookup required degree calculation. 2022-04-14 16:25:52 +02:00
Jack Grigg 90e671e77c Relicense Halo 2 crates as MIT OR Apache 2.0
See this blog post for details:
    https://electriccoin.co/blog/zero-knowledge-proving-system-halo-now-licensed-under-mit-making-it-available-for-anyone-to-use/
2022-04-07 14:22:49 +00:00
therealyingtong a11cb9796e halo2_proofs 0.1.0-beta.4 2022-04-06 12:24:28 +08:00
Sean Bowe 6a31a0e6a1
Apply @str4d's review suggestions. 2022-04-04 14:07:31 -06:00
Sean Bowe fa069a7455
Use unwrap_or_default() instead of unwrap_or(HashMap::new()) 2022-04-03 10:06:19 -06:00
Sean Bowe fd7e9ddbb0
rustfmt 2022-04-02 15:38:46 -06:00
Sean Bowe 4163b8765a
Reduce depth of AST by special casing the application of Horner's rule.
The existing code will fold together a very deep AST that applies Horner's
rule to each gate in a proof -- which could include multiple circuits and
so for some applications will quickly grow such that when we recursively
descend later during evaluation the stack will easily overflow.

This change special cases the application of Horner's rule to a
"DistributePowers" AST node to keep the tree depth from exploding in size.
2022-04-02 13:13:46 -06:00
Jack Grigg 0946bdb455 dev: Enable `VerifyFailure::Permutation` to point to region offsets 2022-03-30 01:39:50 +00:00
therealyingtong 51d34c12a2 Bench heavily padded lookup in MockProver.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2022-03-24 22:02:29 +08:00
Jack Grigg 8acd4abfb3 halo2_proofs 0.1.0-beta.3 2022-03-22 19:59:10 +00:00
Jack Grigg e39c8e94d2 Update changelogs 2022-03-22 19:55:53 +00:00
str4d 8abd7b74db
Merge pull request #417 from zcash/fix-assigned-usage
Expand `Assigned<F>` APIs
2022-03-22 19:46:51 +00:00
str4d 642efc1536
Merge pull request #521 from zcash/reconstruct-selectors
Remove selector_map from pinned verification key and remove VerificationKey serialization
2022-03-18 00:14:36 +00:00
Jack Grigg c6b4fcaf34 Fix docs.rs build
The published source code for each package needs to include the required
header file, and the path to that header file needs to be relative to
the package source (not the repository source). We therefore need to
have the header file present in each workspace package.

Closes zcash/halo2#506.
2022-03-17 19:14:11 +00:00
Sean Bowe f46d77763e
Remove logic for reading and writing VerificationKey to/from buffers. 2022-03-16 14:19:33 -06:00
Sean Bowe 819bc3c2f5
Stop placing the selector_map (which is an internal API detail) in the pinned verification key. 2022-03-16 14:19:13 -06:00
Sean Bowe e10f4e1d0e
Add mechanism for generating a new proof in test. 2022-03-16 12:53:04 -06:00
Daira Hopwood b48b032041
Minor simplification 2022-02-16 17:14:41 +00:00
Jack Grigg 57596cab36 dev: Add a custom `VerifyFailure::CellNotAssigned` emitter
The `dev::tests::unassigned_cell` test case, shown via `assert_eq!(err, Ok(()))`:
```
  left: `Err([CellNotAssigned { gate: Gate { index: 0, name: "Equality check" }, region: Region { index: 0, name: "Faulty synthesis" }, gate_offset: 1, column: Column { index: 1, column_type: Advice }, offset: 1 }])`,
 right: `Ok(())`',
```

Via `impl Display for VerifyFailure`:
```
Region 0 ('Faulty synthesis') uses Gate 0 ('Equality check') at offset 1, which requires cell in column Column { index: 1, column_type: Advice } at offset 1 to be assigned.
```

Via `VerifyFailure::emit`:
```
error: cell not assigned
  Cell layout in region 'Faulty synthesis':
    | Offset | A0 | A1 |
    +--------+----+----+
    |    0   | x0 |    |
    |    1   |    |  X | <--{ X marks the spot! 🦜

  Gate 'Equality check' (applied at offset 1) queries these cells.
```
2022-02-16 13:57:53 +00:00
Jack Grigg 369ff521d3 dev: Store gate offset in `VerifyFailure::CellNotAssigned` 2022-02-16 13:57:53 +00:00
Jack Grigg c19a1ade2a dev: Add a custom `VerifyFailure::Lookup` emitter
The `dev::tests::bad_lookup` test case, shown via `assert_eq!(err, Ok(()))`:
```
  left: `Err([Lookup { lookup_index: 0, location: InRegion { region: Region { index: 2, name: "Faulty synthesis" }, offset: 1 } }])`,
 right: `Ok(())`',
```

Via `impl Display for VerifyFailure`:
```
Lookup 0 is not satisfied in Region 2 ('Faulty synthesis') at offset 1
```

Via `VerifyFailure::emit`:
```
error: lookup input does not exist in table
  (L0) ∉ (F0)

  Lookup inputs:
    L0 = x1 * x0 + (1 - x1) * 0x2
    ^
    | Cell layout in region 'Faulty synthesis':
    |   | Offset | A0 | F1 |
    |   +--------+----+----+
    |   |    1   | x0 | x1 | <--{ Lookup inputs queried here
    |
    | Assigned cell values:
    |   x0 = 0x5
    |   x1 = 1
```
2022-02-16 13:56:17 +00:00
Jack Grigg 62eea4c457 dev: Move cell loaders into `dev::util` 2022-02-16 13:56:17 +00:00
Jack Grigg 44e3cf8c61 dev: Move expression stringifier into `dev::failure::emitter` 2022-02-16 13:56:17 +00:00
Jack Grigg 212e3d07ce dev: Move cell layout emitter into a submodule 2022-02-16 13:56:17 +00:00
Jack Grigg 8e1fb87a33 dev: Add a custom `VerifyFailure::ConstraintNotSatisfied` emitter
An example failure, shown via `assert_eq!(err, Ok(()))`:
```
  left: `Err([ConstraintNotSatisfied { constraint: Constraint { gate: Gate { index: 0, name: "R1CS constraint" }, index: 0, name: "buggy R1CS" }, location: InRegion { region: Region { index: 0, name: "Example region" }, offset: 1 }, cell_values: [(VirtualCell { name: "", column: Column { column_type: Advice, index: 0 }, rotation: 0 }, "0x2"), (VirtualCell { name: "", column: Column { column_type: Advice, index: 1 }, rotation: -1 }, "0x4"), (VirtualCell { name: "", column: Column { column_type: Advice, index: 2 }, rotation: 1 }, "0x8")] }])`,
 right: `Ok(())`',
```

Via `impl Display for VerifyFailure`:
```
Constraint 0 ('buggy R1CS') in gate 0 ('R1CS constraint') is not satisfied in Region 0 ('Example region') at offset 1
- Column('Advice', 0)@0 = 0x2
- Column('Advice', 1)@-1 = 0x4
- Column('Advice', 2)@1 = 0x8
```

Via `VerifyFailure::emit`:
```
error: constraint not satisfied
  Cell layout in region 'Example region':
    | Offset | A0 | A1 | A2 |
    +--------+----+----+----+
    |    0   |    | x1 |    |
    |    1   | x0 |    |    | <--{ Gate 'R1CS constraint' applied here
    |    2   |    |    | x2 |

  Constraint 'buggy R1CS':
    S0 * (x0 * x1 + x2) = 0

  Assigned cell values:
    x0 = 0x2
    x1 = 0x4
    x2 = 0x8
```
2022-02-16 13:56:17 +00:00
Jack Grigg 5cdc029bb4 dev: Add `MockProver::assert_satisfied`
This is equivalent to `assert_eq!(mock_prover.verify(), Ok(()))`, but
pretty-prints the verification failures instead of debug-printing them.
In its initial state, it just prints the `Display` impl.
2022-02-16 13:56:17 +00:00
Jack Grigg 0e08903669 dev: Move `VerifyFailure` and `FailureLocation` into submodule 2022-02-16 13:55:29 +00:00
Daira Hopwood e7ffefdbe2 MockProver: Optimize repetitions of the last usable row, not the zero row.
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2022-02-15 00:30:54 +00:00
Daira Hopwood 7107b8353a MockProver: Use a sorted scan to check that lookup inputs
are contained in the table, fixing a performance regression.
This includes an optimization for "fill rows", which are
assumed in this commit to be all-zeros.

closes #398

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2022-02-14 23:54:06 +00:00
Jack Grigg f642727e51 halo2_proofs 0.1.0-beta.2 2022-02-14 21:58:20 +00:00
Jack Grigg 1d2f698aca Update license dates and links 2022-02-14 21:58:20 +00:00
Jack Grigg e0cc7b39d3 Update changelogs 2022-02-14 21:29:42 +00:00
Sean Bowe 0b73c74f72
Address comments brought up by @str4d. 2022-02-14 10:44:14 -07:00
Sean Bowe f2daf91315
halo2_proofs: change IPA check equation to match the book
The verifier's check in the inner product argument used to assume that the
G'_0 value had an additional (trivial) blinding factor term, which makes
it slightly easier to reason that it never is the point at infinity.
However, we never sample challenges that are zeroes (both for security
and completeness reasons) so this element would never be the point at
infinity anyway. Thus, we can simplify the check with the added benefit of
matching the book's description of the protocol.
2022-02-14 09:37:57 -07:00
ebfull 8c0deb10aa
Merge pull request #495 from zcash/book-consistency
halo2_proofs: rename variables for consistency
2022-02-14 09:24:20 -07:00
Sean Bowe 90bebdf29a
halo2_proofs: add hardcoded proof to plonk_api test for backwards compatibility testing 2022-02-14 08:48:03 -07:00
Sean Bowe 289f24bb8b
Improvements due to @daira's code review. 2022-02-11 08:50:55 -07:00
Sean Bowe a129490517
Minor changes 2022-02-10 08:08:20 -07:00
Sean Bowe a4d3c328b9
halo2_proofs: rename variables for consistency
This changes variable names in the multiopen and commitment opening implementations
and the book's protocol description to keep names and indicies consistent with one
another.

Co-Authored-By: Jack Grigg <jack@electriccoin.co>
2022-02-09 13:37:50 -07:00
Dimitris Apostolou e2f88e450b
Fix typos 2022-02-03 18:01:22 +02:00
Jack Grigg 9a12beee73 halo2_gadgets: Rename `halo2` to `halo2_proofs`
The previous commit renamed `halo2_proofs` back to `halo2` temporarily
to keep the commit size down. This commit performs the rename in a
single pass.
2022-01-27 23:32:04 +00:00
str4d bb56139414
Merge pull request #472 from zcash/separate-single-and-batch-verification
halo2_proofs: Improve `plonk::verify_proof` API
2022-01-27 01:19:34 +00:00
Jack Grigg 4d336f2707 halo2_proofs: Improve `plonk::verify_proof` API
Previously `plonk::verify_proof` took an `MSM` as an argument, to enable
batch verification. However, this also required that it take a source of
randomness in order to enforce separation of proofs within a batch. This
made single-proof verification unnecessarily non-deterministic.

We now have a `VerificationStrategy` trait encapsulating the necessary
details, and separate `SingleVerifier` and `BatchVerifier` structs for
the specific variants. Proof verifiers no longer need to create and
manage the `MSM` themselves, and single-proof verifiers no longer need
to supply a source of randomness.

Co-authored-by: Sean Bowe <sean@electriccoin.co>
2022-01-27 00:20:56 +00:00
Jack Grigg d3faddc53c Fix `impl Debug for poly::Ast`
The auto-derived implementation would only work if the evaluation
context implemented `Debug`, which closures never do.
2022-01-27 00:13:17 +00:00
Jack Grigg 507be292b8 Fix `poly::Evaluator` short-chunk bug
Previously we were passing through the chunk size and index to each
thread's evaluation context, but this was insufficient for them to
determine whether or not they were processing the final chunk, or if
the final chunk was short. This led to constant and linear term chunks
being created with the full chunk size, even if the last chunk was
short. If this longer-than-short chunk reached the root of the AST, it
triggered a panic in the final `copy_from_slice()`.

The bug was obscured in two ways:
- Currently polynomials always have a power-of-two length, and on CPUs
  with power-of-two threads this meant we never produced short chunks.
- The way that subsequent operations like `Ast::Add` were implemented
  meant that if a constant or linear term occurred on the right-hand
  side of an operation, the longer chunks were masked to the short chunk
  length.

We fix this by passing the polynomial length into each thread's context,
so that we can compute the correct length for the final chunk.

Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2022-01-27 00:13:17 +00:00
Jack Grigg 8cfa0bd399 Add a test exposing the `poly::Evaluator` short-chunk bug
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2022-01-27 00:13:17 +00:00
Jack Grigg b7944e5c40 Make `Assigned::Zero` slightly less likely in `Assigned` proptest 2022-01-21 13:57:25 +00:00
Jack Grigg 05a4d26bea Add unary operators to `Assigned` proptest 2022-01-21 13:57:25 +00:00
Jack Grigg a7e45495cf Add `Assigned::{double, square, cube}` methods 2022-01-21 13:57:25 +00:00
Jack Grigg 50b8e05913 Add other `Add*, Sub*, Mul*` variant impls to `Assigned<F>` 2022-01-21 13:57:25 +00:00
Jack Grigg 8d00acace5 `impl<F: Field> Eq for Assigned<F>` 2022-01-21 13:57:24 +00:00
Jack Grigg 927463f76a Add `Assigned::is_zero_vartime` method 2022-01-21 13:57:24 +00:00
Jack Grigg 9d0e0b7be9 Add `AssignedCell<Assigned<F>, F>::evaluate()` method
We don't want to provide a generic `map` function, since that would
enable users to arbitrarily alter the value connected to a given cell.
If a new value is being produced, that should either happen outside of
the context of a cell (e.g. intermediate values from witness generation)
or in the context of a newly-assigned cell.

However, in the case of the `Assigned<F>` type, we do need the ability
to evaluate the deferred inversion in some cases (e.g. to then operate
on the bits of the value). So for this `AssignedCell` specialization, we
provide a pass-through `evaluate()` method that otherwise preserves the
cell-value connection.
2022-01-21 13:57:24 +00:00
Jack Grigg 93ee7143fe `impl From<&Assigned<F>> for Assigned<F>`
In zcash/halo2#383 we altered the bounds on region assignment methods
like `Region::assign_advice` to constrain the value closure's result on
`for<'vr> Assigned<F>: From<&'vr VR>` instead of `VR: Into<Assigned<F>>`.
This had the unintended side-effect that `Assigned<F>` could no longer
be returned from the closure, because we were previously relying on the
implicit `impl From<T> for T` provided by Rust, which no longer fits the
bound. This commit adds the missing from-reference impl to restore
functionality, re-enabling inversion deferrment.
2022-01-21 13:57:24 +00:00
Jack Grigg 3c6558f049 Move `halo2` code into `halo2_proofs` crate 2022-01-20 18:50:43 +00:00