Commit Graph

600 Commits

Author SHA1 Message Date
therealyingtong 58de805a13 sinsemilla::merkle.rs: Use tree::MerklePath::root in tests. 2021-09-16 15:36:24 +02:00
therealyingtong f75f890a64 Update tree::MerklePath::root to be total. 2021-09-16 15:36:24 +02:00
Jack Grigg 414eef3ce5 memuse 0.2 2021-09-14 20:40:15 +01:00
Kris Nuttycombe 4488288ac0
Merge pull request #198 from zcash/merkle-path-test-vectors
Add Merkle path test vectors
2021-09-14 07:22:28 -06:00
str4d 3dd2a1872a
Merge pull request #169 from zcash/circuit-constraint-refinements
Circuit constraint refinements to reduce proof size
2021-09-14 02:05:41 +01:00
Jack Grigg 29a4bbcbc1 Add Merkle path test vectors 2021-09-14 00:15:39 +01:00
Daira Hopwood ee44d2ccf0
Apply suggestions from code review 2021-09-07 02:45:10 +01:00
Daira Hopwood 97e18a8190
Apply suggestions from code review 2021-09-07 00:56:22 +01:00
Daira Hopwood faddaf9e30 note_commit.rs: make two_pow_* definitions more consistent.
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-09-07 00:52:37 +01:00
Jack Grigg 8c82ceecbf ff 0.11, group 0.11, pasta_curves 0.2 etc. 2021-09-06 20:39:43 +01:00
Jack Grigg 7fad21e7d6 Switch to `memuse` crate for measuring heap allocations 2021-09-05 01:33:27 +01:00
Daira Hopwood c24c67d5f0 cargo fmt
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-09-01 14:11:08 +01:00
Kris Nuttycombe e4a54cdf61 Improve error handling in zip32 APIs. 2021-08-31 16:49:58 -06:00
therealyingtong c3e24794f0 zip32.rs: master and child key derivation for ExtendedSpendingKey 2021-08-31 15:49:32 -06:00
Kris Nuttycombe 77be355912 Apply suggestions from code review
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: ying tong <yingtong@z.cash>
2021-08-23 11:29:07 -06:00
Kris Nuttycombe 0449edd5b8 Validate the sign of the y-coordinate for ak when deserializing. 2021-08-23 11:29:07 -06:00
Kris Nuttycombe 43abadfb55 Adds decryption for a specific index within a bundle. 2021-08-23 11:29:07 -06:00
Kris Nuttycombe c406461f64 Expose inner representation of NoteValue 2021-08-23 11:29:07 -06:00
Kris Nuttycombe 872f337811 Expose SpendingKey byte representation. 2021-08-23 11:29:07 -06:00
Kris Nuttycombe c803114bf6 Go ahead and clone IVKs to limit borrowing hassles. 2021-08-23 11:29:07 -06:00
Kris Nuttycombe d8bf892c72 Return key used to decrypt an output along with decrypted note contents. 2021-08-23 11:29:07 -06:00
Kris Nuttycombe 5d78ab3508 Add Eq and Ord implementations for Orchard keys. 2021-08-23 11:29:06 -06:00
Kris Nuttycombe 52f0f158ef Add serialization and parsing of full viewing keys. 2021-08-23 11:28:27 -06:00
Kris Nuttycombe 1fd00e6236 Add raw address serialization and parsing. 2021-08-23 11:28:27 -06:00
Kris Nuttycombe e33cd4ade4 Add trial decryption of actions to Bundle 2021-08-23 11:28:25 -06:00
Kris Nuttycombe 77cf4c9831 Implement IncomingViewingKey::to_bytes 2021-08-23 11:27:02 -06:00
str4d f2400baa01
Improve NoteCommit input value gate doc
Brings it in line with the other gate docs.

