Skip to content

Commit

Permalink
Code cleanup (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: ControlC ControlV <[email protected]>
  • Loading branch information
ControlCplusControlV and ControlC ControlV authored Aug 7, 2022
1 parent 5812004 commit e31712b
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 127 deletions.
Binary file removed .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
Cargo.lock
notes.md
.DS_Store
notes.md
16 changes: 8 additions & 8 deletions verkle-spec/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ pub fn chunkify_code(code: Vec<u8>) -> Vec<Bytes32> {
let mut leftover_push_data = 0usize;
remaining_pushdata_bytes.push(leftover_push_data);

let last_chunk_index = chunked_code31.len()-1;
let last_chunk_index = chunked_code31.len() - 1;
// set this to true, if the last chunk had a push data instruction that
// needed another chunk
let mut last_chunk_push_data = false;
let mut last_chunk_push_data = false;
for (chunk_i, chunk) in chunked_code31.clone().enumerate() {
// Case1: the left over push data is larger than the chunk size
//
// The left over push data can be larger than the chunk size
// For example, if the last instruction was a PUSH32 and chunk size is 31
// We can compute the left over push data for this chunk as 31, the chunk size
// and then the left over push data for the next chunk as 32-31=1
if leftover_push_data > chunk.len() {
if leftover_push_data > chunk.len() {
if chunk_i == last_chunk_index {
last_chunk_push_data = true;
break
last_chunk_push_data = true;
break;
}

leftover_push_data = leftover_push_data - chunk.len();
leftover_push_data -= chunk.len();
remaining_pushdata_bytes.push(chunk.len());
continue;
}
Expand Down Expand Up @@ -103,8 +103,8 @@ pub fn chunkify_code(code: Vec<u8>) -> Vec<Bytes32> {

if last_chunk_push_data {
// If the last chunk had remaining push data to be added
// we add a new chunk with 32 zeroes. This is fine
chunked_code32.push([0u8;32])
// we add a new chunk with 32 zeroes. This is fine
chunked_code32.push([0u8; 32])
}

chunked_code32
Expand Down
Binary file removed verkle-trie/.DS_Store
Binary file not shown.
Binary file removed verkle-trie/src/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion verkle-trie/src/committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub trait Committer {
result += self.scalar_mul(value, lagrange_index)
}

return result;
result
}
}
10 changes: 5 additions & 5 deletions verkle-trie/src/committer/precompute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> Committer for &'a PrecomputeLagrange {
let mut result = Element::zero();

let scalar_table = evaluations
.into_iter()
.iter()
.zip(self.inner.iter())
.filter(|(evals, _)| !evals.is_zero());

Expand All @@ -35,7 +35,7 @@ impl<'a> Committer for &'a PrecomputeLagrange {
.enumerate()
.map(|(row, byte)| {
let point = table.point(row, byte);
Element::from(*point)
*point
})
.sum();
result += partial_result;
Expand All @@ -52,7 +52,7 @@ impl<'a> Committer for &'a PrecomputeLagrange {
.enumerate()
.map(|(row, byte)| {
let point = table.point(row, byte);
Element::from(*point)
*point
})
.sum();
result
Expand Down Expand Up @@ -81,7 +81,7 @@ impl PrecomputeLagrange {
use rayon::prelude::*;
lagrange_points
.into_par_iter()
.map(|point| LagrangeTablePoints::new(point))
.map(LagrangeTablePoints::new)
.collect()
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl LagrangeTablePoints {
// Given [G_1, 2G_1, 3G_1, ... num_points * G_1] and a scalar `k`
// Returns [k * G_1, 2 * k * G_1, 3 * k * G_1, ... num_points * k * G_1]
fn scale_row(points: &[Element], scale: Fr) -> Vec<Element> {
let scaled_row: Vec<Element> = points.into_iter().map(|element| *element * scale).collect();
let scaled_row: Vec<Element> = points.iter().map(|element| *element * scale).collect();

scaled_row
}
Expand Down
5 changes: 1 addition & 4 deletions verkle-trie/src/committer/test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{committer::Committer, constants::CRS};
use ark_ec::ProjectiveCurve;
use ark_ff::PrimeField;
use ark_ff::Zero;

use banderwagon::{Element, Fr};

// A Basic Commit struct to be used in tests.
// In production, we will use the Precomputed points
#[derive(Debug, Clone, Copy)]
Expand Down
11 changes: 6 additions & 5 deletions verkle-trie/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::committer::{precompute::PrecomputeLagrange, test::TestCommitter};
use crate::constants::CRS;
use ark_ec::ProjectiveCurve;
use crate::{
committer::{precompute::PrecomputeLagrange, test::TestCommitter},
constants::CRS,
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use std::fs::File;
/// Generic configuration file to initialise a verkle trie struct
Expand All @@ -14,7 +15,7 @@ pub struct Config<Storage, PolyCommit> {
// to pre-compute. The means that in production, one will not need to recompute
// them. It is possible to use this for tests too, one should ensure that the file exists
// before running the tests; which are ran in parallel.
const PRECOMPUTED_POINTS_PATH: &'static str = "precomputed_points.bin";
const PRECOMPUTED_POINTS_PATH: &str = "precomputed_points.bin";

// TODO: These two functions return Strings, when they should return a result with an enum variant ideally
// TODO: This is an API change and will be done in the API refactor phase.
Expand Down Expand Up @@ -47,7 +48,7 @@ impl<Storage> VerkleConfig<Storage> {
let mut file = File::open(PRECOMPUTED_POINTS_PATH).unwrap();
let committer: PrecomputeLagrange =
CanonicalDeserialize::deserialize_unchecked(&mut file).unwrap();
return Ok(Config { db, committer });
Ok(Config { db, committer })
}
}

Expand Down
2 changes: 1 addition & 1 deletion verkle-trie/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub const FLUSH_BATCH: u32 = 20_000;
pub const VERKLE_NODE_WIDTH: usize = 256;
// Seed used to compute the 256 pedersen generators
// using try-and-increment
const PEDERSEN_SEED: &'static [u8] = b"eth_verkle_oct_2021";
const PEDERSEN_SEED: &[u8] = b"eth_verkle_oct_2021";

pub(crate) const TWO_POW_128: Fr = Fr::new(BigInteger256([
3195623856215021945,
Expand Down
Binary file removed verkle-trie/src/database/.DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions verkle-trie/src/database/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<T: BareMetalKVDb> ReadOnlyHigherDb for GenericBatchDB<T> {

let mut labelled_key = Vec::with_capacity(branch_id.len() + 1);
labelled_key.push(BRANCH_TABLE_MARKER);
labelled_key.extend_from_slice(&branch_id);
labelled_key.extend_from_slice(branch_id);

for i in 0u8..=255 {
let mut child = labelled_key.clone();
Expand All @@ -135,7 +135,7 @@ impl<T: BareMetalKVDb> ReadOnlyHigherDb for GenericBatchDB<T> {
fn get_branch_meta(&self, key: &[u8]) -> Option<BranchMeta> {
let mut labelled_key = Vec::with_capacity(key.len() + 1);
labelled_key.push(BRANCH_TABLE_MARKER);
labelled_key.extend_from_slice(&key);
labelled_key.extend_from_slice(key);

self.inner
.fetch(&labelled_key)
Expand All @@ -146,7 +146,7 @@ impl<T: BareMetalKVDb> ReadOnlyHigherDb for GenericBatchDB<T> {
let mut labelled_key = Vec::with_capacity(branch_id.len() + 2);
labelled_key.push(BRANCH_TABLE_MARKER);

labelled_key.extend_from_slice(&branch_id);
labelled_key.extend_from_slice(branch_id);
labelled_key.push(index);
self.inner
.fetch(&labelled_key)
Expand Down
10 changes: 8 additions & 2 deletions verkle-trie/src/database/memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl MemoryDb {
}
}

impl Default for MemoryDb {
fn default() -> Self {
Self::new()
}
}

impl ReadOnlyHigherDb for MemoryDb {
fn get_stem_meta(&self, stem_key: [u8; 31]) -> Option<StemMeta> {
self.stem_table.get(&stem_key).copied()
Expand Down Expand Up @@ -97,7 +103,7 @@ impl ReadOnlyHigherDb for MemoryDb {

fn get_branch_child(&self, branch_id: &[u8], index: u8) -> Option<BranchChild> {
let mut child_index = Vec::with_capacity(branch_id.len());
child_index.extend_from_slice(&branch_id);
child_index.extend_from_slice(branch_id);
child_index.push(index);

self.branch_table.get(&child_index).copied()
Expand All @@ -116,7 +122,7 @@ impl WriteOnlyHigherDb for MemoryDb {
};
match b_child {
BranchChild::Stem(_) => None, // If its a stem, we return None, this only happens in ChainInsert
BranchChild::Branch(b_meta) => return Some(b_meta),
BranchChild::Branch(b_meta) => Some(b_meta),
}
}

Expand Down
25 changes: 12 additions & 13 deletions verkle-trie/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::constants::CRS;
use ark_ec::AffineCurve;
use crate::constants::{CRS, PRECOMPUTED_WEIGHTS};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};

use banderwagon::Element;
use ipa_multipoint::multiproof::MultiPointProof;
use ipa_multipoint::transcript::Transcript;
use std::collections::{BTreeMap, BTreeSet};

use std::io::{Error, ErrorKind, Result, Read, Write};

// TODO: We use the IO Result while we do not have a dedicated Error enum
type IOResult<T> = ark_std::io::Result<T>;
type IOError = ark_std::io::Error;
type IOErrorKind = ark_std::io::ErrorKind;
type IOResult<T> = Result<T>;
type IOError = Error;
type IOErrorKind = ErrorKind;

mod key_path_finder;
mod opening_data;
Expand Down Expand Up @@ -69,7 +71,7 @@ impl VerificationHint {
// We need the number of keys because we do not serialise the length of
// the ext_status|| depth. This is equal to the number of keys in the proof, which
// we assume the user knows.
pub fn read<R: ark_std::io::Read>(mut reader: R) -> IOResult<VerificationHint> {
pub fn read<R: Read>(mut reader: R) -> IOResult<VerificationHint> {
// First extract the stems with no values opened for them
let mut num_stems = [0u8; 4];
reader.read_exact(&mut num_stems)?;
Expand Down Expand Up @@ -115,7 +117,7 @@ impl VerificationHint {
diff_stem_no_proof,
})
}
pub fn write<W: ark_std::io::Write>(&self, writer: &mut W) -> IOResult<()> {
pub fn write<W: Write>(&self, writer: &mut W) -> IOResult<()> {
// Encode the number of stems with no value openings
let num_stems = self.diff_stem_no_proof.len() as u32;
writer.write(&num_stems.to_le_bytes());
Expand Down Expand Up @@ -152,7 +154,7 @@ impl VerificationHint {
// Encode depth into the byte, it should only be less
// than or equal to 32, and so we only need 5 bits.
debug_assert!(*depth <= 32);
byte = byte | (depth << 3);
byte |= depth << 3;

writer.write(&[byte])?;
}
Expand Down Expand Up @@ -180,7 +182,7 @@ pub struct VerkleProof {
}

impl VerkleProof {
pub fn read<R: ark_std::io::Read>(mut reader: R) -> IOResult<VerkleProof> {
pub fn read<R: Read>(mut reader: R) -> IOResult<VerkleProof> {
let verification_hint = VerificationHint::read(&mut reader)?;

let mut num_comms = [0u8; 4];
Expand All @@ -205,7 +207,7 @@ impl VerkleProof {
})
}

pub fn write<W: ark_std::io::Write>(&self, mut writer: W) -> IOResult<()> {
pub fn write<W: Write>(&self, mut writer: W) -> IOResult<()> {
self.verification_hint.write(&mut writer);

let num_comms = self.comms_sorted.len() as u32;
Expand Down Expand Up @@ -243,9 +245,6 @@ impl VerkleProof {
None => return (false, None),
};

use crate::constants::{PRECOMPUTED_WEIGHTS, VERKLE_NODE_WIDTH};
use ipa_multipoint::transcript::Transcript;

let mut transcript = Transcript::new(b"vt");
let ok = proof.check(&CRS, &PRECOMPUTED_WEIGHTS, &queries, &mut transcript);

Expand Down
2 changes: 1 addition & 1 deletion verkle-trie/src/proof/key_path_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl KeyPathFinder {
//
// We will need this path for update proofs

if &stem_id == &key[0..31] {
if stem_id == key[0..31] {
return KeyPath {
nodes: nodes_by_path,
key_state: KeyState::NotFound(KeyNotFound::StemFound),
Expand Down
13 changes: 6 additions & 7 deletions verkle-trie/src/proof/opening_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use crate::{
};
use ark_ff::{One, PrimeField, Zero};
use banderwagon::Fr;
use ipa_multipoint::lagrange_basis::LagrangeBasis;
use ipa_multipoint::multiproof::{ProverQuery, VerifierQuery};
use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery};
use std::{
collections::{BTreeMap, BTreeSet},
convert::TryInto,
Expand Down Expand Up @@ -167,7 +166,7 @@ impl OpeningData {
// Lets see if it was the stem for the key in question
// If it is for a different stem, then we only need to show
// existence of the extension, and not open C1 or C2
if let Some(_) = key_state.different_stem() {
if key_state.different_stem().is_some() {
opening_data.insert_stem_extension_status(stem, ext_pres);
opening_data.insert_ext_opening(last_node_path, current_stem, last_node_meta);

Expand Down Expand Up @@ -246,7 +245,7 @@ impl ExtOpeningData {
commitment: stem_meta.stem_commitment,
point: 3,
result: stem_meta.hash_c2,
poly: LagrangeBasis::new(ext_func.clone()),
poly: LagrangeBasis::new(ext_func),
};
open_queries.push(open_at_c2);
}
Expand Down Expand Up @@ -312,13 +311,13 @@ impl SuffixOpeningData {
stem_meta.C_2
};
let open_at_val_low = ProverQuery {
commitment: commitment,
commitment,
point: value_lower_index as usize,
result: value_low,
poly: LagrangeBasis::new(c1_or_c2.clone()),
};
let open_at_val_upper = ProverQuery {
commitment: commitment,
commitment,
point: value_upper_index as usize,
result: value_high,
poly: LagrangeBasis::new(c1_or_c2),
Expand Down Expand Up @@ -352,7 +351,7 @@ impl BranchOpeningData {

// Create queries for all of the children we need
for child_index in &self.children {
let child_value = polynomial[*child_index as usize].clone();
let child_value = polynomial[*child_index as usize];

let branch_query = ProverQuery {
commitment: branch_meta.commitment,
Expand Down
13 changes: 6 additions & 7 deletions verkle-trie/src/proof/prover.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use super::{VerificationHint, VerkleProof};
use crate::{
constants::CRS,
constants::{CRS, PRECOMPUTED_WEIGHTS},
database::ReadOnlyHigherDb,
proof::opening_data::{OpeningData, Openings},
};
use ipa_multipoint::multiproof::MultiPoint;
use ipa_multipoint::multiproof::ProverQuery;
use ipa_multipoint::{
multiproof::{MultiPoint, ProverQuery},
transcript::Transcript,
};
use itertools::Itertools;
use std::collections::BTreeSet;

pub fn create_verkle_proof<Storage: ReadOnlyHigherDb>(
storage: &Storage,
keys: Vec<[u8; 32]>,
) -> VerkleProof {
assert!(keys.len() > 0, "cannot create a proof with no keys");
assert!(!keys.is_empty(), "cannot create a proof with no keys");

let (queries, verification_hint) = create_prover_queries(storage, keys);

Expand All @@ -34,9 +36,6 @@ pub fn create_verkle_proof<Storage: ReadOnlyHigherDb>(
.dedup()
.collect();

use crate::constants::{PRECOMPUTED_WEIGHTS, VERKLE_NODE_WIDTH};
use ipa_multipoint::transcript::Transcript;

let mut transcript = Transcript::new(b"vt");
let proof = MultiPoint::open(CRS.clone(), &PRECOMPUTED_WEIGHTS, &mut transcript, queries);

Expand Down
Loading

0 comments on commit e31712b

Please sign in to comment.