5
5
use cfg_if:: cfg_if;
6
6
7
7
cfg_if ! {
8
- if #[ cfg( target_pointer_width = "32" ) ] {
8
+
9
+ if #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ] {
10
+ mod field_8x32;
11
+ } else if #[ cfg( target_pointer_width = "32" ) ] {
9
12
mod field_10x26;
10
13
} else if #[ cfg( target_pointer_width = "64" ) ] {
11
14
mod field_5x52;
@@ -20,7 +23,9 @@ cfg_if! {
20
23
use field_impl:: FieldElementImpl ;
21
24
} else {
22
25
cfg_if! {
23
- if #[ cfg( target_pointer_width = "32" ) ] {
26
+ if #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ] {
27
+ use field_8x32:: FieldElement8x32 as FieldElementImpl ;
28
+ } else if #[ cfg( target_pointer_width = "32" ) ] {
24
29
use field_10x26:: FieldElement10x26 as FieldElementImpl ;
25
30
} else if #[ cfg( target_pointer_width = "64" ) ] {
26
31
use field_5x52:: FieldElement5x52 as FieldElementImpl ;
@@ -99,11 +104,31 @@ impl FieldElement {
99
104
FieldElementImpl :: from_bytes ( bytes) . map ( Self )
100
105
}
101
106
107
+ /// Attempts to parse the given byte array as an SEC1-encoded field element (in little-endian!).
108
+ /// Does not check the result for being in the correct range.
109
+ #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ]
110
+ pub ( crate ) fn from_bytes_unchecked_le ( bytes : & [ u8 ; 32 ] ) -> Self {
111
+ Self ( FieldElementImpl :: from_bytes_unchecked_le ( bytes) )
112
+ }
113
+
102
114
/// Convert a `u64` to a field element.
103
115
pub const fn from_u64 ( w : u64 ) -> Self {
104
116
Self ( FieldElementImpl :: from_u64 ( w) )
105
117
}
106
118
119
+ /// Returns the SEC1 encoding (in little-endian!) of this field element.
120
+ #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ]
121
+ pub fn to_bytes_le ( self ) -> FieldBytes {
122
+ self . 0 . normalize ( ) . to_bytes_le ( )
123
+ }
124
+
125
+ /// Convert a `i64` to a field element.
126
+ /// Returned value may be only weakly normalized.
127
+ #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ]
128
+ pub const fn from_i64 ( w : i64 ) -> Self {
129
+ Self ( FieldElementImpl :: from_i64 ( w) )
130
+ }
131
+
107
132
/// Returns the SEC1 encoding of this field element.
108
133
pub fn to_bytes ( self ) -> FieldBytes {
109
134
self . 0 . normalize ( ) . to_bytes ( )
@@ -140,6 +165,14 @@ impl FieldElement {
140
165
141
166
/// Returns 2*self.
142
167
/// Doubles the magnitude.
168
+ #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ]
169
+ pub fn double ( & self ) -> Self {
170
+ self . mul_single ( 2 )
171
+ }
172
+
173
+ /// Returns 2*self.
174
+ /// Doubles the magnitude.
175
+ #[ cfg( not( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ) ]
143
176
pub fn double ( & self ) -> Self {
144
177
Self ( self . 0 . add ( & ( self . 0 ) ) )
145
178
}
@@ -361,6 +394,13 @@ impl From<u64> for FieldElement {
361
394
}
362
395
}
363
396
397
+ #[ cfg( all( target_os = "zkvm" , target_arch = "riscv32" ) ) ]
398
+ impl From < i64 > for FieldElement {
399
+ fn from ( k : i64 ) -> Self {
400
+ Self ( FieldElementImpl :: from_i64 ( k) )
401
+ }
402
+ }
403
+
364
404
impl PartialEq for FieldElement {
365
405
fn eq ( & self , other : & Self ) -> bool {
366
406
self . 0 . ct_eq ( & ( other. 0 ) ) . into ( )
0 commit comments