Last active
May 29, 2025 01:03
-
-
Save critesjosh/ff8649f918c7d2904ceaa19b5f858720 to your computer and use it in GitHub Desktop.
helper file for generating toml output for use in a Prover.toml and ecdsa_secp256r1::verify_signature in Noir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Ecdsa } from '@aztec/foundation/crypto'; | |
import { randomBytes, createHash } from 'crypto'; | |
import { writeFileSync } from 'fs'; | |
async function main(){ | |
const ecdsa = new Ecdsa("secp256r1"); | |
// Generate random 32 byte Buffer for private key | |
const privateKey = randomBytes(32); | |
// Generate random 32 byte message | |
const message = "hello world"; | |
const publicKey = await ecdsa.computePublicKey(privateKey) | |
// Extract x and y coordinates from public key | |
const publicKeyX = publicKey.slice(0, 32); | |
const publicKeyY = publicKey.slice(32, 64); | |
// Hash the message using SHA-256 | |
const messageHash = new Uint8Array(createHash('sha256').update(message).digest().buffer); | |
const signature = await ecdsa.constructSignature(messageHash, privateKey); | |
// Verify the signature | |
const isValid = await ecdsa.verifySignature(messageHash, publicKey, signature); | |
if (!isValid) { | |
throw new Error('Signature verification failed!'); | |
} | |
console.log('Signature verification successful!'); | |
// Convert Buffers to arrays of numbers | |
const pubKeyXArray = Array.from(publicKeyX); | |
const pubKeyYArray = Array.from(publicKeyY); | |
// Take only the first 64 bytes of the signature (excluding recovery ID) | |
const signatureArray = Array.from(signature.toBuffer().slice(0, 64)); | |
const messageHashArray = Array.from(messageHash); | |
// Create TOML content | |
const tomlContent = `# Test data for ECDSA Secp256r1 verification | |
pub_key_x = ${JSON.stringify(pubKeyXArray)} | |
pub_key_y = ${JSON.stringify(pubKeyYArray)} | |
signature = ${JSON.stringify(signatureArray)} | |
hashed_message = ${JSON.stringify(messageHashArray)} | |
`; | |
// Write to file | |
writeFileSync('ecdsa_test_data.toml', tomlContent); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment