Skip to content

Commit aaa2a75

Browse files
committed
Clean up subtree consistency proof verification logic
Use language from ietf-plants-wg/merkle-tree-certs#139 which aims to clarify some ambiguities.
1 parent d5140e1 commit aaa2a75

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

crates/tlog_tiles/src/tlog.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ pub fn subtree_inclusion_proof_indexes(
463463
/// # Errors
464464
///
465465
/// Will return an error if proof verification fails.
466+
///
467+
/// # Panics
468+
///
469+
/// Will panic if there are internal math errors.
466470
pub fn verify_inclusion_proof(
467471
proof: &Proof,
468472
tree_size: u64,
@@ -490,7 +494,9 @@ pub fn verify_inclusion_proof(
490494
// i. Set r to HASH(0x01 || p || r).
491495
r = node_hash(*p, r);
492496
// ii. If LSB(fn) is not set, then right-shift both fn and sn equally until either LSB(fn) is set or fn is 0.
493-
while !lsb_set(f_n) || f_n == 0 {
497+
//
498+
// NOTE: It must be the case that fn is non-zero, so we can simplify.
499+
while !lsb_set(f_n) {
494500
f_n >>= 1;
495501
s_n >>= 1;
496502
}
@@ -630,6 +636,10 @@ pub fn verify_consistency_proof(
630636
/// # Errors
631637
///
632638
/// Will return an error if proof verification fails.
639+
///
640+
/// # Panics
641+
///
642+
/// Will panic if there are internal math errors.
633643
pub fn verify_subtree_consistency_proof(
634644
proof: &Proof,
635645
n: u64,
@@ -645,8 +655,8 @@ pub fn verify_subtree_consistency_proof(
645655
let mut s_n = end - 1;
646656
// 2. Set r to node_hash.
647657
let mut r = subtree_hash;
648-
// 3. Right-shift fn and sn equally until LSB(fn) is set or sn is zero.
649-
while !(lsb_set(f_n) || s_n == 0) {
658+
// 3. Until LSB(fn) is set or sn is 0, right-shift fn and sn equally.
659+
while !lsb_set(f_n) && s_n != 0 {
650660
f_n >>= 1;
651661
s_n >>= 1;
652662
}
@@ -658,14 +668,14 @@ pub fn verify_subtree_consistency_proof(
658668
}
659669
// 2. Set r to HASH(0x01, || p || r).
660670
r = node_hash(*p, r);
661-
// 3. If LSB(sn) is not set, the right-shift sn until either LSB(sn) is set or sn is zero.
662-
while !(lsb_set(s_n) || s_n == 0) {
671+
// 3. Until LSB(sn) is set, right-shift sn.
672+
while !lsb_set(s_n) {
663673
s_n >>= 1;
664674
}
665-
// 4. Right-shift once more.
675+
// 4. Right-shift sn once more.
666676
s_n >>= 1;
667677
}
668-
// 5. Check sn is 0 and r is root_hash. If either is not equal, fail the proof verification. If all are equal, accept the proof.
678+
// 5. Compare sn to 0 and r to root_hash. If either is not equal, fail the proof verification. If all are equal, accept the proof.
669679
if s_n == 0 && r == root_hash {
670680
Ok(())
671681
} else {
@@ -687,7 +697,7 @@ pub fn verify_subtree_consistency_proof(
687697
let mut f_n = start;
688698
let mut s_n = end - 1;
689699
let mut t_n = n - 1;
690-
// 4. Right-shift fn, sn, and tn equally until LSB(sn) is not set or fn = sn.
700+
// 4. Until LSB(sn) is not set or fn is equal to sn, right-shift fn, sn, and tn equally.
691701
while lsb_set(s_n) && f_n != s_n {
692702
f_n >>= 1;
693703
s_n >>= 1;
@@ -710,27 +720,24 @@ pub fn verify_subtree_consistency_proof(
710720
}
711721
// 2. Set sr to HASH(0x01 || c || sr).
712722
s_r = node_hash(c, s_r);
713-
// 3. If LSB(sn) is not set, then right-shift each of fn, sn, and tn equally until either LSB(sn) is set or sn is 0.
723+
// 3. Until LSB(sn) is set, right-shift fn, sn, and tn equally.
714724
while !lsb_set(s_n) {
715725
f_n >>= 1;
716726
s_n >>= 1;
717727
t_n >>= 1;
718-
if s_n == 0 {
719-
break;
720-
}
721728
}
722729
}
723730
// 3. Otherwise:
724731
else {
725732
// 1. Set sr to HASH(0x01 || sr || c).
726733
s_r = node_hash(s_r, c);
727734
}
728-
// 4. Finally, right-shift each of fn, sn, and tn one time.
735+
// 4. Right-shift fn, sn, and tn once more.
729736
f_n >>= 1;
730737
s_n >>= 1;
731738
t_n >>= 1;
732739
}
733-
// 7. Check tn is 0, fr is node_hash, and sr is root_hash. If any are not equal, fail the proof verification. If all are equal, accept the proof.
740+
// 7. Compare tn to 0, fr to node_hash, and sr to root_hash. If any are not equal, fail the proof verification. If all are equal, accept the proof.
734741
if t_n == 0 && f_r == subtree_hash && s_r == root_hash {
735742
Ok(())
736743
} else {

0 commit comments

Comments
 (0)