Skip to content

Commit

Permalink
Switch to storing PositionVar in bsod var
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Apr 19, 2024
1 parent 30ebbc0 commit 91ea96e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
51 changes: 12 additions & 39 deletions crates/core/component/dex/src/batch_swap_output_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use ark_r1cs_std::{
select::CondSelectGadget,
};
use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError};
use decaf377::{r1cs::FqVar, Fq};
use decaf377::Fq;
use penumbra_proto::{penumbra::core::component::dex::v1 as pb, DomainType};
use penumbra_tct::Position;
use penumbra_tct::{r1cs::PositionVar, Position};
use serde::{Deserialize, Serialize};

use penumbra_num::fixpoint::{bit_constrain, U128x128, U128x128Var};
use penumbra_num::fixpoint::{U128x128, U128x128Var};
use penumbra_num::{Amount, AmountVar};

use crate::TradingPairVar;
Expand Down Expand Up @@ -130,19 +130,9 @@ impl ToConstraintField<Fq> for BatchSwapOutputData {
.expect("trading_pair is a Bls12-377 field member"),
);
public_inputs.extend(
Fq::from(self.epoch_starting_height)
self.sct_position_prefix
.to_field_elements()
.expect("Fq types are Bls12-377 field members"),
);
public_inputs.extend(
Fq::from(self.sct_position_prefix.epoch())
.to_field_elements()
.expect("Fq types are Bls12-377 field members"),
);
public_inputs.extend(
Fq::from(self.sct_position_prefix.block())
.to_field_elements()
.expect("Fq types are Bls12-377 field members"),
.expect("Position types are Bls12-377 field members"),
);
Some(public_inputs)
}
Expand All @@ -156,8 +146,7 @@ pub struct BatchSwapOutputDataVar {
pub unfilled_1: U128x128Var,
pub unfilled_2: U128x128Var,
pub trading_pair: TradingPairVar,
pub epoch: FqVar,
pub block: FqVar,
pub sct_position_prefix: PositionVar,
}

impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
Expand All @@ -181,28 +170,13 @@ impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
let unfilled_1 = U128x128Var::new_variable(cs.clone(), || Ok(unfilled_1_fixpoint), mode)?;
let unfilled_2_fixpoint: U128x128 = output_data.unfilled_2.into();
let unfilled_2 = U128x128Var::new_variable(cs.clone(), || Ok(unfilled_2_fixpoint), mode)?;
let epoch = FqVar::new_variable(
cs.clone(),
|| Ok(Fq::from(output_data.sct_position_prefix.epoch())),
mode,
)?;
let block = FqVar::new_variable(
cs.clone(),
|| Ok(Fq::from(output_data.sct_position_prefix.block())),
mode,
)?;
// Check that epoch and block are 16 bits
let _ = bit_constrain(epoch.clone(), 16);
let _ = bit_constrain(block.clone(), 16);
let trading_pair = TradingPairVar::new_variable_unchecked(
cs.clone(),
|| Ok(output_data.trading_pair),
mode,
)?;
let epoch_starting_height =
FqVar::new_variable(cs, || Ok(Fq::from(output_data.epoch_starting_height)), mode)?;
// Check the epoch starting height is 64 bits
let _ = bit_constrain(epoch_starting_height.clone(), 64);
let sct_position_prefix =
PositionVar::new_variable(cs.clone(), || Ok(output_data.sct_position_prefix), mode)?;

Ok(Self {
delta_1,
Expand All @@ -212,8 +186,7 @@ impl AllocVar<BatchSwapOutputData, Fq> for BatchSwapOutputDataVar {
unfilled_1,
unfilled_2,
trading_pair,
epoch,
block,
sct_position_prefix,
})
}
}
Expand Down Expand Up @@ -454,10 +427,10 @@ mod tests {
lambda_2: Amount::from(1u32),
unfilled_1: Amount::from(1u32),
unfilled_2: Amount::from(1u32),
height: 1,
height: 0,
trading_pair,
epoch_starting_height: 1,
sct_position_prefix: Position::default(),
epoch_starting_height: 0,
sct_position_prefix: 0u64.into(),
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/core/component/dex/src/swap_claim/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ impl ConstraintSynthesizer<Fq> for SwapClaimCircuit {

// Validate the swap commitment's height matches the output data's height (i.e. the clearing price height).
output_data_var
.block
.sct_position_prefix
.block()?
.enforce_equal(&position_var.block()?)?;
output_data_var
.epoch
.sct_position_prefix
.epoch()?
.enforce_equal(&position_var.epoch()?)?;

// Validate that the output data's trading pair matches the note commitment's trading pair.
Expand Down

0 comments on commit 91ea96e

Please sign in to comment.