Skip to content

Commit

Permalink
feat: add entropy to sign
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 20, 2024
1 parent fc50e82 commit 9ad0d2c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-doors-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Added non-deterministic signature generation.
8 changes: 1 addition & 7 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"files": {
"ignore": [
"contracts/generated.ts",
"contracts/lib",
"test/kzg",
"tsconfig.json",
"tsconfig.*.json"
]
"ignore": ["contracts/", "test/kzg", "tsconfig.json", "tsconfig.*.json"]
},
"organizeImports": {
"enabled": true
Expand Down
1 change: 1 addition & 0 deletions site/pages/guides/eip-1193.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Event emitters for EIP-1193 Providers can be created using [`Provider.createEmit
Useful for Wallets that distribute an EIP-1193 Provider (e.g. webpage injection via `window.ethereum`).

```ts twoslash
// @noErrors
import { Provider, RpcRequest, RpcResponse } from 'ox'

// 1. Instantiate a Provider Emitter.
Expand Down
7 changes: 6 additions & 1 deletion src/core/P256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type * as Errors from './Errors.js'
import * as Hex from './Hex.js'
import * as PublicKey from './PublicKey.js'
import type * as Signature from './Signature.js'
import * as Entropy from './internal/entropy.js'

/** Re-export of noble/curves P256 utilities. */
export const noble = secp256r1
Expand Down Expand Up @@ -150,7 +151,11 @@ export function sign(options: sign.Options): Signature.Signature {
const { r, s, recovery } = secp256r1.sign(
payload instanceof Uint8Array ? payload : Bytes.fromHex(payload),
privateKey instanceof Uint8Array ? privateKey : Bytes.fromHex(privateKey),
...(hash ? [{ prehash: true, lowS: true }] : []),
{
extraEntropy: Entropy.extraEntropy,
lowS: true,
...(hash ? { prehash: true } : {}),
},
)
return {
r,
Expand Down
7 changes: 6 additions & 1 deletion src/core/Secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type * as Errors from './Errors.js'
import * as Hex from './Hex.js'
import * as PublicKey from './PublicKey.js'
import type * as Signature from './Signature.js'
import * as Entropy from './internal/entropy.js'
import type { OneOf } from './internal/types.js'

/** Re-export of noble/curves secp256k1 utilities. */
Expand Down Expand Up @@ -191,7 +192,11 @@ export function sign(options: sign.Options): Signature.Signature {
const { r, s, recovery } = secp256k1.sign(
Bytes.from(payload),
Bytes.from(privateKey),
...(hash ? [{ prehash: true, lowS: true }] : []),
{
extraEntropy: Entropy.extraEntropy,
lowS: true,
...(hash ? { prehash: true } : {}),
},
)
return {
r,
Expand Down
24 changes: 12 additions & 12 deletions src/core/_test/P256.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ describe('sign', () => {
})
expect(signature).toMatchInlineSnapshot(
`
{
"r": 25696373395957984486324188498890325781005829812399813021478384321951480608605n,
"s": 71471412582193316356881596002626442093973157771381586232812188859587069632849n,
"yParity": 0,
}
`,
{
"r": 25696373395957984486324188498890325781005829812399813021478384321951480608605n,
"s": 44320676628162932405815850946781131436023797452754174109610070201481442411520n,
"yParity": 1,
}
`,
)
expect(
P256.verify({
Expand Down Expand Up @@ -112,12 +112,12 @@ describe('sign', () => {
})
expect(signature).toMatchInlineSnapshot(
`
{
"r": 109093915289726021639001379260733771573757231672849462488223442695417941697300n,
"s": 109093247477094054921870710630193648963091633971756441119531150894951521569389n,
"yParity": 1,
}
`,
{
"r": 109093915289726021639001379260733771573757231672849462488223442695417941697300n,
"s": 6698841733262193840826736319213924566905321252379319222891108166116990474980n,
"yParity": 0,
}
`,
)
expect(
P256.verify({
Expand Down
6 changes: 6 additions & 0 deletions src/core/internal/entropy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export let extraEntropy: boolean

/** @internal */
export function setExtraEntropy(entropy: boolean) {
extraEntropy = entropy
}
3 changes: 3 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Caches } from 'ox'
import { afterAll, beforeAll, beforeEach, vi } from 'vitest'
import * as instances from './anvil.js'
import * as Entropy from '../src/core/internal/entropy.js'

Entropy.setExtraEntropy(false)

beforeAll(() => {
vi.mock('../src/core/internal/errors.ts', async () => ({
Expand Down

0 comments on commit 9ad0d2c

Please sign in to comment.