Commit Graph

674 Commits

Author SHA1 Message Date
Jack Grigg a83a0b3fd0 Migrate tests from `FieldExt::rand` to `Field::random`
These were missed in zcash/orchard#254.
2022-01-18 14:30:55 +00:00
therealyingtong d4d167c216 Use MockProver::FailureLocation in gadget unit tests.
This was introduced in halo2#433.
2022-01-05 21:30:45 +08:00
therealyingtong 5b26c7d67a Pass rng to create_proof API.
As of halo2#444, all APIs now take `R: RngCore` arguments instead of
internally depending on `rand::rngs::OsRng`.
2022-01-05 21:30:45 +08:00
therealyingtong f28edd886c Remove .into() from arguments to enable_equality().
As of halo2#416, this is handled internally by the function.
2022-01-05 21:30:45 +08:00
str4d 54cdc051fe
Merge pull request #237 from zcash/orchard-mainnet-circuit
Orchard proposed mainnet circuit
2021-12-20 17:49:57 +00:00
Jack Grigg d11fbd4a56 Remove `ValueSum::from_raw`
There is no reason for crate users to be constructing `ValueSum`
directly. We no longer use it to represent `valueBalanceOrchard`,
instead requiring the user to specify their own type.
2021-12-20 16:08:44 +00:00
Jack Grigg 04af08d343 Fix documentation of `orchard::value` module
Closes zcash/orchard#142.
2021-12-20 16:05:33 +00:00
Jack Grigg d84764f2db Remove outdated doc comment on `MerkleHashOrchard`
Closes zcash/orchard#245.
2021-12-20 15:24:48 +00:00
Jack Grigg 0e1220acc9 Merge branch 'main' into orchard-mainnet-circuit 2021-12-20 15:20:33 +00:00
Jack Grigg 369b99ee3f Add `doc_cfg` annotations 2021-12-17 22:08:58 +00:00
Jack Grigg 4b0b32275f Migrate to latest `zcash_note_encryption` API 2021-12-17 05:31:24 +00:00
str4d b13b9677cf
Merge pull request #254 from zcash/pasta_curves-prep
Remove various usages of `FieldExt` methods
2021-12-16 12:07:50 +00:00
Jack Grigg ab930e8866 sinsemilla: Simplify assertions in `MessagePiece::from_bitstring`
Also fixes some incorrect code comments.

Closes zcash/orchard#263.
2021-12-15 22:15:00 +00:00
Jack Grigg 1be54d9f0d Use `<= PrimeField::CAPACITY` instead of `< PrimeField::NUM_BITS` 2021-12-15 15:42:05 +00:00
Jack Grigg 5dd7de3cc7 Remove all uses of `PrimeField::Repr` in generic code
`PrimeField::from_repr` explicitly leaves the endianness opaque. We
therefore can't use it in places we were using `FieldExt::from_bytes`
(which was specifically little-endian) generically, but the previous
commit replaced it everywhere. We now handle generic contexts on a
case-by-case basis:

- Where we needed to convert bitstrings into field elements, we now use
  double-and-add on the field elements directly instead of on bytes.
  This is less efficient, but visible correct (and a future change to
  the `ff` crate APIs could enable the more efficient version).

- `INV_TWO_POW_K`, which is pre-computed for `pallas::Base`, was being
  incorrectly used in a field-generic circuit. We now compute it live.

- `test_zs_and_us` was only used in tests, and hard-coded a field
  element encoding length of 32 bytes. It now uses Pallas concretely.
2021-12-15 15:28:32 +00:00
Jack Grigg 044844c0a0 Reject the identity in `SpendValidatingKey::from_bytes`
`ak_P` is not allowed to be the identity in the Orchard protocol. We
were enforcing this by construction in most places, except for the
parsing of an Orchard full viewing key.

Closes zcash/orchard#261.
2021-12-15 13:48:59 +00:00
Jack Grigg 8fe178e433 poseidon: Seal the sponge modes
A sponge can only have two modes: absorbing, and squeezing.
2021-12-15 13:08:08 +00:00
str4d 5948a4977a
poseidon: Update code comments
Also fixes some clippy lints (public docs linking to private items).

