chore: minor error handling improvements (#325)
* Minor error handling improvements * Fix error
This commit is contained in:
parent
901ba6442c
commit
2c41a6c554
7 changed files with 62 additions and 58 deletions
|
|
@ -312,12 +312,11 @@ impl Pod for MockMainPod {
|
||||||
.deref(
|
.deref(
|
||||||
&self.statements[..input_statement_offset + i],
|
&self.statements[..input_statement_offset + i],
|
||||||
&self.merkle_proofs_containers,
|
&self.merkle_proofs_containers,
|
||||||
)
|
)?
|
||||||
.unwrap()
|
.check_and_log(&self.params, &s.clone().try_into()?)
|
||||||
.check_and_log(&self.params, &s.clone().try_into().unwrap())
|
.map_err(|e| e.into())
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, middleware::Error>>()
|
.collect::<Result<Vec<_>>>()?;
|
||||||
.unwrap();
|
|
||||||
if !statement_check.iter().all(|b| *b) {
|
if !statement_check.iter().all(|b| *b) {
|
||||||
return Err(Error::statement_not_check());
|
return Err(Error::statement_not_check());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -316,8 +316,10 @@ pub(super) fn add_homog_offset<const D: usize, F: ECFieldExt<D>>(
|
||||||
}
|
}
|
||||||
|
|
||||||
const GROUP_ORDER_STR: &str = "1067993516717146951041484916571792702745057740581727230159139685185762082554198619328292418486241";
|
const GROUP_ORDER_STR: &str = "1067993516717146951041484916571792702745057740581727230159139685185762082554198619328292418486241";
|
||||||
pub static GROUP_ORDER: LazyLock<BigUint> =
|
pub static GROUP_ORDER: LazyLock<BigUint> = LazyLock::new(|| {
|
||||||
LazyLock::new(|| BigUint::from_str_radix(GROUP_ORDER_STR, 10).unwrap());
|
BigUint::from_str_radix(GROUP_ORDER_STR, 10)
|
||||||
|
.expect("The input should be a valid decimal string.")
|
||||||
|
});
|
||||||
|
|
||||||
static FIELD_NUM_SQUARES: LazyLock<BigUint> =
|
static FIELD_NUM_SQUARES: LazyLock<BigUint> =
|
||||||
LazyLock::new(|| (ECField::order() - BigUint::one()) >> 1);
|
LazyLock::new(|| (ECField::order() - BigUint::one()) >> 1);
|
||||||
|
|
@ -605,10 +607,8 @@ impl CircuitBuilderElliptic for CircuitBuilder<GoldilocksField, 2> {
|
||||||
let outputs = ECAddHomogOffset::apply(self, &inputs);
|
let outputs = ECAddHomogOffset::apply(self, &inputs);
|
||||||
// plonky2 expects all gate constraints to be satisfied by the zero vector.
|
// plonky2 expects all gate constraints to be satisfied by the zero vector.
|
||||||
// So our elliptic curve addition gate computes [x,z-b,u,t-b], and we have to add the b here.
|
// So our elliptic curve addition gate computes [x,z-b,u,t-b], and we have to add the b here.
|
||||||
let x = FieldTarget::new(outputs[0..5].try_into().unwrap());
|
let [x, z, u, t] =
|
||||||
let z = FieldTarget::new(outputs[5..10].try_into().unwrap());
|
array::from_fn(|j| FieldTarget::new(array::from_fn(|i| outputs[5 * j + i])));
|
||||||
let u = FieldTarget::new(outputs[10..15].try_into().unwrap());
|
|
||||||
let t = FieldTarget::new(outputs[15..20].try_into().unwrap());
|
|
||||||
let b1 = self.constant(Point::B1);
|
let b1 = self.constant(Point::B1);
|
||||||
let z = self.nnf_add_scalar_times_generator_power(b1, 1, &z);
|
let z = self.nnf_add_scalar_times_generator_power(b1, 1, &z);
|
||||||
let t = self.nnf_add_scalar_times_generator_power(b1, 1, &t);
|
let t = self.nnf_add_scalar_times_generator_power(b1, 1, &t);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::marker::PhantomData;
|
use std::{array, marker::PhantomData};
|
||||||
|
|
||||||
use num::BigUint;
|
use num::BigUint;
|
||||||
use plonky2::{
|
use plonky2::{
|
||||||
|
|
@ -217,7 +217,7 @@ impl<const DEG: usize, NNF: OEF<DEG> + FieldExtension<DEG, BaseField = F>>
|
||||||
inputs.extend_from_slice(&x.components);
|
inputs.extend_from_slice(&x.components);
|
||||||
inputs.extend_from_slice(&y.components);
|
inputs.extend_from_slice(&y.components);
|
||||||
let outputs = NNFMulSimple::<DEG, NNF>::apply(self, &inputs);
|
let outputs = NNFMulSimple::<DEG, NNF>::apply(self, &inputs);
|
||||||
OEFTarget::new(outputs.try_into().unwrap())
|
OEFTarget::new(array::from_fn(|i| outputs[i]))
|
||||||
}
|
}
|
||||||
fn nnf_div(&mut self, x: &OEFTarget<DEG, NNF>, y: &OEFTarget<DEG, NNF>) -> OEFTarget<DEG, NNF> {
|
fn nnf_div(&mut self, x: &OEFTarget<DEG, NNF>, y: &OEFTarget<DEG, NNF>) -> OEFTarget<DEG, NNF> {
|
||||||
let one = self.nnf_one();
|
let one = self.nnf_one();
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::array;
|
||||||
|
|
||||||
use plonky2::field::goldilocks_field::GoldilocksField;
|
use plonky2::field::goldilocks_field::GoldilocksField;
|
||||||
|
|
||||||
use crate::backends::plonky2::primitives::ec::{
|
use crate::backends::plonky2::primitives::ec::{
|
||||||
|
|
@ -39,10 +41,8 @@ impl SimpleGate for ECAddHomogOffset {
|
||||||
Self::F: plonky2::field::extension::Extendable<D>,
|
Self::F: plonky2::field::extension::Extendable<D>,
|
||||||
{
|
{
|
||||||
let mut ans = Vec::with_capacity(20);
|
let mut ans = Vec::with_capacity(20);
|
||||||
let x1 = QuinticTensor::from_base(wires[0..5].try_into().unwrap());
|
let [x1, u1, x2, u2] =
|
||||||
let u1 = QuinticTensor::from_base(wires[5..10].try_into().unwrap());
|
array::from_fn(|j| QuinticTensor::from_base(array::from_fn(|i| wires[5 * j + i])));
|
||||||
let x2 = QuinticTensor::from_base(wires[10..15].try_into().unwrap());
|
|
||||||
let u2 = QuinticTensor::from_base(wires[15..20].try_into().unwrap());
|
|
||||||
let out = add_homog_offset(x1, u1, x2, u2);
|
let out = add_homog_offset(x1, u1, x2, u2);
|
||||||
for v in out {
|
for v in out {
|
||||||
ans.extend(v.to_base());
|
ans.extend(v.to_base());
|
||||||
|
|
|
||||||
|
|
@ -528,7 +528,7 @@ impl Intermediate {
|
||||||
h
|
h
|
||||||
}
|
}
|
||||||
fn hash(&self) -> Hash {
|
fn hash(&self) -> Hash {
|
||||||
self.hash.unwrap()
|
self.hash.expect("Hash has not been computed.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -554,7 +554,7 @@ impl Leaf {
|
||||||
h
|
h
|
||||||
}
|
}
|
||||||
fn hash(&self) -> Hash {
|
fn hash(&self) -> Hash {
|
||||||
self.hash.unwrap()
|
self.hash.expect("Hash has not been computed.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -178,23 +178,25 @@ impl CustomPredicateBatchBuilder {
|
||||||
let args = stb
|
let args = stb
|
||||||
.args
|
.args
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| match a {
|
.map(|a| {
|
||||||
|
Ok::<_, Error>(match a {
|
||||||
BuilderArg::Literal(v) => StatementTmplArg::Literal(v.clone()),
|
BuilderArg::Literal(v) => StatementTmplArg::Literal(v.clone()),
|
||||||
BuilderArg::Key(pod_id_wc, key_str) => StatementTmplArg::AnchoredKey(
|
BuilderArg::Key(pod_id_wc, key_str) => StatementTmplArg::AnchoredKey(
|
||||||
resolve_wildcard(args, priv_args, pod_id_wc),
|
resolve_wildcard(args, priv_args, pod_id_wc)?,
|
||||||
Key::from(key_str),
|
Key::from(key_str),
|
||||||
),
|
),
|
||||||
BuilderArg::WildcardLiteral(v) => {
|
BuilderArg::WildcardLiteral(v) => {
|
||||||
StatementTmplArg::Wildcard(resolve_wildcard(args, priv_args, v))
|
StatementTmplArg::Wildcard(resolve_wildcard(args, priv_args, v)?)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
})
|
||||||
StatementTmpl {
|
.collect::<Result<_>>()?;
|
||||||
|
Ok(StatementTmpl {
|
||||||
pred: stb.predicate.clone(),
|
pred: stb.predicate.clone(),
|
||||||
args,
|
args,
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect();
|
})
|
||||||
|
.collect::<Result<_>>()?;
|
||||||
let custom_predicate = CustomPredicate::new(
|
let custom_predicate = CustomPredicate::new(
|
||||||
&self.params,
|
&self.params,
|
||||||
name.into(),
|
name.into(),
|
||||||
|
|
@ -215,12 +217,16 @@ impl CustomPredicateBatchBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_wildcard(args: &[&str], priv_args: &[&str], s: &str) -> Wildcard {
|
fn resolve_wildcard(args: &[&str], priv_args: &[&str], s: &str) -> Result<Wildcard> {
|
||||||
args.iter()
|
args.iter()
|
||||||
.chain(priv_args.iter())
|
.chain(priv_args.iter())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find_map(|(i, name)| (s == *name).then_some(Wildcard::new(s.to_string(), i)))
|
.find_map(|(i, name)| (s == *name).then_some(Wildcard::new(s.to_string(), i)))
|
||||||
.unwrap()
|
.ok_or(Error::custom(format!(
|
||||||
|
"Wildcard {} not amongst args {:?}",
|
||||||
|
s,
|
||||||
|
[args.to_vec(), priv_args.to_vec()].concat()
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -200,45 +200,44 @@ impl MainPodBuilder {
|
||||||
/// - {Dict,Array,Set}Contains/NotContains becomes Contains/NotContains.
|
/// - {Dict,Array,Set}Contains/NotContains becomes Contains/NotContains.
|
||||||
/// - GtEqFromEntries/GtFromEntries/GtToNotEqual becomes
|
/// - GtEqFromEntries/GtFromEntries/GtToNotEqual becomes
|
||||||
/// LtEqFromEntries/LtFromEntries/LtToNotEqual.
|
/// LtEqFromEntries/LtFromEntries/LtToNotEqual.
|
||||||
fn lower_op(op: Operation) -> Operation {
|
fn lower_op(op: Operation) -> Result<Operation> {
|
||||||
use NativeOperation::*;
|
use NativeOperation::*;
|
||||||
use OperationType::*;
|
use OperationType::*;
|
||||||
|
let op_type = op.0.clone();
|
||||||
match op.0 {
|
match op.0 {
|
||||||
Native(DictContainsFromEntries) => {
|
Native(DictContainsFromEntries) => <[_; 3]>::try_from(op.1).map(|[dict, key, value]| {
|
||||||
let [dict, key, value] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(Native(ContainsFromEntries), vec![dict, key, value], op.2)
|
Operation(Native(ContainsFromEntries), vec![dict, key, value], op.2)
|
||||||
}
|
}),
|
||||||
Native(DictNotContainsFromEntries) => {
|
Native(DictNotContainsFromEntries) => <[_; 2]>::try_from(op.1).map(|[dict, key]| {
|
||||||
let [dict, key] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(Native(NotContainsFromEntries), vec![dict, key], op.2)
|
Operation(Native(NotContainsFromEntries), vec![dict, key], op.2)
|
||||||
}
|
}),
|
||||||
Native(SetContainsFromEntries) => {
|
Native(SetContainsFromEntries) => <[_; 2]>::try_from(op.1).map(|[set, value]| {
|
||||||
let [set, value] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(
|
Operation(
|
||||||
Native(ContainsFromEntries),
|
Native(ContainsFromEntries),
|
||||||
vec![set, value.clone(), value],
|
vec![set, value.clone(), value],
|
||||||
op.2,
|
op.2,
|
||||||
)
|
)
|
||||||
}
|
}),
|
||||||
Native(SetNotContainsFromEntries) => {
|
Native(SetNotContainsFromEntries) => <[_; 2]>::try_from(op.1).map(|[set, value]| {
|
||||||
let [set, value] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(Native(NotContainsFromEntries), vec![set, value], op.2)
|
Operation(Native(NotContainsFromEntries), vec![set, value], op.2)
|
||||||
}
|
}),
|
||||||
Native(ArrayContainsFromEntries) => {
|
Native(ArrayContainsFromEntries) => {
|
||||||
let [array, index, value] = op.1.try_into().unwrap(); // TODO: Error handling
|
<[_; 3]>::try_from(op.1).map(|[array, index, value]| {
|
||||||
Operation(Native(ContainsFromEntries), vec![array, index, value], op.2)
|
Operation(Native(ContainsFromEntries), vec![array, index, value], op.2)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
Native(GtEqFromEntries) => {
|
Native(GtEqFromEntries) => <[_; 2]>::try_from(op.1).map(|[entry1, entry2]| {
|
||||||
let [entry1, entry2] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(Native(LtEqFromEntries), vec![entry2, entry1], op.2)
|
Operation(Native(LtEqFromEntries), vec![entry2, entry1], op.2)
|
||||||
}
|
}),
|
||||||
Native(GtFromEntries) => {
|
Native(GtFromEntries) => <[_; 2]>::try_from(op.1).map(|[entry1, entry2]| {
|
||||||
let [entry1, entry2] = op.1.try_into().unwrap(); // TODO: Error handling
|
|
||||||
Operation(Native(LtFromEntries), vec![entry2, entry1], op.2)
|
Operation(Native(LtFromEntries), vec![entry2, entry1], op.2)
|
||||||
|
}),
|
||||||
|
Native(GtToNotEqual) => Ok(Operation(Native(LtToNotEqual), op.1, op.2)),
|
||||||
|
_ => Ok(op),
|
||||||
}
|
}
|
||||||
Native(GtToNotEqual) => Operation(Native(LtToNotEqual), op.1, op.2),
|
.map_err(|_| {
|
||||||
_ => op,
|
Error::op_invalid_args(format!("Invalid arg count in operation {:?}", op_type))
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fills in auxiliary data if necessary/possible.
|
/// Fills in auxiliary data if necessary/possible.
|
||||||
|
|
@ -494,7 +493,7 @@ impl MainPodBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn op(&mut self, public: bool, op: Operation) -> Result<Statement> {
|
fn op(&mut self, public: bool, op: Operation) -> Result<Statement> {
|
||||||
let op = Self::fill_in_aux(Self::lower_op(op))?;
|
let op = Self::fill_in_aux(Self::lower_op(op)?)?;
|
||||||
let st = self.op_statement(op.clone())?;
|
let st = self.op_statement(op.clone())?;
|
||||||
self.insert(public, (st, op));
|
self.insert(public, (st, op));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue