-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathparams.ts
More file actions
142 lines (125 loc) · 4.18 KB
/
Copy pathparams.ts
File metadata and controls
142 lines (125 loc) · 4.18 KB
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
import { bbsCommitMsgs, bbsCommitMsgsConstantTime } from 'crypto-wasm-new';
import {
bbsGenerateSignatureParams,
bbsSignatureParamsToBytes,
bbsSignatureParamsFromBytes,
bbsIsSignatureParamsValid,
bbsAdaptSigParamsForMsgCount,
BbsSigParams
} from 'crypto-wasm-new';
import { flattenMessageStructure, getSigParamsOfRequiredSize } from '../sign-verify-js-objs';
import { ISignatureParams, MessageStructure } from '../types';
/**
* `BBS` signature parameters.
*/
export class BBSSignatureParams implements ISignatureParams {
label?: Uint8Array;
value: BbsSigParams;
constructor(params: BbsSigParams, label?: Uint8Array) {
this.value = params;
this.label = label;
}
static generate(numMessages: number, label?: Uint8Array): BBSSignatureParams {
const params = bbsGenerateSignatureParams(numMessages, label);
return new BBSSignatureParams(params, label);
}
static generateAsBytes(numMessages: number, label?: Uint8Array): Uint8Array {
return BBSSignatureParams.generate(numMessages, label).toBytes();
}
/**
* Commit to given messages and return the commitment
* @param messageToCommit
* @param encodeMessages
*/
commitToMessages(messageToCommit: Map<number, Uint8Array>, encodeMessages: boolean): Uint8Array {
return bbsCommitMsgs(messageToCommit, this.value, encodeMessages);
}
commitToMessagesConstantTime(messageToCommit: Map<number, Uint8Array>, encodeMessages: boolean): Uint8Array {
return bbsCommitMsgsConstantTime(messageToCommit, this.value, encodeMessages);
}
toBytes(): Uint8Array {
return bbsSignatureParamsToBytes(this.value);
}
isValid(): boolean {
return bbsIsSignatureParamsValid(this.value);
}
static valueFromBytes(bytes: Uint8Array): BbsSigParams {
return bbsSignatureParamsFromBytes(bytes);
}
/**
* Transform current signature params to sign a different number of messages. Needs the `label` field to be present
* @param newMsgCount
*/
adapt(newMsgCount: number): this {
if (this.label === undefined) {
throw new Error(`Label should be present`);
}
let newParams: BbsSigParams;
if (newMsgCount <= this.supportedMessageCount()) {
newParams = {
g1: this.value.g1,
g2: this.value.g2,
h: this.value.h.slice(0, newMsgCount)
};
} else {
newParams = bbsAdaptSigParamsForMsgCount(this.value, this.label, newMsgCount);
}
return new (this.constructor as typeof BBSSignatureParams)(newParams, this.label) as this;
}
/**
* Gives `BBSSignatureParams` that can sign `msgCount` number of messages.
* @param msgCount
* @param labelOrParams
*/
static getSigParamsOfRequiredSize(
msgCount: number,
labelOrParams: Uint8Array | BBSSignatureParams
): BBSSignatureParams {
return getSigParamsOfRequiredSize(BBSSignatureParams, msgCount, labelOrParams);
}
/**
* Number of messages that these params support and can be signed. If less or more messages are to be signed, use
* `adapt`
*/
supportedMessageCount(): number {
return this.value.h.length;
}
/**
* Is message index valid as per the params
* @param index
*/
isValidIndex(index: number): boolean {
return index >= 0 && index < this.supportedMessageCount();
}
/**
* Get params, i.e. generator from `this.value.h` for certain indices
* @param indices
*/
getParamsForIndices(indices: number[]): Uint8Array[] {
const p: Uint8Array[] = [];
for (const i of indices) {
if (!this.isValidIndex(i)) {
throw new Error(`Invalid index ${i} for params with supported message count ${this.supportedMessageCount()}`);
}
p.push(this.value.h[i]);
}
return p;
}
static getSigParamsForMsgStructure(
msgStructure: MessageStructure,
labelOrParams: Uint8Array | BBSSignatureParams
): BBSSignatureParams {
const msgCount = Object.keys(flattenMessageStructure(msgStructure)).length;
return this.getSigParamsOfRequiredSize(msgCount, labelOrParams);
}
toJSON(): string {
return JSON.stringify({
value: {
g1: Array.from(this.value.g1),
g2: Array.from(this.value.g2),
h: this.value.h.map((h: Uint8Array) => Array.from(h))
},
label: this.label
});
}
}