Skip to content

Commit

Permalink
curves/Secp256k1: Don't introcude private key bias via bounding
Browse files Browse the repository at this point in the history
  • Loading branch information
pmerkleplant committed Oct 28, 2023
1 parent ee9b411 commit f320f9f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/curves/Secp256k1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import {Random} from "../Random.sol";
* @dev Note that a private key MUST be a field element,
* ie private key ∊ [1, Q).
*
* @dev Note that a private key MUST be created cryptographically sound!
* Generally, this means via secure randomness.
* @dev Note that a private key MUST be created cryptographically secure!
* Generally, this means via randomness sourced from an CSPRNG.
*
* @custom:example Generating a secure private key.
* @custom:example Generating a secure private key:
*
* ```solidity
* import {Secp256k1, PrivateKey} from "crysol/curves/Secp256k1.sol";
Expand All @@ -43,11 +43,11 @@ import {Random} from "../Random.sol";
type PrivateKey is uint;

/**
* @notice PublicKey is a PrivateKey's public identifier
* @notice PublicKey is a private key's public identifier
*
* @dev A public key is derived from a private key via [privKey]G.
* @dev A public key is a point on the secp256k1 curve computed via [privKey]G.
*
* @custom:example Deriving a private key's public key.
* @custom:example Deriving a public key from a private key:
*
* ```solidity
* import {Secp256k1, PrivateKey, PublicKey} from "crysol/curves/Secp256k1.sol";
Expand Down Expand Up @@ -119,9 +119,12 @@ library Secp256k1 {
///
/// @custom:vm Random::readUint()(uint)
function newPrivateKey() internal vmed returns (PrivateKey) {
// Let scalar ∊ [1, Q) sourced cryptographically secure.
uint scalar = (Random.readUint() % (Secp256k1Arithmetic.Q - 1)) + 1;
return PrivateKey.wrap(scalar);
uint scalar = Random.readUint();
while (scalar == 0 || scalar >= Secp256k1.Q) {
// Note to not introduce potential bias via bounding operation.
scalar = Random.readUint();
}
return privateKeyFromUint(scalar);
}

/// @dev Returns whether private key `self` is valid.
Expand Down

0 comments on commit f320f9f

Please sign in to comment.