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

Compatability changes to work with arb nitro #4

Merged
merged 45 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
0d4127f
add getter for expanded roots of unity to debug midmatch
afkbyte May 27, 2024
e14f7bf
add getter for expanded roots of unity to debug midmatch
afkbyte May 27, 2024
ddb2289
change to older version
epociask May 28, 2024
91c0c6e
commit generated lock
epociask May 28, 2024
231c53e
update tracing dependency
epociask May 29, 2024
1ff337e
able to create already padded blobs
afkbyte May 29, 2024
2f747d2
add function to commit to polynomial that already has data as evaluat…
afkbyte May 29, 2024
bda93f8
also add function to commit to kzg proof with polynomial that already…
afkbyte May 29, 2024
cb41718
update num_cpus dep
epociask May 29, 2024
a71041b
update tracing-core
epociask May 29, 2024
5073ea5
update traciing
epociask May 29, 2024
588e3e4
Update toolchain to get build working
teddyknox May 30, 2024
50996c1
Merge branch 'master' into afk/master
afkbyte May 30, 2024
99c67bc
Merge pull request #1 from afkbyte/afk/master
afkbyte May 30, 2024
5dd301f
remove duplicate functions
afkbyte May 30, 2024
41e673e
update tracing dependency
epociask May 29, 2024
c8fa0cc
also add function to commit to kzg proof with polynomial that already…
afkbyte May 29, 2024
13e1187
added funtionality needed for arbitrum
afkbyte May 30, 2024
fbad250
add more tests and error type for polynomial fft
afkbyte May 30, 2024
2cfab03
update false to true in a test
afkbyte May 30, 2024
0d93340
readd is_padded
afkbyte May 30, 2024
5a51bae
remove get_expanded_roots_of_unity
afkbyte May 30, 2024
0040227
readd Cargo.lock to .gitignore
afkbyte May 30, 2024
f2449c3
remove Cargo.lock
afkbyte May 30, 2024
b685fbb
remove now reduntant compute_kzg_proof_with_evaluation_polynomial
afkbyte May 31, 2024
89f297f
cargo fmt
afkbyte May 31, 2024
6ecc853
update variable name in test
afkbyte May 31, 2024
1a3e490
update variable name elsewhere
afkbyte May 31, 2024
e309238
fix failing kzg test
afkbyte May 31, 2024
eeb0d5a
Merge branch 'master' into afk/g1-iift-optional
afkbyte May 31, 2024
df50407
merge fft and ifft functions, readd test to new test files
afkbyte May 31, 2024
be7a2b7
fix clippy
afkbyte May 31, 2024
d130d2b
update kzg test signatures
afkbyte May 31, 2024
56bf11c
run cargo fmt
afkbyte May 31, 2024
10beec1
move to dev deps
afkbyte May 31, 2024
b26c934
add enum to polynomial specifying what form it's in, use that to info…
afkbyte May 31, 2024
ecd1ef2
fix syntax errors in tests
afkbyte May 31, 2024
ef7774b
fix more test syntax errors
afkbyte May 31, 2024
d403cfe
fix logical error in transform_to_form
afkbyte May 31, 2024
7231419
update transform_error test strings
afkbyte May 31, 2024
b10dbff
replace from_padded_bytes with from_padded_bytes_unchecked
afkbyte May 31, 2024
c13ff8c
update benchmarks
afkbyte May 31, 2024
7a0059c
add test coverage for blob tests
afkbyte May 31, 2024
3232ee8
Merge branch 'master' into afk/g1-iift-optional
afkbyte May 31, 2024
9d61ae2
readd polynomial tests
afkbyte May 31, 2024
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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ directories = "5.0.1"
hex-literal = "0.4.1"
rand = "0.8.5"
sha2 = "0.10.8"
tracing = { version = "^0.1.34", features = ["log"] }
tracing-subscriber = "0.3.18"
ureq = "2.9.6"
afkbyte marked this conversation as resolved.
Show resolved Hide resolved
num-bigint = "0.4"
rayon = "^1.5"
num-traits = "0.2"
byteorder = "1.4"
ark-poly = "0.4.2"
crossbeam-channel = "0.5"
num_cpus = "^1.16.0"
num_cpus = "1.13.0"

[dev-dependencies]
criterion = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = 'nightly-2024-04-29'
channel = '1.74'
profile = 'minimal'
components = ['clippy', 'rustfmt']
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "wasm32-unknown-unknown"]
targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "wasm32-unknown-unknown", "aarch64-apple-darwin"]
21 changes: 21 additions & 0 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ impl Blob {
}
}

