openvm_algebra_guest/
exp_bytes.rs

1use core::ops::Mul;
2
3use crate::Field;
4
5pub trait ExpBytes: Field {
6    /// Exponentiates a field element by a value with a sign in big endian byte order
7    fn exp_bytes(&self, is_positive: bool, bytes_be: &[u8]) -> Self
8    where
9        for<'a> &'a Self: Mul<&'a Self, Output = Self>,
10    {
11        let mut x = self.clone();
12
13        if !is_positive {
14            x = Self::ONE.div_unsafe(&x);
15        }
16
17        let mut res = Self::ONE;
18
19        let x_sq = &x * &x;
20        let ops = [x.clone(), x_sq.clone(), &x_sq * &x];
21
22        for &b in bytes_be.iter() {
23            let mut mask = 0xc0;
24            for j in 0..4 {
25                res = &res * &res * &res * &res;
26                let c = (b & mask) >> (6 - 2 * j);
27                if c != 0 {
28                    res *= &ops[(c - 1) as usize];
29                }
30                mask >>= 2;
31            }
32        }
33        res
34    }
35}
36
37impl<F: Field> ExpBytes for F where for<'a> &'a Self: Mul<&'a Self, Output = Self> {}