Skip to content

folding/ivc/curves: use core instead of std #3115

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

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions curves/tests/pasta_curves.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;

use ark_algebra_test_templates::*;
use core::str::FromStr;
use mina_curves::pasta::{Fp, Pallas, ProjectivePallas, ProjectiveVesta};
use num_bigint::BigUint;

Expand Down
2 changes: 1 addition & 1 deletion folding/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
use ark_ec::AffineRepr;
use ark_ff::{Field, Zero};
use ark_poly::Evaluations;
use core::ops::Index;
use kimchi::circuits::{expr::Variable, gate::CurrOrNext};
use std::ops::Index;

#[cfg(not(test))]
use log::debug;
Expand Down
12 changes: 6 additions & 6 deletions folding/src/eval_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub enum EvalLeaf<'a, F> {
Result(Vec<F>),
}

impl<'a, F: std::fmt::Display> std::fmt::Display for EvalLeaf<'a, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<'a, F: core::fmt::Display> core::fmt::Display for EvalLeaf<'a, F> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let slice = match self {
EvalLeaf::Const(c) => {
write!(f, "Const: {}", c)?;
Expand All @@ -25,31 +25,31 @@ impl<'a, F: std::fmt::Display> std::fmt::Display for EvalLeaf<'a, F> {
}
}

impl<'a, F: std::ops::Add<Output = F> + Clone> std::ops::Add for EvalLeaf<'a, F> {
impl<'a, F: core::ops::Add<Output = F> + Clone> core::ops::Add for EvalLeaf<'a, F> {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self::bin_op(|a, b| a + b, self, rhs)
}
}

impl<'a, F: std::ops::Sub<Output = F> + Clone> std::ops::Sub for EvalLeaf<'a, F> {
impl<'a, F: core::ops::Sub<Output = F> + Clone> core::ops::Sub for EvalLeaf<'a, F> {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
Self::bin_op(|a, b| a - b, self, rhs)
}
}

impl<'a, F: std::ops::Mul<Output = F> + Clone> std::ops::Mul for EvalLeaf<'a, F> {
impl<'a, F: core::ops::Mul<Output = F> + Clone> core::ops::Mul for EvalLeaf<'a, F> {
type Output = Self;

fn mul(self, rhs: Self) -> Self {
Self::bin_op(|a, b| a * b, self, rhs)
}
}

impl<'a, F: std::ops::Mul<Output = F> + Clone> std::ops::Mul<F> for EvalLeaf<'a, F> {
impl<'a, F: core::ops::Mul<Output = F> + Clone> core::ops::Mul<F> for EvalLeaf<'a, F> {
type Output = Self;

fn mul(self, rhs: F) -> Self {
Expand Down
22 changes: 11 additions & 11 deletions folding/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub enum Degree {
Two,
}

impl std::ops::Add for Degree {
impl core::ops::Add for Degree {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Expand All @@ -313,7 +313,7 @@ impl std::ops::Add for Degree {
}
}

impl std::ops::Mul for &Degree {
impl core::ops::Mul for &Degree {
type Output = Degree;

fn mul(self, rhs: Self) -> Self::Output {
Expand Down Expand Up @@ -398,23 +398,23 @@ pub enum FoldingCompatibleExpr<C: FoldingConfig> {
Square(Box<Self>),
}

impl<C: FoldingConfig> std::ops::Add for FoldingCompatibleExpr<C> {
impl<C: FoldingConfig> core::ops::Add for FoldingCompatibleExpr<C> {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self::Add(Box::new(self), Box::new(rhs))
}
}

impl<C: FoldingConfig> std::ops::Sub for FoldingCompatibleExpr<C> {
impl<C: FoldingConfig> core::ops::Sub for FoldingCompatibleExpr<C> {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
Self::Sub(Box::new(self), Box::new(rhs))
}
}

impl<C: FoldingConfig> std::ops::Mul for FoldingCompatibleExpr<C> {
impl<C: FoldingConfig> core::ops::Mul for FoldingCompatibleExpr<C> {
type Output = Self;

fn mul(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -502,23 +502,23 @@ pub enum FoldingExp<C: FoldingConfig> {
Square(Box<Self>),
}

impl<C: FoldingConfig> std::ops::Add for FoldingExp<C> {
impl<C: FoldingConfig> core::ops::Add for FoldingExp<C> {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self::Add(Box::new(self), Box::new(rhs))
}
}

impl<C: FoldingConfig> std::ops::Sub for FoldingExp<C> {
impl<C: FoldingConfig> core::ops::Sub for FoldingExp<C> {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
Self::Sub(Box::new(self), Box::new(rhs))
}
}

impl<C: FoldingConfig> std::ops::Mul for FoldingExp<C> {
impl<C: FoldingConfig> core::ops::Mul for FoldingExp<C> {
type Output = Self;

fn mul(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -773,7 +773,7 @@ pub enum Sign {
Neg,
}

impl std::ops::Neg for Sign {
impl core::ops::Neg for Sign {
type Output = Self;

fn neg(self) -> Self {
Expand Down Expand Up @@ -805,7 +805,7 @@ impl<C: FoldingConfig> Term<C> {
}
}

impl<C: FoldingConfig> std::ops::Mul for &Term<C> {
impl<C: FoldingConfig> core::ops::Mul for &Term<C> {
type Output = Term<C>;

fn mul(self, rhs: Self) -> Self::Output {
Expand All @@ -819,7 +819,7 @@ impl<C: FoldingConfig> std::ops::Mul for &Term<C> {
}
}

impl<C: FoldingConfig> std::ops::Neg for Term<C> {
impl<C: FoldingConfig> core::ops::Neg for Term<C> {
type Output = Self;

fn neg(self) -> Self::Output {
Expand Down
6 changes: 2 additions & 4 deletions folding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ark_ec::AffineRepr;
use ark_ff::{Field, One, Zero};
use ark_poly::{EvaluationDomain, Evaluations, Radix2EvaluationDomain};
use core::{fmt::Debug, hash::Hash, iter::successors};
use error_term::{compute_error, ExtendedEnv};
use expressions::{folding_expression, FoldingColumnTrait, IntegratedFoldingExpr};
use instance_witness::{Foldable, RelaxableInstance, RelaxablePair};
Expand All @@ -28,9 +29,6 @@ use mina_poseidon::FqSponge;
use poly_commitment::{commitment::CommitmentCurve, PolyComm, SRS};
use quadraticization::ExtendedWitnessGenerator;
use std::{
fmt::Debug,
hash::Hash,
iter::successors,
rc::Rc,
sync::atomic::{AtomicUsize, Ordering},
};
Expand Down Expand Up @@ -151,7 +149,7 @@ impl<'a, CF: FoldingConfig> FoldingScheme<'a, CF> {
let (expression, extended_witness_generator, quadraticization_columns) =
folding_expression(constraints);
let zero = <ScalarField<CF>>::zero();
let evals = std::iter::repeat(zero).take(domain.size()).collect();
let evals = core::iter::repeat(zero).take(domain.size()).collect();
let zero_vec = Evaluations::from_vec_and_domain(evals, domain);
let final_expression = expression.clone().final_expression();
let scheme = Self {
Expand Down
2 changes: 1 addition & 1 deletion folding/src/quadraticization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<C: FoldingConfig> FoldingExp<C> {
FoldingExp::Double(exp) => exp.degree(),
FoldingExp::Square(exp) => exp.degree() * 2,
FoldingExp::Add(e1, e2) | FoldingExp::Sub(e1, e2) => {
std::cmp::max(e1.degree(), e2.degree())
core::cmp::max(e1.degree(), e2.degree())
}
FoldingExp::Mul(e1, e2) => e1.degree() + e2.degree(),
FoldingExp::Pow(e, i) => e.degree() * (*i as usize),
Expand Down
4 changes: 2 additions & 2 deletions folding/src/standard_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::{
expressions::FoldingColumnTrait, instance_witness::Witness, FoldingConfig, FoldingEnv,
Instance, Side,
};
use core::{fmt::Debug, hash::Hash, marker::PhantomData, ops::Index};
use derivative::Derivative;
use kimchi::{circuits::gate::CurrOrNext, curve::KimchiCurve};
use memoization::ColumnMemoizer;
use poly_commitment::{self, commitment::CommitmentCurve};
use std::{fmt::Debug, hash::Hash, marker::PhantomData, ops::Index};

#[derive(Clone, Default)]
/// Default type for when you don't need structure
Expand Down Expand Up @@ -199,10 +199,10 @@ where
/// in [FoldingEnv::col]
mod memoization {
use ark_ff::Field;
use core::hash::Hash;
use std::{
cell::{OnceCell, RefCell},
collections::HashMap,
hash::Hash,
sync::atomic::{AtomicUsize, Ordering},
};

Expand Down
3 changes: 1 addition & 2 deletions ivc/src/expr_eval.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::ops::Index;

use crate::plonkish_lang::{CombinableEvals, PlonkishChallenge, PlonkishWitnessGeneric};
use ark_ec::AffineRepr;
use ark_ff::Field;
use ark_poly::{Evaluations, Radix2EvaluationDomain as R2D};
use core::ops::Index;
use folding::{
columns::ExtendedFoldingColumn,
eval_leaf::EvalLeaf,
Expand Down
10 changes: 5 additions & 5 deletions ivc/src/plonkish_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// exactly the plonkish language.
use ark_ff::{FftField, Field, One};
use ark_poly::{Evaluations, Radix2EvaluationDomain as R2D};
use core::ops::Index;
use folding::{instance_witness::Foldable, Alphas, Instance, Witness};
use itertools::Itertools;
use kimchi::{self, circuits::berkeley_columns::BerkeleyChallengeTerm};
Expand All @@ -13,7 +14,6 @@ use poly_commitment::{
PolyComm, SRS,
};
use rayon::iter::{IntoParallelIterator as _, ParallelIterator as _};
use std::ops::Index;
use strum_macros::{EnumCount as EnumCountMacro, EnumIter};

/// Vector field over F. Something like a vector.
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct PlonkishWitnessGeneric<const N_COL: usize, const N_FSEL: usize, F: Fi
// This does not have to be part of the witness... can be a static
// precompiled object.
pub fixed_selectors: GenericWitness<N_FSEL, Evals>,
pub phantom: std::marker::PhantomData<F>,
pub phantom: core::marker::PhantomData<F>,
}

pub type PlonkishWitness<const N_COL: usize, const N_FSEL: usize, F> =
Expand Down Expand Up @@ -119,10 +119,10 @@ impl<G: CommitmentCurve, const N_COL: usize, const N_CHALS: usize, const N_ALPHA
{
fn combine(a: Self, b: Self, challenge: G::ScalarField) -> Self {
Self {
commitments: std::array::from_fn(|i| {
commitments: core::array::from_fn(|i| {
(a.commitments[i] + b.commitments[i].mul(challenge)).into()
}),
challenges: std::array::from_fn(|i| a.challenges[i] + challenge * b.challenges[i]),
challenges: core::array::from_fn(|i| a.challenges[i] + challenge * b.challenges[i]),
alphas: Alphas::combine(a.alphas, b.alphas, challenge),
blinder: a.blinder + challenge * b.blinder,
}
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<G: CommitmentCurve, const N_COL: usize, const N_ALPHAS: usize>
{
pub fn from_witness<
EFqSponge: FqSponge<G::BaseField, G, G::ScalarField>,
Srs: SRS<G> + std::marker::Sync,
Srs: SRS<G> + core::marker::Sync,
>(
w: &GenericWitness<N_COL, Evaluations<G::ScalarField, R2D<G::ScalarField>>>,
fq_sponge: &mut EFqSponge,
Expand Down
6 changes: 3 additions & 3 deletions ivc/src/poseidon_55_0_7_3_2/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ where
}

let mut final_state: [Env::Variable; STATE_SIZE] =
std::array::from_fn(|_| Env::constant(F::zero()));
core::array::from_fn(|_| Env::constant(F::zero()));

for i in 0..NB_FULL_ROUND {
let state: [PoseidonColumn<STATE_SIZE, NB_FULL_ROUND>; STATE_SIZE] = {
if i == 0 {
std::array::from_fn(PoseidonColumn::Input)
core::array::from_fn(PoseidonColumn::Input)
} else {
let prev_round = i - 1;
// Previous outputs are in index 4, 9, and 14 if we have 3 elements
std::array::from_fn(|j| PoseidonColumn::Round(prev_round, j * 5 + 4))
core::array::from_fn(|j| PoseidonColumn::Round(prev_round, j * 5 + 4))
}
};
let round_res = compute_one_round::<F, STATE_SIZE, NB_FULL_ROUND, PARAMETERS, Env>(
Expand Down
6 changes: 3 additions & 3 deletions ivc/src/poseidon_55_0_7_3_2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ mod tests {
impl PoseidonParams<Fp, STATE_SIZE, NB_FULL_ROUND> for PoseidonBN254Parameters {
fn constants(&self) -> [[Fp; STATE_SIZE]; NB_FULL_ROUND] {
let rc = &poseidon_params_55_0_7_3::static_params().round_constants;
std::array::from_fn(|i| std::array::from_fn(|j| Fp::from(rc[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fp::from(rc[i][j])))
}

fn mds(&self) -> [[Fp; STATE_SIZE]; STATE_SIZE] {
let mds = &poseidon_params_55_0_7_3::static_params().mds;
std::array::from_fn(|i| std::array::from_fn(|j| Fp::from(mds[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fp::from(mds[i][j])))
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ mod tests {
let mut witness_env: PoseidonWitnessBuilderEnv = WitnessBuilderEnv::create();

let mut fixed_selectors: [Vec<Fp>; N_FSEL] =
std::array::from_fn(|_| vec![Fp::zero(); 1]);
core::array::from_fn(|_| vec![Fp::zero(); 1]);
// Write constants
{
let rc = PoseidonBN254Parameters.constants();
Expand Down
6 changes: 3 additions & 3 deletions ivc/src/poseidon_55_0_7_3_7/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ where
}

let mut final_state: [Env::Variable; STATE_SIZE] =
std::array::from_fn(|_| Env::constant(F::zero()));
core::array::from_fn(|_| Env::constant(F::zero()));

for i in 0..NB_FULL_ROUND {
let state: [PoseidonColumn<STATE_SIZE, NB_FULL_ROUND>; STATE_SIZE] = {
if i == 0 {
std::array::from_fn(PoseidonColumn::Input)
core::array::from_fn(PoseidonColumn::Input)
} else {
std::array::from_fn(|j| PoseidonColumn::Round(i - 1, j))
core::array::from_fn(|j| PoseidonColumn::Round(i - 1, j))
}
};
let round_res = compute_one_round::<F, STATE_SIZE, NB_FULL_ROUND, PARAMETERS, Env>(
Expand Down
6 changes: 3 additions & 3 deletions ivc/src/poseidon_55_0_7_3_7/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ mod tests {
impl PoseidonParams<Fp, STATE_SIZE, NB_FULL_ROUND> for PoseidonBN254Parameters {
fn constants(&self) -> [[Fp; STATE_SIZE]; NB_FULL_ROUND] {
let rc = &poseidon_params_55_0_7_3::static_params().round_constants;
std::array::from_fn(|i| std::array::from_fn(|j| Fp::from(rc[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fp::from(rc[i][j])))
}

fn mds(&self) -> [[Fp; STATE_SIZE]; STATE_SIZE] {
let mds = &poseidon_params_55_0_7_3::static_params().mds;
std::array::from_fn(|i| std::array::from_fn(|j| Fp::from(mds[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fp::from(mds[i][j])))
}
}

Expand Down Expand Up @@ -118,7 +118,7 @@ mod tests {
let mut witness_env: PoseidonWitnessBuilderEnv = WitnessBuilderEnv::create();

let mut fixed_selectors: [Vec<Fp>; N_FSEL] =
std::array::from_fn(|_| vec![Fp::zero(); 1]);
core::array::from_fn(|_| vec![Fp::zero(); 1]);
// Write constants
{
let rc = PoseidonBN254Parameters.constants();
Expand Down
8 changes: 4 additions & 4 deletions ivc/src/poseidon_8_56_5_3_2/bn254/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ with the commit 85cbccd4266cdb567fd47ffc54c3fa52543c2c51
where the curves have been changed in the script params.sage to use bn254
*/

use std::str::FromStr;
use core::str::FromStr;

use super::interpreter::PoseidonParams;

Expand Down Expand Up @@ -1023,18 +1023,18 @@ pub struct PoseidonBN254Parameters;
impl PoseidonParams<Fr, STATE_SIZE, NB_TOTAL_ROUND> for PoseidonBN254Parameters {
fn constants(&self) -> [[Fr; STATE_SIZE]; NB_TOTAL_ROUND] {
let rc = &static_params().round_constants;
std::array::from_fn(|i| std::array::from_fn(|j| Fr::from(rc[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fr::from(rc[i][j])))
}

fn mds(&self) -> [[Fr; STATE_SIZE]; STATE_SIZE] {
let mds = &static_params().mds;
std::array::from_fn(|i| std::array::from_fn(|j| Fr::from(mds[i][j])))
core::array::from_fn(|i| core::array::from_fn(|j| Fr::from(mds[i][j])))
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
use core::str::FromStr;

use super::{static_params, PlonkSpongeConstantsIVC};
use ark_bn254::Fr;
Expand Down
Loading
Loading