Skip to content

Commit

Permalink
change: Migrate workspace to pasta_curves-0.5
Browse files Browse the repository at this point in the history
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: #132
  • Loading branch information
CPerezz committed Mar 2, 2023
1 parent afe4ef4 commit d73e375
Show file tree
Hide file tree
Showing 79 changed files with 735 additions and 712 deletions.
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use halo2_proofs::{
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
poly::Rotation,
};
use halo2curves::{pasta::pallas, FieldExt};
use halo2curves::{pasta::pallas, Field};
use std::collections::HashSet;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions halo2_gadgets/src/ecc/chip/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use group::{
Curve,
};
use halo2_proofs::arithmetic::lagrange_interpolate;
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
use halo2curves::{pasta::pallas, CurveAffine, Field};

/// Window size for fixed-base scalar multiplication
pub const FIXED_BASE_WINDOW_SIZE: usize = 3;
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn test_lagrange_coeffs<C: CurveAffine>(base: C, num_windows: usize) {
#[cfg(test)]
mod tests {
use group::{ff::Field, Curve, Group};
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
use halo2curves::{pasta::pallas, CurveAffine, Field};
use proptest::prelude::*;

use super::{compute_window_table, find_zs_and_us, test_lagrange_coeffs, H, NUM_WINDOWS};
Expand Down
14 changes: 7 additions & 7 deletions halo2_gadgets/src/ecc/chip/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{

use ff::PrimeField;
use halo2_proofs::{
arithmetic::FieldExt,
arithmetic::Field,
circuit::{AssignedCell, Layouter, Region, Value},
plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Error, Selector},
poly::Rotation,
Expand Down Expand Up @@ -389,8 +389,8 @@ impl Config {

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

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

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

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

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

fn deref(&self) -> &Self::Target {
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/mul/incomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2_proofs::{
},
poly::Rotation,
};
use halo2curves::{pasta::pallas, FieldExt};
use halo2curves::{pasta::pallas, Field};

/// A helper struct for implementing single-row double-and-add using incomplete addition.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use halo2_proofs::{
poly::Rotation,
};

use halo2curves::{pasta::pallas, FieldExt};
use halo2curves::{pasta::pallas, Field};

use std::iter;

Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/mul_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use halo2_proofs::{
},
poly::Rotation,
};
use halo2curves::{pasta::pallas, CurveAffine, FieldExt};
use halo2curves::{pasta::pallas, CurveAffine, Field};
use lazy_static::lazy_static;

pub mod base_field_elem;
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/mul_fixed/base_field_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use halo2_proofs::{
plonk::{Advice, Column, ConstraintSystem, Constraints, Error, Expression, Selector},
poly::Rotation,
};
use halo2curves::{pasta::pallas, FieldExt};
use halo2curves::{pasta::pallas, Field};

use std::convert::TryInto;

Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/ecc/chip/mul_fixed/full_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub mod tests {

// [-1]B is the largest scalar field element.
{
let scalar_fixed = -pallas::Scalar::one();
let scalar_fixed = -pallas::Scalar::ONE;
let neg_1 = ScalarFixed::new(
chip.clone(),
layouter.namespace(|| "-1"),
Expand Down
10 changes: 5 additions & 5 deletions halo2_gadgets/src/ecc/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
let magnitude = pallas::Scalar::from_repr(magnitude.to_repr()).unwrap();

let sign = if sign == &&pallas::Base::one() {
pallas::Scalar::one()
pallas::Scalar::ONE
} else {
-pallas::Scalar::one()
-pallas::Scalar::ONE
};

magnitude * sign
Expand All @@ -254,7 +254,7 @@ pub mod tests {
circuit::{AssignedCell, Chip, Layouter, Value},
plonk::{Any, Error},
};
use halo2curves::{pasta::pallas, FieldExt};
use halo2curves::{pasta::pallas, Field};

use crate::{
ecc::{
Expand Down Expand Up @@ -359,9 +359,9 @@ pub mod tests {
let scalar = {
let magnitude = pallas::Scalar::from_repr(magnitude.to_repr()).unwrap();
let sign = if *sign == pallas::Base::one() {
pallas::Scalar::one()
pallas::Scalar::ONE
} else {
-pallas::Scalar::one()
-pallas::Scalar::ONE
};
magnitude * sign
};
Expand Down
24 changes: 12 additions & 12 deletions halo2_gadgets/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::marker::PhantomData;

use group::ff::Field;
use halo2_proofs::{
arithmetic::FieldExt,
arithmetic::Field,
circuit::{AssignedCell, Chip, Layouter},
plonk::Error,
};
Expand All @@ -27,7 +27,7 @@ pub enum PaddedWord<F: Field> {
}

/// The set of circuit instructions required to use the Poseidon permutation.
pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
pub trait PoseidonInstructions<F: Field, S: Spec<F, T, RATE>, const T: usize, const RATE: usize>:
Chip<F>
{
/// Variable representing the word over which the Poseidon permutation operates.
Expand All @@ -45,7 +45,7 @@ pub trait PoseidonInstructions<F: FieldExt, S: Spec<F, T, RATE>, const T: usize,
///
/// [`Hash`]: self::Hash
pub trait PoseidonSpongeInstructions<
F: FieldExt,
F: Field,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
const T: usize,
Expand All @@ -71,7 +71,7 @@ pub trait PoseidonSpongeInstructions<
/// A word over which the Poseidon permutation operates.
#[derive(Debug)]
pub struct Word<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand All @@ -81,7 +81,7 @@ pub struct Word<
}

impl<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonInstructions<F, S, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand All @@ -100,7 +100,7 @@ impl<
}

fn poseidon_sponge<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -122,7 +122,7 @@ fn poseidon_sponge<
/// A Poseidon sponge.
#[derive(Debug)]
pub struct Sponge<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
M: SpongeMode,
Expand All @@ -137,7 +137,7 @@ pub struct Sponge<
}

impl<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<
}

impl<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<
/// A Poseidon hash function, built around a sponge.
#[derive(Debug)]
pub struct Hash<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -252,7 +252,7 @@ pub struct Hash<
}

impl<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, D, T, RATE>,
S: Spec<F, T, RATE>,
D: Domain<F, RATE>,
Expand All @@ -267,7 +267,7 @@ impl<
}

impl<
F: FieldExt,
F: Field,
PoseidonChip: PoseidonSpongeInstructions<F, S, ConstantLength<L>, T, RATE>,
S: Spec<F, T, RATE>,
const T: usize,
Expand Down
36 changes: 18 additions & 18 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryInto;
use std::iter;

use halo2_proofs::{
arithmetic::FieldExt,
arithmetic::Field,
circuit::{AssignedCell, Cell, Chip, Layouter, Region, Value},
plonk::{
Advice, Any, Column, ConstraintSystem, Constraints, Error, Expression, Fixed, Selector,
Expand All @@ -18,7 +18,7 @@ use crate::utilities::Var;

/// Configuration for a [`Pow5Chip`].
#[derive(Clone, Debug)]
pub struct Pow5Config<F: FieldExt, const WIDTH: usize, const RATE: usize> {
pub struct Pow5Config<F: Field, const WIDTH: usize, const RATE: usize> {
pub(crate) state: [Column<Advice>; WIDTH],
partial_sbox: Column<Advice>,
rc_a: [Column<Fixed>; WIDTH],
Expand All @@ -40,11 +40,11 @@ pub struct Pow5Config<F: FieldExt, const WIDTH: usize, const RATE: usize> {
/// The chip is implemented using a single round per row for full rounds, and two rounds
/// per row for partial rounds.
#[derive(Debug)]
pub struct Pow5Chip<F: FieldExt, const WIDTH: usize, const RATE: usize> {
pub struct Pow5Chip<F: Field, const WIDTH: usize, const RATE: usize> {
config: Pow5Config<F, WIDTH, RATE>,
}

impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Pow5Chip<F, WIDTH, RATE> {
impl<F: Field, const WIDTH: usize, const RATE: usize> Pow5Chip<F, WIDTH, RATE> {
/// Configures this chip for use in a circuit.
///
/// # Side-effects
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Pow5Chip<F, WIDTH, RATE
}
}

impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Chip<F> for Pow5Chip<F, WIDTH, RATE> {
impl<F: Field, const WIDTH: usize, const RATE: usize> Chip<F> for Pow5Chip<F, WIDTH, RATE> {
type Config = Pow5Config<F, WIDTH, RATE>;
type Loaded = ();

Expand All @@ -222,7 +222,7 @@ impl<F: FieldExt, const WIDTH: usize, const RATE: usize> Chip<F> for Pow5Chip<F,
}
}

impl<F: FieldExt, S: Spec<F, WIDTH, RATE>, const WIDTH: usize, const RATE: usize>
impl<F: Field, S: Spec<F, WIDTH, RATE>, const WIDTH: usize, const RATE: usize>
PoseidonInstructions<F, S, WIDTH, RATE> for Pow5Chip<F, WIDTH, RATE>
{
type Word = StateWord<F>;
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<F: FieldExt, S: Spec<F, WIDTH, RATE>, const WIDTH: usize, const RATE: usize
}

impl<
F: FieldExt,
F: Field,
S: Spec<F, WIDTH, RATE>,
D: Domain<F, RATE>,
const WIDTH: usize,
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<
};

for i in 0..RATE {
load_state_word(i, F::zero())?;
load_state_word(i, F::ZERO)?;
}
load_state_word(RATE, D::initial_capacity_element())?;

Expand Down Expand Up @@ -372,7 +372,7 @@ impl<
.get(i)
.map(|word| word.0.value().cloned())
// The capacity element is never altered by the input.
.unwrap_or_else(|| Value::known(F::zero()));
.unwrap_or_else(|| Value::known(F::ZERO));
region
.assign_advice(
|| format!("load output_{}", i),
Expand Down Expand Up @@ -403,21 +403,21 @@ impl<

/// A word in the Poseidon state.
#[derive(Clone, Debug)]
pub struct StateWord<F: FieldExt>(AssignedCell<F, F>);
pub struct StateWord<F: Field>(AssignedCell<F, F>);

impl<F: FieldExt> From<StateWord<F>> for AssignedCell<F, F> {
impl<F: Field> From<StateWord<F>> for AssignedCell<F, F> {
fn from(state_word: StateWord<F>) -> AssignedCell<F, F> {
state_word.0
}
}

impl<F: FieldExt> From<AssignedCell<F, F>> for StateWord<F> {
impl<F: Field> From<AssignedCell<F, F>> for StateWord<F> {
fn from(cell_value: AssignedCell<F, F>) -> StateWord<F> {
StateWord(cell_value)
}
}

impl<F: FieldExt> Var<F> for StateWord<F> {
impl<F: Field> Var<F> for StateWord<F> {
fn cell(&self) -> Cell {
self.0.cell()
}
Expand All @@ -428,9 +428,9 @@ impl<F: FieldExt> Var<F> for StateWord<F> {
}

#[derive(Debug)]
struct Pow5State<F: FieldExt, const WIDTH: usize>([StateWord<F>; WIDTH]);
struct Pow5State<F: Field, const WIDTH: usize>([StateWord<F>; WIDTH]);

impl<F: FieldExt, const WIDTH: usize> Pow5State<F, WIDTH> {
impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
fn full_round<const RATE: usize>(
self,
region: &mut Region<F>,
Expand All @@ -450,7 +450,7 @@ impl<F: FieldExt, const WIDTH: usize> Pow5State<F, WIDTH> {
r.as_ref().map(|r| {
r.iter()
.enumerate()
.fold(F::zero(), |acc, (j, r_j)| acc + m_i[j] * r_j)
.fold(F::ZERO, |acc, (j, r_j)| acc + m_i[j] * r_j)
})
});

Expand Down Expand Up @@ -491,7 +491,7 @@ impl<F: FieldExt, const WIDTH: usize> Pow5State<F, WIDTH> {
r.as_ref().map(|r| {
m_i.iter()
.zip(r.iter())
.fold(F::zero(), |acc, (m_ij, r_j)| acc + *m_ij * r_j)
.fold(F::ZERO, |acc, (m_ij, r_j)| acc + *m_ij * r_j)
})
})
.collect();
Expand Down Expand Up @@ -524,7 +524,7 @@ impl<F: FieldExt, const WIDTH: usize> Pow5State<F, WIDTH> {
r_mid.as_ref().map(|r| {
m_i.iter()
.zip(r.iter())
.fold(F::zero(), |acc, (m_ij, r_j)| acc + *m_ij * r_j)
.fold(F::ZERO, |acc, (m_ij, r_j)| acc + *m_ij * r_j)
})
})
.collect();
Expand Down
Loading

0 comments on commit d73e375

Please sign in to comment.