Skip to content

Commit

Permalink
Disambiguate between rlc lookups (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Mason Liang <[email protected]>
  • Loading branch information
z2trillion and Mason Liang authored Sep 24, 2023
1 parent 0bae9ee commit b5ea508
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
30 changes: 17 additions & 13 deletions src/gadgets/canonical_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use super::super::constraint_builder::{
AdviceColumn, BinaryColumn, ConstraintBuilder, FixedColumn, Query, SecondPhaseAdviceColumn,
SelectorColumn,
};
use super::{
byte_bit::RangeCheck256Lookup, byte_representation::RlcLookup, is_zero::IsZeroGadget,
rlc_randomness::RlcRandomness,
};
use super::{byte_bit::RangeCheck256Lookup, is_zero::IsZeroGadget, rlc_randomness::RlcRandomness};
use ethers_core::types::U256;
use halo2_proofs::{
arithmetic::{Field, FieldExt},
Expand All @@ -20,6 +17,11 @@ pub trait CanonicalRepresentationLookup {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 3];
}

// Lookup to prove that Rlc(x: Fr) = y
pub trait FrRlcLookup {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 2];
}

#[derive(Clone)]
pub struct CanonicalRepresentationConfig {
// Lookup columns
Expand All @@ -30,9 +32,9 @@ pub struct CanonicalRepresentationConfig {

// Witness columns
index_is_zero: SelectorColumn, // (0..32).repeat().map(|i| i == 0)
// index_is_31: SelectorColumn, // (0..32).repeat().map(|i| i == 31)
modulus_byte: FixedColumn, // (0..32).repeat().map(|i| Fr::MODULUS.to_be_bytes()[i])
difference: AdviceColumn, // modulus_byte - byte
index_is_31: SelectorColumn, // (0..32).repeat().map(|i| i == 31)
modulus_byte: FixedColumn, // (0..32).repeat().map(|i| Fr::MODULUS.to_be_bytes()[i])
difference: AdviceColumn, // modulus_byte - byte
difference_is_zero: IsZeroGadget,
differences_are_zero_so_far: BinaryColumn, // difference[0] ... difference[index - 1] are all 0.
}
Expand All @@ -44,7 +46,7 @@ impl CanonicalRepresentationConfig {
range_check: &impl RangeCheck256Lookup,
randomness: &RlcRandomness,
) -> Self {
let ([index_is_zero], [index, modulus_byte], [value, byte, difference]) =
let ([index_is_zero, index_is_31], [index, modulus_byte], [value, byte, difference]) =
cb.build_columns(cs);
let [rlc] = cb.second_phase_advice_columns(cs);

Expand Down Expand Up @@ -120,6 +122,7 @@ impl CanonicalRepresentationConfig {
byte,
rlc,
index_is_zero,
index_is_31,
modulus_byte,
difference,
difference_is_zero,
Expand Down Expand Up @@ -153,6 +156,8 @@ impl CanonicalRepresentationConfig {
.assign(region, offset, u64::try_from(index).unwrap());
if index.is_zero() {
self.index_is_zero.enable(region, offset);
} else if index == 31 {
self.index_is_31.enable(region, offset);
}

let difference = Fr::from(u64::from(*modulus_byte)) - Fr::from(u64::from(*byte));
Expand Down Expand Up @@ -187,12 +192,11 @@ impl CanonicalRepresentationLookup for CanonicalRepresentationConfig {
}
}

impl RlcLookup for CanonicalRepresentationConfig {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 3] {
impl FrRlcLookup for CanonicalRepresentationConfig {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 2] {
[
self.value.current(),
self.rlc.current(),
self.index.current(),
self.value.current() * self.index_is_31.current(),
self.rlc.current() * self.index_is_31.current(),
]
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/gadgets/mpt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use word_rlc::{assign as assign_word_rlc, configure as configure_word_rlc};

use super::{
byte_representation::{BytesLookup, RlcLookup},
canonical_representation::FrRlcLookup,
is_zero::IsZeroGadget,
key_bit::KeyBitLookup,
one_hot::OneHot,
Expand Down Expand Up @@ -110,7 +111,7 @@ impl MptUpdateConfig {
rlc: &impl RlcLookup,
bytes: &impl BytesLookup,
rlc_randomness: &RlcRandomness,
fr_rlc: &impl RlcLookup,
fr_rlc: &impl FrRlcLookup,
) -> Self {
let proof_type: OneHot<MPTProofType> = OneHot::configure(cs, cb);
let [storage_key_rlc, old_value, new_value] = cb.second_phase_advice_columns(cs);
Expand Down Expand Up @@ -158,12 +159,12 @@ impl MptUpdateConfig {
);
cb.add_lookup(
"rlc_old_root = rlc(old_root)",
[old_hash.current(), old_hash_rlc.current(), Query::from(31)],
[old_hash.current(), old_hash_rlc.current()],
fr_rlc.lookup(),
);
cb.add_lookup(
"rlc_new_root = rlc(new_root)",
[new_hash.current(), new_hash_rlc.current(), Query::from(31)],
[new_hash.current(), new_hash_rlc.current()],
fr_rlc.lookup(),
);
});
Expand Down

0 comments on commit b5ea508

Please sign in to comment.