use crate::journal::ModelKind;
use crate::models::{Model, TreeNode};
use ark_bn254::Fr;
use ark_ff::{BigInteger, PrimeField};
use light_poseidon::{Poseidon, PoseidonHasher};
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
pub use crate::Commitment;
pub type Opening = [u8; 32];
const MODEL_DOMAIN: u64 = 3;
const INPUT_DOMAIN: u64 = 4;
const STATE_SIZE: usize = 3;
fn poseidon_commit(elements: &[i64], domain: u64) -> Commitment {
poseidon_chain(Fr::from(domain), elements)
}
fn poseidon_commit_hiding(elements: &[i64], domain: u64, opening: &Opening) -> Commitment {
let mut poseidon = Poseidon::<Fr>::new_circom(2).unwrap();
let blinding = Fr::from_le_bytes_mod_order(opening);
let start = poseidon.hash(&[Fr::from(domain), blinding]).unwrap();
poseidon_chain(start, elements)
}
fn poseidon_chain(start: Fr, elements: &[i64]) -> Commitment {
let rate = STATE_SIZE - 1;
let fr_elements: Vec<Fr> = elements
.iter()
.map(|&v| {
if v >= 0 {
Fr::from(v as u64)
} else {
let abs_val = v.unsigned_abs();
Fr::from(0u64) - Fr::from(abs_val)
}
})
.collect();
let mut poseidon = Poseidon::<Fr>::new_circom(2).unwrap();
let mut current_hash = start;
for (chunk_index, chunk) in fr_elements.chunks(rate).enumerate() {
let mut inputs: Vec<Fr> = chunk.to_vec();
while inputs.len() < rate {
inputs.push(Fr::from(0u64));
}
inputs[0] = inputs[0] + current_hash + Fr::from(chunk_index as u64);
let chunk_hash = poseidon.hash(&inputs).unwrap();
current_hash = chunk_hash;
}
let hash_bytes = current_hash.into_bigint().to_bytes_le();
let mut result = [0u8; 32];
result.copy_from_slice(&hash_bytes);
result
}
pub fn commitment_hash(elements: &[i64]) -> Commitment {
poseidon_commit(elements, 0)
}
pub fn model_elements(model: &Model) -> Vec<i64> {
let mut out = Vec::new();
out.push(ModelKind::of(model) as i64);
match model {
Model::LogisticRegression(lr) => {
out.push(lr.weights.len() as i64);
out.extend(lr.weights.iter().map(|w| w.value));
out.push(lr.bias.value);
out.push(lr.decision_threshold.value);
}
Model::DecisionTree(tree) => {
out.push(tree.num_features as i64);
out.push(tree.nodes.len() as i64);
for node in &tree.nodes {
match node {
TreeNode::Split {
feature_index,
threshold,
left,
right,
} => {
out.push(0); out.push(*feature_index as i64);
out.push(threshold.value);
out.push(*left as i64);
out.push(*right as i64);
}
TreeNode::Leaf { value } => {
out.push(1); out.push(value.value);
}
}
}
}
Model::TinyMLP(mlp) => {
out.push(mlp.layers.len() as i64);
for layer in &mlp.layers {
out.push(layer.weights.len() as i64);
out.extend(layer.weights.iter().map(|w| w.value));
out.push(layer.biases.len() as i64);
out.extend(layer.biases.iter().map(|b| b.value));
out.push(layer.input_size as i64);
out.push(layer.output_size as i64);
}
}
}
out
}
pub fn commit_model(model: &Model, opening: &Opening) -> Commitment {
let elements = model_elements(model);
poseidon_commit_hiding(&elements, MODEL_DOMAIN, opening)
}
pub fn commit_inputs(features: &[crate::fixed_point::FixedPoint], opening: &Opening) -> Commitment {
let elements: Vec<i64> = features.iter().map(|f| f.value).collect();
poseidon_commit_hiding(&elements, INPUT_DOMAIN, opening)
}
pub fn commit_i64(elements: &[i64]) -> Commitment {
poseidon_commit(elements, 0)
}
pub fn to_hex(c: &Commitment) -> String {
let mut s = String::with_capacity(64);
let hex: &[u8; 16] = b"0123456789abcdef";
for b in c.iter() {
s.push(hex[(b >> 4) as usize] as char);
s.push(hex[(b & 0xf) as usize] as char);
}
s
}
pub fn from_hex(s: &str) -> Option<Commitment> {
if s.len() != 64 {
return None;
}
let mut out = [0u8; 32];
for i in 0..32 {
out[i] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).ok()?;
}
Some(out)
}
#[cfg(test)]
mod tests_hex {
use super::*;
#[test]
fn hex_round_trips() {
let c = commit_i64(&[1, 2, 3, 4]);
let encoded = to_hex(&c);
assert_eq!(encoded.len(), 64);
assert_eq!(from_hex(&encoded), Some(c));
}
#[test]
fn from_hex_rejects_bad_length() {
assert_eq!(from_hex("abcd"), None);
}
}
#[cfg(test)]
mod tests_stability {
use super::*;
#[test]
fn commitment_is_stable() {
assert_eq!(commit_i64(&[1, 2, 3]), commit_i64(&[1, 2, 3]));
}
#[test]
fn commitment_is_order_sensitive() {
assert_ne!(commit_i64(&[1, 2, 3]), commit_i64(&[3, 2, 1]));
}
#[test]
fn empty_commitment_is_deterministic() {
let empty1 = commit_i64(&[]);
let empty2 = commit_i64(&[]);
assert_eq!(empty1, empty2);
}
#[test]
fn domain_separation_produces_different_hashes() {
let elements = vec![1, 2, 3];
let hash_domain1 = poseidon_commit(&elements, 1);
let hash_domain2 = poseidon_commit(&elements, 2);
assert_ne!(hash_domain1, hash_domain2);
}
}
#[cfg(test)]
mod tests_hiding {
use super::*;
use crate::fixed_point::FixedPoint;
use crate::models::{LogisticRegression, Model};
const OPENING: Opening = [7u8; 32];
fn model(weight: i64) -> Model {
Model::LogisticRegression(LogisticRegression {
weights: vec![
FixedPoint::from_raw(weight, 16),
FixedPoint::from_raw(2, 16),
],
bias: FixedPoint::from_raw(3, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
})
}
#[test]
fn the_same_model_and_opening_give_the_same_commitment() {
assert_eq!(
commit_model(&model(1), &OPENING),
commit_model(&model(1), &OPENING)
);
}
#[test]
fn another_opening_gives_an_unrelated_commitment() {
let mut other = OPENING;
other[0] ^= 1;
assert_ne!(
commit_model(&model(1), &OPENING),
commit_model(&model(1), &other)
);
let inputs = [FixedPoint::from_raw(5, 16)];
assert_ne!(
commit_inputs(&inputs, &OPENING),
commit_inputs(&inputs, &other)
);
}
#[test]
fn a_plain_hash_of_the_right_model_does_not_match() {
let committed = commit_model(&model(1), &OPENING);
for weight in -100..=100 {
let elements = model_elements(&model(weight));
assert_ne!(commitment_hash(&elements), committed, "weight {weight}");
assert_ne!(
poseidon_commit(&elements, MODEL_DOMAIN),
committed,
"weight {weight}"
);
}
}
#[test]
fn model_and_input_commitments_use_separate_domains() {
let elements = [1i64, 2, 3];
assert_ne!(
poseidon_commit_hiding(&elements, MODEL_DOMAIN, &OPENING),
poseidon_commit_hiding(&elements, INPUT_DOMAIN, &OPENING)
);
}
#[test]
fn a_hiding_commitment_differs_from_the_plain_chain() {
let elements = [1i64, 2, 3];
assert_ne!(
poseidon_commit_hiding(&elements, MODEL_DOMAIN, &OPENING),
poseidon_commit(&elements, MODEL_DOMAIN)
);
}
#[test]
fn the_plain_chain_is_unchanged() {
assert_eq!(commit_i64(&[1, 2, 3]), poseidon_commit(&[1, 2, 3], 0));
assert_eq!(
poseidon_chain(Fr::from(0u64), &[1, 2, 3]),
commit_i64(&[1, 2, 3])
);
}
}
#[cfg(test)]
mod tests_model_elements {
use super::*;
use crate::fixed_point::FixedPoint;
use crate::models::{DecisionTree, LogisticRegression, Model, TreeNode};
#[test]
fn logistic_flattens_weights_then_bias() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(2, 16)],
bias: FixedPoint::from_raw(3, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
assert_eq!(model_elements(&model), vec![1, 2, 1, 2, 3, 0]);
}
#[test]
fn tree_flattens_features_and_nodes() {
let model = Model::DecisionTree(DecisionTree {
num_features: 1,
nodes: vec![
TreeNode::Split {
feature_index: 0,
threshold: FixedPoint::from_raw(10, 16),
left: 1,
right: 2,
},
TreeNode::Leaf {
value: FixedPoint::from_raw(0, 16),
},
TreeNode::Leaf {
value: FixedPoint::from_raw(1, 16),
},
],
});
assert_eq!(
model_elements(&model),
vec![0, 1, 3, 0, 0, 10, 1, 2, 1, 0, 1, 1]
);
}
#[test]
fn commit_model_produces_deterministic_hash() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(2, 16)],
bias: FixedPoint::from_raw(3, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
let hash1 = commit_model(&model, &[1; 32]);
let hash2 = commit_model(&model, &[1; 32]);
assert_eq!(hash1, hash2);
}
#[test]
fn different_shapes_do_not_share_a_commitment() {
let lr = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(10, 16), FixedPoint::from_raw(20, 16)],
bias: FixedPoint::from_raw(30, 16),
decision_threshold: FixedPoint::from_raw(40, 16),
});
let tree = Model::DecisionTree(DecisionTree {
num_features: 10,
nodes: vec![
TreeNode::Leaf {
value: FixedPoint::from_raw(20, 16),
},
TreeNode::Leaf {
value: FixedPoint::from_raw(30, 16),
},
TreeNode::Leaf {
value: FixedPoint::from_raw(40, 16),
},
],
});
assert_ne!(
model_elements(&lr),
model_elements(&tree),
"different model shapes must not flatten to the same stream"
);
assert_ne!(
commit_model(&lr, &[1; 32]),
commit_model(&tree, &[1; 32]),
"different model shapes must not share a commitment"
);
}
#[test]
fn commit_inputs_produces_deterministic_hash() {
let features = vec![
FixedPoint::from_raw(1, 16),
FixedPoint::from_raw(2, 16),
FixedPoint::from_raw(3, 16),
];
let hash1 = commit_inputs(&features, &[1; 32]);
let hash2 = commit_inputs(&features, &[1; 32]);
assert_eq!(hash1, hash2);
}
#[test]
fn changing_model_parameter_changes_commitment() {
let model1 = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(2, 16)],
bias: FixedPoint::from_raw(3, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
let model2 = Model::LogisticRegression(LogisticRegression {
weights: vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(99, 16)], bias: FixedPoint::from_raw(3, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
assert_ne!(
commit_model(&model1, &[1; 32]),
commit_model(&model2, &[1; 32])
);
}
#[test]
fn changing_input_changes_commitment() {
let features1 = vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(2, 16)];
let features2 = vec![FixedPoint::from_raw(1, 16), FixedPoint::from_raw(99, 16)]; assert_ne!(
commit_inputs(&features1, &[1; 32]),
commit_inputs(&features2, &[1; 32])
);
}
}
#[cfg(test)]
mod tests_snapshot {
use super::*;
use crate::fixed_point::FixedPoint;
use crate::models::{DecisionTree, DenseLayer, LogisticRegression, Model, TinyMLP, TreeNode};
use insta::assert_debug_snapshot;
const SNAPSHOT_OPENING: Opening = [0x5a; 32];
#[test]
fn snapshot_logistic_regression_commitment() {
let model = Model::LogisticRegression(LogisticRegression {
weights: vec![
FixedPoint::from_raw(100, 16),
FixedPoint::from_raw(200, 16),
FixedPoint::from_raw(300, 16),
],
bias: FixedPoint::from_raw(50, 16),
decision_threshold: FixedPoint::from_raw(0, 16),
});
let hash = commit_model(&model, &SNAPSHOT_OPENING);
assert_debug_snapshot!(hash);
}
#[test]
fn snapshot_decision_tree_commitment() {
let model = Model::DecisionTree(DecisionTree {
num_features: 2,
nodes: vec![
TreeNode::Split {
feature_index: 0,
threshold: FixedPoint::from_raw(150, 16),
left: 1,
right: 2,
},
TreeNode::Leaf {
value: FixedPoint::from_raw(0, 16),
},
TreeNode::Split {
feature_index: 1,
threshold: FixedPoint::from_raw(200, 16),
left: 3,
right: 4,
},
TreeNode::Leaf {
value: FixedPoint::from_raw(1, 16),
},
TreeNode::Leaf {
value: FixedPoint::from_raw(2, 16),
},
],
});
let hash = commit_model(&model, &SNAPSHOT_OPENING);
assert_debug_snapshot!(hash);
}
#[test]
fn snapshot_tiny_mlp_commitment() {
let model = Model::TinyMLP(TinyMLP {
layers: vec![DenseLayer {
weights: vec![FixedPoint::from_raw(10, 16), FixedPoint::from_raw(20, 16)],
biases: vec![FixedPoint::from_raw(5, 16)],
input_size: 2,
output_size: 1,
}],
});
let hash = commit_model(&model, &SNAPSHOT_OPENING);
assert_debug_snapshot!(hash);
}
#[test]
fn snapshot_input_commitment() {
let features = vec![
FixedPoint::from_raw(100, 16),
FixedPoint::from_raw(200, 16),
FixedPoint::from_raw(300, 16),
];
let hash = commit_inputs(&features, &SNAPSHOT_OPENING);
assert_debug_snapshot!(hash);
}
#[test]
fn snapshot_empty_input_commitment() {
let features: Vec<FixedPoint> = vec![];
let hash = commit_inputs(&features, &SNAPSHOT_OPENING);
assert_debug_snapshot!(hash);
}
}
#[cfg(test)]
mod test_determinism {
use super::*;
#[test]
fn poseidon_commitment_is_deterministic() {
let input = [1i64, 2i64, 3i64];
let digest1 = poseidon_commit(&input, 1);
let digest2 = poseidon_commit(&input, 1);
assert_eq!(digest1, digest2, "commitment should be deterministic");
}
}