-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(weigh_justification_and_finalization): pair programming WIP
Co-authored-by: Xearty <[email protected]>
- Loading branch information
Showing
11 changed files
with
4,884 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
3 changes: 3 additions & 0 deletions
3
casper-finality-proofs/bin/weigh_justification_and_finalization.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"moduleResolution": "nodenext", | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
casper-finality-proofs/js_helpers/weigh_justification_and_finalization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod weigh_justification_and_finalization; | ||
pub mod checkpoint; |
Oops, something went wrong.