Commit Graph

91 Commits

Author SHA1 Message Date
Jack Grigg 7eaf6493e8 pairing: Require G1 and G2 to be PrimeCurve
Pairings require that G1, G2, and GT are groups of prime order.
2020-08-13 18:17:29 +01:00
Jack Grigg ed6b7eceef group: Hard-code a w-NAF window size of 4
We were already ignoring the actual bit length of the scalar, and
selecting the window size based on the maximum bit length, which
effectively hard-coded a window size of 4.
2020-08-12 08:12:48 +01:00
Jack Grigg 1e8fd4da68 group: CofactorGroup::mul_by_cofactor -> CofactorGroup::clear_cofactor
The generic API now only guarantees that the torsion component is
cleared deterministically; group elements may be multiplied by multiples
of the cofactor (not necessarily the actual cofactor), as long as the
choice of multiplier is fixed for a given implementation.
2020-08-12 08:12:25 +01:00
Jack Grigg 12f6ec1b2e pairing: Bound Engine::Gt on Group instead of Field 2020-06-24 19:20:25 +12:00
Jack Grigg ad96a38750 group: Make Wnaf generic over Group
Wnaf was originally generic over CurveProjective; in the prior refactor
commit, we renamed this to CofactorCurve. But w-NAF only requires scalar
multiplication, which is provided by the Group trait, so we relax the
bounds on Wnaf to enable it to be used with any group. We move the
generic w-NAF helper methods from the Curve trait to a new WnafGroup
extension trait, to keep the w-NAF API surface self-contained, and not
expose it to users who aren't using it.
2020-06-17 11:42:11 +12:00
Jack Grigg a105ad675a group: Separate prime and cofactor traits into modules
Instead of having the Group crate hold a Subgroup associated type (and
thus needing to define the subgroup of a prime-order group as itself),
we specify two separate sets of traits for prime-order groups and ones
with a cofactor.

Protocol implementors can either restrict their implementations to only
work with PrimeGroup, or can explicitly choose to support CofactorGroup
and then explicitly handle the subgroup edge cases with e.g.
CofactorGroup::mul_by_cofactor (which would be a no-op for PrimeGroup).

Protocol implementors can also choose to specialise to elliptic curves
if they want to leverage an affine representation and mixed addition in
their protocol for efficiency, or they can ignore those traits and stick
with the simpler group-focused traits.
2020-06-17 11:42:10 +12:00
Jack Grigg d52053d877 group: Renaming prior to trait refactor
This will reduce the size of the subsequent refactor diff.
2020-06-17 11:40:00 +12:00
Jack Grigg df13cd7480 group: Remove "compressed" notion from GroupEncoding
A generic group has a single encoding; for elliptic curves, this
happens to be the compressed encoding.
2020-06-17 11:35:17 +12:00
Jack Grigg 71586914d4 group: Extract compressed encodings from CurveAffine trait 2020-06-17 11:35:15 +12:00
Jack Grigg ae2d2b59b9 group: Move uncompressed encodings to an UncompressedEncoding trait
Specifications of deployed elliptic curves fall into one of two
categories:
- They specify both compressed and uncompressed encodings, allowing
  implementations to use either depending on performance vs data size
  considerations.
- They specify a single point encoding format using point compression.

I am unaware of any elliptic curve specification that explicitly forbids
compressed encodings.

To support both categories of elliptic curves, we provide the
CurveAffine::Compressed associated type which all curves must define,
and then curves that additionally specify an uncompressed encoding may
implement the UncompressedEncoding trait and its Uncompressed associated
type.

