You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 5. Compare sn to 0. Compare r against the root_hash. If sn is equal to 0 and r and the root_hash are equal, then the log has proven the inclusion of hash. Otherwise, fail the proof verification.
513
+
// 5. Compare sn to 0. Compare r against the root_hash. If sn is equal to 0
514
+
// and r and the root_hash are equal, then the log has proven the
515
+
// inclusion of hash. Otherwise, fail the proof verification.
// 3. Until LSB(fn) is set or sn is 0, right-shift fn and sn equally.
669
-
while !lsb_set(f_n) && s_n != 0{
659
+
660
+
// Check that `[start, end)` is a valid subtree, and that `end <= n`. If either do not hold, fail proof verification. These checks imply `0 <= start < end <= n`.
661
+
if end > n {
662
+
returnErr(TlogError::InvalidProof);
663
+
}
664
+
665
+
// Set `fn` to `start`, `sn` to `end - 1`, and `tn` to `n - 1`.
666
+
letmut f_n = start;
667
+
letmut s_n = end - 1;
668
+
letmut t_n = n - 1;
669
+
letmut f_r:Hash;
670
+
letmut s_r:Hash;
671
+
// If `sn` is `tn`, then:
672
+
if s_n == t_n {
673
+
// Until `fn` is `sn`, right-shift `fn`, `sn`, and `tn` equally.
674
+
while f_n != s_n {
670
675
f_n >>= 1;
671
676
s_n >>= 1;
677
+
t_n >>= 1;
672
678
}
673
-
// 4. For each value p in the proof array:
674
-
for p in proof {
675
-
// 1. If sn is 0, then stop iteration and fail the proof verification.
676
-
if s_n == 0{
677
-
returnErr(TlogError::InvalidProof);
678
-
}
679
-
// 2. Set r to HASH(0x01, || p || r).
680
-
r = node_hash(*p, r);
681
-
// 3. Until LSB(sn) is set, right-shift sn.
682
-
while !lsb_set(s_n){
683
-
s_n >>= 1;
684
-
}
685
-
// 4. Right-shift sn once more.
679
+
}else
680
+
// Otherwise:
681
+
{
682
+
// Until `LSB(sn)` is not set or `fn` is `sn`, right-shift `fn`, `sn`, and `tn` equally.
683
+
whilelsb_set(s_n) && f_n != s_n {
684
+
f_n >>= 1;
686
685
s_n >>= 1;
687
-
}
688
-
// 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.
689
-
if s_n == 0 && r == root_hash {
690
-
Ok(())
691
-
}else{
692
-
Err(TlogError::InvalidProof)
686
+
t_n >>= 1;
693
687
}
694
688
}
695
-
// 2. Otherwise, run the following:
696
-
else{
697
-
// 1. If proof is an empty array, stop and fail verification.
689
+
// If `fn` is `sn`, set `fr` and `sr` to `node_hash`.
690
+
let proof_offset;
691
+
if f_n == s_n {
692
+
f_r = subtree_hash;
693
+
s_r = subtree_hash;
694
+
proof_offset = 0;
695
+
}else
696
+
// Otherwise:
697
+
{
698
+
// If `proof` is an empty array, stop and fail verification.
698
699
if proof.is_empty(){
699
700
returnErr(TlogError::InvalidProof);
700
701
}
701
-
// 2. If end - start is an exact power of 2, prepend node_hash to the proof array.
702
-
letmut proof = proof.clone();
703
-
if(end - start).is_power_of_two(){
704
-
proof.insert(0, subtree_hash);
705
-
}
706
-
// 3. Set fn to start, sn to end - 1, and tn to n - 1.
707
-
letmut f_n = start;
708
-
letmut s_n = end - 1;
709
-
letmut t_n = n - 1;
710
-
// 4. Until LSB(sn) is not set or fn is equal to sn, right-shift fn, sn, and tn equally.
711
-
whilelsb_set(s_n) && f_n != s_n {
712
-
f_n >>= 1;
713
-
s_n >>= 1;
714
-
t_n >>= 1;
702
+
// Remove the first value of the `proof` array and set `fr` and `sr` to the removed value.
703
+
f_r = proof[0];
704
+
s_r = proof[0];
705
+
proof_offset = 1;
706
+
}
707
+
// For each value `c` in the `proof` array:
708
+
for&c in proof.iter().skip(proof_offset){
709
+
// If `tn` is `0`, then stop the iteration and fail the proof verification.
710
+
if t_n == 0{
711
+
returnErr(TlogError::InvalidProof);
715
712
}
716
-
// 5. Set both fr and sr to the first value in the proof array.
717
-
letmut f_r = proof[0];
718
-
letmut s_r = proof[0];
719
-
// 6. For each subsequent value c in the proof array:
720
-
for c in proof.into_iter().skip(1){
721
-
// 1. If tn is 0, then stop the iteration and fail the proof verification.
722
-
if t_n == 0{
723
-
returnErr(TlogError::InvalidProof);
713
+
// If `LSB(sn)` is set, or if `sn` is equal to `tn`, then:
714
+
iflsb_set(s_n) || s_n == t_n {
715
+
// If `fn < sn`, set `fr` to `HASH(0x01 || c || fr)`.
716
+
if f_n < s_n {
717
+
f_r = node_hash(c, f_r);
724
718
}
725
-
// 2. If LSB(sn) is set, or if sn is equal to tn, then:
726
-
iflsb_set(s_n) || s_n == t_n {
727
-
// 1. If fn < sn, set fr to HASH(0x01 || c || fr).
728
-
if f_n < s_n {
729
-
f_r = node_hash(c, f_r);
730
-
}
731
-
// 2. Set sr to HASH(0x01 || c || sr).
732
-
s_r = node_hash(c, s_r);
733
-
// 3. Until LSB(sn) is set, right-shift fn, sn, and tn equally.
734
-
while !lsb_set(s_n){
735
-
f_n >>= 1;
736
-
s_n >>= 1;
737
-
t_n >>= 1;
738
-
}
739
-
}
740
-
// 3. Otherwise:
741
-
else{
742
-
// 1. Set sr to HASH(0x01 || sr || c).
743
-
s_r = node_hash(s_r, c);
719
+
// Set `sr` to `HASH(0x01 || c || sr)`.
720
+
s_r = node_hash(c, s_r);
721
+
// Until `LSB(sn)` is set, right-shift `fn`, `sn`, and `tn` equally.
722
+
while !lsb_set(s_n){
723
+
f_n >>= 1;
724
+
s_n >>= 1;
725
+
t_n >>= 1;
744
726
}
745
-
// 4. Right-shift fn, sn, and tn once more.
746
-
f_n >>= 1;
747
-
s_n >>= 1;
748
-
t_n >>= 1;
749
727
}
750
-
// 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.
// 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.
0 commit comments