Skip to content

Conversation

@jialinli98
Copy link
Contributor

@jialinli98 jialinli98 commented Jun 30, 2025

Description

Add WNAF validation for ScalarField when N ≥ 64 to ensure the reconstructed value fits within a Field element.

Problem*

Resolves #76

Summary*

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

BEGIN_COMMIT_OVERRIDE
fix: add ScalarField WNAF validation for N >= 64 (#80)
END_COMMIT_OVERRIDE

@jialinli98 jialinli98 requested a review from TomAFrench June 30, 2025 07:03
@jialinli98 jialinli98 changed the title chore: remove N < 64 check in ScalarField WNAF validation chore: add ScalarField WNAF validation for N >= 64 Jul 7, 2025
@jialinli98 jialinli98 requested a review from kashbrti July 10, 2025 06:28
Comment on lines 155 to 157
if N >= 64 {
unsafe { compare_scalar_field_to_bignum(result) };
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unconstrained so it's not enforcing anything.

fn from(input: Field) -> Self {
let result = unsafe { get_wnaf_slices(input) };

if std::runtime::is_unconstrained() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reversed check here.

let scalar_field2 = scalar_field.into();
assert(val as Field == scalar_field2);
}
#[test]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some tests for compare_scalar_field_to_bignum

@jialinli98 jialinli98 force-pushed the jl/unconstrained_nibbles branch from b3cf8dc to 8fcec0c Compare July 23, 2025 17:13
@jialinli98 jialinli98 requested a review from TomAFrench July 23, 2025 18:35
{
x.validate_in_field();
let mut (slices, skew): ([u8; N], bool) = unsafe { get_wnaf_slices2(x) };
//let mut (slices, skew): ([u8; N], bool) = unsafe { get_wnaf_slices2(x) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't leave commented code.

}
#[test]
fn test_get_modulus_slices() {
let modulus_slices: [u8; 64] = unsafe { get_modulus_slices::<64>() };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are throwing "Unnecessary unsafe block" warnings. Ideally this library should not be throwing warnings but we should avoid adding more.

Does your editor show these with yellow squiggly lines?

should_continue = false;
} else if result.base4_slices[i] > expected_slices[i] {
// Found a strictly larger number, this is invalid
assert(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be panic with a useful error message stating that the scalar field is larger than the field modulus, etc.

],
);
}
#[test(should_fail)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can then make these tests more effective by ensuring that we're failing the expected error message.

15, 10, 12, 9, 15, 8, 0, 0, 0, 0, 0, 0,
];
let mut result: ScalarField<65> = ScalarField { base4_slices: [0; 65], skew: true };
for i in 0..64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

64?

Comment on lines 383 to 391
let modulus_slices: [u8; 64] = [
9, 8, 3, 2, 2, 7, 3, 9, 7, 0, 9, 8, 14, 0, 1, 4, 13, 12, 2, 8, 2, 2, 13, 11, 4, 0, 12,
0, 10, 12, 2, 14, 9, 4, 1, 9, 15, 4, 2, 4, 3, 12, 13, 12, 11, 8, 4, 8, 10, 1, 15, 0, 15,
10, 12, 9, 15, 8, 0, 0, 0, 0, 0, 0,
];
let mut result: ScalarField<64> = ScalarField { base4_slices: [0; 64], skew: true };
for i in 0..64 {
result.base4_slices[i] = modulus_slices[i];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not write ScalarField {base4_slices: modulus_slices, skew: true }?

@jialinli98 jialinli98 requested a review from TomAFrench August 7, 2025 02:52
@TomAFrench TomAFrench merged commit c13415f into main Aug 19, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from 👀 To Triage to ✅ Done in Noir Libraries Aug 19, 2025
@noirwhal noirwhal mentioned this pull request Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

ScalarField::From<Field> is underconstrained

3 participants