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

Cleanup/moving primitive roots to consts #38

Merged
merged 53 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
55a5bf0
adding bare changes for batch verification
Jan 4, 2025
259f6b0
adding some comments
anupsv Jan 4, 2025
4c4bdd0
adding more comments
anupsv Jan 4, 2025
4ecd566
moving back to sha2
Jan 6, 2025
cb75b3b
removing a test which is no longer needed. Removing methods no longer…
Jan 6, 2025
42bc913
updates to method visibility, updating tests
Jan 7, 2025
f3dd7f4
merging main
Jan 7, 2025
02fa7ca
fmt fixes
anupsv Jan 7, 2025
0807ce0
clean up
anupsv Jan 7, 2025
da5e5ad
cleanup, optimization, inline docs
anupsv Jan 7, 2025
b58174b
removing unwanted const
anupsv Jan 7, 2025
c1c2f70
more docs and cleanup
anupsv Jan 7, 2025
fa02398
formatting
anupsv Jan 7, 2025
914e059
removing unwanted comments
anupsv Jan 7, 2025
c74accf
merging main
anupsv Jan 8, 2025
f054c19
cargo fmt and clippy
anupsv Jan 8, 2025
90b3d13
adding test for point at infinity
anupsv Jan 8, 2025
486f7da
cleaner errors, cleanup
anupsv Jan 8, 2025
32716fe
adding another test case
anupsv Jan 8, 2025
73bc809
removing unwanted errors
anupsv Jan 8, 2025
618e098
adding fixes per comments
anupsv Jan 8, 2025
f7eb705
adding 4844 spec references
anupsv Jan 8, 2025
61e1744
comment fixes
anupsv Jan 9, 2025
1c2a79d
formatting, adding index out of bound check, removing print statement
anupsv Jan 10, 2025
baf5a44
removing unwanted test, adding test for evaluate_polynomial_in_evalua…
anupsv Jan 10, 2025
aa1ded9
moving test to bottom section
anupsv Jan 10, 2025
86ceab3
Update src/polynomial.rs
anupsv Jan 10, 2025
d67adec
Update src/kzg.rs
anupsv Jan 10, 2025
02194ba
Update src/kzg.rs
anupsv Jan 10, 2025
bfd2fac
Update src/kzg.rs
anupsv Jan 10, 2025
1c9bcac
Update src/helpers.rs
anupsv Jan 10, 2025
f6c07eb
updating deps, and toolchain to 1.84
anupsv Jan 10, 2025
7531098
removing errors test, no longer useful
anupsv Jan 10, 2025
f9bb219
adding to_byte_array arg explanation
anupsv Jan 10, 2025
5f9fa77
fmt fixes
anupsv Jan 10, 2025
cdeae7c
fmt and clippy fixes
anupsv Jan 10, 2025
0624dc6
fixing function names and fmt
anupsv Jan 10, 2025
c6f0bdd
clippy fixes
anupsv Jan 10, 2025
42c328b
moving primitive root to consts, adding helper function and changing …
anupsv Jan 10, 2025
3633bc5
merge main and cleanup
anupsv Jan 12, 2025
61d719a
Merge branch 'master' into cleanup/moving-primitive-roots-to-consts
anupsv Jan 14, 2025
7122430
removing function
anupsv Jan 14, 2025
ab22fa2
wrong branch updated
anupsv Jan 14, 2025
955dea8
removing is_zero
anupsv Jan 14, 2025
d1621a0
comment updates
anupsv Jan 14, 2025
e40ec08
using macro for roots consts
anupsv Jan 14, 2025
3d644d3
merge master
anupsv Jan 15, 2025
b3d7c1e
adding explanation to PRIMITIVE_ROOTS_OF_UNITY
anupsv Jan 15, 2025
641e7b0
Update src/kzg.rs
anupsv Jan 16, 2025
a327933
Update src/helpers.rs
anupsv Jan 16, 2025
a3d7767
comment change, renames and doc fixes
anupsv Jan 16, 2025
d43d004
merge master
anupsv Jan 23, 2025
f4b224f
cargo fmt
anupsv Jan 23, 2025
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
172 changes: 172 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,175 @@ pub enum Endianness {
Big,
Little,
}

