@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,60 @@
1
+ import { EdwardsPoint } from "./curves.js";
2
+ export interface IVrfProof03 {
3
+ gamma: EdwardsPoint;
4
+ challenge: Uint8Array;
5
+ response: Uint8Array;
6
+ }
7
+ export declare function extend_secret_key(secret_key: Uint8Array): [scalar: Uint8Array, extension: Uint8Array];
8
+ export declare function adjust_scalar_bits(bytes: Uint8Array): void;
9
+ export declare function vrf_ed25519_sha512_ell2_generate_proof(secret_key: Uint8Array, public_key: Uint8Array, alpha_string: Uint8Array): IVrfProof03;
10
+ export declare function vrf_ed25519_sha512_ell2_verify_proof(public_key: Uint8Array, alpha_string: Uint8Array, proof: IVrfProof03): boolean;
11
+ export declare function proof_to_hash(proof: IVrfProof03): Uint8Array & {
12
+ length: 64;
13
+ };
14
+ export declare function edwards_hash_from_bytes(bytes: Uint8Array): EdwardsPoint;
15
+ export declare function vrf_ed25519_sha512_ell2_hash_to_curve(public_key: Uint8Array, alpha_string: Uint8Array): EdwardsPoint;
16
+ export declare function scalar_hash_from_bytes(input: Uint8Array): Uint8Array;
17
+ export declare function vrf_ed25519_sha512_ell2_nonce_generation(secret_extension: Uint8Array, compressed_h: Uint8Array): Uint8Array;
18
+ export declare function vrf_ed25519_sha512_ell2_compute_challenge(compressed_h: Uint8Array, compressed_gamma: Uint8Array, compressed_announcement_1: Uint8Array, compressed_announcement_2: Uint8Array): Uint8Array;
19
+ export declare function scalar_from_bits_inplace(bytes: Uint8Array): void;
20
+ /**
21
+ * https://datatracker.ietf.org/doc/html/rfc9381#section-5.1
22
+ *
23
+ * test cases:
24
+ * https://datatracker.ietf.org/doc/html/rfc9381#name-ecvrf-edwards25519-sha512-e
25
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-03#appendix-A.4
26
+ *
27
+ * /// Generate a new VRF proof following the 03 standard. It proceeds as follows:
28
+ * /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
29
+ * /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
30
+ * /// - Compute `Gamma = secret_scalar * H`
31
+ * /// - Generate a proof of discrete logarithm equality between `PK` and `Gamma` with
32
+ * /// bases `generator` and `H` respectively.
33
+ *
34
+ * /
35
+ export function vrf_ed25519_sha512_ell2_prove(sk: Uint8Array, alpha: Uint8Array): IVrfProof03
36
+ {
37
+ /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
38
+ const [ scalar, extension ] = getExtendEd25519PrivateKeyComponents_sync( sk );
39
+ // 1. Use SK to derive the VRF secret scalar x and the VRF public key Y = x*B
40
+ /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
41
+ const pk = new Uint8Array( deriveEd25519PublicKey_sync( sk ) );
42
+ const H = vrf_ed25519_sha512_ell2_hash_to_curve( pk, alpha );
43
+
44
+ const H_point = pointFromBytes( H );
45
+ const gamma = scalarMul( H_point, scalar );
46
+ const compressed_gamma = bigpointToUint8Array( gamma );
47
+ const k = vrf_ed25519_sha512_ell2_nonce_generation( extension, H );
48
+ const announcement_1 = bigpointToUint8Array( scalarMultBase( k ) );
49
+ const announcement_2 = bigpointToUint8Array( scalarMul( H_point, k ) );
50
+ const challange = scalarFromBytes(
51
+ vrf_ed25519_sha512_ell2_challenge_generation( H, compressed_gamma, announcement_1, announcement_2 )
52
+ );
53
+ const response = k + challange * scalar;
54
+ return {
55
+ gamma,
56
+ challange,
57
+ response
58
+ };
59
+ }
60
+ //*/
@@ -0,0 +1,373 @@
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.vrf_ed25519_sha512_ell2_compute_challenge = exports.vrf_ed25519_sha512_ell2_nonce_generation = exports.scalar_hash_from_bytes = exports.vrf_ed25519_sha512_ell2_hash_to_curve = exports.edwards_hash_from_bytes = exports.proof_to_hash = exports.vrf_ed25519_sha512_ell2_verify_proof = exports.vrf_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 = 0x03.
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
+ var ONE = 0x01;
47
+ var TWO = 0x02;
48
+ /*
49
+ pub fn extend(&self) -> (Scalar, [u8; 32]) {
50
+ let mut h: Sha512 = Sha512::new();
51
+ let mut extended = [0u8; 64];
52
+ let mut secret_key_bytes = [0u8; 32];
53
+ let mut extension = [0u8; 32];
54
+
55
+ h.update(self.as_bytes());
56
+ extended.copy_from_slice(&h.finalize().as_slice()[..64]);
57
+
58
+ secret_key_bytes.copy_from_slice(&extended[..32]);
59
+ extension.copy_from_slice(&extended[32..]);
60
+
61
+ secret_key_bytes[0] &= 248;
62
+ secret_key_bytes[31] &= 127;
63
+ secret_key_bytes[31] |= 64;
64
+
65
+ (Scalar::from_bits(secret_key_bytes), extension)
66
+ }
67
+ */
68
+ function extend_secret_key(secret_key) {
69
+ var extended = (0, sha2_512_1.sha2_512_sync)(secret_key);
70
+ var secret_key_bytes = extended.slice(0, 32);
71
+ var extension = extended.slice(32, 64);
72
+ secret_key_bytes[0] &= 248;
73
+ secret_key_bytes[31] &= 127;
74
+ secret_key_bytes[31] |= 64;
75
+ adjust_scalar_bits(secret_key_bytes);
76
+ return [secret_key_bytes, extension];
77
+ }
78
+ exports.extend_secret_key = extend_secret_key;
79
+ function adjust_scalar_bits(bytes) {
80
+ bytes[31] &= 127;
81
+ }
82
+ exports.adjust_scalar_bits = adjust_scalar_bits;
83
+ function vrf_ed25519_sha512_ell2_generate_proof(secret_key, public_key, alpha_string) {
84
+ var _a = __read(extend_secret_key(secret_key), 2), secret_scalar = _a[0], secret_extension = _a[1];
85
+ var h = vrf_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string);
86
+ var compressed_h = h.compress();
87
+ var gamma = h.scalarMul(secret_scalar);
88
+ var compressed_gamma = gamma.compress();
89
+ var k = vrf_ed25519_sha512_ell2_nonce_generation(secret_extension, compressed_h);
90
+ var compressed_announcement_base = curves_1.ED25519_BASEPOINT_POINT.scalarMul(k).compress();
91
+ var compressed_announcement_h = h.scalarMul(k).compress();
92
+ // Self::compute_challenge(&compressed_h, &gamma, &announcement_base, &announcement_h);
93
+ var challenge = vrf_ed25519_sha512_ell2_compute_challenge(compressed_h, compressed_gamma, compressed_announcement_base, compressed_announcement_h);
94
+ var response = (0, curves_1.add_scalars)(k, (0, curves_1.mul_scalars)(challenge, secret_scalar));
95
+ return {
96
+ gamma: gamma,
97
+ challenge: challenge,
98
+ response: response
99
+ };
100
+ }
101
+ exports.vrf_ed25519_sha512_ell2_generate_proof = vrf_ed25519_sha512_ell2_generate_proof;
102
+ /*
103
+ /// Verify VRF function, following the 03 specification.
104
+ pub fn verify(
105
+ &self,
106
+ public_key: &PublicKey03,
107
+ alpha_string: &[u8],
108
+ ) -> Result<[u8; OUTPUT_SIZE], VrfError> {
109
+ let h = Self::hash_to_curve(public_key, alpha_string);
110
+ let compressed_h = h.compress();
111
+
112
+ let decompressed_pk = public_key
113
+ .0
114
+ .decompress()
115
+ .ok_or(VrfError::DecompressionFailed)?;
116
+
117
+ if decompressed_pk.is_small_order() {
118
+ return Err(VrfError::PkSmallOrder);
119
+ }
120
+
121
+ let U = EdwardsPoint::vartime_double_scalar_mul_basepoint(
122
+ &self.challenge.neg(),
123
+ &decompressed_pk,
124
+ &self.response,
125
+ );
126
+ let V = EdwardsPoint::vartime_multiscalar_mul(
127
+ iter::once(self.response).chain(iter::once(self.challenge.neg())),
128
+ iter::once(h).chain(iter::once(self.gamma)),
129
+ );
130
+
131
+ // Now we compute the challenge
132
+ let challenge = Self::compute_challenge(&compressed_h, &self.gamma, &U, &V);
133
+
134
+ if challenge.to_bytes()[..16] == self.challenge.to_bytes()[..16] {
135
+ Ok(self.proof_to_hash())
136
+ } else {
137
+ Err(VrfError::VerificationFailed)
138
+ }
139
+ }
140
+ */
141
+ function vrf_ed25519_sha512_ell2_verify_proof(public_key, alpha_string, proof) {
142
+ var h = vrf_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string);
143
+ var compressed_h = h.compress();
144
+ var decompressed_pk = (0, curves_1.decompressCompressedEdwardsY)(public_key); // pointFromBytes( public_key );
145
+ if (!decompressed_pk || decompressed_pk.is_small_order())
146
+ return false;
147
+ // Scalar
148
+ var proof_challenge = proof.challenge;
149
+ var U = curves_1.EdwardsPoint.vartime_double_scalar_mul_basepoint((0, curves_1.negate_scalar)(proof_challenge), decompressed_pk, proof.response);
150
+ var gamma = proof.gamma;
151
+ /*
152
+ let V = EdwardsPoint::vartime_multiscalar_mul(
153
+ iter::once(self.response).chain(iter::once(self.challenge.neg())),
154
+ iter::once(h).chain(iter::once(self.gamma)),
155
+ );
156
+ */
157
+ var V = curves_1.EdwardsPoint.vartime_multiscalar_mul([proof.response, (0, curves_1.negate_scalar)(proof_challenge)], [h, gamma]);
158
+ var computed_challenge = vrf_ed25519_sha512_ell2_compute_challenge(compressed_h, proof.gamma.compress(), U.compress(), V.compress());
159
+ return (0, uint8array_utils_1.uint8ArrayEq)(computed_challenge.slice(0, 16), proof_challenge.slice(0, 16));
160
+ }
161
+ exports.vrf_ed25519_sha512_ell2_verify_proof = vrf_ed25519_sha512_ell2_verify_proof;
162
+ /*
163
+ /// `proof_to_hash` function, following the 03 specification. This computes the output of the VRF
164
+ /// function. In particular, this function computes
165
+ /// SHA512(SUITE || THREE || Gamma)
166
+ pub fn proof_to_hash(&self) -> [u8; OUTPUT_SIZE] {
167
+ let mut output = [0u8; OUTPUT_SIZE];
168
+ let gamma_cofac = self.gamma.mul_by_cofactor();
169
+ let mut hash = Sha512::new();
170
+ hash.update(SUITE);
171
+ hash.update(THREE);
172
+ hash.update(gamma_cofac.compress().as_bytes());
173
+
174
+ output.copy_from_slice(hash.finalize().as_slice());
175
+ output
176
+ }
177
+
178
+ */
179
+ function proof_to_hash(proof) {
180
+ var gamma_cofac = proof.gamma.mul_by_cofactor();
181
+ var hashInput = new Uint8Array(2 + gamma_cofac.compress().length);
182
+ hashInput[0] = SUITE;
183
+ hashInput[1] = 0x03;
184
+ hashInput.set(gamma_cofac.compress(), 2);
185
+ return (0, sha2_512_1.sha2_512_sync)(hashInput);
186
+ }
187
+ exports.proof_to_hash = proof_to_hash;
188
+ /*
189
+ export function vrfProofToBytesProof( proof: IVrfProof03 )
190
+ {
191
+ return {
192
+ gamma: bigpointToUint8Array( proof.gamma ),
193
+ challange: encodeInt( proof.challange ),
194
+ response: encodeInt( proof.response ),
195
+ }
196
+ }
197
+ //*/
198
+ function edwards_hash_from_bytes(bytes) {
199
+ var hash = (0, sha2_512_1.sha2_512_sync)(bytes);
200
+ var res = hash.slice(0, 32);
201
+ /*
202
+ //////////////////////////////////////////////////////////////////////////////////////
203
+ //// We need to be compatible with the third version of the VRF standard, which ////
204
+ //// always unsets the sign bit. Given the visibility of the functions used to ////
205
+ //// compute the ristretto mapping, we need to fork the original repo, to unset ////
206
+ //// bit in this function. This fork should never be used, unless one needs to be ////
207
+ //// compatible with the version 3 of the VRF standard. ////
208
+ //// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-03#section-5.4.1.2 ////
209
+ //// The goal is to be always a single commit ahead of version 3.2.0, and this ////
210
+ //// single commit only changes the following line, and a disclaimer in the ////
211
+ //// README. ////
212
+ //////////////////////////////////////////////////////////////////////////////////////
213
+ let sign_bit = 0;
214
+
215
+ let fe = FieldElement::from_bytes(&res);
216
+
217
+ let M1 = crate::montgomery::elligator_encode(&fe);
218
+ let E1_opt = M1.to_edwards(sign_bit);
219
+
220
+ E1_opt
221
+ .expect("Montgomery conversion to Edwards point in Elligator failed")
222
+ .mul_by_cofactor()
223
+ */
224
+ var sign_bit = 0;
225
+ var fe = curves_1.FieldElem51.fromBytes(res);
226
+ var M1 = curves_1.FieldElem51.elligator_encode(fe);
227
+ var E1_opt = M1.to_edwards(sign_bit);
228
+ if (!E1_opt) {
229
+ throw new Error("Montgomery conversion to Edwards point in Elligator failed");
230
+ }
231
+ return E1_opt.mul_by_cofactor();
232
+ }
233
+ exports.edwards_hash_from_bytes = edwards_hash_from_bytes;
234
+ function vrf_ed25519_sha512_ell2_hash_to_curve(public_key, alpha_string) {
235
+ var input = new Uint8Array(2 + public_key.length + alpha_string.length);
236
+ input[0] = SUITE;
237
+ input[1] = ONE;
238
+ input.set(public_key, 2);
239
+ input.set(alpha_string, 2 + public_key.length);
240
+ return edwards_hash_from_bytes(input); // sha2_512_sync(input);
241
+ }
242
+ exports.vrf_ed25519_sha512_ell2_hash_to_curve = vrf_ed25519_sha512_ell2_hash_to_curve;
243
+ function scalar_hash_from_bytes(input) {
244
+ return (0, curves_1.scalar_from_bytes_mod_order_wide)((0, sha2_512_1.sha2_512_sync)(input));
245
+ }
246
+ exports.scalar_hash_from_bytes = scalar_hash_from_bytes;
247
+ function vrf_ed25519_sha512_ell2_nonce_generation(secret_extension, compressed_h) {
248
+ var input = new Uint8Array(64);
249
+ input.set(secret_extension, 0);
250
+ input.set(compressed_h, 32);
251
+ //return pointFromBytes(sha2_512_sync(input))
252
+ return scalar_hash_from_bytes(input);
253
+ }
254
+ exports.vrf_ed25519_sha512_ell2_nonce_generation = vrf_ed25519_sha512_ell2_nonce_generation;
255
+ /*
256
+ /// Hash points function, following the 03 specification.
257
+ fn compute_challenge(
258
+ compressed_h: &CompressedEdwardsY,
259
+ gamma: &EdwardsPoint,
260
+ announcement_1: &EdwardsPoint,
261
+ announcement_2: &EdwardsPoint,
262
+ ) -> Scalar {
263
+ // we use a scalar of 16 bytes (instead of 32), but store it in 32 bits, as that is what
264
+ // `Scalar::from_bits()` expects.
265
+ let mut scalar_bytes = [0u8; 32];
266
+ let mut challenge_hash = Sha512::new();
267
+ challenge_hash.update(SUITE);
268
+ challenge_hash.update(TWO);
269
+ challenge_hash.update(compressed_h.to_bytes());
270
+ challenge_hash.update(gamma.compress().as_bytes());
271
+ challenge_hash.update(announcement_1.compress().as_bytes());
272
+ challenge_hash.update(announcement_2.compress().as_bytes());
273
+
274
+ scalar_bytes[..16].copy_from_slice(&challenge_hash.finalize().as_slice()[..16]);
275
+
276
+ Scalar::from_bits(scalar_bytes)
277
+ }
278
+ * /
279
+ export function vrf_ed25519_sha512_ell2_challenge_generation(
280
+ H: Uint8Array,
281
+ gamma: Uint8Array,
282
+ announcement_1: Uint8Array,
283
+ announcement_2: Uint8Array,
284
+ ): Uint8Array
285
+ {
286
+ // const input = new Uint8Array( 2 + H.length + gamma.length + announcement_1.length + announcement_2.length );
287
+ // input[0] = SUITE;
288
+ // input[1] = TWO;
289
+ // input.set( H, 2 );
290
+ // input.set( gamma, 2 + H.length );
291
+ // input.set( announcement_1, 2 + H.length + gamma.length );
292
+ // input.set( announcement_2, 2 + H.length + gamma.length + announcement_1.length );
293
+ // return sha2_512_sync(input).slice(0,16);
294
+ }
295
+ //*/
296
+ function vrf_ed25519_sha512_ell2_compute_challenge(compressed_h, compressed_gamma, compressed_announcement_1, compressed_announcement_2) {
297
+ var scalar_bytes = new Uint8Array(32);
298
+ var hash_input = new Uint8Array(2 +
299
+ compressed_h.length +
300
+ compressed_gamma.length +
301
+ compressed_announcement_1.length +
302
+ compressed_announcement_2.length);
303
+ hash_input[0] = SUITE;
304
+ hash_input[1] = TWO;
305
+ hash_input.set(compressed_h, 2);
306
+ hash_input.set(compressed_gamma, 2 + compressed_h.length);
307
+ hash_input.set(compressed_announcement_1, 2 + compressed_h.length + compressed_gamma.length);
308
+ hash_input.set(compressed_announcement_2, 2 + compressed_h.length + compressed_gamma.length + compressed_announcement_1.length);
309
+ var hash = (0, sha2_512_1.sha2_512_sync)(hash_input);
310
+ scalar_bytes.set(hash.slice(0, 16), 0);
311
+ scalar_from_bits_inplace(scalar_bytes);
312
+ return scalar_bytes;
313
+ }
314
+ exports.vrf_ed25519_sha512_ell2_compute_challenge = vrf_ed25519_sha512_ell2_compute_challenge;
315
+ /*
316
+ // Construct a `Scalar` from the low 255 bits of a 256-bit integer.
317
+ ///
318
+ /// This function is intended for applications like X25519 which
319
+ /// require specific bit-patterns when performing scalar
320
+ /// multiplication.
321
+ pub const fn from_bits(bytes: [u8; 32]) -> Scalar {
322
+ let mut s = Scalar{bytes};
323
+ // Ensure that s < 2^255 by masking the high bit
324
+ s.bytes[31] &= 0b0111_1111;
325
+
326
+ s
327
+ }
328
+ */
329
+ function scalar_from_bits_inplace(bytes) {
330
+ bytes[31] &= 127;
331
+ }
332
+ exports.scalar_from_bits_inplace = scalar_from_bits_inplace;
333
+ /**
334
+ * https://datatracker.ietf.org/doc/html/rfc9381#section-5.1
335
+ *
336
+ * test cases:
337
+ * https://datatracker.ietf.org/doc/html/rfc9381#name-ecvrf-edwards25519-sha512-e
338
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-03#appendix-A.4
339
+ *
340
+ * /// Generate a new VRF proof following the 03 standard. It proceeds as follows:
341
+ * /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
342
+ * /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
343
+ * /// - Compute `Gamma = secret_scalar * H`
344
+ * /// - Generate a proof of discrete logarithm equality between `PK` and `Gamma` with
345
+ * /// bases `generator` and `H` respectively.
346
+ *
347
+ * /
348
+ export function vrf_ed25519_sha512_ell2_prove(sk: Uint8Array, alpha: Uint8Array): IVrfProof03
349
+ {
350
+ /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
351
+ const [ scalar, extension ] = getExtendEd25519PrivateKeyComponents_sync( sk );
352
+ // 1. Use SK to derive the VRF secret scalar x and the VRF public key Y = x*B
353
+ /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
354
+ const pk = new Uint8Array( deriveEd25519PublicKey_sync( sk ) );
355
+ const H = vrf_ed25519_sha512_ell2_hash_to_curve( pk, alpha );
356
+
357
+ const H_point = pointFromBytes( H );
358
+ const gamma = scalarMul( H_point, scalar );
359
+ const compressed_gamma = bigpointToUint8Array( gamma );
360
+ const k = vrf_ed25519_sha512_ell2_nonce_generation( extension, H );
361
+ const announcement_1 = bigpointToUint8Array( scalarMultBase( k ) );
362
+ const announcement_2 = bigpointToUint8Array( scalarMul( H_point, k ) );
363
+ const challange = scalarFromBytes(
364
+ vrf_ed25519_sha512_ell2_challenge_generation( H, compressed_gamma, announcement_1, announcement_2 )
365
+ );
366
+ const response = k + challange * scalar;
367
+ return {
368
+ gamma,
369
+ challange,
370
+ response
371
+ };
372
+ }
373
+ //*/
@@ -0,0 +1,73 @@
1
+ import { EdwardsPoint } from "./curves.js";
2
+ export interface IVrfProof10 {
3
+ gamma: EdwardsPoint;
4
+ challenge: Uint8Array;
5
+ response: Uint8Array;
6
+ }
7
+ export declare function extend_secret_key(secret_key: Uint8Array): [scalar: Uint8Array, extension: Uint8Array];
8
+ export declare function adjust_scalar_bits(bytes: Uint8Array): void;
9
+ export declare function vrf10_ed25519_sha512_ell2_generate_proof(secret_key: Uint8Array, public_key: Uint8Array, alpha_string: Uint8Array): IVrfProof10;
10
+ export declare function vrf10_ed25519_sha512_ell2_verify_proof(public_key: Uint8Array, alpha_string: Uint8Array, proof: IVrfProof10): boolean;
11
+ export declare function vrf10_proof_to_hash(proof: IVrfProof10): Uint8Array & {
12
+ length: 64;
13
+ };
14
+ export declare function edwards_hash_from_bytes(bytes: Uint8Array): EdwardsPoint;
15
+ /**
16
+ * Computing the `hash_to_curve` using try and increment. In order to make the
17
+ * function always terminate, we bound the number of tries to 64. If the try
18
+ * 64 fails, which happens with probability around 1/2^64, we compute the
19
+ * Elligator mapping. This diverges from the standard: the latter describes
20
+ * the function with an infinite loop. To avoid infinite loops or possibly
21
+ * non-terminating functions, we adopt this modification.
22
+ * This function is temporary, until `curve25519` provides a `hash_to_curve` as
23
+ * presented in the standard. Depends on
24
+ * [this pr](https://github.com/dalek-cryptography/curve25519-dalek/pull/377). The
25
+ * goal of providing this temporary function, is to ensure that the rest of the
26
+ * implementation is valid with respect to the standard, by using their test-vectors.
27
+ **/
28
+ export declare function vrf10_ed25519_sha512_ell2_hash_to_curve(public_key: Uint8Array, alpha_string: Uint8Array): EdwardsPoint;
29
+ export declare function scalar_hash_from_bytes(input: Uint8Array): Uint8Array;
30
+ export declare function vrf10_ed25519_sha512_ell2_nonce_generation(secret_extension: Uint8Array, compressed_h: Uint8Array): Uint8Array;
31
+ export declare function vrf10_ed25519_sha512_ell2_compute_challenge(compressed_h: Uint8Array, compressed_gamma: Uint8Array, compressed_announcement_1: Uint8Array, compressed_announcement_2: Uint8Array): Uint8Array;
32
+ export declare function scalar_from_bits_inplace(bytes: Uint8Array): void;
33
+ /**
34
+ * https://datatracker.ietf.org/doc/html/rfc9381#section-5.1
35
+ *
36
+ * test cases:
37
+ * https://datatracker.ietf.org/doc/html/rfc9381#name-ecvrf-edwards25519-sha512-e
38
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-10#appendix-A.4
39
+ *
40
+ * /// Generate a new VRF proof following the 10 standard. It proceeds as follows:
41
+ * /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
42
+ * /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
43
+ * /// - Compute `Gamma = secret_scalar * H`
44
+ * /// - Generate a proof of discrete logarithm equality between `PK` and `Gamma` with
45
+ * /// bases `generator` and `H` respectively.
46
+ *
47
+ * /
48
+ export function vrf_ed25519_sha512_ell2_prove(sk: Uint8Array, alpha: Uint8Array): IVrfProof10
49
+ {
50
+ /// - Extend the secret key, into a `secret_scalar` and the `secret_extension`
51
+ const [ scalar, extension ] = getExtendEd25519PrivateKeyComponents_sync( sk );
52
+ // 1. Use SK to derive the VRF secret scalar x and the VRF public key Y = x*B
53
+ /// - Evaluate `hash_to_curve` over PK || alpha_string to get `H`
54
+ const pk = new Uint8Array( deriveEd25519PublicKey_sync( sk ) );
55
+ const H = vrf_ed25519_sha512_ell2_hash_to_curve( pk, alpha );
56
+
57
+ const H_point = pointFromBytes( H );
58
+ const gamma = scalarMul( H_point, scalar );
59
+ const compressed_gamma = bigpointToUint8Array( gamma );
60
+ const k = vrf_ed25519_sha512_ell2_nonce_generation( extension, H );
61
+ const announcement_1 = bigpointToUint8Array( scalarMultBase( k ) );
62
+ const announcement_2 = bigpointToUint8Array( scalarMul( H_point, k ) );
63
+ const challange = scalarFromBytes(
64
+ vrf_ed25519_sha512_ell2_challenge_generation( H, compressed_gamma, announcement_1, announcement_2 )
65
+ );
66
+ const response = k + challange * scalar;
67
+ return {
68
+ gamma,
69
+ challange,
70
+ response
71
+ };
72
+ }
73
+ //*/