Skip to content

Commit

Permalink
Add Nif derivication encodings for types that can be derieved
Browse files Browse the repository at this point in the history
A couple of strategies were chosen to encode terms.

Structs are often given for Taiga terms, these correspond to maps in
BEAM languages.

Further any struct that is just a struct over an Array got a tuple
specification as they are just wrapper types

This commit Tries to minimze the amount of manual derived terms,
however they do appear in trying to derive some other terms. For these
types, tuples ewre used to make easy implemenattions of serialize and
desearlize
  • Loading branch information
mariari committed Aug 8, 2023
1 parent 317120c commit 22e3779
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
4 changes: 3 additions & 1 deletion taiga_halo2/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use ff::PrimeField;
use halo2_proofs::arithmetic::Field;
use pasta_curves::pallas;
use rand::RngCore;
use rustler::NifStruct;
use std::io;

/// The action result used in transaction.
#[derive(Copy, Debug, Clone)]
#[derive(Copy, Debug, Clone, NifStruct)]
#[module = "Taiga.Action.Instance"]
pub struct ActionInstance {
/// The root of the note commitment Merkle tree.
pub anchor: pallas::Base,
Expand Down
55 changes: 55 additions & 0 deletions taiga_halo2/src/circuit/vp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,71 @@ use vamp_ir::halo2::synth::{make_constant, Halo2Module, PrimeFieldOps};
use vamp_ir::transform::compile;
use vamp_ir::util::{read_inputs_from_file, Config};

use rustler::types::atom;
use rustler::{Decoder, Encoder, Env, NifResult, Term};

#[derive(Debug, Clone)]
pub struct VPVerifyingInfo {
pub vk: VerifyingKey<vesta::Affine>,
pub proof: Proof,
pub public_inputs: ValidityPredicatePublicInputs,
}

rustler::atoms! {verifying_info}

impl Encoder for VPVerifyingInfo {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
(
verifying_info().encode(env),
self.vk.to_bytes().encode(env),
self.proof.encode(env),
self.public_inputs.encode(env),
)
.encode(env)
}
}

impl<'a> Decoder<'a> for VPVerifyingInfo {
fn decode(term: Term<'a>) -> NifResult<Self> {
let (term, vk, proof, public_inputs): (
atom::Atom,
Vec<u8>,
Proof,
ValidityPredicatePublicInputs,
) = term.decode()?;
if term == verifying_info() {
use crate::circuit::vp_examples::TrivialValidityPredicateCircuit;
let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap();
let vk = VerifyingKey::from_bytes::<TrivialValidityPredicateCircuit>(&vk, params)
.map_err(|_e| rustler::Error::Atom("failure to decode"))?;
Ok(VPVerifyingInfo {
vk,
proof,
public_inputs,
})
} else {
Err(rustler::Error::BadArg)
}
}
}

#[derive(Clone, Debug)]
pub struct ValidityPredicatePublicInputs([pallas::Base; VP_CIRCUIT_PUBLIC_INPUT_NUM]);

impl Encoder for ValidityPredicatePublicInputs {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.0.to_vec().encode(env)
}
}

impl<'a> Decoder<'a> for ValidityPredicatePublicInputs {
fn decode(term: Term<'a>) -> NifResult<Self> {
let val: Vec<pallas::Base> = Decoder::decode(term)?;
val.try_into()
.map_err(|_e| rustler::Error::Atom("failure to decode"))
}
}

