Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge signWithEntropy to sign #15

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ jobs:
with:
fetch-depth: 1

- name: Add target wasm32-unknown-unknown
run: rustup target add wasm32-unknown-unknown
- name: Install Rust nightly with wasm32-unknown-unknown, clippy and rustfmt
run: |
rustup toolchain install nightly \
--target wasm32-unknown-unknown \
--component clippy \
--component rustfmt

- name: Install Node.js dependencies
run: npm install

- name: Add components clippy and rustfmt
run: rustup component add clippy rustfmt

- name: Run lint
run: |
make lint
Expand Down
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,10 @@ Returns `null` if result is equal to `0`.
- `Expected Private` if `!isPrivate(d)`
- `Expected Tweak` if `tweak` is not in `[0...order - 1]`

### sign (h, d)
### sign (h, d[, e])

```haskell
sign :: Buffer -> Buffer -> Buffer
```

Returns normalized signatures, each of (r, s) values are guaranteed to less than `order / 2`.
Uses RFC6979.

##### Throws:

- `Expected Private` if `!isPrivate(d)`
- `Expected Scalar` if `h` is not 256-bit

### signWithEntropy (h, d, e)

```haskell
sign :: Buffer -> Buffer -> Buffer -> Buffer
sign :: Buffer -> Buffer [-> Buffer] -> Buffer
```