/// Creates a new `Blob` from the provided byte slice and assumes it's
/// already padded according to DA specs.
pub fn from_padded_bytes(input: &[u8]) -> Result<Self, BlobError> {
// check to see if bytes are modulo bn254
// set_bytes_canonical used in to_fr_array calls from_be_bytes_mod_order
// if the bytes passed into set_bytes_canonical are larger than the bn254 field
// modulo order then the bytes will be modded by the order of the field
let length_after_padding = input.len();
let fr_vec = helpers::to_fr_array(input);
let bytes = helpers::to_byte_array(&fr_vec, length_after_padding);
afkbyte marked this conversation as resolved.
Show resolved Hide resolved
if bytes != input {
return Err(BlobError::NotPaddedError);
}

Ok(Blob {
blob_data: bytes,
is_padded: true,
length_after_padding,
})
}

/// Returns the blob data
pub fn get_blob_data(&self) -> Vec<u8> {
self.blob_data.clone()
Expand Down
9 changes: 9 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum PolynomialError {
SerializationFromStringError,
CommitError(String),
GenericError(String),
FFTError(String),
}

impl fmt::Display for PolynomialError {
Expand All @@ -33,6 +34,7 @@ impl fmt::Display for PolynomialError {
write!(f, "couldn't load string to fr vector")
},
PolynomialError::CommitError(ref msg) => write!(f, "Commitment error: {}", msg),
PolynomialError::FFTError(ref msg) => write!(f, "FFT error: {}", msg),
PolynomialError::GenericError(ref msg) => write!(f, "generic error: {}", msg),
}
}
Expand Down Expand Up @@ -85,6 +87,13 @@ mod tests {
assert_eq!(format!("{}", error), format!("generic error: {}", msg));
}

#[test]
fn test_polynomial_error_fft() {
let msg = String::from("test fft error");
let error = PolynomialError::FFTError(msg.clone());
assert_eq!(format!("{}", error), format!("FFT error: {}", msg));
}

