@@ -332,6 +332,15 @@ impl<B: Backend> CircuitWriter<B> {
332
332
unreachable ! ( "generic array should have been resolved" )
333
333
}
334
334
TyKind :: String ( _) => todo ! ( "String type is not supported for constraints" ) ,
335
+ TyKind :: Tuple ( types) => {
336
+ let mut offset = 0 ;
337
+ for ty in types {
338
+ let size = self . size_of ( ty) ;
339
+ let slice = & input[ offset..( offset + size) ] ;
340
+ self . constrain_inputs_to_main ( slice, input_typ, span) ?;
341
+ offset += size;
342
+ }
343
+ }
335
344
} ;
336
345
Ok ( ( ) )
337
346
}
@@ -726,11 +735,11 @@ impl<B: Backend> CircuitWriter<B> {
726
735
Ok ( Some ( res) )
727
736
}
728
737
729
- ExprKind :: ArrayAccess { array , idx } => {
730
- // retrieve var of array
738
+ ExprKind :: ArrayOrTupleAccess { container , idx } => {
739
+ // retrieve var of container
731
740
let var = self
732
- . compute_expr ( fn_env, array ) ?
733
- . expect ( "array access on non-array " ) ;
741
+ . compute_expr ( fn_env, container ) ?
742
+ . expect ( "container access on non-container " ) ;
734
743
735
744
// compute the index
736
745
let idx_var = self
@@ -742,32 +751,41 @@ impl<B: Backend> CircuitWriter<B> {
742
751
let idx: BigUint = idx. into ( ) ;
743
752
let idx: usize = idx. try_into ( ) . unwrap ( ) ;
744
753
745
- // retrieve the type of the elements in the array
746
- let array_typ = self . expr_type ( array) . expect ( "cannot find type of array" ) ;
754
+ // retrieve the type of the elements in the container
755
+ let container_typ = self
756
+ . expr_type ( container)
757
+ . expect ( "cannot find type of container" ) ;
747
758
748
- let elem_type = match array_typ {
759
+ // actual starting index for narrowing the var depends on the cotainer
760
+ // for arrays it is just idx * elem_size as all elements are of same size
761
+ // while for tuples we have to sum the sizes of all types up to that index
762
+ let ( start, len) = match container_typ {
749
763
TyKind :: Array ( ty, array_len) => {
750
764
if idx >= ( * array_len as usize ) {
751
765
return Err ( self . error (
752
766
ErrorKind :: ArrayIndexOutOfBounds ( idx, * array_len as usize - 1 ) ,
753
767
expr. span ,
754
768
) ) ;
755
769
}
756
- ty
770
+ let len = self . size_of ( ty) ;
771
+ let start = idx * self . size_of ( ty) ;
772
+ ( start, len)
773
+ }
774
+
775
+ TyKind :: Tuple ( typs) => {
776
+ let mut start = 0 ;
777
+ for i in 0 ..idx {
778
+ start += self . size_of ( & typs[ i] ) ;
779
+ }
780
+ ( start, self . size_of ( & typs[ idx] ) )
757
781
}
758
782
_ => Err ( Error :: new (
759
783
"compute-expr" ,
760
- ErrorKind :: UnexpectedError ( "expected array " ) ,
784
+ ErrorKind :: UnexpectedError ( "expected container " ) ,
761
785
expr. span ,
762
786
) ) ?,
763
787
} ;
764
788
765
- // compute the size of each element in the array
766
- let len = self . size_of ( elem_type) ;
767
-
768
- // compute the real index
769
- let start = idx * len;
770
-
771
789
// out-of-bound checks
772
790
if start >= var. len ( ) || start + len > var. len ( ) {
773
791
return Err ( self . error (
@@ -830,6 +848,21 @@ impl<B: Backend> CircuitWriter<B> {
830
848
let var = VarOrRef :: Var ( Var :: new ( cvars, expr. span ) ) ;
831
849
Ok ( Some ( var) )
832
850
}
851
+ // exact copy of Array Declaration there is nothing really different at when looking it from a expression level
852
+ // as both of them are just `Vec<Expr>`
853
+ ExprKind :: TupleDeclaration ( items) => {
854
+ let mut cvars = vec ! [ ] ;
855
+
856
+ for item in items {
857
+ let var = self . compute_expr ( fn_env, item) ?. unwrap ( ) ;
858
+ let to_extend = var. value ( self , fn_env) . cvars . clone ( ) ;
859
+ cvars. extend ( to_extend) ;
860
+ }
861
+
862
+ let var = VarOrRef :: Var ( Var :: new ( cvars, expr. span ) ) ;
863
+
864
+ Ok ( Some ( var) )
865
+ }
833
866
}
834
867
}
835
868
0 commit comments