-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathecdsa.test.ts
151 lines (133 loc) · 4.35 KB
/
ecdsa.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { type EcdsaSignature, NadaValue } from "@nillion/client-wasm";
import { secp256k1 } from "@noble/curves/secp256k1";
import { sha256 } from "@noble/hashes/sha2";
import { beforeAll, describe, expect, it } from "vitest";
import { createSignerFromKey } from "#/payment";
import {
type NadaValuesRecord,
type Uuid,
ValuesPermissionsBuilder,
} from "#/types";
import { UpdatePermissionsBuilder, type VmClient, VmClientBuilder } from "#/vm";
import { Env, PrivateKeyPerSuite } from "./helpers";
describe("Ecdsa Signature", () => {
// Program id
const tecdsaProgramId = "builtin/tecdsa_sign";
// Input store name
const tecdsaKeyName = "tecdsa_private_key";
const tecdsaDigestName = "tecdsa_digest_message";
const tecdsaSignatureName = "tecdsa_signature";
// Party names
const tecdsaKeyParty = "tecdsa_key_party";
const tecdsaDigestParty = "tecdsa_digest_message_party";
const tecdsaOutputParty = "tecdsa_output_party";
const privateKey: Uint8Array = secp256k1.utils.randomPrivateKey();
const publicKey: Uint8Array = secp256k1.getPublicKey(privateKey);
let digestMessage: Uint8Array;
let client: VmClient;
beforeAll(async () => {
const signer = await createSignerFromKey(
PrivateKeyPerSuite.EcdsaSignatures,
);
digestMessage = sha256("A deep message with a deep number: 42");
client = await new VmClientBuilder()
.seed("tests")
.bootnodeUrl(Env.bootnodeUrl)
.chainUrl(Env.nilChainUrl)
.signer(signer)
.build();
});
let privateKeyStoreId: Uuid;
it("store private key", async () => {
privateKeyStoreId = await client
.storeValues()
.ttl(1)
.value(tecdsaKeyName, NadaValue.new_ecdsa_private_key(privateKey))
.build()
.invoke();
expect(privateKeyStoreId).toHaveLength(36);
});
it("update private key permissions", async () => {
const permissions = UpdatePermissionsBuilder.init(client)
.valuesId(privateKeyStoreId)
.grantCompute(client.id, tecdsaProgramId)
.build();
await permissions.invoke();
});
it("retrieve private key", async () => {
const data = await client
.retrieveValues()
.id(privateKeyStoreId)
.build()
.invoke();
const values = data[tecdsaKeyName]!;
expect(values).toBeDefined();
expect(values.type).toBe("EcdsaPrivateKey");
expect(values.value).toEqual(privateKey);
});
let digestMessageStoreId: Uuid;
it("store digest message", async () => {
const permissions = ValuesPermissionsBuilder.defaultOwner(client.id)
.grantCompute(client.id, tecdsaProgramId)
.build();
digestMessageStoreId = await client
.storeValues()
.ttl(1)
.value(
tecdsaDigestName,
NadaValue.new_ecdsa_digest_message(digestMessage),
)
.permissions(permissions)
.build()
.invoke();
expect(digestMessageStoreId).toHaveLength(36);
});
it("retrieve digest message", async () => {
const data = await client
.retrieveValues()
.id(digestMessageStoreId)
.build()
.invoke();
const values = data[tecdsaDigestName]!;
expect(values).toBeDefined();
expect(values.type).toBe("EcdsaDigestMessage");
expect(values.value).toEqual(digestMessage);
});
let computeResultId: Uuid;
it("sign message", async () => {
computeResultId = await client
.invokeCompute()
.program(tecdsaProgramId)
.inputParty(tecdsaKeyParty, client.id)
.inputParty(tecdsaDigestParty, client.id)
.outputParty(tecdsaOutputParty, [client.id])
.valueIds(privateKeyStoreId, digestMessageStoreId)
.build()
.invoke();
expect(computeResultId).toHaveLength(36);
});
let computeResult: NadaValuesRecord;
it("retrieve signature", async () => {
computeResult = await client
.retrieveComputeResult()
.id(computeResultId)
.build()
.invoke();
const values = computeResult[tecdsaSignatureName]!;
expect(values).toBeDefined();
});
it("ecdsa verify", async () => {
const signature = computeResult[tecdsaSignatureName]
?.value as EcdsaSignature;
const r = toBigInt(signature.r());
const s = toBigInt(signature.s());
expect(secp256k1.verify({ r: r, s: s }, digestMessage, publicKey)).true;
});
});
function toBigInt(bytes: Uint8Array) {
let ret = 0n;
for (const value of bytes.values()) {
ret = (ret << 8n) + BigInt(value);
}
return ret;
}