CryptoLib4Pascal icon indicating copy to clipboard operation
CryptoLib4Pascal copied to clipboard

How to bitcoin bip 340 Schnorr Signatures for secp256k1?

Open Gzushgshsh opened this issue 4 months ago • 1 comments

https://bips.xyz/340 link to definition of nip (I only read part of it)

I'm a complete beginner at this, and I'm trying to verify a signature, after looking at the examples I wrote this, but it doesn't seem to work

var
  curve: IX9ECParameters;
  domain: TECDomainParameters;
  xcoord: String;
  bytesPK, sigBytes, msgBytes: TCryptoLibByteArray;
  publicKey: TECPublicKeyParameters;
  signer: ISigner;
begin
  curve := TCustomNamedCurves.GetByName('secp256k1');
  domain := TECDomainParameters.Create(curve.Curve, curve.G, curve.N,
    curve.H, curve.GetSeed);

  xcoord:= '6e468422dfb74a5738702a8823b9b28168abab8655faacb6853cd0ee15deee93';
  bytesPK:= TConverters.ConvertHexStringToBytes('02' + xcoord);

  publicKey:= TECPublicKeyParameters.Create('ECSCHNORR',
    curve.Curve.DecodePoint(bytesPK), domain);

  msgBytes:= TConverters.ConvertHexStringToBytes
    ('4376c65d2f232afbe9b882a35baa4f6fe8667c4e684749af565f981833ed6a65');
  sigBytes:= TConverters.ConvertHexStringToBytes
    ('908a15e46fb4d8675bab026fc230a0e3542bfade63da02d542fb78b2a8513fcd0092619a2c8c1221e581946e0191f2af505dfdf8657a414dbca329186f009262');

  signer:= TSignerUtilities.GetSigner('SHA-256withECSCHNORRSIPA');
  signer.Init(False, publicKey);
  signer.BlockUpdate(msgBytes, 0, system.Length(msgBytes));

  WriteLn(signer.VerifySignature(sigBytes));
end;

Signature, message and xcoord of pubkey is from an example from Nostr protocol https://nostr.com/the-protocol/events https://github.com/nostr-protocol/nips/blob/master/01.md

Gzushgshsh avatar Oct 13 '24 01:10 Gzushgshsh