From 2eccb0726d221bfc66a82c4886d900a37c896a6b Mon Sep 17 00:00:00 2001 From: guipublic Date: Wed, 26 Mar 2025 14:50:59 +0000 Subject: [PATCH] proper use of add_unsafe in unconstrained --- noir_stdlib/src/embedded_curve_ops.nr | 8 +++++++- .../execution_success/multi_scalar_mul/src/main.nr | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/noir_stdlib/src/embedded_curve_ops.nr b/noir_stdlib/src/embedded_curve_ops.nr index 8d343e89fb1..c623b760754 100644 --- a/noir_stdlib/src/embedded_curve_ops.nr +++ b/noir_stdlib/src/embedded_curve_ops.nr @@ -142,7 +142,13 @@ pub fn embedded_curve_add( ) -> EmbeddedCurvePoint { // docs:end:embedded_curve_add if crate::runtime::is_unconstrained() { - embedded_curve_add_unsafe(point1, point2) + if point1.is_infinite { + point2 + } else if point2.is_infinite { + point1 + } else { + embedded_curve_add_unsafe(point1, point2) + } } else { // In a constrained context we need to do some black magic in order to satisfy the backend's // expectations about the inputs to an `embedded_curve_add` opcode. diff --git a/test_programs/execution_success/multi_scalar_mul/src/main.nr b/test_programs/execution_success/multi_scalar_mul/src/main.nr index b191fb7aa1e..3eb237fc82a 100644 --- a/test_programs/execution_success/multi_scalar_mul/src/main.nr +++ b/test_programs/execution_success/multi_scalar_mul/src/main.nr @@ -1,6 +1,6 @@ // This test provides a basic implementation of a MSM in Noir, that allows us to check // performance improvements and regressions. -use std::embedded_curve_ops::embedded_curve_add_unsafe; +use std::embedded_curve_ops::embedded_curve_add; use std::embedded_curve_ops::EmbeddedCurvePoint; // `main` must be marked unconstrained as the function uses `break` internally @@ -40,14 +40,14 @@ unconstrained fn double_then_add_msm( // traversing from second MSB to LSB for j in (index_of_msb + 1)..(254) { // Double - res = embedded_curve_add_unsafe(res, res); + res = embedded_curve_add(res, res); // Add if full_scalar_bits[j] == 1 { - res = embedded_curve_add_unsafe(res, temp); + res = embedded_curve_add(res, temp); } } - acc = embedded_curve_add_unsafe(acc, res); + acc = embedded_curve_add(acc, res); } acc }