How to work with Schnorr Signatures for secp256k1? lazarus / 21-General ---------------------------------------------------------------------- Gzushgshsh · 2024-10-20 12:18 I try to write a client to Nostr protocol https://nostr.com this uses the same signatures of bitcoin BIP-340 https://bips.xyz/340 I found the https://github.com/Xor-el/CryptoLib4Pascal but I can't use it properly. After read the examples and Tests projects I write the simple procedure: (Expected result is a TRUE in the console. Pub Key, message and signature is from a example in Nostr website https://nostr.com/the- protocol/events) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  curve: IX9ECParameters;  domain: TECDomainParameters;  pubKeyBytes, 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);   pubKeyBytes:= THex.Decode    ('02' + '6e468422dfb74a5738702a8823b9b28168abab8655faacb6853cd0ee15deee93');   msgBytes:= THex.Decode    ('4376c65d2f232afbe9b882a35baa4f6fe8667c4e68 4749af565f981833ed6a65');   sigBytes:= THex.Decode    ('908a15e46fb4d8675bab026fc230a0e3542bfade63da02d542fb78b2a8513fcd' +    '0092619a2c8c1221e581946e0191f2af505dfdf8657a414dbca329186f009262');   publicKey:= TECPublicKeyParameters.Create    ( 'ECSCHNORR', curve.Curve.DecodePoint(pubKeyBytes),  domain);   signer:= TSignerUtilities.GetSigner('SHA-256withECSCHNORRSIPA');  signer.Init(False, publicKey);  signer.BlockUpdate(msgBytes, 0, system.Length(msgBytes));   WriteLn(signer.VerifySignature(sigBytes));end; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses  ClpIX9ECParameters, ClpECDomainParameters, ClpCryptoLibTypes, ClpECPublicKeyParameters, ClpISigner,  ClpEncoders, ClpECPublicKeyParameters, ClpCustomNamedCurves, ClpECDomainParameters; ---------------------------------------------------------------------- delphius · 2024-10-22 20:22 Why not use a library specifically designed for this purpose libsecp256k1? Then you can use its capabilities by analogy with OpenSSL, in the example from which the pascal code was made, you can see many additional things like context randomization, key memory cleanup, side attacks protection etc. Its a high-performance high-assurance C library for digital signatures and other cryptographic primitives on the secp256k1 elliptic curve. I haven't checked for Linux, but I'm sure everything will work out there  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program testsig; // Library https://github.com/bitcoin-core/secp256k1/// Windows shared libs compilation https://ru.stackoverflow.com/questions/1587802/secp256k1-сборка- библиотеки-для-c// Example https://github.com/bitcoin- core/secp256k1/blob/master/examples/schnorr.c {$mode delphi}{$H+} uses  SysUtils, fphashutils; const  SECP256K1_CONTEXT_NONE = 1;// https://raw.githubusercontent.com/bitcoin/bips/master/bip-0340/test- vectors.csv - test vectors//000000000000000000000000000000000000000000 0000000000000000000003 secret key//F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9 public key//0000000000000000000000000000000000000000000000000000000000000000 aux_rand//000000000000000000000000000000000000000000000000000000000000 0000 message//E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE 2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536 C0 signature//verification result TRUE  secret_key_st = '0000000000000000000000000000000000000000000000000000000000000003';  public_key_st = 'F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9';  message_st    = '0000000000000000000000000000000000000000000000000000000000000000';  aux_rand_st   = '0000000000000000000000000000000000000000000000000000000000000000';  signature_st  = 'E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F 1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310 536C0'; type  PKeyPair = ^TKeyPair;  TKeyPair = record  end;   PContext = Pointer;   TContextCreate = function(flags: Integer): PContext; cdecl;  TContextDestroy = procedure(ctx: PContext); cdecl;  TKeyPairCreate = function(ctx: PContext; keypair: PKeyPair; seckey: PByteArray): Integer; cdecl;  TKeyPairXOnlyPub = function(ctx: PContext; pubkey: Pointer; pk_parity: Pointer; keypair: PKeyPair): Integer; cdecl;  TSchnorrSigVerify = function(ctx: PContext; signature: PByte; msg: PByte; msg_len: Integer; pubkey: PByte): Integer; cdecl; var  lib: TLibHandle;  secp256k1_context_create: TContextCreate;  secp256k1_context_destroy: TContextDestroy;  secp256k1_keypair_create: TKeyPairCreate;  secp256k1_keypair_xonly_pub: TKeyPairXOnlyPub;  secp256k1_schnorrsig_verify: TSchnorrSigVerify;  ctx: PContext;  secret_key, public_key, message, aux_rand, signature: TBytes;  keypair: TKeyPair; begin  // Load library   lib := LoadLibrary('libsecp256k1.dll');  if lib = 0 then  begin    WriteLn('Failed to load library');    Exit;  end;   // Get function adresses  @secp256k1_context_create := GetProcAddress(lib, 'secp256k1_context_create');  @secp256k1_context_destroy := GetProcAddress(lib, 'secp256k1_context_destroy');  @secp256k1_keypair_create := GetProcAddress(lib, 'secp256k1_keypair_create');  @secp256k1_keypair_xonly_pub := GetProcAddress(lib, 'secp256k1_keypair_xonly_pub');  @secp256k1_schnorrsig_verify := GetProcAddress(lib, 'secp256k1_schnorrsig_verify');   // Create context  ctx := secp256k1_context_create(SECP256K1_CONTEXT_NONE);  if ctx = nil then  begin    WriteLn('Failed to create context');    Exit;  end;   // Convert text vector to bytes  HexStrToBytes(secret_key_st, secret_key);  HexStrToBytes(public_key_st, public_key);  HexStrToBytes(message_st, message);  HexStrToBytes(aux_rand_st, aux_rand);  HexStrToBytes(signature_st, signature);   if secp256k1_keypair_create(ctx, @keypair, @secret_key[0]) = 0 then  begin    WriteLn('Invalid secret key');    Exit;  end;   // Public key serialization  if secp256k1_keypair_xonly_pub(ctx, @public_key[0], nil, @keypair) = 0 then  begin    WriteLn('Failed to extract public key');    Exit;  end;   // Verify a signature. This will return 1 if it's valid and 0 if it's not  if secp256k1_schnorrsig_verify(ctx, @signature[0], @message[0], 32, @public_key[0]) = 1 then     WriteLn('Signature is valid')  else     WriteLn('Signature is invalid');   // Free resources  secp256k1_context_destroy(ctx);  UnloadLibrary(lib);end.