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

Naive multipairing precompile #1

Open
wants to merge 4 commits into
base: kp
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use circuit_encodings::zkevm_circuits::bn254::ec_pairing::{ input_alternative::EcMultiPairingCircuitInstanceWitness, alternative_precompile_naive::ecmultipairing_naive_function_entry_point,
};
use derivative::*;

use super::*;
use crate::boojum::cs::traits::circuit::CircuitBuilder;

type F = GoldilocksField;
type R = Poseidon2Goldilocks;

#[derive(Derivative, serde::Serialize, serde::Deserialize)]
#[derivative(Clone, Copy, Debug, Default(bound = ""))]
pub struct ECMultiPairingNaiveFunctionInstanceSynthesisFunction {
_marker: std::marker::PhantomData<(F, R)>,
}

impl CircuitBuilder<F> for ECMultiPairingNaiveFunctionInstanceSynthesisFunction
where
[(); <LogQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <MemoryQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <DecommitQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <UInt256<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <UInt256<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN + 1]:,
{
fn geometry() -> CSGeometry {
CSGeometry {
num_columns_under_copy_permutation: 100,
num_witness_columns: 0,
num_constant_columns: 8,
max_allowed_constraint_degree: 4,
}
}

fn lookup_parameters() -> LookupParameters {
LookupParameters::UseSpecializedColumnsWithTableIdAsConstant {
width: 3,
num_repetitions: 8,
share_table_id: true,
}
}

fn configure_builder<
T: CsBuilderImpl<F, T>,
GC: GateConfigurationHolder<F>,
TB: StaticToolboxHolder,
>(
builder: CsBuilder<T, F, GC, TB>,
) -> CsBuilder<T, F, impl GateConfigurationHolder<F>, impl StaticToolboxHolder> {
let builder = builder.allow_lookup(<Self as CircuitBuilder<F>>::lookup_parameters());

let builder = ConstantsAllocatorGate::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = FmaGateInBaseFieldWithoutConstant::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = ReductionGate::<F, 4>::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = BooleanConstraintGate::configure_builder(
builder,
GatePlacementStrategy::UseSpecializedColumns {
num_repetitions: 1,
share_constants: false,
},
);
let builder = UIntXAddGate::<32>::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = UIntXAddGate::<16>::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = UIntXAddGate::<8>::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = SelectionGate::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = ZeroCheckGate::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
false,
);
let builder = DotProductGate::<4>::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder = PublicInputGate::configure_builder(
builder,
GatePlacementStrategy::UseGeneralPurposeColumns,
);
let builder =
NopGate::configure_builder(builder, GatePlacementStrategy::UseGeneralPurposeColumns);

builder
}
}

impl ZkSyncUniformSynthesisFunction<F> for ECMultiPairingNaiveFunctionInstanceSynthesisFunction
where
[(); <LogQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <MemoryQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <DecommitQuery<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <UInt256<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:,
[(); <UInt256<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN + 1]:,
{
type Witness = EcMultiPairingCircuitInstanceWitness<F>;
type Config = usize;
type RoundFunction = R;

fn description() -> String {
"Elliptic Curve Pairing".to_string()
}

fn size_hint() -> (Option<usize>, Option<usize>) {
(Some(TARGET_CIRCUIT_TRACE_LENGTH), Some(1 << 26))
}

fn add_tables<CS: ConstraintSystem<F>>(cs: &mut CS) {
let table = create_xor8_table();
cs.add_lookup_table::<Xor8Table, 3>(table);
}

fn synthesize_into_cs_inner<CS: ConstraintSystem<F>>(
cs: &mut CS,
witness: Self::Witness,
round_function: &Self::RoundFunction,
config: Self::Config,
) -> [Num<F>; INPUT_OUTPUT_COMMITMENT_LENGTH] {
ecmultipairing_naive_function_entry_point(cs, witness, round_function, config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ pub mod vm_main;
pub mod ecadd;
pub mod ecmul;
pub mod ecpairing;
pub mod ecmultipairing_naive;
pub mod eip4844;
pub mod linear_hasher;
pub mod modexp;

pub use self::code_decommitter::CodeDecommitterInstanceSynthesisFunction;
pub use self::ecadd::ECAddFunctionInstanceSynthesisFunction;
pub use self::ecmul::ECMulFunctionInstanceSynthesisFunction;
use self::ecmultipairing_naive::ECMultiPairingNaiveFunctionInstanceSynthesisFunction;
pub use self::ecpairing::ECPairingFunctionInstanceSynthesisFunction;
pub use self::ecrecover::ECRecoverFunctionInstanceSynthesisFunction;
pub use self::eip4844::EIP4844InstanceSynthesisFunction;
Expand Down Expand Up @@ -82,6 +84,8 @@ pub type ECMulCircuit =
ZkSyncUniformCircuitInstance<GoldilocksField, ECMulFunctionInstanceSynthesisFunction>;
pub type ECPairingCircuit =
ZkSyncUniformCircuitInstance<GoldilocksField, ECPairingFunctionInstanceSynthesisFunction>;
pub type ECMultiPairingNaiveCircuit =
ZkSyncUniformCircuitInstance<GoldilocksField, ECMultiPairingNaiveFunctionInstanceSynthesisFunction>;
pub type RAMPermutationCircuit =
ZkSyncUniformCircuitInstance<GoldilocksField, RAMPermutationInstanceSynthesisFunction>;
pub type StorageSorterCircuit =
Expand Down Expand Up @@ -133,6 +137,7 @@ pub enum ZkSyncBaseLayerStorage<
ECAdd(T),
ECMul(T),
ECPairing(T),
ECMultiPairingNaive(T),
}

impl<T: Clone + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned>
Expand Down Expand Up @@ -160,6 +165,7 @@ impl<T: Clone + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned
ZkSyncBaseLayerStorage::ECAdd(..) => "ECAdd",
ZkSyncBaseLayerStorage::ECMul(..) => "ECMul",
ZkSyncBaseLayerStorage::ECPairing(..) => "ECPairing",
ZkSyncBaseLayerStorage::ECMultiPairingNaive(..) => "ECMultiPairing naive version",
}
}

Expand Down Expand Up @@ -209,6 +215,9 @@ impl<T: Clone + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned
ZkSyncBaseLayerStorage::ECPairing(..) => {
BaseLayerCircuitType::ECPairingPrecompile as u8
}
ZkSyncBaseLayerStorage::ECMultiPairingNaive(..) => {
BaseLayerCircuitType::ECMultiPairingNaivePrecompile as u8
}
}
}

