Skip to content

Commit

Permalink
feat(weigh_justification_and_finalization): pair programming WIP
Browse files Browse the repository at this point in the history
Co-authored-by: Xearty <[email protected]>
  • Loading branch information
Dimo99 and Xearty committed Oct 11, 2023
1 parent 3bab833 commit 8a1472d
Show file tree
Hide file tree
Showing 11 changed files with 4,884 additions and 9 deletions.
4,424 changes: 4,424 additions & 0 deletions casper-finality-proofs/Cargo.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions casper-finality-proofs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "casper-finality-proofs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "weigh_justification_and_finalization"
path = "bin/weigh_justification_and_finalization.rs"

[dependencies]
plonky2 = { git = "https://github.com/mir-protocol/plonky2.git", default-features = false}
plonky2x = { git = "https://github.com/succinctlabs/succinctx.git", branch = "main" }
serde = { version = "1.0.187", features = ["derive"] }
serde_json = "1.0.103"
ethers = { version = "2.0" }
itertools = { version = "0.10.0", default-features = false }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
6 changes: 6 additions & 0 deletions casper-finality-proofs/js_helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"moduleResolution": "nodenext",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Tree } from '@chainsafe/persistent-merkle-tree';
import { BeaconApi } from '../../relay/implementations/beacon-api';
import { bytesToHex } from '../../libs/typescript/ts-utils/bls';
import { hexToBits } from '../../libs/typescript/ts-utils/hex-utils';

(async () => {
const beaconApi = new BeaconApi([
'http://unstable.mainnet.beacon-api.nimbus.team',
]);
const { beaconState } = await beaconApi.getBeaconState(6953401);

console.log(beaconState.justificationBits.get(0));
console.log(beaconState.justificationBits.get(1));
console.log(beaconState.justificationBits.get(2));
console.log(beaconState.justificationBits.get(3));

const { ssz } = await import('@lodestar/types');

console.log(
'justification bits index',
ssz.capella.BeaconState.getPathInfo(['justification_bits']).gindex,
);

console.log(
'previous justified checkpoint index',
ssz.capella.BeaconState.getPathInfo(['previous_justified_checkpoint'])
.gindex,
);

console.log(
'current justified checkpoint index',
ssz.capella.BeaconState.getPathInfo(['current_justified_checkpoint'])
.gindex,
);

console.log(
'blocks roots index',
ssz.capella.BeaconState.getPathInfo(['block_roots']).gindex,
);

console.log(
'index in the block roots of 123',
ssz.capella.BeaconState.fields.blockRoots.getPathInfo([123]).gindex,
);

let blocks_root_index = ssz.capella.BeaconState.getPathInfo([
'block_roots',
]).gindex;

let epoch_index = ssz.capella.BeaconState.fields.blockRoots.getPathInfo([
123,
]).gindex;

console.log(
'combined index',
BigInt('0b' + blocks_root_index.toString(2) + epoch_index.toString(2)),
);

const beaconStateViewDU = ssz.capella.BeaconState.toViewDU(beaconState);

const tree = new Tree(beaconStateViewDU.node);

console.log(tree.getSingleProof(blocks_root_index).map(bytesToHex));

const blocksRootViewDU = ssz.capella.BeaconState.fields.blockRoots.toViewDU(
beaconState.blockRoots,
);
const blocksRootTree = new Tree(blocksRootViewDU.node);

console.log(blocksRootTree.getSingleProof(epoch_index).map(bytesToHex));

console.log(
'combined proof',
[
...tree.getSingleProof(blocks_root_index),
...blocksRootTree.getSingleProof(epoch_index),
].map(bytesToHex),
);

// beaconState.slot = 12;
// beaconState.balances = [1234];

// const { ssz } = await import('@lodestar/types');
// const pathInfo = ssz.capella.BeaconState.getPathInfo(['historical_summaries']);

// console.log(pathInfo.gindex);

// console.log(beaconState.slot);

// console.log(bytesToHex(ssz.capella.BeaconState.fields.slot.hashTreeRoot(beaconState.slot)));

// const beaconStateViewDU = ssz.capella.BeaconState.toViewDU(beaconState);

// const tree = new Tree(beaconStateViewDU.node);

// const proof = tree.getSingleProof(pathInfo.gindex);

// console.log(proof.map(bytesToHex));
})();
145 changes: 145 additions & 0 deletions casper-finality-proofs/src/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
use plonky2x::{
frontend::vars::SSZVariable,
prelude::{Bytes32Variable, CircuitVariable, U64Variable},
};
use serde::Deserialize;
use itertools::Itertools;

