Skip to content

Commit 0f461f5

Browse files
authored
Remove code from lib.rs / mod.rs files (#451)
1 parent b38ce4a commit 0f461f5

10 files changed

Lines changed: 397 additions & 390 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub const BLAKE2S_IV: [u32; 8] = [
2+
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
3+
];
4+
5+
pub fn blake2s_initial_state() -> [u32; 8] {
6+
let mut h = BLAKE2S_IV;
7+
h[0] ^= 0x01010020;
8+
h
9+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use crate::blake2s_consts::blake2s_initial_state;
2+
use crate::circuit_components::N_COMPONENTS;
3+
use crate::relations::{BLAKE_STATE_RELATION_ID, CommonLookupElements, GATE_RELATION_ID};
4+
use circuits::ivalue::qm31_from_u32s;
5+
use circuits_stark_verifier::proof_from_stark_proof::{pack_component_log_sizes, pack_enable_bits};
6+
use itertools::zip_eq;
7+
use num_traits::Zero;
8+
use stwo::core::channel::Channel;
9+
use stwo::core::fields::FieldExpOps;
10+
use stwo::core::fields::m31::M31;
11+
use stwo::core::fields::qm31::QM31;
12+
use stwo_constraint_framework::Relation;
13+
14+
pub type ComponentLogSize = u32;
15+
pub type ClaimedSum = QM31;
16+
17+
#[derive(Debug, PartialEq)]
18+
pub struct CircuitClaim {
19+
pub log_sizes: [ComponentLogSize; N_COMPONENTS],
20+
pub output_values: Vec<QM31>,
21+
}
22+
impl CircuitClaim {
23+
pub fn mix_into(&self, channel: &mut impl Channel) {
24+
let Self { log_sizes, output_values } = self;
25+
26+
// mix the number of components.
27+
let n_components = log_sizes.len();
28+
channel.mix_felts(&[qm31_from_u32s(n_components as u32, 0, 0, 0)]);
29+
30+
// mix the enable bits into the channel.
31+
channel.mix_felts(&pack_enable_bits(&[true; N_COMPONENTS]));
32+
channel.mix_felts(&pack_component_log_sizes(log_sizes));
33+
// mix the output values into the channel.
34+
channel.mix_felts(output_values);
35+
}
36+
}
37+
38+
pub struct CircuitInteractionElements {
39+
pub common_lookup_elements: CommonLookupElements,
40+
}
41+
impl CircuitInteractionElements {
42+
pub fn draw(channel: &mut impl Channel) -> CircuitInteractionElements {
43+
CircuitInteractionElements { common_lookup_elements: CommonLookupElements::draw(channel) }
44+
}
45+
}
46+
47+
#[derive(Debug, PartialEq)]
48+
pub struct CircuitInteractionClaim {
49+
pub claimed_sums: [ClaimedSum; N_COMPONENTS],
50+
}
51+
impl CircuitInteractionClaim {
52+
pub fn mix_into(&self, channel: &mut impl Channel) {
53+
let Self { claimed_sums } = self;
54+
channel.mix_felts(claimed_sums);
55+
}
56+
}
57+
58+
fn blake_iv_public_logup_sum(
59+
n_blake_gates: usize,
60+
common_lookup_elements: &CommonLookupElements,
61+
) -> QM31 {
62+
// Each Blake gate uses the initial state once and creates one row in blake_output.
63+
// Then blake_output is padded to a power of two, and each padding row uses the
64+
// initial state once. In total we have n_blake_gates.next_power_of_two() uses.
65+
let initial_state_uses = n_blake_gates.next_power_of_two();
66+
67+
let state_relation_id = M31::from(BLAKE_STATE_RELATION_ID);
68+
let initial_state = blake2s_initial_state();
69+
let initial_state_limbs = [
70+
M31::from(initial_state[0] & 0xffff),
71+
M31::from((initial_state[0] >> 16) & 0xffff),
72+
M31::from(initial_state[1] & 0xffff),
73+
M31::from((initial_state[1] >> 16) & 0xffff),
74+
M31::from(initial_state[2] & 0xffff),
75+
M31::from((initial_state[2] >> 16) & 0xffff),
76+
M31::from(initial_state[3] & 0xffff),
77+
M31::from((initial_state[3] >> 16) & 0xffff),
78+
M31::from(initial_state[4] & 0xffff),
79+
M31::from((initial_state[4] >> 16) & 0xffff),
80+
M31::from(initial_state[5] & 0xffff),
81+
M31::from((initial_state[5] >> 16) & 0xffff),
82+
M31::from(initial_state[6] & 0xffff),
83+
M31::from((initial_state[6] >> 16) & 0xffff),
84+
M31::from(initial_state[7] & 0xffff),
85+
M31::from((initial_state[7] >> 16) & 0xffff),
86+
];
87+
88+
let limbs = [
89+
state_relation_id,
90+
M31::from(0u32),
91+
initial_state_limbs[0],
92+
initial_state_limbs[1],
93+
initial_state_limbs[2],
94+
initial_state_limbs[3],
95+
initial_state_limbs[4],
96+
initial_state_limbs[5],
97+
initial_state_limbs[6],
98+
initial_state_limbs[7],
99+
initial_state_limbs[8],
100+
initial_state_limbs[9],
101+
initial_state_limbs[10],
102+
initial_state_limbs[11],
103+
initial_state_limbs[12],
104+
initial_state_limbs[13],
105+
initial_state_limbs[14],
106+
initial_state_limbs[15],
107+
];
108+
let denom: QM31 = common_lookup_elements.combine(&limbs);
109+
denom.inverse() * M31::from(initial_state_uses)
110+
}
111+
112+
pub fn lookup_sum(
113+
claim: &CircuitClaim,
114+
interaction_claim: &CircuitInteractionClaim,
115+
interaction_elements: &CircuitInteractionElements,
116+
output_addresses: &[usize],
117+
n_blake_gates: usize,
118+
) -> QM31 {
119+
let CircuitInteractionClaim { claimed_sums } = interaction_claim;
120+
let component_sum: QM31 = claimed_sums.iter().sum();
121+
122+
// Compute the public logup sum from output gates.
123+
let mut output_sum = QM31::zero();
124+
let gate_relation_id = M31::from(GATE_RELATION_ID);
125+
for (addr, value) in zip_eq(output_addresses, &claim.output_values) {
126+
let values =
127+
[gate_relation_id, M31::from(*addr as u32), value.0.0, value.0.1, value.1.0, value.1.1];
128+
let denom: QM31 = interaction_elements.common_lookup_elements.combine(&values);
129+
output_sum += denom.inverse();
130+
}
131+
132+
// Subtract the blake IV public logup sum (blake IV state is used but never yielded).
133+
let blake_iv_sum =
134+
blake_iv_public_logup_sum(n_blake_gates, &interaction_elements.common_lookup_elements);
135+
136+
component_sum + output_sum - blake_iv_sum
137+
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
use crate::circuit_claim::{CircuitClaim, CircuitInteractionClaim, CircuitInteractionElements};
2+
use crate::components::{
3+
blake_g, blake_gate, blake_output, blake_round, blake_round_sigma, eq, qm31_ops,
4+
range_check_15, range_check_16, triple_xor_32, verify_bitwise_xor_4, verify_bitwise_xor_7,
5+
verify_bitwise_xor_8, verify_bitwise_xor_9, verify_bitwise_xor_12,
6+
};
7+
use stwo::core::air::Component;
8+
use stwo_constraint_framework::TraceLocationAllocator;
9+
use stwo_constraint_framework::preprocessed_columns::PreProcessedColumnId;
10+
11+
macro_rules! define_component_list {
12+
($($variant:ident),* $(,)?) => {
13+
pub enum ComponentList {
14+
$($variant),*
15+
}
16+
pub const N_COMPONENTS: usize = [$(stringify!($variant)),*].len();
17+
};
18+
}
19+
20+
define_component_list! {
21+
Eq,
22+
Qm31Ops,
23+
BlakeGate,
24+
BlakeRound,
25+
BlakeRoundSigma,
26+
BlakeG,
27+
BlakeOutput,
28+
TripleXor32,
29+
VerifyBitwiseXor8,
30+
VerifyBitwiseXor12,
31+
VerifyBitwiseXor4,
32+
VerifyBitwiseXor7,
33+
VerifyBitwiseXor9,
34+
RangeCheck15,
35+
RangeCheck16,
36+
}
37+
38+
pub struct CircuitComponents {
39+
pub eq: eq::Component,
40+
pub qm31_ops: qm31_ops::Component,
41+
pub blake_gate: blake_gate::Component,
42+
pub blake_round: blake_round::Component,
43+
pub blake_round_sigma: blake_round_sigma::Component,
44+
pub blake_g: blake_g::Component,
45+
pub blake_output: blake_output::Component,
46+
pub triple_xor_32: triple_xor_32::Component,
47+
pub verify_bitwise_xor_8: verify_bitwise_xor_8::Component,
48+
pub verify_bitwise_xor_12: verify_bitwise_xor_12::Component,
49+
pub verify_bitwise_xor_4: verify_bitwise_xor_4::Component,
50+
pub verify_bitwise_xor_7: verify_bitwise_xor_7::Component,
51+
pub verify_bitwise_xor_9: verify_bitwise_xor_9::Component,
52+
pub range_check_15: range_check_15::Component,
53+
pub range_check_16: range_check_16::Component,
54+
}
55+
impl CircuitComponents {
56+
pub fn new(
57+
circuit_claim: &CircuitClaim,
58+
interaction_elements: &CircuitInteractionElements,
59+
interaction_claim: &CircuitInteractionClaim,
60+
// Describes the structure of the preprocessed trace. Sensitive to order.
61+
preprocessed_column_ids: &[PreProcessedColumnId],
62+
) -> Self {
63+
let tree_span_provider =
64+
&mut TraceLocationAllocator::new_with_preprocessed_columns(preprocessed_column_ids);
65+
66+
let eq_component = eq::Component::new(
67+
tree_span_provider,
68+
eq::Eval {
69+
log_size: circuit_claim.log_sizes[ComponentList::Eq as usize],
70+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
71+
},
72+
interaction_claim.claimed_sums[ComponentList::Eq as usize],
73+
);
74+
let qm31_ops_component = qm31_ops::Component::new(
75+
tree_span_provider,
76+
qm31_ops::Eval {
77+
log_size: circuit_claim.log_sizes[ComponentList::Qm31Ops as usize],
78+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
79+
},
80+
interaction_claim.claimed_sums[ComponentList::Qm31Ops as usize],
81+
);
82+
let blake_gate_component = blake_gate::Component::new(
83+
tree_span_provider,
84+
blake_gate::Eval {
85+
claim: blake_gate::Claim {
86+
log_size: circuit_claim.log_sizes[ComponentList::BlakeGate as usize],
87+
},
88+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
89+
},
90+
interaction_claim.claimed_sums[ComponentList::BlakeGate as usize],
91+
);
92+
let blake_round_component = blake_round::Component::new(
93+
tree_span_provider,
94+
blake_round::Eval {
95+
claim: blake_round::Claim {
96+
log_size: circuit_claim.log_sizes[ComponentList::BlakeRound as usize],
97+
},
98+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
99+
},
100+
interaction_claim.claimed_sums[ComponentList::BlakeRound as usize],
101+
);
102+
let blake_round_sigma_component = blake_round_sigma::Component::new(
103+
tree_span_provider,
104+
blake_round_sigma::Eval {
105+
claim: blake_round_sigma::Claim {},
106+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
107+
},
108+
interaction_claim.claimed_sums[ComponentList::BlakeRoundSigma as usize],
109+
);
110+
let blake_g_component = blake_g::Component::new(
111+
tree_span_provider,
112+
blake_g::Eval {
113+
claim: blake_g::Claim {
114+
log_size: circuit_claim.log_sizes[ComponentList::BlakeG as usize],
115+
},
116+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
117+
},
118+
interaction_claim.claimed_sums[ComponentList::BlakeG as usize],
119+
);
120+
let blake_output_component = blake_output::Component::new(
121+
tree_span_provider,
122+
blake_output::Eval {
123+
claim: blake_output::Claim {
124+
log_size: circuit_claim.log_sizes[ComponentList::BlakeOutput as usize],
125+
},
126+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
127+
},
128+
interaction_claim.claimed_sums[ComponentList::BlakeOutput as usize],
129+
);
130+
let triple_xor_32_component = triple_xor_32::Component::new(
131+
tree_span_provider,
132+
triple_xor_32::Eval {
133+
claim: triple_xor_32::Claim {
134+
log_size: circuit_claim.log_sizes[ComponentList::TripleXor32 as usize],
135+
},
136+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
137+
},
138+
interaction_claim.claimed_sums[ComponentList::TripleXor32 as usize],
139+
);
140+
let verify_bitwise_xor_8_component = verify_bitwise_xor_8::Component::new(
141+
tree_span_provider,
142+
verify_bitwise_xor_8::Eval {
143+
claim: verify_bitwise_xor_8::Claim {},
144+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
145+
},
146+
interaction_claim.claimed_sums[ComponentList::VerifyBitwiseXor8 as usize],
147+
);
148+
let verify_bitwise_xor_12_component = verify_bitwise_xor_12::Component::new(
149+
tree_span_provider,
150+
verify_bitwise_xor_12::Eval {
151+
claim: verify_bitwise_xor_12::Claim {},
152+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
153+
},
154+
interaction_claim.claimed_sums[ComponentList::VerifyBitwiseXor12 as usize],
155+
);
156+
let verify_bitwise_xor_4_component = verify_bitwise_xor_4::Component::new(
157+
tree_span_provider,
158+
verify_bitwise_xor_4::Eval {
159+
claim: verify_bitwise_xor_4::Claim {},
160+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
161+
},
162+
interaction_claim.claimed_sums[ComponentList::VerifyBitwiseXor4 as usize],
163+
);
164+
let verify_bitwise_xor_7_component = verify_bitwise_xor_7::Component::new(
165+
tree_span_provider,
166+
verify_bitwise_xor_7::Eval {
167+
claim: verify_bitwise_xor_7::Claim {},
168+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
169+
},
170+
interaction_claim.claimed_sums[ComponentList::VerifyBitwiseXor7 as usize],
171+
);
172+
let verify_bitwise_xor_9_component = verify_bitwise_xor_9::Component::new(
173+
tree_span_provider,
174+
verify_bitwise_xor_9::Eval {
175+
claim: verify_bitwise_xor_9::Claim {},
176+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
177+
},
178+
interaction_claim.claimed_sums[ComponentList::VerifyBitwiseXor9 as usize],
179+
);
180+
let range_check_15_component = range_check_15::Component::new(
181+
tree_span_provider,
182+
range_check_15::Eval {
183+
claim: range_check_15::Claim {},
184+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
185+
},
186+
interaction_claim.claimed_sums[ComponentList::RangeCheck15 as usize],
187+
);
188+
let range_check_16_component = range_check_16::Component::new(
189+
tree_span_provider,
190+
range_check_16::Eval {
191+
claim: range_check_16::Claim {},
192+
common_lookup_elements: interaction_elements.common_lookup_elements.clone(),
193+
},
194+
interaction_claim.claimed_sums[ComponentList::RangeCheck16 as usize],
195+
);
196+
Self {
197+
eq: eq_component,
198+
qm31_ops: qm31_ops_component,
199+
blake_gate: blake_gate_component,
200+
blake_round: blake_round_component,
201+
blake_round_sigma: blake_round_sigma_component,
202+
blake_g: blake_g_component,
203+
blake_output: blake_output_component,
204+
triple_xor_32: triple_xor_32_component,
205+
verify_bitwise_xor_8: verify_bitwise_xor_8_component,
206+
verify_bitwise_xor_12: verify_bitwise_xor_12_component,
207+
verify_bitwise_xor_4: verify_bitwise_xor_4_component,
208+
verify_bitwise_xor_7: verify_bitwise_xor_7_component,
209+
verify_bitwise_xor_9: verify_bitwise_xor_9_component,
210+
range_check_15: range_check_15_component,
211+
range_check_16: range_check_16_component,
212+
}
213+
}
214+
215+
pub fn components(self) -> Vec<Box<dyn Component>> {
216+
vec![
217+
Box::new(self.eq) as Box<dyn Component>,
218+
Box::new(self.qm31_ops) as Box<dyn Component>,
219+
Box::new(self.blake_gate) as Box<dyn Component>,
220+
Box::new(self.blake_round) as Box<dyn Component>,
221+
Box::new(self.blake_round_sigma) as Box<dyn Component>,
222+
Box::new(self.blake_g) as Box<dyn Component>,
223+
Box::new(self.blake_output) as Box<dyn Component>,
224+
Box::new(self.triple_xor_32) as Box<dyn Component>,
225+
Box::new(self.verify_bitwise_xor_8) as Box<dyn Component>,
226+
Box::new(self.verify_bitwise_xor_12) as Box<dyn Component>,
227+
Box::new(self.verify_bitwise_xor_4) as Box<dyn Component>,
228+
Box::new(self.verify_bitwise_xor_7) as Box<dyn Component>,
229+
Box::new(self.verify_bitwise_xor_9) as Box<dyn Component>,
230+
Box::new(self.range_check_15) as Box<dyn Component>,
231+
Box::new(self.range_check_16) as Box<dyn Component>,
232+
]
233+
}
234+
}

0 commit comments

Comments
 (0)