@@ -70,6 +70,7 @@ public class Secp256k1 {
7070 static private KeyAgreement ecAdd ;
7171 static private Signature sig ;
7272 static private ECPrivateKey tempPrivateKey ;
73+ static private ECPublicKey tempPublicKey ;
7374 // this one is used for point addition - G changes to arbitrary points
7475 static private ECPrivateKey tempPoint ;
7576 static private TransientHeap heap ;
@@ -97,6 +98,8 @@ static public void init(TransientHeap hp)
9798 Secp256k1 .setCommonCurveParameters (tempPrivateKey );
9899 tempPoint = (ECPrivateKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PRIVATE , KeyBuilder .LENGTH_EC_FP_256 , false );
99100 Secp256k1 .setCommonCurveParameters (tempPoint );
101+ tempPublicKey = (ECPublicKey )KeyBuilder .buildKey (KeyBuilder .TYPE_EC_FP_PUBLIC , KeyBuilder .LENGTH_EC_FP_256 , false );
102+ Secp256k1 .setCommonCurveParameters (tempPublicKey );
100103 // set scalar of the tempPoint to 1
101104 short len = LENGTH_PRIVATE_KEY ;
102105 short off = heap .allocate (len );
@@ -551,8 +554,8 @@ static public short sign(
551554 }
552555 /**
553556 * Signs the message with the private key. The message should be already hashed.
554- * @param scalar - buffer with a scalar ( private key )
555- * @param scalarOff - offset in the scalar buffer
557+ * @param scalar - buffer with a scalar ( private key )
558+ * @param scalarOff - offset in the scalar buffer
556559 * @param msg - buffer with a 32-byte hash to sign
557560 * @param msgOffset - offset in the msg buffer
558561 * @param out - output buffer to write the signature to
@@ -567,6 +570,44 @@ static public short sign(
567570 tempPrivateKey .setS (scalar , scalarOff , LENGTH_PRIVATE_KEY );
568571 return sign (tempPrivateKey , msg , msgOffset , out , outOffset );
569572 }
573+ /**
574+ * Verifies a signature against the message and the public key
575+ * @param pubkey - buffer with the pubkey serialized in uncompressed form
576+ * @param pubkeyOff - offset of the pubkey buffer
577+ * @param msgBuf - buffer with the message to verify (will be sha256-ed)
578+ * @param msgOff - offset in the message buffer
579+ * @param sigBuf - buffer with DER-serialized signature
580+ * @param sigOff - offset of the signature buffer
581+ * @param sigLen - length of the signature in the buffer
582+ * @return true if the signature is valid, false otherwise
583+ */
584+ static public boolean verifyPreimage (
585+ byte [] pubkey , short pubkeyOff ,
586+ byte [] msgBuf , short msgOff , short msgLen ,
587+ byte [] sigBuf , short sigOff , short sigLen )
588+ {
589+ tempPublicKey .setW (pubkey , pubkeyOff , LENGTH_PUBLIC_KEY_UNCOMPRESSED );
590+ return verifyPreimage (tempPublicKey , msgBuf , msgOff , msgLen , sigBuf , sigOff , sigLen );
591+ }
592+ /**
593+ * Verifies a signature against the message and the public key
594+ * @param pubkey - Instance of the ECPublicKey
595+ * @param msgBuf - buffer with the message to verify (will be sha256-ed)
596+ * @param msgOff - offset in the message buffer
597+ * @param sigBuf - buffer with DER-serialized signature
598+ * @param sigOff - offset of the signature buffer
599+ * @param sigLen - length of the signature in the buffer
600+ * @return true if the signature is valid, false otherwise
601+ */
602+ static public boolean verifyPreimage (
603+ ECPublicKey publicKey ,
604+ byte [] msgBuf , short msgOff , short msgLen ,
605+ byte [] sigBuf , short sigOff , short sigLen )
606+ {
607+ sig .init (publicKey , Signature .MODE_VERIFY );
608+ return sig .verify (msgBuf , msgOff , msgLen ,
609+ sigBuf , sigOff , sigLen );
610+ }
570611 /**
571612 * Generates a random 32-byte secret up to the group order. It is always a valid private key.
572613 * @param buf - buffer where to put the secret
0 commit comments