#[derive(Debug, Clone, Copy)]
pub struct CheckpointVariable {
pub epoch: U64Variable,
pub root: Bytes32Variable,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Checkpoint {
pub epoch: u64,
pub root: String,
}

// TODO: implement
impl CircuitVariable for CheckpointVariable {
type ValueType<F: plonky2x::prelude::RichField> = Checkpoint;

fn init_unsafe<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
) -> Self {
todo!()
}

fn variables(&self) -> Vec<plonky2x::prelude::Variable> {
todo!()
}

fn from_variables_unsafe(variables: &[plonky2x::prelude::Variable]) -> Self {
todo!()
}

fn assert_is_valid<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
&self,
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
) {
todo!()
}

fn elements<F: plonky2x::prelude::RichField>(value: Self::ValueType<F>) -> Vec<F> {
todo!()
}

fn from_elements<F: plonky2x::prelude::RichField>(elements: &[F]) -> Self::ValueType<F> {
todo!()
}

fn init<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
) -> Self {
let variable = Self::init_unsafe(builder);
variable.assert_is_valid(builder);
variable
}

fn constant<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
value: Self::ValueType<L::Field>,
) -> Self {
let field_elements = Self::elements::<L::Field>(value);
let variables = field_elements
.into_iter()
.map(|element| builder.constant::<plonky2x::prelude::Variable>(element))
.collect_vec();
// Because this is a constant, we do not need to add constraints to ensure validity
// as it is assumed that the value is valid.
Self::from_variables_unsafe(&variables)
}

fn from_variables<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
variables: &[plonky2x::prelude::Variable],
) -> Self {
let variable = Self::from_variables_unsafe(variables);
variable.assert_is_valid(builder);
variable
}

fn get<F: plonky2x::prelude::RichField, W: plonky2x::prelude::Witness<F>>(
&self,
witness: &W,
) -> Self::ValueType<F> {
let target_values = self
.targets()
.into_iter()
.map(|t| witness.get_target(t))
.collect::<Vec<F>>();
Self::from_elements::<F>(&target_values)
}

fn set<F: plonky2x::prelude::RichField, W: plonky2x::prelude::WitnessWrite<F>>(
&self,
witness: &mut W,
value: Self::ValueType<F>,
) {
let elements = Self::elements::<F>(value);
let targets = self.targets();
assert_eq!(elements.len(), targets.len());
for (element, target) in elements.into_iter().zip(targets.into_iter()) {
witness.set_target(target, element);
}
}

fn targets(&self) -> Vec<plonky2x::prelude::Target> {
self.variables().into_iter().map(|v| v.0).collect()
}

fn from_targets(targets: &[plonky2x::prelude::Target]) -> Self {
Self::from_variables_unsafe(
&targets
.iter()
.map(|t| plonky2x::prelude::Variable(*t))
.collect_vec(),
)
}

fn nb_elements() -> usize {
type L = plonky2x::prelude::DefaultParameters;
const D: usize = 2;
plonky2x::utils::disable_logging();
let mut builder = plonky2x::prelude::CircuitBuilder::<L, D>::new();
let variable = builder.init_unsafe::<Self>();
plonky2x::utils::enable_logging();
variable.variables().len()
}
}

impl SSZVariable for CheckpointVariable {
fn hash_tree_root<L: plonky2x::prelude::PlonkParameters<D>, const D: usize>(
&self,
builder: &mut plonky2x::prelude::CircuitBuilder<L, D>,
) -> Bytes32Variable {
let epoch_leaf = self.epoch.hash_tree_root(builder);
let root_leaf = self.root.hash_tree_root(builder);

builder.sha256_pair(epoch_leaf, root_leaf)
}
}

// TODO: test
2 changes: 2 additions & 0 deletions casper-finality-proofs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod weigh_justification_and_finalization;
pub mod checkpoint;
Loading

0 comments on commit 8a1472d

Please sign in to comment.