Skip to content

Commit 233dbc6

Browse files
CPerezzVelaciela
authored andcommitted
change: Migrate workspace to pasta_curves-0.5 (privacy-scaling-explorations#157)
* change: Migrate workspace to pasta_curves-0.5 This ports the majority of the workspace to the `pasta_curves-0.5.0` leaving some tricky edge-cases that we need to handle carefully. Resolves: privacy-scaling-explorations#132 * fix: Complete latest trait bounds to compile halo2proofs * change: Migrate examples & benches to pasta 0.5 * change: Migrate halo2_gadgets to pasta-0.5 * change: Update gadgets outdated code with latest upstream * fix: Sha3 gadget circuit * fix: doc tests * chore: Update merged main * fix: Apply review suggestions
1 parent 323f403 commit 233dbc6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+864
-803
lines changed

halo2_gadgets/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]
2424
[dependencies]
2525
arrayvec = "0.7.0"
2626
bitvec = "1"
27-
ff = "0.12"
28-
group = "0.12"
27+
ff = { version = "0.13", features = ["bits"] }
28+
group = "0.13"
2929
halo2_proofs = { version = "0.2", path = "../halo2_proofs" }
3030
lazy_static = "1"
31-
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = '0.3.0' }
31+
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2" }
3232
proptest = { version = "1.0.0", optional = true }
3333
rand = "0.8"
3434
subtle = "2.3"

halo2_gadgets/benches/poseidon.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use halo2_proofs::{
2121
use halo2curves::pasta::{pallas, vesta, EqAffine, Fp};
2222

2323
use halo2_gadgets::poseidon::{
24-
primitives::{self as poseidon, ConstantLength, Spec},
24+
primitives::{self as poseidon, generate_constants, ConstantLength, Mds, Spec},
2525
Hash, Pow5Chip, Pow5Config,
2626
};
2727
use std::convert::TryInto;
@@ -139,6 +139,10 @@ impl<const WIDTH: usize, const RATE: usize> Spec<Fp, WIDTH, RATE> for MySpec<WID
139139
fn secure_mds() -> usize {
140140
0
141141
}
142+
143+
fn constants() -> (Vec<[Fp; WIDTH]>, Mds<Fp, WIDTH>, Mds<Fp, WIDTH>) {
144+
generate_constants::<_, Self, WIDTH, RATE>()
145+
}
142146
}
143147

144148
const K: u32 = 7;

halo2_gadgets/src/ecc/chip/add.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::EccPoint;
2+
use ff::PrimeField;
23
use halo2_proofs::{
34
circuit::Region,
45
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
56
poly::Rotation,
67
};
7-
use halo2curves::{pasta::pallas, FieldExt};
8+
use halo2curves::pasta::pallas;
89
use std::collections::HashSet;
910

1011
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

halo2_gadgets/src/ecc/chip/constants.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use group::{
66
Curve,
77
};
88
use halo2_proofs::arithmetic::lagrange_interpolate;
9-
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
9+
use halo2curves::{pasta::pallas, CurveAffine};
1010