pairing::PairingCurveAffine continues to require that its groups provide
uncompressed encodings, because this is relied upon by bellman::groth16.
We can revisit this restriction when that module is refactored as a
separate crate.
2020-06-17 11:31:07 +12:00
Jack Grigg 4e685a847d Merge branch 'pairing-refactor' 2020-06-17 09:48:24 +12:00
Jack Grigg 03f086221b pairing: Move PairingCurveAffine::Prepared to MultiMillerLoop trait
Prepared elements are only used by MultiMillerLoop, and we don't need
the ability to "prepare" G1 elements there.
2020-06-03 20:50:02 +12:00
Jack Grigg b9d6df9133 pairing: Extract Engine::miller_loop into a MultiMillerLoop trait
This enables MultiMillerLoop to be conditionally implemented, for
example in libraries where Engine::pairing supports no-std, but
MultiMillerLoop requires an allocator.
2020-05-31 08:35:02 +12:00
Jack Grigg da2e638c7d pairing: Pass affine references to Engine::pairing 2020-05-31 08:34:07 +12:00
Jack Grigg 57bb18ca6f pairing: Move final_exponentiation into a MillerLoopResult trait 2020-05-31 08:34:03 +12:00
Jack Grigg c8bf2e9fb7 pairing: Separate associated types for MillerLoopResult and Gt 2020-05-31 08:22:52 +12:00
Jack Grigg 534c99327a pairing: Remove Engine::Fq and Engine::Fqe
These are unused now that the Base associated types have been removed
from the group traits.
2020-05-31 08:22:52 +12:00
Jack Grigg 5a40a0fe8f group: Remove CurveProjective::Base and CurveAffine::Base
These associated types were completly unused. The only place we need
information about the base field of an elliptic curve is inside Jubjub
when operating over its coordinates to implement EC math inside the
circuit, and we can handle that either concretely, or with a future
trait specifically for that use-case.
2020-05-31 08:22:52 +12:00
Jack Grigg 0a0e513948 ff: Remove ScalarEngine 2020-05-31 02:06:24 +12:00
Jack Grigg 4edff96321 Fix various lints 2020-05-22 18:52:35 +12:00
Jack Grigg 5f1607c9b5 group: Rename into_*(&self) -> to_*(&self)
Rust naming convention uses the into_ prefix for methods that consume
self, and the to_ prefix for methods that take an immutable reference.
2020-05-20 11:32:12 +12:00
Jack Grigg ceecd32ac4 group: Remove EncodedPoint::from_affine
The EncodedPoint trait is replaced by explicit bounds on the
CurveAffine::{Uncompressed, Compressed} associated types.
2020-05-20 11:32:12 +12:00
Jack Grigg b77f8dddda group: Remove EncodedPoint::{into_affine, into_affine_unchecked}
Replaced by explicit APIs on the CurveAffine trait.

GroupDecodingError has been moved into pairing::bls12_381::ec, as it is
no longer used by the group traits.
2020-05-20 11:32:12 +12:00
Jack Grigg b0a3713d7e group: Add mixed-addition scalar multiplication bounds to CurveAffine
Replaces the explicit CurveAffine::mul trait method.
2020-05-20 11:32:12 +12:00
Jack Grigg 0941dddc13 group: Return subtle::Choice from CurveAffine::is_identity 2020-05-20 11:32:12 +12:00
Jack Grigg b94d567076 group: Direct-to-affine CurveProjective::batch_normalize
Replaces the mutating CurveProjective::batch_normalization API, and
removes the need for CurveProjective::is_normalized.