pub const PRIMITIVE_ROOTS_OF_UNITY: [[u64; 4]; 29] = [
samlaf marked this conversation as resolved.
Show resolved Hide resolved
[1, 0, 0, 0],
[
4891460686036598784,
2896914383306846353,
13281191951274694749,
3486998266802970665,
],
[
2527087222397613622,
6695204439272418284,
327476452638867716,
3486998266802970665,
],
[
10704402425919135360,
5936434144913852938,
17049394741569908534,
3112970176132370210,
],
[
12760115869041182219,
11055769604441701272,
2048662040769024140,
2380201477744082766,
],
[
4534364320199676624,
11247974383505828637,
10842451344304943785,
704024743550620626,
],
[
15822547390047388218,
5090979784920605886,
14000806642677820914,
1447929602671906762,
],
[
11317061033624672273,
2146903538426596473,
16094987232344622048,
1650355948196268433,
],
[
14453002906517207670,
7023718024139043376,
17331575720852783024,
554159777355432964,
],
[
9888356262503077838,
16730919078578367861,
18428778117559158389,
1089287402140675931,
],
[
1295231164603062546,
4159771074203146155,
16987221740319371747,
503587051935343648,
],
[
14990892138116034274,
13312970452735684810,
12971070440946883953,
178513978866336811,
],
[
2037726488899540026,
8570747871144680377,
134492760481370281,
662545464149659919,
],
[
5378594887492817680,
15212020320748318070,
8420921063925371,
31432055529778786,
],
[
12513816448973172357,
13018005335927991014,
2255830314307594408,
3284907887559304321,
],
[
12192467936159919355,
155983510951534601,
103989586739080144,
3250374553247014033,
],
[
15087775233214268343,
1641667745675509373,
7249316272930938111,
67187630906928453,
],
[
2841305553363228901,
365614731812479246,
14082910820695433715,
2015411323404765442,
],
[
9567545175896342752,
14227021851996246461,
6629710949923774029,
1863853281588889281,
],
[
6637559829441244349,
3481942500310468410,
5873418851262023957,
2477233939078171218,
],
[
5999407858879526999,
12421321615888692383,
2832916697199010563,
2743358069261914211,
],
[
12882679250960823587,
6410035656685935843,
4765394507826527372,
2156530982351265050,
],
[
12064543440463545470,
17012944649580374971,
14817070057475047957,
1934629495612430759,
],
[
13064173989021140381,
6233199863045921055,
7745012067293438265,
148898490380240717,
],
[
2028293168271393883,
1467625613274850966,
9596654050255193119,
909634523159679186,
],
[
17063381693978129178,
8873981638024286830,
11644904980285164756,
3058875137825915375,
],
[
2051879287397454354,
6943234193189566894,
77669474422529354,
1182008650693047218,
],
[
237279305830403140,
13007017166795668898,
6856003240715543396,
331831521350773736,
],
[
11229192882073836016,
4624371214017703636,
63235024940837564,
3043318377369730693,
],
];
1 change: 0 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub enum PolynomialError {
/// Error related to Fast Fourier Transform (FFT) operations with a descriptive message.
#[error("FFT error: {0}")]
FFTError(String),

/// A generic error with a descriptive message.
#[error("generic error: {0}")]
GenericError(String),
Expand Down
45 changes: 43 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::cmp;
use crate::{
arith,
consts::{
Endianness, BYTES_PER_FIELD_ELEMENT, KZG_ENDIANNESS, SIZE_OF_G1_AFFINE_COMPRESSED,
SIZE_OF_G2_AFFINE_COMPRESSED,
Endianness, BYTES_PER_FIELD_ELEMENT, KZG_ENDIANNESS, PRIMITIVE_ROOTS_OF_UNITY,
SIZE_OF_G1_AFFINE_COMPRESSED, SIZE_OF_G2_AFFINE_COMPRESSED,
},
errors::KzgError,
traits::ReadPointFromBytes,
Expand Down Expand Up @@ -94,6 +94,10 @@ pub fn remove_empty_byte_from_padded_bytes_unchecked(data: &[u8]) -> Vec<u8> {
valid_data
}

pub fn is_zero(fr: Fr) -> bool {
fr.0 .0.iter().fold(0, |acc, &x| acc | x) == 0
}

samlaf marked this conversation as resolved.
Show resolved Hide resolved
pub fn set_bytes_canonical(data: &[u8]) -> Fr {
Fr::from_be_bytes_mod_order(data)
}
Expand Down Expand Up @@ -475,3 +479,40 @@ pub fn g1_lincomb(points: &[G1Affine], scalars: &[Fr]) -> Result<G1Affine, KzgEr
// This is typically needed as most protocols expect points in affine form
Ok(lincomb.into_affine())
}

/// Retrieves and converts a primitive root of unity to a field element
///
/// # Arguments
/// * `index` - Index of the primitive root to retrieve from PRIMITIVE_ROOTS_OF_UNITY array
///
/// # Returns
/// * `Result<Fr, KzgError>` - Field element representation of the primitive root if successful,
/// or KzgError if index is invalid or conversion fails
///
/// # Errors
/// - Returns KzgError::GenericError if:
/// - Index is out of bounds for PRIMITIVE_ROOTS_OF_UNITY array
/// - BigInt conversion to field element fails
///
/// # Details
/// - Looks up a primitive root of unity from a predefined array using the given index
/// - Converts the BigInt representation to an Fr field element
/// - Commonly used in FFT and polynomial operations requiring roots of unity
///
/// # Example
/// ```
/// use rust_kzg_bn254::helpers::get_and_convert_primitive_root_to_fr;
/// let root = get_and_convert_primitive_root_to_fr(0); // Gets first primitive root
/// ```
pub fn get_and_convert_primitive_root_to_fr(index: usize) -> Result<Fr, KzgError> {
let found_root_of_unity_bigint = PRIMITIVE_ROOTS_OF_UNITY
.get(index)
.ok_or_else(|| KzgError::GenericError("Root of unity not found".to_string()))?;
samlaf marked this conversation as resolved.
Show resolved Hide resolved

let found_root_of_unity = Fr::from_bigint(BigInt::new(*found_root_of_unity_bigint))
.ok_or_else(|| {
KzgError::GenericError("Failed to convert root of unity from bigint to Fr".to_string())
})?;

Ok(found_root_of_unity)
}
64 changes: 7 additions & 57 deletions src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup, VariableBaseMSM};
use ark_ff::{BigInteger, Field, PrimeField};
use ark_poly::{EvaluationDomain, GeneralEvaluationDomain};
use ark_serialize::{CanonicalSerialize, Read};
use ark_std::{iterable::Iterable, ops::Div, str::FromStr, One, Zero};
use ark_std::{iterable::Iterable, ops::Div, One, Zero};
use crossbeam_channel::{bounded, Sender};
use num_traits::ToPrimitive;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
Expand Down Expand Up @@ -193,16 +193,12 @@ impl KZG {
));
}