Returns normalized signatures, each of (r, s) values are guaranteed to less than `order / 2`.
Expand Down
1 change: 0 additions & 1 deletion benches/cryptocoinjs_secp256k1.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function createApi(secp256k1) {
secp256k1.privateKeyTweakAdd(new Uint8Array(d), tweak),
// privateSub
sign: (h, d) => secp256k1.ecdsaSign(h, d),
// signWithEntropy
verify: (h, Q, signature) => secp256k1.ecdsaVerify(signature, h, Q),
};
}
1 change: 0 additions & 1 deletion benches/noble_secp256k1.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export default {
// privateAdd
// privateSub
sign: secp256k1.sign,
// signWithEntropy
// verify: (h, Q, signature) => secp256k1.verify(signature, h, Q),
};
3 changes: 1 addition & 2 deletions examples/random-in-node/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ print([
{ name: "pointMultiply", args: [data.pubkey, data.tweak] },
{ name: "privateAdd", args: [data.seckey, data.tweak] },
{ name: "privateSub", args: [data.seckey, data.tweak] },
{ name: "sign", args: [data.hash, data.seckey] },
{ name: "signWithEntropy", args: [data.hash, data.seckey, data.entropy] },
{ name: "sign", args: [data.hash, data.seckey, data.entropy] },
{
name: "verify",
args: [data.hash, data.pubkey, secp256k1.sign(data.hash, data.seckey)],
Expand Down
100 changes: 2 additions & 98 deletions examples/react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,6 @@ const App = withStyles(useStyles)(
classes={this.props.classes}
hash={this.state.data?.hash}
seckey={this.state.data?.seckey}
/>
</Box>
<Box className={this.props.classes.methodBox}>
<ApiSignWithEntropy
classes={this.props.classes}
hash={this.state.data?.hash}
seckey={this.state.data?.seckey}
entropy={this.state.data?.entropy}
/>
</Box>
Expand Down Expand Up @@ -983,94 +976,6 @@ const ApiPrivateSub = withStyles(useStyles)(
);

const ApiSign = withStyles(useStyles)(
class extends Component {
constructor(props) {
super(props);
this.state = {
hash: "",
hash_valid: undefined,
seckey: "",
seckey_valid: undefined,
result: undefined,
};
}

componentDidUpdate(prevProps, prevState) {
if (
prevProps.hash !== this.props.hash ||
prevProps.seckey !== this.props.seckey
) {
this.setState({
hash: this.props.hash,
seckey: this.props.seckey,
});
}

if (
prevState.hash !== this.state.hash ||
prevState.seckey !== this.state.seckey
) {
const { hash, seckey } = this.state;
const hash_valid = hash === "" ? undefined : validate.isHash(hash);
const seckey_valid =
seckey === "" ? undefined : secp256k1.isPrivate(seckey);
const result =
hash === "" && seckey === ""
? undefined
: secp256k1.sign(hash, seckey);
this.setState({ hash_valid, seckey_valid, result });
}
}

render() {
return (
<>
<Typography variant="h6">
sign(h: Uint8Array, d: Uint8Array) =&gt; Uint8Array
</Typography>
<TextField
label="Hash as HEX string"
onChange={createInputChange(this, "hash")}
value={this.state.hash}
fullWidth
margin="normal"
variant="outlined"
InputProps={getInputProps(
this.state.hash_valid,
this.props.classes
)}
/>
<TextField
label="Private Key as HEX string"
onChange={createInputChange(this, "seckey")}
value={this.state.seckey}
fullWidth
margin="normal"
variant="outlined"
InputProps={getInputProps(
this.state.seckey_valid,
this.props.classes
)}
/>
<TextField
label="Output, Signature as HEX string"
value={
this.state.result === undefined
? ""
: this.state.result || "Invalid result"
}
fullWidth
margin="normal"
variant="outlined"
InputProps={getInputProps(this.state.result, this.props.classes)}
/>
</>
);
}
}
);

const ApiSignWithEntropy = withStyles(useStyles)(
class extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -1112,7 +1017,7 @@ const ApiSignWithEntropy = withStyles(useStyles)(
const result =
hash === "" && seckey === "" && entropy === ""
? undefined
: secp256k1.signWithEntropy(hash, seckey, entropy);
: secp256k1.sign(hash, seckey, entropy);
this.setState({ hash_valid, seckey_valid, entropy_valid, result });
}
}
Expand All @@ -1121,8 +1026,7 @@ const ApiSignWithEntropy = withStyles(useStyles)(
return (
<>
<Typography variant="h6">
signWithEntropy(h: Uint8Array, d: Uint8Array, e: Uint8Array) =&gt;
Uint8Array
sign(h: Uint8Array, d: Uint8Array, e: Uint8Array) =&gt; Uint8Array
</Typography>
<TextField
label="Hash as HEX string"
Expand Down
4 changes: 2 additions & 2 deletions secp256k1-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn init(mut exports: JsObject, env: Env) -> NapiResult<()> {
exports.create_named_method("pointMultiply", point_multiply)?;
exports.create_named_method("privateAdd", private_add)?;
exports.create_named_method("privateSub", private_sub)?;
exports.create_named_method("signWithEntropy", sign_with_entropy)?;
exports.create_named_method("sign", sign)?;
exports.create_named_method("verify", verify)?;

Ok(())
Expand Down Expand Up @@ -280,7 +280,7 @@ fn private_sub(ctx: CallContext) -> NapiResult<JsUnknown> {
}

#[js_function(3)]
fn sign_with_entropy(ctx: CallContext) -> NapiResult<JsTypedArray> {
fn sign(ctx: CallContext) -> NapiResult<JsTypedArray> {
let h = get_typed_array(&ctx, 0)?;
let d = get_typed_array(&ctx, 1)?;
let e = ctx.get::<JsUnknown>(2)?;
Expand Down
25 changes: 7 additions & 18 deletions src_ts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface Secp256k1InternalApi {
) => Uint8Array | null;
privateAdd: (d: Uint8Array, tweak: Uint8Array) => Uint8Array | null;
privateSub: (d: Uint8Array, tweak: Uint8Array) => Uint8Array | null;
signWithEntropy: (h: Uint8Array, d: Uint8Array, e?: Uint8Array) => Uint8Array;
sign: (h: Uint8Array, d: Uint8Array, e?: Uint8Array) => Uint8Array;
verify: (
h: Uint8Array,
Q: Uint8Array,
Expand Down Expand Up @@ -55,8 +55,7 @@ export interface Secp256k1Api {
) => Uint8Array | null;
privateAdd: (d: Uint8Array, tweak: Uint8Array) => Uint8Array | null;
privateSub: (d: Uint8Array, tweak: Uint8Array) => Uint8Array | null;
sign: (h: Uint8Array, d: Uint8Array) => Uint8Array;
signWithEntropy: (h: Uint8Array, d: Uint8Array, e?: Uint8Array) => Uint8Array;
sign: (h: Uint8Array, d: Uint8Array, e?: Uint8Array) => Uint8Array;
verify: (
h: Uint8Array,
Q: Uint8Array,
Expand All @@ -77,17 +76,6 @@ export default function createApi(
: validate.PUBLIC_KEY_UNCOMPRESSED_SIZE;
}

function signWithEntropy(
h: Uint8Array,
d: Uint8Array,
e?: Uint8Array
): Uint8Array {
validate.validateHash(h);
validate.validatePrivate(d);
validate.validateExtraData(e);
return secp256k1.signWithEntropy(h, d, e);
}

return {
__initializeContext(): void {
secp256k1.initializeContext();
Expand Down Expand Up @@ -169,12 +157,13 @@ export default function createApi(
return secp256k1.privateSub(d, tweak);
},

sign(h: Uint8Array, d: Uint8Array): Uint8Array {
return signWithEntropy(h, d);
sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array {
validate.validateHash(h);
validate.validatePrivate(d);
validate.validateExtraData(e);
return secp256k1.sign(h, d, e);
},

signWithEntropy,

verify(
h: Uint8Array,
Q: Uint8Array,
Expand Down
1 change: 0 additions & 1 deletion src_ts/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export const {
privateAdd,
privateSub,
sign,
signWithEntropy,
verify,
} = wasm;
1 change: 0 additions & 1 deletion src_ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ export const {
privateAdd,
privateSub,
sign,
signWithEntropy,
verify,
} = addon || wasm;
2 changes: 1 addition & 1 deletion src_ts/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default createApi({
}
},

signWithEntropy(h, d, e) {
sign(h, d, e) {
try {
HASH_INPUT.set(h);
PRIVATE_KEY_INPUT.set(d);
Expand Down
15 changes: 6 additions & 9 deletions tests/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ export default function (secp256k1) {
const expectedExtraEntropyN = fromHex(f.extraEntropyN);
const expectedExtraEntropyMax = fromHex(f.extraEntropyMax);

const sig = secp256k1.sign(m, d);
const extraEntropyUndefined = secp256k1.sign(m, d);
const extraEntropy0 = secp256k1.sign(m, d, buf1);
const extraEntropy1 = secp256k1.sign(m, d, buf2);
const extraEntropyRand = secp256k1.sign(m, d, buf3);
const extraEntropyN = secp256k1.sign(m, d, buf4);
const extraEntropyMax = secp256k1.sign(m, d, buf5);

const extraEntropyUndefined = secp256k1.signWithEntropy(m, d, undefined);
const extraEntropy0 = secp256k1.signWithEntropy(m, d, buf1);
const extraEntropy1 = secp256k1.signWithEntropy(m, d, buf2);
const extraEntropyRand = secp256k1.signWithEntropy(m, d, buf3);
const extraEntropyN = secp256k1.signWithEntropy(m, d, buf4);
const extraEntropyMax = secp256k1.signWithEntropy(m, d, buf5);

t.same(sig, expectedSig, `sign(${f.m}, ...) == ${f.signature}`);
t.same(
extraEntropyUndefined,
expectedSig,
Expand Down
1 change: 0 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ test("functions exported properly", (t) => {
"privateAdd",
"privateSub",
"sign",
"signWithEntropy",
"verify",
];
const source =
Expand Down