-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathelliptic_curve.move
120 lines (96 loc) · 4.44 KB
/
elliptic_curve.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
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
/// Library for Elliptic Curve operations on chain. We specifically support the Ristretto-255 sub-group.
module sui::elliptic_curve {
use std::vector;
///////////////////////////////////
/// Elliptic Curve structs
///////////////////////////////////
/// Represents a point on the Ristretto-255 subgroup.
struct RistrettoPoint has copy, drop, store {
// A 32-byte representation of the group element.
value: vector<u8>
}
/// Represents a scalar within the Curve25519 prime-order group.
struct Scalar has copy, drop, store {
// A 32-byte representation of the scalar
value: vector<u8>
}
///////////////////////////////////
/// Private
///////////////////////////////////
/// @param value: The value to commit to
/// @param blinding_factor: A random number used to ensure that the commitment is hiding.
native fun native_create_pedersen_commitment(value: vector<u8>, blinding_factor: vector<u8>): vector<u8>;
/// @param self: bytes representation of an EC point on the Ristretto-255 subgroup
/// @param other: bytes representation of an EC point on the Ristretto-255 subgroup
/// A native move wrapper around the addition of Ristretto points. Returns self + other.
native fun native_add_ristretto_point(point1: vector<u8>, point2: vector<u8>): vector<u8>;
/// @param self: bytes representation of an EC point on the Ristretto-255 subgroup
/// @param other: bytes representation of an EC point on the Ristretto-255 subgroup
/// A native move wrapper around the subtraction of Ristretto points. Returns self - other.
native fun native_subtract_ristretto_point(point1: vector<u8>, point2: vector<u8>): vector<u8>;
/// @param value: the value of the to-be-created scalar
/// TODO: Transfer this into a Move function some time in the future.
/// A native move wrapper for the creation of Scalars on Curve25519.
native fun native_scalar_from_u64(value: u64): vector<u8>;
/// @param value: the bytes representation of the scalar.
/// TODO: Transfer this into a Move function some time in the future.
/// A native move wrapper for the creation of Scalars on Curve25519.
native fun native_scalar_from_bytes(bytes: vector<u8>): vector<u8>;
///////////////////////////////////
/// Public
///////////////////////////////////
// Scalar
///////////////////////
/// Create a field element from u64
public fun new_scalar_from_u64(value: u64): Scalar {
Scalar {
value: native_scalar_from_u64(value)
}
}
/// Create a pedersen commitment from two field elements
public fun create_pedersen_commitment(value: Scalar, blinding_factor: Scalar): RistrettoPoint {
return RistrettoPoint {
value: native_create_pedersen_commitment(value.value, blinding_factor.value)
}
}
/// Creates a new field element from byte representation. Note that
/// `value` must be 32-bytes
public fun new_scalar_from_bytes(value: vector<u8>): Scalar {
Scalar {
value: native_scalar_from_bytes(value)
}
}
/// Get the byte representation of the field element
public fun scalar_bytes(self: &Scalar): vector<u8> {
self.value
}
// EC Point
///////////////////////
/// Get the underlying compressed byte representation of the group element
public fun bytes(self: &RistrettoPoint): vector<u8> {
self.value
}
/// Perform addition on two group elements
public fun add(self: &RistrettoPoint, other: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
value: native_add_ristretto_point(self.value, other.value)
}
}
/// Perform subtraction on two group elements
public fun subtract(self: &RistrettoPoint, other: &RistrettoPoint): RistrettoPoint {
RistrettoPoint {
value: native_subtract_ristretto_point(self.value, other.value)
}
}
/// Attempt to create a new group element from compressed bytes representation
public fun new_from_bytes(bytes: vector<u8>): RistrettoPoint {
assert!(vector::length(&bytes) == 32, 1);
RistrettoPoint {
value: bytes
}
}
// TODO: Add arithmetic for Scalar elements. We just need add, subtract, and multiply.
// TODO: Add scalar to point multiplication for group elements.
}