Commit graph

200 commits

Author SHA1 Message Date
Henry de Valence
6503378fef Save 2.5% on variable-base scmul by squeezing some multiplications.
This saves 4 point doublings by unwrapping the first loop iteration,
as well as 63 field multiplications (one per iteration) by managing
curve model choice explicitly.
2019-05-08 21:04:13 -07:00
Henry de Valence
b52c7ddafe Suppress a generated <br> which breaks KaTeX. 2019-05-06 15:18:16 -07:00
Mark Blunk
50f46c7685 fix typo in torsion documentation 2019-04-02 14:33:27 -06:00
Henry de Valence
b8c62ec4b4 Attempt to fix cfg(rustdoc) on docs.rs
Building the docs currently doesn't work, because rustdoc enables parts of the
code (to document them) which then don't check (because there are missing
exports or dependencies).  This **should** fix the issue, but there's no way to
test without publishing a new version.
2019-02-15 11:18:25 -08:00
Henry de Valence
092ff52cb0 Remove constant-time multiscalar precomputation.
This doesn't (yet) give any speedup over the non-precomputed multiscalar
multiplication, and it's not clear that it's a good idea to commit to
supporting it in the future.  Removing it means that it's not committed-to as
part of the public API, but the source is still there in the tree if we want to
revisit it later.
2019-02-13 12:26:31 -08:00
Henry de Valence
8adcfb7fa3 Simplify length checking. 2019-02-12 12:52:02 -08:00
Henry de Valence
727ba86292 Rework the vartime precomputation trait to be Option-al 2019-02-12 12:52:02 -08:00
Henry de Valence
e693d7f020 fixup AVX2 ascii blocks so they don't run as doctests 2019-02-12 12:52:02 -08:00
Henry de Valence
8b0ad2b03d Add vector implementation of precomputation. 2019-02-12 12:52:02 -08:00
Henry de Valence
c6acdfd5e2 Add serial implementation of precomputation. 2019-02-12 12:52:02 -08:00
Henry de Valence
5daff66079 Move cfgs into outer module. 2019-02-12 12:52:02 -08:00
Henry de Valence
e3ca8961fc Fix rustdoc feature flags to compile on stable. 2019-01-18 14:19:39 -08:00
Henry de Valence
9ddbf1ba83 Add notes on the IFMA implementation. 2019-01-18 14:19:39 -08:00
Henry de Valence
dc8431429f Refactor documentation to use doc_cfg. 2019-01-18 14:19:39 -08:00
Henry de Valence
5700add507 Clean up warnings. 2019-01-18 14:19:30 -08:00
Henry de Valence
fa42708257 Change naming scheme for multiplication accumulators. 2019-01-18 14:19:30 -08:00
Henry de Valence
a84f9eafe4 Allow non_snake_case in the IFMA field and edwards modules. 2019-01-18 14:19:30 -08:00
Henry de Valence
06766bfdea Delay shuffles that cross the "Great Wall of Intel". 2019-01-18 14:19:30 -08:00
Henry de Valence
33aa544cf4 Add IFMA constants file. 2019-01-18 14:19:30 -08:00
Henry de Valence
f825426ff4 Add impl Default, Identity for ExtendedPoint, CachedPoint. 2019-01-18 14:19:30 -08:00
Henry de Valence
da62569355 Add lookup table support. 2019-01-18 14:19:30 -08:00
Henry de Valence
6cec313f16 Add impl of Sub and Neg for Edwards points. 2019-01-18 14:19:30 -08:00
Henry de Valence
6faaef21df Add mul_by_pow_2 for IFMA 2019-01-18 14:19:30 -08:00
Henry de Valence
8e38ff2859 Add Edwards doubling implementation. 2019-01-18 14:19:30 -08:00
Henry de Valence
c4617b28a9 Add squaring implementation. 2019-01-18 14:19:30 -08:00
Henry de Valence
a62571b003 Reduce field elements during conversion to vector form.
The IFMA types have tighter bounds than the u64 types.
2019-01-18 14:19:30 -08:00
Henry de Valence
3f21bb3b58 Add skeleton of parallel Edwards formulas. 2019-01-18 14:19:30 -08:00
Henry de Valence
5880fced2b Add skeleton of shuffle and blend functions. 2019-01-18 14:19:30 -08:00
Henry de Valence
d90bb779ca Delay all computations using x[0], y[0]
Since computation of the 0 term in reduction requires a multiplication with a
4-cycle latency, this ensures that the rest of the computation can start before
the 0 term is finished
2019-01-18 14:19:30 -08:00
Henry de Valence
c925c76342 Change the strategy for reducing the high half of the product. 2019-01-18 14:19:30 -08:00
Henry de Valence
607b839032 Change size of limbs in the reduction test. 2019-01-18 14:19:30 -08:00
Henry de Valence
14163131f9 Lift the reduction state of the field elements into the type system.
This splits the `FieldElement51x4` type into two types:

- `F51x4Reduced` (with reduced limbs)
- `F51x4Unreduced` (with unreduced limbs)

The reduction is implemented as a `From` impl to convert one type to the other.

The output of a multiplication is now a `F51x4Unreduced`.  The reason is that
the inputs to IFMA operations must be at most 52 bits, so it's not possible to
perform an addition of (51+epsilon)-bit values and still be small enough to be
used as an input to multiplication.  So, it doesn't make sense to perform a
reduction at the end of a multiplication, because the reduced values will be
fed into an addition or subtraction, which then needs to be re-reduced.
2019-01-18 14:19:30 -08:00
Henry de Valence
ff49673214 Add impl Mul<(u32, u32, u32, u32)> for FieldElement51x4. 2019-01-18 14:19:30 -08:00
Henry de Valence
70199d6094 Add impl Mul<FieldElement51x4> for FieldElement51x4. 2019-01-18 14:19:30 -08:00
Henry de Valence
aa73d7b1bc Add a FieldElement51x4 type. 2019-01-18 14:19:30 -08:00
Henry de Valence
be77af1cd8 Remove unnecessary feature gate.
The vector backend doesn't generate constants in build.rs, so this isn't necessary.
2019-01-18 14:18:55 -08:00
Henry de Valence
47a164da4d Replace avx2_backend with simd_backend (autoselects AVX2/IFMA) 2019-01-18 01:52:17 -08:00
Henry de Valence
9ed2128a10 Add stub code for IFMA intrinsics in Rust. 2019-01-18 01:52:17 -08:00
Henry de Valence
0a97f5fe8c Don't compile the serial scalar_mul when using the vector one. 2019-01-18 01:52:07 -08:00
Henry de Valence
f1d2b5182b Restructure source tree into serial and vector backends.
This begins to attempt to restructure the source tree so that the common parts
are common and the different parts are different.

The backend is now split into two parts:
- serial (containing the implementation using serial formulas and mixed-model arithmetic).
- vector (containing the implementation using parallel formulas and single-model arithmetic).

The serial scalar_mul tree is now under backend::serial::scalar_mul.
The avx2 scalar_mul tree is now under backend::avx2::scalar_mul.
2019-01-18 01:49:40 -08:00
Henry de Valence
94cb3e7842 Rename field types based on radix.
`FieldElement32` -> `FieldElement2625`
`FieldElement64` -> `FieldElement51`
`Scalar32` -> `Scalar29`
`Scalar64` -> `Scalar52`

This naming is more accurate and would let us add an ADX backend later.
2019-01-17 22:21:55 -08:00
Henry de Valence
a116fd9679 test subtle 2.0 2018-11-05 12:06:23 -08:00
Henry de Valence
53fcd1060d Change internal API to use ConditionallySelectable 2018-11-02 14:17:43 -07:00
Henry de Valence
288625418d Migrate to packed_simd from core::simd 2018-07-26 12:40:34 -07:00
Tony Arcieri
10e8abf926 Unify alloc and std cargo features
This change provides a common convention for using allocator-dependent
features with:

    #![cfg(feature = "alloc")]

When available, `Vec` is imported consistently as `prelude::Vec`, which
means modules that need access to `Vec` can simply do:

    use prelude::*;

and if an allocator is available, `Vec` will be in the crate prelude.

This allows all `alloc` vs `std` gating to be handled in `lib.rs`,
`build.rs`, and `prelude.rs` so the rest of the codebase doesn't have to
do any gating whatsoever.
2018-07-23 10:50:21 -07:00
Henry de Valence
f675f4cd2b fixup! Allow Options in the VartimeMultiscalarMul trait 2018-07-20 20:33:33 -07:00
Henry de Valence
5a58f42155 Point to https://ristretto.group since our notes live there now. 2018-07-20 12:24:28 -07:00
Henry de Valence
1e74cb3e56 Replace Scalar::from_u64 with From impls
Unfortunately, Rust selects `i32` as the type for an integer literal
when the literal has no other type constraints.  This means that someone
cannot write `Scalar::from(1)`, as Rust will choose `i32` as the type for
`1`, and we don't `impl From<i32> for Scalar`.

We could implement `From` conversions for signed integers, but since
`Scalar` operations should be constant-time by default, this would
require us to extract the sign bit of the integer and use it to
conditionally select between the positive and negative of Scalar
constructed from the value bits.  This is more expensive than the
unsigned operation, and I don't think it's what anyone really wants.

Making API consumers specify that their literals are unsigned is
slightly annoying, but better than the above alternative.

