Skip to content

Commit

Permalink
Made the BEAM NIF integration locked behind a feature flag
Browse files Browse the repository at this point in the history
Instead of always building with NIF integration, we instead hide it
behind a feature flag. This means that the dependency has no effect if
it is note wanted
  • Loading branch information
mariari committed Aug 25, 2023
1 parent 5123828 commit 85fa6ed
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 24 deletions.
6 changes: 5 additions & 1 deletion taiga_halo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rustler = "0.29.1"
rustler = {version = "0.29.1", optional = true}
rand = "0.8.4"
lazy_static = "1"
blake2b_simd = "1"
Expand All @@ -27,6 +27,10 @@ num-bigint = "0.4.3"
criterion = "0.5.1"
proptest = "1.0.0"

[features]
default = []
nif = ["rustler", "pasta_curves/repr-erlang"]

[[bench]]
name = "action_proof"
harness = false
Expand Down
6 changes: 4 additions & 2 deletions taiga_halo2/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use ff::PrimeField;
use halo2_proofs::arithmetic::Field;
use pasta_curves::pallas;
use rand::RngCore;
#[cfg(feature = "nif")]
use rustler::NifStruct;
use std::io;

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

#[cfg(feature = "nif")]
use rustler::types::atom;
#[cfg(feature = "nif")]
use rustler::{Decoder, Encoder, Env, NifResult, Term};

#[derive(Debug, Clone)]
Expand All @@ -66,8 +68,10 @@ pub struct VPVerifyingInfo {
pub public_inputs: ValidityPredicatePublicInputs,
}

#[cfg(feature = "nif")]
rustler::atoms! {verifying_info}

