Skip to main content

arcis_compiler/
traits.rs

1use crate::{
2    core::circuits::boolean::{boolean_value::Boolean, byte::Byte},
3    utils::{
4        crypto::key::X25519PublicKey,
5        curve_point::Curve,
6        elliptic_curve::F25519,
7        number::Number,
8    },
9};
10use std::ops::Not;
11
12pub trait Equal<Other>: Sized {
13    type Output: Not<Output = Self::Output>;
14
15    fn eq(self, other: Other) -> Self::Output;
16    fn ne(self, other: Other) -> Self::Output {
17        Self::eq(self, other).not()
18    }
19}
20
21pub trait IsZero {
22    type Output;
23
24    fn is_zero(&self) -> Self::Output;
25}
26
27pub trait GreaterEqual<Other>: Sized {
28    type Output: Not<Output = Self::Output>;
29
30    fn ge(self, other: Other) -> Self::Output;
31    fn lt(self, other: Other) -> Self::Output {
32        Self::ge(self, other).not()
33    }
34}
35
36pub trait GreaterThan<Other>: Sized {
37    type Output: Not<Output = Self::Output>;
38
39    fn gt(self, other: Other) -> Self::Output;
40    fn le(self, other: Other) -> Self::Output {
41        Self::gt(self, other).not()
42    }
43}
44
45/// Implement [`Equal`] for all types that implement [`Eq`].
46impl<T> Equal<T> for T
47where
48    T: Eq,
49{
50    type Output = bool;
51
52    fn eq(self, other: T) -> Self::Output {
53        self == other
54    }
55}
56
57/// Implement [`GreaterEqual`] for all types that implement [`PartialOrd`].
58impl<T> GreaterEqual<T> for T
59where
60    T: PartialOrd,
61{
62    type Output = bool;
63
64    fn ge(self, other: T) -> Self::Output {
65        self >= other
66    }
67}
68
69/// Implement [`GreaterThan`] for all types that implement [`PartialOrd`].
70impl<T> GreaterThan<T> for T
71where
72    T: PartialOrd,
73{
74    type Output = bool;
75
76    fn gt(self, other: T) -> Self::Output {
77        self > other
78    }
79}
80
81pub trait Selectable<T = Self> {
82    type Conditional;
83    type Output;
84
85    fn construct_selection(condition: Self::Conditional, a: Self, b: T) -> Self::Output;
86}
87
88pub trait Select<T, U, V> {
89    fn select(self, a: T, b: V) -> U;
90}
91
92pub trait Enc<T> {
93    fn reveal(self) -> T;
94}
95
96pub trait FromLeBits<B: Boolean> {
97    fn from_le_bits(bits: Vec<B>, signed: bool) -> Self;
98}
99
100pub trait GetBit {
101    type Output: Boolean;
102
103    fn get_bit(&self, index: usize, signed: bool) -> Self::Output;
104}
105
106pub trait FromLeBytes {
107    fn from_le_bytes(bytes: [u8; 32]) -> Self;
108}
109
110pub trait ToLeBytes {
111    type BooleanOutput: Boolean;
112
113    fn to_le_bytes(self) -> [Byte<Self::BooleanOutput>; 32];
114}
115
116pub trait Random {
117    fn random() -> Self;
118}
119
120pub trait RandomBit {
121    fn random() -> Self;
122}
123
124pub trait Reveal {
125    fn reveal(self) -> Self;
126}
127
128pub trait Invert {
129    fn invert(self, is_expected_non_zero: bool) -> Self;
130}
131
132pub trait Pow {
133    fn pow(self, e: &Number, is_expected_non_zero: bool) -> Self;
134}
135
136pub trait Keccak {
137    fn f1600(state: [Byte<Self>; 200]) -> [Byte<Self>; 200]
138    where
139        Self: Boolean;
140
141    fn sponge<const R: usize, const N: usize>(input_bytes: Vec<Byte<Self>>) -> [Byte<Self>; N]
142    where
143        Self: Boolean,
144    {
145        if input_bytes.len() > 1 << 20 {
146            panic!(
147                "sha3 not supported on inputs of more than 2^20 bytes (found {})",
148                input_bytes.len()
149            );
150        }
151        if !matches!(R, 72 | 136) {
152            panic!("rate in bytes must be 72 or 136 (found {R})");
153        }
154        let mut state = [Byte::from(0u8); 200];
155        // absorb the input blocks
156        input_bytes.chunks(R).for_each(|chunk| {
157            chunk.iter().copied().enumerate().for_each(|(i, c)| {
158                state[i] ^= c;
159            });
160            if chunk.len() == R {
161                state = Keccak::f1600(state);
162            }
163        });
164        // do the padding
165        let block_size = input_bytes.len() % R;
166        state[block_size] ^= Byte::from(0x06);
167        state[R - 1] ^= Byte::from(0x80);
168        state = Keccak::f1600(state);
169        // squeezing phase
170        (0..N)
171            .step_by(R)
172            .fold(Vec::new(), |mut acc, pos| {
173                let block_size = (N - pos).min(R);
174                acc.append(&mut state[0..block_size].to_vec());
175                if acc.len() < N {
176                    state = Keccak::f1600(state);
177                }
178                acc
179            })
180            .try_into()
181            .unwrap_or_else(|v: Vec<Byte<Self>>| {
182                panic!("Expected a Vec of length {N} (found {})", v.len())
183            })
184    }
185}
186
187pub trait WithBooleanBounds {
188    fn with_boolean_bounds(&self) -> Self;
189}
190
191pub trait ToMontgomery {
192    type Output: F25519;
193
194    fn to_montgomery(self, is_expected_non_identity: bool) -> (Self::Output, Self::Output);
195}
196
197pub trait MxeX25519PrivateKey {
198    fn mxe_x25519_private_key() -> Self;
199}
200
201pub trait MxeRescueKey {
202    fn mxe_rescue_key(i: usize) -> Self;
203}
204
205pub trait GetSharedRescueKey<C: Curve> {
206    fn get_shared_rescue_key(pubkey: X25519PublicKey<C>, i: usize) -> Self;
207}
208
209/// Trait used to convert the ECDH output to the target field.
210/// The implementor must make sure that the conversion is injective!
211pub trait FromF25519<T: F25519> {
212    #[allow(non_snake_case)]
213    fn from_F25519(value: T) -> Vec<Self>
214    where
215        Self: Sized;
216}