Commit Graph

553 Commits

Author SHA1 Message Date
str4d cc3e1ad0b4
Merge pull request #111 from zcash/ecc-mul
[ECC chip] Fixed- and variable-base scalar multiplication
2021-07-15 11:16:12 +01:00
therealyingtong 425ee6e038 Docfixes and minor refactors.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-15 12:27:12 +08:00
therealyingtong b696163e31 mul.rs: Explain ordering of mul::incomplete advice columns. 2021-07-14 18:30:43 +08:00
Daira Hopwood 43ffa37740 [book] Nullifiers: the scalar is (...) mod p, not ... (mod p).
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-07-12 20:24:18 +01:00
Daira Hopwood c76358769c book/src/design/nullifiers.md: cosmetics (make the table fit).
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
2021-07-12 20:20:00 +01:00
therealyingtong 0ade539441 utilities::tests::test_range_check(): Test range_check() helper.
Verify that this constraint fails when the witnessed value is out
of range.
2021-07-09 23:17:42 +08:00
ying tong 7b3a0c8a29
Merge pull request #54 from zcash/book-ecc-gadget
[book] Document ECC gadget in circuit
2021-07-09 22:18:16 +08:00
therealyingtong 6c41c72e66 utilities::range_check: Correct range_check expression
Previously, we were multiplying the expression by 0, which led it
to always evaluate to true.
2021-07-09 22:03:26 +08:00
therealyingtong 8a9f8218e9 mul_fixed::base_field_elem: Remove double-enable of base_field_fixed_mul.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-09 10:23:40 +08:00
therealyingtong d9f134ac4b [book] Details and formatting changes.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-09 10:09:10 +08:00
str4d 74df35ce89
Merge pull request #136 from nuttycom/total_merkle_crh_orchard
Implements the updated, total definition of MerkleCRH^Orchard
2021-07-09 02:26:10 +01:00
ying tong 2febafbdfe
Apply suggestions from code review
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: str4d <jack@electriccoin.co>
2021-07-08 16:40:44 +08:00
therealyingtong ae4e54dce8 gadget::utilities: Add test cases for bitrange_subset() helper. 2021-07-08 16:29:07 +08:00
therealyingtong 5c38f53b58 mul::tests: Witness expected point and constrain result to be equal. 2021-07-08 15:17:52 +08:00
therealyingtong e2ea443fad mul_fixed::*::tests: Witness expected point and constrain result to be equal. 2021-07-08 15:06:47 +08:00
therealyingtong 22ec16f129 Minor refactors, cleanups, clippy fixes, docfixes.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-08 13:31:56 +08:00
therealyingtong 96863c9f73 mul_fixed::*: Use a separate region for complete addition assignment.
The mul_fixed regions use complete addition on the last window,
and incomplete addition on all other windows. However, the complete
addition does not depend on any offsets in the incomplete addition
region, and can be separated into a disjoint region. Since incomplete
addition uses only four advice columns, while complete addition uses
nine, separating the regions would allow the layouter to optimise
their placement.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-08 12:04:43 +08:00
therealyingtong d0e34cd204 mul_fixed::base_field_elem: Eliminate alpha_0 lookup decomposition.
We can use the three-bit existing running sum decomposition to
constrain alpha_0 to be within 130 bits. This removes the need for
a 10-bit lookup decomposition of alpha_0.

Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-08 11:24:18 +08:00
therealyingtong afc8d9a142 [book] Eliminate alpha_0 lookup decomposition when checking canonicity of base field element used in fixed-base mul. 2021-07-08 11:12:13 +08:00
therealyingtong f42d48b8a4 mul_fixed::base_field_elem: Fix two_pow_130 expression.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-08 08:49:46 +08:00
therealyingtong ae72501b70 mul_fixed::base_field_elem: Add constraint alpha_2 = 0 => alpha_1 = 0.
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-07 23:12:31 +08:00
therealyingtong 72e469ee10 mul_fixed::base_field_elem.rs: Check canonicity of base field element used in fixed-base scalar mul.
When using a base field element as the scalar in fixed-base mul,
we check the canonicity of its decomposition.
2021-07-07 23:12:31 +08:00
therealyingtong b69094036c chip::mul_fixed.rs: Make q_mul_fixed a selector instead of fixed column. 2021-07-07 23:12:31 +08:00
therealyingtong 23f2ed5abe gadget::utilities.rs: Add bitrange_subset() helper. 2021-07-07 23:12:31 +08:00
therealyingtong 2d343af068 Update mul_fixed_* APIs to take Layouter instead of Region.
These APIs are not called internally.
2021-07-07 23:10:59 +08:00
therealyingtong d550e156d9 mul_fixed_*::tests: Constrain zero outputs in mul_fixed tests.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
therealyingtong 9fd4d7df27 Cleanups and clippy fixes.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-07-07 23:10:59 +08:00
therealyingtong 2536555299 mul_fixed: Constrain interpolated window mul to be on curve.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
Jack Grigg 67caed52eb mul::incomplete: Constrain final iteration correctly
The differences between the final iteration and prior iterations are:
- The final iteration does not constrain (x_T, y_T) to propagate down.
- The final iteration constrains an assigned y_A output instead of a
  derived y_A from the next iteration's variables.

We also swap the init_y constraint to match the book.

