@@ -22,16 +22,15 @@ use crate::parsed::visitor::{Children, ExpressionVisitable};
2222pub use crate :: parsed:: BinaryOperator ;
2323pub use crate :: parsed:: UnaryOperator ;
2424use crate :: parsed:: {
25- self , ArrayExpression , EnumDeclaration , EnumVariant , NamedType , SourceReference ,
26- TraitDeclaration , TraitImplementation , TypeDeclaration ,
25+ self , ArrayExpression , EnumDeclaration , EnumVariant , FunctionCall , IndexAccess , NamedType ,
26+ SourceReference , TraitDeclaration , TraitImplementation , TypeDeclaration ,
2727} ;
2828pub use contains_next_ref:: ContainsNextRef ;
2929
3030#[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , PartialEq , Eq , Hash ) ]
3131pub enum StatementIdentifier {
3232 /// Either an intermediate column or a definition.
3333 Definition ( String ) ,
34- PublicDeclaration ( String ) ,
3534 /// Index into the vector of proof items / identities.
3635 ProofItem ( usize ) ,
3736 /// Index into the vector of prover functions.
@@ -44,7 +43,6 @@ pub enum StatementIdentifier {
4443pub struct Analyzed < T > {
4544 pub definitions : HashMap < String , ( Symbol , Option < FunctionValueDefinition > ) > ,
4645 pub solved_impls : SolvedTraitImpls ,
47- pub public_declarations : HashMap < String , PublicDeclaration > ,
4846 pub intermediate_columns : HashMap < String , ( Symbol , Vec < AlgebraicExpression < T > > ) > ,
4947 pub identities : Vec < Identity < T > > ,
5048 pub prover_functions : Vec < Expression > ,
@@ -122,7 +120,10 @@ impl<T> Analyzed<T> {
122120 }
123121 /// @returns the number of public inputs
124122 pub fn publics_count ( & self ) -> usize {
125- self . public_declarations . len ( )
123+ self . definitions
124+ . iter ( )
125+ . filter ( |( _name, ( symbol, _) ) | matches ! ( symbol. kind, SymbolKind :: Public ( ) ) )
126+ . count ( )
126127 }
127128
128129 pub fn name_to_poly_id ( & self ) -> BTreeMap < String , PolyID > {
@@ -187,8 +188,12 @@ impl<T> Analyzed<T> {
187188 & self ,
188189 ) -> impl Iterator < Item = ( & String , & PublicDeclaration ) > {
189190 self . source_order . iter ( ) . filter_map ( move |statement| {
190- if let StatementIdentifier :: PublicDeclaration ( name) = statement {
191- if let Some ( public_declaration) = self . public_declarations . get ( name) {
191+ if let StatementIdentifier :: Definition ( name) = statement {
192+ if let Some ( (
193+ _,
194+ Some ( FunctionValueDefinition :: PublicDeclaration ( public_declaration) ) ,
195+ ) ) = self . definitions . get ( name)
196+ {
192197 return Some ( ( name, public_declaration) ) ;
193198 }
194199 }
@@ -398,29 +403,26 @@ impl<T> Analyzed<T> {
398403 /// Retrieves (name, col_name, poly_id, offset, stage) of each public witness in the trace.
399404 pub fn get_publics ( & self ) -> Vec < ( String , String , PolyID , usize , u8 ) > {
400405 let mut publics = self
401- . public_declarations
402- . values ( )
403- . map ( |public_declaration| {
406+ . public_declarations_in_source_order ( )
407+ . map ( |( name, public_declaration) | {
404408 let column_name = public_declaration. referenced_poly_name ( ) ;
405409 let ( poly_id, stage) = {
406- let symbol = & self . definitions [ & public_declaration. polynomial . name ] . 0 ;
410+ let symbol = & self . definitions [ & public_declaration. referenced_poly ( ) . name ] . 0 ;
407411 (
408412 symbol
409413 . array_elements ( )
410- . nth ( public_declaration. array_index . unwrap_or_default ( ) )
414+ . nth (
415+ public_declaration
416+ . referenced_poly_array_index ( )
417+ . unwrap_or_default ( ) ,
418+ )
411419 . unwrap ( )
412420 . 1 ,
413421 symbol. stage . unwrap_or_default ( ) as u8 ,
414422 )
415423 } ;
416- let row_offset = public_declaration. index as usize ;
417- (
418- public_declaration. name . clone ( ) ,
419- column_name,
420- poly_id,
421- row_offset,
422- stage,
423- )
424+ let row_offset = public_declaration. row ( ) as usize ;
425+ ( name. clone ( ) , column_name, poly_id, row_offset, stage)
424426 } )
425427 . collect :: < Vec < _ > > ( ) ;
426428
@@ -559,6 +561,7 @@ pub fn type_from_definition(
559561 ty : trait_func. ty . clone ( ) ,
560562 } )
561563 }
564+ FunctionValueDefinition :: PublicDeclaration ( _) => Some ( Type :: Expr . into ( ) ) ,
562565 }
563566 } else {
564567 assert ! (
@@ -771,6 +774,8 @@ impl Symbol {
771774pub enum SymbolKind {
772775 /// Fixed, witness or intermediate polynomial
773776 Poly ( PolynomialType ) ,
777+ /// Public declaration
778+ Public ( ) ,
774779 /// Other symbol, depends on the type.
775780 /// Examples include functions not of the type "int -> fe".
776781 Other ( ) ,
@@ -784,6 +789,7 @@ pub enum FunctionValueDefinition {
784789 TypeConstructor ( Arc < EnumDeclaration > , EnumVariant ) ,
785790 TraitDeclaration ( TraitDeclaration ) ,
786791 TraitFunction ( Arc < TraitDeclaration > , NamedType ) ,
792+ PublicDeclaration ( PublicDeclaration ) ,
787793}
788794
789795impl Children < Expression > for FunctionValueDefinition {
@@ -799,6 +805,7 @@ impl Children<Expression> for FunctionValueDefinition {
799805 FunctionValueDefinition :: TypeConstructor ( _, variant) => variant. children ( ) ,
800806 FunctionValueDefinition :: TraitDeclaration ( trait_decl) => trait_decl. children ( ) ,
801807 FunctionValueDefinition :: TraitFunction ( _, trait_func) => trait_func. children ( ) ,
808+ FunctionValueDefinition :: PublicDeclaration ( pub_decl) => pub_decl. children ( ) ,
802809 }
803810 }
804811
@@ -814,6 +821,7 @@ impl Children<Expression> for FunctionValueDefinition {
814821 FunctionValueDefinition :: TypeConstructor ( _, variant) => variant. children_mut ( ) ,
815822 FunctionValueDefinition :: TraitDeclaration ( trait_decl) => trait_decl. children_mut ( ) ,
816823 FunctionValueDefinition :: TraitFunction ( _, trait_func) => trait_func. children_mut ( ) ,
824+ FunctionValueDefinition :: PublicDeclaration ( pub_decl) => pub_decl. children_mut ( ) ,
817825 }
818826 }
819827}
@@ -836,26 +844,80 @@ impl Children<Expression> for NamedType {
836844 }
837845}
838846
839- #[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , Hash ) ]
847+ #[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , Hash , PartialEq , Eq ) ]
840848pub struct PublicDeclaration {
841849 pub id : u64 ,
842850 pub source : SourceRef ,
843851 pub name : String ,
844- pub polynomial : PolynomialReference ,
845- pub array_index : Option < usize > ,
846- /// The evaluation point of the polynomial, not the array index.
847- pub index : DegreeType ,
852+ /// The declaration value, in two possible forms: polynomial[array_index](row) OR polynomial(row)
853+ /// where "row" is the evaluation point of the polynomial.
854+ pub value : Expression ,
848855}
849856
850857impl PublicDeclaration {
858+ pub fn referenced_poly ( & self ) -> & PolynomialReference {
859+ match & self . value {
860+ Expression :: FunctionCall ( _, FunctionCall { function, .. } ) => match function. as_ref ( ) {
861+ Expression :: Reference ( _, Reference :: Poly ( poly) ) => poly,
862+ Expression :: IndexAccess ( _, IndexAccess { array, .. } ) => match array. as_ref ( ) {
863+ Expression :: Reference ( _, Reference :: Poly ( poly) ) => poly,
864+ _ => panic ! ( "Expected Reference." ) ,
865+ } ,
866+ _ => panic ! ( "Expected Reference or IndexAccess." ) ,
867+ } ,
868+ _ => panic ! ( "Expected FunctionCall." ) ,
869+ }
870+ }
871+
872+ pub fn referenced_poly_array_index ( & self ) -> Option < usize > {
873+ match & self . value {
874+ Expression :: FunctionCall ( _, FunctionCall { function, .. } ) => match function. as_ref ( ) {
875+ Expression :: Reference ( _, Reference :: Poly ( _) ) => None ,
876+ Expression :: IndexAccess ( _, IndexAccess { index, .. } ) => match index. as_ref ( ) {
877+ Expression :: Number ( _, index) => Some ( index. value . clone ( ) . try_into ( ) . unwrap ( ) ) ,
878+ _ => panic ! ( "Expected Number." ) ,
879+ } ,
880+ _ => panic ! ( "Expected Reference or IndexAccess." ) ,
881+ } ,
882+ _ => panic ! ( "Expected FunctionCall." ) ,
883+ }
884+ }
885+
886+ /// Returns the name of the polynomial referenced by the public declaration.
887+ /// Includes the array index if present.
851888 pub fn referenced_poly_name ( & self ) -> String {
852- match self . array_index {
853- Some ( index) => format ! ( "{}[{}]" , self . polynomial. name, index) ,
854- None => self . polynomial . name . clone ( ) ,
889+ let name = self . referenced_poly ( ) . name . clone ( ) ;
890+ let index = self . referenced_poly_array_index ( ) ;
891+ if let Some ( index) = index {
892+ format ! ( "{name}[{index}]" )
893+ } else {
894+ name
895+ }
896+ }
897+
898+ pub fn row ( & self ) -> DegreeType {
899+ match & self . value {
900+ Expression :: FunctionCall ( _, FunctionCall { arguments, .. } ) => {
901+ assert ! ( arguments. len( ) == 1 ) ;
902+ match & arguments[ 0 ] {
903+ Expression :: Number ( _, index) => index. value . clone ( ) . try_into ( ) . unwrap ( ) ,
904+ _ => panic ! ( "Expected Number." ) ,
905+ }
906+ }
907+ _ => panic ! ( "Expected FunctionCall." ) ,
855908 }
856909 }
857910}
858911
912+ impl Children < Expression > for PublicDeclaration {
913+ fn children ( & self ) -> Box < dyn Iterator < Item = & Expression > + ' _ > {
914+ Box :: new ( self . value . children ( ) )
915+ }
916+ fn children_mut ( & mut self ) -> Box < dyn Iterator < Item = & mut Expression > + ' _ > {
917+ Box :: new ( self . value . children_mut ( ) )
918+ }
919+ }
920+
859921#[ derive(
860922 Debug , PartialEq , Eq , PartialOrd , Ord , Clone , Serialize , Deserialize , JsonSchema , Hash ,
861923) ]
0 commit comments