fullcodec_plonk/
util.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use dusk_bls12_381::{
8    BlsScalar, G1Affine, G1Projective, G2Affine, G2Projective,
9};
10use rand_core::RngCore;
11use sp_std::vec::Vec;
12
13/// Returns a vector of BlsScalars of increasing powers of x from x^0 to x^d.
14pub(crate) fn powers_of(
15    scalar: &BlsScalar,
16    max_degree: usize,
17) -> Vec<BlsScalar> {
18    let mut powers = Vec::with_capacity(max_degree + 1);
19    powers.push(BlsScalar::one());
20    for i in 1..=max_degree {
21        powers.push(powers[i - 1] * scalar);
22    }
23    powers
24}
25
26/// Generates a random BlsScalar using a RNG seed.
27pub(crate) fn random_scalar(rng: impl RngCore) -> BlsScalar {
28    BlsScalar::random(rng)
29}
30
31/// Generates a random G1 Point using an RNG seed.
32pub(crate) fn random_g1_point(rng: impl RngCore) -> G1Projective {
33    G1Affine::generator() * random_scalar(rng)
34}
35/// Generates a random G2 point using an RNG seed.
36pub(crate) fn random_g2_point(rng: impl RngCore) -> G2Projective {
37    G2Affine::generator() * random_scalar(rng)
38}
39
40/// This function is only used to generate the SRS.
41/// The intention is just to compute the resulting points
42/// of the operation `a*P, b*P, c*P ... (n-1)*P` into a `Vec`.
43pub(crate) fn slow_multiscalar_mul_single_base(
44    scalars: &[BlsScalar],
45    base: G1Projective,
46) -> Vec<G1Projective> {
47    scalars.iter().map(|s| base * *s).collect()
48}
49
50// while we do not have batch inversion for scalars
51use core::ops::MulAssign;
52
53pub fn batch_inversion(v: &mut [BlsScalar]) {
54    // Montgomery’s Trick and Fast Implementation of Masked AES
55    // Genelle, Prouff and Quisquater
56    // Section 3.2
57
58    // First pass: compute [a, ab, abc, ...]
59    let mut prod = Vec::with_capacity(v.len());
60    let mut tmp = BlsScalar::one();
61    for f in v.iter().filter(|f| f != &&BlsScalar::zero()) {
62        tmp.mul_assign(f);
63        prod.push(tmp);
64    }
65
66    // Invert `tmp`.
67    tmp = tmp.invert().unwrap(); // Guaranteed to be nonzero.
68
69    // Second pass: iterate backwards to compute inverses
70    for (f, s) in v
71        .iter_mut()
72        // Backwards
73        .rev()
74        // Ignore normalized elements
75        .filter(|f| f != &&BlsScalar::zero())
76        // Backwards, skip last element, fill in one for last term.
77        .zip(prod.into_iter().rev().skip(1).chain(Some(BlsScalar::one())))
78    {
79        // tmp := tmp * f; f := tmp * s = 1/f
80        let new_tmp = tmp * *f;
81        *f = tmp * s;
82        tmp = new_tmp;
83    }
84}
85#[cfg(test)]
86mod test {
87    use super::*;
88    use sp_std::vec;
89
90    #[test]
91    fn test_batch_inversion() {
92        let one = BlsScalar::from(1);
93        let two = BlsScalar::from(2);
94        let three = BlsScalar::from(3);
95        let four = BlsScalar::from(4);
96        let five = BlsScalar::from(5);
97
98        let original_scalars = vec![one, two, three, four, five];
99        let mut inverted_scalars = vec![one, two, three, four, five];
100
101        batch_inversion(&mut inverted_scalars);
102        for (x, x_inv) in original_scalars.iter().zip(inverted_scalars.iter()) {
103            assert_eq!(x.invert().unwrap(), *x_inv);
104        }
105    }
106}