fullcodec_plonk/constraint_system/ecc/scalar_mul/
variable_base.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 crate::constraint_system::{TurboComposer, Witness, WitnessPoint};
8
9impl TurboComposer {
10    /// Evaluate `jubjub ยท point` as a [`WitnessPoint`]
11    pub fn component_mul_point(
12        &mut self,
13        jubjub: Witness,
14        point: WitnessPoint,
15    ) -> WitnessPoint {
16        // Turn scalar into bits
17        let scalar_bits = self.component_decomposition::<252>(jubjub);
18
19        let identity = self.append_constant_identity();
20        let mut result = identity;
21
22        for bit in scalar_bits.iter().rev() {
23            result = self.component_add_point(result, result);
24
25            let point_to_add = self.component_select_identity(*bit, point);
26            result = self.component_add_point(result, point_to_add);
27        }
28
29        result
30    }
31}
32
33#[cfg(feature = "std")]
34#[cfg(test)]
35mod tests {
36    use crate::constraint_system::helper::*;
37    use dusk_bls12_381::BlsScalar;
38    use dusk_jubjub::GENERATOR;
39    use dusk_jubjub::{JubJubAffine, JubJubExtended, JubJubScalar};
40
41    #[test]
42    fn test_var_base_scalar_mul() {
43        let res = gadget_tester(
44            |composer| {
45                let scalar = JubJubScalar::from_bytes_wide(&[
46                    182, 44, 247, 214, 94, 14, 151, 208, 130, 16, 200, 204,
47                    147, 32, 104, 166, 0, 59, 52, 1, 1, 59, 103, 6, 169, 175,
48                    51, 101, 234, 180, 125, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50                    0, 0,
51                ]);
52                let bls_scalar = BlsScalar::from(scalar);
53                let secret_scalar = composer.append_witness(bls_scalar);
54
55                let expected_point: JubJubAffine =
56                    (JubJubExtended::from(GENERATOR) * scalar).into();
57
58                let point = composer.append_point(GENERATOR);
59
60                let point_scalar =
61                    composer.component_mul_point(secret_scalar, point);
62
63                composer
64                    .assert_equal_public_point(point_scalar, expected_point);
65            },
66            4096,
67        );
68        assert!(res.is_ok());
69    }
70}