def handle_homomorphic_builtin_function_call(self, ast: FunctionCallExpr, func: BuiltinFunction): # First - same as non-homomorphic - check that argument types conform to op signature if not func.is_eq(): for arg, t in zip(ast.args, func.input_types()): if not arg.instanceof_data_type(t): raise TypeMismatchException(t, arg.annotated_type.type_name, arg) homomorphic_func = func.select_homomorphic_overload( ast.args, ast.analysis) if homomorphic_func is None: raise TypeException( f'Operation \'{func.op}\' requires all arguments to be accessible, ' f'i.e. @all or provably equal to @me', ast) # We could perform homomorphic operations on-chain by using some Solidity arbitrary precision math library. # For now, keep it simple and evaluate homomorphic operations in Python and check the result in the circuit. func.is_private = True ast.annotated_type = homomorphic_func.output_type() func.homomorphism = ast.annotated_type.homomorphism expected_arg_types = homomorphic_func.input_types() # Check that the argument types are correct ast.args[:] = map(lambda arg, arg_pt: self.get_rhs(arg, arg_pt), ast.args, expected_arg_types)
def handle_builtin_function_call(self, ast: FunctionCallExpr, func: BuiltinFunction): # handle special cases if func.is_ite(): cond_t = ast.args[0].annotated_type # Ensure that condition is boolean if not cond_t.type_name.implicitly_convertible_to( TypeName.bool_type()): raise TypeMismatchException(TypeName.bool_type(), cond_t.type_name, ast.args[0]) res_t = ast.args[1].annotated_type.type_name.combined_type( ast.args[2].annotated_type.type_name, True) if cond_t.is_private(): # Everything is turned private func.is_private = True a = res_t.annotate(Expression.me_expr()) else: p = ast.args[1].annotated_type.combined_privacy( ast.analysis, ast.args[2].annotated_type) a = res_t.annotate(p) ast.args[1] = self.get_rhs(ast.args[1], a) ast.args[2] = self.get_rhs(ast.args[2], a) ast.annotated_type = a return elif func.is_parenthesis(): ast.annotated_type = ast.args[0].annotated_type return # Check that argument types conform to op signature parameter_types = func.input_types() if not func.is_eq(): for arg, t in zip(ast.args, parameter_types): if not arg.instanceof_data_type(t): raise TypeMismatchException(t, arg.annotated_type.type_name, arg) t1 = ast.args[0].annotated_type.type_name t2 = None if len( ast.args) == 1 else ast.args[1].annotated_type.type_name if len(ast.args) == 1: arg_t = 'lit' if ast.args[ 0].annotated_type.type_name.is_literal else t1 else: assert len(ast.args) == 2 is_eq_with_tuples = func.is_eq() and isinstance(t1, TupleType) arg_t = t1.combined_type(t2, convert_literals=is_eq_with_tuples) # Infer argument and output types if arg_t == 'lit': res = func.op_func( *[arg.annotated_type.type_name.value for arg in ast.args]) if isinstance(res, bool): assert func.output_type() == TypeName.bool_type() out_t = BooleanLiteralType(res) else: assert func.output_type() == TypeName.number_type() out_t = NumberLiteralType(res) if func.is_eq(): arg_t = t1.to_abstract_type().combined_type( t2.to_abstract_type(), True) elif func.output_type() == TypeName.bool_type(): out_t = TypeName.bool_type() else: out_t = arg_t assert arg_t is not None and (arg_t != 'lit' or not func.is_eq()) private_args = any(map(self.has_private_type, ast.args)) if private_args: assert arg_t != 'lit' if func.can_be_private(): if func.is_shiftop(): if not ast.args[1].annotated_type.type_name.is_literal: raise TypeException( 'Private shift expressions must use a constant (literal) shift amount', ast.args[1]) if ast.args[1].annotated_type.type_name.value < 0: raise TypeException('Cannot shift by negative amount', ast.args[1]) if func.is_bitop() or func.is_shiftop(): for arg in ast.args: if arg.annotated_type.type_name.elem_bitwidth == 256: raise TypeException( 'Private bitwise and shift operations are only supported for integer types < 256 bit, ' 'please use a smaller type', arg) if func.is_arithmetic(): for a in ast.args: if a.annotated_type.type_name.elem_bitwidth == 256: issue_compiler_warning( func, 'Possible field prime overflow', 'Private arithmetic 256bit operations overflow at FIELD_PRIME.\n' 'If you need correct overflow behavior, use a smaller integer type.' ) break elif func.is_comp(): for a in ast.args: if a.annotated_type.type_name.elem_bitwidth == 256: issue_compiler_warning( func, 'Possible private comparison failure', 'Private 256bit comparison operations will fail for values >= 2^252.\n' 'If you cannot guarantee that the value stays in range, you must use ' 'a smaller integer type to ensure correctness.' ) break func.is_private = True p = Expression.me_expr() else: raise TypeException( f'Operation \'{func.op}\' does not support private operands', ast) else: p = None if arg_t != 'lit': # Add implicit casts for arguments arg_pt = arg_t.annotate(p) if func.is_shiftop() and p is not None: ast.args[0] = self.get_rhs(ast.args[0], arg_pt) else: ast.args[:] = map( lambda argument: self.get_rhs(argument, arg_pt), ast.args) ast.annotated_type = out_t.annotate(p)