Skip to content

Commit

Permalink
re-export arkworks traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Jan 10, 2024
1 parent f6b9e2f commit 47c08c4
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions banderwagon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,18 @@ use ark_ed_on_bls12_381_bandersnatch::Fq;
use ark_ff::BigInteger256;
pub use element::{multi_scalar_mul, Element, Fr};

pub trait VerkleField {
fn zero() -> Self;
fn is_zero(&self) -> bool;

fn one() -> Self;

fn from_be_bytes_mod_order(bytes: &[u8]) -> Self;
fn from_le_bytes_mod_order(bytes: &[u8]) -> Self;
}

// TODO: This avoids leaking arkworks in verkle_trie, but it's not ideal
// TODO: Another method would be to create a new type that wraps Fr
impl VerkleField for Fr {
fn zero() -> Self {
ark_ff::Zero::zero()
}
fn is_zero(&self) -> bool {
ark_ff::Zero::is_zero(self)
}

fn one() -> Self {
ark_ff::One::one()
}

fn from_be_bytes_mod_order(bytes: &[u8]) -> Self {
ark_ff::PrimeField::from_be_bytes_mod_order(bytes)
}

fn from_le_bytes_mod_order(bytes: &[u8]) -> Self {
ark_ff::PrimeField::from_le_bytes_mod_order(bytes)
}
// Re-export arkworks traits that one may need to use in order to use
// specific methods on field elements and for serialization.
//
// For example, if we expose Fr directly, then for consumers to call methods like Fr::one()
// they will need to import ark_ff::One, which means they will need to import
// ark_ff as a dependency.
//
// This reexport allows us to avoid that.
pub use trait_defs::*;
pub mod trait_defs {
pub use ark_ff::{One, PrimeField, Zero};
pub use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
}

pub const fn fr_from_u64_limbs(limbs: [u64; 4]) -> Fr {
Expand All @@ -47,9 +28,6 @@ pub const fn fr_from_u64_limbs(limbs: [u64; 4]) -> Fr {
//
// This is useful in try-and-increment algorithms.
pub fn try_reduce_to_element(bytes: &[u8]) -> Option<Element> {
use ark_ff::PrimeField;
use ark_serialize::CanonicalSerialize;

// The Element::from_bytes method does not reduce the bytes, it expects the
// input to be in a canonical format, so we must do the reduction ourselves
let x_coord = Fq::from_be_bytes_mod_order(bytes);
Expand Down

0 comments on commit 47c08c4

Please sign in to comment.