Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayKostadinov21 committed Dec 20, 2023
1 parent 1960626 commit 511782f
Show file tree
Hide file tree
Showing 263 changed files with 3,480 additions and 299 deletions.
119 changes: 83 additions & 36 deletions casper-finality-proofs/src/prove_finality/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,10 @@ impl Circuit for ProveFinality {
let justification_bits = builder.read::<JustificationBitsVariable>();
let previous_epoch_attested_validators = builder.read::<Gwei>();
let current_epoch_attested_validators = builder.read::<Gwei>();
let previous_justified_checkpoint = builder.read::<CheckpointVariable>();
let current_justified_checkpoint = builder.read::<CheckpointVariable>();

let zero = builder.constant::<U64Variable>(0);
let one = builder.constant::<U64Variable>(1);
let two = builder.constant::<U64Variable>(2);
let thirty_two = builder.constant::<U64Variable>(32);

let target_source_difference = builder.sub(target.epoch, source.epoch);
let target_source_difference_equals_one = builder.is_equal(target_source_difference, one);
let target_source_difference_equals_two = builder.is_equal(target_source_difference, two);
let target_source_difference_equals_one_or_two = builder.or(
target_source_difference_equals_one,
target_source_difference_equals_two,
);
assert_is_true(builder, target_source_difference_equals_one_or_two);

let current_epoch = builder.div(slot, thirty_two);
let current_epoch_target_difference = builder.sub(current_epoch, target.epoch);
let target_is_first_bit = builder.is_equal(current_epoch_target_difference, zero);
let target_is_second_bit = builder.is_equal(current_epoch_target_difference, one);
let is_target_first_or_second_bit = builder.or(target_is_first_bit, target_is_second_bit);
assert_is_true(builder, is_target_first_or_second_bit);
validate_target_source_difference(builder, source.clone(), target.clone());

let new_justification_bits = process_justifications(
builder,
Expand All @@ -52,24 +35,21 @@ impl Circuit for ProveFinality {
current_epoch_attested_validators,
);

let thirty_two = builder.constant::<U64Variable>(32);
let new_justification_bits = new_justification_bits.bits.as_slice();
let current_epoch = builder.div(slot, thirty_two);
let source_index = builder.sub(current_epoch, source.epoch);
let target_index = current_epoch_target_difference;
for i in 0..4 {
let current_index = builder.constant::<U64Variable>(i as u64);
let in_range_source_index = builder.lte(current_index, source_index);
let in_range_target_index = builder.gte(current_index, target_index);

let in_range = builder.and(in_range_source_index, in_range_target_index);

let in_range_or_justification_bits_value =
builder.or(new_justification_bits[i], in_range);

builder.assert_is_equal(
new_justification_bits[i],
in_range_or_justification_bits_value,
);
}
let target_index = builder.sub(current_epoch, target.epoch);

validate_source(
builder,
source,
target_index,
previous_justified_checkpoint,
current_justified_checkpoint,
);

validate_justification_bits(builder, source_index, target_index, new_justification_bits);
}
}

Expand Down Expand Up @@ -122,3 +102,70 @@ fn is_supermajority_link_in_votes<L: PlonkParameters<D>, const D: usize>(
total_num_validators_four_times,
)
}

pub fn validate_target_source_difference<L: PlonkParameters<D>, const D: usize>(
builder: &mut CircuitBuilder<L, D>,
source: CheckpointVariable,
target: CheckpointVariable,
) {
let one = builder.one();
let two = builder.constant::<U64Variable>(2);

let target_source_difference = builder.sub(target.epoch, source.epoch);
let target_source_difference_equals_one = builder.is_equal(target_source_difference, one);
let target_source_difference_equals_two = builder.is_equal(target_source_difference, two);
let target_source_difference_equals_one_or_two = builder.or(
target_source_difference_equals_one,
target_source_difference_equals_two,
);
assert_is_true(builder, target_source_difference_equals_one_or_two);
}

pub fn validate_source<L: PlonkParameters<D>, const D: usize>(
builder: &mut CircuitBuilder<L, D>,
source: CheckpointVariable,
target_idx: U64Variable,
previous_justified_checkpoint: CheckpointVariable,
current_justified_checkpoint: CheckpointVariable,
) {
let zero = builder.zero();
let one = builder.one();

let source_is_current_justified_checkpoint_pred =
builder.is_equal(source.clone(), current_justified_checkpoint);
let source_is_previous_justified_checkpoint_pred =
builder.is_equal(source.clone(), previous_justified_checkpoint);

let target_is_current_epoch_pred = builder.is_equal(target_idx, zero);
let target_is_previous_epoch_pred = builder.is_equal(target_idx, one);

let is_valid_pair_1_pred = builder.and(
target_is_current_epoch_pred,
source_is_current_justified_checkpoint_pred,
);
let is_valid_pair_2_pred = builder.and(
target_is_previous_epoch_pred,
source_is_previous_justified_checkpoint_pred,
);
let is_valid_pair_pred = builder.or(is_valid_pair_1_pred, is_valid_pair_2_pred);
assert_is_true(builder, is_valid_pair_pred);
}

pub fn validate_justification_bits<L: PlonkParameters<D>, const D: usize>(
builder: &mut CircuitBuilder<L, D>,
source_index_epoch: U64Variable,
target_index_epoch: U64Variable,
justification_bits: &[BoolVariable],
) {
for i in 0..4 {
let current_index = builder.constant::<U64Variable>(i as u64);
let in_range_source_index = builder.lte(current_index, source_index_epoch);
let in_range_target_index = builder.gte(current_index, target_index_epoch);

let in_range = builder.and(in_range_source_index, in_range_target_index);

let in_range_or_justification_bits_value = builder.or(justification_bits[i], in_range);

builder.assert_is_equal(justification_bits[i], in_range_or_justification_bits_value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456788,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
false,
false,
false
]
],
"previous_justified_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"current_justified_checkpoint": {
"epoch": 123456789,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
},
"finalized_checkpoint": {
"epoch": 0,
"root": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
Loading

0 comments on commit 511782f

Please sign in to comment.