From 1c3b069a97875a60dde1b3abecd0719f697e5022 Mon Sep 17 00:00:00 2001 From: mariari Date: Tue, 8 Aug 2023 06:38:28 +0800 Subject: [PATCH] Create a proxy Type to Allow easier derivation 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 --- taiga_halo2/src/shielded_ptx.rs | 46 ++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/taiga_halo2/src/shielded_ptx.rs b/taiga_halo2/src/shielded_ptx.rs index 0af50c4b..12878b96 100644 --- a/taiga_halo2/src/shielded_ptx.rs +++ b/taiga_halo2/src/shielded_ptx.rs @@ -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 { @@ -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, + inputs: Vec, + outputs: Vec, +} + impl ShieldedPartialTransaction { pub fn build( input_info: [InputNoteProvingInfo; NUM_NOTE], @@ -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 { + 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 { @@ -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 { + let val: ShieldedPartialTransactionProxy = Decoder::decode(term)?; + val.to_concrete() + .ok_or(rustler::Error::RaiseAtom("Could not decode proxy")) + } +} + impl ActionVerifyingInfo { pub fn create(action_info: ActionInfo, mut rng: R) -> Result { let (action_instance, circuit) = action_info.build();