Skip to content

Commit 394746b

Browse files
committed
doc comments added for jubjub wrapper
1 parent c93a51a commit 394746b

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

mithril-stm/src/signature_scheme/schnorr_signature/jubjub/curve_points.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,59 @@ use std::ops::{Add, Mul};
99
use super::{BaseFieldElement, ScalarFieldElement};
1010
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
1111

12+
/// Represents a point in affine coordinates on the Jubjub curve
1213
#[derive(Clone)]
1314
pub(crate) struct AffinePoint(JubjubAffinePoint);
1415

1516
impl AffinePoint {
17+
/// Converts a projective point to its affine representation
1618
pub(crate) fn from_projective_point(projective_point: ProjectivePoint) -> Self {
1719
AffinePoint(JubjubAffinePoint::from(projective_point.0))
1820
}
1921

22+
/// Retrieves the u-coordinate of the affine point
2023
pub(crate) fn get_u(&self) -> BaseFieldElement {
2124
BaseFieldElement(self.0.get_u())
2225
}
2326

27+
/// Retrieves the v-coordinate of the affine point
2428
pub(crate) fn get_v(&self) -> BaseFieldElement {
2529
BaseFieldElement(self.0.get_v())
2630
}
2731
}
2832

2933
impl From<&PrimeOrderProjectivePoint> for AffinePoint {
34+
/// Converts a prime order projective point to its affine representation
3035
fn from(prime_order_projective_point: &PrimeOrderProjectivePoint) -> Self {
3136
AffinePoint(JubjubAffinePoint::from(JubjubExtended::from(
3237
prime_order_projective_point.0,
3338
)))
3439
}
3540
}
3641

42+
/// Represents a point in projective coordinates on the Jubjub curve
3743
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3844
pub(crate) struct ProjectivePoint(pub(crate) JubjubExtended);
3945

4046
impl ProjectivePoint {
47+
/// Hashes input bytes to a projective point on the Jubjub curve
4148
pub(crate) fn hash_to_projective_point(input: &[u8]) -> Self {
4249
ProjectivePoint(JubjubExtended::hash_to_point(input))
4350
}
4451

52+
/// Retrieves the (u, v) coordinates of the projective point in affine representation
4553
pub(crate) fn get_coordinates(&self) -> (BaseFieldElement, BaseFieldElement) {
4654
let affine_point = AffinePoint::from_projective_point(*self);
4755

4856
(affine_point.get_u(), affine_point.get_v())
4957
}
5058

59+
/// Converts the projective point to its byte representation
5160
pub(crate) fn to_bytes(self) -> [u8; 32] {
5261
self.0.to_bytes()
5362
}
5463

64+
/// Constructs a projective point from its byte representation
5565
pub(crate) fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
5666
let mut projective_point_bytes = [0u8; 32];
5767
projective_point_bytes
@@ -63,6 +73,7 @@ impl ProjectivePoint {
6373
}
6474
}
6575

