Skip to content

Commit

Permalink
Derive WitnessVarLengthEncodable for witnesses used in BWG (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov authored Aug 8, 2024
1 parent 08cae87 commit 3ba23f7
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 75 deletions.
10 changes: 9 additions & 1 deletion src/base_structures/decommit_query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ use boojum::gadgets::traits::allocatable::CSPlaceholder;
use boojum::gadgets::traits::allocatable::{CSAllocatable, CSAllocatableExt};
use boojum::gadgets::traits::castable::WitnessCastable;
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;
use boojum::gadgets::traits::encodable::{CircuitEncodable, CircuitEncodableExt};
use boojum::gadgets::traits::selectable::Selectable;
use boojum::gadgets::traits::witnessable::WitnessHookable;
use boojum::gadgets::u32::UInt32;
use boojum::{field::SmallField, gadgets::u256::UInt256};

#[derive(Derivative, CSAllocatable, CSSelectable, WitnessHookable, CSVarLengthEncodable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
WitnessHookable,
CSVarLengthEncodable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct DecommitQuery<F: SmallField> {
pub code_hash: UInt256<F>,
Expand Down
13 changes: 11 additions & 2 deletions src/base_structures/log_query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use boojum::gadgets::traits::allocatable::CSPlaceholder;
use boojum::gadgets::traits::allocatable::{CSAllocatable, CSAllocatableExt};
use boojum::gadgets::traits::castable::WitnessCastable;
use boojum::gadgets::traits::encodable::CircuitEncodableExt;
use boojum::gadgets::traits::encodable::{CircuitEncodable, CircuitVarLengthEncodable};
use boojum::gadgets::traits::encodable::{
CircuitEncodable, CircuitVarLengthEncodable, WitnessVarLengthEncodable,
};
use boojum::gadgets::traits::selectable::Selectable;
use boojum::gadgets::traits::witnessable::WitnessHookable;
use boojum::gadgets::u160::{recompose_address_from_u32x5, UInt160};
Expand All @@ -18,7 +20,14 @@ use boojum::gadgets::u32::UInt32;
use boojum::gadgets::u8::UInt8;
use cs_derive::*;

#[derive(Derivative, CSAllocatable, CSSelectable, WitnessHookable, CSVarLengthEncodable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
WitnessHookable,
CSVarLengthEncodable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug, Hash)]
pub struct LogQuery<F: SmallField> {
pub address: UInt160<F>,
Expand Down
19 changes: 17 additions & 2 deletions src/base_structures/precompile_input_outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ use boojum::gadgets::traits::allocatable::CSAllocatable;
use boojum::gadgets::traits::allocatable::CSPlaceholder;
use boojum::gadgets::traits::auxiliary::PrettyComparison;
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;
use boojum::gadgets::traits::selectable::Selectable;
use boojum::gadgets::traits::witnessable::WitnessHookable;
use cs_derive::*;

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct PrecompileFunctionInputData<F: SmallField> {
Expand All @@ -36,7 +44,14 @@ impl<F: SmallField> CSPlaceholder<F> for PrecompileFunctionInputData<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct PrecompileFunctionOutputData<F: SmallField> {
Expand Down
10 changes: 9 additions & 1 deletion src/base_structures/register/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ use boojum::cs::traits::cs::DstBuffer;
use boojum::cs::Variable;
use boojum::gadgets::traits::allocatable::{CSAllocatable, CSAllocatableExt};
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;
use boojum::gadgets::traits::selectable::Selectable;
use boojum::gadgets::traits::witnessable::WitnessHookable;

use cs_derive::*;

#[derive(Derivative, CSSelectable, CSAllocatable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSSelectable,
CSAllocatable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug, Hash)]
pub struct VMRegister<F: SmallField> {
pub is_pointer: Boolean<F>,
Expand Down
13 changes: 11 additions & 2 deletions src/base_structures/vm_state/callstack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use super::*;
use boojum::serde_utils::BigArraySerde;

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[CSSelectableBound(
"where [(); <ExecutionContextRecord::<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:"
Expand Down Expand Up @@ -40,7 +47,9 @@ use crate::base_structures::vm_state::saved_context::ExecutionContextRecord;

// execution context that keeps all explicit data about the current execution frame,
// and avoid recomputing of quantities that also do not change between calls
#[derive(Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable, WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct FullExecutionContext<F: SmallField> {
pub saved_context: ExecutionContextRecord<F>,
Expand Down
23 changes: 20 additions & 3 deletions src/base_structures/vm_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use boojum::gadgets::num::Num;
use boojum::gadgets::traits::allocatable::*;
use boojum::gadgets::traits::auxiliary::PrettyComparison;
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;
use boojum::gadgets::traits::selectable::*;
use boojum::gadgets::traits::witnessable::WitnessHookable;
use boojum::gadgets::u16::UInt16;
Expand All @@ -29,7 +30,9 @@ pub const QUEUE_STATE_WIDTH: usize = 4;

use zkevm_opcode_defs::REGISTERS_COUNT;

#[derive(Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable, WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct ArithmeticFlagsPort<F: SmallField> {
pub overflow_or_less_than: Boolean<F>,
Expand Down Expand Up @@ -83,7 +86,14 @@ impl<F: SmallField> ArithmeticFlagsPort<F> {
}
}

#[derive(Derivative, CSSelectable, CSAllocatable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSSelectable,
CSAllocatable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[CSSelectableBound(
"where [(); <ExecutionContextRecord::<F> as CSAllocatableExt<F>>::INTERNAL_STRUCT_LEN]:"
Expand Down Expand Up @@ -153,7 +163,14 @@ impl<F: SmallField> CSPlaceholder<F> for VmLocalState<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct GlobalContext<F: SmallField> {
pub zkporter_is_available: Boolean<F>,
Expand Down
5 changes: 4 additions & 1 deletion src/base_structures/vm_state/saved_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use boojum::cs::Variable;
use boojum::gadgets::traits::allocatable::CSAllocatableExt;
use boojum::gadgets::traits::castable::WitnessCastable;
use boojum::gadgets::traits::encodable::CircuitEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;
use boojum::gadgets::traits::selectable::parallel_select_variables;
use cs_derive::*;
use ethereum_types::Address;
Expand Down Expand Up @@ -31,7 +32,9 @@ use super::*;
// - per-context computed rollback segment length
// - per-context rollback_head

#[derive(Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative, CSAllocatable, CSVarLengthEncodable, WitnessHookable, WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct ExecutionContextRecord<F: SmallField> {
pub this: UInt160<F>, // unfortunately delegatecall mangles this field - it can not be restored from callee's caller
Expand Down
40 changes: 34 additions & 6 deletions src/code_unpacker_sha256/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use boojum::gadgets::{
boolean::Boolean,
queue::*,
traits::{
allocatable::*, encodable::CircuitVarLengthEncodable, selectable::Selectable,
witnessable::WitnessHookable,
allocatable::*, encodable::CircuitVarLengthEncodable, encodable::WitnessVarLengthEncodable,
selectable::Selectable, witnessable::WitnessHookable,
},
u16::UInt16,
u256::UInt256,
Expand All @@ -18,7 +18,14 @@ use boojum::serde_utils::BigArraySerde;
use cs_derive::*;
use derivative::*;

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct CodeDecommittmentFSM<F: SmallField> {
pub sha256_inner_state: [UInt32<F>; 8], // 8 uint32 words of internal sha256 state
Expand Down Expand Up @@ -53,7 +60,14 @@ impl<F: SmallField> CSPlaceholder<F> for CodeDecommittmentFSM<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct CodeDecommitterFSMInputOutput<F: SmallField> {
Expand All @@ -73,7 +87,14 @@ impl<F: SmallField> CSPlaceholder<F> for CodeDecommitterFSMInputOutput<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
pub struct CodeDecommitterInputData<F: SmallField> {
pub memory_queue_initial_state: QueueState<F, FULL_SPONGE_QUEUE_STATE_WIDTH>,
Expand All @@ -92,7 +113,14 @@ impl<F: SmallField> CSPlaceholder<F> for CodeDecommitterInputData<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct CodeDecommitterOutputData<F: SmallField> {
Expand Down
31 changes: 26 additions & 5 deletions src/demux_log_queue/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use boojum::gadgets::{
boolean::Boolean,
queue::*,
traits::{
allocatable::*, encodable::CircuitVarLengthEncodable, selectable::Selectable,
witnessable::WitnessHookable,
allocatable::*, encodable::CircuitVarLengthEncodable, encodable::WitnessVarLengthEncodable,
selectable::Selectable, witnessable::WitnessHookable,
},
};
use boojum::serde_utils::BigArraySerde;
Expand All @@ -22,7 +22,14 @@ use derivative::*;

use super::NUM_DEMUX_OUTPUTS;

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct LogDemuxerFSMInputOutput<F: SmallField> {
Expand All @@ -40,7 +47,14 @@ impl<F: SmallField> CSPlaceholder<F> for LogDemuxerFSMInputOutput<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct LogDemuxerInputData<F: SmallField> {
Expand All @@ -55,7 +69,14 @@ impl<F: SmallField> CSPlaceholder<F> for LogDemuxerInputData<F> {
}
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct LogDemuxerOutputData<F: SmallField> {
Expand Down
13 changes: 10 additions & 3 deletions src/ecrecover/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ use boojum::cs::Variable;
use boojum::gadgets::queue::*;
use boojum::gadgets::traits::allocatable::CSAllocatable;
use boojum::gadgets::traits::allocatable::CSPlaceholder;
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;

use boojum::gadgets::traits::auxiliary::PrettyComparison;
use boojum::gadgets::traits::encodable::CircuitVarLengthEncodable;
use boojum::gadgets::traits::encodable::WitnessVarLengthEncodable;

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitnessHookable,
WitVarLengthEncodable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct EcrecoverCircuitFSMInputOutput<F: SmallField> {
Expand Down
12 changes: 10 additions & 2 deletions src/eip_4844/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use boojum::gadgets::u8::UInt8;
use boojum::gadgets::{
boolean::Boolean,
traits::{
encodable::CircuitVarLengthEncodable, selectable::Selectable, witnessable::WitnessHookable,
encodable::CircuitVarLengthEncodable, encodable::WitnessVarLengthEncodable,
selectable::Selectable, witnessable::WitnessHookable,
},
};
use boojum::serde_utils::BigArraySerde;
Expand All @@ -27,7 +28,14 @@ pub struct BlobChunk<F: SmallField> {
pub inner: [UInt8<F>; BLOB_CHUNK_SIZE],
}

#[derive(Derivative, CSAllocatable, CSSelectable, CSVarLengthEncodable, WitnessHookable)]
#[derive(
Derivative,
CSAllocatable,
CSSelectable,
CSVarLengthEncodable,
WitVarLengthEncodable,
WitnessHookable,
)]
#[derivative(Clone, Copy, Debug)]
#[DerivePrettyComparison("true")]
pub struct EIP4844OutputData<F: SmallField> {
Expand Down
Loading

0 comments on commit 3ba23f7

Please sign in to comment.