// Get the primitive roots of unity
let primitive_roots_of_unity = Self::get_primitive_roots_of_unity()?;

// Find the root of unity corresponding to the calculated log2 value
let found_root_of_unity = primitive_roots_of_unity
.get(log2_of_evals as usize)
.ok_or_else(|| KzgError::GenericError("Root of unity not found".to_string()))?;
let found_root_of_unity =
anupsv marked this conversation as resolved.
Show resolved Hide resolved
helpers::get_and_convert_primitive_root_to_fr(log2_of_evals.into())?;

// Expand the root to get all the roots of unity
let mut expanded_roots_of_unity = Self::expand_root_of_unity(found_root_of_unity);
let mut expanded_roots_of_unity = Self::expand_root_of_unity(&found_root_of_unity);

// Remove the last element to avoid duplication
expanded_roots_of_unity.truncate(expanded_roots_of_unity.len() - 1);
Expand Down Expand Up @@ -278,13 +274,9 @@ impl KZG {
));
}

let primitive_roots_of_unity = Self::get_primitive_roots_of_unity()?;
let found_root_of_unity = primitive_roots_of_unity
.get(log2_of_evals.to_usize().ok_or_else(|| {
KzgError::GenericError("Failed to convert log2_of_evals to usize".to_string())
})?)
.ok_or_else(|| KzgError::GenericError("Root of unity not found".to_string()))?;
let mut expanded_roots_of_unity = Self::expand_root_of_unity(found_root_of_unity);
let found_root_of_unity =
helpers::get_and_convert_primitive_root_to_fr(log2_of_evals.into())?;
let mut expanded_roots_of_unity = Self::expand_root_of_unity(&found_root_of_unity);
expanded_roots_of_unity.truncate(expanded_roots_of_unity.len() - 1);

