3
3
4
4
import static java .util .Objects .requireNonNull ;
5
5
6
+ import com .google .common .base .Supplier ;
7
+ import com .google .common .base .Suppliers ;
6
8
import com .hedera .cryptography .rpm .HistoryLibraryBridge ;
7
9
import com .hedera .cryptography .rpm .ProvingAndVerifyingSnarkKeys ;
8
10
import com .hedera .cryptography .rpm .SigningAndVerifyingSchnorrKeys ;
@@ -23,11 +25,18 @@ public class HistoryLibraryImpl implements HistoryLibrary {
23
25
24
26
private static final SplittableRandom RANDOM = new SplittableRandom ();
25
27
private static final HistoryLibraryBridge BRIDGE = HistoryLibraryBridge .getInstance ();
26
- private static ProvingAndVerifyingSnarkKeys SNARK_KEYS ;
28
+ private static final Supplier <ProvingAndVerifyingSnarkKeys > SNARK_KEYS = Suppliers .memoize (() -> {
29
+ try {
30
+ var elf = HistoryLibraryBridge .loadAddressBookRotationProgram ();
31
+ return BRIDGE .snarkVerificationKey (elf );
32
+ } catch (IOException e ) {
33
+ throw new IllegalStateException ("Could not load HistoryLibrary ELF" , e );
34
+ }
35
+ });
27
36
28
37
@ Override
29
38
public Bytes snarkVerificationKey () {
30
- return Bytes .wrap (snarkKeys ().verifyingKey ());
39
+ return Bytes .wrap (SNARK_KEYS . get ().verifyingKey ());
31
40
}
32
41
33
42
@ Override
@@ -94,8 +103,8 @@ public Bytes proveChainOfTrust(
94
103
throw new IllegalArgumentException ("The number of weights and verifying keys must be the same" );
95
104
}
96
105
final var proof = BRIDGE .proveChainOfTrust (
97
- snarkKeys ().provingKey (),
98
- snarkKeys ().verifyingKey (),
106
+ SNARK_KEYS . get ().provingKey (),
107
+ SNARK_KEYS . get ().verifyingKey (),
99
108
genesisAddressBookHash .toByteArray (),
100
109
currentAddressBookVerifyingKeys ,
101
110
currentAddressBookWeights ,
@@ -111,18 +120,6 @@ public Bytes proveChainOfTrust(
111
120
@ Override
112
121
public boolean verifyChainOfTrust (@ NonNull final Bytes proof ) {
113
122
requireNonNull (proof );
114
- return BRIDGE .verifyChainOfTrust (snarkKeys ().verifyingKey (), proof .toByteArray ());
115
- }
116
-
117
- private ProvingAndVerifyingSnarkKeys snarkKeys () {
118
- try {
119
- if (SNARK_KEYS == null ) {
120
- final var elf = HistoryLibraryBridge .loadAddressBookRotationProgram ();
121
- SNARK_KEYS = BRIDGE .snarkVerificationKey (elf );
122
- }
123
- return SNARK_KEYS ;
124
- } catch (IOException e ) {
125
- throw new IllegalStateException ("Could not load HistoryLibrary ELF" , e );
126
- }
123
+ return BRIDGE .verifyChainOfTrust (SNARK_KEYS .get ().verifyingKey (), proof .toByteArray ());
127
124
}
128
125
}
0 commit comments