snarkvm_circuit_types_field/
neg.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> Neg for Field<E> {
19    type Output = Self;
20
21    /// Performs the unary `-` operation.
22    fn neg(self) -> Self::Output {
23        (&self).neg()
24    }
25}
26
27impl<E: Environment> Neg for &Field<E> {
28    type Output = Field<E>;
29
30    /// Performs the unary `-` operation.
31    fn neg(self) -> Self::Output {
32        (-&self.linear_combination).into()
33    }
34}
35
36impl<E: Environment> Metrics<dyn Neg<Output = Field<E>>> for Field<E> {
37    type Case = Mode;
38
39    fn count(_case: &Self::Case) -> Count {
40        Count::is(0, 0, 0, 0)
41    }
42}
43
44impl<E: Environment> OutputMode<dyn Neg<Output = Field<E>>> for Field<E> {
45    type Case = Mode;
46
47    fn output_mode(case: &Self::Case) -> Mode {
48        match case {
49            Mode::Constant => Mode::Constant,
50            _ => Mode::Private,
51        }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use snarkvm_circuit_environment::Circuit;
59
60    const ITERATIONS: u64 = 1_000;
61
62    fn check_neg(name: &str, mode: Mode, rng: &mut TestRng) {
63        let check_neg = |given: console::Field<<Circuit as Environment>::Network>| {
64            // Compute it's negation.
65            let expected = given.neg();
66            let candidate = Field::<Circuit>::new(mode, given);
67
68            // Check negation.
69            Circuit::scope(name, || {
70                let result = candidate.neg();
71                assert_eq!(expected, result.eject_value());
72                assert_count!(Neg(Field) => Field, &mode);
73                assert_output_mode!(Neg(Field) => Field, &mode, result);
74            });
75        };
76
77        for _ in 0..ITERATIONS {
78            // Sample a random element.
79            let given = Uniform::rand(rng);
80            check_neg(given)
81        }
82        // Check zero case.
83        check_neg(console::Field::<<Circuit as Environment>::Network>::zero());
84        // Check one case.
85        check_neg(console::Field::<<Circuit as Environment>::Network>::one());
86    }
87
88    #[test]
89    fn test_neg() {
90        let mut rng = TestRng::default();
91
92        check_neg("Constant", Mode::Constant, &mut rng);
93        check_neg("Public", Mode::Public, &mut rng);
94        check_neg("Private", Mode::Private, &mut rng);
95    }
96
97    #[test]
98    fn test_zero() {
99        let zero = console::Field::<<Circuit as Environment>::Network>::zero();
100
101        let candidate = Field::<Circuit>::zero();
102        assert_eq!(zero, candidate.eject_value());
103        assert_eq!(zero, (-&candidate).eject_value());
104        assert_eq!(zero, (-(-candidate)).eject_value());
105
106        let candidate = Field::<Circuit>::new(Mode::Public, zero);
107        assert_eq!(zero, candidate.eject_value());
108        assert_eq!(zero, (-&candidate).eject_value());
109        assert_eq!(zero, (-(-candidate)).eject_value());
110
111        let candidate = Field::<Circuit>::new(Mode::Private, zero);
112        assert_eq!(zero, candidate.eject_value());
113        assert_eq!(zero, (-&candidate).eject_value());
114        assert_eq!(zero, (-(-candidate)).eject_value());
115    }
116
117    #[test]
118    fn test_one() {
119        let one = console::Field::<<Circuit as Environment>::Network>::one();
120
121        let candidate = Field::<Circuit>::one();
122        assert_eq!(one, candidate.eject_value());
123        assert_eq!(-one, (-&candidate).eject_value());
124        assert_eq!(one, (-(-candidate)).eject_value());
125
126        let candidate = Field::<Circuit>::new(Mode::Public, one);
127        assert_eq!(one, candidate.eject_value());
128        assert_eq!(-one, (-&candidate).eject_value());
129        assert_eq!(one, (-(-candidate)).eject_value());
130
131        let candidate = Field::<Circuit>::new(Mode::Private, one);
132        assert_eq!(one, candidate.eject_value());
133        assert_eq!(-one, (-&candidate).eject_value());
134        assert_eq!(one, (-(-candidate)).eject_value());
135    }
136}