76+
/// Checks if the projective point is of prime order
6677
pub(crate) fn is_prime_order(self) -> bool {
6778
self.0.is_prime_order().into()
6879
}
@@ -71,6 +82,7 @@ impl ProjectivePoint {
7182
impl Add for ProjectivePoint {
7283
type Output = ProjectivePoint;
7384

85+
/// Adds two projective points
7486
fn add(self, other: ProjectivePoint) -> ProjectivePoint {
7587
ProjectivePoint(self.0 + other.0)
7688
}
@@ -79,26 +91,31 @@ impl Add for ProjectivePoint {
7991
impl Mul<ProjectivePoint> for ScalarFieldElement {
8092
type Output = ProjectivePoint;
8193

94+
/// Multiplies a projective point by a scalar field element
95+
/// Returns the resulting projective point
8296
fn mul(self, point: ProjectivePoint) -> ProjectivePoint {
8397
ProjectivePoint(point.0 * self.0)
8498
}
8599
}
86100

87101
impl From<PrimeOrderProjectivePoint> for ProjectivePoint {
102+
/// Converts a prime order projective point to a projective point
88103
fn from(prime_order_projective_point: PrimeOrderProjectivePoint) -> Self {
89104
ProjectivePoint(JubjubExtended::from(prime_order_projective_point.0))
90105
}
91106
}
92107

108+
/// Represents a point of prime order in projective coordinates on the Jubjub curve
93109
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
94110
pub(crate) struct PrimeOrderProjectivePoint(pub(crate) JubjubSubgroup);
95111

96112
impl PrimeOrderProjectivePoint {
113+
/// Creates the generator point of the prime order subgroup
97114
pub(crate) fn create_generator() -> Self {
98115
PrimeOrderProjectivePoint(JubjubSubgroup::generator())
99116
}
100117

101-
/// Check if the given point is on the curve using its coordinates
118+
/// Checks if the given point is on the curve using its coordinates
102119
pub(crate) fn is_on_curve(&self) -> StmResult<PrimeOrderProjectivePoint> {
103120
let point_affine_representation = AffinePoint::from(self);
104121
let (x, y) = (
@@ -119,10 +136,12 @@ impl PrimeOrderProjectivePoint {
119136
Ok(*self)
120137
}
121138

139+
/// Converts the prime order projective point to its byte representation
122140
pub(crate) fn to_bytes(self) -> [u8; 32] {
123141
self.0.to_bytes()
124142
}
125143

144+
/// Constructs a prime order projective point from its byte representation
126145
pub(crate) fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
127146
let mut prime_order_projective_point_bytes = [0u8; 32];
128147
prime_order_projective_point_bytes
@@ -140,6 +159,7 @@ impl PrimeOrderProjectivePoint {
140159
impl Add for PrimeOrderProjectivePoint {
141160
type Output = PrimeOrderProjectivePoint;
142161

162+
/// Adds two prime order projective points
143163
fn add(self, other: PrimeOrderProjectivePoint) -> PrimeOrderProjectivePoint {
144164
PrimeOrderProjectivePoint(self.0 + other.0)
145165
}
@@ -148,6 +168,8 @@ impl Add for PrimeOrderProjectivePoint {
148168
impl Mul<PrimeOrderProjectivePoint> for ScalarFieldElement {
149169
type Output = PrimeOrderProjectivePoint;
150170

171+
/// Multiplies a prime order projective point by a scalar field element
172+
/// Returns the resulting prime order projective point
151173
fn mul(self, point: PrimeOrderProjectivePoint) -> PrimeOrderProjectivePoint {
152174
PrimeOrderProjectivePoint(point.0 * self.0)
153175
}

mithril-stm/src/signature_scheme/schnorr_signature/jubjub/field_elements.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use std::ops::{Add, Mul, Sub};
66

77
use crate::{StmResult, signature_scheme::SchnorrSignatureError};
88

9+
/// Represents an element in the base field of the Jubjub curve
910
#[derive(Debug, Clone, PartialEq, Eq)]
1011
pub(crate) struct BaseFieldElement(pub(crate) JubjubBase);
1112

1213
impl BaseFieldElement {
14+
/// Retrieves the multiplicative identity element of the base field
1315
pub(crate) fn get_one() -> Self {
1416
BaseFieldElement(JubjubBase::ONE)
1517
}
@@ -18,6 +20,7 @@ impl BaseFieldElement {
1820
impl Add for BaseFieldElement {
1921
type Output = BaseFieldElement;
2022

23+
/// Adds two base field elements
2124
fn add(self, other: BaseFieldElement) -> BaseFieldElement {
2225
BaseFieldElement(self.0 + other.0)
2326
}
@@ -26,6 +29,7 @@ impl Add for BaseFieldElement {
2629
impl Sub for &BaseFieldElement {
2730
type Output = BaseFieldElement;
2831

32+
/// Subtracts one base field element from another
2933
fn sub(self, other: &BaseFieldElement) -> BaseFieldElement {
3034
BaseFieldElement(self.0 - other.0)
3135
}
@@ -34,6 +38,7 @@ impl Sub for &BaseFieldElement {
3438
impl Mul for BaseFieldElement {
3539
type Output = BaseFieldElement;
3640

41+
/// Multiplies two base field elements
3742
fn mul(self, other: BaseFieldElement) -> BaseFieldElement {
3843
BaseFieldElement(self.0 * other.0)
3944
}
@@ -42,33 +47,40 @@ impl Mul for BaseFieldElement {
4247
impl Mul for &BaseFieldElement {
4348
type Output = BaseFieldElement;
4449

50+
/// Multiplies a base field element by another base field element
4551
fn mul(self, other: &BaseFieldElement) -> BaseFieldElement {
4652
BaseFieldElement(self.0 * other.0)
4753
}
4854
}
4955

56+
/// Represents an element in the scalar field of the Jubjub curve
5057
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5158
pub(crate) struct ScalarFieldElement(pub(crate) JubjubScalar);
5259

5360
impl ScalarFieldElement {
61+
/// Generates a new random scalar field element
5462
pub(crate) fn new_random_scalar(rng: &mut (impl RngCore + CryptoRng)) -> Self {
5563
ScalarFieldElement(JubjubScalar::random(rng))
5664
}
5765

66+
/// Checks if the scalar field element is zero
5867
pub(crate) fn is_zero(&self) -> bool {
5968
if self.0 == JubjubScalar::zero() {
6069
return true;
6170
}
6271
false
6372
}
6473

74+
/// Checks if the scalar field element is one
6575
pub(crate) fn is_one(&self) -> bool {
6676
if self.0 == JubjubScalar::one() {
6777
return true;
6878
}
6979
false
7080
}
7181

82+
/// Generates a new random non-zero scalar field element
83+
/// Returns an error if unable to generate a non-zero scalar after 100 attempts
7284
pub(crate) fn new_random_nonzero_scalar(
7385
rng: &mut (impl RngCore + CryptoRng),
7486
) -> StmResult<Self> {
@@ -81,10 +93,12 @@ impl ScalarFieldElement {
8193
Err(anyhow!(SchnorrSignatureError::RandomScalarGeneration))
8294
}
8395

96+
/// Converts the scalar field element to its byte representation
8497
pub(crate) fn to_bytes(self) -> [u8; 32] {
8598
self.0.to_bytes()
8699
}
87100

101+
/// Constructs a scalar field element from its byte representation
88102
pub(crate) fn from_bytes(bytes: &[u8]) -> StmResult<Self> {
89103
let mut scalar_bytes = [0u8; 32];
90104
scalar_bytes.copy_from_slice(
@@ -105,6 +119,7 @@ impl ScalarFieldElement {
105119
impl Mul for ScalarFieldElement {
106120
type Output = ScalarFieldElement;
107121

122+
/// Multiplies two scalar field elements
108123
fn mul(self, other: ScalarFieldElement) -> ScalarFieldElement {
109124
ScalarFieldElement(self.0 * other.0)
110125
}
@@ -113,6 +128,7 @@ impl Mul for ScalarFieldElement {
113128
impl Sub for ScalarFieldElement {
114129
type Output = ScalarFieldElement;
115130

131+
/// Subtracts one scalar field element from another
116132
fn sub(self, other: ScalarFieldElement) -> ScalarFieldElement {
117133
ScalarFieldElement(self.0 - other.0)
118134
}

mithril-stm/src/signature_scheme/schnorr_signature/jubjub/poseidon_digest.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use super::{BaseFieldElement, ScalarFieldElement};
66
/// A DST (Domain Separation Tag) to distinguish between use of Poseidon hash
77
const DST_SIGNATURE: JubjubBase = JubjubBase::from_raw([0u64, 0, 0, 0]);
88

9+
/// Computes a truncated Poseidon digest over the provided base field elements
10+
/// Returns a scalar field element as the digest
911
pub(crate) fn compute_truncated_digest(input: &[BaseFieldElement]) -> ScalarFieldElement {
1012
let mut poseidon_input = vec![DST_SIGNATURE];
1113
poseidon_input.extend(input.iter().map(|i| i.0).collect::<Vec<JubjubBase>>());

0 commit comments

Comments
 (0)