1111
/// Window size for fixed-base scalar multiplication
1212
pub const FIXED_BASE_WINDOW_SIZE: usize = 3;
@@ -61,7 +61,7 @@ fn compute_window_table<C: CurveAffine>(base: C, num_windows: usize) -> Vec<[C;
6161
// Generate window table entries for the last window, w = `num_windows - 1`.
6262
// For the last window, we compute [k * (2^3)^w - sum]B, where sum is defined
6363
// as sum = \sum_{j = 0}^{`num_windows - 2`} 2^{3j+1}
64-
let sum = (0..(num_windows - 1)).fold(C::Scalar::zero(), |acc, j| {
64+
let sum = (0..(num_windows - 1)).fold(C::Scalar::ZERO, |acc, j| {
6565
acc + C::Scalar::from(2).pow(&[FIXED_BASE_WINDOW_SIZE as u64 * j as u64 + 1, 0, 0, 0])
6666
});
6767
window_table.push(
@@ -181,7 +181,7 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
181181
.rev()
182182
.cloned()
183183
.reduce(|acc, coeff| acc * x + coeff)
184-
.unwrap_or_else(C::Base::zero)
184+
.unwrap_or(C::Base::ZERO)
185185
}
186186

187187
let lagrange_coeffs = compute_lagrange_coeffs(base, num_windows);
@@ -213,7 +213,7 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
213213

214214
// Compute the actual x-coordinate of the multiple [k * (8^84) - offset]B,
215215
// where offset = \sum_{j = 0}^{83} 2^{3j+1}
216-
let offset = (0..(num_windows - 1)).fold(C::Scalar::zero(), |acc, w| {
216+
let offset = (0..(num_windows - 1)).fold(C::Scalar::ZERO, |acc, w| {
217217
acc + C::Scalar::from(2).pow(&[FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1, 0, 0, 0])
218218
});
219219
let scalar = C::Scalar::from(bits as u64)
@@ -229,8 +229,9 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
229229

230230
#[cfg(test)]
231231
mod tests {
232+
use ff::FromUniformBytes;
232233
use group::{ff::Field, Curve, Group};
233-
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
234+
use halo2curves::{pasta::pallas, CurveAffine};
234235
use proptest::prelude::*;
235236

236237
use super::{compute_window_table, find_zs_and_us, test_lagrange_coeffs, H, NUM_WINDOWS};
@@ -241,7 +242,7 @@ mod tests {
241242
// Instead of rejecting out-of-range bytes, let's reduce them.
242243
let mut buf = [0; 64];
243244
buf[..32].copy_from_slice(&bytes);
244-
let scalar = pallas::Scalar::from_bytes_wide(&buf);
245+
let scalar = pallas::Scalar::from_uniform_bytes(&buf);
245246
pallas::Point::generator() * scalar
246247
}
247248
}

halo2_gadgets/src/ecc/chip/mul.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ use std::{
88
ops::{Deref, Range},
99
};
1010

11-
use ff::PrimeField;
1211
use halo2_proofs::{
13-
arithmetic::FieldExt,
12+
arithmetic::Field,
1413
circuit::{AssignedCell, Layouter, Region, Value},
1514
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Selector},
1615
poly::Rotation,
1716
};
18-
use uint::construct_uint;
19-
17+
use halo2curves::group::ff::PrimeField;
2018
use halo2curves::pasta::pallas;
19+
use uint::construct_uint;
2120

2221
mod complete;
2322
pub(super) mod incomplete;
@@ -389,8 +388,8 @@ impl Config {
389388

390389
#[derive(Clone, Debug)]
391390
// `x`-coordinate of the accumulator.
392-
struct X<F: FieldExt>(AssignedCell<Assigned<F>, F>);
393-
impl<F: FieldExt> Deref for X<F> {
391+
struct X<F: Field>(AssignedCell<Assigned<F>, F>);
392+
impl<F: Field> Deref for X<F> {
394393
type Target = AssignedCell<Assigned<F>, F>;
395394

396395
fn deref(&self) -> &Self::Target {
@@ -400,8 +399,8 @@ impl<F: FieldExt> Deref for X<F> {
400399

401400
#[derive(Clone, Debug)]
402401
// `y`-coordinate of the accumulator.
403-
struct Y<F: FieldExt>(AssignedCell<Assigned<F>, F>);
404-
impl<F: FieldExt> Deref for Y<F> {
402+
struct Y<F: Field>(AssignedCell<Assigned<F>, F>);
403+
impl<F: Field> Deref for Y<F> {
405404
type Target = AssignedCell<Assigned<F>, F>;
406405

407406
fn deref(&self) -> &Self::Target {
@@ -411,8 +410,8 @@ impl<F: FieldExt> Deref for Y<F> {
411410

412411
#[derive(Clone, Debug)]
413412
// Cumulative sum `z` used to decompose the scalar.
414-
struct Z<F: FieldExt>(AssignedCell<F, F>);
415-
impl<F: FieldExt> Deref for Z<F> {
413+
struct Z<F: Field>(AssignedCell<F, F>);
414+
impl<F: Field> Deref for Z<F> {
416415
type Target = AssignedCell<F, F>;
417416

418417
fn deref(&self) -> &Self::Target {

halo2_gadgets/src/ecc/chip/mul/incomplete.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use super::super::NonIdentityEccPoint;
22
use super::{X, Y, Z};
33
use crate::utilities::bool_check;
4+
use ff::PrimeField;
45
use halo2_proofs::{
56
circuit::{Region, Value},
67
plonk::{
78
Advice, Column, ConstraintSystem, Constraints, Error, Expression, Selector, VirtualCells,
89
},
910
poly::Rotation,
1011
};
11-
use halo2curves::{pasta::pallas, FieldExt};
12+
use halo2curves::pasta::pallas;
1213

1314
/// A helper struct for implementing single-row double-and-add using incomplete addition.
1415
#[derive(Copy, Clone, Debug, Eq, PartialEq)]

halo2_gadgets/src/ecc/chip/mul/overflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use halo2_proofs::{
99
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
1010
poly::Rotation,
1111
};
12-
13-
use halo2curves::{pasta::pallas, FieldExt};
12+
use halo2curves::group::ff::PrimeField;
13+
use halo2curves::pasta::pallas;
1414

1515
use std::iter;
1616

halo2_gadgets/src/ecc/chip/mul_fixed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::utilities::decompose_running_sum::RunningSumConfig;
77
use std::marker::PhantomData;
88

99
use group::{
10-
ff::{PrimeField, PrimeFieldBits},
10+
ff::{Field, PrimeField, PrimeFieldBits},
1111
Curve,
1212
};
1313
use halo2_proofs::{
@@ -18,7 +18,7 @@ use halo2_proofs::{
1818
},
1919
poly::Rotation,
2020
};
21-
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
21+
use halo2curves::{pasta::pallas, CurveAffine};
2222
use lazy_static::lazy_static;
2323

2424
pub mod base_field_elem;

halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use halo2_proofs::{
1313
plonk::{Advice, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
1414
poly::Rotation,
1515
};
16-
use halo2curves::{pasta::pallas, FieldExt};
16+
use halo2curves::pasta::pallas;
1717

1818
use std::convert::TryInto;
1919

halo2_gadgets/src/ecc/chip/mul_fixed/full_width.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub mod tests {
295295

296296
// [-1]B is the largest scalar field element.
297297
{
298-
let scalar_fixed = -pallas::Scalar::one();
298+
let scalar_fixed = -pallas::Scalar::ONE;
299299
let neg_1 = ScalarFixed::new(
300300
chip.clone(),
301301
layouter.namespace(|| "-1"),

halo2_gadgets/src/ecc/chip/mul_fixed/short.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
209209
// tested at the circuit-level.
210210
{
211211
use super::super::FixedPoint;
212+
use ff::Field;
212213
use group::{ff::PrimeField, Curve};
213214

214215
scalar
@@ -228,9 +229,9 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
228229
let magnitude = pallas::Scalar::from_repr(magnitude.to_repr()).unwrap();
229230

230231
let sign = if sign == &&pallas::Base::one() {
231-
pallas::Scalar::one()
232+
pallas::Scalar::ONE
232233
} else {
233-
-pallas::Scalar::one()
234+
-pallas::Scalar::ONE
234235
};
235236

236237
magnitude * sign
@@ -248,13 +249,16 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
248249

249250
#[cfg(test)]
250251
pub mod tests {
251-
use group::{ff::PrimeField, Curve};
252+
use group::{
253+
ff::{Field, PrimeField},
254+
Curve,
255+
};
252256
use halo2_proofs::{
253257
arithmetic::CurveAffine,
254258
circuit::{AssignedCell, Chip, Layouter, Value},
255259
plonk::{Any, Error},
256260
};
257-
use halo2curves::{pasta::pallas, FieldExt};
261+
use halo2curves::pasta::pallas;
258262

259263
use crate::{
260264
ecc::{
@@ -359,9 +363,9 @@ pub mod tests {
359363
let scalar = {
360364
let magnitude = pallas::Scalar::from_repr(magnitude.to_repr()).unwrap();
361365
let sign = if *sign == pallas::Base::one() {
362-
pallas::Scalar::one()
366+
pallas::Scalar::ONE
363367
} else {
364-
-pallas::Scalar::one()
368+
-pallas::Scalar::ONE
365369
};
366370
magnitude * sign
367371
};

halo2_gadgets/src/poseidon.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::convert::TryInto;
44
use std::fmt;
55
use std::marker::PhantomData;
66

7+
use ff::PrimeField;
78
use group::ff::Field;
89
use halo2_proofs::{
9-
arithmetic::FieldExt,
1010
circuit::{AssignedCell, Chip, Layouter},
1111
plonk::Error,
1212
};
@@ -27,7 +27,7 @@ pub enum PaddedWord<F: Field> {
2727
}
2828

2929
/// The set of circuit instructions required to use the Poseidon permutation.
30-
pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
30+
pub trait PoseidonInstructions<F: Field, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
3131
Chip<F>
3232
{
3333
/// Variable representing the word over which the Poseidon permutation operates.
@@ -45,7 +45,7 @@ pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize,
4545
///
4646
/// [`Hash`]: self::Hash
4747
pub trait PoseidonSpongeInstructions<
48-
F: FieldExt,
48+
F: Field,
4949
S: Spec<F, T, RATE>,
5050
D: Domain<F, RATE>,
5151
const T: usize,
@@ -71,7 +71,7 @@ pub trait PoseidonSpongeInstructions<
7171
/// A word over which the Poseidon permutation operates.
7272
#[derive(Debug)]
7373
pub struct Word<
74-
F: FieldExt,
74+
F: Field,
7575
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
7676
S: Spec<F, T, RATE>,
7777
const T: usize,
@@ -81,7 +81,7 @@ pub struct Word<
8181
}
8282

8383
impl<
84-
F: FieldExt,
84+
F: Field,
8585
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
8686
S: Spec<F, T, RATE>,
8787
const T: usize,
@@ -100,7 +100,7 @@ impl<
100100
}
101101

102102
fn poseidon_sponge<
103-
F: FieldExt,
103+
F: Field,
104104
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
105105
S: Spec<F, T, RATE>,
106106
D: Domain<F, RATE>,
@@ -122,7 +122,7 @@ fn poseidon_sponge<
122122
/// A Poseidon sponge.
123123
#[derive(Debug)]
124124
pub struct Sponge<
125-
F: FieldExt,
125+
F: Field,
126126
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
127127
S: Spec<F, T, RATE>,
128128
M: SpongeMode,
@@ -137,7 +137,7 @@ pub struct Sponge<
137137
}
138138

139139
impl<
140-
F: FieldExt,
140+
F: Field,
141141
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
142142
S: Spec<F, T, RATE>,
143143
D: Domain<F, RATE>,
@@ -210,7 +210,7 @@ impl<
210210
}
211211

212212
impl<
213-
F: FieldExt,
213+
F: Field,
214214
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
215215
S: Spec<F, T, RATE>,
216216
D: Domain<F, RATE>,
@@ -241,7 +241,7 @@ impl<
241241
/// A Poseidon hash function, built around a sponge.
242242
#[derive(Debug)]
243243
pub struct Hash<
244-
F: FieldExt,
244+
F: Field,
245245
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
246246
S: Spec<F, T, RATE>,
247247
D: Domain<F, RATE>,
@@ -252,7 +252,7 @@ pub struct Hash<
252252
}
253253

254254
impl<
255-
F: FieldExt,
255+
F: Field,
256256
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
257257
S: Spec<F, T, RATE>,
258258
D: Domain<F, RATE>,
@@ -267,7 +267,7 @@ impl<
267267
}
268268

269269
impl<
270-
F: FieldExt,
270+
F: PrimeField,
271271
PoseidonChip: PoseidonSpongeInstructions<F, S, ConstantLength<L>, T, RATE>,
272272
S: Spec<F, T, RATE>,
273273
const T: usize,

0 commit comments

Comments
 (0)