@ohos-ports/crypto 0.0.0-stage → 0.3.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +60 -2
  3. package/dist/bech32.d.ts +53 -0
  4. package/dist/bech32.js +187 -0
  5. package/dist/blake2b.d.ts +7 -0
  6. package/dist/blake2b.js +213 -0
  7. package/dist/bls12_318.d.ts +48 -0
  8. package/dist/bls12_318.js +268 -0
  9. package/dist/ed25519.d.ts +55 -0
  10. package/dist/ed25519.js +721 -0
  11. package/dist/hasGlobalWebCrypto.d.ts +1 -0
  12. package/dist/hasGlobalWebCrypto.js +21 -0
  13. package/dist/hmacSHA512.d.ts +6 -0
  14. package/dist/hmacSHA512.js +14 -0
  15. package/dist/index.d.ts +14 -0
  16. package/dist/index.js +30 -0
  17. package/dist/keccak.d.ts +4 -0
  18. package/dist/keccak.js +43 -0
  19. package/dist/kes/constants.d.ts +2 -0
  20. package/dist/kes/constants.js +5 -0
  21. package/dist/kes/index.d.ts +0 -0
  22. package/dist/kes/index.js +0 -0
  23. package/dist/kes/macro/sum_kes.d.ts +15 -0
  24. package/dist/kes/macro/sum_kes.js +9 -0
  25. package/dist/kes/singleKes.d.ts +0 -0
  26. package/dist/kes/singleKes.js +0 -0
  27. package/dist/noble/_assert.d.ts +23 -0
  28. package/dist/noble/_assert.js +55 -0
  29. package/dist/noble/_md.d.ts +29 -0
  30. package/dist/noble/_md.js +170 -0
  31. package/dist/noble/_shortw_utils.d.ts +61 -0
  32. package/dist/noble/_shortw_utils.js +63 -0
  33. package/dist/noble/_u64.d.ts +28 -0
  34. package/dist/noble/_u64.js +116 -0
  35. package/dist/noble/abstract/bls.d.ts +119 -0
  36. package/dist/noble/abstract/bls.js +334 -0
  37. package/dist/noble/abstract/curve.d.ts +83 -0
  38. package/dist/noble/abstract/curve.js +319 -0
  39. package/dist/noble/abstract/edwards.d.ts +85 -0
  40. package/dist/noble/abstract/edwards.js +508 -0
  41. package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
  42. package/dist/noble/abstract/hash-to-curve.js +220 -0
  43. package/dist/noble/abstract/modular.d.ts +122 -0
  44. package/dist/noble/abstract/modular.js +455 -0
  45. package/dist/noble/abstract/montgomery.d.ts +28 -0
  46. package/dist/noble/abstract/montgomery.js +172 -0
  47. package/dist/noble/abstract/utils.d.ts +106 -0
  48. package/dist/noble/abstract/utils.js +442 -0
  49. package/dist/noble/bls12_381.d.ts +95 -0
  50. package/dist/noble/bls12_381.js +1434 -0
  51. package/dist/noble/crypto.d.ts +1 -0
  52. package/dist/noble/crypto.js +4 -0
  53. package/dist/noble/curves/curve.d.ts +69 -0
  54. package/dist/noble/curves/curve.js +167 -0
  55. package/dist/noble/curves/modular.d.ts +122 -0
  56. package/dist/noble/curves/modular.js +442 -0
  57. package/dist/noble/curves/utils.d.ts +93 -0
  58. package/dist/noble/curves/utils.js +385 -0
  59. package/dist/noble/curves/weierstrass.d.ts +240 -0
  60. package/dist/noble/curves/weierstrass.js +1178 -0
  61. package/dist/noble/ed25519.d.ts +100 -0
  62. package/dist/noble/ed25519.js +494 -0
  63. package/dist/noble/hmac.d.ts +25 -0
  64. package/dist/noble/hmac.js +101 -0
  65. package/dist/noble/index.d.ts +2 -0
  66. package/dist/noble/index.js +20 -0
  67. package/dist/noble/keccak.d.ts +58 -0
  68. package/dist/noble/keccak.js +289 -0
  69. package/dist/noble/ripemd_160.d.ts +20 -0
  70. package/dist/noble/ripemd_160.js +147 -0
  71. package/dist/noble/secp256k1.d.ts +98 -0
  72. package/dist/noble/secp256k1.js +309 -0
  73. package/dist/noble/sha256.d.ts +34 -0
  74. package/dist/noble/sha256.js +146 -0
  75. package/dist/noble/sha512.d.ts +66 -0
  76. package/dist/noble/sha512.js +301 -0
  77. package/dist/noble/utils.d.ts +90 -0
  78. package/dist/noble/utils.js +278 -0
  79. package/dist/ripemd_160.d.ts +1 -0
  80. package/dist/ripemd_160.js +23 -0
  81. package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
  82. package/dist/rust_vrf_reimpl/curves.js +4225 -0
  83. package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
  84. package/dist/rust_vrf_reimpl/vrf03.js +373 -0
  85. package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
  86. package/dist/rust_vrf_reimpl/vrf10.js +468 -0
  87. package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
  88. package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
  89. package/dist/secp256k1.d.ts +4 -0
  90. package/dist/secp256k1.js +33 -0
  91. package/dist/sha2_256.d.ts +16 -0
  92. package/dist/sha2_256.js +403 -0
  93. package/dist/sha2_512.d.ts +16 -0
  94. package/dist/sha2_512.js +256 -0
  95. package/dist/sha3.d.ts +17 -0
  96. package/dist/sha3.js +181 -0
  97. package/dist/types.d.ts +26 -0
  98. package/dist/types.js +120 -0
  99. package/dist/utils/assert.d.ts +3 -0
  100. package/dist/utils/assert.js +55 -0
  101. package/dist/utils/base32.d.ts +5 -0
  102. package/dist/utils/base32.js +52 -0
  103. package/dist/utils/bigintToBuffer.d.ts +1 -0
  104. package/dist/utils/bigintToBuffer.js +27 -0
  105. package/dist/utils/bytesToHex.d.ts +2 -0
  106. package/dist/utils/bytesToHex.js +15 -0
  107. package/dist/utils/getRandomValues.d.ts +4 -0
  108. package/dist/utils/getRandomValues.js +12 -0
  109. package/dist/utils/hexToBytes.d.ts +2 -0
  110. package/dist/utils/hexToBytes.js +14 -0
  111. package/dist/utils/index.d.ts +3 -0
  112. package/dist/utils/index.js +19 -0
  113. package/dist/utils/positiveMod.d.ts +1 -0
  114. package/dist/utils/positiveMod.js +11 -0
  115. package/dist/utils/promisifySync.d.ts +0 -0
  116. package/dist/utils/promisifySync.js +0 -0
  117. package/dist/utils/rotr32.d.ts +2 -0
  118. package/dist/utils/rotr32.js +7 -0
  119. package/dist/vrf.d.ts +74 -0
  120. package/dist/vrf.js +286 -0
  121. package/package.json +49 -4
