snarkvm_circuit_types_field/
div.rs

1// Copyright 2024 Aleo Network Foundation
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> Div<Field<E>> for Field<E> {
19    type Output = Field<E>;
20
21    fn div(self, other: Field<E>) -> Self::Output {
22        self / &other
23    }
24}
25
26impl<E: Environment> Div<&Field<E>> for Field<E> {
27    type Output = Field<E>;
28
29    fn div(self, other: &Field<E>) -> Self::Output {
30        &self / other
31    }
32}
33
34impl<E: Environment> Div<Field<E>> for &Field<E> {
35    type Output = Field<E>;
36
37    fn div(self, other: Field<E>) -> Self::Output {
38        self / &other
39    }
40}
41
42impl<E: Environment> Div<&Field<E>> for &Field<E> {
43    type Output = Field<E>;
44
45    fn div(self, other: &Field<E>) -> Self::Output {
46        let mut output = self.clone();
47        output /= other;
48        output
49    }
50}
51
52impl<E: Environment> DivAssign<Self> for Field<E> {
53    fn div_assign(&mut self, other: Self) {
54        *self /= &other;
55    }
56}
57
58impl<E: Environment> DivAssign<&Self> for Field<E> {
59    #[allow(clippy::suspicious_op_assign_impl)]
60    fn div_assign(&mut self, other: &Self) {
61        match other.is_constant() {
62            // If `other` is a constant and zero, halt since the inverse of zero is undefined.
63            true if other.eject_value().is_zero() => E::halt("Attempted to divide by zero."),
64            // If `other` is a constant and non-zero, we can perform multiplication and inversion for 0 constraints.
65            // If `self` is a constant, we can perform multiplication and inversion for 1 constraint.
66            // Otherwise, we can perform multiplication and inversion for 2 constraints.
67            _ => *self *= other.inverse(),
68        }
69    }
70}
71
72impl<E: Environment> Metrics<dyn Div<Field<E>, Output = Field<E>>> for Field<E> {
73    type Case = (Mode, Mode);
74
75    fn count(case: &Self::Case) -> Count {
76        match case {
77            (Mode::Constant, Mode::Constant) | (_, Mode::Constant) => Count::is(1, 0, 0, 0),
78            (Mode::Constant, _) => Count::is(0, 0, 1, 1),
79            (_, _) => Count::is(0, 0, 2, 2),
80        }
81    }
82}
83
84impl<E: Environment> OutputMode<dyn Div<Field<E>, Output = Field<E>>> for Field<E> {
85    type Case = (CircuitType<Field<E>>, CircuitType<Field<E>>);
86
87    fn output_mode(case: &Self::Case) -> Mode {
88        match (case.0.mode(), case.1.mode()) {
89            (Mode::Constant, Mode::Constant) => Mode::Constant,
90            (Mode::Public, Mode::Constant) => match &case.1 {
91                CircuitType::Constant(constant) => match constant.eject_value().is_one() {
92                    true => Mode::Public,
93                    false => Mode::Private,
94                },
95                _ => E::halt("The constant is required to determine the output mode of Public + Constant"),
96            },
97            (_, _) => Mode::Private,
98        }
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use snarkvm_circuit_environment::{Circuit, assert_count_fails};
106
107    const ITERATIONS: u64 = 1000;
108
109    fn check_div(
110        name: &str,
111        first: &console::Field<<Circuit as Environment>::Network>,
112        second: &console::Field<<Circuit as Environment>::Network>,
113        mode_a: Mode,
114        mode_b: Mode,
115    ) {
116        let a = &Field::<Circuit>::new(mode_a, *first);
117        let b = &Field::<Circuit>::new(mode_b, *second);
118
119        match second.is_zero() {
120            true => match mode_b.is_constant() {
121                true => {
122                    let result = std::panic::catch_unwind(|| Field::div(a.clone(), b));
123                    assert!(result.is_err());
124                }
125                false => {
126                    Circuit::scope(name, || {
127                        let _ = a / b;
128                        assert_count_fails!(Div(Field, Field) => Field, &(a.eject_mode(), b.eject_mode()));
129                    });
130                }
131            },
132            false => {
133                let expected = *first / *second;
134                Circuit::scope(name, || {
135                    let candidate = a / b;
136                    assert_eq!(expected, candidate.eject_value(), "({} / {})", a.eject_value(), b.eject_value());
137                    assert_count!(Div(Field, Field) => Field, &(a.eject_mode(), b.eject_mode()));
138                    assert_output_mode!(Div(Field, Field) => Field, &(CircuitType::from(a), CircuitType::from(b)), candidate);
139                });
140            }
141        }
142    }
143
144    fn check_div_assign(
145        name: &str,
146        first: &console::Field<<Circuit as Environment>::Network>,
147        second: &console::Field<<Circuit as Environment>::Network>,
148        mode_a: Mode,
149        mode_b: Mode,
150    ) {
151        let a = &Field::<Circuit>::new(mode_a, *first);
152        let b = &Field::<Circuit>::new(mode_b, *second);
153
154        match second.is_zero() {
155            true => match mode_b.is_constant() {
156                true => {
157                    let result = std::panic::catch_unwind(|| Field::div_assign(&mut a.clone(), b));
158                    assert!(result.is_err());
159                }
160                false => {
161                    Circuit::scope(name, || {
162                        let mut candidate = a.clone();
163                        candidate /= b;
164                        assert_count_fails!(Div(Field, Field) => Field, &(a.eject_mode(), b.eject_mode()));
165                    });
166                }
167            },
168            false => {
169                let expected = *first / *second;
170                Circuit::scope(name, || {
171                    let mut candidate = a.clone();
172                    candidate /= b;
173                    assert_eq!(expected, candidate.eject_value(), "({} /= {})", a.eject_value(), b.eject_value());
174                    assert_count!(Div(Field, Field) => Field, &(a.eject_mode(), b.eject_mode()));
175                    assert_output_mode!(Div(Field, Field) => Field, &(CircuitType::from(a), CircuitType::from(b)), candidate);
176                });
177            }
178        }
179    }
180
181    fn run_test(mode_a: Mode, mode_b: Mode) {
182        let mut rng = TestRng::default();
183
184        for i in 0..ITERATIONS {
185            let first = Uniform::rand(&mut rng);
186            let second = Uniform::rand(&mut rng);
187
188            let name = format!("Div: a / b {i}");
189            check_div(&name, &first, &second, mode_a, mode_b);
190            let name = format!("DivAssign: a / b {i}");
191            check_div_assign(&name, &first, &second, mode_a, mode_b);
192
193            // Check division by one.
194            let one = console::Field::<<Circuit as Environment>::Network>::one();
195            let name = format!("Div By One {i}");
196            check_div(&name, &first, &one, mode_a, mode_b);
197            let name = format!("DivAssign By One {i}");
198            check_div_assign(&name, &first, &one, mode_a, mode_b);
199
200            // Check division by zero.
201            let zero = console::Field::<<Circuit as Environment>::Network>::zero();
202            let name = format!("Div By Zero {i}");
203            check_div(&name, &first, &zero, mode_a, mode_b);
204            let name = format!("DivAssign By Zero {i}");
205            check_div_assign(&name, &first, &zero, mode_a, mode_b);
206        }
207    }
208
209    #[test]
210    fn test_constant_div_constant() {
211        run_test(Mode::Constant, Mode::Constant);
212    }
213
214    #[test]
215    fn test_constant_div_public() {
216        run_test(Mode::Constant, Mode::Public);
217    }
218
219    #[test]
220    fn test_constant_div_private() {
221        run_test(Mode::Constant, Mode::Private);
222    }
223
224    #[test]
225    fn test_public_div_constant() {
226        run_test(Mode::Public, Mode::Constant);
227    }
228
229    #[test]
230    fn test_public_div_public() {
231        run_test(Mode::Public, Mode::Public);
232    }
233
234    #[test]
235    fn test_public_div_private() {
236        run_test(Mode::Public, Mode::Private);
237    }
238
239    #[test]
240    fn test_private_div_constant() {
241        run_test(Mode::Private, Mode::Constant);
242    }
243
244    #[test]
245    fn test_private_div_public() {
246        run_test(Mode::Private, Mode::Public);
247    }
248
249    #[test]
250    fn test_private_div_private() {
251        run_test(Mode::Private, Mode::Private);
252    }
253
254    #[test]
255    fn test_div_by_zero_fails() {
256        let zero = console::Field::<<Circuit as Environment>::Network>::zero();
257        let one = console::Field::<<Circuit as Environment>::Network>::one();
258
259        let result = std::panic::catch_unwind(|| Field::<Circuit>::one() / Field::zero());
260        assert!(result.is_err()); // Probe further for specific error type here, if desired
261
262        let result =
263            std::panic::catch_unwind(|| Field::<Circuit>::new(Mode::Constant, one) / Field::new(Mode::Constant, zero));
264        assert!(result.is_err()); // Probe further for specific error type here, if desired
265
266        Circuit::scope("Public Div by Zero", || {
267            let _ = Field::<Circuit>::new(Mode::Public, one) / Field::new(Mode::Public, zero);
268            assert!(!Circuit::is_satisfied_in_scope());
269        });
270
271        Circuit::scope("Private Div by Zero", || {
272            let _ = Field::<Circuit>::new(Mode::Private, one) / Field::new(Mode::Private, zero);
273            assert!(!Circuit::is_satisfied_in_scope());
274        });
275    }
276}