#[cfg(feature = "nif")]
impl Encoder for VPVerifyingInfo {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
(
Expand All @@ -80,6 +84,7 @@ impl Encoder for VPVerifyingInfo {
}
}

#[cfg(feature = "nif")]
impl<'a> Decoder<'a> for VPVerifyingInfo {
fn decode(term: Term<'a>) -> NifResult<Self> {
let (term, vk, proof, public_inputs): (
Expand Down Expand Up @@ -107,12 +112,14 @@ impl<'a> Decoder<'a> for VPVerifyingInfo {
#[derive(Clone, Debug)]
pub struct ValidityPredicatePublicInputs([pallas::Base; VP_CIRCUIT_PUBLIC_INPUT_NUM]);

#[cfg(feature = "nif")]
impl Encoder for ValidityPredicatePublicInputs {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.0.to_vec().encode(env)
}
}

#[cfg(feature = "nif")]
impl<'a> Decoder<'a> for ValidityPredicatePublicInputs {
fn decode(term: Term<'a>) -> NifResult<Self> {
let val: Vec<pallas::Base> = Decoder::decode(term)?;
Expand Down
8 changes: 6 additions & 2 deletions taiga_halo2/src/circuit/vp_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use halo2_proofs::{
use lazy_static::lazy_static;
use pasta_curves::pallas;
use rand::{rngs::OsRng, RngCore};
#[cfg(feature = "nif")]
use rustler::{Decoder, Encoder, Env, NifResult, NifStruct, Term};

pub mod cascade_intent;
Expand All @@ -42,8 +43,9 @@ pub struct TrivialValidityPredicateCircuit {
}

// I only exist to allow trivial derivation of the nifstruct
#[derive(Clone, Debug, Default, NifStruct)]
#[module = "Taiga.VP.Trivial"]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.VP.Trivial")]
struct TrivialValidtyPredicateCircuitProxy {
owned_note_pub_id: pallas::Base,
input_notes: Vec<Note>,
Expand Down Expand Up @@ -84,11 +86,13 @@ impl TrivialValidtyPredicateCircuitProxy {
})
}
}
#[cfg(feature = "nif")]
impl Encoder for TrivialValidityPredicateCircuit {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.to_proxy().encode(env)
}
}
#[cfg(feature = "nif")]
impl<'a> Decoder<'a> for TrivialValidityPredicateCircuit {
fn decode(term: Term<'a>) -> NifResult<Self> {
let val: TrivialValidtyPredicateCircuitProxy = Decoder::decode(term)?;
Expand Down
14 changes: 9 additions & 5 deletions taiga_halo2/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ use pasta_curves::{
pallas,
};
use rand::RngCore;
#[cfg(feature = "nif")]
use rustler::{NifStruct, NifTuple};
use std::{
hash::{Hash, Hasher},
io,
};

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

impl NoteCommitment {
Expand All @@ -55,8 +57,9 @@ impl Default for NoteCommitment {
}

/// A note
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, NifStruct)]
#[module = "Taiga.Note"]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", 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 @@ -77,8 +80,9 @@ pub struct Note {
}

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

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

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

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

impl Proof {
Expand Down
20 changes: 14 additions & 6 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;
#[cfg(feature = "nif")]
use rustler::{Decoder, Encoder, Env, NifResult, NifStruct, Term};

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

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

#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, NifStruct)]
#[module = "Taiga.Note.VerifyingInfo"]
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.Note.VerifyingInfo")]
pub struct NoteVPVerifyingInfoSet {
app_vp_verifying_info: VPVerifyingInfo,
app_dynamic_vp_verifying_info: Vec<VPVerifyingInfo>,
Expand All @@ -40,8 +43,9 @@ pub struct NoteVPVerifyingInfoSet {
}

// Is easier to derive traits for
#[derive(Debug, Clone, NifStruct)]
#[module = "Taiga.Shielded.PTX"]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.Shielded.PTX")]
struct ShieldedPartialTransactionProxy {
actions: Vec<ActionVerifyingInfo>,
inputs: Vec<NoteVPVerifyingInfoSet>,
Expand Down Expand Up @@ -272,11 +276,15 @@ impl BorshDeserialize for ShieldedPartialTransaction {
})
}
}

#[cfg(feature = "nif")]
impl Encoder for ShieldedPartialTransaction {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.to_proxy().encode(env)
}
}

#[cfg(feature = "nif")]
impl<'a> Decoder<'a> for ShieldedPartialTransaction {
fn decode(term: Term<'a>) -> NifResult<Self> {
let val: ShieldedPartialTransactionProxy = Decoder::decode(term)?;
Expand Down
15 changes: 11 additions & 4 deletions taiga_halo2/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use pasta_curves::{
pallas,
};
use rand::{CryptoRng, RngCore};
#[cfg(feature = "nif")]
use rustler::types::atom;
#[cfg(feature = "nif")]
use rustler::{atoms, Decoder, Env, NifRecord, NifResult, NifStruct, Term};

#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
Expand All @@ -31,14 +33,16 @@ pub enum InProgressBindingSignature {
Unauthorized(BindingSigningKey),
}

#[derive(Debug, Clone, BorshDeserialize, BorshSerialize, NifRecord)]
#[tag = "bundle"]
#[derive(Debug, Clone, BorshDeserialize, BorshSerialize)]
#[cfg_attr(feature = "nif", derive(NifRecord))]
#[cfg_attr(feature = "nif", tag = "bundle")]
pub struct ShieldedPartialTxBundle {
partial_txs: Vec<ShieldedPartialTransaction>,
}

#[derive(Debug, Clone, PartialEq, Eq, NifStruct)]
#[module = "Taiga.Transaction.Result"]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "nif", derive(NifStruct))]
#[cfg_attr(feature = "nif", module = "Taiga.Transaction.Result")]
pub struct ShieldedResult {
anchors: Vec<pallas::Base>,
nullifiers: Vec<Nullifier>,
Expand Down Expand Up @@ -202,8 +206,10 @@ impl Transaction {
}
}

#[cfg(feature = "nif")]
atoms! { transaction }

#[cfg(feature = "nif")]
impl rustler::Encoder for Transaction {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
(
Expand All @@ -219,6 +225,7 @@ impl rustler::Encoder for Transaction {
}
}

#[cfg(feature = "nif")]
impl<'a> Decoder<'a> for Transaction {
fn decode(term: Term<'a>) -> NifResult<Self> {
let (term, shielded_ptx_bundle, transparent_bytes, sig_bytes): (
Expand Down
4 changes: 3 additions & 1 deletion taiga_halo2/src/value_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use halo2_proofs::arithmetic::CurveAffine;
use pasta_curves::group::cofactor::CofactorCurveAffine;
use pasta_curves::group::{Curve, Group, GroupEncoding};
use pasta_curves::pallas;
#[cfg(feature = "nif")]
use rustler::NifTuple;
use subtle::CtOption;

#[derive(Copy, Clone, Debug, NifTuple)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "nif", derive(NifTuple))]
pub struct ValueCommitment(pallas::Point);

impl ValueCommitment {
Expand Down

0 comments on commit 85fa6ed

Please sign in to comment.