Skip to content

Commit

Permalink
Arrabiata: instantiate constraints environment with ArrabiataCurve
Browse files Browse the repository at this point in the history
  • Loading branch information
dannywillems committed Oct 17, 2024
1 parent 9a71cc8 commit a3591cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
29 changes: 16 additions & 13 deletions arrabiata/src/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{columns::Column, interpreter::InterpreterEnv};
use crate::{
columns::{Gadget, E},
curve::ArrabiataCurve,
interpreter::{self, Instruction, Side},
MAX_DEGREE, NUMBER_OF_COLUMNS, NUMBER_OF_PUBLIC_INPUTS,
};
use ark_ff::{Field, PrimeField};
use kimchi::circuits::{
expr::{ConstantTerm::Literal, Expr, ExprInner, Operations, Variable},
gate::CurrOrNext,
Expand All @@ -14,23 +14,26 @@ use num_bigint::BigInt;
use o1_utils::FieldHelpers;

#[derive(Clone, Debug)]
pub struct Env<Fp: Field> {
pub poseidon_mds: Vec<Vec<Fp>>,
pub struct Env<C: ArrabiataCurve> {
pub poseidon_mds: Vec<Vec<C::ScalarField>>,
/// The parameter a is the coefficients of the elliptic curve in affine
/// coordinates.
// FIXME: this is ugly. Let use the curve as a parameter. Only lazy for now.
pub a: BigInt,
pub idx_var: usize,
pub idx_var_next_row: usize,
pub idx_var_pi: usize,
pub constraints: Vec<E<Fp>>,
pub constraints: Vec<E<C::ScalarField>>,
pub activated_gadget: Option<Gadget>,
}

impl<Fp: PrimeField> Env<Fp> {
pub fn new(poseidon_mds: Vec<Vec<Fp>>, a: BigInt) -> Self {
impl<C: ArrabiataCurve> Env<C> {
pub fn new(poseidon_mds: Vec<Vec<C::ScalarField>>, a: BigInt) -> Self {
// This check might not be useful
assert!(a < Fp::modulus_biguint().into(), "a is too large");
assert!(
a < C::ScalarField::modulus_biguint().into(),
"a is too large"
);
Self {
poseidon_mds,
a,
Expand All @@ -48,10 +51,10 @@ impl<Fp: PrimeField> Env<Fp> {
/// proof.
/// The constraint environment must be instantiated only once, at the last step
/// of the computation.
impl<Fp: PrimeField> InterpreterEnv for Env<Fp> {
impl<C: ArrabiataCurve> InterpreterEnv for Env<C> {
type Position = (Column, CurrOrNext);

type Variable = E<Fp>;
type Variable = E<C::ScalarField>;

fn allocate(&mut self) -> Self::Position {
assert!(self.idx_var < NUMBER_OF_COLUMNS, "Maximum number of columns reached ({NUMBER_OF_COLUMNS}), increase the number of columns");
Expand Down Expand Up @@ -81,7 +84,7 @@ impl<Fp: PrimeField> InterpreterEnv for Env<Fp> {

fn constant(&self, value: BigInt) -> Self::Variable {
let v = value.to_biguint().unwrap();
let v = Fp::from_biguint(&v).unwrap();
let v = C::ScalarField::from_biguint(&v).unwrap();
let v_inner = Operations::from(Literal(v));
Self::Variable::constant(v_inner)
}
Expand Down Expand Up @@ -304,7 +307,7 @@ impl<Fp: PrimeField> InterpreterEnv for Env<Fp> {
}
}

impl<F: PrimeField> Env<F> {
impl<C: ArrabiataCurve> Env<C> {
/// Get all the constraints for the IVC circuit, only.
///
/// The following gadgets are used in the IVC circuit:
Expand All @@ -317,7 +320,7 @@ impl<F: PrimeField> Env<F> {
// the computation of the challenges.
// FIXME: add a test checking that whatever the value given in parameter of
// the gadget, the constraints are the same
pub fn get_all_constraints_for_ivc(&self) -> Vec<E<F>> {
pub fn get_all_constraints_for_ivc(&self) -> Vec<E<C::ScalarField>> {
// Copying the instance we got in parameter, and making it mutable to
// avoid modifying the original instance.
let mut env = self.clone();
Expand Down Expand Up @@ -354,7 +357,7 @@ impl<F: PrimeField> Env<F> {
// FIXME: the application should be given as an argument to handle Rust
// zkApp. It is only for the PoC.
// FIXME: the selectors are not added for now.
pub fn get_all_constraints(&self) -> Vec<E<F>> {
pub fn get_all_constraints(&self) -> Vec<E<C::ScalarField>> {
let mut constraints = self.get_all_constraints_for_ivc();

// Copying the instance we got in parameter, and making it mutable to
Expand Down
18 changes: 9 additions & 9 deletions arrabiata/tests/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ use arrabiata::{
poseidon_3_60_0_5_5_fp, poseidon_3_60_0_5_5_fq, MAX_DEGREE, NUMBER_OF_COLUMNS,
NUMBER_OF_PUBLIC_INPUTS,
};
use mina_curves::pasta::fields::{Fp, Fq};
use mina_curves::pasta::{curves::vesta::Vesta, fields::Fp, Pallas};
use mvpoly::{monomials::Sparse, MVPoly};
use num_bigint::BigInt;
use std::collections::HashMap;

fn helper_compute_constraints_gadget(instr: Instruction, exp_constraints: usize) {
let mut constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};

interpreter::run_ivc(&mut constraints_fp, instr);
assert_eq!(constraints_fp.constraints.len(), exp_constraints);

let mut constraints_fq = {
let poseidon_mds = poseidon_3_60_0_5_5_fq::static_params().mds.clone();
constraints::Env::<Fq>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Pallas>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};
interpreter::run_ivc(&mut constraints_fq, instr);
assert_eq!(constraints_fq.constraints.len(), exp_constraints);
Expand All @@ -31,7 +31,7 @@ fn helper_compute_constraints_gadget(instr: Instruction, exp_constraints: usize)
fn helper_check_expected_degree_constraints(instr: Instruction, exp_degrees: HashMap<u64, usize>) {
let mut constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};
interpreter::run_ivc(&mut constraints_fp, instr);

Expand Down Expand Up @@ -63,7 +63,7 @@ fn helper_gadget_number_of_columns_used(
) {
let mut constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};
interpreter::run_ivc(&mut constraints_fp, instr);

Expand All @@ -77,7 +77,7 @@ fn helper_gadget_number_of_columns_used(
fn helper_check_gadget_activated(instr: Instruction, gadget: Gadget) {
let mut constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};
interpreter::run_ivc(&mut constraints_fp, instr);

Expand Down Expand Up @@ -121,7 +121,7 @@ fn test_gadget_elliptic_curve_addition() {
fn test_ivc_total_number_of_constraints_ivc() {
let constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};

let constraints = constraints_fp.get_all_constraints_for_ivc();
Expand All @@ -132,7 +132,7 @@ fn test_ivc_total_number_of_constraints_ivc() {
fn test_degree_of_constraints_ivc() {
let constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};

let constraints = constraints_fp.get_all_constraints_for_ivc();
Expand Down Expand Up @@ -173,7 +173,7 @@ fn test_gadget_elliptic_curve_scaling() {
fn test_integration_with_mvpoly_to_compute_cross_terms() {
let constraints_fp = {
let poseidon_mds = poseidon_3_60_0_5_5_fp::static_params().mds.clone();
constraints::Env::<Fp>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
constraints::Env::<Vesta>::new(poseidon_mds.to_vec(), BigInt::from(0_usize))
};

let constraints = constraints_fp.get_all_constraints_for_ivc();
Expand Down

0 comments on commit a3591cb

Please sign in to comment.