Co-authored-by: therealyingtong <yingtong@z.cash>
2021-07-07 23:10:59 +08:00
therealyingtong 33b66ab796 tests::print_ecc_chip(): Print ECC chip.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
therealyingtong 3f961ab29a mul::process_lsb(): Clean up assignments and boolean-constrain LSB.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
therealyingtong 6ffd867e23 mul::complete.rs: Constrain negation of (x_p, y_p) in double-and-add.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
therealyingtong e75c176181 mul::incomplete.rs: Make offsets more intuitive 2021-07-07 23:10:59 +08:00
therealyingtong 4d69dec00f mul::incomplete.rs: Constrain first and last y_a values.
Co-authored-by: Jack Grigg <jack@electriccoin.co>
2021-07-07 23:10:59 +08:00
therealyingtong b363492a35 ecc::chip.rs: Introduce circuit-wide "constants" fixed column
At certain points in the circuit, we need to constrain cells in
advice columns to equal a fixed constant. Instead of defining a
new fixed column for each constant, we pass around a single
shared by all chips, that is included in the permutation over all
advice columns.

This lets us load all needed constants into a single column and
directly constrain advice cells with an equality constraint.
2021-07-07 23:10:59 +08:00
therealyingtong 69d6629ac6 chip::mul.rs: Enforce LSB if/else condition
On the LSB of the scalar, we assign a point (x,y) = (x_p, -y_p)
if LSB = 0, and (0,0) otherwise. This if/else condition must be
enforced.

Co-authored-by: Sean Bowe <ewillbefull@gmail.com>
2021-07-07 23:10:59 +08:00
Jack Grigg e726fee19b mul_fixed: Avoid computing fixed constants during proving
This decreases proving time in the Action circuit by 17%.
2021-07-07 23:10:59 +08:00
Jack Grigg b15343f6f7 Add `OrchardFixedBasesFull::{generator, u}` methods
Using these in `OrchardFixedBases::{generator, u}` instead of the
`impl From<OrchardFixedBasesFull> for OrchardFixedBase` means we avoid
computing the Lagrange coefficients for the generator (which were then
immediately dropped).

This decreases proving time in the Action circuit by 53%.
2021-07-07 23:10:59 +08:00
therealyingtong 09b4da197d base_field_elem.rs: Support fixed-base mul using base field element.
In Orchard nullifier derivation, we multiply the fixed base
K^Orchard by a value encoded as a base field element. This commit
introduces an API that allows using a base field element as the
"scalar" in fixed-base scalar multiplication.

The API currently assumes that the base field element is output by
another instruction (i.e. there is no instruction to directly
witness it).
2021-07-07 23:10:59 +08:00
therealyingtong 37074c64f5 mul_fixed::short: Check that last window is either 0 or 1.
The magnitude of the short signed scalar must be 64 bits. We decompose
the magnitude into 22 3-bit windows and check that each window is in
the 3-bit range.

However, since the first 21 windows have already accounted for 63 bits,
the last window is constrained to be a single bit.
2021-07-07 23:10:59 +08:00
therealyingtong 747f71ca80 constants.rs: Add unit tests for T_P, T_Q constants. 2021-07-07 23:10:59 +08:00
therealyingtong 5ae9890913 mul::overflow.rs: Overflow check in variable-base scalar mul
Simplify the canonicity check for variable-base scalar multiplication,
by range-checking the low 130 bits rather than the low 127 bits.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: ying tong <yingtong@z.cash>
2021-07-07 23:10:59 +08:00
therealyingtong a3ca27b756 ecc::tests: Add tests for variable- and fixed-base scalar mul. 2021-07-07 23:10:59 +08:00
therealyingtong ae25310385 chip::mul_fixed.rs: Implement fixed-base scalar mul instruction.
Fixed-base scalar mul makes use of the add_incomplete and add
instructions internally. The full-width and short signed share
some common logic, which is captured in chip::mul_fixed.rs.

The signed short variant introduces additional logic to handle
the scalar's sign. This is done in the submodule mul_fixed::short.
2021-07-07 23:10:59 +08:00
therealyingtong a263774abf chip::witness_scalar_fixed.rs: Implement witness_scalar_fixed instruction.
A scalar used in fixed-base scalar mul needs to be decomposed into
windows to use with the fixed-base window table. Both full-width
and short signed scalars share some logic (captured in the function
decompose_scalar_fixed()).

A short signed scalar introduces additional logic: its magnitude is
decomposed, and its sign is separately witnessed. This is handled
in the submodule witness_scalar_fixed::short.
2021-07-07 23:10:59 +08:00
therealyingtong cc9dd20536 chip::mul.rs: Implement variable-base scalar mul instruction.
This uses the complete addition instruction internally. The module
is split up into mul::incomplete.rs and mul::complete.rs, where
mul::incomplete handles the incomplete additions used in the
starting rounds of the variable-base scalar mul algorithm, and
mul::complete handles the complete additions in the final rounds.

Incomplete additions are broken into "hi" and "lo" halves and
processed on the same rows across different columns. This is an
optimization to make full use of the advice columns in this
instruction.
2021-07-07 23:10:59 +08:00
therealyingtong 64a2b02d42 ecc::chip.rs: Witness scalar for variable-base scalar mul 2021-07-07 23:10:59 +08:00
therealyingtong 0f60a81485 ecc::chip.rs: Add EccScalarFixed, EccScalarFixedShort structs 2021-07-07 23:10:59 +08:00
therealyingtong 091592e110 [book] Document canonicity check for fixed-base scalar mul when base field element is used as the scalar. 2021-07-07 17:10:18 +08:00
therealyingtong 32f9622c23 [book] Document lookup range check and its use in overflow check. 2021-07-03 19:30:27 +08:00