Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea3fccc

Browse files
committedJul 19, 2024
Add pkcs8 encoding function to secp256r1
1 parent 8d337dc commit ea3fccc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 

‎src/crypto/secp256r1.sol

+23
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,27 @@ library Secp256r1 {
66
function isScalar(uint256 scalar) internal pure returns (bool) {
77
return scalar > 0 && scalar < ORDER;
88
}
9+
10+
function encodeScalarPkcs8Der(uint256 scalar) internal pure returns (bytes memory) {
11+
// PrivateKeyInfo ::= SEQUENCE {
12+
// version Version,
13+
// privateKeyAlgorithm AlgorithmIdentifier,
14+
// privateKey OCTET STRING,
15+
// attributes [0] IMPLICIT Attributes OPTIONAL }
16+
//
17+
// 30 81 41 ; SEQUENCE (0x41 bytes)
18+
// 02 01 00 ; INTEGER (0)
19+
// 30 13 ; SEQUENCE (0x13 bytes)
20+
// 06 07 ; OBJECT IDENTIFIER (ecdsaWithSHA1)
21+
// 2A 86 48 CE 3D 02 01
22+
// 06 08 ; OBJECT IDENTIFIER (prime256v1)
23+
// 2A 86 48 CE 3D 03 01 07
24+
// 04 27 ; OCTET STRING (0x27 bytes)
25+
// 30 25 ; SEQUENCE (0x25 bytes)
26+
// 02 01 01 ; INTEGER (1)
27+
// 04 20 ; OCTET STRING (32 bytes)
28+
// <32 byte private key>
29+
return
30+
bytes.concat(hex"308141020100301306072a8648ce3d020106082a8648ce3d030107042730250201010420", bytes32(scalar));
31+
}
932
}

0 commit comments

Comments
 (0)
Please sign in to comment.