#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
use ff::{Field, PrimeField};
use num_bigint::{BigInt, BigUint};
use num_traits::{Num as _, Signed as _};
use crate::error::Error;
pub mod cells;
pub mod circuit;
pub mod error;
pub mod macros;
pub use haloumi_ir as ir;
pub trait Halo2Types<F: Field> {
type InstanceCol: std::fmt::Debug + Copy + Clone;
type AdviceCol: std::fmt::Debug + Copy + Clone;
type Cell: std::fmt::Debug + Copy + Clone;
type AssignedCell<V>;
type Region<'a>;
type Error: Into<crate::error::Error> + From<crate::error::Error>;
type RegionIndex: std::hash::Hash + Copy + Eq;
type Expression;
type Rational;
}
pub fn parse_field<F: PrimeField>(s: &str) -> Result<F, Error> {
if s.is_empty() {
return Err(Error::FieldParsingError);
}
let ten = F::from(10);
s.chars()
.map(|c| c.to_digit(10).ok_or(Error::FieldParsingError))
.map(|r| r.map(u64::from))
.map(|r| r.map(F::from))
.fold(Ok(F::ZERO), |acc, c| Ok(acc? * ten + c?))
}
fn modulus<F: PrimeField>() -> BigUint {
BigUint::from_str_radix(&F::MODULUS[2..], 16).unwrap()
}
fn modulus_signed<F: PrimeField>() -> BigInt {
BigInt::from_str_radix(&F::MODULUS[2..], 16).unwrap()
}
pub fn big_to_fe<F: PrimeField>(e: BigUint) -> F {
let modulus = modulus::<F>();
let e = e % modulus;
F::from_str_vartime(&e.to_str_radix(10)[..]).unwrap()
}
pub fn sbig_to_fe<F: PrimeField>(mut e: BigInt) -> F {
let modulus = modulus_signed::<F>();
e = (e % modulus).abs();
F::from_str_vartime(&e.to_str_radix(10)[..]).unwrap()
}
pub fn fe_to_big<F: PrimeField>(fe: F) -> BigUint {
BigUint::from_bytes_le(fe.to_repr().as_ref())
}
#[macro_export]
macro_rules! cell_to_expr {
($x:expr, $F:ty) => {{
let c = $x.cell();
i32::try_from(c.row_offset)
.map(midnight_proofs::poly::Rotation)
.map(|r| c.column.query_cell::<$F>(r))
.map_err($crate::error::Error::from)
}};
}