It would also be nice to change `Scalar::from_hash` to be
`impl<D: Digest<OutputSize = U64>> From<D> for Scalar`,
but this isn't currently allowed by Rust (since that `impl` "could"
conflict with the `impl From<u8>` if someone decided that `u8` should
`impl Digest`).
2018-07-19 08:39:09 -07:00
Isis Lovecruft
f43f4f9770
Update year in copyright notices to 2018. 2018-07-05 00:30:27 +00:00
Sean Bowe
61d6d89cd8
Fix comment describing Montgomery adjustment factor's value. 2018-07-02 10:41:45 -06:00
Henry de Valence
16f39c82e5 Remove yolocrypto from avx2_backend 2018-06-18 13:32:04 -07:00
Henry de Valence
d791047aac Rewrite notes and documentation. 2018-06-18 13:22:03 -07:00
Henry de Valence
15f97221ba Suppress extraneous warnings 2018-06-15 13:44:31 -07:00
Henry de Valence
7198719419 Document bounds on FieldElement32x4 functions 2018-06-15 13:36:38 -07:00
Henry de Valence
9f5bd8c4c0 Rename reduce32 to reduce and have it return its result.
This means that all FieldElement32x4 operations return values, vs mutating interior state.
2018-06-14 15:54:36 -07:00
Henry de Valence
bab1ebbeb8 Replace scale_by_curve_constants by a Mul<(u32,u32,u32,u32)> impl 2018-06-14 15:42:20 -07:00
Henry de Valence
6ef9e9dcfb Make publicity a little more consistent 2018-06-14 15:22:16 -07:00
Henry de Valence
97292fef91 Move packing functions to top of the module 2018-06-14 15:21:51 -07:00
Henry de Valence
4dc219910a Move blend_lanes into the blend function 2018-06-14 15:13:53 -07:00
Henry de Valence
64b1b481ba Rewrite diff_sum in terms of shuffle, blend, negate 2018-06-14 14:59:11 -07:00
Henry de Valence
fe51adad31 Don't expose u32x8 unpacking functions 2018-06-14 14:32:13 -07:00
Henry de Valence
c46ec9638c Add documentation 2018-06-14 14:23:24 -07:00
Henry de Valence
eea3eadf5b Replace special-case swap_{AB,CD} methods with general shuffles 2018-06-14 14:23:24 -07:00
Henry de Valence
02296fafb5 Delete unused constant 2018-06-14 14:23:24 -07:00
Henry de Valence
25d9f3f6ca Eliminate vector constants from edwards module 2018-06-14 14:23:24 -07:00
Henry de Valence
cc8728b2a6 Add comment about rustc-constant-info to blend function 2018-06-14 14:23:24 -07:00
Henry de Valence
28f10bc183 Change Lanes::ALL to Lanes::ABCD for consistency 2018-06-14 14:23:24 -07:00
Henry de Valence
794ed5c8e3 Rewrite the doubling horrorshow 2018-06-14 14:23:24 -07:00
Henry de Valence
30a2b01c05 Add more selectors to the Lanes enum 2018-06-14 14:19:46 -07:00
Henry de Valence
64cb999866 Make platform-vector lanes constants private 2018-06-14 14:19:26 -07:00
Henry de Valence
00d8b6ea4f Change negate_D, negate_D_lazy to impl Neg, negate_lazy 2018-06-14 14:17:36 -07:00
Henry de Valence
14ce6d3da6 Add a shuffling abstraction for FieldElement32x4 2018-06-14 14:10:37 -07:00
Henry de Valence
c8dc2a6418 Implement addition for FieldElement32x4 2018-06-14 14:10:32 -07:00
Henry de Valence
bd1e3c5f3e some rustfmt changes 2018-06-14 14:05:52 -07:00
Henry de Valence
149c5004e8 Use multiscalar traits for the backend implementations. 2018-05-15 11:33:38 -07:00
Henry de Valence
34c43c20a9 Rework backend selection code.
Each backend can now be selected by an individual feature:

- `u32_backend` for `backend::u32`;
- `u64_backend` for `backend::u64`;
- `avx2_backend` for `backend::avx2`;

