-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ristretto255.move
1194 lines (949 loc) · 43.6 KB
/
ristretto255.move
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// This module contains functions for Ristretto255 curve arithmetic, assuming addition as the group operation.
///
/// The order of the Ristretto255 elliptic curve group is $\ell = 2^252 + 27742317777372353535851937790883648493$, same
/// as the order of the prime-order subgroup of Curve25519.
///
/// This module provides two structs for encoding Ristretto elliptic curves to the developer:
///
/// - First, a 32-byte-sized CompressedRistretto struct, which is used to persist points in storage.
///
/// - Second, a larger, in-memory, RistrettoPoint struct, which is decompressable from a CompressedRistretto struct. This
/// larger struct can be used for fast arithmetic operations (additions, multiplications, etc.). The results can be saved
/// back into storage by compressing RistrettoPoint structs back to CompressedRistretto structs.
///
/// This module also provides a Scalar struct for persisting scalars in storage and doing fast arithmetic on them.
///
/// One invariant maintained by this module is that all CompressedRistretto structs store a canonically-encoded point,
/// which can always be decompressed into a valid point on the curve as a RistrettoPoint struct. Unfortunately, due to
/// limitations in our underlying curve25519-dalek elliptic curve library, this decompression will unnecessarily verify
/// the validity of the point and thus slightly decrease performance.
///
/// Similarly, all Scalar structs store a canonically-encoded scalar, which can always be safely operated on using
/// arithmetic operations.
///
/// In the future, we might support additional features:
///
/// * For scalars:
/// - batch_invert()
///
/// * For points:
/// - double()
/// + The challenge is that curve25519-dalek does NOT export double for Ristretto points (nor for Edwards)
///
/// - double_and_compress_batch()
///
/// - fixed-base, variable-time via optional_mixed_multiscalar_mul() in VartimePrecomputedMultiscalarMul
/// + This would require a storage-friendly RistrettoBasepointTable and an in-memory variant of it too
/// + Similar to the CompressedRistretto and RistrettoPoint structs in this module
/// + The challenge is that curve25519-dalek's RistrettoBasepointTable is not serializable
module aptos_std::ristretto255 {
use std::option::Option;
#[test_only]
use std::option;
//
// Constants
//
/// The order of the Ristretto255 group and its scalar field, in little-endian.
const ORDER_ELL: vector<u8> = x"edd3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010";
/// `ORDER_ELL` - 1: i.e., the "largest", reduced scalar in the field
const L_MINUS_ONE: vector<u8> = x"ecd3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010";
/// The maximum size in bytes of a canonically-encoded Scalar is 32 bytes.
const MAX_SCALAR_NUM_BYTES: u64 = 32u64;
/// The maximum size in bits of a canonically-encoded Scalar is 256 bits.
const MAX_SCALAR_NUM_BITS: u64 = 256u64;
/// The maximum size in bytes of a canonically-encoded Ristretto255 point is 32 bytes.
const MAX_POINT_NUM_BYTES: u64 = 32u64;
/// The basepoint (generator) of the Ristretto255 group
const BASE_POINT: vector<u8> = x"e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76";
//
// Reasons for error codes
//
/// The number of scalars does not match the number of points.
const E_DIFFERENT_NUM_POINTS_AND_SCALARS: u64 = 1;
/// Expected more than zero points as input.
const E_ZERO_POINTS: u64 = 2;
/// Expected more than zero scalars as input.
const E_ZERO_SCALARS: u64 = 3;
/// Too many points have been created in the current transaction execution.
const E_TOO_MANY_POINTS_CREATED: u64 = 4;
//
// Scalar and point structs
//
/// This struct represents a scalar as a little-endian byte encoding of an integer in $\mathbb{Z}_\ell$, which is
/// stored in `data`. Here, \ell denotes the order of the scalar field (and the underlying elliptic curve group).
struct Scalar has copy, store, drop {
data: vector<u8>
}
/// This struct represents a serialized point on the Ristretto255 curve, in 32 bytes.
/// This struct can be decompressed from storage into an in-memory RistrettoPoint, on which fast curve arithmetic
/// can be performed.
struct CompressedRistretto has copy, store, drop {
data: vector<u8>
}
/// This struct represents an in-memory Ristretto255 point and supports fast curve arithmetic.
///
/// An important invariant: There will never be two RistrettoPoint's constructed with the same handle. One can have
/// immutable references to the same RistrettoPoint, of course.
struct RistrettoPoint has drop {
handle: u64
}
//
// Functions for arithmetic on points
//
/// Returns the identity point as a CompressedRistretto.
public fun point_identity_compressed(): CompressedRistretto {
CompressedRistretto {
data: x"0000000000000000000000000000000000000000000000000000000000000000"
}
}
/// Returns the identity point as a CompressedRistretto.
public fun point_identity(): RistrettoPoint {
RistrettoPoint {
handle: point_identity_internal()
}
}
/// Returns the basepoint (generator) of the Ristretto255 group as a compressed point
public fun basepoint_compressed(): CompressedRistretto {
CompressedRistretto {
data: BASE_POINT
}
}
/// Returns the basepoint (generator) of the Ristretto255 group
public fun basepoint(): RistrettoPoint {
let (handle, _) = point_decompress_internal(BASE_POINT);
RistrettoPoint {
handle
}
}
/// Multiplies the basepoint (generator) of the Ristretto255 group by a scalar and returns the result.
/// This call is much faster than `point_mul(&basepoint(), &some_scalar)` because of precomputation tables.
public fun basepoint_mul(a: &Scalar): RistrettoPoint {
RistrettoPoint {
handle: basepoint_mul_internal(a.data)
}
}
/// Creates a new CompressedRistretto point from a sequence of 32 bytes. If those bytes do not represent a valid
/// point, returns None.
public fun new_compressed_point_from_bytes(bytes: vector<u8>): Option<CompressedRistretto> {
if (point_is_canonical_internal(bytes)) {
std::option::some(CompressedRistretto {
data: bytes
})
} else {
std::option::none<CompressedRistretto>()
}
}
/// Creates a new RistrettoPoint from a sequence of 32 bytes. If those bytes do not represent a valid point,
/// returns None.
public fun new_point_from_bytes(bytes: vector<u8>): Option<RistrettoPoint> {
let (handle, is_canonical) = point_decompress_internal(bytes);
if (is_canonical) {
std::option::some(RistrettoPoint { handle })
} else {
std::option::none<RistrettoPoint>()
}
}
/// Hashes the input to a uniformly-at-random RistrettoPoint via SHA512.
public fun new_point_from_sha512(sha512: vector<u8>): RistrettoPoint {
RistrettoPoint {
handle: new_point_from_sha512_internal(sha512)
}
}
/// Samples a uniformly-at-random RistrettoPoint given a sequence of 64 uniformly-at-random bytes. This function
/// can be used to build a collision-resistant hash function that maps 64-byte messages to RistrettoPoint's.
public fun new_point_from_64_uniform_bytes(bytes: vector<u8>): Option<RistrettoPoint> {
if (std::vector::length(&bytes) == 64) {
std::option::some(RistrettoPoint {
handle: new_point_from_64_uniform_bytes_internal(bytes)
})
} else {
std::option::none<RistrettoPoint>()
}
}
/// Decompresses a CompressedRistretto from storage into a RistrettoPoint which can be used for fast arithmetic.
public fun point_decompress(point: &CompressedRistretto): RistrettoPoint {
// NOTE: Our CompressedRistretto invariant assures us that every CompressedRistretto in storage is a valid
// RistrettoPoint
let (handle, _) = point_decompress_internal(point.data);
RistrettoPoint { handle }
}
/// Compresses a RistrettoPoint to a CompressedRistretto which can be put in storage.
public fun point_compress(point: &RistrettoPoint): CompressedRistretto {
CompressedRistretto {
data: point_compress_internal(point)
}
}
/// Returns the sequence of bytes representin this Ristretto point.
/// To convert a RistrettoPoint 'p' to bytes, first compress it via `c = point_compress(&p)`, and then call this
/// function on `c`.
public fun point_to_bytes(point: &CompressedRistretto): vector<u8> {
point.data
}
/// Returns a * point.
public fun point_mul(point: &RistrettoPoint, a: &Scalar): RistrettoPoint {
RistrettoPoint {
handle: point_mul_internal(point, a.data, false)
}
}
/// Sets a *= point and returns 'a'.
public fun point_mul_assign(point: &mut RistrettoPoint, a: &Scalar): &mut RistrettoPoint {
point_mul_internal(point, a.data, true);
point
}
/// Returns (a * some_point + b * base_point), where base_point is the Ristretto basepoint encoded in `BASE_POINT`.
public fun basepoint_double_mul(a: &Scalar, some_point: &RistrettoPoint, b: &Scalar): RistrettoPoint {
RistrettoPoint {
handle: basepoint_double_mul_internal(a.data, some_point, b.data)
}
}
/// Returns a + b
public fun point_add(a: &RistrettoPoint, b: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
handle: point_add_internal(a, b, false)
}
}
/// Sets a += b and returns 'a'.
public fun point_add_assign(a: &mut RistrettoPoint, b: &RistrettoPoint): &mut RistrettoPoint {
point_add_internal(a, b, true);
a
}
/// Returns a - b
public fun point_sub(a: &RistrettoPoint, b: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
handle: point_sub_internal(a, b, false)
}
}
/// Sets a -= b and returns 'a'.
public fun point_sub_assign(a: &mut RistrettoPoint, b: &RistrettoPoint): &mut RistrettoPoint {
point_sub_internal(a, b, true);
a
}
/// Returns -a
public fun point_neg(a: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
handle: point_neg_internal(a, false)
}
}
/// Sets a = -a, and returns 'a'.
public fun point_neg_assign(a: &mut RistrettoPoint): &mut RistrettoPoint {
point_neg_internal(a, true);
a
}
/// Returns true if the two RistrettoPoints are the same points on the elliptic curve.
native public fun point_equals(g: &RistrettoPoint, h: &RistrettoPoint): bool;
/// Computes a multi-scalar multiplication, returning a_1 p_1 + a_2 p_2 + ... + a_n p_n.
/// This function is much faster than computing each a_i p_i using `point_mul` and adding up the results using `point_add`.
public fun multi_scalar_mul(points: &vector<RistrettoPoint>, scalars: &vector<Scalar>): RistrettoPoint {
assert!(!std::vector::is_empty(points), std::error::invalid_argument(E_ZERO_POINTS));
assert!(!std::vector::is_empty(scalars), std::error::invalid_argument(E_ZERO_SCALARS));
assert!(std::vector::length(points) == std::vector::length(scalars), std::error::invalid_argument(E_DIFFERENT_NUM_POINTS_AND_SCALARS));
RistrettoPoint {
handle: multi_scalar_mul_internal<RistrettoPoint, Scalar>(points, scalars)
}
}
//
// Functions for arithmetic on Scalars
//
/// Given a sequence of 32 bytes, checks if they canonically-encode a Scalar and return it.
/// Otherwise, returns None.
public fun new_scalar_from_bytes(bytes: vector<u8>): Option<Scalar> {
if (scalar_is_canonical_internal(bytes)) {
std::option::some(Scalar {
data: bytes
})
} else {
std::option::none<Scalar>()
}
}
/// Hashes the input to a uniformly-at-random Scalar via SHA512
public fun new_scalar_from_sha512(sha512_input: vector<u8>): Scalar {
Scalar {
data: scalar_from_sha512_internal(sha512_input)
}
}
/// Creates a Scalar from an u8.
public fun new_scalar_from_u8(byte: u8): Scalar {
let s = scalar_zero();
let byte_zero = std::vector::borrow_mut(&mut s.data, 0);
*byte_zero = byte;
s
}
/// Creates a Scalar from an u64.
public fun new_scalar_from_u64(eight_bytes: u64): Scalar {
Scalar {
data: scalar_from_u64_internal(eight_bytes)
}
}
/// Creates a Scalar from an u128.
public fun new_scalar_from_u128(sixteen_bytes: u128): Scalar {
Scalar {
data: scalar_from_u128_internal(sixteen_bytes)
}
}
/// Creates a Scalar from 32 bytes by reducing the little-endian-encoded number in those bytes modulo $\ell$.
public fun new_scalar_reduced_from_32_bytes(bytes: vector<u8>): Option<Scalar> {
if (std::vector::length(&bytes) == 32) {
std::option::some(Scalar {
data: scalar_reduced_from_32_bytes_internal(bytes)
})
} else {
std::option::none()
}
}
/// Samples a scalar uniformly-at-random given 64 uniform-at-random bytes as input by reducing the little-endian-encoded number
/// in those bytes modulo $\ell$.
public fun new_scalar_uniform_from_64_bytes(bytes: vector<u8>): Option<Scalar> {
if (std::vector::length(&bytes) == 64) {
std::option::some(Scalar {
data: scalar_uniform_from_64_bytes_internal(bytes)
})
} else {
std::option::none()
}
}
/// Returns 0 as a Scalar.
public fun scalar_zero(): Scalar {
Scalar {
data: x"0000000000000000000000000000000000000000000000000000000000000000"
}
}
/// Returns true if the given Scalar equals 0.
public fun scalar_is_zero(s: &Scalar): bool {
s.data == x"0000000000000000000000000000000000000000000000000000000000000000"
}
/// Returns 1 as a Scalar.
public fun scalar_one(): Scalar {
Scalar {
data: x"0100000000000000000000000000000000000000000000000000000000000000"
}
}
/// Returns true if the given Scalar equals 1.
public fun scalar_is_one(s: &Scalar): bool {
s.data == x"0100000000000000000000000000000000000000000000000000000000000000"
}
/// Returns true if the two scalars are equal.
public fun scalar_equals(lhs: &Scalar, rhs: &Scalar): bool {
lhs.data == rhs.data
}
/// Returns the inverse s^{-1} mod \ell of a scalar s.
/// Returns None if s is zero.
public fun scalar_invert(s: &Scalar): Option<Scalar> {
if (scalar_is_zero(s)) {
std::option::none<Scalar>()
} else {
std::option::some(Scalar {
data: scalar_invert_internal(s.data)
})
}
}
/// Returns the product of the two scalars.
public fun scalar_mul(a: &Scalar, b: &Scalar): Scalar {
Scalar {
data: scalar_mul_internal(a.data, b.data)
}
}
/// Computes the product of 'a' and 'b' and assigns the result to 'a'.
/// Returns 'a'.
public fun scalar_mul_assign(a: &mut Scalar, b: &Scalar): &mut Scalar {
a.data = scalar_mul(a, b).data;
a
}
/// Returns the sum of the two scalars.
public fun scalar_add(a: &Scalar, b: &Scalar): Scalar {
Scalar {
data: scalar_add_internal(a.data, b.data)
}
}
/// Computes the sum of 'a' and 'b' and assigns the result to 'a'
/// Returns 'a'.
public fun scalar_add_assign(a: &mut Scalar, b: &Scalar): &mut Scalar {
a.data = scalar_add(a, b).data;
a
}
/// Returns the difference of the two scalars.
public fun scalar_sub(a: &Scalar, b: &Scalar): Scalar {
Scalar {
data: scalar_sub_internal(a.data, b.data)
}
}
/// Subtracts 'b' from 'a' and assigns the result to 'a'.
/// Returns 'a'.
public fun scalar_sub_assign(a: &mut Scalar, b: &Scalar): &mut Scalar {
a.data = scalar_sub(a, b).data;
a
}
/// Returns the negation of 'a': i.e., $(0 - a) \mod \ell$.
public fun scalar_neg(a: &Scalar): Scalar {
Scalar {
data: scalar_neg_internal(a.data)
}
}
/// Replaces 'a' by its negation.
/// Returns 'a'.
public fun scalar_neg_assign(a: &mut Scalar): &mut Scalar {
a.data = scalar_neg(a).data;
a
}
/// Returns the byte-representation of the scalar.
public fun scalar_to_bytes(s: &Scalar): vector<u8> {
s.data
}
//
// Only used internally for implementing CompressedRistretto and RistrettoPoint
//
native fun new_point_from_sha512_internal(sha512: vector<u8>): u64;
native fun new_point_from_64_uniform_bytes_internal(bytes: vector<u8>): u64;
native fun point_is_canonical_internal(bytes: vector<u8>): bool;
native fun point_identity_internal(): u64;
native fun point_decompress_internal(maybe_non_canonical_bytes: vector<u8>): (u64, bool);
native fun point_compress_internal(point: &RistrettoPoint): vector<u8>;
native fun point_mul_internal(point: &RistrettoPoint, a: vector<u8>, in_place: bool): u64;
native fun basepoint_mul_internal(a: vector<u8>): u64;
native fun basepoint_double_mul_internal(a: vector<u8>, some_point: &RistrettoPoint, b: vector<u8>): u64;
native fun point_add_internal(a: &RistrettoPoint, b: &RistrettoPoint, in_place: bool): u64;
native fun point_sub_internal(a: &RistrettoPoint, b: &RistrettoPoint, in_place: bool): u64;
native fun point_neg_internal(a: &RistrettoPoint, in_place: bool): u64;
/// The generic arguments are needed to deal with some Move VM peculiarities which prevent us from borrowing the
/// points (or scalars) inside a &vector in Rust.
///
/// WARNING: This function can only be called with P = RistrettoPoint and S = Scalar.
native fun multi_scalar_mul_internal<P, S>(points: &vector<P>, scalars: &vector<S>): u64;
//
// Only used internally for implementing Scalar.
//
native fun scalar_is_canonical_internal(s: vector<u8>): bool;
native fun scalar_from_u64_internal(num: u64): vector<u8>;
native fun scalar_from_u128_internal(num: u128): vector<u8>;
native fun scalar_reduced_from_32_bytes_internal(bytes: vector<u8>): vector<u8>;
native fun scalar_uniform_from_64_bytes_internal(bytes: vector<u8>): vector<u8>;
native fun scalar_invert_internal(bytes: vector<u8>): vector<u8>;
native fun scalar_from_sha512_internal(sha512_input: vector<u8>): vector<u8>;
native fun scalar_mul_internal(a_bytes: vector<u8>, b_bytes: vector<u8>): vector<u8>;
native fun scalar_add_internal(a_bytes: vector<u8>, b_bytes: vector<u8>): vector<u8>;
native fun scalar_sub_internal(a_bytes: vector<u8>, b_bytes: vector<u8>): vector<u8>;
native fun scalar_neg_internal(a_bytes: vector<u8>): vector<u8>;
//
// Testing
//
// The scalar 2
const TWO_SCALAR: vector<u8> = x"0200000000000000000000000000000000000000000000000000000000000000";
// Non-canonical scalar: the order \ell of the group + 1
const L_PLUS_ONE: vector<u8> = x"eed3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010";
// Non-canonical scalar: the order \ell of the group + 2
const L_PLUS_TWO: vector<u8> = x"efd3f55c1a631258d69cf7a2def9de1400000000000000000000000000000010";
// Some random scalar denoted by X
const X_SCALAR: vector<u8> = x"4e5ab4345d4708845913b4641bc27d5252a585101bcc4244d449f4a879d9f204";
// X^{-1} = 1/X = 6859937278830797291664592131120606308688036382723378951768035303146619657244
// 0x1CDC17FCE0E9A5BBD9247E56BB016347BBBA31EDD5A9BB96D50BCD7A3F962A0F
const X_INV_SCALAR: vector<u8> = x"1cdc17fce0e9a5bbd9247e56bb016347bbba31edd5a9bb96d50bcd7a3f962a0f";
// Some random scalar Y = 2592331292931086675770238855846338635550719849568364935475441891787804997264
const Y_SCALAR: vector<u8> = x"907633fe1c4b66a4a28d2dd7678386c353d0de5455d4fc9de8ef7ac31f35bb05";
// X * Y = 5690045403673944803228348699031245560686958845067437804563560795922180092780
const X_TIMES_Y_SCALAR: vector<u8> = x"6c3374a1894f62210aaa2fe186a6f92ce0aa75c2779581c295fc08179a73940c";
// X + 2^256 * X \mod \ell
const REDUCED_X_PLUS_2_TO_256_TIMES_X_SCALAR: vector<u8> = x"d89ab38bd279024745639ed817ad3f64cc005b32db9939f91c521fc564a5c008";
// sage: l = 2^252 + 27742317777372353535851937790883648493
// sage: big = 2^256 - 1
// sage: repr((big % l).digits(256))
const REDUCED_2_256_MINUS_1_SCALAR: vector<u8> = x"1c95988d7431ecd670cf7d73f45befc6feffffffffffffffffffffffffffff0f";
const NON_CANONICAL_ALL_ONES: vector<u8> = x"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
const A_SCALAR: vector<u8> = x"1a0e978a90f6622d3747023f8ad8264da758aa1b88e040d1589e7b7f2376ef09";
// Generated in curve25519-dalek via:
// ```
// let mut hasher = Sha512::default();
// hasher.update(b"bello!");
// let s = Scalar::from_hash(hasher);
// println!("scalar: {:x?}", s.to_bytes());
// ```
const B_SCALAR: vector<u8> = x"dbfd97afd38a06f0138d0527efb28ead5b7109b486465913bf3aa472a8ed4e0d";
const A_TIMES_B_SCALAR: vector<u8> = x"2ab50e383d7c210f74d5387330735f18315112d10dfb98fcce1e2620c0c01402";
const A_PLUS_B_SCALAR: vector<u8> = x"083839dd491e57c5743710c39a91d6e502cab3cf0e279ae417d91ff2cb633e07";
/// A_SCALAR * BASE_POINT, computed by modifying a test in curve25519-dalek in src/edwards.rs to do:
/// ```
/// let comp = RistrettoPoint(A_TIMES_BASEPOINT.decompress().unwrap()).compress();
/// println!("hex: {:x?}", comp.to_bytes());
/// ```
const A_TIMES_BASE_POINT: vector<u8> = x"96d52d9262ee1e1aae79fbaee8c1d9068b0d01bf9a4579e618090c3d1088ae10";
const A_POINT: vector<u8> = x"e87feda199d72b83de4f5b2d45d34805c57019c6c59c42cb70ee3d19aa996f75";
const B_POINT: vector<u8> = x"fa0b3624b081c62f364d0b2839dcc76d7c3ab0e27e31beb2b9ed766575f28e76";
const A_PLUS_B_POINT: vector<u8> = x"70cf3753475b9ff33e2f84413ed6b5052073bccc0a0a81789d3e5675dc258056";
// const NON_CANONICAL_LARGEST_ED25519_S: vector<u8> = x"f8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f";
// const CANONICAL_LARGEST_ED25519_S_PLUS_ONE: vector<u8> = x"7e344775474a7f9723b63a8be92ae76dffffffffffffffffffffffffffffff0f";
// const CANONICAL_LARGEST_ED25519_S_MINUS_ONE: vector<u8> = x"7c344775474a7f9723b63a8be92ae76dffffffffffffffffffffffffffffff0f";
#[test]
fun test_point_decompression() {
let compressed = new_compressed_point_from_bytes(A_POINT);
assert!(std::option::is_some(&compressed), 1);
let point = new_point_from_bytes(A_POINT);
assert!(std::option::is_some(&point), 1);
let point = std::option::extract(&mut point);
let compressed = std::option::extract(&mut compressed);
let same_point = point_decompress(&compressed);
assert!(point_equals(&point, &same_point), 1);
}
#[test]
fun test_point_equals() {
let g = basepoint();
let same_g = std::option::extract(&mut new_point_from_bytes(BASE_POINT));
let ag = std::option::extract(&mut new_point_from_bytes(A_TIMES_BASE_POINT));
assert!(point_equals(&g, &same_g), 1);
assert!(!point_equals(&g, &ag), 1);
}
#[test]
fun test_point_mul() {
// fetch g
let g = basepoint();
// fetch a
let a = std::option::extract(&mut new_scalar_from_bytes(A_SCALAR));
// fetch expected a*g
let ag = std::option::extract(&mut new_point_from_bytes(A_TIMES_BASE_POINT));
// compute a*g
let p = point_mul(&g, &a);
// sanity-check the handles
assert!(g.handle == 0, 1);
assert!(ag.handle == 1, 1);
assert!(p.handle == 2, 1);
assert!(!point_equals(&g, &ag), 1); // make sure input g remains unmodifed
assert!(point_equals(&p, &ag), 1); // make sure output a*g is correct
}
#[test]
fun test_point_mul_assign() {
let g = basepoint();
assert!(g.handle == 0, 1);
let a = std::option::extract(&mut new_scalar_from_bytes(A_SCALAR));
let ag = std::option::extract(&mut new_point_from_bytes(A_TIMES_BASE_POINT));
assert!(ag.handle == 1, 1);
assert!(!point_equals(&g, &ag), 1);
{
// NOTE: new_g is just a mutable reference to g
let upd_g = point_mul_assign(&mut g, &a);
// in a mul_assign the returned &mut RistrettoPoint reference should have the same handle as 'g'
assert!(upd_g.handle == 0, 1);
assert!(point_equals(upd_g, &ag), 1);
};
assert!(point_equals(&g, &ag), 1);
}
#[test]
fun test_point_add() {
// fetch a
let a = std::option::extract(&mut new_point_from_bytes(A_POINT));
// fetch b
let b = std::option::extract(&mut new_point_from_bytes(B_POINT));
// fetch expected a + b
let a_plus_b = std::option::extract(&mut new_point_from_bytes(A_PLUS_B_POINT));
// compute a*g
let result = point_add(&a, &b);
assert!(!point_equals(&a, &b), 1);
// sanity-check the handles
assert!(a.handle == 0, 1);
assert!(b.handle == 1, 1);
assert!(a_plus_b.handle == 2, 1);
assert!(result.handle == 3, 1);
assert!(!point_equals(&a, &result), 1); // make sure input a remains unmodifed
assert!(!point_equals(&b, &result), 1); // make sure input b remains unmodifed
assert!(point_equals(&a_plus_b, &result), 1); // make sure output a+b is correct
}
#[test]
fun test_point_add_assign_0_0() {
test_point_add_assign_internal(0, 0);
}
#[test]
fun test_point_add_assign_1_0() {
test_point_add_assign_internal(1, 0);
}
#[test]
fun test_point_add_assign_0_1() {
test_point_add_assign_internal(0, 1);
}
#[test]
fun test_point_add_assign_3_7() {
test_point_add_assign_internal(3, 7);
}
#[test_only]
fun test_point_add_assign_internal(before_a_gap: u64, before_b_gap: u64) {
// create extra RistrettoPoints here, so as to generate different PointStore layouts inside the native Rust implementation
let c = before_a_gap;
while (c > 0) {
let _ignore = std::option::extract(&mut new_point_from_bytes(BASE_POINT));
c = c - 1;
};
// fetch a
let a = std::option::extract(&mut new_point_from_bytes(A_POINT));
// create extra RistrettoPoints here, so as to generate different PointStore layouts inside the native Rust implementation
let c = before_b_gap;
while (c > 0) {
let _ignore = std::option::extract(&mut new_point_from_bytes(BASE_POINT));
c = c - 1;
};
// fetch b
let b = std::option::extract(&mut new_point_from_bytes(B_POINT));
let a_plus_b = std::option::extract(&mut new_point_from_bytes(A_PLUS_B_POINT));
// sanity-check the handles
assert!(a.handle == before_a_gap, 1);
assert!(b.handle == 1 + before_a_gap + before_b_gap, 1);
assert!(a_plus_b.handle == 2 + before_a_gap + before_b_gap, 1);
assert!(!point_equals(&a, &b), 1);
assert!(!point_equals(&a, &a_plus_b), 1);
{
// NOTE: new_h is just a mutable reference to g
let upd_a = point_add_assign(&mut a, &b);
// in a add_assign the returned &mut RistrettoPoint reference should have the same handle as 'a'
assert!(upd_a.handle == before_a_gap, 1);
assert!(point_equals(upd_a, &a_plus_b), 1);
};
assert!(point_equals(&a, &a_plus_b), 1);
}
#[test]
fun test_point_sub() {
// fetch a
let a = std::option::extract(&mut new_point_from_bytes(A_POINT));
// fetch b
let b = std::option::extract(&mut new_point_from_bytes(B_POINT));
// fetch expected a + b
let a_plus_b = std::option::extract(&mut new_point_from_bytes(A_PLUS_B_POINT));
// compute a*g
let result = point_sub(&a_plus_b, &b);
assert!(!point_equals(&a, &b), 1);
// sanity-check the handles
assert!(a.handle == 0, 1);
assert!(b.handle == 1, 1);
assert!(a_plus_b.handle == 2, 1);
assert!(result.handle == 3, 1);
assert!(!point_equals(&a_plus_b, &result), 1); // make sure input a_plus_b remains unmodifed
assert!(!point_equals(&b, &result), 1); // make sure input b remains unmodifed
assert!(point_equals(&a, &result), 1); // make sure output 'a+b-b' is correct
}
#[test]
fun test_point_neg() {
let a = std::option::extract(&mut new_point_from_bytes(A_POINT));
let neg_a = point_neg(&a);
assert!(a.handle != neg_a.handle, 1);
assert!(!point_equals(&a, &neg_a), 1);
assert!(!point_equals(&point_add(&point_identity(), &a), &neg_a), 1);
assert!(point_equals(&point_add(&a, &neg_a), &point_identity()), 1);
let handle = a.handle;
let neg_a_ref = point_neg_assign(&mut a);
assert!(handle == neg_a_ref.handle, 1);
assert!(point_equals(neg_a_ref, &neg_a), 1);
}
#[test]
fun test_basepoint_mul() {
let a = Scalar { data: A_SCALAR };
let basepoint = basepoint();
let expected = point_mul(&basepoint, &a);
assert!(point_equals(&expected, &basepoint_mul(&a)), 1);
}
#[test]
fun test_basepoint_double_mul() {
let expected = option::extract(&mut new_point_from_bytes(x"be5d615d8b8f996723cdc6e1895b8b6d312cc75d1ffb0259873b99396a38c05a"));
let a = Scalar { data: A_SCALAR };
let a_point = option::extract(&mut new_point_from_bytes(A_POINT));
let b = Scalar { data: B_SCALAR };
assert!(point_equals(&expected, &basepoint_double_mul(&a, &a_point, &b)), 1);
}
#[test]
#[expected_failure]
fun test_multi_scalar_mul_aborts_empty_scalars() {
multi_scalar_mul(&vector[ basepoint() ], &vector[]);
}
#[test]
#[expected_failure]
fun test_multi_scalar_mul_aborts_empty_points() {
multi_scalar_mul(&vector[ ], &vector[ Scalar { data: A_SCALAR } ]);
}
#[test]
#[expected_failure]
fun test_multi_scalar_mul_aborts_empty_all() {
multi_scalar_mul(&vector[ ], &vector[ ]);
}
#[test]
#[expected_failure]
fun test_multi_scalar_mul_aborts_different_sizes() {
multi_scalar_mul(&vector[ basepoint() ], &vector[ Scalar { data: A_SCALAR }, Scalar { data: B_SCALAR } ]);
}
#[test]
fun test_multi_scalar_mul_single() {
// Test single exp
let points = vector[
basepoint(),
];
let scalars = vector[
Scalar { data: A_SCALAR },
];
let result = multi_scalar_mul(&points, &scalars);
let expected = std::option::extract(&mut new_point_from_bytes(A_TIMES_BASE_POINT));
assert!(point_equals(&result, &expected), 1);
}
#[test]
fun test_multi_scalar_mul_double() {
// Test double exp
let points = vector[
basepoint(),
basepoint(),
];
let scalars = vector[
Scalar { data: A_SCALAR },
Scalar { data: B_SCALAR },
];
let result = multi_scalar_mul(&points, &scalars);
let expected = basepoint_double_mul(
std::vector::borrow(&scalars, 0),
&basepoint(),
std::vector::borrow(&scalars, 1));
assert!(point_equals(&result, &expected), 1);
}
#[test]
fun test_multi_scalar_mul_many() {
let scalars = vector[
new_scalar_from_sha512(b"1"),
new_scalar_from_sha512(b"2"),
new_scalar_from_sha512(b"3"),
new_scalar_from_sha512(b"4"),
new_scalar_from_sha512(b"5"),
];
let points = vector[
new_point_from_sha512(b"1"),
new_point_from_sha512(b"2"),
new_point_from_sha512(b"3"),
new_point_from_sha512(b"4"),
new_point_from_sha512(b"5"),
];
let expected = std::option::extract(&mut new_point_from_bytes(x"c4a98fbe6bd0f315a0c150858aec8508be397443093e955ef982e299c1318928"));
let result = multi_scalar_mul(&points, &scalars);
assert!(point_equals(&expected, &result), 1);
}
#[test]
fun test_new_point_from_sha512() {
let msg = b"To really appreciate architecture, you may even need to commit a murder";
let expected = option::extract(&mut new_point_from_bytes(x"baaa91eb43e5e2f12ffc96347e14bc458fdb1772b2232b08977ee61ea9f84e31"));
assert!(point_equals(&expected, &new_point_from_sha512(msg)), 1);
}
#[test]
fun test_new_point_from_64_uniform_bytes() {
let bytes_64 = x"baaa91eb43e5e2f12ffc96347e14bc458fdb1772b2232b08977ee61ea9f84e31e87feda199d72b83de4f5b2d45d34805c57019c6c59c42cb70ee3d19aa996f75";
let expected = option::extract(&mut new_point_from_bytes(x"4a8e429f906478654232d7ae180ad60854754944ac67f38e20d8fa79e4b7d71e"));
let point = option::extract(&mut new_point_from_64_uniform_bytes(bytes_64));
assert!(point_equals(&expected, &point), 1);
}
#[test]
fun test_scalar_basic_viability() {
// Test conversion from u8
let two = Scalar { data: TWO_SCALAR };
assert!(scalar_equals(&new_scalar_from_u8(2u8), &two), 1);
// Test conversion from u64
assert!(scalar_equals(&new_scalar_from_u64(2u64), &two), 1);
// Test conversion from u128
assert!(scalar_equals(&new_scalar_from_u128(2u128), &two), 1);
// Test (0 - 1) % order = order - 1
assert!(scalar_equals(&scalar_sub(&scalar_zero(), &scalar_one()), &Scalar { data: L_MINUS_ONE }), 1);
}
#[test]
/// Tests deserializing a Scalar from a sequence of canonical bytes
fun test_scalar_from_canonical_bytes() {
// Too few bytes
assert!(std::option::is_none(&new_scalar_from_bytes(x"00")), 1);
// 32 zero bytes are canonical
assert!(std::option::is_some(&new_scalar_from_bytes(x"0000000000000000000000000000000000000000000000000000000000000000")), 1);
// Non-canonical because unreduced
assert!(std::option::is_none(&new_scalar_from_bytes(x"1010101010101010101010101010101010101010101010101010101010101010")), 1);
// Canonical because \ell - 1
assert!(std::option::is_some(&new_scalar_from_bytes(L_MINUS_ONE)), 1);
// Non-canonical because \ell
assert!(std::option::is_none(&new_scalar_from_bytes(ORDER_ELL)), 1);
// Non-canonical because \ell+1
assert!(std::option::is_none(&new_scalar_from_bytes(L_PLUS_ONE)), 1);
// Non-canonical because \ell+2
assert!(std::option::is_none(&new_scalar_from_bytes(L_PLUS_TWO)), 1);
// Non-canonical because high bit is set
let non_canonical_highbit = vector[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
let non_canonical_highbit_hex = x"0000000000000000000000000000000000000000000000000000000000000080";
assert!(non_canonical_highbit == non_canonical_highbit_hex, 1);
assert!(std::option::is_none(&new_scalar_from_bytes(non_canonical_highbit)), 1);
}
#[test]
fun test_scalar_zero() {
// 0 == 0
assert!(scalar_is_zero(&scalar_zero()), 1);
assert!(scalar_is_zero(&new_scalar_from_u8(0u8)), 1);
// 0 != 1
assert!(scalar_is_zero(&scalar_one()) == false, 1);
// Pick a random scalar by hashing from some "random" bytes
let s = new_scalar_from_sha512(x"deadbeef");
// Technically, there is a negligible probability (i.e., 1/2^\ell) that the hashed s is zero or one
assert!(scalar_is_zero(&s) == false, 1);
assert!(scalar_is_one(&s) == false, 1);
// Multiply 0 with a random scalar and make sure you get zero
assert!(scalar_is_zero(&scalar_mul(&scalar_zero(), &s)), 1);
assert!(scalar_is_zero(&scalar_mul(&s, &scalar_zero())), 1);
}
#[test]
fun test_scalar_one() {
// 1 == 1
assert!(scalar_is_one(&scalar_one()), 1);
assert!(scalar_is_one(&new_scalar_from_u8(1u8)), 1);
// 1 != 0
assert!(scalar_is_one(&scalar_zero()) == false, 1);
// Pick a random scalar by hashing from some "random" bytes
let s = new_scalar_from_sha512(x"deadbeef");
let inv = scalar_invert(&s);
// Technically, there is a negligible probability (i.e., 1/2^\ell) that s was zero and the call above returned None
assert!(std::option::is_some(&inv), 1);
let inv = std::option::extract(&mut inv);
// Multiply s with s^{-1} and make sure you get one
assert!(scalar_is_one(&scalar_mul(&s, &inv)), 1);