|
| 1 | +import { Buffer32 } from '@aztec/foundation/buffer'; |
| 2 | + |
| 3 | +import { generatePrivateKey } from 'viem/accounts'; |
| 4 | + |
| 5 | +import type { EthAddress } from '../../eth-address/index.js'; |
| 6 | +import { Signature } from '../../eth-signature/eth_signature.js'; |
| 7 | +import { Secp256k1Signer } from './secp256k1_signer.js'; |
| 8 | +import { |
| 9 | + Secp256k1Error, |
| 10 | + flipSignature, |
| 11 | + makeEthSignDigest, |
| 12 | + normalizeSignature, |
| 13 | + recoverAddress, |
| 14 | + tryRecoverAddress, |
| 15 | +} from './utils.js'; |
| 16 | + |
| 17 | +describe('ecdsa malleability', () => { |
| 18 | + let privateKey: `0x${string}`; |
| 19 | + let signer: Secp256k1Signer; |
| 20 | + let expectedAddress: EthAddress; |
| 21 | + let message: Buffer32; |
| 22 | + let digest: Buffer32; |
| 23 | + let originalSignature: Signature; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + // Generate a random private key and signer |
| 27 | + privateKey = generatePrivateKey(); |
| 28 | + signer = new Secp256k1Signer(Buffer32.fromBuffer(Buffer.from(privateKey.slice(2), 'hex'))); |
| 29 | + expectedAddress = signer.address; |
| 30 | + |
| 31 | + // Create a random message and sign it |
| 32 | + message = Buffer32.random(); |
| 33 | + digest = makeEthSignDigest(message); |
| 34 | + originalSignature = signer.sign(digest); |
| 35 | + }); |
| 36 | + |
| 37 | + it('recovers the same address from both original and flipped signatures', () => { |
| 38 | + // Recover address from original signature |
| 39 | + const recoveredFromOriginal = recoverAddress(digest, originalSignature); |
| 40 | + expect(recoveredFromOriginal.toString()).toEqual(expectedAddress.toString()); |
| 41 | + |
| 42 | + // Flip the signature |
| 43 | + const flippedSignature = flipSignature(originalSignature); |
| 44 | + |
| 45 | + // Ensure the flipped signature is different |
| 46 | + expect(flippedSignature.equals(originalSignature)).toBe(false); |
| 47 | + expect(flippedSignature.r.equals(originalSignature.r)).toBe(true); // r should be the same |
| 48 | + expect(flippedSignature.s.equals(originalSignature.s)).toBe(false); // s should be different |
| 49 | + expect(flippedSignature.v).not.toEqual(originalSignature.v); // v should be different |
| 50 | + |
| 51 | + // Recover address from flipped signature (must use allowMalleable: true) |
| 52 | + const recoveredFromFlipped = recoverAddress(digest, flippedSignature, { allowMalleable: true }); |
| 53 | + expect(recoveredFromFlipped.toString()).toEqual(expectedAddress.toString()); |
| 54 | + expect(() => recoverAddress(digest, flippedSignature)).toThrow(Secp256k1Error); |
| 55 | + |
| 56 | + // Both recovered addresses should match |
| 57 | + expect(recoveredFromOriginal.equals(recoveredFromFlipped)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('flips signature back and forth correctly', () => { |
| 61 | + // Flip once |
| 62 | + const flipped = flipSignature(originalSignature); |
| 63 | + |
| 64 | + // Flip back |
| 65 | + const flippedBack = flipSignature(flipped); |
| 66 | + |
| 67 | + // Should match the original |
| 68 | + expect(flippedBack.equals(originalSignature)).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('rejects malleable signatures by default in recoverAddress', () => { |
| 72 | + // Original signature should work |
| 73 | + expect(() => recoverAddress(digest, originalSignature)).not.toThrow(); |
| 74 | + |
| 75 | + // Flip the signature to make it malleable |
| 76 | + const flippedSignature = flipSignature(originalSignature); |
| 77 | + |
| 78 | + // Flipped signature should be rejected by default |
| 79 | + expect(() => recoverAddress(digest, flippedSignature)).toThrow(Secp256k1Error); |
| 80 | + }); |
| 81 | + |
| 82 | + it('accepts malleable signatures when allowMalleable is true', () => { |
| 83 | + // Flip the signature to make it malleable |
| 84 | + const flippedSignature = flipSignature(originalSignature); |
| 85 | + |
| 86 | + // Should work with allowMalleable: true |
| 87 | + const recoveredAddress = recoverAddress(digest, flippedSignature, { allowMalleable: true }); |
| 88 | + expect(recoveredAddress.toString()).toEqual(expectedAddress.toString()); |
| 89 | + }); |
| 90 | + |
| 91 | + it('rejects malleable signatures by default in tryRecoverAddress', () => { |
| 92 | + // Original signature should work |
| 93 | + expect(tryRecoverAddress(digest, originalSignature)).toBeDefined(); |
| 94 | + |
| 95 | + // Flip the signature to make it malleable |
| 96 | + const flippedSignature = flipSignature(originalSignature); |
| 97 | + |
| 98 | + // Flipped signature should return undefined by default |
| 99 | + expect(tryRecoverAddress(digest, flippedSignature)).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('accepts malleable signatures in tryRecoverAddress when allowMalleable is true', () => { |
| 103 | + // Flip the signature to make it malleable |
| 104 | + const flippedSignature = flipSignature(originalSignature); |
| 105 | + |
| 106 | + // Should work with allowMalleable: true |
| 107 | + const recoveredAddress = tryRecoverAddress(digest, flippedSignature, { allowMalleable: true }); |
| 108 | + expect(recoveredAddress).toBeDefined(); |
| 109 | + expect(recoveredAddress!.toString()).toEqual(expectedAddress.toString()); |
| 110 | + }); |
| 111 | + |
| 112 | + it('normalizes signature with high s-value correctly', () => { |
| 113 | + // Flip the signature to create a high s-value signature |
| 114 | + const highSSignature = flipSignature(originalSignature); |
| 115 | + |
| 116 | + // Recover address using the high s-value signature with allowMalleable: true |
| 117 | + const recoveredAddress = recoverAddress(digest, highSSignature, { allowMalleable: true }); |
| 118 | + expect(recoveredAddress.toString()).toEqual(expectedAddress.toString()); |
| 119 | + |
| 120 | + // Check that the signature is flipped back to low s-value when normalized |
| 121 | + const normalizedSignature = flipSignature(highSSignature); |
| 122 | + expect(normalizedSignature.equals(originalSignature)).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('does not alter low s-value signatures when normalizing', () => { |
| 126 | + // Recover address using the original low s-value signature |
| 127 | + const recoveredAddress = recoverAddress(digest, originalSignature); |
| 128 | + expect(recoveredAddress.toString()).toEqual(expectedAddress.toString()); |
| 129 | + |
| 130 | + // Normalize the signature (should remain unchanged) |
| 131 | + const normalizedSignature = normalizeSignature(originalSignature); |
| 132 | + expect(normalizedSignature.equals(originalSignature)).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('rejects signatures with invalid v-value', () => { |
| 136 | + const signature = new Signature(originalSignature.r, originalSignature.s, originalSignature.v - 27); |
| 137 | + expect(() => recoverAddress(digest, signature)).toThrow(Secp256k1Error); |
| 138 | + const recoveredAddress = recoverAddress(digest, signature, { allowYParityAsV: true }); |
| 139 | + expect(recoveredAddress.toString()).toEqual(expectedAddress.toString()); |
| 140 | + }); |
| 141 | +}); |
0 commit comments