snarkvm_circuit_types_field/
square.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<E: Environment> Square for Field<E> {
19    type Output = Field<E>;
20
21    fn square(&self) -> Self::Output {
22        self * self
23    }
24}
25
26impl<E: Environment> Metrics<dyn Square<Output = Field<E>>> for Field<E> {
27    type Case = Mode;
28
29    fn count(case: &Self::Case) -> Count {
30        match case.is_constant() {
31            true => Count::is(0, 0, 0, 0),
32            false => Count::is(0, 0, 1, 1),
33        }
34    }
35}
36
37impl<E: Environment> OutputMode<dyn Square<Output = Field<E>>> for Field<E> {
38    type Case = Mode;
39
40    fn output_mode(input: &Self::Case) -> Mode {
41        match input.is_constant() {
42            true => Mode::Constant,
43            false => Mode::Private,
44        }
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use snarkvm_circuit_environment::Circuit;
52
53    const ITERATIONS: u64 = 500;
54
55    fn check_square(name: &str, expected: &console::Field<<Circuit as Environment>::Network>, a: &Field<Circuit>) {
56        Circuit::scope(name, || {
57            let result = a.square();
58            assert_eq!(*expected, result.eject_value());
59            assert_count!(Square(Field) => Field, &(a.eject_mode()));
60            assert_output_mode!(Square(Field) => Field, &(a.eject_mode()), result);
61        });
62    }
63
64    fn run_test(mode: Mode, rng: &mut TestRng) {
65        for i in 0..ITERATIONS {
66            // Sample a random element
67            let first = Uniform::rand(rng);
68            let a = Field::<Circuit>::new(mode, first);
69
70            let name = format!("Square: {i}");
71            check_square(&name, &first.square(), &a);
72        }
73
74        // Test zero case.
75        let name = "Square Zero";
76        let zero = console::Field::<<Circuit as Environment>::Network>::zero();
77        check_square(name, &zero, &Field::new(mode, zero));
78
79        // Test one case.
80        let name = "Square One";
81        let one = console::Field::<<Circuit as Environment>::Network>::one();
82        check_square(name, &one, &Field::new(mode, one));
83    }
84
85    #[test]
86    fn test_square() {
87        let mut rng = TestRng::default();
88
89        run_test(Mode::Constant, &mut rng);
90        run_test(Mode::Public, &mut rng);
91        run_test(Mode::Private, &mut rng);
92    }
93
94    #[test]
95    fn test_0_square() {
96        let zero = console::Field::<<Circuit as Environment>::Network>::zero();
97
98        let candidate = Field::<Circuit>::new(Mode::Public, zero).square();
99        assert_eq!(zero, candidate.eject_value());
100    }
101
102    #[test]
103    fn test_1_double() {
104        let one = console::Field::<<Circuit as Environment>::Network>::one();
105
106        let candidate = Field::<Circuit>::new(Mode::Public, one).square();
107        assert_eq!(one, candidate.eject_value());
108    }
109
110    #[test]
111    fn test_2_double() {
112        let one = console::Field::<<Circuit as Environment>::Network>::one();
113        let two = one + one;
114        let four = two.square();
115
116        let candidate = (Field::<Circuit>::new(Mode::Public, one) + Field::new(Mode::Public, one)).square();
117        assert_eq!(four, candidate.eject_value());
118    }
119}