Co-authored-by: Daira Hopwood <daira@jacaranda.org>
2021-12-15 13:04:54 +00:00
str4d a64e2d64a8
poseidon: Remove `M: SpongeMode` from `PhantomData`
`M` was at one point only used as a type marker, but now it stores per-mode state.

Co-authored-by: ying tong <yingtong@z.cash>
2021-12-15 13:01:53 +00:00
Jack Grigg 423006b905 poseidon: Remove redundant additions when squeezing sponge
In the previous commit, we fixed a bug where padding was being added to
the state when the sponge was in squeezing mode. But there's no need to
assign a circuit region in which we add constant zeroes to the state :)
2021-12-10 02:40:41 +00:00
Jack Grigg ae72813f77 poseidon: Fix padding to follow sponge construction
Sponge constructions pad the entire input message and then split it into
rate-sized chunks. The previous implementation was using an incorrect
duplex-like hybrid where padding was applied to each chunked input. We
now use an enum to distinguish message and padding words being absorbed
into the sponge.

This also fixes two previous bugs:
- If a `ConstantLength` hash had a length greater than the permutation's
  rate but not a multiple of it, no padding would be generated and the
  circuit would fail to create proofs.
- If a sponge usage required more output than the permutation's rate,
  the squeeze-side permutations would in some cases incorrectly apply
  padding, when it should instead use the prior state as-is. We now add
  zeroes instead.

This change doesn't alter the Orchard circuit, because it doesn't need
any padding cells, only takes a single field element as output, and
padding is still assigned in the same region as before.
2021-12-10 02:40:41 +00:00
Jack Grigg fdeb2fb817 poseidon: Add circuit test case that fails
This exposes a bug in the way padding was being handled by the invalid
sponge-duplex hybrid construction.
2021-12-10 02:40:41 +00:00
Jack Grigg bfc65d5985 poseidon: Remove `self` parameter from `Domain` trait methods
For almost all the sponge constructions defined in the Poseidon paper,
the domain can be defined completely statically. Variable-length hashing
requires knowledge of the message length, but that can be provided to
the fixed padding function in a subsequent commit, and in any case we
can't use variable-length inputs in a circuit.
2021-12-10 02:40:33 +00:00
Jack Grigg 9f654005c7 poseidon: Replace the `Duplex` struct with a `Sponge` struct
The `Sponge` struct's API correctly enforces the properties of a sponge:
it can absorb an arbitrary number of elements, and then squeeze an
arbitrary number of elements, but cannot absorb after it has squeezed.

Co-authored-by: ying tong <yingtong@z.cash>
2021-12-10 02:40:32 +00:00
Jack Grigg b827298d42 poseidon: Rename `SpongeState` to `SpongeRate` 2021-12-09 17:19:05 +00:00
Jack Grigg 0378898289 Replace `FieldExt::{from, to}_bytes` with `PrimeField::{from, to}_repr` 2021-12-09 15:39:37 +00:00
Jack Grigg 6f0cab5ffd Replace `FieldExt::from_u64` with `PrimeField: From<u64>` 2021-12-09 15:38:36 +00:00
Jack Grigg a4135dde24 ecc::chip: Fix `print_ecc_chip`
The ECC test chip performs various checks that assume the chip will only
be synthesized with witnesses. This assumption is broken by the chip
printer test, so we fix the assumption here.
2021-12-09 00:49:01 +00:00
Jack Grigg 6b84d0955a Fix "complex type" clippy lints 2021-12-08 04:11:57 +00:00
Jack Grigg a44253acc7 ecc::chip: Define a `MagnitudeSign` type alias
This fixes some "complex type" clippy lints, and also will make it
easier to change it to a better-typed struct later.
2021-12-08 02:23:51 +00:00
Jack Grigg bacdf67428 Remove the `CellValue` type
In order to make the changeover easier to review, we redefined
`CellValue<F>` to be `AssignedCell<F, F>`. Now we remove that type and
rename throughout the codebase.
2021-12-08 02:10:17 +00:00
Jack Grigg 65a89f099b Replace `gadget::utilities::copy` with `AssignedCell::copy_advice`
Also replaces other copy-advice implementations that weren't using
`copy`.
2021-12-08 01:50:02 +00:00
Jack Grigg 3079800f42 Remove `Var::new` trait method
As the underlying `Region` methods now return `AssignedCell` instead of
`Cell`, we can simplify all the places where we then constructed a
`CellValue` struct.
2021-12-08 01:48:17 +00:00
Jack Grigg 9b41a06363 Migrate to halo2 version with `AssignedCell`
We change `CellValue` into a typedef of `AssignedCell` to simplify the
migration in this commit.