Co-authored-by: ying tong <yingtong@z.cash>
2021-08-19 14:35:56 +01:00
str4d bac22d9b19
clippy: Remove redundant clones
Co-authored-by: ying tong <yingtong@z.cash>
2021-08-19 14:34:15 +01:00
str4d ac900148ed
Fix typo in gate documentation
Co-authored-by: ying tong <yingtong@z.cash>
2021-08-19 14:33:52 +01:00
str4d b4a82211ce
Merge pull request #184 from zcash/poseidon-domain-spec
poseidon::Domain: Remove Spec trait bound.
2021-08-17 12:55:01 +01:00
str4d cb28e00ebd
Merge pull request #178 from zcash/batch-note-decryption
Speed up batched note decryption
2021-08-13 14:27:41 +01:00
Jack Grigg 79988a5317 Move the interpolation logic into `SharedSecret::batch_to_affine`
This makes the method interface clearer, as the same pattern of shared
secrets is returned as was provided.
2021-08-13 14:27:20 +01:00
therealyingtong 1f852544cf poseidon::Domain: Remove Spec trait bound.
The methods in the Domain trait are not generic over Spec.
2021-08-13 14:47:02 +08:00
str4d 4e33fe7aec Use correct symbol for incomplete addition
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-08-12 21:34:35 +01:00
str4d 459e68b71e
Fix clippy lint
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-08-12 21:32:14 +01:00
Jack Grigg 9f3c9a7e60 Use mixed addition for Sinsemilla bases
Performance improvements:
- MerkleCRH:  ~5%
- Commit^ivk: ~1%
- NoteCommit: ~3%
2021-08-12 15:45:00 +01:00
Jack Grigg 6197a0ef62 Use `group::Wnaf` to accelerate `sinsemilla::CommitDomain::commit`
Performance improvements:
- Commit^ivk: ~31%
- NoteCommit: ~22%
2021-08-12 15:45:00 +01:00
str4d 5f0c3b3585
Merge pull request #179 from zcash/sinsemilla-bases
primitives::sinsemilla: Use hard-coded generators in sinsemilla_s.
2021-08-12 15:18:38 +01:00
therealyingtong 92a7e20d30 Remove sinsemilla_s_generators() function.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-08-12 20:54:51 +08:00
Jack Grigg c79acc0e08 Fix length of output Vec for `SharedSecret::batch_to_affine`
It was too long, and `group::Curve::batch_normalize` panics if its
inputs are not the same length (which would be the case if a batch
included an output with an invalid `ephemeral_key`).
2021-08-12 13:40:56 +01:00
therealyingtong a9e96eb0a4 sinsemilla_s: Add documentation. 2021-08-12 16:15:24 +08:00
therealyingtong 995728caa6 primitives::sinsemilla: Use hard-coded generators in sinsemilla_s. 2021-08-12 15:45:14 +08:00
Jack Grigg 8e13986101 Implement `Domain::batch_epk` for note decryption
Improves throughput of batched trial decryption by around 10%.
2021-08-12 01:36:38 +01:00
Jack Grigg 8c15cc25be Benchmark batch trial decryption 2021-08-12 01:36:38 +01:00
Jack Grigg 0d306d18aa Expose and benchmark Poseidon 2021-08-10 13:44:04 +01:00
Jack Grigg 08b279b900 Expose and benchmark Sinsemilla primitive 2021-08-10 13:39:14 +01:00
therealyingtong e62cfaa398 ExtractedNoteCommitment::from_bytes: Document cmx canonicity. 2021-08-09 20:11:27 +08:00
Jack Grigg f4a8c082a9 Use w-NAF in `ka_orchard`
Improves the base-line cost of trial decryption by over 40%.
2021-08-06 13:43:19 +01:00
Jack Grigg 2283310236 Expose `orchard::note_encryption::{CompactAction, OrchardNoteEncryption}`
This also removes the `orchard::OrchardDomain` re-export, which is now
available at `orchard::note_encryption::OrchardDomain`.
2021-08-06 01:01:12 +01:00
Kris Nuttycombe d8091dd575 Update incrementalmerkletree version. 2021-08-05 07:51:19 -06:00
Daira Hopwood e4612f7f6c Update Poseidon instantiation from 58 to 56 partial rounds. fixes #166
Test vectors are from https://github.com/zcash-hackworks/zcash-test-vectors/pull/45

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-08-04 13:04:13 +01:00
Jack Grigg 9af22a8cbc circuit: Add region layout diagrams for y_switch constraint
Helps to see why we can't optimise it to remove the `prev` query.
2021-07-29 20:57:33 +01:00
Jack Grigg 6aa85fcdfe circuit: Refactor NoteCommit input processing into multiple regions
The new regions take up more cells overall, but across fewer columns,
and the gates now only query `cur` and `next` rows.
2021-07-29 20:13:27 +01:00
Jack Grigg 2198675f9d circuit: Rotate `q_commit_ivk` selector up by one row
This ensures the Commit^ivk gate only queries `cur` and `next` rows.
2021-07-29 14:56:56 +01:00
Jack Grigg 0009070358 circuit: Rotate`q_mul_lsb` selector up by one row
This ensures the "LSB check" gate only queries `cur` and `next` rows.
2021-07-29 14:56:56 +01:00
Jack Grigg 16e9076080 Add names to some nameless constraints 2021-07-29 14:56:56 +01:00
str4d 8454f86d42
Merge pull request #140 from nuttycom/bundle_zip244_commitments
Implement ZIP-244 txid and authorizing commitments.
2021-07-29 14:45:24 +01:00
Kris Nuttycombe 40d80c4d6f
Apply suggestions from code review
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-29 07:16:14 -06:00
Jack Grigg 9117273c08 Fix bug in `Builder` initialization of `Circuit` struct
`rcv` was being used correctly outside the circuit to derive `cv_net`
but then `Circuit` was just storing 0. The `round_trip` test passed
because it uses `rcv = 0` everywhere.
2021-07-28 22:51:43 +01:00
Jack Grigg a33d1bd90f Add circuit benchmarks and (on Unix) flamegraphs
- Benchmarks:  `cargo bench`
- Flamegraphs: `cargo bench -- --profile-time 100`
2021-07-28 15:09:31 +01:00
Jack Grigg 513f3cf8a6 Make `Builder::build` public 2021-07-28 14:37:12 +01:00
Jack Grigg 01fbd59683 Move proof creation out of `Builder::build` 2021-07-28 14:37:12 +01:00
Jack Grigg 6b495f711a Extract InProgress type from Unauthorized and PartiallyAuthorized
This enables bundle proofs and signatures to be handled separately
outside the builder.
2021-07-28 13:48:03 +01:00
Daira Hopwood 145da9c510 Update to assign_table API.
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-07-27 18:32:32 +01:00
therealyingtong d3a7e9ed39 lookup_range_check: Merge running sum and short lookup arguments.
The lookup running sum decomposition uses the same lookup table as
its short variant. These two lookup arguments have been merged.

Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-27 09:50:17 +01:00
str4d bb90f2eb7d
Merge pull request #101 from zcash/action-circuit
Action circuit
2021-07-27 09:49:23 +01:00
str4d 620e227854
Fix y-coordinate recovery in NoteCommit tests 2021-07-27 09:27:33 +01:00
therealyingtong 3f506a0129 circuit.rs: Minor cleanups and column optimisations.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-27 15:41:26 +08:00
therealyingtong 664125f44f commit_ivk::tests: Check value of output ivk against expected ivk. 2021-07-27 15:33:13 +08:00
therealyingtong fa135fe62e note_commit::tests: Constrain output of NoteCommit to expected point. 2021-07-27 15:23:00 +08:00
therealyingtong 7aa3174880 sinsemilla::note_commit: Improve NoteCommit gate layout.
By rearranging the pieces in the gate, we remove a prev() query and
preserve proximity between pieces involved in the same constraint.