impl VPVerifyingInfo {
pub fn verify(&self) -> Result<(), Error> {
let params = SETUP_PARAMS_MAP.get(&VP_CIRCUIT_PARAMS_SIZE).unwrap();
Expand Down
9 changes: 6 additions & 3 deletions taiga_halo2/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ use pasta_curves::{
pallas,
};
use rand::RngCore;
use rustler::{NifStruct, NifTuple};
use std::{
hash::{Hash, Hasher},
io,
};

/// A commitment to a note.
#[derive(Copy, Debug, Clone)]
#[derive(Copy, Debug, Clone, NifTuple)]
pub struct NoteCommitment(pallas::Point);

impl NoteCommitment {
Expand All @@ -54,7 +55,8 @@ impl Default for NoteCommitment {
}

/// A note
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, NifStruct)]
#[module = "Taiga.Note"]
pub struct Note {
pub note_type: NoteType,
/// app_data_dynamic is the data defined in application vp and will NOT be used to derive type
Expand All @@ -75,7 +77,8 @@ pub struct Note {
}

/// The parameters in the NoteType are used to derive note type.
#[derive(Debug, Clone, Copy, Default, Eq)]
#[derive(Debug, Clone, Copy, Default, Eq, NifStruct)]
#[module = "Taiga.NoteType"]
pub struct NoteType {
/// app_vk is the compressed verifying key of VP
pub app_vk: pallas::Base,
Expand Down
5 changes: 3 additions & 2 deletions taiga_halo2/src/nullifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use pasta_curves::group::cofactor::CofactorCurveAffine;
use pasta_curves::group::ff::PrimeField;
use pasta_curves::pallas;
use rand::RngCore;
use rustler::{NifTaggedEnum, NifTuple};
use subtle::CtOption;

/// The unique nullifier.
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Debug, Clone, PartialEq, Eq, NifTuple)]
pub struct Nullifier(pallas::Base);

/// The NullifierKeyContainer contains the nullifier_key or the nullifier_key commitment
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Debug, Clone, PartialEq, Eq, NifTaggedEnum)]
pub enum NullifierKeyContainer {
// The NullifierKeyContainer::Commitment is the commitment of NullifierKeyContainer::Key `nk_com = Commitment(nk, 0)`
Commitment(pallas::Base),
Expand Down
3 changes: 2 additions & 1 deletion taiga_halo2/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use halo2_proofs::{
};
use pasta_curves::{pallas, vesta};
use rand::RngCore;
use rustler::NifTuple;

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, NifTuple)]
pub struct Proof(Vec<u8>);

impl Proof {
Expand Down
7 changes: 5 additions & 2 deletions taiga_halo2/src/shielded_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use halo2_proofs::plonk::Error;
use pasta_curves::pallas;
use rand::RngCore;
use rustler::NifStruct;

#[derive(Debug, Clone)]
pub struct ShieldedPartialTransaction {
Expand All @@ -22,13 +23,15 @@ pub struct ShieldedPartialTransaction {
outputs: [NoteVPVerifyingInfoSet; NUM_NOTE],
}

#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, NifStruct)]
#[module = "Taiga.Action.VerifyingInfo"]
pub struct ActionVerifyingInfo {
action_proof: Proof,
action_instance: ActionInstance,
}

#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, NifStruct)]
#[module = "Taiga.Note.VerifyingInfo"]
pub struct NoteVPVerifyingInfoSet {
app_vp_verifying_info: VPVerifyingInfo,
app_dynamic_vp_verifying_info: Vec<VPVerifyingInfo>,
Expand Down
4 changes: 3 additions & 1 deletion taiga_halo2/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use pasta_curves::{
pallas,
};
use rand::{CryptoRng, RngCore};
use rustler::NifStruct;

#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
pub struct Transaction {
Expand All @@ -34,7 +35,8 @@ pub struct ShieldedPartialTxBundle {
partial_txs: Vec<ShieldedPartialTransaction>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, NifStruct)]
#[module = "Taiga.Transaction.Result"]
pub struct ShieldedResult {
anchors: Vec<pallas::Base>,
nullifiers: Vec<Nullifier>,
Expand Down
3 changes: 2 additions & 1 deletion taiga_halo2/src/value_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use halo2_proofs::arithmetic::CurveAffine;
use pasta_curves::group::cofactor::CofactorCurveAffine;
use pasta_curves::group::{Curve, Group, GroupEncoding};
use pasta_curves::pallas;
use rustler::NifTuple;
use subtle::CtOption;

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, NifTuple)]
pub struct ValueCommitment(pallas::Point);

impl ValueCommitment {
Expand Down

0 comments on commit 22e3779

Please sign in to comment.