The `u64_backend` is selected by default, since most people use X64 and we have
no way to select based on target (see discussion in #126).  However, these
changes mean that it is possible to select the backend explicitly, and if we
had the ability to select target-default features, we could do so easily.
2018-05-14 17:43:54 -07:00
Henry de Valence
9b6c932635 Rename 'precomputed_tables' to the more accurate 'stage2_build' 2018-05-14 15:41:45 -07:00
Henry de Valence
185bbd3da8 Fix build on recent nightlies (was broken due to type inference failure) 2018-05-02 17:30:10 -07:00
Henry de Valence
285e57f2ff
Merge pull request #128 from hdevalence/feature/avx2-docs
Update docs for AVX2 backend
2018-04-08 17:16:05 -07:00
Henry de Valence
67ba201835 Update AVX2 documentation 2018-04-08 16:59:57 -07:00
Henry de Valence
9fc5602ce7 Split AVX2 docs into markdown file 2018-04-08 15:15:23 -07:00
Henry de Valence
6b768c2a1a Change AVX2 backend to use width-8 tables 2018-04-05 16:20:47 -07:00
Henry & Isis
753a0292de
Rename OddLookupTable to NafLookupTable5.
An OddLookupTable corresponds to a non-adjacent form of width 5.
2018-04-05 04:48:28 +00:00
Henry de Valence
7e0ddf6b98 Rewrite NAF code to work with more window sizes
Change Scalar::non_adjacent_form() to take a width parameter.

This rewrite also makes it faster, although it's probably a ways off
from optimal. I don't know how much it matters.

TODO: write up description of why this computes the same thing.

Thanks to @oleganza for pointing out an error reading bits across words
in an earlier version of this code.
2018-04-04 21:13:09 -07:00
Henry de Valence
5f136fbd0c Remove some warnings.
Not all of the warnings are removed, since although this code works, it still
needs a significant amount of cleanup, editing, and polish.
2018-04-04 10:25:42 -07:00
Henry de Valence
b0bda0278c Drop the stdsimd crate in favor of core::{simd, arch}.
This change required some work, because the to-be-stabilized SIMD functions
don't allow non-constant `imm8`s.  Previously, the `stdsimd` functions had a
constifying macro that ensured that the immediates were known.  The dalek code
used this to build helper functions which would be inlined into different
places where the immediates were known.  Unfortunately, since constexprs aren't
fully supported in Rust yet, this is done by a hidden compiler attribute, and
there's no way to propagate "constness".

To deal with this, some of the functions are specialized (e.g.,
`square_and_negate_D` instead of taking a mask), and others use an enum.
2018-04-03 17:58:23 -07:00
Henry de Valence
4a648df713 Feature-gate multiscalar impls on alloc 2018-03-26 17:58:31 -07:00
Henry de Valence
7ef6a1e6fa Reorganize AVX2 point code 2018-03-26 17:41:05 -07:00
Henry de Valence
e8b053b281 Remove AVX2 addition formulas
Only the readdition formulas are actually used by scalar multiplication, so
there's no reason to implement vectorized addition.
2018-03-26 17:41:05 -07:00
Henry de Valence
c73a0fd0d6 Remove AVX2 fixed-base code.
This was faster than the non-AVX2 code, but the serial code is already so fast that there's no reason not to use it.
2018-03-26 17:41:05 -07:00
Henry de Valence
0c4e7188a0 Pull out vartime double-base scalar mul code 2018-03-26 17:41:05 -07:00
Henry de Valence
2d99892eab Pull out variable-time straus implementation 2018-03-26 17:41:05 -07:00
Henry de Valence
2864a422bc Pull out constant-time straus implementation 2018-03-26 16:04:17 -07:00
Henry de Valence
ac739a3edd Split out constant-time variable-base scalar mul.
The serial (`u32`/`u64`) implementations use a multiple curve models, passing
between extended and projective coordinates when performing addition and
doubling (respectively). But the AVX2 backend doesn't, so in order to write a
single scalar mult implementation, we have to either abstract over the curve
models or have two implementations.

A generic solution is possible but extremely unreadable: the scalar mul
implementation would be parameterized over the point types used by the serial
implementations, with many where clauses describing how the types relate. The
AVX2 types could then be substituted in the appropriate places.

Instead we just duplicate the code into the `avx2` backend.
2018-03-26 16:01:51 -07:00
Henry de Valence
76a8d43a04 Create a new scalar_mul module hierarchy.
This should contain generic implementations of scalar multiplication algorithms
that can be used with multiple backends.  The goal is to move the existing
scalar multiplication code into this submodule, then call it from the
user-facing API.  This can also contain code for things we can't do now, like
multiscalar multiplication with precomputation.
2018-03-26 14:34:28 -07:00
Henry de Valence
d6b8389428 Use criterion.rs instead of libtest for benchmarks.
Since Criterion can only benchmark public API, these changes just drop
all internal benchmarks (e.g., benchmarks for field operations). But
those are usually microbenchmarks whose meaning is kind of questionable
anyways, so I don't think this is a big loss.

The `bench` feature disappears, since Criterion works on stable Rust.
2018-03-25 17:14:37 -07:00
Henry de Valence
c9239f54e9 Merge branch 'fix/warnings' into develop 2018-03-22 12:34:31 -07:00
Henry de Valence
f2e44898ee Suppress warnings about square() on UnpackedScalars 2018-03-22 12:17:23 -07:00
Henry de Valence
e73b635fe0 Remove unused constants 2018-03-22 12:17:23 -07:00
Henry de Valence
844da9712b Fix AVX2 docs formatting, remove obsolete AVX512 note 2018-03-22 12:13:39 -07:00