Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.12.3 #121

Closed
wants to merge 231 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
231 commits
Select commit Hold shift + click to select a range
9652657
Basic skeleton for pippenger
CPerezz Mar 3, 2020
c0f88a1
Add pippenger fn as pub
CPerezz Mar 3, 2020
44738cb
Add std feature as default
CPerezz Mar 3, 2020
bb52be3
Implement pippenger w/ tests for Bls12_381
CPerezz Mar 3, 2020
adea4fe
Add credits on docs
CPerezz Mar 3, 2020
126ab1f
Add endo feature enabled by default
CPerezz Mar 3, 2020
c53f591
Use Iter instead of &Vec for params of pippenger
CPerezz Mar 4, 2020
5ec2ded
Basic msm_var_base impl
CPerezz Mar 9, 2020
0a69a8b
Refactor Shr impl
CPerezz Mar 9, 2020
3cccf58
Implement reduce for Scalar
CPerezz Mar 15, 2020
415249a
Fix MSM impl & add tests
CPerezz Mar 15, 2020
59570db
Merge pull request #1 from dusk-network/multiexp_2
CPerezz Mar 15, 2020
a28bb32
Expose Scalar constants needed by plonk
CPerezz Mar 17, 2020
bcd12c0
Merge pull request #2 from dusk-network/pub_constants
CPerezz Mar 17, 2020
6b2b19d
Internal repr getter
CPerezz Mar 19, 2020
413e2e5
Rename S as TWO_ADACITY and expose it
CPerezz Mar 19, 2020
46c47b6
Merge branch 'master' into pub_limbs
CPerezz Mar 19, 2020
31805e1
Expose TWO_ADACITY
CPerezz Mar 19, 2020
ff916f0
Merge pull request #3 from dusk-network/pub_limbs
CPerezz Mar 19, 2020
45cd9b8
Implement base-4 conversion for Scalar
CPerezz Mar 23, 2020
830bd72
Fix len & add more tests for base4 transf
CPerezz Mar 23, 2020
436d5f0
Impl PartialOrd & Ord for Scalar
CPerezz Mar 26, 2020
b2f63af
Implement XOR & AND for Scalar
CPerezz Mar 30, 2020
3bc10c2
Gen XOR & AND result from_raw repr
CPerezz Mar 30, 2020
c1b3de3
Add more tests and fix BitAnd for non-ref vals
CPerezz Mar 31, 2020
a2091fc
Impl Iter Sum & Product for Scalar
CPerezz Apr 6, 2020
d7dd7b0
Remove base_4 translation method.
CPerezz Apr 6, 2020
afceb82
Merge branch 'master' into base4
CPerezz Apr 6, 2020
bed7733
Merge pull request #4 from dusk-network/base4
CPerezz Apr 6, 2020
bf195c4
Fix merge errors
CPerezz Apr 6, 2020
48feb0a
Implement serde for `Scalar`
CPerezz Apr 14, 2020
4e201bc
Merge pull request #6 from dusk-network/serde_scalar
CPerezz Apr 14, 2020
c90f437
Implement Serde gor `G1Affine`
CPerezz Apr 15, 2020
4e023ca
Merge pull request #8 from dusk-network/serde_g1_affine
CPerezz Apr 15, 2020
eb4661a
Implement Serde for `G2Affine`
CPerezz Apr 15, 2020
4d71da2
Implement Serde for Fp
CPerezz Apr 15, 2020
a60660e
Fix some dep warnings
CPerezz Apr 15, 2020
8275987
Implement Serde for Fp2
CPerezz Apr 15, 2020
372e8ef
Implement Serde for `Fp6`
CPerezz Apr 15, 2020
bcac76a
Merge pull request #13 from dusk-network/fp_fp6_serde_add
CPerezz Apr 15, 2020
a2af4be
Merge branch 'master' into g2_serde_impl
CPerezz Apr 15, 2020
dfe777f
Implement Serde for G2Prepared
CPerezz Apr 15, 2020
12c6c82
Merge pull request #11 from dusk-network/g2_serde_impl
CPerezz Apr 15, 2020
d020d5a
Impl `random` for `Scalar`
CPerezz Apr 19, 2020
7e9bca7
Merge pull request #15 from dusk-network/random_scalar
CPerezz Apr 19, 2020
497d457
Include docs for crates .io publishing
CPerezz Apr 24, 2020
73e1cf1
Merge branch 'master' of https://github.com/dusk-network/bls12_381
CPerezz Apr 24, 2020
02ac049
Update README.md
CPerezz Apr 24, 2020
a694e58
Apply cargo fmt & exclude gitignore from pkg
CPerezz Apr 25, 2020
0155494
Merge branch 'master' of https://github.com/dusk-network/bls12_381
CPerezz Apr 25, 2020
f070b6d
Changing Dependencies
May 12, 2020
3c23b5c
Setting Scalar field generators
May 13, 2020
5acb5b7
Amend types
May 13, 2020
f091583
Merge pull request #17 from dusk-network/Dependecies
May 13, 2020
3efc257
Implement to_bits conversion for `Scalar`
Jun 2, 2020
4a32034
Apply @zer0 suggestions to bit_conv
Jun 2, 2020
73fc6f2
Merge pull request #19 from dusk-network/bits_repr
CPerezz Jun 2, 2020
cbd3ca2
Bump to v0.1.2
Jul 20, 2020
e93441c
Allow unused pub Gens
Jul 20, 2020
4509a2f
Merge pull request #21 from dusk-network/v0.1.2
CPerezz Jul 20, 2020
adb5ca3
Implement is_one & is_zero for Scalar
Aug 11, 2020
1c49442
Impl pow_of_2 for Scalar
Aug 11, 2020
66f2db1
Run cargo fmt
Aug 11, 2020
5eec1f8
Cargo fmt
Aug 11, 2020
5427837
Merge pull request #24 from dusk-network/one_zero
CPerezz Aug 11, 2020
7b34992
Merge branch 'master' into pow_2
CPerezz Aug 11, 2020
d681f28
Merge pull request #25 from dusk-network/pow_2
CPerezz Aug 11, 2020
59a3035
Bump to v_0.1.3
Aug 11, 2020
65f3af2
Merge pull request #26 from dusk-network/v_0.1.3
CPerezz Aug 11, 2020
37509f0
Update subtle deps & bump 0.1.4
Sep 11, 2020
7430e69
Update CI using GH workflows
Sep 11, 2020
37ce8e4
Fix benches
Sep 11, 2020
d4b4a98
Define markdown as python/sage
Sep 11, 2020
8e42d1e
Merge pull request #32 from dusk-network/add_ci
CPerezz Sep 11, 2020
1038590
Merge pull request #31 from dusk-network/update_subtle
CPerezz Sep 11, 2020
37cdf1f
Add rust toolchain file for nightly as default
Sep 11, 2020
6721106
Take toolchain from toolchain file in CI
Sep 11, 2020
3c208d8
Fix nit
Sep 11, 2020
e158113
Merge pull request #34 from dusk-network/ci_toolchain
CPerezz Sep 11, 2020
9a2fe8f
Remove nightly toolchain spec from nightly workflow
Sep 11, 2020
713188f
Deny broken intra links
Sep 11, 2020
38806d5
Merge pull request #35 from dusk-network/ci_toolchain
CPerezz Sep 11, 2020
6111758
Update Scalar to be compat with Canon 0.4
vlopes11 Oct 29, 2020
1c37dea
Bump version to 0.1.5
vlopes11 Oct 29, 2020
21036de
Changelog fix
vlopes11 Oct 29, 2020
d0a72a3
Merge pull request #37 from dusk-network/vlopes11/bls-canon-upd
vlopes11 Oct 29, 2020
e42cfcf
Add `Canon` deps behind a feature flag
Nov 3, 2020
3ee5e10
Update Releases.md
Nov 3, 2020
a375dff
Bump to v0.2.0
Nov 3, 2020
58b3073
Add optional repos dep to canon feature
Nov 3, 2020
eb084b7
Update CI for fmt toolchain used
Nov 3, 2020
85ac418
Merge pull request #38 from dusk-network/canon_flag
CPerezz Nov 3, 2020
5deaf9b
no-std compatibility fix
vlopes11 Nov 8, 2020
7aad612
Export scalar as `BlsScalar`
vlopes11 Nov 9, 2020
c73c68a
CI feature for no-std check
vlopes11 Nov 9, 2020
cdfdf2b
constants as part of no-std
vlopes11 Nov 9, 2020
dbe3ffe
Minor version to 0.3.0
vlopes11 Nov 9, 2020
9200d4d
Merge pull request #39 from dusk-network/vlope11/scalar-no-std
vlopes11 Nov 9, 2020
e38140a
no-std compat for pairings feature
vlopes11 Dec 24, 2020
efcd349
Serde as non-default feature
vlopes11 Jan 5, 2021
19514df
Add CI for serde feature test
vlopes11 Jan 5, 2021
9544cd1
Serde feature tests warnings
vlopes11 Jan 5, 2021
b5d4d2f
Merge pull request #41 from dusk-network/vlopes11/no-std-pairings
vlopes11 Jan 5, 2021
7a0c658
Implement/derive Canon for all 'groups' types
jules Jan 5, 2021
922a53d
G1Affine to/from_bytes removal
vlopes11 Jan 12, 2021
6c4b85c
Remove CtOption from G1Affine::from_bytes
vlopes11 Jan 12, 2021
aa8d9b7
Downgrade rand to 0.7
vlopes11 Jan 13, 2021
35ae61a
Merge pull request #43 from dusk-network/canon-groups
jules Jan 13, 2021
0e5a9a8
dusk-bytes to/from bytes for public types
vlopes11 Jan 13, 2021
8aa33db
Infinity encoded into unchecked serialization
vlopes11 Jan 14, 2021
b257b8d
Include dusk-bytes Hex for Scalar and Affine
vlopes11 Jan 14, 2021
1503274
Merge branch 'master' into vlopes11/g1affine-raw-bytes
vlopes11 Jan 14, 2021
da45f5e
Update `dusk-bytes` to `v0.1`
vlopes11 Jan 21, 2021
b7d18e2
v0.5.0 released
vlopes11 Jan 21, 2021
4bc85e4
Merge pull request #45 from dusk-network/vlopes11/g1affine-raw-bytes
vlopes11 Jan 21, 2021
16d8378
Update dusk-bytes
vlopes11 Jan 22, 2021
2537a42
Merge pull request #49 from dusk-network/vlopes11/update-dusk-bytes
vlopes11 Jan 22, 2021
6dc0c63
Fixes #50 - Incorrect encoding for unchecked bytes
vlopes11 Jan 25, 2021
59a3268
Fix changelog
vlopes11 Jan 26, 2021
19392f0
Fix changelog version
vlopes11 Jan 26, 2021
7ae8d67
Merge pull request #51 from dusk-network/vlopes11/50-unchecked-bytes
vlopes11 Jan 26, 2021
ac479b7
Issue #52 - Update canonical to v0.5
vlopes11 Jan 27, 2021
a1beb8b
Merge pull request #53 from dusk-network/vlopes11/bump-canonical
vlopes11 Jan 27, 2021
16a6288
Support no_std without errors
Mar 10, 2021
44953b7
Update CHANGELOG
Mar 11, 2021
b37134f
Move tests to test modules begind test feature flag
Mar 11, 2021
1705ae8
Fix broken_intra_doc_links to new syntax for 2021
Mar 11, 2021
f049c48
Update Canonical to v0.6
Apr 12, 2021
78f406c
Fix crate benchmarks
Apr 12, 2021
0398ef0
Merge pull request #59 from dusk-network/fix_benches
CPerezz Apr 12, 2021
e956382
Merge pull request #58 from dusk-network/canonuical_0.6
CPerezz Apr 12, 2021
d1c8ab9
Update CHANGELOG.md
Apr 12, 2021
0e5c188
Bump to dusk-bls12_381-0.7
Apr 12, 2021
28b79ae
Merge pull request #55 from dusk-network/release-0.7
CPerezz Apr 12, 2021
22e1255
Fix new Canonical version impl
Apr 14, 2021
2e4706a
Update CHANGELOG.md
Apr 14, 2021
2271f22
Merge pull request #62 from dusk-network/fix_canonical
CPerezz Apr 14, 2021
7f627d1
Add canon feature build & test to CI
Apr 14, 2021
9afad23
Merge pull request #64 from dusk-network/update_ci
CPerezz Apr 14, 2021
7838c34
Update README.md
Apr 14, 2021
13e9e91
Update CHANGELOG
Apr 14, 2021
5c79f3e
Remove unnecesary .into()s from Canon impls
Apr 14, 2021
3e58cb6
Merge pull request #68 from dusk-network/canon_intos
CPerezz Apr 14, 2021
be7053b
Bump to dusk-bls12_381-rc.0
Apr 14, 2021
e0465d1
Update CHANGELOG.md
Apr 27, 2021
f30269f
Bump to v0.8.0
Apr 27, 2021
5de2898
Update CHANGELOG.md
Apr 28, 2021
7055275
Merge pull request #67 from dusk-network/release-0.8.0-rc.0
CPerezz Apr 28, 2021
54bc51f
Fix doc comment
moCello Feb 24, 2022
96c4930
Update rust edition to 2021
moCello Feb 24, 2022
1023e4b
Update `canonical` and `canonical-derive` to v0.7
moCello Feb 24, 2022
b1412f8
Update CHANGELOG
moCello Feb 24, 2022
35e10b8
Bump to v0.9
moCello Feb 24, 2022
b40a7cd
Optimize `invert` of `Scalar`
moCello Mar 4, 2022
043fe96
Optimize `invert` with respect to constant time
moCello Mar 4, 2022
43fb8af
Update CHANGELOG
moCello Mar 4, 2022
ed4d87c
Add check for zero
moCello Mar 4, 2022
09dee60
Update CHANGELOG
moCello May 25, 2022
2c679a2
Bump to `v0.10.0`
moCello May 25, 2022
ba59c8b
Apply patch to improve G2 arithmetic and pairings
xevisalle May 27, 2022
5ea3bda
Add patch to have fast subgroup check for is_torsion_free
xevisalle May 27, 2022
f05bc09
Update CHANGELOG.md and fix fmt
xevisalle May 27, 2022
00efc28
Merge pull request #87 from dusk-network/apply_patch
xevisalle Jul 22, 2022
2c0838b
Add `Hash` implementation to `BlsScalar`
Jul 22, 2022
d751be7
Update CHANGELOG.md
Jul 22, 2022
a477023
Merge pull request #89 from dusk-network/blsscalar-hash
ureeves Jul 25, 2022
70bc1dd
Add `rkyv` implementation behind feature gate
Jul 25, 2022
343bed3
Update CHANGELOG.md
Jul 25, 2022
dfe60df
Merge pull request #91 from dusk-network/rkyv
ureeves Jul 26, 2022
435f7c0
Bump version to `0.10.1`
Jul 27, 2022
7e2f4c8
Merge pull request #92 from dusk-network/release-0.10.1
ureeves Jul 27, 2022
2ad7f19
Remove `Sized` bound on `G2Prepared` `rkyv` impl
Jul 28, 2022
2912eea
Merge pull request #95 from dusk-network/g2prepared-rkyv-unsized
ureeves Aug 1, 2022
30fd5e7
Add `rkyv-impl` feature and implement `CheckBytes`
Aug 16, 2022
838b267
Bump version to `0.11.0-rc.0`
Aug 16, 2022
b84d18a
Merge pull request #96 from dusk-network/bytecheck
ureeves Aug 16, 2022
d659d95
Bump version to `0.11.0`
Aug 17, 2022
0cda1b5
Merge pull request #97 from dusk-network/release-0.11.0
ureeves Aug 17, 2022
10465a4
Add support for `rkyv-impl` under `no_std`
Sep 14, 2022
6f2083b
Update toolchain to `nightly-2022-08-08`
Sep 14, 2022
2dacfbb
Merge pull request #98 from dusk-network/rkyv-no-default
ureeves Sep 14, 2022
868e6f1
Introduce type wrapping `subtle::Choice`
Sep 23, 2022
8606daf
Derive `rkyv` traits instead of manual impl
Sep 23, 2022
ea895f1
Merge pull request #99 from dusk-network/wrapped-choice
Oct 18, 2022
8cf118c
Bump version to `0.11.1`
Oct 19, 2022
0ed3c5d
Update CHANGELOG
Oct 19, 2022
344a067
Merge pull request #101 from dusk-network/release-0.11.1
Oct 19, 2022
9d2cd02
Use `rkyv/alloc` on `rkyv-impl` feature
Oct 20, 2022
5a18a03
Make archived structs available only on `rkyv-impl`
Oct 20, 2022
2943e66
Merge pull request #102 from dusk-network/rkyv-alloc
Oct 21, 2022
fac5ec9
Bump version to `0.11.2`
Oct 26, 2022
75e46ec
Merge pull request #103 from dusk-network/release-0.11.2
Oct 26, 2022
4906be0
Update issue links
moCello May 31, 2022
96b039e
Merge pull request #88 from dusk-network/mocello/changelog
moCello Apr 25, 2023
6da1e45
Fix invalid `Cargo.toml` categories
HDauven Apr 29, 2023
35a45ef
Fix documentation links
HDauven Apr 29, 2023
df5e130
Remove docs in `Cargo.toml`
HDauven Apr 29, 2023
dbb90b6
Merge pull request #105 from dusk-network/fix-cargo-toml-75-93
HDauven Apr 29, 2023
9be37c7
Bump version to `0.11.3`
May 17, 2023
ce28729
Merge pull request #107 from dusk-network/release-0.11.3
May 17, 2023
64df1c6
Remove scalar GENERATOR
moCello May 19, 2023
458bf7a
Merge pull request #111 from dusk-network/mocello/100_generator
moCello May 22, 2023
90c79c2
Remove canonical and canonical_derive
moCello May 22, 2023
e25560a
Merge pull request #112 from dusk-network/mocello/108_canonical
moCello May 22, 2023
db23f1c
Update CI
HDauven May 26, 2023
286fc64
Merge pull request #115 from dusk-network/standardize-ci
HDauven May 30, 2023
d85dd8b
Seperate our additions from the original crate
moCello May 26, 2023
9e6db83
Merge pull request #113 from dusk-network/mocello/109_seperate
moCello Jun 1, 2023
b33dfde
Merge contents of upstream `zkcrypto/bls12_381`
Jun 1, 2023
7916590
Relax `BlsScalar::random` to only require `RngCore`
Jun 5, 2023
f374036
Remove duplicate `Scalar::sqrt` and `Scalar::random`
Jun 7, 2023
1dcced1
Merge pull request #116 from dusk-network/zkcrypto
moCello Jun 7, 2023
61fd9c7
Bump version to '0.12.0`
moCello Jun 7, 2023
c9a6025
Update CHANGELOG
moCello Jun 7, 2023
ca540f0
Merge pull request #119 from dusk-network/release-0.12.0
moCello Jun 7, 2023
f1a784c
Move dusk modules to separate files
moCello Oct 10, 2023
2f02918
Expose `scalar::GENERATOR`
moCello Oct 11, 2023
211e087
Fix clippy in dusk modules
moCello Oct 11, 2023
92be646
Merge pull request #120 from dusk-network/mocello/fix_compatibility
moCello Oct 11, 2023
738e03f
Bump to v0.12.1
moCello Oct 11, 2023
6165e27
Merge pull request #122 from dusk-network/release-0.12.1
moCello Oct 11, 2023
737dabc
Fix no-default-features
moCello Oct 11, 2023
eaeb28b
Merge pull request #123 from dusk-network/mocello/fix-no-default-feature
moCello Oct 11, 2023
0d5af01
Bump to v0.12.2
moCello Oct 11, 2023
c9790e0
Merge pull request #124 from dusk-network/release-0.12.2
moCello Oct 11, 2023
c210972
Add uniform random scalar generation
moCello Oct 23, 2023
7990d15
Merge pull request #126 from dusk-network/mocello/random
moCello Oct 25, 2023
5b5b174
Add hash impl to scalar
HDauven Oct 27, 2023
6b5716c
Update CHANGELOG
HDauven Oct 27, 2023
3ebde2e
Merge pull request #127 from dusk-network/scalar-hash-equality
HDauven Oct 30, 2023
1945a56
Bump to v0.12.3
moCello Nov 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2018"

