Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve pairing #139

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions pallets/zkrollup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use pallet_plonk::{FullcodecRng, Plonk, Proof};
use traits::Rollup;
use zkstd::common::Pairing;
use zkstd::common::{Pairing, RedDSA};

#[frame_support::pallet]
pub mod pallet {
Expand All @@ -52,8 +52,11 @@ pub mod pallet {
//
// >
type Plonk: Plonk<<Self as pallet_plonk::Config>::P>;
type RedDsa: RedDSA<
ScalarField = <<Self as pallet_plonk::Config>::P as Pairing>::ScalarField,
>;
type Transaction: Parameter + Member + Default + Copy;
type Batch: BatchGetter<<Self as pallet_plonk::Config>::P>
type Batch: BatchGetter<<Self as pallet::Config>::RedDsa>
+ Parameter
+ Member
+ Default
Expand Down Expand Up @@ -179,12 +182,6 @@ pub mod pallet {
Self::deposit_event(Event::StateUpdated(new_root));
Ok(().into())
}

// pub fn check_balance(&self, merkle_proof: MerkleProof<F, H, N>) -> u64 {
// // merkle_verify(merkle_proof, self.rollup_state_root);
// // get_balance()
// 0
// }
}

#[pallet::event]
Expand Down
17 changes: 7 additions & 10 deletions pallets/zkrollup/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bls_12_381::Fr;
use ec_pairing::TatePairing;
use frame_support::parameter_types;
use frame_system as system;
use red_jubjub::PublicKey;
use red_jubjub::{PublicKey, RedJubjub};
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -60,25 +60,22 @@ impl system::Config for Test {

impl pallet_plonk::Config for Test {
type P = TatePairing;
type CustomCircuit = BatchCircuit<TatePairing, Poseidon<Fr, 2>, 2, 2>;
type CustomCircuit = BatchCircuit<RedJubjub, Poseidon<Fr, 2>, 2, 2>;
type Event = Event;
}

impl zkrollup_pallet::Config for Test {
type Event = Event;

type Transaction = Transaction<<Self as pallet_plonk::Config>::P>;

type Transaction = Transaction<Self::RedDsa>;
type PublicKey = PublicKey<Self::RedDsa>;
type Plonk = Plonk;
type RedDsa = RedJubjub;
type Batch = Batch<
<Self as pallet_plonk::Config>::P,
Self::RedDsa,
Poseidon<<<Self as pallet_plonk::Config>::P as Pairing>::ScalarField, 2>,
2,
2,
>;

type PublicKey = PublicKey<<Self as pallet_plonk::Config>::P>;

type Plonk = Plonk;
}

// Build genesis storage according to the mock runtime.
Expand Down
67 changes: 17 additions & 50 deletions pallets/zkrollup/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{self as zkrollup_pallet};
use bls_12_381::Fr;
use ec_pairing::TatePairing;
use frame_support::{construct_runtime, parameter_types};
use red_jubjub::PublicKey;
use red_jubjub::{PublicKey, RedJubjub};
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand All @@ -14,9 +14,6 @@ use sp_runtime::{
use zkrollup::{Batch, BatchCircuit, Poseidon, Transaction};
use zkstd::common::Pairing;

// let last_level_size = leaves.len().next_power_of_two();
// let tree_size = 2 * last_level_size - 1;
// let tree_height = tree_height(tree_size as u64);
const TREE_HEIGH: usize = 3;
// Need to specify the size of tree as well
const BATCH_SIZE: usize = 2;
Expand Down Expand Up @@ -69,23 +66,22 @@ impl frame_system::Config for TestRuntime {

impl pallet_plonk::Config for TestRuntime {
type P = TatePairing;
type CustomCircuit = BatchCircuit<TatePairing, Poseidon<Fr, 2>, TREE_HEIGH, BATCH_SIZE>;
type CustomCircuit = BatchCircuit<RedJubjub, Poseidon<Fr, 2>, TREE_HEIGH, BATCH_SIZE>;
type Event = Event;
}

impl Config for TestRuntime {
type Transaction = Transaction<<Self as pallet_plonk::Config>::P>;

type Transaction = Transaction<Self::RedDsa>;
type PublicKey = PublicKey<Self::RedDsa>;
type Event = Event;
type Plonk = Plonk;
type RedDsa = RedJubjub;
type Batch = Batch<
<Self as pallet_plonk::Config>::P,
Self::RedDsa,
Poseidon<<<Self as pallet_plonk::Config>::P as Pairing>::ScalarField, 2>,
TREE_HEIGH,
BATCH_SIZE,
>;

type PublicKey = PublicKey<<Self as pallet_plonk::Config>::P>;
type Event = Event;
type Plonk = Plonk;
}

#[cfg(test)]
Expand All @@ -100,17 +96,6 @@ mod zkrollup_tests {
use zkrollup::{BatchGetter, Poseidon, RollupOperator, TransactionData};
use zkstd::common::Group;

// fn events() -> Vec<Event> {
// let evt = System::events()
// .into_iter()
// .map(|evt| evt.event)
// .collect::<Vec<_>>();

// System::reset_events();

// evt
// }

fn get_rng() -> FullcodecRng {
FullcodecRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
Expand Down Expand Up @@ -141,11 +126,13 @@ mod zkrollup_tests {
let pp = Plonk::public_params().unwrap();

// 3. Create an operator
let mut operator =
RollupOperator::<TatePairing, Poseidon<Fr, 2>, TREE_HEIGH, BATCH_SIZE>::new(
Poseidon::<Fr, 2>::new(),
pp,
);
let mut operator = RollupOperator::<
RedJubjub,
TatePairing,
Poseidon<Fr, 2>,
TREE_HEIGH,
BATCH_SIZE,
>::new(Poseidon::<Fr, 2>::new(), pp);

// Assures that null elements' hashes are correct
assert_eq!(
Expand All @@ -169,13 +156,6 @@ mod zkrollup_tests {
// 6. Add them to the deposit pool on the L1
assert_ok!(ZkRollup::deposit(alice_origin, deposit1.0, deposit1.1));

// let deposit = events();
// assert_eq!(
// deposit,
// [Event::main_contract(crate::Event::Deposit(deposit1.0, deposit1.1)),]
// );
// if let Event::main_contract(crate::Event::Deposit(amount, address)) = deposit.first().unwrap() {

// 7. Explicitly process data on L2. Will be changed, when communication between layers will be decided.
operator.process_deposit(deposit1.0, deposit1.1);
// }
Expand All @@ -188,14 +168,7 @@ mod zkrollup_tests {

// Same for the second deposit
assert_ok!(ZkRollup::deposit(bob_origin, deposit2.0, deposit2.1));
// let deposit = events();
// assert_eq!(
// deposit,
// [Event::main_contract(crate::Event::Deposit(deposit2.0, deposit2.1)),]
// );
// if let Event::main_contract(crate::Event::Deposit(amount, address)) = deposit.first().unwrap() {
operator.process_deposit(deposit2.0, deposit2.1);
// }

assert_eq!(
operator.state_root(),
Expand Down Expand Up @@ -234,23 +207,17 @@ mod zkrollup_tests {
public_inputs,
batch
));
// assert_eq!(
// events(),
// [Event::main_contract(crate::Event::StateUpdated(
// root_after_tx
// )),]
// );

// 11. Check that state root on L1 changed.
assert_eq!(<ZkRollup as Rollup>::state_root(), operator.state_root());

// Withdraw

// 1. Burn funds on L2 by sending to a special address
let alice_withdraw: Transaction<TatePairing> =
let alice_withdraw: Transaction<RedJubjub> =
TransactionData::new(alice_address, PublicKey::zero(), 5)
.signed(alice_secret, &mut rng);
let bob_withdraw: Transaction<TatePairing> =
let bob_withdraw: Transaction<RedJubjub> =
TransactionData::new(bob_address, PublicKey::zero(), 5)
.signed(bob_secret, &mut rng);

Expand Down
1 change: 0 additions & 1 deletion primitive/redjubjub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ parity-scale-codec = { version = "2.0.0", default-features = false, features = [
[dev-dependencies]
hex-literal = { version = "0.3.1", default-features = false }
rand = "0.8"
ec-pairing = { path = "../pairing", default-features = false }

[features]
default = ["std"]
Expand Down
8 changes: 4 additions & 4 deletions primitive/redjubjub/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bls_12_381::Fr;
use zkstd::common::{FftField, Pairing, TwistedEdwardsAffine};
use zkstd::common::{FftField, RedDSA, TwistedEdwardsAffine};

pub(crate) const SAPLING_PERSONAL: &[u8; 16] = b"Zcash_RedJubjubH";

Expand Down Expand Up @@ -32,12 +32,12 @@ fn sapling_base_point_y<F: FftField>() -> F {
F::from(SAPLING_BASE_POINT_Y.inner())
}

pub fn sapling_base_point<P: Pairing>() -> P::JubjubAffine {
pub fn sapling_base_point<P: RedDSA>() -> P::JubjubAffine {
let x = sapling_base_point_x();
let y = sapling_base_point_y();
P::JubjubAffine::from_raw_unchecked(x, y)
}

pub fn sapling_redjubjub_cofactor<SF: FftField>() -> SF {
SF::from(SAPLING_REDJUBJUB_COFACTOR.inner())
pub fn sapling_redjubjub_cofactor<F: FftField>() -> F {
F::from(SAPLING_REDJUBJUB_COFACTOR.inner())
}
29 changes: 21 additions & 8 deletions primitive/redjubjub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,37 @@ mod private_key;
mod public_key;
mod signature;

use bls_12_381::Fr;
pub use hash::sapling_hash;
use jub_jub::{Fp, JubjubAffine, JubjubExtended};
pub use private_key::SecretKey;
pub use public_key::PublicKey;
pub use signature::Signature;
use zkstd::common::Pairing;
use zkstd::common::RedDSA;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, PartialOrd, Ord)]
pub struct RedJubjub {}

impl RedDSA for RedJubjub {
type ScalarField = Fr;

type JubjubScalar = Fp;

type JubjubAffine = JubjubAffine;

type JubjubExtended = JubjubExtended;
}

// TODO fix pairing dependency
/// An redjubjub secret key and public key pair.
#[derive(Copy, Clone, Debug)]
pub struct Keypair<P: Pairing> {
pub struct Keypair<P: RedDSA> {
/// secret key
pub secret: SecretKey<P>,
/// public key
pub public: PublicKey<P>,
}

impl<P: Pairing> Keypair<P> {
impl<P: RedDSA> Keypair<P> {
pub fn new(secret: SecretKey<P>) -> Self {
let public = secret.to_public_key();
Self { secret, public }
Expand All @@ -48,7 +62,6 @@ impl<P: Pairing> Keypair<P> {
#[cfg(test)]
mod tests {
use super::*;
use ec_pairing::TatePairing;
use jub_jub::Fp;
use rand_core::OsRng;
use zkstd::common::{Group, SigUtils};
Expand All @@ -57,7 +70,7 @@ mod tests {
fn sig_utils() {
let randomness = OsRng;
let msg = b"test";
let secret = SecretKey::<TatePairing>(Fp::random(OsRng));
let secret = SecretKey::<RedJubjub>(Fp::random(OsRng));
let sig = secret.sign(msg, randomness);
let pub_key = secret.to_public_key();

Expand All @@ -81,7 +94,7 @@ mod tests {
let wrong_msg = b"tes";
let randomness = OsRng;

let priv_key = SecretKey::<TatePairing>(Fp::random(OsRng));
let priv_key = SecretKey::<RedJubjub>(Fp::random(OsRng));
let sig = priv_key.sign(msg, randomness);
let pub_key = priv_key.to_public_key();

Expand All @@ -96,7 +109,7 @@ mod tests {
let msg = b"test";
let wrong_msg = b"tes";

let priv_key = SecretKey::<TatePairing>(Fp::random(OsRng));
let priv_key = SecretKey::<RedJubjub>(Fp::random(OsRng));
let pub_key = priv_key.to_public_key();

// randomization
Expand Down
8 changes: 4 additions & 4 deletions primitive/redjubjub/src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use super::signature::Signature;
use crate::constant::sapling_base_point;

use rand_core::RngCore;
use zkstd::common::{FftField, Pairing, SigUtils};
use zkstd::common::{FftField, RedDSA, SigUtils};

/// RedJubjub secret key struct used for signing transactions
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SecretKey<P: Pairing>(pub(crate) P::JubjubScalar);
pub struct SecretKey<P: RedDSA>(pub(crate) P::JubjubScalar);

impl<P: Pairing> SigUtils<32> for SecretKey<P> {
impl<P: RedDSA> SigUtils<32> for SecretKey<P> {
fn from_bytes(bytes: [u8; 32]) -> Option<Self> {
P::JubjubScalar::from_bytes(bytes).map(Self::new)
}
Expand All @@ -20,7 +20,7 @@ impl<P: Pairing> SigUtils<32> for SecretKey<P> {
}
}

impl<P: Pairing> SecretKey<P> {
impl<P: RedDSA> SecretKey<P> {
pub fn new(key: P::JubjubScalar) -> Self {
Self(key)
}
Expand Down
6 changes: 3 additions & 3 deletions primitive/redjubjub/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use zkstd::common::*;
Decode,
Encode,
)]
pub struct PublicKey<P: Pairing>(pub(crate) P::JubjubExtended);
pub struct PublicKey<P: RedDSA>(pub(crate) P::JubjubExtended);

impl<P: Pairing> SigUtils<32> for PublicKey<P> {
impl<P: RedDSA> SigUtils<32> for PublicKey<P> {
fn to_bytes(self) -> [u8; 32] {
self.0.to_bytes()
}
Expand All @@ -33,7 +33,7 @@ impl<P: Pairing> SigUtils<32> for PublicKey<P> {
}
}

impl<P: Pairing> PublicKey<P> {
impl<P: RedDSA> PublicKey<P> {
pub fn new(raw: P::JubjubExtended) -> Self {
PublicKey(raw)
}
Expand Down
Loading
Loading