The migration from `CellValue` to `AssignedCell` requires several other
changes:

- `<CellValue as Var>::value()` returned `Option<F>`, whereas
  `AssignedCell::<F, F>::value()` returns `Option<&F>`. This means we
  need to dereference, use `Option::cloned`, or alter functions to take
  `&F` arguments.
- `StateWord` in the Poseidon chip has been changed to a newtype around
  `AssignedCell` (the chip was written before `CellValue` existed).
2021-12-08 01:45:00 +00:00
Jack Grigg 5cb838f1a2 circuit: Remove `Copy` impl from `poseidon::pow5::StateWord`
We will be making it a newtype around `halo2::circuit::AssignedCell`,
which does not impl `Copy`.
2021-12-08 01:44:09 +00:00
Jack Grigg e99fc92e4b circuit: Use `Field::is_zero_vartime` 2021-12-08 01:44:08 +00:00
Jack Grigg 50b4600a1a circuit: Remove `Copy` impl from `CellValue`
We will be replacing it with `halo2::circuit::AssignedCell`, which does
not impl `Copy`.
2021-12-08 01:43:00 +00:00
str4d 55567f31ed
Merge pull request #248 from zcash/ecc-config-refactor
circuit: Refactor `EccConfig` away from `impl From<EccConfig>`.
2021-12-08 01:40:14 +00:00
str4d a38e2ff728
Ensure lo and hi incomplete ranges line up
The previous code assumed that `pallas::Scalar::NUM_BITS` was odd, which is true, but might not remain so after a future generalisation refactor.
2021-12-08 01:00:18 +00:00
Jack Grigg fe7796b884 circuit: Ensure that the real proof length matches calculated length 2021-12-06 19:44:44 +00:00
Jack Grigg e2c300368b circuit: Pin the proof size
This is to ensure that if any future circuit changes are made, their
effect on the proof size (if any) will be noticed.
2021-12-06 18:01:55 +00:00
str4d 42ad193b58
Merge pull request #247 from zcash/ternary-expr
circuit: Introduce ternary expression helper.
2021-12-06 17:32:35 +00:00
therealyingtong a09173a331 ecc::chip: Remove chip-level permutation.
We have now refactored away from the impl From<EccConfig> pattern
so that each sub-config can equality-enable the columns they need.
2021-12-04 04:45:06 +00:00
therealyingtong c00ee1707e mul_fixed::base_field_elem: Refactor base_field_elem::Config.
This commit does not result in circuit changes.
2021-12-04 04:45:06 +00:00
therealyingtong 687e220c36 mul_fixed::short: Refactor short::Config.
This commit does not result in circuit changes.
2021-12-04 04:45:06 +00:00
therealyingtong 165c9b6941 mul_fixed::full_width: Refactor full_width::Config.
This commit does not result in circuit changes.
2021-12-04 04:45:06 +00:00
therealyingtong f472a16b32 chip::mul_fixed: Move running_sum_config into mul_fixed::Config. 2021-12-04 04:45:06 +00:00
therealyingtong 1a7e832ed4 chip::mul_fixed: Refactor mul_fixed::Config.
This commit does not introduce circuit changes.
2021-12-04 04:45:06 +00:00
therealyingtong a7dad1d611 chip::mul: Refactor mul::Config.
This commit does not introduce additional circuit changes.
2021-12-04 04:45:05 +00:00
therealyingtong 440cd14dbb mul::overflow: Refactor overflow::Config.
This is only used in chip::mul::Config. In a subsequent commit,
this will be configured from mul::Config instead of from
ecc::chip::Config.

This commit does not result in circuit changes.
2021-12-04 04:44:41 +00:00