Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions compiler/noirc_evaluator/src/ssa/interpreter/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,28 @@ pub enum InternalError {
InvalidInputSize { expected_size: usize, size: usize },
#[error("Constant `{constant}` does not fit in type `{typ}`")]
ConstantDoesNotFitInType { constant: FieldElement, typ: NumericType },
#[error(
"The value assigned to `{value_id}` expects a type `{expected_type}` but it got assigned a value with type `{actual_type}` "
)]
ValueTypeDoesNotMatchReturnType {
value_id: ValueId,
expected_type: String,
actual_type: String,
},
#[error(
"Expected result type to be `{expected_type} but it was `{actual_type}` in {instruction}"
)]
UnexpectedResultType {
expected_type: &'static str,
actual_type: String,
instruction: &'static str,
},
#[error(
"Expected result length to be `{expected_length} but it was `{actual_length}` in {instruction}"
)]
UnexpectedResultLength {
expected_length: usize,
actual_length: usize,
instruction: &'static str,
},
}
91 changes: 69 additions & 22 deletions compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
Ok(Vec::new())
} else {
// Static assert can either have 2 arguments, in which case the second one is a string,
// or it can have more arguments in case fmtstring or some other non-string value is passed.

Check warning on line 59 in compiler/noirc_evaluator/src/ssa/interpreter/intrinsics.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (fmtstring)
// For simplicity, we won't build the dynamic message here.
let message = if args.len() == 2 {
self.lookup_string(args[1], "static_assert")?
Expand Down Expand Up @@ -124,7 +124,7 @@
acvm::blackbox_solver::aes128_encrypt(&inputs, iv_array, key_array)
.map_err(Self::convert_error)?;
let result = result.iter().map(|v| (*v as u128).into());
let result = Value::array_from_iter(result, NumericType::NativeField)?;
let result = Value::array_from_iter(result, NumericType::unsigned(8))?;
Ok(vec![result])
}
acvm::acir::BlackBoxFunc::AND => {
Expand All @@ -148,7 +148,7 @@
let result =
acvm::blackbox_solver::blake2s(&inputs).map_err(Self::convert_error)?;
let result = result.iter().map(|e| (*e as u128).into());
let result = Value::array_from_iter(result, NumericType::NativeField)?;
let result = Value::array_from_iter(result, NumericType::unsigned(8))?;
Ok(vec![result])
}
acvm::acir::BlackBoxFunc::Blake3 => {
Expand All @@ -157,7 +157,7 @@
let results =
acvm::blackbox_solver::blake3(&inputs).map_err(Self::convert_error)?;
let results = results.iter().map(|e| (*e as u128).into());
let results = Value::array_from_iter(results, NumericType::NativeField)?;
let results = Value::array_from_iter(results, NumericType::unsigned(8))?;
Ok(vec![results])
}
acvm::acir::BlackBoxFunc::EcdsaSecp256k1 => {
Expand Down Expand Up @@ -286,8 +286,8 @@
}
let solver = bn254_blackbox_solver::Bn254BlackBoxSolver(false);
let result = solver.multi_scalar_mul(&points, &scalars_lo, &scalars_hi);
let (a, b, c) = result.map_err(Self::convert_error)?;
let result = Value::array_from_iter([a, b, c], NumericType::NativeField)?;
let (x, y, is_infinite) = result.map_err(Self::convert_error)?;
let result = new_embedded_curve_point(x, y, is_infinite)?;
Ok(vec![result])
}
acvm::acir::BlackBoxFunc::Keccakf1600 => {
Expand Down Expand Up @@ -327,8 +327,8 @@
);
let result =
solver.ec_add(&lhs.0, &lhs.1, &lhs.2.into(), &rhs.0, &rhs.1, &rhs.2.into());
let (x, y, inf) = result.map_err(Self::convert_error)?;
let result = Value::array_from_iter([x, y, inf], NumericType::NativeField)?;
let (x, y, is_infinite) = result.map_err(Self::convert_error)?;
let result = new_embedded_curve_point(x, y, is_infinite)?;
Ok(vec![result])
}
acvm::acir::BlackBoxFunc::BigIntAdd
Expand Down Expand Up @@ -374,7 +374,7 @@
})?;
acvm::blackbox_solver::sha256_compression(&mut state, &inputs);
let result = state.iter().map(|e| (*e as u128).into());
let result = Value::array_from_iter(result, NumericType::NativeField)?;
let result = Value::array_from_iter(result, NumericType::unsigned(32))?;
Ok(vec![result])
}
},
Expand All @@ -385,32 +385,61 @@
}
Intrinsic::DerivePedersenGenerators => {
check_argument_count(args, 2, intrinsic)?;

let inputs =
self.lookup_bytes(args[0], "call DerivePedersenGenerators BlackBox")?;
let index = self.lookup_u32(args[1], "call DerivePedersenGenerators BlackBox")?;
let generators = derive_generators(&inputs, inputs.len() as u32, index);

// The definition is:
//
// ```noir
// fn __derive_generators<let N: u32, let M: u32>(
// domain_separator_bytes: [u8; M],
// starting_index: u32,
// ) -> [EmbeddedCurvePoint; N] {}
// ```
//
// We need to get N from the return type.
if results.len() != 1 {
return Err(InterpreterError::Internal(
InternalError::UnexpectedResultLength {
actual_length: results.len(),
expected_length: 1,
instruction: "call DerivePedersenGenerators BlackBox",
},
));
}

let result_type = self.dfg().type_of_value(results[0]);
let Type::Array(_, n) = result_type else {
return Err(InterpreterError::Internal(InternalError::UnexpectedResultType {
actual_type: result_type.to_string(),
expected_type: "array",
instruction: "call DerivePedersenGenerators BlackBox",
}));
};

let generators = derive_generators(&inputs, n, index);
let mut result = Vec::with_capacity(inputs.len());
for generator in generators.iter() {
let x_big: BigUint = generator.x.into();
let x = FieldElement::from_le_bytes_reduce(&x_big.to_bytes_le());
let y_big: BigUint = generator.y.into();
let y = FieldElement::from_le_bytes_reduce(&y_big.to_bytes_le());
let generator_slice = Value::array_from_iter(
[x, y, generator.infinity.into()],
NumericType::NativeField,
)?;
result.push(generator_slice);
result.push(Value::from_constant(x, NumericType::NativeField)?);
result.push(Value::from_constant(y, NumericType::NativeField)?);
result.push(Value::from_constant(
generator.infinity.into(),
NumericType::bool(),
)?);
}
let results = Value::array(
result,
vec![Type::Array(
std::sync::Arc::new(vec![
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::NativeField),
]),
3,
)],
vec![
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::bool()),
],
);
Ok(vec![results])
}
Expand Down Expand Up @@ -643,3 +672,21 @@
}))
}
}

fn new_embedded_curve_point(
x: FieldElement,
y: FieldElement,
is_infinite: FieldElement,
) -> IResult<Value> {
let x = Value::from_constant(x, NumericType::NativeField)?;
let y = Value::from_constant(y, NumericType::NativeField)?;
let is_infinite = Value::from_constant(is_infinite, NumericType::bool())?;
Ok(Value::array(
vec![x, y, is_infinite],
vec![
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::NativeField),
Type::Numeric(NumericType::bool()),
],
))
}
Loading
Loading