| 1 |
// Autogenerated: 'src/ExtractionOCaml/unsaturated_solinas' --lang Zig --internal-static --public-function-case camelCase --private-function-case camelCase --public-type-case UpperCamelCase --private-type-case UpperCamelCase --no-prefix-fiat --package-name 25519 '' 64 '(auto)' '2^255 - 19' carry_mul carry_square carry add sub opp selectznz to_bytes from_bytes relax carry_scmul121666 |
| 2 |
// curve description (via package name): 25519 |
| 3 |
// machine_wordsize = 64 (from "64") |
| 4 |
// requested operations: carry_mul, carry_square, carry, add, sub, opp, selectznz, to_bytes, from_bytes, relax, carry_scmul121666 |
| 5 |
// n = 5 (from "(auto)") |
| 6 |
// s-c = 2^255 - [(1, 19)] (from "2^255 - 19") |
| 7 |
// tight_bounds_multiplier = 1 (from "") |
| 8 |
// |
| 9 |
// Computed values: |
| 10 |
// carry_chain = [0, 1, 2, 3, 4, 0, 1] |
| 11 |
// eval z = z[0] + (z[1] << 51) + (z[2] << 102) + (z[3] << 153) + (z[4] << 204) |
| 12 |
// bytes_eval z = z[0] + (z[1] << 8) + (z[2] << 16) + (z[3] << 24) + (z[4] << 32) + (z[5] << 40) + (z[6] << 48) + (z[7] << 56) + (z[8] << 64) + (z[9] << 72) + (z[10] << 80) + (z[11] << 88) + (z[12] << 96) + (z[13] << 104) + (z[14] << 112) + (z[15] << 120) + (z[16] << 128) + (z[17] << 136) + (z[18] << 144) + (z[19] << 152) + (z[20] << 160) + (z[21] << 168) + (z[22] << 176) + (z[23] << 184) + (z[24] << 192) + (z[25] << 200) + (z[26] << 208) + (z[27] << 216) + (z[28] << 224) + (z[29] << 232) + (z[30] << 240) + (z[31] << 248) |
| 13 |
// balance = [0xfffffffffffda, 0xffffffffffffe, 0xffffffffffffe, 0xffffffffffffe, 0xffffffffffffe] |
| 14 |
|
| 15 |
const std = @import("std"); |
| 16 |
const mode = @import("builtin").mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels |
| 17 |
|
| 18 |
inline fn cast(comptime DestType: type, target: anytype) DestType { |
| 19 |
@setEvalBranchQuota(10000); |
| 20 |
if (@typeInfo(@TypeOf(target)) == .int) { |
| 21 |
const dest = @typeInfo(DestType).int; |
| 22 |
const source = @typeInfo(@TypeOf(target)).int; |
| 23 |
if (dest.bits < source.bits) { |
| 24 |
const T = std.meta.Int(source.signedness, dest.bits); |
| 25 |
return @bitCast(@as(T, @truncate(target))); |
| 26 |
} |
| 27 |
} |
| 28 |
return target; |
| 29 |
} |
| 30 |
|
| 31 |
// The type LooseFieldElement is a field element with loose bounds. |
| 32 |
// Bounds: [[0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000]] |
| 33 |
pub const LooseFieldElement = [5]u64; |
| 34 |
|
| 35 |
// The type TightFieldElement is a field element with tight bounds. |
| 36 |
// Bounds: [[0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000]] |
| 37 |
pub const TightFieldElement = [5]u64; |
| 38 |
|
| 39 |
/// The function addcarryxU51 is an addition with carry. |
| 40 |
/// |
| 41 |
/// Postconditions: |
| 42 |
/// out1 = (arg1 + arg2 + arg3) mod 2^51 |
| 43 |
/// out2 = ⌊(arg1 + arg2 + arg3) / 2^51⌋ |
| 44 |
/// |
| 45 |
/// Input Bounds: |
| 46 |
/// arg1: [0x0 ~> 0x1] |
| 47 |
/// arg2: [0x0 ~> 0x7ffffffffffff] |
| 48 |
/// arg3: [0x0 ~> 0x7ffffffffffff] |
| 49 |
/// Output Bounds: |
| 50 |
/// out1: [0x0 ~> 0x7ffffffffffff] |
| 51 |
/// out2: [0x0 ~> 0x1] |
| 52 |
inline fn addcarryxU51(out1: *u64, out2: *u1, arg1: u1, arg2: u64, arg3: u64) void { |
| 53 |
@setRuntimeSafety(mode == .Debug); |
| 54 |
|
| 55 |
const x1 = ((cast(u64, arg1) + arg2) + arg3); |
| 56 |
const x2 = (x1 & 0x7ffffffffffff); |
| 57 |
const x3 = cast(u1, (x1 >> 51)); |
| 58 |
out1.* = x2; |
| 59 |
out2.* = x3; |
| 60 |
} |
| 61 |
|
| 62 |
/// The function subborrowxU51 is a subtraction with borrow. |
| 63 |
/// |
| 64 |
/// Postconditions: |
| 65 |
/// out1 = (-arg1 + arg2 + -arg3) mod 2^51 |
| 66 |
/// out2 = -⌊(-arg1 + arg2 + -arg3) / 2^51⌋ |
| 67 |
/// |
| 68 |
/// Input Bounds: |
| 69 |
/// arg1: [0x0 ~> 0x1] |
| 70 |
/// arg2: [0x0 ~> 0x7ffffffffffff] |
| 71 |
/// arg3: [0x0 ~> 0x7ffffffffffff] |
| 72 |
/// Output Bounds: |
| 73 |
/// out1: [0x0 ~> 0x7ffffffffffff] |
| 74 |
/// out2: [0x0 ~> 0x1] |
| 75 |
inline fn subborrowxU51(out1: *u64, out2: *u1, arg1: u1, arg2: u64, arg3: u64) void { |
| 76 |
@setRuntimeSafety(mode == .Debug); |
| 77 |
|
| 78 |
const x1 = cast(i64, (cast(i128, cast(i64, (cast(i128, arg2) - cast(i128, arg1)))) - cast(i128, arg3))); |
| 79 |
const x2 = cast(i1, (x1 >> 51)); |
| 80 |
const x3 = cast(u64, (cast(i128, x1) & cast(i128, 0x7ffffffffffff))); |
| 81 |
out1.* = x3; |
| 82 |
out2.* = cast(u1, (cast(i2, 0x0) - cast(i2, x2))); |
| 83 |
} |
| 84 |
|
| 85 |
/// The function cmovznzU64 is a single-word conditional move. |
| 86 |
/// |
| 87 |
/// Postconditions: |
| 88 |
/// out1 = (if arg1 = 0 then arg2 else arg3) |
| 89 |
/// |
| 90 |
/// Input Bounds: |
| 91 |
/// arg1: [0x0 ~> 0x1] |
| 92 |
/// arg2: [0x0 ~> 0xffffffffffffffff] |
| 93 |
/// arg3: [0x0 ~> 0xffffffffffffffff] |
| 94 |
/// Output Bounds: |
| 95 |
/// out1: [0x0 ~> 0xffffffffffffffff] |
| 96 |
inline fn cmovznzU64(out1: *u64, arg1: u1, arg2: u64, arg3: u64) void { |
| 97 |
@setRuntimeSafety(mode == .Debug); |
| 98 |
|
| 99 |
const x1 = (~(~arg1)); |
| 100 |
const x2 = cast(u64, (cast(i128, cast(i1, (cast(i2, 0x0) - cast(i2, x1)))) & cast(i128, 0xffffffffffffffff))); |
| 101 |
const x3 = ((x2 & arg3) | ((~x2) & arg2)); |
| 102 |
out1.* = x3; |
| 103 |
} |
| 104 |
|
| 105 |
/// The function carryMul multiplies two field elements and reduces the result. |
| 106 |
/// |
| 107 |
/// Postconditions: |
| 108 |
/// eval out1 mod m = (eval arg1 * eval arg2) mod m |
| 109 |
/// |
| 110 |
pub fn carryMul(out1: *TightFieldElement, arg1: LooseFieldElement, arg2: LooseFieldElement) void { |
| 111 |
@setRuntimeSafety(mode == .Debug); |
| 112 |
|
| 113 |
const x1 = (cast(u128, (arg1[4])) * cast(u128, ((arg2[4]) * 0x13))); |
| 114 |
const x2 = (cast(u128, (arg1[4])) * cast(u128, ((arg2[3]) * 0x13))); |
| 115 |
const x3 = (cast(u128, (arg1[4])) * cast(u128, ((arg2[2]) * 0x13))); |
| 116 |
const x4 = (cast(u128, (arg1[4])) * cast(u128, ((arg2[1]) * 0x13))); |
| 117 |
const x5 = (cast(u128, (arg1[3])) * cast(u128, ((arg2[4]) * 0x13))); |
| 118 |
const x6 = (cast(u128, (arg1[3])) * cast(u128, ((arg2[3]) * 0x13))); |
| 119 |
const x7 = (cast(u128, (arg1[3])) * cast(u128, ((arg2[2]) * 0x13))); |
| 120 |
const x8 = (cast(u128, (arg1[2])) * cast(u128, ((arg2[4]) * 0x13))); |
| 121 |
const x9 = (cast(u128, (arg1[2])) * cast(u128, ((arg2[3]) * 0x13))); |
| 122 |
const x10 = (cast(u128, (arg1[1])) * cast(u128, ((arg2[4]) * 0x13))); |
| 123 |
const x11 = (cast(u128, (arg1[4])) * cast(u128, (arg2[0]))); |
| 124 |
const x12 = (cast(u128, (arg1[3])) * cast(u128, (arg2[1]))); |
| 125 |
const x13 = (cast(u128, (arg1[3])) * cast(u128, (arg2[0]))); |
| 126 |
const x14 = (cast(u128, (arg1[2])) * cast(u128, (arg2[2]))); |
| 127 |
const x15 = (cast(u128, (arg1[2])) * cast(u128, (arg2[1]))); |
| 128 |
const x16 = (cast(u128, (arg1[2])) * cast(u128, (arg2[0]))); |
| 129 |
const x17 = (cast(u128, (arg1[1])) * cast(u128, (arg2[3]))); |
| 130 |
const x18 = (cast(u128, (arg1[1])) * cast(u128, (arg2[2]))); |
| 131 |
const x19 = (cast(u128, (arg1[1])) * cast(u128, (arg2[1]))); |
| 132 |
const x20 = (cast(u128, (arg1[1])) * cast(u128, (arg2[0]))); |
| 133 |
const x21 = (cast(u128, (arg1[0])) * cast(u128, (arg2[4]))); |
| 134 |
const x22 = (cast(u128, (arg1[0])) * cast(u128, (arg2[3]))); |
| 135 |
const x23 = (cast(u128, (arg1[0])) * cast(u128, (arg2[2]))); |
| 136 |
const x24 = (cast(u128, (arg1[0])) * cast(u128, (arg2[1]))); |
| 137 |
const x25 = (cast(u128, (arg1[0])) * cast(u128, (arg2[0]))); |
| 138 |
const x26 = (x25 + (x10 + (x9 + (x7 + x4)))); |
| 139 |
const x27 = cast(u64, (x26 >> 51)); |
| 140 |
const x28 = cast(u64, (x26 & cast(u128, 0x7ffffffffffff))); |
| 141 |
const x29 = (x21 + (x17 + (x14 + (x12 + x11)))); |
| 142 |
const x30 = (x22 + (x18 + (x15 + (x13 + x1)))); |
| 143 |
const x31 = (x23 + (x19 + (x16 + (x5 + x2)))); |
| 144 |
const x32 = (x24 + (x20 + (x8 + (x6 + x3)))); |
| 145 |
const x33 = (cast(u128, x27) + x32); |
| 146 |
const x34 = cast(u64, (x33 >> 51)); |
| 147 |
const x35 = cast(u64, (x33 & cast(u128, 0x7ffffffffffff))); |
| 148 |
const x36 = (cast(u128, x34) + x31); |
| 149 |
const x37 = cast(u64, (x36 >> 51)); |
| 150 |
const x38 = cast(u64, (x36 & cast(u128, 0x7ffffffffffff))); |
| 151 |
const x39 = (cast(u128, x37) + x30); |
| 152 |
const x40 = cast(u64, (x39 >> 51)); |
| 153 |
const x41 = cast(u64, (x39 & cast(u128, 0x7ffffffffffff))); |
| 154 |
const x42 = (cast(u128, x40) + x29); |
| 155 |
const x43 = cast(u64, (x42 >> 51)); |
| 156 |
const x44 = cast(u64, (x42 & cast(u128, 0x7ffffffffffff))); |
| 157 |
const x45 = (x43 * 0x13); |
| 158 |
const x46 = (x28 + x45); |
| 159 |
const x47 = (x46 >> 51); |
| 160 |
const x48 = (x46 & 0x7ffffffffffff); |
| 161 |
const x49 = (x47 + x35); |
| 162 |
const x50 = cast(u1, (x49 >> 51)); |
| 163 |
const x51 = (x49 & 0x7ffffffffffff); |
| 164 |
const x52 = (cast(u64, x50) + x38); |
| 165 |
out1[0] = x48; |
| 166 |
out1[1] = x51; |
| 167 |
out1[2] = x52; |
| 168 |
out1[3] = x41; |
| 169 |
out1[4] = x44; |
| 170 |
} |
| 171 |
|
| 172 |
/// The function carrySquare squares a field element and reduces the result. |
| 173 |
/// |
| 174 |
/// Postconditions: |
| 175 |
/// eval out1 mod m = (eval arg1 * eval arg1) mod m |
| 176 |
/// |
| 177 |
pub fn carrySquare(out1: *TightFieldElement, arg1: LooseFieldElement) void { |
| 178 |
@setRuntimeSafety(mode == .Debug); |
| 179 |
|
| 180 |
const x1 = ((arg1[4]) * 0x13); |
| 181 |
const x2 = (x1 * 0x2); |
| 182 |
const x3 = ((arg1[4]) * 0x2); |
| 183 |
const x4 = ((arg1[3]) * 0x13); |
| 184 |
const x5 = (x4 * 0x2); |
| 185 |
const x6 = ((arg1[3]) * 0x2); |
| 186 |
const x7 = ((arg1[2]) * 0x2); |
| 187 |
const x8 = ((arg1[1]) * 0x2); |
| 188 |
const x9 = (cast(u128, (arg1[4])) * cast(u128, x1)); |
| 189 |
const x10 = (cast(u128, (arg1[3])) * cast(u128, x2)); |
| 190 |
const x11 = (cast(u128, (arg1[3])) * cast(u128, x4)); |
| 191 |
const x12 = (cast(u128, (arg1[2])) * cast(u128, x2)); |
| 192 |
const x13 = (cast(u128, (arg1[2])) * cast(u128, x5)); |
| 193 |
const x14 = (cast(u128, (arg1[2])) * cast(u128, (arg1[2]))); |
| 194 |
const x15 = (cast(u128, (arg1[1])) * cast(u128, x2)); |
| 195 |
const x16 = (cast(u128, (arg1[1])) * cast(u128, x6)); |
| 196 |
const x17 = (cast(u128, (arg1[1])) * cast(u128, x7)); |
| 197 |
const x18 = (cast(u128, (arg1[1])) * cast(u128, (arg1[1]))); |
| 198 |
const x19 = (cast(u128, (arg1[0])) * cast(u128, x3)); |
| 199 |
const x20 = (cast(u128, (arg1[0])) * cast(u128, x6)); |
| 200 |
const x21 = (cast(u128, (arg1[0])) * cast(u128, x7)); |
| 201 |
const x22 = (cast(u128, (arg1[0])) * cast(u128, x8)); |
| 202 |
const x23 = (cast(u128, (arg1[0])) * cast(u128, (arg1[0]))); |
| 203 |
const x24 = (x23 + (x15 + x13)); |
| 204 |
const x25 = cast(u64, (x24 >> 51)); |
| 205 |
const x26 = cast(u64, (x24 & cast(u128, 0x7ffffffffffff))); |
| 206 |
const x27 = (x19 + (x16 + x14)); |
| 207 |
const x28 = (x20 + (x17 + x9)); |
| 208 |
const x29 = (x21 + (x18 + x10)); |
| 209 |
const x30 = (x22 + (x12 + x11)); |
| 210 |
const x31 = (cast(u128, x25) + x30); |
| 211 |
const x32 = cast(u64, (x31 >> 51)); |
| 212 |
const x33 = cast(u64, (x31 & cast(u128, 0x7ffffffffffff))); |
| 213 |
const x34 = (cast(u128, x32) + x29); |
| 214 |
const x35 = cast(u64, (x34 >> 51)); |
| 215 |
const x36 = cast(u64, (x34 & cast(u128, 0x7ffffffffffff))); |
| 216 |
const x37 = (cast(u128, x35) + x28); |
| 217 |
const x38 = cast(u64, (x37 >> 51)); |
| 218 |
const x39 = cast(u64, (x37 & cast(u128, 0x7ffffffffffff))); |
| 219 |
const x40 = (cast(u128, x38) + x27); |
| 220 |
const x41 = cast(u64, (x40 >> 51)); |
| 221 |
const x42 = cast(u64, (x40 & cast(u128, 0x7ffffffffffff))); |
| 222 |
const x43 = (x41 * 0x13); |
| 223 |
const x44 = (x26 + x43); |
| 224 |
const x45 = (x44 >> 51); |
| 225 |
const x46 = (x44 & 0x7ffffffffffff); |
| 226 |
const x47 = (x45 + x33); |
| 227 |
const x48 = cast(u1, (x47 >> 51)); |
| 228 |
const x49 = (x47 & 0x7ffffffffffff); |
| 229 |
const x50 = (cast(u64, x48) + x36); |
| 230 |
out1[0] = x46; |
| 231 |
out1[1] = x49; |
| 232 |
out1[2] = x50; |
| 233 |
out1[3] = x39; |
| 234 |
out1[4] = x42; |
| 235 |
} |
| 236 |
|
| 237 |
/// The function carry reduces a field element. |
| 238 |
/// |
| 239 |
/// Postconditions: |
| 240 |
/// eval out1 mod m = eval arg1 mod m |
| 241 |
/// |
| 242 |
pub fn carry(out1: *TightFieldElement, arg1: LooseFieldElement) void { |
| 243 |
@setRuntimeSafety(mode == .Debug); |
| 244 |
|
| 245 |
const x1 = (arg1[0]); |
| 246 |
const x2 = ((x1 >> 51) + (arg1[1])); |
| 247 |
const x3 = ((x2 >> 51) + (arg1[2])); |
| 248 |
const x4 = ((x3 >> 51) + (arg1[3])); |
| 249 |
const x5 = ((x4 >> 51) + (arg1[4])); |
| 250 |
const x6 = ((x1 & 0x7ffffffffffff) + ((x5 >> 51) * 0x13)); |
| 251 |
const x7 = (cast(u64, cast(u1, (x6 >> 51))) + (x2 & 0x7ffffffffffff)); |
| 252 |
const x8 = (x6 & 0x7ffffffffffff); |
| 253 |
const x9 = (x7 & 0x7ffffffffffff); |
| 254 |
const x10 = (cast(u64, cast(u1, (x7 >> 51))) + (x3 & 0x7ffffffffffff)); |
| 255 |
const x11 = (x4 & 0x7ffffffffffff); |
| 256 |
const x12 = (x5 & 0x7ffffffffffff); |
| 257 |
out1[0] = x8; |
| 258 |
out1[1] = x9; |
| 259 |
out1[2] = x10; |
| 260 |
out1[3] = x11; |
| 261 |
out1[4] = x12; |
| 262 |
} |
| 263 |
|
| 264 |
/// The function add adds two field elements. |
| 265 |
/// |
| 266 |
/// Postconditions: |
| 267 |
/// eval out1 mod m = (eval arg1 + eval arg2) mod m |
| 268 |
/// |
| 269 |
pub fn add(out1: *LooseFieldElement, arg1: TightFieldElement, arg2: TightFieldElement) void { |
| 270 |
@setRuntimeSafety(mode == .Debug); |
| 271 |
|
| 272 |
const x1 = ((arg1[0]) + (arg2[0])); |
| 273 |
const x2 = ((arg1[1]) + (arg2[1])); |
| 274 |
const x3 = ((arg1[2]) + (arg2[2])); |
| 275 |
const x4 = ((arg1[3]) + (arg2[3])); |
| 276 |
const x5 = ((arg1[4]) + (arg2[4])); |
| 277 |
out1[0] = x1; |
| 278 |
out1[1] = x2; |
| 279 |
out1[2] = x3; |
| 280 |
out1[3] = x4; |
| 281 |
out1[4] = x5; |
| 282 |
} |
| 283 |
|
| 284 |
/// The function sub subtracts two field elements. |
| 285 |
/// |
| 286 |
/// Postconditions: |
| 287 |
/// eval out1 mod m = (eval arg1 - eval arg2) mod m |
| 288 |
/// |
| 289 |
pub fn sub(out1: *LooseFieldElement, arg1: TightFieldElement, arg2: TightFieldElement) void { |
| 290 |
@setRuntimeSafety(mode == .Debug); |
| 291 |
|
| 292 |
const x1 = ((0xfffffffffffda + (arg1[0])) - (arg2[0])); |
| 293 |
const x2 = ((0xffffffffffffe + (arg1[1])) - (arg2[1])); |
| 294 |
const x3 = ((0xffffffffffffe + (arg1[2])) - (arg2[2])); |
| 295 |
const x4 = ((0xffffffffffffe + (arg1[3])) - (arg2[3])); |
| 296 |
const x5 = ((0xffffffffffffe + (arg1[4])) - (arg2[4])); |
| 297 |
out1[0] = x1; |
| 298 |
out1[1] = x2; |
| 299 |
out1[2] = x3; |
| 300 |
out1[3] = x4; |
| 301 |
out1[4] = x5; |
| 302 |
} |
| 303 |
|
| 304 |
/// The function opp negates a field element. |
| 305 |
/// |
| 306 |
/// Postconditions: |
| 307 |
/// eval out1 mod m = -eval arg1 mod m |
| 308 |
/// |
| 309 |
pub fn opp(out1: *LooseFieldElement, arg1: TightFieldElement) void { |
| 310 |
@setRuntimeSafety(mode == .Debug); |
| 311 |
|
| 312 |
const x1 = (0xfffffffffffda - (arg1[0])); |
| 313 |
const x2 = (0xffffffffffffe - (arg1[1])); |
| 314 |
const x3 = (0xffffffffffffe - (arg1[2])); |
| 315 |
const x4 = (0xffffffffffffe - (arg1[3])); |
| 316 |
const x5 = (0xffffffffffffe - (arg1[4])); |
| 317 |
out1[0] = x1; |
| 318 |
out1[1] = x2; |
| 319 |
out1[2] = x3; |
| 320 |
out1[3] = x4; |
| 321 |
out1[4] = x5; |
| 322 |
} |
| 323 |
|
| 324 |
/// The function selectznz is a multi-limb conditional select. |
| 325 |
/// |
| 326 |
/// Postconditions: |
| 327 |
/// out1 = (if arg1 = 0 then arg2 else arg3) |
| 328 |
/// |
| 329 |
/// Input Bounds: |
| 330 |
/// arg1: [0x0 ~> 0x1] |
| 331 |
/// arg2: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]] |
| 332 |
/// arg3: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]] |
| 333 |
/// Output Bounds: |
| 334 |
/// out1: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]] |
| 335 |
pub fn selectznz(out1: *[5]u64, arg1: u1, arg2: [5]u64, arg3: [5]u64) void { |
| 336 |
@setRuntimeSafety(mode == .Debug); |
| 337 |
|
| 338 |
var x1: u64 = undefined; |
| 339 |
cmovznzU64(&x1, arg1, (arg2[0]), (arg3[0])); |
| 340 |
var x2: u64 = undefined; |
| 341 |
cmovznzU64(&x2, arg1, (arg2[1]), (arg3[1])); |
| 342 |
var x3: u64 = undefined; |
| 343 |
cmovznzU64(&x3, arg1, (arg2[2]), (arg3[2])); |
| 344 |
var x4: u64 = undefined; |
| 345 |
cmovznzU64(&x4, arg1, (arg2[3]), (arg3[3])); |
| 346 |
var x5: u64 = undefined; |
| 347 |
cmovznzU64(&x5, arg1, (arg2[4]), (arg3[4])); |
| 348 |
out1[0] = x1; |
| 349 |
out1[1] = x2; |
| 350 |
out1[2] = x3; |
| 351 |
out1[3] = x4; |
| 352 |
out1[4] = x5; |
| 353 |
} |
| 354 |
|
| 355 |
/// The function toBytes serializes a field element to bytes in little-endian order. |
| 356 |
/// |
| 357 |
/// Postconditions: |
| 358 |
/// out1 = map (λ x, ⌊((eval arg1 mod m) mod 2^(8 * (x + 1))) / 2^(8 * x)⌋) [0..31] |
| 359 |
/// |
| 360 |
/// Output Bounds: |
| 361 |
/// out1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]] |
| 362 |
pub fn toBytes(out1: *[32]u8, arg1: TightFieldElement) void { |
| 363 |
@setRuntimeSafety(mode == .Debug); |
| 364 |
|
| 365 |
var x1: u64 = undefined; |
| 366 |
var x2: u1 = undefined; |
| 367 |
subborrowxU51(&x1, &x2, 0x0, (arg1[0]), 0x7ffffffffffed); |
| 368 |
var x3: u64 = undefined; |
| 369 |
var x4: u1 = undefined; |
| 370 |
subborrowxU51(&x3, &x4, x2, (arg1[1]), 0x7ffffffffffff); |
| 371 |
var x5: u64 = undefined; |
| 372 |
var x6: u1 = undefined; |
| 373 |
subborrowxU51(&x5, &x6, x4, (arg1[2]), 0x7ffffffffffff); |
| 374 |
var x7: u64 = undefined; |
| 375 |
var x8: u1 = undefined; |
| 376 |
subborrowxU51(&x7, &x8, x6, (arg1[3]), 0x7ffffffffffff); |
| 377 |
var x9: u64 = undefined; |
| 378 |
var x10: u1 = undefined; |
| 379 |
subborrowxU51(&x9, &x10, x8, (arg1[4]), 0x7ffffffffffff); |
| 380 |
var x11: u64 = undefined; |
| 381 |
cmovznzU64(&x11, x10, cast(u64, 0x0), 0xffffffffffffffff); |
| 382 |
var x12: u64 = undefined; |
| 383 |
var x13: u1 = undefined; |
| 384 |
addcarryxU51(&x12, &x13, 0x0, x1, (x11 & 0x7ffffffffffed)); |
| 385 |
var x14: u64 = undefined; |
| 386 |
var x15: u1 = undefined; |
| 387 |
addcarryxU51(&x14, &x15, x13, x3, (x11 & 0x7ffffffffffff)); |
| 388 |
var x16: u64 = undefined; |
| 389 |
var x17: u1 = undefined; |
| 390 |
addcarryxU51(&x16, &x17, x15, x5, (x11 & 0x7ffffffffffff)); |
| 391 |
var x18: u64 = undefined; |
| 392 |
var x19: u1 = undefined; |
| 393 |
addcarryxU51(&x18, &x19, x17, x7, (x11 & 0x7ffffffffffff)); |
| 394 |
var x20: u64 = undefined; |
| 395 |
var x21: u1 = undefined; |
| 396 |
addcarryxU51(&x20, &x21, x19, x9, (x11 & 0x7ffffffffffff)); |
| 397 |
const x22 = (x20 << 4); |
| 398 |
const x23 = (x18 * cast(u64, 0x2)); |
| 399 |
const x24 = (x16 << 6); |
| 400 |
const x25 = (x14 << 3); |
| 401 |
const x26 = cast(u8, (x12 & cast(u64, 0xff))); |
| 402 |
const x27 = (x12 >> 8); |
| 403 |
const x28 = cast(u8, (x27 & cast(u64, 0xff))); |
| 404 |
const x29 = (x27 >> 8); |
| 405 |
const x30 = cast(u8, (x29 & cast(u64, 0xff))); |
| 406 |
const x31 = (x29 >> 8); |
| 407 |
const x32 = cast(u8, (x31 & cast(u64, 0xff))); |
| 408 |
const x33 = (x31 >> 8); |
| 409 |
const x34 = cast(u8, (x33 & cast(u64, 0xff))); |
| 410 |
const x35 = (x33 >> 8); |
| 411 |
const x36 = cast(u8, (x35 & cast(u64, 0xff))); |
| 412 |
const x37 = cast(u8, (x35 >> 8)); |
| 413 |
const x38 = (x25 + cast(u64, x37)); |
| 414 |
const x39 = cast(u8, (x38 & cast(u64, 0xff))); |
| 415 |
const x40 = (x38 >> 8); |
| 416 |
const x41 = cast(u8, (x40 & cast(u64, 0xff))); |
| 417 |
const x42 = (x40 >> 8); |
| 418 |
const x43 = cast(u8, (x42 & cast(u64, 0xff))); |
| 419 |
const x44 = (x42 >> 8); |
| 420 |
const x45 = cast(u8, (x44 & cast(u64, 0xff))); |
| 421 |
const x46 = (x44 >> 8); |
| 422 |
const x47 = cast(u8, (x46 & cast(u64, 0xff))); |
| 423 |
const x48 = (x46 >> 8); |
| 424 |
const x49 = cast(u8, (x48 & cast(u64, 0xff))); |
| 425 |
const x50 = cast(u8, (x48 >> 8)); |
| 426 |
const x51 = (x24 + cast(u64, x50)); |
| 427 |
const x52 = cast(u8, (x51 & cast(u64, 0xff))); |
| 428 |
const x53 = (x51 >> 8); |
| 429 |
const x54 = cast(u8, (x53 & cast(u64, 0xff))); |
| 430 |
const x55 = (x53 >> 8); |
| 431 |
const x56 = cast(u8, (x55 & cast(u64, 0xff))); |
| 432 |
const x57 = (x55 >> 8); |
| 433 |
const x58 = cast(u8, (x57 & cast(u64, 0xff))); |
| 434 |
const x59 = (x57 >> 8); |
| 435 |
const x60 = cast(u8, (x59 & cast(u64, 0xff))); |
| 436 |
const x61 = (x59 >> 8); |
| 437 |
const x62 = cast(u8, (x61 & cast(u64, 0xff))); |
| 438 |
const x63 = (x61 >> 8); |
| 439 |
const x64 = cast(u8, (x63 & cast(u64, 0xff))); |
| 440 |
const x65 = cast(u1, (x63 >> 8)); |
| 441 |
const x66 = (x23 + cast(u64, x65)); |
| 442 |
const x67 = cast(u8, (x66 & cast(u64, 0xff))); |
| 443 |
const x68 = (x66 >> 8); |
| 444 |
const x69 = cast(u8, (x68 & cast(u64, 0xff))); |
| 445 |
const x70 = (x68 >> 8); |
| 446 |
const x71 = cast(u8, (x70 & cast(u64, 0xff))); |
| 447 |
const x72 = (x70 >> 8); |
| 448 |
const x73 = cast(u8, (x72 & cast(u64, 0xff))); |
| 449 |
const x74 = (x72 >> 8); |
| 450 |
const x75 = cast(u8, (x74 & cast(u64, 0xff))); |
| 451 |
const x76 = (x74 >> 8); |
| 452 |
const x77 = cast(u8, (x76 & cast(u64, 0xff))); |
| 453 |
const x78 = cast(u8, (x76 >> 8)); |
| 454 |
const x79 = (x22 + cast(u64, x78)); |
| 455 |
const x80 = cast(u8, (x79 & cast(u64, 0xff))); |
| 456 |
const x81 = (x79 >> 8); |
| 457 |
const x82 = cast(u8, (x81 & cast(u64, 0xff))); |
| 458 |
const x83 = (x81 >> 8); |
| 459 |
const x84 = cast(u8, (x83 & cast(u64, 0xff))); |
| 460 |
const x85 = (x83 >> 8); |
| 461 |
const x86 = cast(u8, (x85 & cast(u64, 0xff))); |
| 462 |
const x87 = (x85 >> 8); |
| 463 |
const x88 = cast(u8, (x87 & cast(u64, 0xff))); |
| 464 |
const x89 = (x87 >> 8); |
| 465 |
const x90 = cast(u8, (x89 & cast(u64, 0xff))); |
| 466 |
const x91 = cast(u8, (x89 >> 8)); |
| 467 |
out1[0] = x26; |
| 468 |
out1[1] = x28; |
| 469 |
out1[2] = x30; |
| 470 |
out1[3] = x32; |
| 471 |
out1[4] = x34; |
| 472 |
out1[5] = x36; |
| 473 |
out1[6] = x39; |
| 474 |
out1[7] = x41; |
| 475 |
out1[8] = x43; |
| 476 |
out1[9] = x45; |
| 477 |
out1[10] = x47; |
| 478 |
out1[11] = x49; |
| 479 |
out1[12] = x52; |
| 480 |
out1[13] = x54; |
| 481 |
out1[14] = x56; |
| 482 |
out1[15] = x58; |
| 483 |
out1[16] = x60; |
| 484 |
out1[17] = x62; |
| 485 |
out1[18] = x64; |
| 486 |
out1[19] = x67; |
| 487 |
out1[20] = x69; |
| 488 |
out1[21] = x71; |
| 489 |
out1[22] = x73; |
| 490 |
out1[23] = x75; |
| 491 |
out1[24] = x77; |
| 492 |
out1[25] = x80; |
| 493 |
out1[26] = x82; |
| 494 |
out1[27] = x84; |
| 495 |
out1[28] = x86; |
| 496 |
out1[29] = x88; |
| 497 |
out1[30] = x90; |
| 498 |
out1[31] = x91; |
| 499 |
} |
| 500 |
|
| 501 |
/// The function fromBytes deserializes a field element from bytes in little-endian order. |
| 502 |
/// |
| 503 |
/// Postconditions: |
| 504 |
/// eval out1 mod m = bytes_eval arg1 mod m |
| 505 |
/// |
| 506 |
/// Input Bounds: |
| 507 |
/// arg1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]] |
| 508 |
pub fn fromBytes(out1: *TightFieldElement, arg1: [32]u8) void { |
| 509 |
@setRuntimeSafety(mode == .Debug); |
| 510 |
|
| 511 |
const x1 = (cast(u64, (arg1[31])) << 44); |
| 512 |
const x2 = (cast(u64, (arg1[30])) << 36); |
| 513 |
const x3 = (cast(u64, (arg1[29])) << 28); |
| 514 |
const x4 = (cast(u64, (arg1[28])) << 20); |
| 515 |
const x5 = (cast(u64, (arg1[27])) << 12); |
| 516 |
const x6 = (cast(u64, (arg1[26])) << 4); |
| 517 |
const x7 = (cast(u64, (arg1[25])) << 47); |
| 518 |
const x8 = (cast(u64, (arg1[24])) << 39); |
| 519 |
const x9 = (cast(u64, (arg1[23])) << 31); |
| 520 |
const x10 = (cast(u64, (arg1[22])) << 23); |
| 521 |
const x11 = (cast(u64, (arg1[21])) << 15); |
| 522 |
const x12 = (cast(u64, (arg1[20])) << 7); |
| 523 |
const x13 = (cast(u64, (arg1[19])) << 50); |
| 524 |
const x14 = (cast(u64, (arg1[18])) << 42); |
| 525 |
const x15 = (cast(u64, (arg1[17])) << 34); |
| 526 |
const x16 = (cast(u64, (arg1[16])) << 26); |
| 527 |
const x17 = (cast(u64, (arg1[15])) << 18); |
| 528 |
const x18 = (cast(u64, (arg1[14])) << 10); |
| 529 |
const x19 = (cast(u64, (arg1[13])) << 2); |
| 530 |
const x20 = (cast(u64, (arg1[12])) << 45); |
| 531 |
const x21 = (cast(u64, (arg1[11])) << 37); |
| 532 |
const x22 = (cast(u64, (arg1[10])) << 29); |
| 533 |
const x23 = (cast(u64, (arg1[9])) << 21); |
| 534 |
const x24 = (cast(u64, (arg1[8])) << 13); |
| 535 |
const x25 = (cast(u64, (arg1[7])) << 5); |
| 536 |
const x26 = (cast(u64, (arg1[6])) << 48); |
| 537 |
const x27 = (cast(u64, (arg1[5])) << 40); |
| 538 |
const x28 = (cast(u64, (arg1[4])) << 32); |
| 539 |
const x29 = (cast(u64, (arg1[3])) << 24); |
| 540 |
const x30 = (cast(u64, (arg1[2])) << 16); |
| 541 |
const x31 = (cast(u64, (arg1[1])) << 8); |
| 542 |
const x32 = (arg1[0]); |
| 543 |
const x33 = (x31 + cast(u64, x32)); |
| 544 |
const x34 = (x30 + x33); |
| 545 |
const x35 = (x29 + x34); |
| 546 |
const x36 = (x28 + x35); |
| 547 |
const x37 = (x27 + x36); |
| 548 |
const x38 = (x26 + x37); |
| 549 |
const x39 = (x38 & 0x7ffffffffffff); |
| 550 |
const x40 = cast(u8, (x38 >> 51)); |
| 551 |
const x41 = (x25 + cast(u64, x40)); |
| 552 |
const x42 = (x24 + x41); |
| 553 |
const x43 = (x23 + x42); |
| 554 |
const x44 = (x22 + x43); |
| 555 |
const x45 = (x21 + x44); |
| 556 |
const x46 = (x20 + x45); |
| 557 |
const x47 = (x46 & 0x7ffffffffffff); |
| 558 |
const x48 = cast(u8, (x46 >> 51)); |
| 559 |
const x49 = (x19 + cast(u64, x48)); |
| 560 |
const x50 = (x18 + x49); |
| 561 |
const x51 = (x17 + x50); |
| 562 |
const x52 = (x16 + x51); |
| 563 |
const x53 = (x15 + x52); |
| 564 |
const x54 = (x14 + x53); |
| 565 |
const x55 = (x13 + x54); |
| 566 |
const x56 = (x55 & 0x7ffffffffffff); |
| 567 |
const x57 = cast(u8, (x55 >> 51)); |
| 568 |
const x58 = (x12 + cast(u64, x57)); |
| 569 |
const x59 = (x11 + x58); |
| 570 |
const x60 = (x10 + x59); |
| 571 |
const x61 = (x9 + x60); |
| 572 |
const x62 = (x8 + x61); |
| 573 |
const x63 = (x7 + x62); |
| 574 |
const x64 = (x63 & 0x7ffffffffffff); |
| 575 |
const x65 = cast(u8, (x63 >> 51)); |
| 576 |
const x66 = (x6 + cast(u64, x65)); |
| 577 |
const x67 = (x5 + x66); |
| 578 |
const x68 = (x4 + x67); |
| 579 |
const x69 = (x3 + x68); |
| 580 |
const x70 = (x2 + x69); |
| 581 |
const x71 = (x1 + x70); |
| 582 |
out1[0] = x39; |
| 583 |
out1[1] = x47; |
| 584 |
out1[2] = x56; |
| 585 |
out1[3] = x64; |
| 586 |
out1[4] = x71; |
| 587 |
} |
| 588 |
|
| 589 |
/// The function relax is the identity function converting from tight field elements to loose field elements. |
| 590 |
/// |
| 591 |
/// Postconditions: |
| 592 |
/// out1 = arg1 |
| 593 |
/// |
| 594 |
pub fn relax(out1: *LooseFieldElement, arg1: TightFieldElement) void { |
| 595 |
@setRuntimeSafety(mode == .Debug); |
| 596 |
|
| 597 |
const x1 = (arg1[0]); |
| 598 |
const x2 = (arg1[1]); |
| 599 |
const x3 = (arg1[2]); |
| 600 |
const x4 = (arg1[3]); |
| 601 |
const x5 = (arg1[4]); |
| 602 |
out1[0] = x1; |
| 603 |
out1[1] = x2; |
| 604 |
out1[2] = x3; |
| 605 |
out1[3] = x4; |
| 606 |
out1[4] = x5; |
| 607 |
} |
| 608 |
|
| 609 |
/// The function carryScmul121666 multiplies a field element by 121666 and reduces the result. |
| 610 |
/// |
| 611 |
/// Postconditions: |
| 612 |
/// eval out1 mod m = (121666 * eval arg1) mod m |
| 613 |
/// |
| 614 |
pub fn carryScmul121666(out1: *TightFieldElement, arg1: LooseFieldElement) void { |
| 615 |
@setRuntimeSafety(mode == .Debug); |
| 616 |
|
| 617 |
const x1 = (cast(u128, 0x1db42) * cast(u128, (arg1[4]))); |
| 618 |
const x2 = (cast(u128, 0x1db42) * cast(u128, (arg1[3]))); |
| 619 |
const x3 = (cast(u128, 0x1db42) * cast(u128, (arg1[2]))); |
| 620 |
const x4 = (cast(u128, 0x1db42) * cast(u128, (arg1[1]))); |
| 621 |
const x5 = (cast(u128, 0x1db42) * cast(u128, (arg1[0]))); |
| 622 |
const x6 = cast(u64, (x5 >> 51)); |
| 623 |
const x7 = cast(u64, (x5 & cast(u128, 0x7ffffffffffff))); |
| 624 |
const x8 = (cast(u128, x6) + x4); |
| 625 |
const x9 = cast(u64, (x8 >> 51)); |
| 626 |
const x10 = cast(u64, (x8 & cast(u128, 0x7ffffffffffff))); |
| 627 |
const x11 = (cast(u128, x9) + x3); |
| 628 |
const x12 = cast(u64, (x11 >> 51)); |
| 629 |
const x13 = cast(u64, (x11 & cast(u128, 0x7ffffffffffff))); |
| 630 |
const x14 = (cast(u128, x12) + x2); |
| 631 |
const x15 = cast(u64, (x14 >> 51)); |
| 632 |
const x16 = cast(u64, (x14 & cast(u128, 0x7ffffffffffff))); |
| 633 |
const x17 = (cast(u128, x15) + x1); |
| 634 |
const x18 = cast(u64, (x17 >> 51)); |
| 635 |
const x19 = cast(u64, (x17 & cast(u128, 0x7ffffffffffff))); |
| 636 |
const x20 = (x18 * 0x13); |
| 637 |
const x21 = (x7 + x20); |
| 638 |
const x22 = cast(u1, (x21 >> 51)); |
| 639 |
const x23 = (x21 & 0x7ffffffffffff); |
| 640 |
const x24 = (cast(u64, x22) + x10); |
| 641 |
const x25 = cast(u1, (x24 >> 51)); |
| 642 |
const x26 = (x24 & 0x7ffffffffffff); |
| 643 |
const x27 = (cast(u64, x25) + x13); |
| 644 |
out1[0] = x23; |
| 645 |
out1[1] = x26; |
| 646 |
out1[2] = x27; |
| 647 |
out1[3] = x16; |
| 648 |
out1[4] = x19; |
| 649 |
} |