Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove noir-lang/ec dependency #12507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ authors = [""]
compiler_version = ">=0.18.0"

[dependencies]
ec = { tag = "v0.1.2", git = "https://github.com/noir-lang/ec" }
sha256 = { tag = "v0.1.2", git = "https://github.com/noir-lang/sha256" }
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::{
hash::poseidon2_hash_with_separator,
public_keys::{IvpkM, NpkM, OvpkM, PublicKeys, ToPoint, TpkM},
traits::{Deserialize, Empty, FromField, Packable, Serialize, ToField},
utils::field::{pow, sqrt},
};

// We do below because `use crate::point::Point;` does not work
use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

use crate::public_keys::AddressPoint;
use ec::{pow, sqrt};
use std::{
embedded_curve_ops::{EmbeddedCurveScalar, fixed_base_scalar_mul as derive_public_key},
ops::Add,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@ pub fn min(f1: Field, f2: Field) -> Field {
}
}

global C1: u32 = 28;
global C3: Field = 40770029410420498293352137776570907027550720424234931066070132305055;
global C5: Field = 19103219067921713944291392827692070036145651957329286315305642004821462161904;

pub(crate) fn pow(x: Field, y: Field) -> Field {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

let mut r = 1 as Field;
let b: [u1; 254] = y.to_le_bits();

for i in 0..254 {
r *= r;
r *= (b[254 - 1 - i] as Field) * x + (1 - b[254 - 1 - i] as Field);
}

r
}

// Tonelli-Shanks algorithm for computing the square root of a Field element.
// Requires C1 = max{c: 2^c divides (p-1)}, where p is the order of Field
// as well as C3 = (C2 - 1)/2, where C2 = (p-1)/(2^c1),
// and C5 = ZETA^C2, where ZETA is a non-square element of Field.
// These are pre-computed above as globals.
pub(crate) fn sqrt(x: Field) -> Field {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add a fuzz test for the function?
we can just pick a field element x, square them, and check that the sqrt is +-x

Copy link
Member Author

@TomAFrench TomAFrench Mar 6, 2025

Choose a reason for hiding this comment

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

I think this can wait until we choose a final home for these functions. We're not losing any test coverage with this PR (pow and sqrt are completely untested inside of noir-lang/ec).

Copy link
Contributor

Choose a reason for hiding this comment

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

as these are functions on the native field type, shouldn't they naturally be in stdlib?

let mut z = pow(x, C3);
let mut t = z * z * x;
z *= x;
let mut b = t;
let mut c = C5;

for i in 0..(C1 - 1) {
for _j in 1..(C1 - i - 1) {
b *= b;
}

z *= if b == 1 { 1 } else { c };

c *= c;

t *= if b == 1 { 1 } else { c };

b = t;
}

z
}

#[test]
unconstrained fn bytes_field_test() {
// Tests correctness of field_from_bytes_32_trunc against existing methods
Expand Down