From 298883cd8565ddeef9f8f115095be04c54b031a1 Mon Sep 17 00:00:00 2001 From: Steve Wang Date: Mon, 17 Jul 2023 14:32:42 +0800 Subject: [PATCH] reorganized tests; retired irrelevant ones --- src/convert_to_chiquito.rs | 222 +++--- src/deserialize.rs | 497 +++++++++++++ src/tests/test_deserialize.rs | 1231 --------------------------------- 3 files changed, 606 insertions(+), 1344 deletions(-) delete mode 100644 src/tests/test_deserialize.rs diff --git a/src/convert_to_chiquito.rs b/src/convert_to_chiquito.rs index c7f6683..47babfc 100644 --- a/src/convert_to_chiquito.rs +++ b/src/convert_to_chiquito.rs @@ -16,134 +16,130 @@ use chiquito::{ }; use std::collections::HashMap; -impl Circuit { - fn to_chiquito_ast(self: Circuit) -> cCircuit { - let ast = circuit::("", |ctx| { - let mut uuid_map: HashMap = HashMap::new(); // Python id to Rust id - let mut query_map: HashMap> = HashMap::new(); // Rust id to Rust Queriable - let mut step_type_handler_map: HashMap = HashMap::new(); +pub fn to_chiquito_ast(circuit_to_convert: Circuit) -> cCircuit { + let ast = circuit::("", |ctx| { + let mut uuid_map: HashMap = HashMap::new(); // Python id to Rust id + let mut query_map: HashMap> = HashMap::new(); // Rust id to Rust Queriable + let mut step_type_handler_map: HashMap = HashMap::new(); - for p in self.forward_signals.iter() { - let r = ctx.forward_with_phase(p.annotation, p.phase); - uuid_map.insert(p.id, r.uuid()); - query_map.insert(r.uuid(), r); // Queriable implements Copy. - } + for p in circuit_to_convert.forward_signals.iter() { + let r = ctx.forward_with_phase(p.annotation, p.phase); + uuid_map.insert(p.id, r.uuid()); + query_map.insert(r.uuid(), r); // Queriable implements Copy. + } - for p in self.shared_signals.iter() { - let r = ctx.shared_with_phase(p.annotation, p.phase); - uuid_map.insert(p.id, r.uuid()); - query_map.insert(r.uuid(), r); - } + for p in circuit_to_convert.shared_signals.iter() { + let r = ctx.shared_with_phase(p.annotation, p.phase); + uuid_map.insert(p.id, r.uuid()); + query_map.insert(r.uuid(), r); + } - for p in self.fixed_signals.iter() { - let r = ctx.fixed(p.annotation); - uuid_map.insert(p.id, r.uuid()); - query_map.insert(r.uuid(), r); - } + for p in circuit_to_convert.fixed_signals.iter() { + let r = ctx.fixed(p.annotation); + uuid_map.insert(p.id, r.uuid()); + query_map.insert(r.uuid(), r); + } - for p in self.exposed.iter() { - let r_id = uuid_map - .get(&p.id) - .expect("Exposed signal not found in uuid_map."); - let r = *query_map - .get(r_id) - .expect("Exposed signal not found in forward_query_map."); - ctx.expose(r); - } + for p in circuit_to_convert.exposed.iter() { + let r_id = uuid_map + .get(&p.id) + .expect("Exposed signal not found in uuid_map."); + let r = *query_map + .get(r_id) + .expect("Exposed signal not found in forward_query_map."); + ctx.expose(r); + } - for (step_type_id, step_type) in self.step_types.clone() { - let handler = ctx.step_type(Box::leak(step_type.name.clone().into_boxed_str())); - uuid_map.insert(step_type_id, handler.uuid()); - step_type_handler_map.insert(handler.uuid(), handler); // StepTypeHandler impl Copy trait. + for (step_type_id, step_type) in circuit_to_convert.step_types.clone() { + let handler = ctx.step_type(Box::leak(step_type.name.clone().into_boxed_str())); + uuid_map.insert(step_type_id, handler.uuid()); + step_type_handler_map.insert(handler.uuid(), handler); // StepTypeHandler impl Copy trait. - ctx.step_type_def(handler, |ctx| { - for p in step_type.signals.iter() { - let r = ctx.internal(p.annotation); - uuid_map.insert(p.id, r.uuid()); - query_map.insert(r.uuid(), r); + ctx.step_type_def(handler, |ctx| { + for p in step_type.signals.iter() { + let r = ctx.internal(p.annotation); + uuid_map.insert(p.id, r.uuid()); + query_map.insert(r.uuid(), r); + } + ctx.setup(|ctx| { + for p in step_type.constraints.iter() { + let constraint = cbConstraint { + annotation: p.annotation.clone(), + expr: to_chiquito_expr(&p.expr, &uuid_map, &query_map), + typing: Typing::AntiBooly, + }; + ctx.constr(constraint); + } + for p in step_type.transition_constraints.iter() { + let constraint = cbConstraint { + annotation: p.annotation.clone(), + expr: to_chiquito_expr(&p.expr, &uuid_map, &query_map), + typing: Typing::AntiBooly, + }; + ctx.transition(constraint); } - ctx.setup(|ctx| { - for p in step_type.constraints.iter() { - let constraint = cbConstraint { - annotation: p.annotation.clone(), - expr: p.expr.to_chiquito_expr(&uuid_map, &query_map), - typing: Typing::AntiBooly, - }; - ctx.constr(constraint); - } - for p in step_type.transition_constraints.iter() { - let constraint = cbConstraint { - annotation: p.annotation.clone(), - expr: p.expr.to_chiquito_expr(&uuid_map, &query_map), - typing: Typing::AntiBooly, - }; - ctx.transition(constraint); - } - }); - - ctx.wg(|_ctx, ()| {}) // Don't need wg for ast. }); - if let Some(p_id) = self.first_step { - let r_id = uuid_map - .get(&p_id) - .expect("Step type not found in uuid_map."); - let r = *step_type_handler_map - .get(r_id) - .expect("Step type not found in step_type_handler_map."); - ctx.pragma_first_step(r); - } - - if let Some(p_id) = self.last_step { - let r_id = uuid_map - .get(&p_id) - .expect("Step type not found in uuid_map."); - let r = *step_type_handler_map - .get(r_id) - .expect("Step type not found in step_type_handler_map."); - ctx.pragma_last_step(r); - } + ctx.wg(|_ctx, ()| {}) // Don't need wg for ast. + }); - ctx.pragma_num_steps(self.num_steps); + if let Some(p_id) = circuit_to_convert.first_step { + let r_id = uuid_map + .get(&p_id) + .expect("Step type not found in uuid_map."); + let r = *step_type_handler_map + .get(r_id) + .expect("Step type not found in step_type_handler_map."); + ctx.pragma_first_step(r); } - }); - - ast - } -} -impl Expr { - fn to_chiquito_expr( - self: &Expr, - uuid_map: &HashMap, - query_map: &HashMap>, - ) -> cExpr { - match self { - Expr::Const(p) => cExpr::Const(*p), - Expr::Sum(p) => cExpr::Sum( - p.into_iter() - .map(|p| p.to_chiquito_expr(uuid_map, query_map)) - .collect(), - ), - Expr::Mul(p) => cExpr::Mul( - p.into_iter() - .map(|p| p.to_chiquito_expr(uuid_map, query_map)) - .collect(), - ), - Expr::Neg(p) => cExpr::Neg(Box::new((*p).to_chiquito_expr(uuid_map, query_map))), - Expr::Pow(arg0, arg1) => cExpr::Pow( - Box::new((*arg0).to_chiquito_expr(uuid_map, query_map)), - *arg1, - ), - Expr::Query(p) => { + if let Some(p_id) = circuit_to_convert.last_step { let r_id = uuid_map - .get(&p.uuid()) - .expect("Exposed signal not found in uuid_map."); - let r = *query_map + .get(&p_id) + .expect("Step type not found in uuid_map."); + let r = *step_type_handler_map .get(r_id) - .expect("Exposed signal not found in forward_query_map."); - cExpr::Query(r) + .expect("Step type not found in step_type_handler_map."); + ctx.pragma_last_step(r); } + + ctx.pragma_num_steps(circuit_to_convert.num_steps); + } + }); + + ast +} + +pub fn to_chiquito_expr( + expr_to_convert: &Expr, + uuid_map: &HashMap, + query_map: &HashMap>, +) -> cExpr { + match expr_to_convert { + Expr::Const(p) => cExpr::Const(*p), + Expr::Sum(p) => cExpr::Sum( + p.into_iter() + .map(|p| to_chiquito_expr(p, uuid_map, query_map)) + .collect(), + ), + Expr::Mul(p) => cExpr::Mul( + p.into_iter() + .map(|p| to_chiquito_expr(p, uuid_map, query_map)) + .collect(), + ), + Expr::Neg(p) => cExpr::Neg(Box::new(to_chiquito_expr(p, uuid_map, query_map))), + Expr::Pow(arg0, arg1) => cExpr::Pow( + Box::new(to_chiquito_expr(&arg0, uuid_map, query_map)), + *arg1, + ), + Expr::Query(p) => { + let r_id = uuid_map + .get(&p.uuid()) + .expect("Exposed signal not found in uuid_map."); + let r = *query_map + .get(r_id) + .expect("Exposed signal not found in forward_query_map."); + cExpr::Query(r) } } } diff --git a/src/deserialize.rs b/src/deserialize.rs index 1766e3e..e8f1bec 100644 --- a/src/deserialize.rs +++ b/src/deserialize.rs @@ -585,3 +585,500 @@ impl_deserialize!(StepTypeVisitor, StepType); impl_deserialize!(CircuitVisitor, Circuit); impl_deserialize!(TraceWitnessVisitor, TraceWitness); impl_deserialize!(StepInstanceVisitor, StepInstance); + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_circuit() { + use crate::convert_to_chiquito::to_chiquito_ast; + let json = r#" + { + "step_types": { + "10": { + "id": 10, + "name": "fibo_step", + "signals": [ + { + "id": 11, + "annotation": "c" + } + ], + "constraints": [ + { + "annotation": "((a + b) == c)", + "expr": { + "Sum": [ + { + "Forward": [ + { + "id": 8, + "phase": 0, + "annotation": "a" + }, + false + ] + }, + { + "Forward": [ + { + "id": 9, + "phase": 0, + "annotation": "b" + }, + false + ] + }, + { + "Neg": { + "Internal": { + "id": 11, + "annotation": "c" + } + } + } + ] + } + } + ], + "transition_constraints": [ + { + "annotation": "(b == next(a))", + "expr": { + "Sum": [ + { + "Forward": [ + { + "id": 9, + "phase": 0, + "annotation": "b" + }, + false + ] + }, + { + "Neg": { + "Forward": [ + { + "id": 8, + "phase": 0, + "annotation": "a" + }, + true + ] + } + } + ] + } + }, + { + "annotation": "(c == next(b))", + "expr": { + "Sum": [ + { + "Internal": { + "id": 11, + "annotation": "c" + } + }, + { + "Neg": { + "Forward": [ + { + "id": 9, + "phase": 0, + "annotation": "b" + }, + true + ] + } + } + ] + } + } + ], + "annotations": { + "11": "c" + } + }, + "12": { + "id": 12, + "name": "fibo_last_step", + "signals": [ + { + "id": 13, + "annotation": "c" + } + ], + "constraints": [ + { + "annotation": "((a + b) == c)", + "expr": { + "Sum": [ + { + "Forward": [ + { + "id": 8, + "phase": 0, + "annotation": "a" + }, + false + ] + }, + { + "Forward": [ + { + "id": 9, + "phase": 0, + "annotation": "b" + }, + false + ] + }, + { + "Neg": { + "Internal": { + "id": 13, + "annotation": "c" + } + } + } + ] + } + } + ], + "transition_constraints": [], + "annotations": { + "13": "c" + } + } + }, + "forward_signals": [ + { + "id": 8, + "phase": 0, + "annotation": "a" + }, + { + "id": 9, + "phase": 0, + "annotation": "b" + } + ], + "shared_signals": [], + "fixed_signals": [], + "exposed": [], + "annotations": { + "8": "a", + "9": "b", + "10": "fibo_step", + "12": "fibo_last_step" + }, + "first_step": 10, + "last_step": null, + "num_steps": 0, + "id": 1 + } + "#; + let circuit: Circuit = serde_json::from_str(json).unwrap(); + // print!("{:?}", circuit); + println!("{:?}", to_chiquito_ast::<()>(circuit)); + } + + #[test] + fn test_step_type() { + let json = r#" + { + "id":1, + "name":"fibo", + "signals":[ + { + "id":1, + "annotation":"a" + }, + { + "id":2, + "annotation":"b" + } + ], + "constraints":[ + { + "annotation":"constraint", + "expr":{ + "Sum":[ + { + "Const":1 + }, + { + "Mul":[ + { + "Internal":{ + "id":3, + "annotation":"c" + } + }, + { + "Const":3 + } + ] + } + ] + } + }, + { + "annotation":"constraint", + "expr":{ + "Sum":[ + { + "Const":1 + }, + { + "Mul":[ + { + "Shared":[ + { + "id":4, + "phase":2, + "annotation":"d" + }, + 1 + ] + }, + { + "Const":3 + } + ] + } + ] + } + } + ], + "transition_constraints":[ + { + "annotation":"trans", + "expr":{ + "Sum":[ + { + "Const":1 + }, + { + "Mul":[ + { + "Forward":[ + { + "id":5, + "phase":1, + "annotation":"e" + }, + true + ] + }, + { + "Const":3 + } + ] + } + ] + } + }, + { + "annotation":"trans", + "expr":{ + "Sum":[ + { + "Const":1 + }, + { + "Mul":[ + { + "Fixed":[ + { + "id":6, + "annotation":"e" + }, + 2 + ] + }, + { + "Const":3 + } + ] + } + ] + } + } + ], + "annotations":{ + "5":"a", + "6":"b", + "7":"c" + } + } + "#; + let step_type: StepType = serde_json::from_str(json).unwrap(); + // print!("{:?}", step_type); + println!("{:?}", step_type); + } + + #[test] + fn test_constraint() { + let json = r#" + {"annotation": "constraint", + "expr": + { + "Sum": [ + { + "Internal": { + "id": 27, + "annotation": "a" + } + }, + { + "Fixed": [ + { + "id": 28, + "annotation": "b" + }, + 1 + ] + }, + { + "Shared": [ + { + "id": 29, + "phase": 1, + "annotation": "c" + }, + 2 + ] + }, + { + "Forward": [ + { + "id": 30, + "phase": 2, + "annotation": "d" + }, + true + ] + }, + { + "StepTypeNext": { + "id": 31, + "annotation": "e" + } + }, + { + "Const": 3 + }, + { + "Mul": [ + { + "Const": 4 + }, + { + "Const": 5 + } + ] + }, + { + "Neg": { + "Const": 2 + } + }, + { + "Pow": [ + { + "Const": 3 + }, + 4 + ] + } + ] + } + }"#; + let constraint: Constraint = serde_json::from_str(json).unwrap(); + println!("{:?}", constraint); + let transition_constraint: TransitionConstraint = serde_json::from_str(json).unwrap(); + println!("{:?}", transition_constraint); + } + + #[test] + fn test_expr() { + let json = r#" + { + "Sum": [ + { + "Internal": { + "id": 27, + "annotation": "a" + } + }, + { + "Fixed": [ + { + "id": 28, + "annotation": "b" + }, + 1 + ] + }, + { + "Shared": [ + { + "id": 29, + "phase": 1, + "annotation": "c" + }, + 2 + ] + }, + { + "Forward": [ + { + "id": 30, + "phase": 2, + "annotation": "d" + }, + true + ] + }, + { + "StepTypeNext": { + "id": 31, + "annotation": "e" + } + }, + { + "Const": 3 + }, + { + "Mul": [ + { + "Const": 4 + }, + { + "Const": 5 + } + ] + }, + { + "Neg": { + "Const": 2 + } + }, + { + "Pow": [ + { + "Const": 3 + }, + 4 + ] + } + ] + }"#; + let expr: Expr = serde_json::from_str(json).unwrap(); + // println!("{}", serde_json::to_string(&expr).unwrap()); + println!("{:?}", expr); + } +} diff --git a/src/tests/test_deserialize.rs b/src/tests/test_deserialize.rs deleted file mode 100644 index c4b7ae5..0000000 --- a/src/tests/test_deserialize.rs +++ /dev/null @@ -1,1231 +0,0 @@ -#[cfg(test)] -mod tests { - use halo2_proofs::{halo2curves::bn256::Fr, plonk::Fixed}; - use serde::{Deserialize, Serialize}; - use serde_json::*; - use std::{collections::HashMap, fmt::Debug, rc::Rc}; - - #[test] - fn test() { - #[derive(Clone, Deserialize, Serialize)] - #[serde(tag = "t", content = "c")] - pub enum Expr { - Const(F), - Sum(Vec>), - Mul(Vec>), - Neg(Box>), - Pow(Box>, u32), - // Query(Queriable), - } - - impl Debug for Expr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Const(arg0) => { - let formatted = format!("{:?}", arg0); - if formatted.starts_with("0x") { - let s = format!( - "0x{}", - formatted.trim_start_matches("0x").trim_start_matches('0') - ); - write!(f, "{}", s) - } else { - write!(f, "{}", formatted) - } - } - Self::Sum(arg0) => write!( - f, - "({})", - arg0.iter() - .map(|v| format!("{:?}", v)) - .collect::>() - .join(" + ") - ), - Self::Mul(arg0) => write!( - f, - "({})", - arg0.iter() - .map(|v| format!("{:?}", v)) - .collect::>() - .join(" * ") - ), - Self::Neg(arg0) => write!(f, "-{:?}", arg0), - Self::Pow(arg0, arg1) => write!(f, "({:?})^{}", arg0, arg1), - // Self::Query(arg0) => write!(f, "{:?}", arg0), - // Self::Halo2Expr(arg0) => write!(f, "halo2({:?})", arg0), - } - } - } - - let json = r#"{"t": "Sum", "c": [{"t": "Const", "c": 0}, {"t": "Mul", "c": [{"t": "Const", "c": 1}, {"t": "Const", "c": 2}, {"t": "Neg", "c": {"t": "Const", "c": 3}}]}, {"t": "Pow", "c": [{"t": "Const", "c": 4}, 5]}] }"#; - let expr: Expr = serde_json::from_str(json).unwrap(); - // println!("{}", serde_json::to_string(&expr).unwrap()); - println!("{:?}", expr); - } - - // #[test] - // fn test_query() { - // use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; - // use std::fmt; - // use core::result::Result; - // use serde_json::*; - // use chiquito::ast::{ - // expr::query::Queriable, - // InternalSignal, ForwardSignal, SharedSignal, FixedSignal, - // }; - - // // #[derive(Clone, Deserialize, Serialize)] - // // #[serde(tag = "t", content = "c")] - // // pub enum Queriable { - // // Internal(InternalSignal), - // // Forward(ForwardSignal, bool), - // // Shared(SharedSignal, i32), - // // Fixed(FixedSignal, i32), - // // StepTypeNext(StepTypeHandler), - // // Halo2AdviceQuery(ImportedHalo2Advice, i32), - // // Halo2FixedQuery(ImportedHalo2Fixed, i32), - // // #[allow(non_camel_case_types)] - // // _unaccessible(PhantomData), - // // } - - // let json = r#"{"t": "Sum", "c": [{"t": "Const", "c": 0}, {"t": "Mul", "c": [{"t": "Const", "c": 1}, {"t": "Const", "c": 2}, {"t": "Neg", "c": {"t": "Const", "c": 3}}]}, {"t": "Pow", "c": [{"t": "Const", "c": 4}, 5]}] }"#; - // let query: Queriable:: = serde_json::from_str(json).unwrap(); - // // println!("{}", serde_json::to_string(&expr).unwrap()); - // println!("{:?}", query); - // } - #[test] - fn test_python_circuit_json() { - use crate::Circuit; - let json = r#" - { - "step_types": { - "10": { - "id": 10, - "name": "fibo_step", - "signals": [ - { - "id": 11, - "annotation": "c" - } - ], - "constraints": [ - { - "annotation": "((a + b) == c)", - "expr": { - "Sum": [ - { - "Forward": [ - { - "id": 8, - "phase": 0, - "annotation": "a" - }, - false - ] - }, - { - "Forward": [ - { - "id": 9, - "phase": 0, - "annotation": "b" - }, - false - ] - }, - { - "Neg": { - "Internal": { - "id": 11, - "annotation": "c" - } - } - } - ] - } - } - ], - "transition_constraints": [ - { - "annotation": "(b == next(a))", - "expr": { - "Sum": [ - { - "Forward": [ - { - "id": 9, - "phase": 0, - "annotation": "b" - }, - false - ] - }, - { - "Neg": { - "Forward": [ - { - "id": 8, - "phase": 0, - "annotation": "a" - }, - true - ] - } - } - ] - } - }, - { - "annotation": "(c == next(b))", - "expr": { - "Sum": [ - { - "Internal": { - "id": 11, - "annotation": "c" - } - }, - { - "Neg": { - "Forward": [ - { - "id": 9, - "phase": 0, - "annotation": "b" - }, - true - ] - } - } - ] - } - } - ], - "annotations": { - "11": "c" - } - }, - "12": { - "id": 12, - "name": "fibo_last_step", - "signals": [ - { - "id": 13, - "annotation": "c" - } - ], - "constraints": [ - { - "annotation": "((a + b) == c)", - "expr": { - "Sum": [ - { - "Forward": [ - { - "id": 8, - "phase": 0, - "annotation": "a" - }, - false - ] - }, - { - "Forward": [ - { - "id": 9, - "phase": 0, - "annotation": "b" - }, - false - ] - }, - { - "Neg": { - "Internal": { - "id": 13, - "annotation": "c" - } - } - } - ] - } - } - ], - "transition_constraints": [], - "annotations": { - "13": "c" - } - } - }, - "forward_signals": [ - { - "id": 8, - "phase": 0, - "annotation": "a" - }, - { - "id": 9, - "phase": 0, - "annotation": "b" - } - ], - "shared_signals": [], - "fixed_signals": [], - "exposed": [], - "annotations": { - "8": "a", - "9": "b", - "10": "fibo_step", - "12": "fibo_last_step" - }, - "first_step": 10, - "last_step": null, - "num_steps": 0, - "id": 1 - } - "#; - let circuit: Circuit = serde_json::from_str(json).unwrap(); - // print!("{:?}", circuit); - println!("{:?}", circuit.to_chiquito_ast::<()>()); - } - - #[test] - fn test_python_steptype_json() { - use crate::StepType; - let json = r#"{"id": 1, "name": "fibo", "signals": [{"id": 1, "annotation": "a"}, {"id": 2, "annotation": "b"}], "constraints": [{"annotation": "constraint", "expr": {"Sum": [{"Const": 1}, {"Mul": [{"Internal": {"id": 3, "annotation": "c"}}, {"Const": 3}]}]}}, {"annotation": "constraint", "expr": {"Sum": [{"Const": 1}, {"Mul": [{"Shared": [{"id": 4, "phase": 2, "annotation": "d"}, 1]}, {"Const": 3}]}]}}], "transition_constraints": [{"annotation": "trans", "expr": {"Sum": [{"Const": 1}, {"Mul": [{"Forward": [{"id": 5, "phase": 1, "annotation": "e"}, true]}, {"Const": 3}]}]}}, {"annotation": "trans", "expr": {"Sum": [{"Const": 1}, {"Mul": [{"Fixed": [{"id": 6, "annotation": "e"}, 2]}, {"Const": 3}]}]}}], "annotations": {"5": "a", "6": "b", "7": "c"}}"#; - let step_type: StepType = serde_json::from_str(json).unwrap(); - // println!("{}", serde_json::to_string(&expr).unwrap()); - println!("{:?}", step_type); - } - - #[test] - fn test_convert_same_type() { - use chiquito::ast::expr::Expr as cExpr; - - #[derive(Clone)] - pub enum Expr { - Const(F), - Sum(Vec>), - Mul(Vec>), - Neg(Box>), - Pow(Box>, u32), - // Query(Queriable), - // Halo2Expr(Expression), - } - - impl Expr { - fn to_cexpr(expr: Expr) -> cExpr { - match expr { - Self::Const(arg0) => cExpr::Const(arg0), - Self::Sum(arg0) => cExpr::Sum(arg0.into_iter().map(Self::to_cexpr).collect()), - Self::Mul(arg0) => cExpr::Mul(arg0.into_iter().map(Self::to_cexpr).collect()), - Self::Neg(arg0) => cExpr::Neg(Box::new(Self::to_cexpr(*arg0))), - Self::Pow(arg0, arg1) => { - cExpr::Pow(Box::new(Self::to_cexpr(*arg0)), arg1) - } - // Self::Query(arg0) => cExpr::Query(arg0), - // Self::Halo2Expr(arg0) => cExpr::Halo2Expr(arg0), - } - } - } - - let expr: Expr = Expr::Sum(vec![ - Expr::Const(1), - Expr::Mul(vec![ - Expr::Const(2), - Expr::Const(3), - Expr::Neg(Box::new(Expr::Const(4))), - ]), - Expr::Pow(Box::new(Expr::Const(5)), 6), - ]); - let cexpr = Expr::::to_cexpr(expr); - println!("{:?}", cexpr); - } - - #[test] - fn test_custom_deserialize() { - use crate::{Circuit, Constraint, Expr, StepType, TransitionConstraint}; - use core::result::Result; - use pyo3::prelude::*; - use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor}; - use serde_json::*; - use std::{ - collections::HashMap, - fmt::{self, Debug}, - marker::PhantomData, - }; - - let json_circuit = r#" - { - "step_types": { - "0": { - "id": 3, - "name": "fibo step", - "signals": [ - { - "id": 4, - "annotation": "a" - }, - { - "id": 5, - "annotation": "b" - } - ], - "constraints": [ - { - "annotation": "constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Pow": [ - { - "Internal": { - "id": 32, - "annotation": "f" - } - }, - 4 - ] - }, - { - "Mul": [ - { - "Fixed": [{ - "id": 33, - "annotation": "g" - }, 2] - }, - { - "Internal": { - "id": 34, - "annotation": "h" - } - } - ] - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "transition_constraints": [ - { - "annotation": "transition constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "transition constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "annotations": { - "40": "test annotation 1", - "41": "test annotation 2" - } - }, - "1": { - "id": 3, - "name": "fibo step", - "signals": [ - { - "id": 4, - "annotation": "a" - }, - { - "id": 5, - "annotation": "b" - } - ], - "constraints": [ - { - "annotation": "constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Pow": [ - { - "Internal": { - "id": 32, - "annotation": "f" - } - }, - 4 - ] - }, - { - "Mul": [ - { - "Fixed": [{ - "id": 33, - "annotation": "g" - }, 2] - }, - { - "Internal": { - "id": 34, - "annotation": "h" - } - } - ] - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "transition_constraints": [ - { - "annotation": "transition constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "transition constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "annotations": { - "40": "test annotation 1", - "41": "test annotation 2" - } - } - }, - - "forward_signals": [ - { - "id": 80, - "phase": 1, - "annotation": "l" - }, - { - "id": 81, - "phase": 2, - "annotation": "m" - } - ], - "shared_signals": [ - { - "id": 82, - "phase": 1, - "annotation": "n" - }, - { - "id": 83, - "phase": 2, - "annotation": "o" - } - ], - "fixed_signals": [ - { - "id": 84, - "annotation": "p" - }, - { - "id": 85, - "annotation": "q" - } - ], - "exposed": [ - { - "id": 86, - "phase": 1, - "annotation": "r" - }, - { - "id": 87, - "phase": 2, - "annotation": "s" - } - ], - "annotations": { - "88": "test annotation 3", - "89": "test annotation 4" - }, - "first_step": null, - "last_step": 21, - "num_steps": 10, - "id": 100 - - } - "#; - - let json_circuit: Circuit = serde_json::from_str(json_circuit).unwrap(); - println!("{:?}", json_circuit); - - let json_steptype = r#" - { - "id": 3, - "name": "fibo step", - "signals": [ - { - "id": 4, - "annotation": "a" - }, - { - "id": 5, - "annotation": "b" - } - ], - "constraints": [ - { - "annotation": "constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Pow": [ - { - "Internal": { - "id": 32, - "annotation": "f" - } - }, - 4 - ] - }, - { - "Mul": [ - { - "Fixed": [{ - "id": 33, - "annotation": "g" - }, 2] - }, - { - "Internal": { - "id": 34, - "annotation": "h" - } - } - ] - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "transition_constraints": [ - { - "annotation": "transition constraint 1", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Neg": { - "Internal": { - "id": 35, - "annotation": "i" - } - } - } - ] - } - }, - { - "annotation": "transition constraint 2", - "expr": - { - "Mul": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - } - ] - } - } - ], - "annotations": { - "40": "test annotation 1", - "41": "test annotation 2" - } - } - "#; - let json_steptype: StepType = serde_json::from_str(json_steptype).unwrap(); - println!("{:?}", json_steptype); - - let json_constraint = r#" - {"annotation": "constraint", - "expr": - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Mul": [ - { - "Const": 4 - }, - { - "Const": 5 - } - ] - }, - { - "Neg": { - "Const": 2 - } - }, - { - "Pow": [ - { - "Const": 3 - }, - 4 - ] - } - ] - } - }"#; - let constraint: Constraint = serde_json::from_str(json_constraint).unwrap(); - println!("{:?}", constraint); - let transition_constraint: TransitionConstraint = - serde_json::from_str(json_constraint).unwrap(); - println!("{:?}", transition_constraint); - - let json_expr = r#" - { - "Sum": [ - { - "Internal": { - "id": 27, - "annotation": "a" - } - }, - { - "Fixed": [ - { - "id": 28, - "annotation": "b" - }, - 1 - ] - }, - { - "Shared": [ - { - "id": 29, - "phase": 1, - "annotation": "c" - }, - 2 - ] - }, - { - "Forward": [ - { - "id": 30, - "phase": 2, - "annotation": "d" - }, - true - ] - }, - { - "StepTypeNext": { - "id": 31, - "annotation": "e" - } - }, - { - "Const": 3 - }, - { - "Mul": [ - { - "Const": 4 - }, - { - "Const": 5 - } - ] - }, - { - "Neg": { - "Const": 2 - } - }, - { - "Pow": [ - { - "Const": 3 - }, - 4 - ] - } - ] - }"#; - let expr: Expr = serde_json::from_str(json_expr).unwrap(); - // println!("{}", serde_json::to_string(&expr).unwrap()); - println!("{:?}", expr); - } -}