[dependencies]
byteorder = { version = "^1.2.3", default-features = false, features = ["i128"] }
rayon = "1.3.0"

[package.metadata.docs.rs]
rustdoc-args = [ "--html-in-header", "katex-header.html" ]
Expand Down
113 changes: 111 additions & 2 deletions src/multiscalar_mul.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Multiscalar multiplication implementation using pippenger algorithm.
use crate::{g1::G1Projective, scalar::Scalar};
use crate::{
g1::{G1Affine, G1Projective},
scalar::Scalar,
};
use byteorder;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -175,12 +178,108 @@ fn to_radix_2w(scalar: &Scalar, w: usize) -> [i8; 43] {
digits
}

#[cfg(feature = "std")]
/// Performs a Variable Base Multiscalar Multiplication.
pub fn msm_variable_base(points: &[G1Affine], scalars: &[Scalar]) -> G1Projective {
use rayon::prelude::*;

let c = if scalars.len() < 32 {
3
} else {
ln_without_floats(scalars.len()) + 2
};

let num_bits = 255usize;
let fr_one = Scalar::one();

let zero = G1Projective::identity();
let window_starts: Vec<_> = (0..num_bits).step_by(c).collect();

let window_starts_iter = window_starts.into_par_iter();

// Each window is of size `c`.
// We divide up the bits 0..num_bits into windows of size `c`, and
// in parallel process each such window.
let window_sums: Vec<_> = window_starts_iter
.map(|w_start| {
let mut res = zero;
// We don't need the "zero" bucket, so we only have 2^c - 1 buckets
let mut buckets = vec![zero; (1 << c) - 1];
scalars
.iter()
.zip(points)
.filter(|(s, _)| !(*s == &Scalar::zero()))
.for_each(|(&scalar, base)| {
if scalar == fr_one {
// We only process unit scalars once in the first window.
if w_start == 0 {
res = res.add_mixed(base);
}
} else {
let mut scalar = scalar.reduce();

// We right-shift by w_start, thus getting rid of the
// lower bits.
scalar.divn(w_start as u32);

// We mod the remaining bits by the window size.
let scalar = scalar.0[0] % (1 << c);

// If the scalar is non-zero, we update the corresponding
// bucket.
// (Recall that `buckets` doesn't have a zero bucket.)
if scalar != 0 {
buckets[(scalar - 1) as usize] =
buckets[(scalar - 1) as usize].add_mixed(base);
}
}
});

let mut running_sum = G1Projective::identity();
for b in buckets.into_iter().rev() {
running_sum = running_sum + b;
res += &running_sum;
}

res
})
.collect();

// We store the sum for the lowest window.
let lowest = *window_sums.first().unwrap();
// We're traversing windows from high to low.
window_sums[1..]
.iter()
.rev()
.fold(zero, |mut total, sum_i| {
total += sum_i;
for _ in 0..c {
total = total.double();
}
total
})
+ lowest
}

fn ln_without_floats(a: usize) -> usize {
// log2(a) * ln(2)
(log2(a) * 69 / 100) as usize
}
fn log2(x: usize) -> u32 {
if x <= 1 {
return 0;
}

let n = x.leading_zeros();
core::mem::size_of::<usize>() as u32 * 8 - n
}

mod tests {
use super::*;

#[cfg(feature = "std")]
#[test]
fn multiscalar_mul() {
fn pippenger_test() {
// Reuse points across different tests
let mut n = 512;
let x = Scalar::from(2128506u64).invert().unwrap();
Expand Down Expand Up @@ -208,4 +307,14 @@ mod tests {
n = n / 2;
}
}

#[cfg(feature = "std")]
#[test]
fn msm_variable_base_test() {
let points = vec![G1Affine::generator()];
let scalars = vec![Scalar::from(100u64)];
let premultiplied = G1Projective::generator() * Scalar::from(100u64);
let subject = msm_variable_base(&points, &scalars);
assert_eq!(subject, premultiplied);
}
}
35 changes: 34 additions & 1 deletion src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use core::convert::TryFrom;
use core::fmt;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Shr, Sub, SubAssign};

use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

Expand Down Expand Up @@ -273,6 +273,12 @@ impl Scalar {
(&Scalar(val)).mul(&R2)
}

/// Reduces the scalar and returns it multiplied by the montgomery
/// radix.
pub fn reduce(&self) -> Scalar {
Scalar::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0)
}

/// Squares this element.
#[inline]
pub const fn square(&self) -> Scalar {
Expand Down Expand Up @@ -611,6 +617,33 @@ impl Scalar {

Scalar([d0 & mask, d1 & mask, d2 & mask, d3 & mask])
}

/// SHR impl
#[inline]
pub fn divn(&mut self, mut n: u32) {
if n >= 256 {
*self = Self::from(0);
return;
}

while n >= 64 {
let mut t = 0;
for i in self.0.iter_mut().rev() {
core::mem::swap(&mut t, i);
}
n -= 64;
}

if n > 0 {
let mut t = 0;
for i in self.0.iter_mut().rev() {
let t2 = *i << (64 - n);
*i >>= n;
*i |= t;
t = t2;
}
}
}
}

impl<'a> From<&'a Scalar> for [u8; 32] {
Expand Down