Skip to content

Commit 9b6510f

Browse files
committed
refactor: validity check as a static method
1 parent edb8db4 commit 9b6510f

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export { state, State, declareState } from './lib/mina/v1/state.js';
6262

6363
export type { JsonProof } from './lib/proof-system/zkprogram.js';
6464
export { SelfProof, verify, Empty, Undefined, Void } from './lib/proof-system/zkprogram.js';
65-
export { VerificationKey, checkVkValidity } from './lib/proof-system/verification-key.js';
65+
export { VerificationKey } from './lib/proof-system/verification-key.js';
6666
export { type ProofBase, Proof, DynamicProof } from './lib/proof-system/proof.js';
6767
export { FeatureFlags } from './lib/proof-system/feature-flags.js';
6868
export { Cache, CacheHeader } from './lib/proof-system/cache.js';

src/lib/mina/v1/transaction-validation.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Types, TypesBigint } from '../../../bindings/mina-transaction/v1/types.
2121
import type { NetworkId } from '../../../mina-signer/src/types.js';
2222
import type { Account } from './account.js';
2323
import type { NetworkValue } from './precondition.js';
24-
import { checkVkValidity } from '../../proof-system/verification-key.js';
24+
import { VerificationKey } from '../../proof-system/verification-key.js';
2525

2626
export {
2727
reportGetAccountError,
@@ -247,7 +247,7 @@ async function verifyAccountUpdate(
247247
assert(verificationKeyRaw !== undefined, 'Account does not have a verification key');
248248
let verificationKey = verificationKeyRaw.data;
249249

250-
const isVkValid = await checkVkValidity(verificationKeyRaw);
250+
const isVkValid = await VerificationKey.checkValidity(verificationKeyRaw);
251251
if (!isVkValid)
252252
throw Error(`The verification key hash is not consistent with the provided data`);
253253

@@ -312,7 +312,9 @@ async function verifyAccountUpdate(
312312
});
313313

314314
if (accountUpdate.update.verificationKey.isSome.toBoolean()) {
315-
const isVkValid = await checkVkValidity(accountUpdate.update.verificationKey.value);
315+
const isVkValid = await VerificationKey.checkValidity(
316+
accountUpdate.update.verificationKey.value
317+
);
316318
if (!isVkValid)
317319
throw Error(`The verification key hash is not consistent with the provided data`);
318320
}

src/lib/proof-system/verification-key.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Struct } from '../provable/types/struct.js';
55
import { Field } from '../provable/wrapped.js';
66
import { inCircuitVkHash } from './zkprogram.js';
77

8-
export { VerificationKey, checkVkValidity };
8+
export { VerificationKey };
99

1010
class VerificationKey extends Struct({
1111
...provable({ data: String, hash: Field }),
@@ -28,35 +28,25 @@ class VerificationKey extends Struct({
2828
hash: Field(RAW_VERIFICATION_KEY.hash),
2929
});
3030
}
31+
32+
static async checkValidity(x: VerificationKey): Promise<boolean> {
33+
try {
34+
let { runAndCheckSync } = await synchronousRunners();
35+
36+
runAndCheckSync(() => {
37+
let vk = Pickles.sideLoaded.vkToCircuit(() => x.data);
38+
let inCircuitHash = inCircuitVkHash(vk);
39+
inCircuitHash.assertEquals(x.hash);
40+
});
41+
return true;
42+
} catch {
43+
return false;
44+
}
45+
}
3146
}
3247

3348
const RAW_VERIFICATION_KEY = {
3449
hash: '3392518251768960475377392625298437850623664973002200885669375116181514017494',
3550
// oxlint-disable-line
3651
data: 'AgIBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALsq7cojes8ZcUc9M9RbZY9U7nhj8KnfU3yTEgqjtXQbAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC7Ku3KI3rPGXFHPTPUW2WPVO54Y/Cp31N8kxIKo7V0GwEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAuyrtyiN6zxlxRz0z1Ftlj1TueGPwqd9TfJMSCqO1dBsBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALsq7cojes8ZcUc9M9RbZY9U7nhj8KnfU3yTEgqjtXQbAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC7Ku3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-
*
42-
* Warning: This function is not meant to be used in provable code.
43-
*
44-
* @param vk - The verification key to validate
45-
* @returns Promise<boolean> - true if the verification key is valid, false otherwise
46-
* @async
47-
*/
48-
const checkVkValidity = async (vk: VerificationKey): Promise<boolean> => {
49-
try {
50-
let { runAndCheckSync } = await synchronousRunners();
51-
const verificationKey = VerificationKey.fromValue(vk);
52-
53-
runAndCheckSync(() => {
54-
let vk = Pickles.sideLoaded.vkToCircuit(() => verificationKey.data);
55-
let inCircuitHash = inCircuitVkHash(vk);
56-
inCircuitHash.assertEquals(verificationKey.hash);
57-
});
58-
return true;
59-
} catch {
60-
return false;
61-
}
62-
};

0 commit comments

Comments
 (0)