@@ -0,0 +1,468 @@
1
+ "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.scalar_from_bits_inplace = exports.vrf10_ed25519_sha512_ell2_compute_challenge = exports.vrf10_ed25519_sha512_ell2_nonce_generation = exports.scalar_hash_from_bytes = exports.vrf10_ed25519_sha512_ell2_hash_to_curve = exports.edwards_hash_from_bytes = exports.vrf10_proof_to_hash = exports.vrf10_ed25519_sha512_ell2_verify_proof = exports.vrf10_ed25519_sha512_ell2_generate_proof = exports.adjust_scalar_bits = exports.extend_secret_key = void 0;
20
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
21
+ var sha2_512_1 = require("../sha2_512.js");
22
+ var curves_1 = require("./curves.js");
23
+ /**
24
+ 4.4. RSA-FDH-VRF Ciphersuites
25
+
26
+ This document defines RSA-FDH-VRF-SHA256 as follows:
27
+
28
+ suite_string = 0x01.
29
+ The hash function Hash is SHA-256 as specified in [RFC6234], with hLen = 32.
30
+ MGF_salt = I2OSP(k, 4) || I2OSP(n, k).
31
+
32
+ This document defines RSA-FDH-VRF-SHA384 as follows:
33
+
34
+ suite_string = 0x02.
35
+ The hash function Hash is SHA-384 as specified in [RFC6234], with hLen = 48.
36
+ MGF_salt = I2OSP(k, 4) || I2OSP(n, k).
37
+
38
+ This document defines RSA-FDH-VRF-SHA512 as follows:
39
+
40
+ suite_string = 0x10.
41
+ The hash function Hash is SHA-512 as specified in [RFC6234], with hLen = 64.
42
+ MGF_salt = I2OSP(k, 4) || I2OSP(n, k).
43
+
44
+ */
45
+ var SUITE = 0x04;
46
+ /*
47
+ /// Temporary SUITE identifier, as TAI uses 0x03
48
+ pub const SUITE_TEMP: &[u8] = &[0x03];
49
+ */
50
+ var SUITE_TEMP = 0x03;
51
+ var ONE = 0x01;
52
+ var TWO = 0x02;
53
+ /*
54
+ pub fn extend(&self) -> (Scalar, [u8; 32]) {
55
+ let mut h: Sha512 = Sha512::new();
56
+ let mut extended = [0u8; 64];
57
+ let mut secret_key_bytes = [0u8; 32];
58
+ let mut extension = [0u8; 32];
59
+
60
+ h.update(self.as_bytes());
61
+ extended.copy_from_slice(&h.finalize().as_slice()[..64]);
62
+
63
+ secret_key_bytes.copy_from_slice(&extended[..32]);
64
+ extension.copy_from_slice(&extended[32..]);
65
+
66
+ secret_key_bytes[0] &= 248;
67
+ secret_key_bytes[31] &= 127;
68
+ secret_key_bytes[31] |= 64;
69
+
70
+ (Scalar::from_bits(secret_key_bytes), extension)
71
+ }
72
+ */
73
+ function extend_secret_key(secret_key) {
74
+ var extended = (0, sha2_512_1.sha2_512_sync)(secret_key);
75
+ var secret_key_bytes = extended.slice(0, 32);
76
+ var extension = extended.slice(32, 64);
77
+ secret_key_bytes[0] &= 248;
78
+ secret_key_bytes[31] &= 127;
79
+ secret_key_bytes[31] |= 64;
80
+ adjust_scalar_bits(secret_key_bytes);
81
+ return [secret_key_bytes, extension];
82
+ }
83
+ exports.extend_secret_key = extend_secret_key;
84
+ function adjust_scalar_bits(bytes) {
85
+ bytes[31] &= 127;
86
+ }
87
+ exports.adjust_scalar_bits = adjust_scalar_bits;
88
+ function vrf10_ed25519_sha512_ell2_generate_proof(secret_key, public_key, alpha_string) {
89
+ var _a = __read(extend_secret_key(secret_key), 2), secret_scalar = _a[0], secret_extension = _a[1];
90
+ var h = vrf10_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string);
91
+ var compressed_h = h.compress();
92
+ var gamma = h.scalarMul(secret_scalar);
93
+ var compressed_gamma = gamma.compress();
94
+ var k = vrf10_ed25519_sha512_ell2_nonce_generation(secret_extension, compressed_h);
95
+ var compressed_announcement_base = curves_1.ED25519_BASEPOINT_POINT.scalarMul(k).compress();
96
+ var compressed_announcement_h = h.scalarMul(k).compress();
97
+ // Self::compute_challenge(&compressed_h, &gamma, &announcement_base, &announcement_h);
98
+ var challenge = vrf10_ed25519_sha512_ell2_compute_challenge(compressed_h, compressed_gamma, compressed_announcement_base, compressed_announcement_h);
99
+ var response = (0, curves_1.add_scalars)(k, (0, curves_1.mul_scalars)(challenge, secret_scalar));
100
+ return {
101
+ gamma: gamma,
102
+ challenge: challenge,
103
+ response: response
104
+ };
105
+ }
106
+ exports.vrf10_ed25519_sha512_ell2_generate_proof = vrf10_ed25519_sha512_ell2_generate_proof;
107
+ /*
108
+ /// Verify VRF function, following the 10 specification.
109
+ pub fn verify(
110
+ &self,
111
+ public_key: &PublicKey10,
112
+ alpha_string: &[u8],
113
+ ) -> Result<[u8; OUTPUT_SIZE], VrfError> {
114
+ let h = Self::hash_to_curve(public_key, alpha_string);
115
+ let compressed_h = h.compress();
116
+
117
+ let decompressed_pk = public_key
118
+ .0
119
+ .decompress()
120
+ .ok_or(VrfError::DecompressionFailed)?;
121
+
122
+ if decompressed_pk.is_small_order() {
123
+ return Err(VrfError::PkSmallOrder);
124
+ }
125
+
126
+ let U = EdwardsPoint::vartime_double_scalar_mul_basepoint(
127
+ &self.challenge.neg(),
128
+ &decompressed_pk,
129
+ &self.response,
130
+ );
131
+ let V = EdwardsPoint::vartime_multiscalar_mul(
132
+ iter::once(self.response).chain(iter::once(self.challenge.neg())),
133
+ iter::once(h).chain(iter::once(self.gamma)),
134
+ );
135
+
136
+ // Now we compute the challenge
137
+ let challenge = Self::compute_challenge(&compressed_h, &self.gamma, &U, &V);
138
+
139
+ if challenge.to_bytes()[..16] == self.challenge.to_bytes()[..16] {
140
+ Ok(self.proof_to_hash())
141
+ } else {
142
+ Err(VrfError::VerificationFailed)
143
+ }
144
+ }
145
+ */
146
+ function vrf10_ed25519_sha512_ell2_verify_proof(public_key, alpha_string, proof) {
147
+ var h = vrf10_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string);
148
+ var compressed_h = h.compress();
149
+ var decompressed_pk = (0, curves_1.decompressCompressedEdwardsY)(public_key); // pointFromBytes( public_key );
150
+ if (!decompressed_pk || decompressed_pk.is_small_order())
151
+ return false;
152
+ // Scalar
153
+ var proof_challenge = proof.challenge;
154
+ var gamma = proof.gamma;
155
+ var U = curves_1.EdwardsPoint.vartime_double_scalar_mul_basepoint((0, curves_1.negate_scalar)(proof_challenge), decompressed_pk, proof.response);
156
+ /*
157
+ let V = EdwardsPoint::vartime_multiscalar_mul(
158
+ iter::once(self.response).chain(iter::once(self.challenge.neg())),
159
+ iter::once(h).chain(iter::once(self.gamma)),
160
+ );
161
+ */
162
+ var V = curves_1.EdwardsPoint.vartime_multiscalar_mul([proof.response, (0, curves_1.negate_scalar)(proof_challenge)], [h, gamma]);
163
+ var computed_challenge = vrf10_ed25519_sha512_ell2_compute_challenge(compressed_h, proof.gamma.compress(), U.compress(), V.compress());
164
+ return (0, uint8array_utils_1.uint8ArrayEq)(computed_challenge.slice(0, 16), proof_challenge.slice(0, 16));
165
+ }
166
+ exports.vrf10_ed25519_sha512_ell2_verify_proof = vrf10_ed25519_sha512_ell2_verify_proof;
167
+ /*
168
+ /// `proof_to_hash` function, following the 10 specification. This computes the output of the VRF
169
+ /// function. In particular, this function computes
170
+ /// SHA512(SUITE || THREE || Gamma || ZERO)
171
+ fn proof_to_hash(&self) -> [u8; OUTPUT_SIZE] {
172
+ let mut output = [0u8; OUTPUT_SIZE];
173
+ let gamma_cofac = self.gamma.mul_by_cofactor();
174
+ let mut hash = Sha512::new();
175
+ hash.update(SUITE_TEMP);
176
+ hash.update(THREE);
177
+ hash.update(gamma_cofac.compress().as_bytes());
178
+ hash.update(ZERO);
179
+
180
+ output.copy_from_slice(hash.finalize().as_slice());
181
+ output
182
+ }
183
+ */
184
+ function vrf10_proof_to_hash(proof) {
185
+ var compressed_gamma_cofac = proof.gamma.mul_by_cofactor().compress();
186
+ var hashInput = new Uint8Array(3 + compressed_gamma_cofac.length);
187
+ hashInput[0] = SUITE_TEMP;
188
+ hashInput[1] = 0x03;
189
+ hashInput.set(compressed_gamma_cofac, 2);
190
+ hashInput[2 + compressed_gamma_cofac.length] = 0;
191
+ return (0, sha2_512_1.sha2_512_sync)(hashInput);
192
+ }
193
+ exports.vrf10_proof_to_hash = vrf10_proof_to_hash;
194
+ /*
195
+ export function vrfProofToBytesProof( proof: IVrfProof10 )
196
+ {
197
+ return {
198
+ gamma: bigpointToUint8Array( proof.gamma ),
199
+ challange: encodeInt( proof.challange ),
200
+ response: encodeInt( proof.response ),
201
+ }
202
+ }
203
+ //*/
204
+ function edwards_hash_from_bytes(bytes) {
205
+ var hash = (0, sha2_512_1.sha2_512_sync)(bytes);
206
+ var res = hash.slice(0, 32);
207
+ /*
208
+ //////////////////////////////////////////////////////////////////////////////////////
209
+ //// We need to be compatible with the third version of the VRF standard, which ////
210
+ //// always unsets the sign bit. Given the visibility of the functions used to ////
211
+ //// compute the ristretto mapping, we need to fork the original repo, to unset ////
212
+ //// bit in this function. This fork should never be used, unless one needs to be ////
213
+ //// compatible with the version 3 of the VRF standard. ////
214
+ //// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-10#section-5.4.1.2 ////
215
+ //// The goal is to be always a single commit ahead of version 3.2.0, and this ////
216
+ //// single commit only changes the following line, and a disclaimer in the ////
217
+ //// README. ////
218
+ //////////////////////////////////////////////////////////////////////////////////////
219
+ let sign_bit = 0;
220
+
221
+ let fe = FieldElement::from_bytes(&res);
222
+
223
+ let M1 = crate::montgomery::elligator_encode(&fe);
224
+ let E1_opt = M1.to_edwards(sign_bit);
225
+
226
+ E1_opt
227
+ .expect("Montgomery conversion to Edwards point in Elligator failed")
228
+ .mul_by_cofactor()
229
+ */
230
+ var sign_bit = 0;
231
+ var fe = curves_1.FieldElem51.fromBytes(res);
232
+ var M1 = curves_1.FieldElem51.elligator_encode(fe);
233
+ var E1_opt = M1.to_edwards(sign_bit);
234
+ if (!E1_opt) {
235
+ throw new Error("Montgomery conversion to Edwards point in Elligator failed");
236
+ }
237
+ return E1_opt.mul_by_cofactor();
238
+ }
239
+ exports.edwards_hash_from_bytes = edwards_hash_from_bytes;
240
+ /*
241
+ /// Computing the `hash_to_curve` using try and increment. In order to make the
242
+ /// function always terminate, we bound the number of tries to 64. If the try
243
+ /// 64 fails, which happens with probability around 1/2^64, we compute the
244
+ /// Elligator mapping. This diverges from the standard: the latter describes
245
+ /// the function with an infinite loop. To avoid infinite loops or possibly
246
+ /// non-terminating functions, we adopt this modification.
247
+ /// This function is temporary, until `curve25519` provides a `hash_to_curve` as
248
+ /// presented in the standard. Depends on
249
+ /// [this pr](https://github.com/dalek-cryptography/curve25519-dalek/pull/377). The
250
+ /// goal of providing this temporary function, is to ensure that the rest of the
251
+ /// implementation is valid with respect to the standard, by using their test-vectors.
252
+ pub(crate) fn hash_to_curve(public_key: &PublicKey10, alpha_string: &[u8]) -> EdwardsPoint {
253
+ let mut counter = 0u8;
254
+ let mut hash_input = Vec::with_capacity(4 + PUBLIC_KEY_SIZE + alpha_string.len());
255
+ hash_input.extend_from_slice(SUITE_TEMP);
256
+ hash_input.extend_from_slice(ONE);
257
+ hash_input.extend_from_slice(public_key.as_bytes());
258
+ hash_input.extend_from_slice(alpha_string);
259
+ hash_input.extend_from_slice(&counter.to_be_bytes());
260
+ hash_input.extend_from_slice(ZERO);
261
+
262
+ while counter < 64 {
263
+ hash_input[2 + PUBLIC_KEY_SIZE + alpha_string.len()] = counter.to_be_bytes()[0];
264
+ if let Some(result) =
265
+ CompressedEdwardsY::from_slice(&Sha512::digest(&hash_input)[..32]).decompress()
266
+ {
267
+ return result.mul_by_cofactor();
268
+ };
269
+
270
+ counter += 1;
271
+ }
272
+
273
+ EdwardsPoint::hash_from_bytes::<Sha512>(&hash_input)
274
+ }
275
+ */
276
+ /**
277
+ * Computing the `hash_to_curve` using try and increment. In order to make the
278
+ * function always terminate, we bound the number of tries to 64. If the try
279
+ * 64 fails, which happens with probability around 1/2^64, we compute the
280
+ * Elligator mapping. This diverges from the standard: the latter describes
281
+ * the function with an infinite loop. To avoid infinite loops or possibly
282
+ * non-terminating functions, we adopt this modification.
283
+ * This function is temporary, until `curve25519` provides a `hash_to_curve` as
284
+ * presented in the standard. Depends on
285
+ * [this pr](https://github.com/dalek-cryptography/curve25519-dalek/pull/377). The
286
+ * goal of providing this temporary function, is to ensure that the rest of the
287
+ * implementation is valid with respect to the standard, by using their test-vectors.
288
+ **/
289
+ function vrf10_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string) {
290
+ var counter_u8 = 0;
291
+ var input = new Uint8Array(4 + 32 + alpha_string.length);
292
+ input[0] = SUITE_TEMP;
293
+ input[1] = ONE;
294
+ input.set(public_key.slice(0, 32), 2);
295
+ input.set(alpha_string, 34);
296
+ input[34 + alpha_string.length] = counter_u8;
297
+ input[35 + alpha_string.length] = 0;
298
+ while (counter_u8 < 64) {
299
+ input[34 + alpha_string.length] = counter_u8;
300
+ var result = (0, curves_1.decompressCompressedEdwardsY)((0, sha2_512_1.sha2_512_sync)(input).slice(0, 32));
301
+ if (result)
302
+ return result.mul_by_cofactor();
303
+ ++counter_u8;
304
+ }
305
+ return edwards_hash_from_bytes(input); // sha2_512_sync(input);
306
+ }
307
+ exports.vrf10_ed25519_sha512_ell2_hash_to_curve = vrf10_ed25519_sha512_ell2_hash_to_curve;
308
+ function scalar_hash_from_bytes(input) {
309
+ return (0, curves_1.scalar_from_bytes_mod_order_wide)((0, sha2_512_1.sha2_512_sync)(input));
310
+ }
311
+ exports.scalar_hash_from_bytes = scalar_hash_from_bytes;
312
+ function vrf10_ed25519_sha512_ell2_nonce_generation(secret_extension, compressed_h) {
313
+ var input = new Uint8Array(64);
314
+ input.set(secret_extension.slice(0, 32), 0);
315
+ input.set(compressed_h.slice(0, 32), 32);
316
+ //return pointFromBytes(sha2_512_sync(input))
317
+ return scalar_hash_from_bytes(input);
318
+ }
319
+ exports.vrf10_ed25519_sha512_ell2_nonce_generation = vrf10_ed25519_sha512_ell2_nonce_generation;
320
+ /*
321
+ /// Hash points function, following the 10 specification.
322
+ fn compute_challenge(
323
+ compressed_h: &CompressedEdwardsY,
324
+ gamma: &EdwardsPoint,
325
+ announcement_1: &EdwardsPoint,
326
+ announcement_2: &EdwardsPoint,
327
+ ) -> Scalar {
328
+ // we use a scalar of 16 bytes (instead of 32), but store it in 32 bits, as that is what
329
+ // `Scalar::from_bits()` expects.
330
+ let mut scalar_bytes = [0u8; 32];
331
+ let mut challenge_hash = Sha512::new();
332
+ challenge_hash.update(SUITE);
333
+ challenge_hash.update(TWO);
334
+ challenge_hash.update(compressed_h.to_bytes());
335
+ challenge_hash.update(gamma.compress().as_bytes());
336
+ challenge_hash.update(announcement_1.compress().as_bytes());
337
+ challenge_hash.update(announcement_2.compress().as_bytes());
338
+
339
+ scalar_bytes[..16].copy_from_slice(&challenge_hash.finalize().as_slice()[..16]);
340
+
341
+ Scalar::from_bits(scalar_bytes)
342
+ }
343
+ * /
344
+ export function vrf_ed25519_sha512_ell2_challenge_generation(
345
+ H: Uint8Array,
346
+ gamma: Uint8Array,
347
+ announcement_1: Uint8Array,
348
+ announcement_2: Uint8Array,
349
+ ): Uint8Array
350
+ {
351
+ // const input = new Uint8Array( 2 + H.length + gamma.length + announcement_1.length + announcement_2.length );
352
+ // input[0] = SUITE;
353
+ // input[1] = TWO;
354
+ // input.set( H, 2 );
355
+ // input.set( gamma, 2 + H.length );
356
+ // input.set( announcement_1, 2 + H.length + gamma.length );
357
+ // input.set( announcement_2, 2 + H.length + gamma.length + announcement_1.length );
358
+ // return sha2_512_sync(input).slice(0,16);
359
+ }
360
+ //*/
361
+ /*
362
+ /// Hash points function, following the 10 specification.
363
+ pub(crate) fn compute_challenge(
364
+ compressed_h: &CompressedEdwardsY,
365
+ gamma: &EdwardsPoint,
366
+ announcement_1: &EdwardsPoint,
367
+ announcement_2: &EdwardsPoint,
368
+ ) -> Scalar {
369
+ // we use a scalar of 16 bytes (instead of 32), but store it in 32 bits, as that is what
370
+ // `Scalar::from_bits()` expects.
371
+ let mut scalar_bytes = [0u8; 32];
372
+ let mut challenge_hash = Sha512::new();
373
+ challenge_hash.update(SUITE_TEMP);
374
+ challenge_hash.update(TWO);
375
+ challenge_hash.update(compressed_h.to_bytes());
376
+ challenge_hash.update(gamma.compress().as_bytes());
377
+ challenge_hash.update(announcement_1.compress().as_bytes());
378
+ challenge_hash.update(announcement_2.compress().as_bytes());
379
+ challenge_hash.update(ZERO);
380
+
381
+ scalar_bytes[..16].copy_from_slice(&challenge_hash.finalize().as_slice()[..16]);
382
+
383
+ Scalar::from_bits(scalar_bytes)
384
+ }
385
+ */
386
+ function vrf10_ed25519_sha512_ell2_compute_challenge(compressed_h, compressed_gamma, compressed_announcement_1, compressed_announcement_2) {
387
+ var scalar_bytes = new Uint8Array(32);
388
+ var hash_input = new Uint8Array(3 +
389
+ compressed_h.length +
390
+ compressed_gamma.length +
391
+ compressed_announcement_1.length +
392
+ compressed_announcement_2.length);
393
+ hash_input[0] = SUITE_TEMP;
394
+ hash_input[1] = TWO;
395
+ var len = 2;
396
+ hash_input.set(compressed_h, len);
397
+ len += compressed_h.length;
398
+ hash_input.set(compressed_gamma, len);
399
+ len += compressed_gamma.length;
400
+ hash_input.set(compressed_announcement_1, len);
401
+ len += compressed_announcement_1.length;
402
+ hash_input.set(compressed_announcement_2, len);
403
+ hash_input[len + compressed_announcement_2.length] = 0;
404
+ var hash = (0, sha2_512_1.sha2_512_sync)(hash_input);
405
+ scalar_bytes.set(hash.slice(0, 16), 0);
406
+ scalar_from_bits_inplace(scalar_bytes);
407
+ return scalar_bytes;
408
+ }
409
+ exports.vrf10_ed25519_sha512_ell2_compute_challenge = vrf10_ed25519_sha512_ell2_compute_challenge;
410
+ /*
411
+ // Construct a `Scalar` from the low 255 bits of a 256-bit integer.
412
+ ///
413
+ /// This function is intended for applications like X25519 which
414
+ /// require specific bit-patterns when performing scalar
415
+ /// multiplication.
416
+ pub const fn from_bits(bytes: [u8; 32]) -> Scalar {
417
+ let mut s = Scalar{bytes};
418
+ // Ensure that s < 2^255 by masking the high bit
419
+ s.bytes[31] &= 0b0111_1111;
420
+
421
+ s
422
+ }
423
+ */
424
+ function scalar_from_bits_inplace(bytes) {
425
+ bytes[31] &= 127;
426
+ }
427
+ exports.scalar_from_bits_inplace = scalar_from_bits_inplace;
428
+ /**
429
+ * https://datatracker.ietf.org/doc/html/rfc9381#section-5.1
430
+ *
431
+ * test cases:
432
+ * https://datatracker.ietf.org/doc/html/rfc9381#name-ecvrf-edwards25519-sha512-e
433
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-10#appendix-A.4
434
+ *
435
+ * /// Generate a new VRF proof following the 10 standard. It proceeds as follows:
436
+ * /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
437
+ * /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
438
+ * /// - Compute `Gamma = secret_scalar * H`
439
+ * /// - Generate a proof of discrete logarithm equality between `PK` and `Gamma` with
440
+ * /// bases `generator` and `H` respectively.
441
+ *
442
+ * /
443
+ export function vrf_ed25519_sha512_ell2_prove(sk: Uint8Array, alpha: Uint8Array): IVrfProof10
444
+ {
445
+ /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
446
+ const [ scalar, extension ] = getExtendEd25519PrivateKeyComponents_sync( sk );
447
+ // 1. Use SK to derive the VRF secret scalar x and the VRF public key Y = x*B
448
+ /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
449
+ const pk = new Uint8Array( deriveEd25519PublicKey_sync( sk ) );
450
+ const H = vrf_ed25519_sha512_ell2_hash_to_curve( pk, alpha );
451
+
452
+ const H_point = pointFromBytes( H );
453
+ const gamma = scalarMul( H_point, scalar );
454
+ const compressed_gamma = bigpointToUint8Array( gamma );
455
+ const k = vrf_ed25519_sha512_ell2_nonce_generation( extension, H );
456
+ const announcement_1 = bigpointToUint8Array( scalarMultBase( k ) );
457
+ const announcement_2 = bigpointToUint8Array( scalarMul( H_point, k ) );
458
+ const challange = scalarFromBytes(
459
+ vrf_ed25519_sha512_ell2_challenge_generation( H, compressed_gamma, announcement_1, announcement_2 )
460
+ );
461
+ const response = k + challange * scalar;
462
+ return {
463
+ gamma,
464
+ challange,
465
+ response
466
+ };
467
+ }
468
+ //*/
@@ -0,0 +1,53 @@
1
+ import { EdwardsPoint } from "./curves.js";
2
+ export interface IVrfBatchProof10 {
3
+ gamma: EdwardsPoint;
4
+ u_point: EdwardsPoint;
5
+ v_point: EdwardsPoint;
6
+ response: Uint8Array;
7
+ }
8
+ export declare function vrf10_batch_ed25519_sha512_ell2_proof_to_hash(proof: IVrfBatchProof10): Uint8Array & {
9
+ length: 64;
10
+ };
11
+ export declare function vrf10_batch_ed25519_sha512_ell2_generate_proof(secret_key: Uint8Array, public_key: Uint8Array, alpha_string: Uint8Array): IVrfBatchProof10;
12
+ export declare function vrf10_batch_ed25519_sha512_ell2_verify_proof(public_key: Uint8Array, alpha_string: Uint8Array, proof: IVrfBatchProof10): boolean;
13
+ /**
14
+ * https://datatracker.ietf.org/doc/html/rfc9381#section-5.1
15
+ *
16
+ * test cases:
17
+ * https://datatracker.ietf.org/doc/html/rfc9381#name-ecvrf-edwards25519-sha512-e
18
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-10#appendix-A.4
19
+ *
20
+ * /// Generate a new VRF proof following the 10 standard. It proceeds as follows:
21
+ * /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
22
+ * /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
23
+ * /// - Compute `Gamma = secret_scalar * H`
24
+ * /// - Generate a proof of discrete logarithm equality between `PK` and `Gamma` with
25
+ * /// bases `generator` and `H` respectively.
26
+ *
27
+ * /
28
+ function vrf_ed25519_sha512_ell2_prove(sk: Uint8Array, alpha: Uint8Array): IVrfBatchProof10
29
+ {
30
+ /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
31
+ const [ scalar, extension ] = getExtendEd25519PrivateKeyComponents_sync( sk );
32
+ // 1. Use SK to derive the VRF secret scalar x and the VRF public key Y = x*B
33
+ /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
34
+ const pk = new Uint8Array( deriveEd25519PublicKey_sync( sk ) );
35
+ const H = vrf_ed25519_sha512_ell2_hash_to_curve( pk, alpha );
36
+
37
+ const H_point = pointFromBytes( H );
38
+ const gamma = scalarMul( H_point, scalar );
39
+ const compressed_gamma = bigpointToUint8Array( gamma );
40
+ const k = vrf_ed25519_sha512_ell2_nonce_generation( extension, H );
41
+ const announcement_1 = bigpointToUint8Array( scalarMultBase( k ) );
42
+ const announcement_2 = bigpointToUint8Array( scalarMul( H_point, k ) );
43
+ const challange = scalarFromBytes(
44
+ vrf_ed25519_sha512_ell2_challenge_generation( H, compressed_gamma, announcement_1, announcement_2 )
45
+ );
46
+ const response = k + challange * scalar;
47
+ return {
48
+ gamma,
49
+ challange,
50
+ response
51
+ };
52
+ }
53
+ //*/