-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify_o1.js
61 lines (54 loc) · 1.85 KB
/
verify_o1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { parsePublicKeyHex, parsePayloadHex, parseSignatureHex} from './utils.js'
import { credential, assertion } from './webauthn.js'
import {
Struct,
ZkProgram,
Crypto,
createForeignCurve,
createEcdsa,
Bool,
} from 'o1js'
// parse webauthn data
const publicKeyHex = parsePublicKeyHex(credential.response.attestationObject)
const payloadHex = parsePayloadHex(assertion.response.clientDataJSON, assertion.response.authenticatorData)
const signatureHex = parseSignatureHex(assertion.response.signature)
// init
class Secp256r1 extends createForeignCurve(Crypto.CurveParams.Secp256r1) {}
class EcdsaP256 extends createEcdsa(Secp256r1) {}
class Params extends Struct({
publicKey: Secp256r1,
payload: Secp256r1.Scalar,
signature: EcdsaP256,
}) {}
export const WebAuthnP256 = ZkProgram({
name: 'webauthn-p256',
publicInput: Params,
publicOutput: Bool,
methods: {
verifySignature: {
privateInputs: [],
async method(params) {
const { publicKey, payload, signature } = params
/*
Use verify for a byte array of the unhashed payload.
Use verifySignedHash for a hashed payload (parsed and supplied as scalar).
https://github.com/o1-labs/o1js/blob/6ebbc23710f6de023fea6d83dc93c5a914c571f2/src/lib/provable/crypto/foreign-ecdsa.ts#L81-L102
*/
const isValid = signature.verifySignedHash(payload, publicKey)
return { publicOutput: isValid }
},
},
},
})
// parse hex values
const publicKey_ = Secp256r1.fromHex(publicKeyHex)
const payload_ = Secp256r1.Scalar.from(payloadHex)
const signature_ = EcdsaP256.fromHex(signatureHex)
// run zk program
await WebAuthnP256.compile()
const isvalid = await WebAuthnP256.verifySignature({
publicKey: publicKey_,
payload: payload_,
signature: signature_,
})
console.log('signature is valid: ', isvalid.proof.publicOutput.toBoolean())