Skip to content

Commit 57fd3ba

Browse files
authored
chore: replace Scalar::from(0u64) and Scalar::from(1u64) (#280)
* replace Scalar::from(0u64) and Scalar::from(1u64) * fix imports
1 parent 9351e91 commit 57fd3ba

File tree

10 files changed

+26
-24
lines changed

10 files changed

+26
-24
lines changed

cryptography/erasure_codes/src/reed_solomon.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ mod tests {
450450
// zero out `num_erasures` amount of evaluations to simulate erasures
451451
let mut missing_indices = Vec::new();
452452
for index in 0..num_erasures {
453-
codewords_with_erasures[index] = Scalar::from(0);
453+
codewords_with_erasures[index] = Scalar::ZERO;
454454
missing_indices.push(index);
455455
}
456456

@@ -490,7 +490,7 @@ mod tests {
490490
let mut missing_block_indices = Vec::new();
491491
for index in 0..num_block_erasures {
492492
for block in &mut blocks {
493-
block[index] = Scalar::from(0)
493+
block[index] = Scalar::ZERO
494494
}
495495
missing_block_indices.push(index);
496496
}

cryptography/kzg_multi_open/benches/benchmark.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub fn create_insecure_commit_opening_keys() -> (CommitKey, OpeningKey) {
101101
let g1_gen = G1Projective::generator();
102102

103103
let mut g1_points = Vec::new();
104-
let secret = -Scalar::from(1 as u64);
104+
let secret = -Scalar::ONE;
105105
let mut current_secret_pow = Scalar::ONE;
106106
for _ in 0..num_coefficients_in_polynomial {
107107
g1_points.push(g1_gen * current_secret_pow);
@@ -112,7 +112,7 @@ pub fn create_insecure_commit_opening_keys() -> (CommitKey, OpeningKey) {
112112
let ck = CommitKey::new(g1_points.clone());
113113

114114
let mut g2_points = Vec::new();
115-
let secret = -Scalar::from(1 as u64);
115+
let secret = -Scalar::ONE;
116116
let mut current_secret_pow = Scalar::ONE;
117117
let g2_gen = G2Projective::generator();
118118
// The setup needs 65 g1 elements for the opening key, in order

cryptography/kzg_multi_open/src/fk20/cosets.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bls12_381::Scalar;
1+
use bls12_381::{ff::Field, Scalar};
22
use polynomial::domain::Domain;
33

44
/// Reverses the least significant `bits` of the given number `n`.
@@ -113,7 +113,7 @@ pub fn recover_evaluations_in_domain_order(
113113
return None;
114114
}
115115

116-
let mut elements = vec![Scalar::from(0u64); domain_size];
116+
let mut elements = vec![Scalar::ZERO; domain_size];
117117

118118
// Check that each coset has the same size
119119
let coset_len = coset_evaluations[0].len();

cryptography/kzg_multi_open/src/fk20/h_poly.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::fk20::toeplitz::ToeplitzMatrix;
2-
use bls12_381::{G1Projective, Scalar};
2+
use bls12_381::{ff::Field, G1Projective, Scalar};
33
use polynomial::monomial::PolyCoeff;
44

55
use super::batch_toeplitz::BatchToeplitzMatrixVecMul;
@@ -47,7 +47,7 @@ pub(crate) fn compute_h_poly_commitments(
4747
let mut matrices = Vec::with_capacity(toeplitz_rows.len());
4848
// We want to do `coset_size` toeplitz matrix multiplications
4949
for row in toeplitz_rows.into_iter() {
50-
let mut toeplitz_column = vec![Scalar::from(0u64); row.len()];
50+
let mut toeplitz_column = vec![Scalar::ZERO; row.len()];
5151
toeplitz_column[0] = row[0];
5252

5353
matrices.push(ToeplitzMatrix::new(row, toeplitz_column));

cryptography/kzg_multi_open/src/fk20/toeplitz.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,11 @@ struct DenseMatrix {
207207
impl DenseMatrix {
208208
/// Converts a `ToeplitzMatrix` into a `DenseMatrix`
209209
fn from_toeplitz(toeplitz: ToeplitzMatrix) -> DenseMatrix {
210+
use bls12_381::ff::Field;
211+
210212
let rows = toeplitz.col.len();
211213
let cols = toeplitz.row.len();
212-
let mut matrix = vec![vec![Scalar::from(0u64); toeplitz.col.len()]; toeplitz.row.len()];
214+
let mut matrix = vec![vec![Scalar::ZERO; toeplitz.col.len()]; toeplitz.row.len()];
213215

214216
for i in 0..rows {
215217
for j in 0..cols {

cryptography/kzg_multi_open/src/fk20/verifier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl FK20Verifier {
177177
// One can view this as trading a scalar multiplication for a field addition.
178178
//
179179
// The extra field additions are being calculated in the for loop below.
180-
let mut weights = vec![Scalar::from(0); num_unique_commitments];
180+
let mut weights = vec![Scalar::ZERO; num_unique_commitments];
181181
for (commitment_index, r_power) in commitment_indices.iter().zip(r_powers.iter()) {
182182
weights[*commitment_index as usize] += r_power;
183183
}

cryptography/kzg_multi_open/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn create_insecure_commit_opening_keys(
2828
let g1_gen = G1Projective::generator();
2929

3030
let mut g1_points = Vec::new();
31-
let secret = -Scalar::from(1 as u64);
31+
let secret = -Scalar::ONE;
3232
let mut current_secret_pow = Scalar::ONE;
3333
for _ in 0..num_coefficients_in_polynomial {
3434
g1_points.push(g1_gen * current_secret_pow);
@@ -39,7 +39,7 @@ pub(crate) fn create_insecure_commit_opening_keys(
3939
let ck = CommitKey::new(g1_points.clone());
4040

4141
let mut g2_points = Vec::new();
42-
let secret = -Scalar::from(1 as u64);
42+
let secret = -Scalar::ONE;
4343
let mut current_secret_pow = Scalar::ONE;
4444
let g2_gen = G2Projective::generator();
4545
// The setup needs 65 g1 elements for the opening key, in order

cryptography/kzg_multi_open/src/naive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{commit_key::CommitKey, opening_key::OpeningKey};
2-
use bls12_381::{multi_pairings, G1Point, G1Projective, G2Point, G2Prepared, Scalar};
2+
use bls12_381::{ff::Field, multi_pairings, G1Point, G1Projective, G2Point, G2Prepared, Scalar};
33
use polynomial::monomial::{lagrange_interpolate, poly_eval, poly_sub, vanishing_poly, PolyCoeff};
44

55
/// This modules contains code to create and verify opening proofs in a naive way.
@@ -67,7 +67,7 @@ fn _compute_multi_opening_naive(
6767
// Divides `self` by x-z using Ruffinis rule
6868
fn divide_by_linear(poly: &[Scalar], z: Scalar) -> Vec<Scalar> {
6969
let mut quotient: Vec<Scalar> = Vec::with_capacity(poly.len());
70-
let mut k = Scalar::from(0u64);
70+
let mut k = Scalar::ZERO;
7171

7272
for coeff in poly.iter().rev() {
7373
let t = *coeff + k;

cryptography/polynomial/src/domain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ mod tests {
390390
}
391391
fn powers_of(scalar: &Scalar, max_degree: usize) -> Vec<Scalar> {
392392
let mut powers = Vec::new();
393-
powers.push(Scalar::from(1u64));
393+
powers.push(Scalar::ONE);
394394
for i in 1..=max_degree {
395395
powers.push(powers[i - 1] * scalar);
396396
}

cryptography/polynomial/src/monomial.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn poly_sub(a: PolyCoeff, b: PolyCoeff) -> PolyCoeff {
4242
/// Given a polynomial `f(x)` and a scalar `z`. This method will compute
4343
/// the result of `f(z)` and return the result.
4444
pub fn poly_eval(poly: &PolyCoeff, value: &Scalar) -> Scalar {
45-
let mut result = Scalar::from(0u64);
45+
let mut result = Scalar::ZERO;
4646
for coeff in poly.iter().rev() {
4747
result = result * value + coeff;
4848
}
@@ -66,9 +66,9 @@ pub fn poly_mul(a: PolyCoeff, b: PolyCoeff) -> PolyCoeff {
6666
///
6767
/// Example: vanishing_poly([1, 2, 3]) = (x - 1)(x - 2)(x - 3)
6868
pub fn vanishing_poly(roots: &[Scalar]) -> PolyCoeff {
69-
let mut poly = vec![Scalar::from(1u64)];
69+
let mut poly = vec![Scalar::ONE];
7070
for root in roots {
71-
poly = poly_mul(poly, vec![-root, Scalar::from(1u64)]);
71+
poly = poly_mul(poly, vec![-root, Scalar::ONE]);
7272
}
7373
poly
7474
}
@@ -89,13 +89,13 @@ pub fn lagrange_interpolate(points: &[(Scalar, Scalar)]) -> Option<Vec<Scalar>>
8989
max_degree_plus_one >= 2,
9090
"should interpolate for degree >= 1"
9191
);
92-
let mut coeffs = vec![Scalar::from(0u64); max_degree_plus_one];
92+
let mut coeffs = vec![Scalar::ZERO; max_degree_plus_one];
9393
// external iterator
9494
for (k, p_k) in points.iter().enumerate() {
9595
let (x_k, y_k) = p_k;
9696
// coeffs from 0 to max_degree - 1
97-
let mut contribution = vec![Scalar::from(0u64); max_degree_plus_one];
98-
let mut denominator = Scalar::from(1u64);
97+
let mut contribution = vec![Scalar::ZERO; max_degree_plus_one];
98+
let mut denominator = Scalar::ONE;
9999
let mut max_contribution_degree = 0;
100100
// internal iterator
101101
for (j, p_j) in points.iter().enumerate() {
@@ -127,7 +127,7 @@ pub fn lagrange_interpolate(points: &[(Scalar, Scalar)]) -> Option<Vec<Scalar>>
127127
})
128128
.collect();
129129

130-
contribution.insert(0, Scalar::from(0u64));
130+
contribution.insert(0, Scalar::ZERO);
131131
contribution.truncate(max_degree_plus_one);
132132

133133
assert_eq!(mul_by_minus_x_j.len(), max_degree_plus_one);
@@ -161,7 +161,7 @@ mod tests {
161161
use bls12_381::ff::Field;
162162

163163
fn naive_poly_eval(poly: &PolyCoeff, value: &Scalar) -> Scalar {
164-
let mut result = Scalar::from(0u64);
164+
let mut result = Scalar::ZERO;
165165
for (i, coeff) in poly.iter().enumerate() {
166166
result += coeff * value.pow_vartime(&[i as u64]);
167167
}
@@ -241,7 +241,7 @@ mod tests {
241241

242242
// Check that this polynomial evaluates to zero on the roots
243243
for root in roots.iter() {
244-
assert_eq!(poly_eval(&poly, &root), Scalar::from(0u64));
244+
assert_eq!(poly_eval(&poly, &root), Scalar::ZERO);
245245
}
246246
}
247247

0 commit comments

Comments
 (0)