params.completed_setup = true;
Expand Down Expand Up @@ -333,48 +325,6 @@ impl KZG {
roots
}

/// Precompute the primitive roots of unity for binary powers that divide r - 1
/// TODO(anupsv): Move this to the constants file. Ref: https://github.com/Layr-Labs/rust-kzg-bn254/issues/31
fn get_primitive_roots_of_unity() -> Result<Vec<Fr>, KzgError> {
let data: [&str; 29] = [
"1",
"21888242871839275222246405745257275088548364400416034343698204186575808495616",
"21888242871839275217838484774961031246007050428528088939761107053157389710902",
"19540430494807482326159819597004422086093766032135589407132600596362845576832",
"14940766826517323942636479241147756311199852622225275649687664389641784935947",
"4419234939496763621076330863786513495701855246241724391626358375488475697872",
"9088801421649573101014283686030284801466796108869023335878462724291607593530",
"10359452186428527605436343203440067497552205259388878191021578220384701716497",
"3478517300119284901893091970156912948790432420133812234316178878452092729974",
"6837567842312086091520287814181175430087169027974246751610506942214842701774",
"3161067157621608152362653341354432744960400845131437947728257924963983317266",
"1120550406532664055539694724667294622065367841900378087843176726913374367458",
"4158865282786404163413953114870269622875596290766033564087307867933865333818",
"197302210312744933010843010704445784068657690384188106020011018676818793232",
"20619701001583904760601357484951574588621083236087856586626117568842480512645",
"20402931748843538985151001264530049874871572933694634836567070693966133783803",
"421743594562400382753388642386256516545992082196004333756405989743524594615",
"12650941915662020058015862023665998998969191525479888727406889100124684769509",
"11699596668367776675346610687704220591435078791727316319397053191800576917728",
"15549849457946371566896172786938980432421851627449396898353380550861104573629",
"17220337697351015657950521176323262483320249231368149235373741788599650842711",
"13536764371732269273912573961853310557438878140379554347802702086337840854307",
"12143866164239048021030917283424216263377309185099704096317235600302831912062",
"934650972362265999028062457054462628285482693704334323590406443310927365533",
"5709868443893258075976348696661355716898495876243883251619397131511003808859",
"19200870435978225707111062059747084165650991997241425080699860725083300967194",
"7419588552507395652481651088034484897579724952953562618697845598160172257810",
"2082940218526944230311718225077035922214683169814847712455127909555749686340",
"19103219067921713944291392827692070036145651957329286315305642004821462161904",
];
data.iter()
.map(Fr::from_str)
.collect::<Result<Vec<_>, _>>()
.map_err(|_| {
KzgError::GenericError("Failed to parse primitive roots of unity".to_string())
})
}

/// helper function to get g1 points
pub fn get_g1_points(&self) -> Vec<G1Affine> {
self.g1.to_vec()
Expand Down
Loading
Loading