Skip to content

Commit

Permalink
Create a proxy Type to Allow easier derivation
Browse files Browse the repository at this point in the history
Here we create a proxy that lets us derive the encoder and decoder
traits.

This proxy type is not meant to be used, and thus it's not exported,
however it is useful for setting up the correct instance for
ShieldedPartialtransaction

Hopefully this isn't too much noise
  • Loading branch information
mariari committed Aug 8, 2023
1 parent 22e3779 commit 1c3b069
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion taiga_halo2/src/shielded_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use halo2_proofs::plonk::Error;
use pasta_curves::pallas;
use rand::RngCore;
use rustler::NifStruct;
use rustler::{Decoder, Encoder, Env, NifResult, NifStruct, Term};

#[derive(Debug, Clone)]
pub struct ShieldedPartialTransaction {
Expand All @@ -39,6 +39,15 @@ pub struct NoteVPVerifyingInfoSet {
// When the verifier proof is added, we may need to reconsider the structure of `VPVerifyingInfo`
}

// Is easier to derive traits for
#[derive(Debug, Clone, NifStruct)]
#[module = "Taiga.Shielded.PTX"]
struct ShieldedPartialTransactionProxy {
actions: Vec<ActionVerifyingInfo>,
inputs: Vec<NoteVPVerifyingInfoSet>,
outputs: Vec<NoteVPVerifyingInfoSet>,
}

impl ShieldedPartialTransaction {
pub fn build<R: RngCore>(
input_info: [InputNoteProvingInfo; NUM_NOTE],
Expand Down Expand Up @@ -166,6 +175,28 @@ impl ShieldedPartialTransaction {
}
Ok(())
}

// Conversion to the generic length proxy
fn to_proxy(&self) -> ShieldedPartialTransactionProxy {
ShieldedPartialTransactionProxy {
actions: self.actions.to_vec(),
inputs: self.inputs.to_vec(),
outputs: self.outputs.to_vec(),
}
}
}

impl ShieldedPartialTransactionProxy {
fn to_concrete(&self) -> Option<ShieldedPartialTransaction> {
let actions = self.actions.clone().try_into().ok()?;
let inputs = self.inputs.clone().try_into().ok()?;
let outputs = self.outputs.clone().try_into().ok()?;
Some(ShieldedPartialTransaction {
actions,
inputs,
outputs,
})
}
}

impl Executable for ShieldedPartialTransaction {
Expand Down Expand Up @@ -241,6 +272,19 @@ impl BorshDeserialize for ShieldedPartialTransaction {
})
}
}
impl Encoder for ShieldedPartialTransaction {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.to_proxy().encode(env)
}
}
impl<'a> Decoder<'a> for ShieldedPartialTransaction {
fn decode(term: Term<'a>) -> NifResult<Self> {
let val: ShieldedPartialTransactionProxy = Decoder::decode(term)?;
val.to_concrete()
.ok_or(rustler::Error::RaiseAtom("Could not decode proxy"))
}
}

impl ActionVerifyingInfo {
pub fn create<R: RngCore>(action_info: ActionInfo, mut rng: R) -> Result<Self, Error> {
let (action_instance, circuit) = action_info.build();
Expand Down

0 comments on commit 1c3b069

Please sign in to comment.