Expand All @@ -234,6 +243,7 @@ impl<T: Clone + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned
ZkSyncBaseLayerStorage::ECAdd(inner) => inner,
ZkSyncBaseLayerStorage::ECMul(inner) => inner,
ZkSyncBaseLayerStorage::ECPairing(inner) => inner,
ZkSyncBaseLayerStorage::ECMultiPairingNaive(inner) => inner,
}
}

Expand Down Expand Up @@ -273,6 +283,7 @@ impl<T: Clone + std::fmt::Debug + serde::Serialize + serde::de::DeserializeOwned
a if a == BaseLayerCircuitType::ECAddPrecompile as u8 => Self::ECAdd(inner),
a if a == BaseLayerCircuitType::ECMulPrecompile as u8 => Self::ECMul(inner),
a if a == BaseLayerCircuitType::ECPairingPrecompile as u8 => Self::ECPairing(inner),
a if a == BaseLayerCircuitType::ECMultiPairingNaivePrecompile as u8 => Self::ECMultiPairingNaive(inner),
a @ _ => panic!("unknown numeric type {}", a),
}
}
Expand Down Expand Up @@ -313,6 +324,7 @@ where
ECAdd(ECAddCircuit),
ECMul(ECMulCircuit),
ECPairing(ECPairingCircuit),
ECMultiPairingNaive(ECMultiPairingNaiveCircuit),
}

impl ZkSyncBaseLayerCircuit
Expand Down Expand Up @@ -347,6 +359,7 @@ where
ZkSyncBaseLayerCircuit::ECAdd(..) => "ECAdd",
ZkSyncBaseLayerCircuit::ECMul(..) => "ECMul",
ZkSyncBaseLayerCircuit::ECPairing(..) => "ECPairing",
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(..) => "ECMultiPairingNaive",
}
}

Expand All @@ -372,6 +385,8 @@ where
ZkSyncBaseLayerCircuit::ECAdd(inner) => inner.size_hint(),
ZkSyncBaseLayerCircuit::ECMul(inner) => inner.size_hint(),
ZkSyncBaseLayerCircuit::ECPairing(inner) => inner.size_hint(),
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(inner) => inner.size_hint(),

}
}

Expand Down Expand Up @@ -473,6 +488,7 @@ where
ZkSyncBaseLayerCircuit::ECAdd(inner) => Self::synthesis_inner::<_, CR>(inner, hint),
ZkSyncBaseLayerCircuit::ECMul(inner) => Self::synthesis_inner::<_, CR>(inner, hint),
ZkSyncBaseLayerCircuit::ECPairing(inner) => Self::synthesis_inner::<_, CR>(inner, hint),
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(inner) => Self::synthesis_inner::<_, CR>(inner, hint),
}
}

Expand All @@ -498,6 +514,7 @@ where
ZkSyncBaseLayerCircuit::ECAdd(inner) => inner.geometry_proxy(),
ZkSyncBaseLayerCircuit::ECMul(inner) => inner.geometry_proxy(),
ZkSyncBaseLayerCircuit::ECPairing(inner) => inner.geometry_proxy(),
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(inner) => inner.geometry_proxy(),
}
}

Expand Down Expand Up @@ -563,6 +580,9 @@ where
ZkSyncBaseLayerCircuit::ECPairing(inner) => {
inner.debug_witness();
}
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(inner) => {
inner.debug_witness();
}
};

()
Expand Down Expand Up @@ -613,6 +633,9 @@ where
ZkSyncBaseLayerCircuit::ECMul(..) => BaseLayerCircuitType::ECMulPrecompile as u8,
ZkSyncBaseLayerCircuit::ECPairing(..) => {
BaseLayerCircuitType::ECPairingPrecompile as u8
},
ZkSyncBaseLayerCircuit::ECMultiPairingNaive(..) => {
BaseLayerCircuitType::ECMultiPairingNaivePrecompile as u8
}
}
}
Expand Down
Loading