Skip to main content

midnight_circuits/utils/
types.rs

1// This file is part of MIDNIGHT-ZK.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
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// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use core::fmt::Debug;
15
16use midnight_proofs::circuit::Value;
17#[cfg(any(test, feature = "testing"))]
18use rand::RngCore;
19
20use crate::{field::AssignedNative, CircuitField};
21
22/// Trait for dealing with public inputs. `Instantiable` is implemented on
23/// off-circuit types to determine the way these types are transformed into
24/// vectors of native values.
25/// Analogous functions exists for constraining public inputs in-circuit in
26/// the [crate::instructions::PublicInputInstructions] trait.
27pub trait Instantiable<F: CircuitField>: InnerValue {
28    /// This function is the off-circuit analog of
29    /// [crate::instructions::PublicInputInstructions::as_public_input].
30    fn as_public_input(element: &<Self as InnerValue>::Element) -> Vec<F>;
31}
32
33/// Trait for accessing the value inside assigned circuit elements.
34pub trait InnerValue: Clone + Debug {
35    /// Represents the unassigned type corresponding to the [InnerValue]
36    type Element: Clone + Debug;
37
38    /// Returns the value of the assigned element.
39    fn value(&self) -> Value<Self::Element>;
40}
41
42impl<T: InnerValue, const L: usize> InnerValue for [T; L] {
43    type Element = [T::Element; L];
44
45    fn value(&self) -> Value<Self::Element> {
46        let val = Value::from_iter(self.iter().map(|val| val.value()));
47        // We know sizes will match due to the type system. The problem is
48        // that the type Value is not right enough.
49        val.map(|v: Vec<T::Element>| v.try_into().unwrap())
50    }
51}
52
53/// Trait for accessing constant values of the inner value type of an assigned
54/// element.
55pub trait InnerConstants: InnerValue {
56    /// The zero of Self::Element (additive identity).
57    fn inner_zero() -> Self::Element;
58
59    /// The unit of Self::Element (multiplicative identity and/or additive
60    /// generator).
61    fn inner_one() -> Self::Element;
62}
63
64impl<F: CircuitField> Instantiable<F> for AssignedNative<F> {
65    fn as_public_input(element: &F) -> Vec<F> {
66        vec![*element]
67    }
68}
69
70impl<F: CircuitField> InnerValue for AssignedNative<F> {
71    type Element = F;
72
73    fn value(&self) -> Value<F> {
74        self.value().cloned()
75    }
76}
77
78impl<F: CircuitField> InnerConstants for AssignedNative<F> {
79    fn inner_zero() -> F {
80        F::ZERO
81    }
82
83    fn inner_one() -> F {
84        F::ONE
85    }
86}
87
88#[cfg(any(test, feature = "testing"))]
89/// A trait for types that can be inverted. This should only
90/// be used for testing.
91pub trait Invertible {
92    /// Returns the multiplicative inverse of the given value.
93    ///
94    /// # Panics
95    ///
96    /// If the given value does not have an inverse.
97    fn invert(&self) -> Self;
98}
99
100#[cfg(any(test, feature = "testing"))]
101impl<F: CircuitField> Invertible for F {
102    fn invert(&self) -> F {
103        self.invert().unwrap()
104    }
105}
106
107#[cfg(any(test, feature = "testing"))]
108/// A trait for types that can be sampled at random. This should only
109/// be used for testing.
110pub trait Sampleable: InnerValue {
111    /// Returns a random inner element, given a random number generator.
112    fn sample_inner(rng: impl RngCore) -> Self::Element;
113}
114
115#[cfg(any(test, feature = "testing"))]
116impl<F: CircuitField> Sampleable for AssignedNative<F> {
117    fn sample_inner(rng: impl RngCore) -> F {
118        F::random(rng)
119    }
120}