secp256k1公钥计算示例

secp256k1公钥计算示例

参照:

https://en.bitcoin.it/wiki/Secp256k1

secp256k1源码下载:

https://github.com/bitcoin-core/secp256k1

secp256k1用于计算私钥对应的公钥,用到函数的如下:

secp256k1上下文对象结构体生成函数:

/** Create a secp256k1 context object.
 *
 *  Returns: a newly created context object.
 *  In:      flags: which parts of the context to initialize.
 *
 *  See also secp256k1_context_randomize.
 */
SECP256K1_API secp256k1_context* secp256k1_context_create(
    unsigned int flags
) SECP256K1_WARN_UNUSED_RESULT;

secp256k1上下文对象销毁函数:

/** Destroy a secp256k1 context object.
 *
 *  The context pointer may not be used afterwards.
 *  Args:   ctx: an existing context to destroy (cannot be NULL)
 */
SECP256K1_API void secp256k1_context_destroy(
    secp256k1_context* ctx
);

secp256k1上下文对象结构体的说明如下:

/** Opaque data structure that holds context information (precomputed tables etc.).
 *
 *  The purpose of context structures is to cache large precomputed data tables
 *  that are expensive to construct, and also to maintain the randomization data
 *  for blinding.
 *
 *  Do not create a new context object for each operation, as construction is
 *  far slower than all other API calls (~100 times slower than an ECDSA
 *  verification).
 *
 *  A constructed context can safely be used from multiple threads
 *  simultaneously, but API call that take a non-const pointer to a context
 *  need exclusive access to it. In particular this is the case for
 *  secp256k1_context_destroy and secp256k1_context_randomize.
 *
 *  Regarding randomization, either do it once at creation time (in which case
 *  you do not need any locking for the other calls), or use a read-write lock.
 */
typedef struct secp256k1_context_struct secp256k1_context;

计算公钥函数:

/** Compute the public key for a secret key.
 
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Java中使用secp256k1签名并恢复公钥,你可以使用Bouncy Castle库。以下是一个示例代码: ```java import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.signers.ECDSASigner; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.util.encoders.Hex; import java.math.BigInteger; import java.security.SecureRandom; public class ECDSAExample { public static void main(String[] args) { // Generate a random private key SecureRandom random = new SecureRandom(); BigInteger privateKey = new BigInteger(256, random); // Define the secp256k1 curve parameters ECDomainParameters curve = ECNamedCurveTable.getByName("secp256k1"); // Create an EC private key object ECPrivateKeyParameters privateKeyParams = new ECPrivateKeyParameters(privateKey, curve); // Create an EC signer object ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); // Initialize the signer with the private key signer.init(true, privateKeyParams); // Generate a random message to sign byte[] message = "Hello, world!".getBytes(); // Calculate the signature BigInteger[] signature = signer.generateSignature(message); // Print the signature System.out.println("Signature: " + Hex.toHexString(signature[0].toByteArray()) + Hex.toHexString(signature[1].toByteArray())); // Recover the public key from the signature ECPoint publicKey = signer.getPublicKey(); // Create an EC public key object ECPublicKeyParameters publicKeyParams = new ECPublicKeyParameters(publicKey, curve); // Verify the signature using the recovered public key signer.init(false, publicKeyParams); boolean valid = signer.verifySignature(message, signature[0], signature[1]); System.out.println("Valid signature? " + valid); } } ``` 这个示例代码使用Bouncy Castle库生成一个随机的私钥,使用secp256k1曲线参数创建一个EC私钥对象,并使用ECDSASigner进行签名和验证。签名后,公钥从签名中恢复,并用于验证签名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值