use lens::{brakedown::Brakedown, Commitment, Lens, MultilinearPoly};
use nebu::Goldilocks;
use crate::types::Particle;
#[inline]
pub fn goldilocks_from_u64(v: u64) -> Goldilocks {
Goldilocks::new(v)
}
pub fn goldilocks_from_bytes32(b: &[u8; 32]) -> [Goldilocks; 4] {
let mut out = [Goldilocks::ZERO; 4];
for (i, chunk) in b.chunks_exact(8).enumerate() {
let mut buf = [0u8; 8];
buf.copy_from_slice(chunk);
out[i] = Goldilocks::new(u64::from_le_bytes(buf));
}
out
}
pub fn commit_dim(entries: &[(Particle, Vec<Goldilocks>)]) -> Commitment {
if entries.is_empty() {
let sentinel: Vec<Goldilocks> = b"bbg-empty-dim"
.iter()
.map(|&b| Goldilocks::new(b as u64))
.collect();
let target = sentinel.len().next_power_of_two();
let mut padded = sentinel;
padded.resize(target, Goldilocks::ZERO);
return Brakedown::commit_raw(&padded);
}
let mut elems: Vec<Goldilocks> = Vec::new();
for (key, vals) in entries {
let key_elems = goldilocks_from_bytes32(key);
elems.extend_from_slice(&key_elems);
elems.extend_from_slice(vals);
}
let target = elems.len().next_power_of_two();
elems.resize(target, Goldilocks::ZERO);
let poly = MultilinearPoly::new(elems);
Brakedown::commit(&poly)
}
pub fn bbg_poly_commit(dim_commits: &[Commitment; 11]) -> Particle {
let mut buf = Vec::with_capacity(352); for c in dim_commits.iter() {
buf.extend_from_slice(c.as_bytes());
}
let elems: Vec<Goldilocks> = buf.iter().map(|&b| Goldilocks::new(b as u64)).collect();
let target = elems.len().next_power_of_two();
let mut padded = elems;
padded.resize(target, Goldilocks::ZERO);
let commit = Brakedown::commit_raw(&padded);
let mut out = [0u8; 32];
let cb = commit.as_bytes();
let len = cb.len().min(32);
out[..len].copy_from_slice(&cb[..len]);
out
}