@@ -22,16 +22,15 @@ use crate::parsed::visitor::{Children, ExpressionVisitable};
22
22
pub use crate :: parsed:: BinaryOperator ;
23
23
pub use crate :: parsed:: UnaryOperator ;
24
24
use 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 ,
27
27
} ;
28
28
pub use contains_next_ref:: ContainsNextRef ;
29
29
30
30
#[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , PartialEq , Eq , Hash ) ]
31
31
pub enum StatementIdentifier {
32
32
/// Either an intermediate column or a definition.
33
33
Definition ( String ) ,
34
- PublicDeclaration ( String ) ,
35
34
/// Index into the vector of proof items / identities.
36
35
ProofItem ( usize ) ,
37
36
/// Index into the vector of prover functions.
@@ -44,7 +43,6 @@ pub enum StatementIdentifier {
44
43
pub struct Analyzed < T > {
45
44
pub definitions : HashMap < String , ( Symbol , Option < FunctionValueDefinition > ) > ,
46
45
pub solved_impls : SolvedTraitImpls ,
47
- pub public_declarations : HashMap < String , PublicDeclaration > ,
48
46
pub intermediate_columns : HashMap < String , ( Symbol , Vec < AlgebraicExpression < T > > ) > ,
49
47
pub identities : Vec < Identity < T > > ,
50
48
pub prover_functions : Vec < Expression > ,
@@ -122,7 +120,10 @@ impl<T> Analyzed<T> {
122
120
}
123
121
/// @returns the number of public inputs
124
122
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 ( )
126
127
}
127
128
128
129
pub fn name_to_poly_id ( & self ) -> BTreeMap < String , PolyID > {
@@ -187,8 +188,12 @@ impl<T> Analyzed<T> {
187
188
& self ,
188
189
) -> impl Iterator < Item = ( & String , & PublicDeclaration ) > {
189
190
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
+ {
192
197
return Some ( ( name, public_declaration) ) ;
193
198
}
194
199
}
@@ -398,29 +403,26 @@ impl<T> Analyzed<T> {
398
403
/// Retrieves (name, col_name, poly_id, offset, stage) of each public witness in the trace.
399
404
pub fn get_publics ( & self ) -> Vec < ( String , String , PolyID , usize , u8 ) > {
400
405
let mut publics = self
401
- . public_declarations
402
- . values ( )
403
- . map ( |public_declaration| {
406
+ . public_declarations_in_source_order ( )
407
+ . map ( |( name, public_declaration) | {
404
408
let column_name = public_declaration. referenced_poly_name ( ) ;
405
409
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 ;
407
411
(
408
412
symbol
409
413
. 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
+ )
411
419
. unwrap ( )
412
420
. 1 ,
413
421
symbol. stage . unwrap_or_default ( ) as u8 ,
414
422
)
415
423
} ;
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)
424
426
} )
425
427
. collect :: < Vec < _ > > ( ) ;
426
428
@@ -559,6 +561,7 @@ pub fn type_from_definition(
559
561
ty : trait_func. ty . clone ( ) ,
560
562
} )
561
563
}
564
+ FunctionValueDefinition :: PublicDeclaration ( _) => Some ( Type :: Expr . into ( ) ) ,
562
565
}
563
566
} else {
564
567
assert ! (
@@ -771,6 +774,8 @@ impl Symbol {
771
774
pub enum SymbolKind {
772
775
/// Fixed, witness or intermediate polynomial
773
776
Poly ( PolynomialType ) ,
777
+ /// Public declaration
778
+ Public ( ) ,
774
779
/// Other symbol, depends on the type.
775
780
/// Examples include functions not of the type "int -> fe".
776
781
Other ( ) ,
@@ -784,6 +789,7 @@ pub enum FunctionValueDefinition {
784
789
TypeConstructor ( Arc < EnumDeclaration > , EnumVariant ) ,
785
790
TraitDeclaration ( TraitDeclaration ) ,
786
791
TraitFunction ( Arc < TraitDeclaration > , NamedType ) ,
792
+ PublicDeclaration ( PublicDeclaration ) ,
787
793
}
788
794
789
795
impl Children < Expression > for FunctionValueDefinition {
@@ -799,6 +805,7 @@ impl Children<Expression> for FunctionValueDefinition {
799
805
FunctionValueDefinition :: TypeConstructor ( _, variant) => variant. children ( ) ,
800
806
FunctionValueDefinition :: TraitDeclaration ( trait_decl) => trait_decl. children ( ) ,
801
807
FunctionValueDefinition :: TraitFunction ( _, trait_func) => trait_func. children ( ) ,
808
+ FunctionValueDefinition :: PublicDeclaration ( pub_decl) => pub_decl. children ( ) ,
802
809
}
803
810
}
804
811
@@ -814,6 +821,7 @@ impl Children<Expression> for FunctionValueDefinition {
814
821
FunctionValueDefinition :: TypeConstructor ( _, variant) => variant. children_mut ( ) ,
815
822
FunctionValueDefinition :: TraitDeclaration ( trait_decl) => trait_decl. children_mut ( ) ,
816
823
FunctionValueDefinition :: TraitFunction ( _, trait_func) => trait_func. children_mut ( ) ,
824
+ FunctionValueDefinition :: PublicDeclaration ( pub_decl) => pub_decl. children_mut ( ) ,
817
825
}
818
826
}
819
827
}
@@ -836,26 +844,80 @@ impl Children<Expression> for NamedType {
836
844
}
837
845
}
838
846
839
- #[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , Hash ) ]
847
+ #[ derive( Debug , Clone , Serialize , Deserialize , JsonSchema , Hash , PartialEq , Eq ) ]
840
848
pub struct PublicDeclaration {
841
849
pub id : u64 ,
842
850
pub source : SourceRef ,
843
851
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 ,
848
855
}
849
856
850
857
impl 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.
851
888
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." ) ,
855
908
}
856
909
}
857
910
}
858
911
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
+
859
921
#[ derive(
860
922
Debug , PartialEq , Eq , PartialOrd , Ord , Clone , Serialize , Deserialize , JsonSchema , Hash ,
861
923
) ]
0 commit comments