From 08ff5deb7c7d26acd26a046bf70b45482cb5e168 Mon Sep 17 00:00:00 2001 From: cpunkzzz Date: Sun, 2 Oct 2022 20:52:29 -0400 Subject: [PATCH 1/5] Add mul goldilocks --- risc0/zkvm/platform/io.h | 10 ++++ risc0/zkvm/platform/memory.h | 1 + risc0/zkvm/platform/risc0.ld | 1 + risc0/zkvm/prove/io_handler.cpp | 40 ++++++++++++++ risc0/zkvm/sdk/rust/guest/src/lib.rs | 3 ++ risc0/zkvm/sdk/rust/guest/src/mul.rs | 62 ++++++++++++++++++++++ risc0/zkvm/sdk/rust/platform/src/io.rs | 10 ++++ risc0/zkvm/sdk/rust/platform/src/memory.rs | 1 + 8 files changed, 128 insertions(+) create mode 100644 risc0/zkvm/sdk/rust/guest/src/mul.rs diff --git a/risc0/zkvm/platform/io.h b/risc0/zkvm/platform/io.h index 83e20751c2..55d3436b2a 100644 --- a/risc0/zkvm/platform/io.h +++ b/risc0/zkvm/platform/io.h @@ -26,6 +26,7 @@ constexpr size_t kGPIO_GetKey = 0x01F0010; constexpr size_t kGPIO_SendRecvChannel = 0x01F00014; constexpr size_t kGPIO_SendRecvSize = 0x01F00018; constexpr size_t kGPIO_SendRecvAddr = 0x01F0001C; +constexpr size_t kGPIO_Mul = 0x01F00020; // Standard ZKVM channels; must match zkvm/sdk/rust/platform/src/io.rs. @@ -67,6 +68,15 @@ struct ShaDescriptor { uint32_t digest; }; +struct MulDescriptor { + // Address of first byte of MUL data to process + // 64 bits for first operand and 64 bits for second + uint32_t source; + + // 64 bit result + uint32_t result; +}; + inline volatile ShaDescriptor* volatile* GPIO_SHA() { return reinterpret_cast(kGPIO_SHA); } diff --git a/risc0/zkvm/platform/memory.h b/risc0/zkvm/platform/memory.h index 7b427a43f9..e88098cd7e 100644 --- a/risc0/zkvm/platform/memory.h +++ b/risc0/zkvm/platform/memory.h @@ -46,6 +46,7 @@ MEM_REGION(SHA, 0x02A00000, k1MB) MEM_REGION(WOM, 0x02B00000, 21 * k1MB) MEM_REGION(Output, 0x02B00000, 20 * k1MB) MEM_REGION(Commit, 0x03F00000, k1MB) +MEM_REGION(MUL, 0x04000000, k1MB) // clang-format on #define PTR_TO(type, name) reinterpret_cast(kMem##name##Start); diff --git a/risc0/zkvm/platform/risc0.ld b/risc0/zkvm/platform/risc0.ld index ed4e89e238..7e5678db1b 100644 --- a/risc0/zkvm/platform/risc0.ld +++ b/risc0/zkvm/platform/risc0.ld @@ -30,6 +30,7 @@ MEMORY { prog (X) : ORIGIN = 0x02000000, LENGTH = 10M sha : ORIGIN = 0x02A00000, LENGTH = 1M wom : ORIGIN = 0x02B00000, LENGTH = 21M + mul : ORIGIN = 0x04000000, LENGTH = 1M } SECTIONS { diff --git a/risc0/zkvm/prove/io_handler.cpp b/risc0/zkvm/prove/io_handler.cpp index 68784408ef..73a681d7d4 100644 --- a/risc0/zkvm/prove/io_handler.cpp +++ b/risc0/zkvm/prove/io_handler.cpp @@ -46,6 +46,39 @@ static void processSHA(MemoryState& mem, const ShaDescriptor& desc) { } } +static void processMul(MemoryState& mem, const MulDescriptor& desc) { + uint32_t first_operand[2]; + uint32_t second_operand[2]; + + first_operand[0] = mem.loadBE(desc.source); + LOG(1, "Input[" << hex(0, 2) << "]: " << hex(desc.source) << " -> " << hex(first_operand[0])); + first_operand[1] = mem.loadBE(desc.source + 4); + LOG(1, "Input[" << hex(1, 2) << "]: " << hex(desc.source + 4) << " -> " << hex(first_operand[1])); + second_operand[0] = mem.loadBE(desc.source + 8); + LOG(1, + "Input[" << hex(2, 2) << "]: " << hex(desc.source + 8) << " -> " << hex(second_operand[0])); + second_operand[1] = mem.loadBE(desc.source + 12); + LOG(1, + "Input[" << hex(3, 2) << "]: " << hex(desc.source + 12) << " -> " << hex(second_operand[1])); + + // MSB is at 0 + uint64_t first = first_operand[1] | (uint64_t(first_operand[0]) << 32); + uint64_t second = second_operand[1] | (uint64_t(second_operand[0]) << 32); + + __uint128_t result = __uint128_t(first) * __uint128_t(second); + + // goldilocks + uint64_t moded_result = result % 0xFFFFFFFF00000001; + + uint32_t high = (uint32_t)((moded_result & 0xFFFFFFFF00000000LL) >> 32); + uint32_t low = (uint32_t)(moded_result & 0xFFFFFFFFLL); + + LOG(1, "Output[" << hex(0, 2) << "]: " << hex(desc.result) << " <- " << hex(high)); + mem.store(desc.result, high); + LOG(1, "Output[" << hex(1, 2) << "]: " << hex(desc.result + 4) << " <- " << hex(low)); + mem.store(desc.result + 4, low); +} + void IoHandler::onFault(const std::string& msg) { throw std::runtime_error(msg); } @@ -63,6 +96,13 @@ void MemoryHandler::onInit(MemoryState& mem) { void MemoryHandler::onWrite(MemoryState& mem, uint32_t cycle, uint32_t addr, uint32_t value) { LOG(2, "MemoryHandler::onWrite> " << hex(addr) << ": " << hex(value)); switch (addr) { + case kGPIO_Mul: { + LOG(1, "MemoryHandler::onWrite> GPIO_MUL"); + MulDescriptor desc; + mem.loadRegion(value, &desc, sizeof(desc)); + processMul(mem, desc); + break; + } case kGPIO_SHA: { LOG(1, "MemoryHandler::onWrite> GPIO_SHA"); ShaDescriptor desc; diff --git a/risc0/zkvm/sdk/rust/guest/src/lib.rs b/risc0/zkvm/sdk/rust/guest/src/lib.rs index a7fd6745da..094fc776d4 100644 --- a/risc0/zkvm/sdk/rust/guest/src/lib.rs +++ b/risc0/zkvm/sdk/rust/guest/src/lib.rs @@ -32,6 +32,9 @@ pub mod env; /// Functions for computing SHA-256 hashes. pub mod sha; +/// mul +pub mod mul; + /// Functions for handling input and output pub mod io; diff --git a/risc0/zkvm/sdk/rust/guest/src/mul.rs b/risc0/zkvm/sdk/rust/guest/src/mul.rs new file mode 100644 index 0000000000..f95cff644d --- /dev/null +++ b/risc0/zkvm/sdk/rust/guest/src/mul.rs @@ -0,0 +1,62 @@ +use core::{cell::UnsafeCell, mem}; + +use _alloc::{boxed::Box, vec::Vec}; +use risc0_zkvm::platform::{io::{MulDescriptor, GPIO_MUL}, memory}; + +// Current sha descriptor index. +struct CurOutput(UnsafeCell); + +// SAFETY: single threaded environment +unsafe impl Sync for CurOutput {} + +static CUR_OUTPUT: CurOutput = CurOutput(UnsafeCell::new(0)); + +pub struct MulGoldilocks([u32; 2]); + +fn alloc_output() -> *mut MulDescriptor { + // SAFETY: Single threaded and this is the only place we use CUR_DESC. + unsafe { + let cur_desc = CUR_OUTPUT.0.get(); + let ptr = (memory::MUL.start() as *mut MulDescriptor).add(*cur_desc); + *cur_desc += 1; + ptr + } +} + +pub fn mul_goldilocks(a: &u64, b: &u64) -> &'static MulGoldilocks { + // Allocate fresh memory that's guaranteed to be uninitialized so + // the host can write to it. + let mut buf = Vec::::with_capacity(4); + let a_hi = (u32)((a & 0xFFFFFFFF00000000LL) >> 32); + let a_lo = (u32)(a & 0xFFFFFFFFLL); + + let b_hi = (u32)((b & 0xFFFFFFFF00000000LL) >> 32); + let b_lo = (u32)(b & 0xFFFFFFFFLL); + + buf.push(a_hi); + buf.push(a_lo); + buf.push(b_hi); + buf.push(b_lo); + + unsafe { + let alloced = Box::>::new( + mem::MaybeUninit::::uninit(), + ); + let output = (*Box::into_raw(alloced)).as_mut_ptr(); + mul_raw(&buf[..], output); + &*output + } +} + +pub(crate) unsafe fn mul_raw(data: &[u32], result: *mut MulGoldilocks) { + let output_ptr = alloc_output(); + + let ptr = data.as_ptr(); + super::memory_barrier(ptr); + output_ptr.write_volatile(MulDescriptor { + source: ptr as usize, + result: result as usize, + }); + + GPIO_MUL.as_ptr().write_volatile(output_ptr); +} diff --git a/risc0/zkvm/sdk/rust/platform/src/io.rs b/risc0/zkvm/sdk/rust/platform/src/io.rs index b75b641db3..34a950351f 100644 --- a/risc0/zkvm/sdk/rust/platform/src/io.rs +++ b/risc0/zkvm/sdk/rust/platform/src/io.rs @@ -49,6 +49,8 @@ pub const GPIO_SENDRECV_CHANNEL: Gpio = Gpio::new(0x01F0_0014); pub const GPIO_SENDRECV_SIZE: Gpio = Gpio::new(0x01F0_0018); pub const GPIO_SENDRECV_ADDR: Gpio<*const u8> = Gpio::new(0x01F0_001C); +pub const GPIO_MUL: Gpio<*const MulDescriptor> = Gpio::new(0x01F0_0020); + pub mod addr { pub const GPIO_SHA: u32 = super::GPIO_SHA.addr(); pub const GPIO_COMMIT: u32 = super::GPIO_COMMIT.addr(); @@ -59,6 +61,8 @@ pub mod addr { pub const GPIO_SENDRECV_CHANNEL: u32 = super::GPIO_SENDRECV_CHANNEL.addr(); pub const GPIO_SENDRECV_SIZE: u32 = super::GPIO_SENDRECV_SIZE.addr(); pub const GPIO_SENDRECV_ADDR: u32 = super::GPIO_SENDRECV_ADDR.addr(); + + pub const GPIO_MUL: u32 = super::GPIO_MUL.addr(); } #[repr(C)] @@ -75,6 +79,12 @@ pub struct SHADescriptor { pub digest: usize, } +#[repr(C)] +pub struct MulDescriptor { + pub source: usize, + pub result: usize, +} + #[repr(C)] pub struct GetKeyDescriptor { pub name: u32, diff --git a/risc0/zkvm/sdk/rust/platform/src/memory.rs b/risc0/zkvm/sdk/rust/platform/src/memory.rs index e9034d9d4a..df658de1fe 100644 --- a/risc0/zkvm/sdk/rust/platform/src/memory.rs +++ b/risc0/zkvm/sdk/rust/platform/src/memory.rs @@ -65,3 +65,4 @@ pub const SHA: Region = Region::new(0x02A0_0000, mb(1)); pub const WOM: Region = Region::new(0x02B0_0000, mb(21)); pub const OUTPUT: Region = Region::new(0x02B0_0000, mb(20)); pub const COMMIT: Region = Region::new(0x03F0_0000, mb(1)); +pub const MUL: Region = Region::new(0x0400_0000, mb(1)); From 9eeb01b6c5a9d11cdfe500832c53b658c5d495bc Mon Sep 17 00:00:00 2001 From: cpunkzzz Date: Mon, 3 Oct 2022 16:35:08 -0400 Subject: [PATCH 2/5] fix regions --- risc0/zkvm/platform/memory.h | 8 +++---- risc0/zkvm/platform/risc0.ld | 4 ++-- risc0/zkvm/sdk/rust/guest/src/mul.rs | 26 +++++++++++++++++----- risc0/zkvm/sdk/rust/platform/src/memory.rs | 8 +++---- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/risc0/zkvm/platform/memory.h b/risc0/zkvm/platform/memory.h index e88098cd7e..c3fe992ba3 100644 --- a/risc0/zkvm/platform/memory.h +++ b/risc0/zkvm/platform/memory.h @@ -43,10 +43,10 @@ MEM_REGION(Input, 0x01E00000, k1MB) MEM_REGION(GPIO, 0x01F00000, k1MB) MEM_REGION(Prog, 0x02000000, 10 * k1MB) MEM_REGION(SHA, 0x02A00000, k1MB) -MEM_REGION(WOM, 0x02B00000, 21 * k1MB) -MEM_REGION(Output, 0x02B00000, 20 * k1MB) -MEM_REGION(Commit, 0x03F00000, k1MB) -MEM_REGION(MUL, 0x04000000, k1MB) +MEM_REGION(MUL, 0x02B00000, k1MB) +MEM_REGION(WOM, 0x02C00000, 21 * k1MB) +MEM_REGION(Output, 0x02C00000, 20 * k1MB) +MEM_REGION(Commit, 0x04000000, k1MB) // clang-format on #define PTR_TO(type, name) reinterpret_cast(kMem##name##Start); diff --git a/risc0/zkvm/platform/risc0.ld b/risc0/zkvm/platform/risc0.ld index 7e5678db1b..7a60a5a2cb 100644 --- a/risc0/zkvm/platform/risc0.ld +++ b/risc0/zkvm/platform/risc0.ld @@ -29,8 +29,8 @@ MEMORY { gpio : ORIGIN = 0x01F00000, LENGTH = 1M prog (X) : ORIGIN = 0x02000000, LENGTH = 10M sha : ORIGIN = 0x02A00000, LENGTH = 1M - wom : ORIGIN = 0x02B00000, LENGTH = 21M - mul : ORIGIN = 0x04000000, LENGTH = 1M + mul : ORIGIN = 0x02B00000, LENGTH = 1M + wom : ORIGIN = 0x02C00000, LENGTH = 21M } SECTIONS { diff --git a/risc0/zkvm/sdk/rust/guest/src/mul.rs b/risc0/zkvm/sdk/rust/guest/src/mul.rs index f95cff644d..5fb7401aea 100644 --- a/risc0/zkvm/sdk/rust/guest/src/mul.rs +++ b/risc0/zkvm/sdk/rust/guest/src/mul.rs @@ -1,7 +1,10 @@ use core::{cell::UnsafeCell, mem}; use _alloc::{boxed::Box, vec::Vec}; -use risc0_zkvm::platform::{io::{MulDescriptor, GPIO_MUL}, memory}; +use risc0_zkvm::platform::{ + io::{MulDescriptor, GPIO_MUL}, + memory, +}; // Current sha descriptor index. struct CurOutput(UnsafeCell); @@ -11,8 +14,20 @@ unsafe impl Sync for CurOutput {} static CUR_OUTPUT: CurOutput = CurOutput(UnsafeCell::new(0)); +/// Result of multiply goldilocks pub struct MulGoldilocks([u32; 2]); +impl MulGoldilocks { + /// Get the result as u64 + pub fn get_u64(&self) -> u64 { + let mut res = 0u64; + for i in 0..2 { + res |= (self.0[i] as u64) << (32 * i); + } + res + } +} + fn alloc_output() -> *mut MulDescriptor { // SAFETY: Single threaded and this is the only place we use CUR_DESC. unsafe { @@ -23,15 +38,16 @@ fn alloc_output() -> *mut MulDescriptor { } } +/// Multiply goldilocks oracle, verification is done separately pub fn mul_goldilocks(a: &u64, b: &u64) -> &'static MulGoldilocks { // Allocate fresh memory that's guaranteed to be uninitialized so // the host can write to it. let mut buf = Vec::::with_capacity(4); - let a_hi = (u32)((a & 0xFFFFFFFF00000000LL) >> 32); - let a_lo = (u32)(a & 0xFFFFFFFFLL); + let a_hi = ((a & 0xFFFFFFFF00000000) >> 32) as u32; + let a_lo = (a & 0xFFFFFFFF) as u32; - let b_hi = (u32)((b & 0xFFFFFFFF00000000LL) >> 32); - let b_lo = (u32)(b & 0xFFFFFFFFLL); + let b_hi = ((b & 0xFFFFFFFF00000000) >> 32) as u32; + let b_lo = (b & 0xFFFFFFFF) as u32; buf.push(a_hi); buf.push(a_lo); diff --git a/risc0/zkvm/sdk/rust/platform/src/memory.rs b/risc0/zkvm/sdk/rust/platform/src/memory.rs index df658de1fe..c491a07a6c 100644 --- a/risc0/zkvm/sdk/rust/platform/src/memory.rs +++ b/risc0/zkvm/sdk/rust/platform/src/memory.rs @@ -62,7 +62,7 @@ pub const INPUT: Region = Region::new(0x01E0_0000, mb(1)); pub const GPIO: Region = Region::new(0x01F0_0000, mb(1)); pub const PROG: Region = Region::new(0x0200_0000, mb(10)); pub const SHA: Region = Region::new(0x02A0_0000, mb(1)); -pub const WOM: Region = Region::new(0x02B0_0000, mb(21)); -pub const OUTPUT: Region = Region::new(0x02B0_0000, mb(20)); -pub const COMMIT: Region = Region::new(0x03F0_0000, mb(1)); -pub const MUL: Region = Region::new(0x0400_0000, mb(1)); +pub const MUL: Region = Region::new(0x02B0_0000, mb(1)); +pub const WOM: Region = Region::new(0x02C0_0000, mb(21)); +pub const OUTPUT: Region = Region::new(0x02C0_0000, mb(20)); +pub const COMMIT: Region = Region::new(0x0400_0000, mb(1)); From aa258322625777d7da92473438b6b58d54d0495c Mon Sep 17 00:00:00 2001 From: cpunkzzz Date: Mon, 3 Oct 2022 20:32:43 -0400 Subject: [PATCH 3/5] Solved memory overflow --- risc0/zkvm/platform/memory.h | 6 +++--- risc0/zkvm/platform/risc0.ld | 2 +- risc0/zkvm/sdk/rust/platform/src/memory.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/risc0/zkvm/platform/memory.h b/risc0/zkvm/platform/memory.h index c3fe992ba3..66723d37f3 100644 --- a/risc0/zkvm/platform/memory.h +++ b/risc0/zkvm/platform/memory.h @@ -44,9 +44,9 @@ MEM_REGION(GPIO, 0x01F00000, k1MB) MEM_REGION(Prog, 0x02000000, 10 * k1MB) MEM_REGION(SHA, 0x02A00000, k1MB) MEM_REGION(MUL, 0x02B00000, k1MB) -MEM_REGION(WOM, 0x02C00000, 21 * k1MB) -MEM_REGION(Output, 0x02C00000, 20 * k1MB) -MEM_REGION(Commit, 0x04000000, k1MB) +MEM_REGION(WOM, 0x02C00000, 20 * k1MB) +MEM_REGION(Output, 0x02C00000, 19 * k1MB) +MEM_REGION(Commit, 0x03F00000, k1MB) // clang-format on #define PTR_TO(type, name) reinterpret_cast(kMem##name##Start); diff --git a/risc0/zkvm/platform/risc0.ld b/risc0/zkvm/platform/risc0.ld index 7a60a5a2cb..7b51400427 100644 --- a/risc0/zkvm/platform/risc0.ld +++ b/risc0/zkvm/platform/risc0.ld @@ -30,7 +30,7 @@ MEMORY { prog (X) : ORIGIN = 0x02000000, LENGTH = 10M sha : ORIGIN = 0x02A00000, LENGTH = 1M mul : ORIGIN = 0x02B00000, LENGTH = 1M - wom : ORIGIN = 0x02C00000, LENGTH = 21M + wom : ORIGIN = 0x02C00000, LENGTH = 20M } SECTIONS { diff --git a/risc0/zkvm/sdk/rust/platform/src/memory.rs b/risc0/zkvm/sdk/rust/platform/src/memory.rs index c491a07a6c..5b2fea9209 100644 --- a/risc0/zkvm/sdk/rust/platform/src/memory.rs +++ b/risc0/zkvm/sdk/rust/platform/src/memory.rs @@ -63,6 +63,6 @@ pub const GPIO: Region = Region::new(0x01F0_0000, mb(1)); pub const PROG: Region = Region::new(0x0200_0000, mb(10)); pub const SHA: Region = Region::new(0x02A0_0000, mb(1)); pub const MUL: Region = Region::new(0x02B0_0000, mb(1)); -pub const WOM: Region = Region::new(0x02C0_0000, mb(21)); -pub const OUTPUT: Region = Region::new(0x02C0_0000, mb(20)); -pub const COMMIT: Region = Region::new(0x0400_0000, mb(1)); +pub const WOM: Region = Region::new(0x02C0_0000, mb(20)); +pub const OUTPUT: Region = Region::new(0x02C0_0000, mb(19)); +pub const COMMIT: Region = Region::new(0x03F0_0000, mb(1)); From e23deab759c00849329ef9b469271b36ce3a99c0 Mon Sep 17 00:00:00 2001 From: cpunkzzz Date: Tue, 4 Oct 2022 20:28:18 -0400 Subject: [PATCH 4/5] fix alignment --- risc0/zkvm/prove/io_handler.cpp | 26 ++++++++++---------------- risc0/zkvm/sdk/rust/guest/src/mul.rs | 8 +++----- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/risc0/zkvm/prove/io_handler.cpp b/risc0/zkvm/prove/io_handler.cpp index 73a681d7d4..d34070f20d 100644 --- a/risc0/zkvm/prove/io_handler.cpp +++ b/risc0/zkvm/prove/io_handler.cpp @@ -47,23 +47,17 @@ static void processSHA(MemoryState& mem, const ShaDescriptor& desc) { } static void processMul(MemoryState& mem, const MulDescriptor& desc) { - uint32_t first_operand[2]; - uint32_t second_operand[2]; + uint32_t a_hi = mem.load(desc.source); + LOG(1, "Input[" << hex(0, 2) << "]: " << hex(desc.source) << " -> " << hex(a_hi)); + uint32_t a_lo = mem.load(desc.source + 4); + LOG(1, "Input[" << hex(1, 2) << "]: " << hex(desc.source + 4) << " -> " << hex(a_lo)); + uint32_t b_hi = mem.load(desc.source + 8); + LOG(1, "Input[" << hex(2, 2) << "]: " << hex(desc.source + 8) << " -> " << hex(b_hi)); + uint32_t b_lo = mem.load(desc.source + 12); + LOG(1, "Input[" << hex(3, 2) << "]: " << hex(desc.source + 12) << " -> " << hex(b_lo)); - first_operand[0] = mem.loadBE(desc.source); - LOG(1, "Input[" << hex(0, 2) << "]: " << hex(desc.source) << " -> " << hex(first_operand[0])); - first_operand[1] = mem.loadBE(desc.source + 4); - LOG(1, "Input[" << hex(1, 2) << "]: " << hex(desc.source + 4) << " -> " << hex(first_operand[1])); - second_operand[0] = mem.loadBE(desc.source + 8); - LOG(1, - "Input[" << hex(2, 2) << "]: " << hex(desc.source + 8) << " -> " << hex(second_operand[0])); - second_operand[1] = mem.loadBE(desc.source + 12); - LOG(1, - "Input[" << hex(3, 2) << "]: " << hex(desc.source + 12) << " -> " << hex(second_operand[1])); - - // MSB is at 0 - uint64_t first = first_operand[1] | (uint64_t(first_operand[0]) << 32); - uint64_t second = second_operand[1] | (uint64_t(second_operand[0]) << 32); + uint64_t first = a_lo | (uint64_t(a_hi) << 32); + uint64_t second = b_lo | (uint64_t(b_hi) << 32); __uint128_t result = __uint128_t(first) * __uint128_t(second); diff --git a/risc0/zkvm/sdk/rust/guest/src/mul.rs b/risc0/zkvm/sdk/rust/guest/src/mul.rs index 5fb7401aea..73299ed4fe 100644 --- a/risc0/zkvm/sdk/rust/guest/src/mul.rs +++ b/risc0/zkvm/sdk/rust/guest/src/mul.rs @@ -1,5 +1,7 @@ use core::{cell::UnsafeCell, mem}; +use crate::env::log; +use _alloc::format; use _alloc::{boxed::Box, vec::Vec}; use risc0_zkvm::platform::{ io::{MulDescriptor, GPIO_MUL}, @@ -20,11 +22,7 @@ pub struct MulGoldilocks([u32; 2]); impl MulGoldilocks { /// Get the result as u64 pub fn get_u64(&self) -> u64 { - let mut res = 0u64; - for i in 0..2 { - res |= (self.0[i] as u64) << (32 * i); - } - res + (self.0[1] as u64) | ((self.0[0] as u64) << 32) } } From a9945a43f86fb0da1ce05cfb3695ff59d1643d2d Mon Sep 17 00:00:00 2001 From: cpunkzzz Date: Wed, 5 Oct 2022 20:46:32 -0400 Subject: [PATCH 5/5] slight improvement --- risc0/zkvm/sdk/rust/guest/src/mul.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/risc0/zkvm/sdk/rust/guest/src/mul.rs b/risc0/zkvm/sdk/rust/guest/src/mul.rs index 73299ed4fe..22593744f7 100644 --- a/risc0/zkvm/sdk/rust/guest/src/mul.rs +++ b/risc0/zkvm/sdk/rust/guest/src/mul.rs @@ -38,19 +38,13 @@ fn alloc_output() -> *mut MulDescriptor { /// Multiply goldilocks oracle, verification is done separately pub fn mul_goldilocks(a: &u64, b: &u64) -> &'static MulGoldilocks { - // Allocate fresh memory that's guaranteed to be uninitialized so - // the host can write to it. - let mut buf = Vec::::with_capacity(4); let a_hi = ((a & 0xFFFFFFFF00000000) >> 32) as u32; let a_lo = (a & 0xFFFFFFFF) as u32; let b_hi = ((b & 0xFFFFFFFF00000000) >> 32) as u32; let b_lo = (b & 0xFFFFFFFF) as u32; - buf.push(a_hi); - buf.push(a_lo); - buf.push(b_hi); - buf.push(b_lo); + let buf = [a_hi, a_lo, b_hi, b_lo]; unsafe { let alloced = Box::>::new(