The new temporary implementation in pairing::bls12_381::ec is adapted
from bls12_381::g1.
2020-05-20 11:32:12 +12:00
Jack Grigg 669f2b43eb group: Return subtle::Choice from Group::is_identity 2020-05-15 10:24:30 +12:00
Jack Grigg ec88778258 group: Add scalar multiplication bounds to Group
The Scalar associated type is moved from CurveProjective to Group.
2020-05-15 10:24:30 +12:00
Jack Grigg 0df950dc0d group: Add Group::Subgroup associated type
For prime-order groups, this may be Self.
2020-05-15 00:20:32 +12:00
Jack Grigg d2aa87f084 group: Move CurveProjective::double to Group 2020-05-15 00:20:32 +12:00
Jack Grigg e3d84280b9 group: Sum + for<'a> Sum<&'a Self> bounds for Group 2020-05-15 00:20:32 +12:00
Jack Grigg 5397d73e0b group: Define group operations on Group trait
The GroupOps trait represents the group operation (addition), and the
combination of the group operation with group inversion (subtraction).
Group inversion (negation) is constrained directly on the Group trait.
2020-05-15 00:20:32 +12:00
Jack Grigg 69f53febcf group: Introduce Group and PrimeGroup traits
Group represents a cryptographic group with a large prime-order subgroup
and a small cofactor. PrimeGroup further constrains the group to have a
cofactor of one.
2020-05-15 00:20:32 +12:00
Jack Grigg 7203a7a30a Remove Engine associated type from CurveAffine and CurveProjective
The type Curve*::Engine::Fr is equivalent to Curve*::Scalar, making
Engine a redundant associated type.
2020-05-15 00:20:32 +12:00
Jack Grigg 8add78a327 Make CurveProjective::double take &self and return Self 2020-05-15 00:20:32 +12:00
Jack Grigg 4f2220fbb1 group: Rename zero and one to identity and generator 2020-05-15 00:20:32 +12:00
Jack Grigg d7d49285d8 Remove explicit std::marker from Sized bound
Sized is always part of the prelude, and binding on std causes
compilation issues for no-std crates.
2020-05-15 00:20:32 +12:00
Jack Grigg c597db59a6 ff: Rename PrimeField::into_repr -> PrimeField::to_repr 2020-05-02 18:55:13 +12:00
Jack Grigg 9114c367f4 ff_derive: Generate modulus representation with correct endianness
Now that PrimeField::ReprEndianness exists, users can obtain a
known-endianness representation from the output of PrimeField::char
(which is a PrimeField::Repr, and should return a representation with
the same endianness as PrimeField::into_repr).
2020-05-02 18:54:02 +12:00
Jack Grigg 15e229509a ff: Move pow_vartime back into Field trait
The only places we don't use constant u64 limbs, we use PrimeField::char
instead (except in a single test where we use a field element).
2020-05-02 18:54:02 +12:00
Jack Grigg 38f87c2e73 ff: Add PrimeField::ReprEndianness associated type
This enables generic code to reliably operate on the bits of an encoded
field element, by converting them to and from a known (little)
endianness.

The BitAnd and Shr bounds on PrimeField are now removed, as users can
perform these operations themselves as needed.
2020-05-02 18:54:02 +12:00
Jack Grigg 55568b4d6e ff: Remove frobenius_map from Field trait
It is only used internally in the bls12_381 crate, and field extensions
aren't exposed anywhere in the Zcash stack.
2020-05-02 16:07:48 +12:00
Jack Grigg 1761ebfb35 ff: Remove SqrtField trait
The sqrt() function is now part of the Field trait. ff_derive returns an
error on fields for which it does not support generating a square root
function.

Note that Fq6 and Fq12 in pairing::bls12_381 leave the function
unimplemented. They will be dropped once the migration to the bls12_381
crate is complete. The equivalent structs in that crate are not exposed.
2020-05-02 16:07:48 +12:00
Jack Grigg 49f119fb03 ff: Remove PrimeFieldRepr trait
The ff::PrimeField::Repr associated type now has the minimal necessary
bounds, which can be satisfied by a newtype around a byte array.
2020-04-23 18:15:14 +12:00
Jack Grigg 08500ee712 ff: PrimeField: BitAnd<u64, Output = u64> + Shr<u32, Output = Self> 2020-04-23 16:23:24 +12:00
Jack Grigg 1fdca393bb ff: PrimeField::{is_even, is_odd} 2020-04-23 16:23:24 +12:00
Jack Grigg 232f0a50b8 ff: Rework BitIterator to work with both u8 and u64 limb sizes
This enables BitIterator to be used with both the byte encoding and limb
representation of scalars.
2020-04-23 16:23:24 +12:00
Jack Grigg fd79de5408 ff: Add PrimeField: From<u64> constraint 2020-04-23 16:23:24 +12:00
Jack Grigg b6457a905b ff: Move pow_vartime into a trait that is generic over the limb size
The trait is implemented by default for u8 and u64, allowing pow_vartime
to be used with both the byte encoding and limb representation of field
elements.
2020-03-28 00:27:59 +13:00
Jack Grigg 6e53cf3c4c group: Take scalar by reference in CurveProjective::recommended_wnaf_for_scalar 2020-03-26 19:00:46 +13:00