mirror of
https://github.com/logos-storage/logos-storage-proofs.git
synced 2026-01-04 06:23:11 +00:00
rework funcs
This commit is contained in:
parent
8b823c418e
commit
68c333f2a8
@ -234,13 +234,14 @@ mod tests {
|
|||||||
|
|
||||||
// Serialize the value types to an array pointer
|
// Serialize the value types to an array pointer
|
||||||
write_value(&mut buf, &data).unwrap();
|
write_value(&mut buf, &data).unwrap();
|
||||||
let mut rd = &buf[..];
|
let mut rd: &[u8] = &buf[..];
|
||||||
|
|
||||||
let args = read_value(&mut rd).unwrap();
|
let args = read_value(&mut rd).unwrap();
|
||||||
|
|
||||||
assert!(Value::is_map(&args));
|
assert!(Value::is_map(&args));
|
||||||
assert!(Value::is_array(&args["chunks"]));
|
assert!(Value::is_array(&args["chunks"]));
|
||||||
assert!(Value::is_array(&args["chunks"][0]));
|
assert!(Value::is_array(&args["chunks"][0]));
|
||||||
|
|
||||||
let mut arg_chunks: Vec<Vec<U256>> = Vec::new();
|
let mut arg_chunks: Vec<Vec<U256>> = Vec::new();
|
||||||
|
|
||||||
// deserialize the data back into u256's
|
// deserialize the data back into u256's
|
||||||
|
|||||||
@ -10,6 +10,30 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read};
|
|||||||
use ark_std::rand::rngs::ThreadRng;
|
use ark_std::rand::rngs::ThreadRng;
|
||||||
use ruint::aliases::U256;
|
use ruint::aliases::U256;
|
||||||
|
|
||||||
|
use rmpv;
|
||||||
|
use rmpv::decode::read_value;
|
||||||
|
|
||||||
|
type CircomBuilderParams = CircomBuilder<ark_ec::bn::Bn<ark_bn254::Parameters>>;
|
||||||
|
|
||||||
|
const EXT_ID_U256_LE: i8 = 50;
|
||||||
|
const EXT_ID_U256_BE: i8 = 51;
|
||||||
|
|
||||||
|
fn decode_u256(val: &rmpv::Value) -> Result<U256, String> {
|
||||||
|
match val {
|
||||||
|
rmpv::Value::Ext(id, val) => {
|
||||||
|
match *id {
|
||||||
|
EXT_ID_U256_LE =>
|
||||||
|
match U256::try_from_le_slice(val) {
|
||||||
|
Some(i) => Ok(i),
|
||||||
|
None => Err("error parsing 256".to_string()),
|
||||||
|
}
|
||||||
|
num => return Err(format!("unhandled ext id {}", num)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => return Err("expected ext mpack kind".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StorageProofs {
|
pub struct StorageProofs {
|
||||||
builder: CircomBuilder<Bn254>,
|
builder: CircomBuilder<Bn254>,
|
||||||
@ -83,9 +107,48 @@ impl StorageProofs {
|
|||||||
|
|
||||||
pub fn proof_build_inputs(
|
pub fn proof_build_inputs(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
mut inputs: &[u8]
|
||||||
) -> Result<CircomCircuit<ark_ec::bn::Bn<ark_bn254::Parameters>>, String> {
|
) -> Result<CircomCircuit<ark_ec::bn::Bn<ark_bn254::Parameters>>, String> {
|
||||||
|
|
||||||
let mut builder = self.builder.clone();
|
let values: rmpv::Value = read_value(&mut inputs).map_err(|e| e.to_string())?;
|
||||||
|
let args: &Vec<(rmpv::Value, rmpv::Value)> = match values.as_map() {
|
||||||
|
Some(args) => args,
|
||||||
|
None => return Err("args must be a map of string to arrays".to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut builder: CircomBuilderParams = self.builder.clone();
|
||||||
|
for (key, val) in args {
|
||||||
|
let name = match key.as_str() {
|
||||||
|
Some(n) => n,
|
||||||
|
None => return Err(format!("expected string value")),
|
||||||
|
};
|
||||||
|
match val {
|
||||||
|
rmpv::Value::Array(vals) => {
|
||||||
|
// add a (name, Vec<u256>) or (name, Vev<Vec<u256>>) arrays
|
||||||
|
if vals.len() > 0 && vals[0].is_array() {
|
||||||
|
for inner_val in vals {
|
||||||
|
match inner_val.as_array() {
|
||||||
|
Some(inner_vals) => {
|
||||||
|
for val in inner_vals {
|
||||||
|
builder.push_input(name, decode_u256(val)?);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for val in vals {
|
||||||
|
builder.push_input(name, decode_u256(val)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
rmpv::Value::Ext(_, _) => {
|
||||||
|
// directly add a (name,u256) arg pair
|
||||||
|
builder.push_input(name, decode_u256(val)?);
|
||||||
|
},
|
||||||
|
_ => return Err("unhandled argument kind".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// vec of vecs is flattened, since wasm expects a contiguous array in memory
|
// vec of vecs is flattened, since wasm expects a contiguous array in memory
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user