Skip to content

Commit

Permalink
Start assignment from second row
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Liang committed Oct 5, 2023
1 parent b884801 commit 193f667
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/gadgets/byte_bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ByteBitGadget {
}

pub fn assign<F: FieldExt>(&self, region: &mut Region<'_, F>) {
let mut offset = 0;
let mut offset = 1;
for byte in 0..256 {
for index in 0..8 {
self.byte.assign(region, offset, byte);
Expand All @@ -49,7 +49,7 @@ impl ByteBitGadget {
}

pub fn n_rows_required() -> usize {
256 * 8
256 * 8 + 1
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gadgets/byte_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl ByteRepresentationConfig {
.chain(u128s.iter().map(u128_to_big_endian))
.chain(frs.iter().map(fr_to_big_endian));

let mut offset = 0;
let mut offset = 1;
for byte_representation in byte_representations {
let mut value = F::zero();
let mut rlc = Value::known(F::zero());
Expand Down Expand Up @@ -140,7 +140,7 @@ impl ByteRepresentationConfig {
}

pub fn n_rows_required(u32s: &[u32], u64s: &[u64], u128s: &[u128], frs: &[Fr]) -> usize {
u32s.len() * 4 + u64s.len() * 8 + u128s.len() * 16 + frs.len() * 31
1 + u32s.len() * 4 + u64s.len() * 8 + u128s.len() * 16 + frs.len() * 31
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/gadgets/canonical_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl CanonicalRepresentationConfig {
let mut modulus_bytes = [0u8; 32];
modulus.to_big_endian(&mut modulus_bytes);

let mut offset = 0;
let mut offset = 1;
for value in values.iter() {
let mut bytes = value.to_bytes();
bytes.reverse();
Expand Down Expand Up @@ -213,7 +213,7 @@ impl CanonicalRepresentationConfig {
}

pub fn n_rows_required(values: &[Fr]) -> usize {
values.len() * 32
values.len() * 32 + 1
}
}

Expand Down Expand Up @@ -285,7 +285,7 @@ mod test {
layouter.assign_region(
|| "",
|mut region| {
for offset in 0..(8 * 256) {
for offset in 1..(1 + 8 * 256) {
selector.enable(&mut region, offset);
}
byte_bit.assign(&mut region);
Expand Down
4 changes: 3 additions & 1 deletion src/gadgets/key_bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ impl KeyBitConfig {
pub fn assign(&self, region: &mut Region<'_, Fr>, lookups: &[(Fr, usize, bool)]) {
// TODO; dedup lookups
for (offset, (value, index, bit)) in lookups.iter().enumerate() {
// TODO: either move the disabled row to the end of the assigment or get rid of it entirely.
let offset = offset + 1; // Start assigning at offet = 1 because the first row is disabled.
let bytes = value.to_bytes();

let index_div_8 = index / 8; // index = (31 - index/8) * 8
Expand Down Expand Up @@ -195,7 +197,7 @@ mod test {
layouter.assign_region(
|| "",
|mut region| {
for offset in 0..(8 * 256) {
for offset in 1..(1 + 8 * 256) {
selector.enable(&mut region, offset);
}

Expand Down
18 changes: 18 additions & 0 deletions src/gadgets/mpt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,9 @@ pub fn key_bit_lookups(proofs: &[Proof]) -> Vec<(Fr, usize, bool)> {
}
lookups.extend(proof.storage.key_bit_lookups());
}

lookups.sort();
lookups.dedup();
lookups
}

Expand Down Expand Up @@ -2210,6 +2213,19 @@ pub fn byte_representations(proofs: &[Proof]) -> (Vec<u32>, Vec<u64>, Vec<u128>,
_ => {}
}
}

u32s.sort();
u32s.dedup();

u64s.sort();
u64s.dedup();

u128s.sort();
u128s.dedup();

frs.sort();
frs.dedup();

(u32s, u64s, u128s, frs)
}

Expand All @@ -2224,5 +2240,7 @@ pub fn mpt_update_keys(proofs: &[Proof]) -> Vec<Fr> {
keys.push(proof.claim.old_root);
keys.push(proof.claim.new_root);
}
keys.sort();
keys.dedup();
keys
}
9 changes: 6 additions & 3 deletions src/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ impl MptCircuitConfig {
proofs: &[Proof],
n_rows: usize,
) -> Result<(), Error> {
dbg!(Self::n_rows_required(proofs));

let randomness = self.rlc_randomness.value(layouter);
let (u32s, u64s, u128s, frs) = byte_representations(proofs);

Expand Down Expand Up @@ -179,12 +181,13 @@ impl MptCircuitConfig {
/// The number of minimum number of rows required for the mpt circuit.
pub fn n_rows_required(proofs: &[Proof]) -> usize {
let (u32s, u64s, u128s, frs) = byte_representations(proofs);
// We need two addtional rows: disabled row for disabled mpt update lookups and one final
// padding row to satisfy the "final mpt update is padding" constraint.
2 + *[

// +1 for the final padding row to satisfy the "final mpt update is padding" constraint.
1 + *[
MptUpdateConfig::n_rows_required(proofs),
CanonicalRepresentationConfig::n_rows_required(&mpt_update_keys(proofs)),
KeyBitConfig::n_rows_required(&key_bit_lookups(proofs)),
// TODO: move rlc lookup for frs into CanonicalRepresentationConfig.
ByteRepresentationConfig::n_rows_required(&u32s, &u64s, &u128s, &frs),
ByteBitGadget::n_rows_required(),
]
Expand Down
34 changes: 34 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,3 +1046,37 @@ fn create_name_registrator_per_txs_not_enough_gas_d0_g0_v0() {
.unwrap(),
);
}

#[test]
fn derp() {
assert!(*HASH_SCHEME_DONE);
let mut generator = WitnessGenerator::from(&ZktrieState::default());
generator.handle_new_state(
mpt_zktrie::mpt_circuits::MPTProofType::BalanceChanged,
Address::repeat_byte(1),
U256::from(23),
U256::zero(),
None,
);

let trace = generator.handle_new_state(
mpt_zktrie::mpt_circuits::MPTProofType::AccountDoesNotExist,
Address::repeat_byte(2),
U256::zero(),
U256::zero(),
None,
);
let json = serde_json::to_string_pretty(&trace).unwrap();
let trace: SMTTrace = serde_json::from_str(&json).unwrap();

let witness = vec![(MPTProofType::AccountDoesNotExist, trace); 3000];
let proofs: Vec<_> = witness.clone().into_iter().map(Proof::from).collect();

let n_rows_required = MptCircuitConfig::n_rows_required(&proofs);

let circuit = TestCircuit::new(n_rows_required, witness);
let prover = MockProver::<Fr>::run(14, &circuit, vec![]).unwrap();
assert_eq!(prover.verify(), Ok(()),);

panic!();
}

0 comments on commit 193f667

Please sign in to comment.