Skip to content

Commit

Permalink
Update to arkworks 0.4.2: arrabiata,folding,ivc,msm,o1vm
Browse files Browse the repository at this point in the history
  • Loading branch information
volhovm committed Sep 20, 2024
1 parent 454fdd5 commit bca9769
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 148 deletions.
6 changes: 3 additions & 3 deletions arrabiata/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A prover for the Nova recursive SNARK.
use crate::proof::Proof;
use ark_ec::AffineCurve;
use ark_ec::AffineRepr;
use ark_ff::PrimeField;

use crate::witness::Env;
Expand All @@ -12,8 +12,8 @@ use crate::witness::Env;
pub fn prove<
Fp: PrimeField,
Fq: PrimeField,
E1: AffineCurve<ScalarField = Fp, BaseField = Fq>,
E2: AffineCurve<ScalarField = Fq, BaseField = Fp>,
E1: AffineRepr<ScalarField = Fp, BaseField = Fq>,
E2: AffineRepr<ScalarField = Fq, BaseField = Fp>,
>(
_env: &Env<Fp, Fq, E1, E2>,
) -> Result<Proof, String> {
Expand Down
18 changes: 9 additions & 9 deletions arrabiata/src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ec::{AffineCurve, SWModelParameters};
use ark_ec::{models::short_weierstrass::SWCurveConfig, AffineRepr};
use ark_ff::PrimeField;
use ark_poly::Evaluations;
use kimchi::circuits::domains::EvaluationDomains;
Expand Down Expand Up @@ -32,8 +32,8 @@ pub const IVC_STARTING_INSTRUCTION: Instruction = Instruction::Poseidon(0);
pub struct Env<
Fp: PrimeField,
Fq: PrimeField,
E1: AffineCurve<ScalarField = Fp, BaseField = Fq>,
E2: AffineCurve<ScalarField = Fq, BaseField = Fp>,
E1: AffineRepr<ScalarField = Fp, BaseField = Fq>,
E2: AffineRepr<ScalarField = Fq, BaseField = Fp>,
> {
// ----------------
// Setup related (domains + SRS)
Expand Down Expand Up @@ -183,8 +183,8 @@ impl<
E2: CommitmentCurve<ScalarField = Fq, BaseField = Fp>,
> InterpreterEnv for Env<Fp, Fq, E1, E2>
where
<E1::Params as ark_ec::ModelParameters>::BaseField: PrimeField,
<E2::Params as ark_ec::ModelParameters>::BaseField: PrimeField,
<E1::Params as ark_ec::CurveConfig>::BaseField: PrimeField,
<E2::Params as ark_ec::CurveConfig>::BaseField: PrimeField,
{
type Position = Column;

Expand Down Expand Up @@ -787,8 +787,8 @@ impl<
sponge_e2: [BigInt; 3],
) -> Self {
{
assert!(Fp::size_in_bits() <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fp is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}");
assert!(Fq::size_in_bits() <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fq is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}");
assert!(Fp::MODULUS_BIT_SIZE <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fp is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}");
assert!(Fq::MODULUS_BIT_SIZE <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fq is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}");
let modulus_fp = Fp::modulus_biguint();
assert!(
(modulus_fp - BigUint::from(1_u64)).gcd(&BigUint::from(POSEIDON_ALPHA))
Expand Down Expand Up @@ -843,10 +843,10 @@ impl<

// Default set to the blinders. Using double to make the EC scaling happy.
let previous_commitments_e1: Vec<PolyComm<E1>> = (0..NUMBER_OF_COLUMNS)
.map(|_| PolyComm::new(vec![srs_e1.h + srs_e1.h]))
.map(|_| PolyComm::new(vec![(srs_e1.h + srs_e1.h).into()]))
.collect();
let previous_commitments_e2: Vec<PolyComm<E2>> = (0..NUMBER_OF_COLUMNS)
.map(|_| PolyComm::new(vec![srs_e2.h + srs_e2.h]))
.map(|_| PolyComm::new(vec![(srs_e2.h + srs_e2.h).into()]))
.collect();
// FIXME: zero will not work.
let ivc_accumulator_e1: Vec<PolyComm<E1>> = (0..NUMBER_OF_COLUMNS)
Expand Down
28 changes: 15 additions & 13 deletions arrabiata/tests/witness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use ark_ec::{short_weierstrass_jacobian::GroupAffine, ProjectiveCurve, SWModelParameters};
use ark_ec::{
models::short_weierstrass::{Affine, SWCurveConfig},
AffineRepr, Group,
};
use ark_ff::{PrimeField, UniformRand};
use arrabiata::{
interpreter::{self, Instruction, InterpreterEnv},
Expand Down Expand Up @@ -29,20 +32,18 @@ impl SpongeConstants for PlonkSpongeConstants {
const PERM_INITIAL_ARK: bool = false;
}

fn helper_generate_random_elliptic_curve_point<RNG, P: SWModelParameters>(
rng: &mut RNG,
) -> GroupAffine<P>
fn helper_generate_random_elliptic_curve_point<RNG, P: SWCurveConfig>(rng: &mut RNG) -> Affine<P>
where
P::BaseField: PrimeField,
RNG: RngCore + CryptoRng,
{
let p1_x = P::BaseField::rand(rng);
let mut p1: Option<GroupAffine<P>> = GroupAffine::<P>::get_point_from_x(p1_x, false);
let mut p1: Option<Affine<P>> = Affine::<P>::get_point_from_x_unchecked(p1_x, false);
while p1.is_none() {
let p1_x = P::BaseField::rand(rng);
p1 = GroupAffine::<P>::get_point_from_x(p1_x, false);
p1 = Affine::<P>::get_point_from_x_unchecked(p1_x, false);
}
let p1: GroupAffine<P> = p1.unwrap().scale_by_cofactor().into();
let p1: Affine<P> = p1.unwrap().mul_by_cofactor_to_group().into();
p1
}

Expand Down Expand Up @@ -135,7 +136,7 @@ fn test_unit_witness_elliptic_curve_addition() {
assert_eq!(env.current_iteration, 0);
let (exp_x3, exp_y3) = {
let res: Pallas =
env.ivc_accumulator_e2[0].elems[0] + env.previous_commitments_e2[0].elems[0];
(env.ivc_accumulator_e2[0].elems[0] + env.previous_commitments_e2[0].elems[0]).into();
let (x3, y3) = res.to_coordinates().unwrap();
(
x3.to_biguint().to_bigint().unwrap(),
Expand All @@ -154,7 +155,7 @@ fn test_unit_witness_elliptic_curve_addition() {
assert_eq!(env.current_iteration, 1);
let (exp_x3, exp_y3) = {
let res: Vesta =
env.ivc_accumulator_e1[0].elems[0] + env.previous_commitments_e1[0].elems[0];
(env.ivc_accumulator_e1[0].elems[0] + env.previous_commitments_e1[0].elems[0]).into();
let (x3, y3) = res.to_coordinates().unwrap();
(
x3.to_biguint().to_bigint().unwrap(),
Expand All @@ -173,7 +174,7 @@ fn test_unit_witness_elliptic_curve_addition() {
assert_eq!(env.current_iteration, 2);
let (exp_x3, exp_y3) = {
let res: Pallas =
env.ivc_accumulator_e2[0].elems[0] + env.previous_commitments_e2[0].elems[0];
(env.ivc_accumulator_e2[0].elems[0] + env.previous_commitments_e2[0].elems[0]).into();
let (x3, y3) = res.to_coordinates().unwrap();
(
x3.to_biguint().to_bigint().unwrap(),
Expand Down Expand Up @@ -210,7 +211,7 @@ fn test_witness_double_elliptic_curve_point() {
let p1_y = env.write_column(pos_y, p1.y.to_biguint().into());
let (res_x, res_y) = env.double_ec_point(pos_x, pos_y, p1_x, p1_y);

let exp_res: Pallas = p1 + p1;
let exp_res: Pallas = (p1 + p1).into();
let exp_x: BigInt = exp_res.x.to_biguint().into();
let exp_y: BigInt = exp_res.y.to_biguint().into();

Expand Down Expand Up @@ -253,8 +254,9 @@ where
};

let p1_proj: ProjectivePallas = p1.into();
let p1_r: Pallas = p1_proj.mul(r.clone().to_u64_digits().1).into();
let exp_res: Pallas = p1_r + env.srs_e2.h;
// @volhovm TODO check if mul_bigint is what was intended
let p1_r: Pallas = p1_proj.mul_bigint(r.clone().to_u64_digits().1).into();
let exp_res: Pallas = (p1_r + env.srs_e2.h).into();

let exp_x: BigInt = exp_res.x.to_biguint().into();
let exp_y: BigInt = exp_res.y.to_biguint().into();
Expand Down
40 changes: 20 additions & 20 deletions folding/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
instance_witness::Instance,
ExpExtension, FoldingConfig, Radix2EvaluationDomain, RelaxedInstance, RelaxedWitness,
};
use ark_ec::AffineCurve;
use ark_ec::AffineRepr;
use ark_ff::{Field, Zero};
use ark_poly::Evaluations;
use kimchi::circuits::{expr::Variable, gate::CurrOrNext};
Expand Down Expand Up @@ -85,33 +85,33 @@ pub trait Provide<C: FoldingConfig> {
fn resolve(
&self,
inner: FoldingCompatibleExprInner<C>,
domain: Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
) -> Vec<<C::Curve as AffineCurve>::ScalarField>;
domain: Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
) -> Vec<<C::Curve as AffineRepr>::ScalarField>;
}

impl<C: FoldingConfig> Provide<C> for Provider<C>
where
C::Witness: Index<
C::Column,
Output = Evaluations<
<C::Curve as AffineCurve>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
<C::Curve as AffineRepr>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
>,
>,
C::Witness: Index<
C::Selector,
Output = Evaluations<
<C::Curve as AffineCurve>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
<C::Curve as AffineRepr>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
>,
>,
C::Instance: Index<C::Challenge, Output = <C::Curve as AffineCurve>::ScalarField>,
C::Instance: Index<C::Challenge, Output = <C::Curve as AffineRepr>::ScalarField>,
{
fn resolve(
&self,
inner: FoldingCompatibleExprInner<C>,
domain: Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
) -> Vec<<C::Curve as AffineCurve>::ScalarField> {
domain: Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
) -> Vec<<C::Curve as AffineRepr>::ScalarField> {
let domain_size = domain.size as usize;
match inner {
FoldingCompatibleExprInner::Constant(c) => {
Expand Down Expand Up @@ -145,24 +145,24 @@ where
C::Witness: Index<
C::Column,
Output = Evaluations<
<C::Curve as AffineCurve>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
<C::Curve as AffineRepr>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
>,
>,
C::Witness: Index<
C::Selector,
Output = Evaluations<
<C::Curve as AffineCurve>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
<C::Curve as AffineRepr>::ScalarField,
Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
>,
>,
C::Instance: Index<C::Challenge, Output = <C::Curve as AffineCurve>::ScalarField>,
C::Instance: Index<C::Challenge, Output = <C::Curve as AffineRepr>::ScalarField>,
{
fn resolve(
&self,
inner: FoldingCompatibleExprInner<C>,
domain: Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
) -> Vec<<C::Curve as AffineCurve>::ScalarField> {
domain: Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
) -> Vec<<C::Curve as AffineRepr>::ScalarField> {
match inner {
FoldingCompatibleExprInner::Extensions(ext) => match ext {
ExpExtension::U => {
Expand Down Expand Up @@ -204,8 +204,8 @@ pub trait Checker<C: FoldingConfig>: Provide<C> {
fn check_rec(
&self,
exp: FoldingCompatibleExpr<C>,
domain: Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
) -> Vec<<C::Curve as AffineCurve>::ScalarField> {
domain: Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
) -> Vec<<C::Curve as AffineRepr>::ScalarField> {
let e2 = exp.clone();
let res = match exp {
FoldingCompatibleExpr::Atom(inner) => self.resolve(inner, domain),
Expand Down Expand Up @@ -249,7 +249,7 @@ pub trait Checker<C: FoldingConfig>: Provide<C> {
fn check(
&self,
exp: &FoldingCompatibleExpr<C>,
domain: Radix2EvaluationDomain<<C::Curve as AffineCurve>::ScalarField>,
domain: Radix2EvaluationDomain<<C::Curve as AffineRepr>::ScalarField>,
) {
let res = self.check_rec(exp.clone(), domain);
for (i, row) in res.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions folding/src/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! scheme as they describe the basic expressiveness of the system.
use crate::FoldingConfig;
use ark_ec::AffineCurve;
use ark_ec::AffineRepr;
use derivative::Derivative;
use kimchi::circuits::expr::Variable;

Expand All @@ -28,7 +28,7 @@ pub enum ExtendedFoldingColumn<C: FoldingConfig> {
/// The error term introduced in the "relaxed" instance.
Error,
/// A constant value in our expression
Constant(<C::Curve as AffineCurve>::ScalarField),
Constant(<C::Curve as AffineRepr>::ScalarField),
/// A challenge used by the PIOP or the folding scheme.
Challenge(C::Challenge),
/// A list of randomizer to combine expressions
Expand Down
18 changes: 9 additions & 9 deletions folding/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ use crate::{
quadraticization::{quadraticize, ExtendedWitnessGenerator, Quadraticized},
FoldingConfig, ScalarField,
};
use ark_ec::AffineCurve;
use ark_ec::AffineRepr;
use ark_ff::One;
use derivative::Derivative;
use itertools::Itertools;
Expand Down Expand Up @@ -366,7 +366,7 @@ pub enum ExpExtension<C: FoldingConfig> {
Debug(bound = "C: FoldingConfig")
)]
pub enum FoldingCompatibleExprInner<C: FoldingConfig> {
Constant(<C::Curve as AffineCurve>::ScalarField),
Constant(<C::Curve as AffineRepr>::ScalarField),
Challenge(C::Challenge),
Cell(Variable<C::Column>),
/// extra nodes created by folding, should not be passed to folding
Expand Down Expand Up @@ -748,7 +748,7 @@ impl<C: FoldingConfig> FoldingExp<C> {
Mul(e1, e2)
}
// TODO: Replace with `Pow`
FoldingExp::Pow(_, 0) => Atom(Constant(<C::Curve as AffineCurve>::ScalarField::one())),
FoldingExp::Pow(_, 0) => Atom(Constant(<C::Curve as AffineRepr>::ScalarField::one())),
FoldingExp::Pow(e, 1) => e.into_compatible(),
FoldingExp::Pow(e, i) => {
let e = e.into_compatible();
Expand Down Expand Up @@ -930,7 +930,7 @@ pub fn extract_terms<C: FoldingConfig>(exp: FoldingExp<C>) -> Box<dyn Iterator<I
Pow(_, 0) => Box::new(
[Term {
exp: FoldingExp::Atom(ExtendedFoldingColumn::Constant(
<C::Curve as AffineCurve>::ScalarField::one(),
<C::Curve as AffineRepr>::ScalarField::one(),
)),
sign: Sign::Pos,
}]
Expand Down Expand Up @@ -1003,7 +1003,7 @@ pub fn folding_expression<C: FoldingConfig>(

impl<F, Config: FoldingConfig> From<ConstantExprInner<F>> for FoldingCompatibleExprInner<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Curve: AffineRepr<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
{
fn from(expr: ConstantExprInner<F>) -> Self {
Expand All @@ -1024,7 +1024,7 @@ where
impl<F, Col, Config: FoldingConfig<Column = Col>> From<ExprInner<ConstantExprInner<F>, Col>>
for FoldingCompatibleExprInner<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Curve: AffineRepr<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
{
// TODO: check if this needs some special treatment for Extensions
Expand All @@ -1045,7 +1045,7 @@ where
impl<F, Col, Config: FoldingConfig<Column = Col>>
From<Operations<ExprInner<ConstantExprInner<F>, Col>>> for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Curve: AffineRepr<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
{
fn from(expr: Operations<ExprInner<ConstantExprInner<F>, Col>>) -> Self {
Expand All @@ -1071,7 +1071,7 @@ where
impl<F, Col, Config: FoldingConfig<Column = Col>> From<Operations<ConstantExprInner<F>>>
for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Curve: AffineRepr<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
{
fn from(expr: Operations<ConstantExprInner<F>>) -> Self {
Expand All @@ -1097,7 +1097,7 @@ impl<F, Col, Config: FoldingConfig<Column = Col>>
From<Operations<ExprInner<Operations<ConstantExprInner<F>>, Col>>>
for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Curve: AffineRepr<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
{
fn from(expr: Operations<ExprInner<Operations<ConstantExprInner<F>>, Col>>) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions folding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! [expressions].
// TODO: the documentation above might need more descriptions.

use ark_ec::AffineCurve;
use ark_ec::AffineRepr;
use ark_ff::{Field, One, Zero};
use ark_poly::{EvaluationDomain, Evaluations, Radix2EvaluationDomain};
use error_term::{compute_error, ExtendedEnv};
Expand Down Expand Up @@ -59,8 +59,8 @@ pub mod checker;
// complexity for clippy.
// Should be moved into FoldingConfig, but associated type defaults are unstable
// at the moment.
type ScalarField<C> = <<C as FoldingConfig>::Curve as AffineCurve>::ScalarField;
type BaseField<C> = <<C as FoldingConfig>::Curve as AffineCurve>::BaseField;
type ScalarField<C> = <<C as FoldingConfig>::Curve as AffineRepr>::ScalarField;
type BaseField<C> = <<C as FoldingConfig>::Curve as AffineRepr>::BaseField;

// 'static seems to be used for expressions. Can we get rid of it?
pub trait FoldingConfig: Debug + 'static {
Expand Down Expand Up @@ -91,7 +91,7 @@ pub trait FoldingConfig: Debug + 'static {
type Structure: Clone;

type Env: FoldingEnv<
<Self::Curve as AffineCurve>::ScalarField,
<Self::Curve as AffineRepr>::ScalarField,
Self::Instance,
Self::Witness,
Self::Column,
Expand Down
Loading

0 comments on commit bca9769

Please sign in to comment.