Skip to content

Commit 60f7077

Browse files
authored
Update Fts.java
1 parent 27e69af commit 60f7077

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

code/Fts.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void main(String[] args) {
1717
* Computes the message digest SHA2(b1 | b2 | b3 | b4) where
1818
* | denotes concatenation.
1919
*/
20-
public byte[] SHA2(byte[] b1, byte[] b2, byte[] b3, byte[] b4) {
20+
public static byte[] SHA2(byte[] b1, byte[] b2, byte[] b3, byte[] b4) {
2121
try {
2222
MessageDigest sha2 = MessageDigest.getInstance("SHA-256");
2323
byte[] b = new byte[b1.length + b2.length + b3.length + b4.length];
@@ -35,7 +35,7 @@ public byte[] SHA2(byte[] b1, byte[] b2, byte[] b3, byte[] b4) {
3535
/**
3636
* Computes the message digest SHA2(b).
3737
*/
38-
public byte[] SHA2(byte[] b) {
38+
public static byte[] SHA2(byte[] b) {
3939
try {
4040
MessageDigest sha2 = MessageDigest.getInstance("SHA-256");
4141
return sha2.digest(b);
@@ -50,9 +50,8 @@ public Fts() {
5050
List<Stakeholder> stakeholders = new ArrayList<>();
5151
for (int i = 0, c = 20; i < 8; i++, c = c % 2 == 0 ? c / 2 : c*3 + 1) {
5252
final String name = String.format("Stakeholder %d", i);
53-
final String auditPath = Integer.toBinaryString(i);
5453
final int coins = c;
55-
stakeholders.add(new Stakeholder(name, coins, auditPath));
54+
stakeholders.add(new Stakeholder(name, coins));
5655
}
5756
// Create the Merkle tree
5857
final Node[] tree = createMerkleTree(stakeholders);
@@ -110,7 +109,7 @@ public FtsResult ftsTree(Node[] tree, Random rng) {
110109

111110
public boolean ftsVerify(Random rng, byte[] merkleRootHash, FtsResult ftsResult) {
112111
StringBuilder auditPath = new StringBuilder();
113-
System.out.print("Checking audit path... ");
112+
System.out.print("Building audit path... ");
114113
for (ProofEntry proofEntry : ftsResult.getMerkleProof()) {
115114
final int x1 = proofEntry.getLeftBound();
116115
final int x2 = proofEntry.getRightBound();
@@ -123,18 +122,14 @@ public boolean ftsVerify(Random rng, byte[] merkleRootHash, FtsResult ftsResult)
123122
auditPath.append('1');
124123
}
125124
}
126-
if (!auditPath.toString().equals(ftsResult.getStakeholder().getAuditPath())) {
127-
System.out.println(String.format("Not OK! (expected %s)", ftsResult.getStakeholder().getAuditPath()));
128-
return false;
129-
}
130-
System.out.println("OK!");
125+
System.out.println("OK");
131126
byte[] hx = SHA2(ftsResult.getStakeholder().toBytes());
132127
for (int i = ftsResult.getMerkleProof().size() - 1; i >= 0; i--) {
133128
final ProofEntry proofEntry = ftsResult.getMerkleProof().get(i);
134129
final byte[] x1 = String.valueOf(proofEntry.getLeftBound()).getBytes(StandardCharsets.US_ASCII);
135130
final byte[] x2 = String.valueOf(proofEntry.getRightBound()).getBytes(StandardCharsets.US_ASCII);
136131
final byte[] hy = proofEntry.getMerkleHash();
137-
if (ftsResult.getStakeholder().getAuditPath().charAt(i) == '0') {
132+
if (auditPath.charAt(i) == '0') {
138133
hx = SHA2(hx, hy, x1, x2);
139134
} else {
140135
hx = SHA2(hy, hx, x1, x2);

0 commit comments

Comments
 (0)