Skip to content

Commit

Permalink
partial_fulfillment_intent::swap::Swap: introduce fill() method
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed Oct 16, 2023
1 parent 12c14d4 commit f7a7336
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
34 changes: 6 additions & 28 deletions taiga_halo2/src/circuit/vp_examples/partial_fulfillment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ mod tests {
token::{Token, TokenAuthorization},
};
use crate::constant::VP_CIRCUIT_PARAMS_SIZE;
use crate::nullifier::{Nullifier, NullifierKeyContainer};
use halo2_proofs::arithmetic::Field;
use halo2_proofs::dev::MockProver;
use rand::rngs::OsRng;
Expand All @@ -202,14 +201,10 @@ mod tests {
let sk = pallas::Scalar::random(&mut rng);
let auth = TokenAuthorization::from_sk_vk(&sk, &COMPRESSED_TOKEN_AUTH_VK);

let sell = {
let sell = Token::new("token1".to_string(), 2u64);
let rho = Nullifier::random(&mut rng);
let nk = NullifierKeyContainer::random_key(&mut rng);
sell.create_random_token_note(&mut rng, rho, nk, &auth)
};
let sell = Token::new("token1".to_string(), 2u64);
let buy = Token::new("token2".to_string(), 4u64);
let swap = Swap { sell, buy, auth };

let swap = Swap::random(&mut rng, sell, buy, auth);
let intent_note = swap.create_intent_note(&mut rng);

(swap, intent_note)
Expand Down Expand Up @@ -252,12 +247,7 @@ mod tests {
Note::random_padding_output_note(&mut rng, input_padding_note.get_nf().unwrap());

let bob_sell = swap.buy.clone();
let bob_sold_note = bob_sell.create_random_token_note(
&mut rng,
swap.sell.note().rho,
swap.sell.note().nk_container,
&swap.auth,
);
let (bob_sold_note, _) = swap.fill(&mut rng, bob_sell);

let input_notes = [intent_note, input_padding_note];
let output_notes = [*bob_sold_note.note(), output_padding_note];
Expand Down Expand Up @@ -287,21 +277,9 @@ mod tests {
let input_notes = [intent_note, input_padding_note];

let bob_sell = Token::new(swap.buy.name().inner().to_string(), 2u64);
let bob_sold_note = bob_sell.create_random_token_note(
&mut rng,
swap.sell.note().rho,
swap.sell.note().nk_container,
&swap.auth,
);
let (bob_sold_note, bob_returned_note) = swap.fill(&mut rng, bob_sell);

let bob_return = Token::new(swap.sell.token_name().inner().to_string(), 1u64);
let bob_returned_note = bob_return.create_random_token_note(
&mut rng,
swap.sell.note().rho,
swap.sell.note().nk_container,
&swap.auth,
);
let output_notes = [*bob_sold_note.note(), *bob_returned_note.note()];
let output_notes = [*bob_sold_note.note(), *bob_returned_note.unwrap().note()];

let circuit = PartialFulfillmentIntentValidityPredicateCircuit {
owned_note_pub_id: intent_note.get_nf().unwrap().inner(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,60 @@ pub(super) struct Swap {
}

impl Swap {
pub(super) fn random(
mut rng: impl RngCore,
sell: Token,
buy: Token,
auth: TokenAuthorization,
) -> Self {
assert_eq!(buy.value() % sell.value(), 0);

let sell = {
let rho = Nullifier::random(&mut rng);
let nk = NullifierKeyContainer::random_key(&mut rng);
sell.create_random_token_note(&mut rng, rho, nk, &auth)
};

Swap { sell, buy, auth }
}

/// Either:
/// - completely fills the swap using a single `TokenNote`, or
/// - partially fills the swap, producing a `TokenNote` and a
/// returned note.
pub(super) fn fill(
&self,
mut rng: impl RngCore,
offer: Token,
) -> (TokenNote, Option<TokenNote>) {
assert_eq!(offer.name(), self.buy.name());

let ratio = self.buy.value() / self.sell.value;
assert_eq!(offer.value() % ratio, 0);

let offer_note = offer.create_random_token_note(
&mut rng,
self.sell.note().rho,
self.sell.note().nk_container,
&self.auth,
);

let mut returned_note = None;
if offer.value() < self.buy.value() {
let filled_value = offer.value() / ratio;
let returned_value = self.sell.value - filled_value;
let returned_token =
Token::new(self.sell.token_name().inner().to_string(), returned_value);
returned_note = Some(returned_token.create_random_token_note(
&mut rng,
self.sell.note().rho,
self.sell.note().nk_container,
&self.auth,
));
}
(offer_note, returned_note)
}

pub(super) fn encode_app_data_static(&self) -> pallas::Base {
poseidon_hash_n([
self.sell.encode_name(),
Expand Down
2 changes: 1 addition & 1 deletion taiga_halo2/src/circuit/vp_examples/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ lazy_static! {
pub static ref COMPRESSED_TOKEN_VK: pallas::Base = TOKEN_VK.get_compressed();
}

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TokenName(String);

impl TokenName {
Expand Down

0 comments on commit f7a7336

Please sign in to comment.