This commit also includes several minor fixes:
- use strict mode for decomposition of j in y-coordinate check;
- Name All Polynomial Constraints;
- remove point_repr() helper function;
- variable renaming and docfixes.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-27 13:51:35 +08:00
therealyingtong e4a960d7f1 sinsemilla::note_commit: Simplify y canonicity check region layout
Instead of separately witnessing k_1 and equating it to z1_j, we
can directly make use of z1_j in the gate. This allows us to fit
the region into a 5 x 2 area, improving the layout.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-27 12:49:42 +08:00
therealyingtong b3ccd3f0dd Use halo2 selector optimizations. 2021-07-27 03:14:34 +08:00
therealyingtong 65ccf80560 sinsemilla::note_commit: Check canonicity of y(g_d), y(pk_d).
Even though we only use the LSB of the y-coordinates as inputs to
the Sinsemilla hash, we still have to check that they are consistent
with the g_d and pk_d points that were passed in.

Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-27 03:13:54 +08:00
therealyingtong 57f23d9f17 sinsemilla::commit_ivk: Fix two_pow_5 constraint bug.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-26 10:05:15 +08:00
therealyingtong 5b63550f50 sinsemilla::note_commit: Check that g1_g2_prime < 2^130 instead of 2^140.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-25 21:56:55 +08:00
therealyingtong d9351df544 sinsemilla::commit_ivk: Use 9 advice columns instead of 10
Change the region layout to only use 9 advice columns instead of 10.
Also rename variables to match the book.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-25 21:10:13 +08:00
therealyingtong 5999d4be6d sinsemilla::commit_ivk.rs: Change z14_c -> z13_c
This matches the constraint specified in the book.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-25 21:10:13 +08:00
therealyingtong 4d1cd2651a Return full running sum [z_0, ..., z_W] from lookup_range_check and decompose_running_sum.
Previously, these two helpers were returning different outputs.
They have now been standardised to return only the full running
sum.

