| 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 poly1305 '' 32 '(auto)' '2^130 - 5' carry_mul carry_square carry add sub opp selectznz to_bytes from_bytes relax |
| 2 |
// curve description (via package name): poly1305 |
| 3 |
// machine_wordsize = 32 (from "32") |
| 4 |
// requested operations: carry_mul, carry_square, carry, add, sub, opp, selectznz, to_bytes, from_bytes, relax |
| 5 |
// n = 5 (from "(auto)") |
| 6 |
// s-c = 2^130 - [(1, 5)] (from "2^130 - 5") |
| 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] << 26) + (z[2] << 52) + (z[3] << 78) + (z[4] << 104) |
| 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) |
| 13 |
// balance = [0x7fffff6, 0x7fffffe, 0x7fffffe, 0x7fffffe, 0x7fffffe] |
| 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 ~> 0xc000000], [0x0 ~> 0xc000000], [0x0 ~> 0xc000000], [0x0 ~> 0xc000000], [0x0 ~> 0xc000000]] |
| 33 |
pub const LooseFieldElement = [5]u32; |
| 34 |
|
| 35 |
// The type TightFieldElement is a field element with tight bounds. |
| 36 |
// Bounds: [[0x0 ~> 0x4000000], [0x0 ~> 0x4000000], [0x0 ~> 0x4000000], [0x0 ~> 0x4000000], [0x0 ~> 0x4000000]] |
| 37 |
pub const TightFieldElement = [5]u32; |
| 38 |
|
| 39 |
/// The function addcarryxU26 is an addition with carry. |
| 40 |
/// |
| 41 |
/// Postconditions: |
| 42 |
/// out1 = (arg1 + arg2 + arg3) mod 2^26 |
| 43 |
/// out2 = ⌊(arg1 + arg2 + arg3) / 2^26⌋ |
| 44 |
/// |
| 45 |
/// Input Bounds: |
| 46 |
/// arg1: [0x0 ~> 0x1] |
| 47 |
/// arg2: [0x0 ~> 0x3ffffff] |
| 48 |
/// arg3: [0x0 ~> 0x3ffffff] |
| 49 |
/// Output Bounds: |
| 50 |
/// out1: [0x0 ~> 0x3ffffff] |
| 51 |
/// out2: [0x0 ~> 0x1] |
| 52 |
inline fn addcarryxU26(out1: *u32, out2: *u1, arg1: u1, arg2: u32, arg3: u32) void { |
| 53 |
@setRuntimeSafety(mode == .Debug); |
| 54 |
|
| 55 |
const x1 = ((cast(u32, arg1) + arg2) + arg3); |
| 56 |
const x2 = (x1 & 0x3ffffff); |
| 57 |
const x3 = cast(u1, (x1 >> 26)); |
| 58 |
out1.* = x2; |
| 59 |
out2.* = x3; |
| 60 |
} |
| 61 |
|
| 62 |
/// The function subborrowxU26 is a subtraction with borrow. |
| 63 |
/// |
| 64 |
/// Postconditions: |
| 65 |
/// out1 = (-arg1 + arg2 + -arg3) mod 2^26 |
| 66 |
/// out2 = -⌊(-arg1 + arg2 + -arg3) / 2^26⌋ |
| 67 |
/// |
| 68 |
/// Input Bounds: |
| 69 |
/// arg1: [0x0 ~> 0x1] |
| 70 |
/// arg2: [0x0 ~> 0x3ffffff] |
| 71 |
/// arg3: [0x0 ~> 0x3ffffff] |
| 72 |
/// Output Bounds: |
| 73 |
/// out1: [0x0 ~> 0x3ffffff] |
| 74 |
/// out2: [0x0 ~> 0x1] |
| 75 |
inline fn subborrowxU26(out1: *u32, out2: *u1, arg1: u1, arg2: u32, arg3: u32) void { |
| 76 |
@setRuntimeSafety(mode == .Debug); |
| 77 |
|
| 78 |
const x1 = cast(i32, (cast(i64, cast(i32, (cast(i64, arg2) - cast(i64, arg1)))) - cast(i64, arg3))); |
| 79 |
const x2 = cast(i1, (x1 >> 26)); |
| 80 |
const x3 = cast(u32, (cast(i64, x1) & cast(i64, 0x3ffffff))); |
| 81 |
out1.* = x3; |
| 82 |
out2.* = cast(u1, (cast(i2, 0x0) - cast(i2, x2))); |
| 83 |
} |
| 84 |
|
| 85 |
/// The function cmovznzU32 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 ~> 0xffffffff] |
| 93 |
/// arg3: [0x0 ~> 0xffffffff] |
| 94 |
/// Output Bounds: |
| 95 |
/// out1: [0x0 ~> 0xffffffff] |
| 96 |
inline fn cmovznzU32(out1: *u32, arg1: u1, arg2: u32, arg3: u32) void { |
| 97 |
@setRuntimeSafety(mode == .Debug); |
| 98 |
|
| 99 |
const x1 = (~(~arg1)); |
| 100 |
const x2 = cast(u32, (cast(i64, cast(i1, (cast(i2, 0x0) - cast(i2, x1)))) & cast(i64, 0xffffffff))); |
| 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(u64, (arg1[4])) * cast(u64, ((arg2[4]) * 0x5))); |
| 114 |
const x2 = (cast(u64, (arg1[4])) * cast(u64, ((arg2[3]) * 0x5))); |
| 115 |
const x3 = (cast(u64, (arg1[4])) * cast(u64, ((arg2[2]) * 0x5))); |
| 116 |
const x4 = (cast(u64, (arg1[4])) * cast(u64, ((arg2[1]) * 0x5))); |
| 117 |
const x5 = (cast(u64, (arg1[3])) * cast(u64, ((arg2[4]) * 0x5))); |
| 118 |
const x6 = (cast(u64, (arg1[3])) * cast(u64, ((arg2[3]) * 0x5))); |
| 119 |
const x7 = (cast(u64, (arg1[3])) * cast(u64, ((arg2[2]) * 0x5))); |
| 120 |
const x8 = (cast(u64, (arg1[2])) * cast(u64, ((arg2[4]) * 0x5))); |
| 121 |
const x9 = (cast(u64, (arg1[2])) * cast(u64, ((arg2[3]) * 0x5))); |
| 122 |
const x10 = (cast(u64, (arg1[1])) * cast(u64, ((arg2[4]) * 0x5))); |
| 123 |
const x11 = (cast(u64, (arg1[4])) * cast(u64, (arg2[0]))); |
| 124 |
const x12 = (cast(u64, (arg1[3])) * cast(u64, (arg2[1]))); |
| 125 |
const x13 = (cast(u64, (arg1[3])) * cast(u64, (arg2[0]))); |
| 126 |
const x14 = (cast(u64, (arg1[2])) * cast(u64, (arg2[2]))); |
| 127 |
const x15 = (cast(u64, (arg1[2])) * cast(u64, (arg2[1]))); |
| 128 |
const x16 = (cast(u64, (arg1[2])) * cast(u64, (arg2[0]))); |
| 129 |
const x17 = (cast(u64, (arg1[1])) * cast(u64, (arg2[3]))); |
| 130 |
const x18 = (cast(u64, (arg1[1])) * cast(u64, (arg2[2]))); |
| 131 |
const x19 = (cast(u64, (arg1[1])) * cast(u64, (arg2[1]))); |
| 132 |
const x20 = (cast(u64, (arg1[1])) * cast(u64, (arg2[0]))); |
| 133 |
const x21 = (cast(u64, (arg1[0])) * cast(u64, (arg2[4]))); |
| 134 |
const x22 = (cast(u64, (arg1[0])) * cast(u64, (arg2[3]))); |
| 135 |
const x23 = (cast(u64, (arg1[0])) * cast(u64, (arg2[2]))); |
| 136 |
const x24 = (cast(u64, (arg1[0])) * cast(u64, (arg2[1]))); |
| 137 |
const x25 = (cast(u64, (arg1[0])) * cast(u64, (arg2[0]))); |
| 138 |
const x26 = (x25 + (x10 + (x9 + (x7 + x4)))); |
| 139 |
const x27 = (x26 >> 26); |
| 140 |
const x28 = cast(u32, (x26 & cast(u64, 0x3ffffff))); |
| 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 = (x27 + x32); |
| 146 |
const x34 = (x33 >> 26); |
| 147 |
const x35 = cast(u32, (x33 & cast(u64, 0x3ffffff))); |
| 148 |
const x36 = (x34 + x31); |
| 149 |
const x37 = (x36 >> 26); |
| 150 |
const x38 = cast(u32, (x36 & cast(u64, 0x3ffffff))); |
| 151 |
const x39 = (x37 + x30); |
| 152 |
const x40 = (x39 >> 26); |
| 153 |
const x41 = cast(u32, (x39 & cast(u64, 0x3ffffff))); |
| 154 |
const x42 = (x40 + x29); |
| 155 |
const x43 = cast(u32, (x42 >> 26)); |
| 156 |
const x44 = cast(u32, (x42 & cast(u64, 0x3ffffff))); |
| 157 |
const x45 = (cast(u64, x43) * cast(u64, 0x5)); |
| 158 |
const x46 = (cast(u64, x28) + x45); |
| 159 |
const x47 = cast(u32, (x46 >> 26)); |
| 160 |
const x48 = cast(u32, (x46 & cast(u64, 0x3ffffff))); |
| 161 |
const x49 = (x47 + x35); |
| 162 |
const x50 = cast(u1, (x49 >> 26)); |
| 163 |
const x51 = (x49 & 0x3ffffff); |
| 164 |
const x52 = (cast(u32, 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]) * 0x5); |
| 181 |
const x2 = (x1 * 0x2); |
| 182 |
const x3 = ((arg1[4]) * 0x2); |
| 183 |
const x4 = ((arg1[3]) * 0x5); |
| 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(u64, (arg1[4])) * cast(u64, x1)); |
| 189 |
const x10 = (cast(u64, (arg1[3])) * cast(u64, x2)); |
| 190 |
const x11 = (cast(u64, (arg1[3])) * cast(u64, x4)); |
| 191 |
const x12 = (cast(u64, (arg1[2])) * cast(u64, x2)); |
| 192 |
const x13 = (cast(u64, (arg1[2])) * cast(u64, x5)); |
| 193 |
const x14 = (cast(u64, (arg1[2])) * cast(u64, (arg1[2]))); |
| 194 |
const x15 = (cast(u64, (arg1[1])) * cast(u64, x2)); |
| 195 |
const x16 = (cast(u64, (arg1[1])) * cast(u64, x6)); |
| 196 |
const x17 = (cast(u64, (arg1[1])) * cast(u64, x7)); |
| 197 |
const x18 = (cast(u64, (arg1[1])) * cast(u64, (arg1[1]))); |
| 198 |
const x19 = (cast(u64, (arg1[0])) * cast(u64, x3)); |
| 199 |
const x20 = (cast(u64, (arg1[0])) * cast(u64, x6)); |
| 200 |
const x21 = (cast(u64, (arg1[0])) * cast(u64, x7)); |
| 201 |
const x22 = (cast(u64, (arg1[0])) * cast(u64, x8)); |
| 202 |
const x23 = (cast(u64, (arg1[0])) * cast(u64, (arg1[0]))); |
| 203 |
const x24 = (x23 + (x15 + x13)); |
| 204 |
const x25 = (x24 >> 26); |
| 205 |
const x26 = cast(u32, (x24 & cast(u64, 0x3ffffff))); |
| 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 = (x25 + x30); |
| 211 |
const x32 = (x31 >> 26); |
| 212 |
const x33 = cast(u32, (x31 & cast(u64, 0x3ffffff))); |
| 213 |
const x34 = (x32 + x29); |
| 214 |
const x35 = (x34 >> 26); |
| 215 |
const x36 = cast(u32, (x34 & cast(u64, 0x3ffffff))); |
| 216 |
const x37 = (x35 + x28); |
| 217 |
const x38 = (x37 >> 26); |
| 218 |
const x39 = cast(u32, (x37 & cast(u64, 0x3ffffff))); |
| 219 |
const x40 = (x38 + x27); |
| 220 |
const x41 = cast(u32, (x40 >> 26)); |
| 221 |
const x42 = cast(u32, (x40 & cast(u64, 0x3ffffff))); |
| 222 |
const x43 = (cast(u64, x41) * cast(u64, 0x5)); |
| 223 |
const x44 = (cast(u64, x26) + x43); |
| 224 |
const x45 = cast(u32, (x44 >> 26)); |
| 225 |
const x46 = cast(u32, (x44 & cast(u64, 0x3ffffff))); |
| 226 |
const x47 = (x45 + x33); |
| 227 |
const x48 = cast(u1, (x47 >> 26)); |
| 228 |
const x49 = (x47 & 0x3ffffff); |
| 229 |
const x50 = (cast(u32, 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 >> 26) + (arg1[1])); |
| 247 |
const x3 = ((x2 >> 26) + (arg1[2])); |
| 248 |
const x4 = ((x3 >> 26) + (arg1[3])); |
| 249 |
const x5 = ((x4 >> 26) + (arg1[4])); |
| 250 |
const x6 = ((x1 & 0x3ffffff) + ((x5 >> 26) * 0x5)); |
| 251 |
const x7 = (cast(u32, cast(u1, (x6 >> 26))) + (x2 & 0x3ffffff)); |
| 252 |
const x8 = (x6 & 0x3ffffff); |
| 253 |
const x9 = (x7 & 0x3ffffff); |
| 254 |
const x10 = (cast(u32, cast(u1, (x7 >> 26))) + (x3 & 0x3ffffff)); |
| 255 |
const x11 = (x4 & 0x3ffffff); |
| 256 |
const x12 = (x5 & 0x3ffffff); |
| 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 = ((0x7fffff6 + (arg1[0])) - (arg2[0])); |
| 293 |
const x2 = ((0x7fffffe + (arg1[1])) - (arg2[1])); |
| 294 |
const x3 = ((0x7fffffe + (arg1[2])) - (arg2[2])); |
| 295 |
const x4 = ((0x7fffffe + (arg1[3])) - (arg2[3])); |
| 296 |
const x5 = ((0x7fffffe + (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 = (0x7fffff6 - (arg1[0])); |
| 313 |
const x2 = (0x7fffffe - (arg1[1])); |
| 314 |
const x3 = (0x7fffffe - (arg1[2])); |
| 315 |
const x4 = (0x7fffffe - (arg1[3])); |
| 316 |
const x5 = (0x7fffffe - (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 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]] |
| 332 |
/// arg3: [[0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]] |
| 333 |
/// Output Bounds: |
| 334 |
/// out1: [[0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]] |
| 335 |
pub fn selectznz(out1: *[5]u32, arg1: u1, arg2: [5]u32, arg3: [5]u32) void { |
| 336 |
@setRuntimeSafety(mode == .Debug); |
| 337 |
|
| 338 |
var x1: u32 = undefined; |
| 339 |
cmovznzU32(&x1, arg1, (arg2[0]), (arg3[0])); |
| 340 |
var x2: u32 = undefined; |
| 341 |
cmovznzU32(&x2, arg1, (arg2[1]), (arg3[1])); |
| 342 |
var x3: u32 = undefined; |
| 343 |
cmovznzU32(&x3, arg1, (arg2[2]), (arg3[2])); |
| 344 |
var x4: u32 = undefined; |
| 345 |
cmovznzU32(&x4, arg1, (arg2[3]), (arg3[3])); |
| 346 |
var x5: u32 = undefined; |
| 347 |
cmovznzU32(&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..16] |
| 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 ~> 0x3]] |
| 362 |
pub fn toBytes(out1: *[17]u8, arg1: TightFieldElement) void { |
| 363 |
@setRuntimeSafety(mode == .Debug); |
| 364 |
|
| 365 |
var x1: u32 = undefined; |
| 366 |
var x2: u1 = undefined; |
| 367 |
subborrowxU26(&x1, &x2, 0x0, (arg1[0]), 0x3fffffb); |
| 368 |
var x3: u32 = undefined; |
| 369 |
var x4: u1 = undefined; |
| 370 |
subborrowxU26(&x3, &x4, x2, (arg1[1]), 0x3ffffff); |
| 371 |
var x5: u32 = undefined; |
| 372 |
var x6: u1 = undefined; |
| 373 |
subborrowxU26(&x5, &x6, x4, (arg1[2]), 0x3ffffff); |
| 374 |
var x7: u32 = undefined; |
| 375 |
var x8: u1 = undefined; |
| 376 |
subborrowxU26(&x7, &x8, x6, (arg1[3]), 0x3ffffff); |
| 377 |
var x9: u32 = undefined; |
| 378 |
var x10: u1 = undefined; |
| 379 |
subborrowxU26(&x9, &x10, x8, (arg1[4]), 0x3ffffff); |
| 380 |
var x11: u32 = undefined; |
| 381 |
cmovznzU32(&x11, x10, cast(u32, 0x0), 0xffffffff); |
| 382 |
var x12: u32 = undefined; |
| 383 |
var x13: u1 = undefined; |
| 384 |
addcarryxU26(&x12, &x13, 0x0, x1, (x11 & 0x3fffffb)); |
| 385 |
var x14: u32 = undefined; |
| 386 |
var x15: u1 = undefined; |
| 387 |
addcarryxU26(&x14, &x15, x13, x3, (x11 & 0x3ffffff)); |
| 388 |
var x16: u32 = undefined; |
| 389 |
var x17: u1 = undefined; |
| 390 |
addcarryxU26(&x16, &x17, x15, x5, (x11 & 0x3ffffff)); |
| 391 |
var x18: u32 = undefined; |
| 392 |
var x19: u1 = undefined; |
| 393 |
addcarryxU26(&x18, &x19, x17, x7, (x11 & 0x3ffffff)); |
| 394 |
var x20: u32 = undefined; |
| 395 |
var x21: u1 = undefined; |
| 396 |
addcarryxU26(&x20, &x21, x19, x9, (x11 & 0x3ffffff)); |
| 397 |
const x22 = (x18 << 6); |
| 398 |
const x23 = (x16 << 4); |
| 399 |
const x24 = (x14 << 2); |
| 400 |
const x25 = cast(u8, (x12 & cast(u32, 0xff))); |
| 401 |
const x26 = (x12 >> 8); |
| 402 |
const x27 = cast(u8, (x26 & cast(u32, 0xff))); |
| 403 |
const x28 = (x26 >> 8); |
| 404 |
const x29 = cast(u8, (x28 & cast(u32, 0xff))); |
| 405 |
const x30 = cast(u8, (x28 >> 8)); |
| 406 |
const x31 = (x24 + cast(u32, x30)); |
| 407 |
const x32 = cast(u8, (x31 & cast(u32, 0xff))); |
| 408 |
const x33 = (x31 >> 8); |
| 409 |
const x34 = cast(u8, (x33 & cast(u32, 0xff))); |
| 410 |
const x35 = (x33 >> 8); |
| 411 |
const x36 = cast(u8, (x35 & cast(u32, 0xff))); |
| 412 |
const x37 = cast(u8, (x35 >> 8)); |
| 413 |
const x38 = (x23 + cast(u32, x37)); |
| 414 |
const x39 = cast(u8, (x38 & cast(u32, 0xff))); |
| 415 |
const x40 = (x38 >> 8); |
| 416 |
const x41 = cast(u8, (x40 & cast(u32, 0xff))); |
| 417 |
const x42 = (x40 >> 8); |
| 418 |
const x43 = cast(u8, (x42 & cast(u32, 0xff))); |
| 419 |
const x44 = cast(u8, (x42 >> 8)); |
| 420 |
const x45 = (x22 + cast(u32, x44)); |
| 421 |
const x46 = cast(u8, (x45 & cast(u32, 0xff))); |
| 422 |
const x47 = (x45 >> 8); |
| 423 |
const x48 = cast(u8, (x47 & cast(u32, 0xff))); |
| 424 |
const x49 = (x47 >> 8); |
| 425 |
const x50 = cast(u8, (x49 & cast(u32, 0xff))); |
| 426 |
const x51 = cast(u8, (x49 >> 8)); |
| 427 |
const x52 = cast(u8, (x20 & cast(u32, 0xff))); |
| 428 |
const x53 = (x20 >> 8); |
| 429 |
const x54 = cast(u8, (x53 & cast(u32, 0xff))); |
| 430 |
const x55 = (x53 >> 8); |
| 431 |
const x56 = cast(u8, (x55 & cast(u32, 0xff))); |
| 432 |
const x57 = cast(u8, (x55 >> 8)); |
| 433 |
out1[0] = x25; |
| 434 |
out1[1] = x27; |
| 435 |
out1[2] = x29; |
| 436 |
out1[3] = x32; |
| 437 |
out1[4] = x34; |
| 438 |
out1[5] = x36; |
| 439 |
out1[6] = x39; |
| 440 |
out1[7] = x41; |
| 441 |
out1[8] = x43; |
| 442 |
out1[9] = x46; |
| 443 |
out1[10] = x48; |
| 444 |
out1[11] = x50; |
| 445 |
out1[12] = x51; |
| 446 |
out1[13] = x52; |
| 447 |
out1[14] = x54; |
| 448 |
out1[15] = x56; |
| 449 |
out1[16] = x57; |
| 450 |
} |
| 451 |
|
| 452 |
/// The function fromBytes deserializes a field element from bytes in little-endian order. |
| 453 |
/// |
| 454 |
/// Postconditions: |
| 455 |
/// eval out1 mod m = bytes_eval arg1 mod m |
| 456 |
/// |
| 457 |
/// Input Bounds: |
| 458 |
/// 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 ~> 0x3]] |
| 459 |
pub fn fromBytes(out1: *TightFieldElement, arg1: [17]u8) void { |
| 460 |
@setRuntimeSafety(mode == .Debug); |
| 461 |
|
| 462 |
const x1 = (cast(u32, (arg1[16])) << 24); |
| 463 |
const x2 = (cast(u32, (arg1[15])) << 16); |
| 464 |
const x3 = (cast(u32, (arg1[14])) << 8); |
| 465 |
const x4 = (arg1[13]); |
| 466 |
const x5 = (cast(u32, (arg1[12])) << 18); |
| 467 |
const x6 = (cast(u32, (arg1[11])) << 10); |
| 468 |
const x7 = (cast(u32, (arg1[10])) << 2); |
| 469 |
const x8 = (cast(u32, (arg1[9])) << 20); |
| 470 |
const x9 = (cast(u32, (arg1[8])) << 12); |
| 471 |
const x10 = (cast(u32, (arg1[7])) << 4); |
| 472 |
const x11 = (cast(u32, (arg1[6])) << 22); |
| 473 |
const x12 = (cast(u32, (arg1[5])) << 14); |
| 474 |
const x13 = (cast(u32, (arg1[4])) << 6); |
| 475 |
const x14 = (cast(u32, (arg1[3])) << 24); |
| 476 |
const x15 = (cast(u32, (arg1[2])) << 16); |
| 477 |
const x16 = (cast(u32, (arg1[1])) << 8); |
| 478 |
const x17 = (arg1[0]); |
| 479 |
const x18 = (x16 + cast(u32, x17)); |
| 480 |
const x19 = (x15 + x18); |
| 481 |
const x20 = (x14 + x19); |
| 482 |
const x21 = (x20 & 0x3ffffff); |
| 483 |
const x22 = cast(u8, (x20 >> 26)); |
| 484 |
const x23 = (x13 + cast(u32, x22)); |
| 485 |
const x24 = (x12 + x23); |
| 486 |
const x25 = (x11 + x24); |
| 487 |
const x26 = (x25 & 0x3ffffff); |
| 488 |
const x27 = cast(u8, (x25 >> 26)); |
| 489 |
const x28 = (x10 + cast(u32, x27)); |
| 490 |
const x29 = (x9 + x28); |
| 491 |
const x30 = (x8 + x29); |
| 492 |
const x31 = (x30 & 0x3ffffff); |
| 493 |
const x32 = cast(u8, (x30 >> 26)); |
| 494 |
const x33 = (x7 + cast(u32, x32)); |
| 495 |
const x34 = (x6 + x33); |
| 496 |
const x35 = (x5 + x34); |
| 497 |
const x36 = (x3 + cast(u32, x4)); |
| 498 |
const x37 = (x2 + x36); |
| 499 |
const x38 = (x1 + x37); |
| 500 |
out1[0] = x21; |
| 501 |
out1[1] = x26; |
| 502 |
out1[2] = x31; |
| 503 |
out1[3] = x35; |
| 504 |
out1[4] = x38; |
| 505 |
} |
| 506 |
|
| 507 |
/// The function relax is the identity function converting from tight field elements to loose field elements. |
| 508 |
/// |
| 509 |
/// Postconditions: |
| 510 |
/// out1 = arg1 |
| 511 |
/// |
| 512 |
pub fn relax(out1: *LooseFieldElement, arg1: TightFieldElement) void { |
| 513 |
@setRuntimeSafety(mode == .Debug); |
| 514 |
|
| 515 |
const x1 = (arg1[0]); |
| 516 |
const x2 = (arg1[1]); |
| 517 |
const x3 = (arg1[2]); |
| 518 |
const x4 = (arg1[3]); |
| 519 |
const x5 = (arg1[4]); |
| 520 |
out1[0] = x1; |
| 521 |
out1[1] = x2; |
| 522 |
out1[2] = x3; |
| 523 |
out1[3] = x4; |
| 524 |
out1[4] = x5; |
| 525 |
} |