#[test]
fn test_polynomial_error_equality() {
let error1 = PolynomialError::SerializationFromStringError;
Expand Down
110 changes: 92 additions & 18 deletions src/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ impl Kzg {
}

/// commit the actual polynomial with the values setup
pub fn commit(&self, polynomial: &Polynomial) -> Result<G1Affine, KzgError> {
pub fn commit(
&self,
polynomial: &Polynomial,
is_data_polynomial_evaluations: bool,
afkbyte marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<G1Affine, KzgError> {
if polynomial.len() > self.g1.len() {
return Err(KzgError::SerializationError(
"polynomial length is not correct".to_string(),
Expand All @@ -405,20 +409,30 @@ impl Kzg {

// Perform the multi-exponentiation
config.install(|| {
let bases = self.g1_ifft(polynomial.len()).unwrap();
let bases = if is_data_polynomial_evaluations {
// if the blob data is polynomial evaluations then we use the original g1 points
self.g1[..polynomial.len()].to_vec()
} else {
// Use inverse FFT if not
self.g1_ifft(polynomial.len())?
};

match G1Projective::msm(&bases, &polynomial.to_vec()) {
Ok(res) => Ok(res.into_affine()),
Err(err) => Err(KzgError::CommitError(err.to_string())),
}
})
}

/// 4844 compatible helper function
pub fn blob_to_kzg_commitment(&self, blob: &Blob) -> Result<G1Affine, KzgError> {
pub fn blob_to_kzg_commitment(
&self,
blob: &Blob,
is_blob_ifft: bool,
) -> Result<G1Affine, KzgError> {
let polynomial = blob
.to_polynomial()
.map_err(|err| KzgError::SerializationError(err.to_string()))?;
let commitment = self.commit(&polynomial)?;
let commitment = self.commit(&polynomial, is_blob_ifft)?;
Ok(commitment)
}

Expand All @@ -427,8 +441,14 @@ impl Kzg {
&self,
polynomial: &Polynomial,
index: u64,
is_data_polynomial_evaluations: bool,
) -> Result<G1Affine, KzgError> {
self.compute_kzg_proof(polynomial, index, &self.expanded_roots_of_unity)
self.compute_kzg_proof(
polynomial,
index,
&self.expanded_roots_of_unity,
is_data_polynomial_evaluations,
)
}

/// function to compute the kzg proof given the values.
Expand All @@ -437,6 +457,7 @@ impl Kzg {
polynomial: &Polynomial,
index: u64,
root_of_unities: &Vec<Fr>,
is_data_polynomial_evaluations: bool,
) -> Result<G1Affine, KzgError> {
if !self.params.completed_setup {
return Err(KzgError::GenericError(
Expand Down Expand Up @@ -487,9 +508,15 @@ impl Kzg {
}
}

let g1_lagrange = self.g1_ifft(polynomial.len())?;
let bases = if is_data_polynomial_evaluations {
// if the blob data is polynomial evaluations then we use the original g1 points
self.g1[..polynomial.len()].to_vec()
} else {
// Use inverse FFT if not
self.g1_ifft(polynomial.len())?
};

match G1Projective::msm(&g1_lagrange, &quotient_poly) {
match G1Projective::msm(&bases, &quotient_poly) {
Ok(res) => Ok(G1Affine::from(res)),
Err(err) => Err(KzgError::SerializationError(err.to_string())),
}
Expand Down Expand Up @@ -623,7 +650,7 @@ mod tests {
}

let polynomial = Polynomial::new(&poly, 2).unwrap();
let result = KZG_3000.commit(&polynomial);
let result = KZG_3000.commit(&polynomial, false);
assert_eq!(
result,
Err(KzgError::SerializationError(
Expand Down Expand Up @@ -751,7 +778,7 @@ mod tests {
use ark_bn254::Fq;

let blob = Blob::from_bytes_and_pad(GETTYSBURG_ADDRESS_BYTES);
let fn_output = KZG_3000.blob_to_kzg_commitment(&blob).unwrap();
let fn_output = KZG_3000.blob_to_kzg_commitment(&blob, false).unwrap();
let commitment_from_da = G1Affine::new_unchecked(
Fq::from_str(
"2961155957874067312593973807786254905069537311739090798303675273531563528369",
Expand Down Expand Up @@ -786,9 +813,13 @@ mod tests {

let index = rand::thread_rng()
.gen_range(0..input_poly.get_length_of_padded_blob_as_fr_vector());
let commitment = kzg.commit(&input_poly.clone()).unwrap();
let commitment = kzg.commit(&input_poly.clone(), false).unwrap();
let proof = kzg
.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap())
.compute_kzg_proof_with_roots_of_unity(
&input_poly,
index.try_into().unwrap(),
false,
)
.unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
Expand Down Expand Up @@ -821,9 +852,9 @@ mod tests {
let mut kzg = KZG_INSTANCE.clone();

let input = Blob::from_bytes_and_pad(GETTYSBURG_ADDRESS_BYTES);
let input_poly = input.to_polynomial().unwrap();
let input_poly_coefficients = input.to_polynomial().unwrap();

for index in 0..input_poly.len() - 1 {
for index in 0..input_poly_coefficients.len() - 1 {
// let index = rand::thread_rng().gen_range(0..input_poly.len());
kzg.data_setup_custom(4, input.len().try_into().unwrap())
.unwrap();
Expand All @@ -835,11 +866,15 @@ mod tests {
break;
}
}
let commitment = kzg.commit(&input_poly.clone()).unwrap();
let commitment = kzg.commit(&input_poly_coefficients.clone(), false).unwrap();
let proof = kzg
.compute_kzg_proof_with_roots_of_unity(&input_poly, index.try_into().unwrap())
.compute_kzg_proof_with_roots_of_unity(
&input_poly_coefficients,
index.try_into().unwrap(),
false,
)
.unwrap();
let value_fr = input_poly.get_at_index(index).unwrap();
let value_fr = input_poly_coefficients.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
let pairing_result =
kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone());
Expand All @@ -854,6 +889,45 @@ mod tests {
false
)
}

let mut input_poly_evaluation = input_poly_coefficients.clone();
input_poly_evaluation.ifft_on_elements().unwrap();

for index in 0..input_poly_evaluation.len() - 1 {
kzg.data_setup_custom(4, input.len().try_into().unwrap())
.unwrap();
let mut rand_index = rand::thread_rng().gen_range(0..kzg.expanded_roots_of_unity.len());
loop {
if index == rand_index {
rand_index = rand::thread_rng().gen_range(0..kzg.expanded_roots_of_unity.len());
} else {
break;
}
}
let commitment = kzg.commit(&input_poly_evaluation.clone(), true).unwrap();
let proof = kzg
.compute_kzg_proof_with_roots_of_unity(
&input_poly_evaluation,
index.try_into().unwrap(),
true,
)
.unwrap();
// because this polynomial is evaluations we want to get the value at the same index of the polynomial that is the coefficients of the polynomial
let value_fr = input_poly_coefficients.get_at_index(index).unwrap();
let z_fr = kzg.get_nth_root_of_unity(index).unwrap();
let pairing_result =
kzg.verify_kzg_proof(commitment, proof, value_fr.clone(), z_fr.clone());
assert_eq!(pairing_result, true);
assert_eq!(
kzg.verify_kzg_proof(
commitment,
proof,
value_fr.clone(),
kzg.get_nth_root_of_unity(rand_index).unwrap().clone()
),
true
)
}
}

#[test]
Expand Down Expand Up @@ -1016,7 +1090,7 @@ mod tests {
kzg.data_setup_custom(4, poly.len().try_into().unwrap())
.unwrap();
let result = kzg
.compute_kzg_proof(&poly, index, &roots_of_unities)
.compute_kzg_proof(&poly, index, &roots_of_unities, false)
.unwrap();
assert_eq!(gnark_proof, result)
}
Expand Down
Loading
Loading