Note the z_0 is the original element being decomposed by the
helper.
2021-07-25 21:10:13 +08:00
Jack Grigg 092cc389bb More small circuit optimisations
- Placing the Poseidon `state` columns after the `partial_sbox` column
  instead of before it causes them to line up with vast stretch of free
  space, enabling the pad-and-add region to be layed out there.

- Using the `Region::assign_advice_from_constant` API to initialise the
  Poseidon state removes fixed-column contention between that region and
  fixed-base scalar multiplication, enabling it to also be layed out
  within the free space.
  - If https://github.com/zcash/halo2/issues/334 were implemented then
    this region would disappear.

- The overflow check in variable-base scalar mul is also moved into the
  columns with free space.
2021-07-25 21:10:13 +08:00
therealyingtong 7af1ae5b52 note_commit: Decompose q_canon into two binary selectors.
Previously, q_canon was a non-binary fixed column that was set to
either {1, 2}. It has been decomposed into two binary selectors.
2021-07-25 21:10:13 +08:00
therealyingtong 76c73531c8 lookup_range_check: Replace short_lookup_bitshift with selector.
Previously, the short_lookup_bitshift fixed column was a non-binary
selector that both provided a constant value and toggled a gate.

Now, the constant value is copied in from the global constants API,
and the toggle is handled by a q_lookup_bitshift selector.
2021-07-25 21:10:13 +08:00
therealyingtong 283b45169a merkle: Replace l_plus_1 with l
Since l_plus_1 is no longer used as a selector, we can directly
use the value l in the gate.
2021-07-25 21:10:13 +08:00
therealyingtong 29f185014f merkle: Replace l_plus_1 fixed column with q_decompose selector.
Previously, l_plus_1 was a non-binary fixed column, used to
1. provide the value of l + 1; and
2. toggle the decomposition gate.

Now, the value is copied in from the global constants column, and
the toggle is handled by a binary q_decompose selector.
2021-07-25 21:10:13 +08:00
therealyingtong f532ecec10 mul::incomplete.rs: Decompose q_mul into binary selectors.
Previously, q_mul was a non-binary selector that could be set to
1, 2, or 3. We now decompose it into three binary selectors
q_mul_{1,2,3}.
2021-07-25 21:10:13 +08:00
therealyingtong f6c951d975 sinsemilla: Decompose fixed_y_q into binary selector and constant.
Previously, fixed_y_q was a non-binary selector that both loaded
the y_Q value and toggled the y_Q gate.

