add mpack proof func - tests infra

This commit is contained in:
Jaremy Creechley 2023-12-11 11:29:17 -07:00
parent 60e341912f
commit be9c891fef
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300

View File

@ -134,3 +134,89 @@ impl StorageProofs {
Ok(())
}
}
#[cfg(test)]
mod tests {
use ark_std::rand::{distributions::Alphanumeric, rngs::ThreadRng, Rng};
use rs_poseidon::poseidon::hash;
use ruint::aliases::U256;
use crate::circuit_tests::utils::{digest, treehash};
use rmpv::Value;
use rmpv::encode::write_value;
use rmpv::decode::read_value;
#[test]
fn test_mpack() {
let mut buf = Vec::new();
let val = Value::from("le message");
// example of serializing the random chunk data
// we build them up in mpack Value enums
let data = (0..4)
.map(|_| {
let rng = ThreadRng::default();
let preimages: Vec<U256> = rng
.sample_iter(Alphanumeric)
.take(256)
.map(|c| U256::from(c))
.collect();
let hash = digest(&preimages, Some(16));
(preimages, hash)
})
.collect::<Vec<(Vec<U256>, U256)>>();
let chunks = data.iter()
.map(|c| {
let x = c.0.iter()
.map(|c| Value::Ext(50, c.to_le_bytes_vec()))
.collect::<Vec<Value>>();
Value::Array(x)
})
.collect::<Vec<Value>>();
let chunks = Value::Array(chunks);
let mut data = Value::Map(vec![(Value::String("chunks".into()), chunks.clone() )]);
println!("Debug: chunks: {:?}", chunks[0][0]);
// Serialize the value types to an array pointer
write_value(&mut buf, &data).unwrap();
let mut rd = &buf[..];
let args = read_value(&mut rd).unwrap();
assert!(Value::is_map(&args));
assert!(Value::is_array(&args["chunks"]));
assert!(Value::is_array(&args["chunks"][0]));
let mut arg_chunks: Vec<Vec<U256>> = Vec::new();
// deserialize the data back into u256's
// instead of this, we'll want to use `builder.push_input`
args["chunks"]
.as_array()
.unwrap()
.iter()
.for_each(|c| {
if let Some(x) = c.as_array() {
let mut vals: Vec<U256> = Vec::new();
x.iter().for_each(|n| {
let b = n.as_ext().unwrap();
// ensure it's a LE uin256 which we've set as ext 50
assert_eq!(b.0, 50);
vals.push(U256::try_from_le_slice(b.1).unwrap());
// TODO: change to use
// builder.push_input("hashes", *c)
});
arg_chunks.push(vals);
} else {
panic!("unhandled type!");
}
});
assert_eq!(arg_chunks.len(), 4);
assert_eq!(arg_chunks[0].len(), 256);
}
}