diff --git a/Sources/BeaconChain/BeaconChain.swift b/Sources/BeaconChain/BeaconChain.swift index 305452a..07a4230 100644 --- a/Sources/BeaconChain/BeaconChain.swift +++ b/Sources/BeaconChain/BeaconChain.swift @@ -200,17 +200,6 @@ extension BeaconChain { return firstCommittee[Int(slot) % firstCommittee.count] } - static func merkleRoot(values: [Bytes32]) -> Bytes32 { - var o = [Data](repeating: Data(repeating: 0, count: 1), count: values.count - 1) - o.append(contentsOf: values) - - for i in stride(from: values.count - 1, through: 0, by: -1) { - o[i] = hash(o[i * 2] + o[i * 2 + 1]) - } - - return o[1] - } - static func getAttestationParticipants( state: BeaconState, attestationData: AttestationData, diff --git a/Sources/BeaconChain/Merkle.swift b/Sources/BeaconChain/Merkle.swift new file mode 100644 index 0000000..99e0784 --- /dev/null +++ b/Sources/BeaconChain/Merkle.swift @@ -0,0 +1,28 @@ +import Foundation + +class Merkle { + + static func root(_ values: [Bytes32]) -> Bytes32 { + var o = [Data](repeating: Data(repeating: 0, count: 1), count: values.count - 1) + o.append(contentsOf: values) + + for i in stride(from: values.count - 1, through: 0, by: -1) { + o[i] = BeaconChain.hash(o[i * 2] + o[i * 2 + 1]) + } + + return o[1] + } + + static func verifyBranch(leaf: Bytes32, branch: [Bytes32], depth: Int, index: Int, root: Bytes32) -> Bool { + var value = leaf + for i in 0.. Bool { - var value = leaf - for i in 0..