Now, the gate is toggled by a q_s4 simple selector, while the value
is loaded into a separate fixed column.
2021-07-25 21:10:01 +08:00
therealyingtong cba0d8672b Remove standard PLONK helper.
The Action circuit only used standard PLONK in one place. Since it
used non-binary selectors, it cannot be optimised by the halo2
selector optimisations. We now replace it with a custom gate which
uses a binary selector.
2021-07-23 17:58:58 +08:00
Kris Nuttycombe b86967bc57
Merge pull request #135 from zcash/patch-ncc
Partial fixes from NCC draft report
2021-07-22 15:25:05 -06:00
therealyingtong 8cf7a6872c Minor refactors, text fixes, and docfixes.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-23 00:15:54 +08:00
Jack Grigg 43d187283e Use an existing fixed column for Action circuit global constants 2021-07-21 17:20:39 +01:00
Jack Grigg 9d6ae478b3 Switch to `floor_planner::V1`
This correctly lays out the circuit in 2^11 rows.
2021-07-21 17:19:15 +01:00
Jack Grigg 3462d3e051 Optimise layout of witnessed Sinsemilla message pieces
The Sinsemilla chip witnesses message pieces in individual regions, and
then copies them into the `hash_piece` region to initialize the running
sum. Previously these occured in the same column, but we can reduce the
utilized rows of the Action circuit by moving these into a less-used
column.

If https://github.com/zcash/halo2/issues/334 is implemented, this change
would be unnecessary, as the witnessed message piece regions would never
be assigned into the circuit.
2021-07-21 16:56:06 +01:00
Jack Grigg bf72e308bd De-duplicate LookupRangeCheckConfig
We were configuring multiple instances of this across all of the advice
columns, in order to spread their assignments. However, we are actually
more constrained by columns than rows, and we have comparatively few
rows of range check logic required for the Action circuit.

We now use a single LookupRangeCheckConfig for the entire circuit. The
reduction in lookup arguments and fixed columns cuts the proof size in
half (now at 6048 bytes when using `floor_planner::V1`).

Co-authored-by: therealyingtong <yingtong@z.cash>
2021-07-21 16:03:54 +01:00
Jack Grigg 5e6c8ae380 Optimise location of Poseidon within Action circuit
- Move Poseidon into the right-hand advice columns. The Action circuit
  has 33 Sinsemilla invocations with 510-bit inputs (the 32 Merkle path
  hashes, and Commit^ivk). Poseidon fits within the row count of one of
  these invocations, so we can run it in parallel with these.

- Share fixed columns between ECC and Poseidon chips. Poseidon requires
  four advice columns, while ECC incomplete addition requires six, so we
  could choose to configure them in parallel. However, we only use a
  single Poseidon invocation, and we have the rows to accomodate it
  serially with fixed-base scalar mul. Sharing the ECC chip's 8 Lagrange
  coefficient fixed columns instead reduces the proof size.

  - We position Poseidon in the right-most 6 fixed columns, anticipating
    a further optimisation to Sinsemilla that will occupy the left-most
    2 fixed columns.
2021-07-21 16:03:54 +01:00
therealyingtong d80333799d Combine constants fixed columns using assign_advice_from_constant.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-21 20:35:43 +08:00
therealyingtong a10aefc8c1 circuit.rs: Implement enable spends and enable outputs logic.
The utilities::enable_flag gadget has been deleted, since the
public inputs API makes it more convenient to inline the gate.
2021-07-21 20:35:43 +08:00
therealyingtong 6f4b5b0340 circuit.rs: Constrain derived circuit values to equal public inputs. 2021-07-21 20:35:43 +08:00
therealyingtong 059af49f46 Circuit::synthesize: New note commitment integrity. 2021-07-21 20:35:43 +08:00
therealyingtong 1a1a65af4a Circuit::synthesize(): Old note commmitment integrity. 2021-07-21 20:35:43 +08:00
therealyingtong 84172e0326 note_commit.rs: Test canonicity check for different input values. 2021-07-21 20:35:43 +08:00