Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modexp feijoa #377

Open
wants to merge 7 commits into
base: develop-feijoa
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 35 additions & 50 deletions main/modexp/array_lib/array_add_AGTB.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,27 @@
;; · out = inA + inB, with len(out) <= C + 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; function array_add_AGTB(a: bigint[], b: bigint[], base: bigint): bigint[] {
; const alen = a.length;
; const blen = b.length;
; let result = new Array<bigint>(alen);
; let sum = 0n;
; let carry = 0n;
; for (let i = 0; i < blen; i++) {
; sum = a[i] + b[i] + carry;
; carry = sum >= base ? 1n : 0n;
; out[i] = sum - carry * base;
; }
; for (let i = blen; i < alen; i++) {
; sum = a[i] + carry;
; carry = sum == base ? 1n : 0n; // the past carry is at most 1n
; out[i] = sum - carry * base;
; }

; if (carry === 1n) {
; result.push(carry);
; }
; return result;
; }
;; WARNING: This function is tailored for checking that a = q·b + r in the array_div_long function.
;; In can be used for other purposes except for the worst case, i.e. when len(inA) == %ARRAY_MAX_LEN_DOUBLED.
;; Specifically, there is an implicit assumption that the output len of this function cannot be
;; greater than %ARRAY_MAX_LEN_DOUBLED, and do not allow to generate the proof in such case.

; NOTE: It's unoptimized for the case where len(inB) = 1. Use array_add_short instead.

; code
; --------------------------
; first_iteration <-- Compute a[0] + b[0]
; while(inB_index_check) { <-- While 0 < i < len(b)
; loop2inB <-- Compute a[i] + b[i] + carry
; }
; while (inA_index_check) { <-- While 0 < i < len(a)
; loop2inA <-- Compute a[i] + carry
; }
; 1] check_carry <-- If there is a carry, append it to the result
; 2] trim <-- Otherwise, trim the result
; end
; --------------------------

VAR GLOBAL array_add_AGTB_inA[%ARRAY_MAX_LEN_DOUBLED]
VAR GLOBAL array_add_AGTB_inB[%ARRAY_MAX_LEN]
VAR GLOBAL array_add_AGTB_out[%ARRAY_MAX_LEN_DOUBLED] ; This cannot be bigger because we use it for division checking
Expand Down Expand Up @@ -71,10 +67,11 @@ array_add_AGTB_first_iteration:
;-----------------

1 => E
$ => B :MLOAD(array_add_AGTB_len_inB)
B - E :JMPZ(array_add_AGTB_loop_index_check)

array_add_AGTB_loopZero2inB:
array_add_AGTB_inB_index_check:
$ - E :F_MLOAD(array_add_AGTB_len_inB), JMPZ(array_add_AGTB_inA_index_check)

array_add_AGTB_loop2inB:
; a[i] + b[i], where a[i],b[i] ∈ [0,base-1]: This number cannot be GT base + (base - 2), two chunks
$ => A :MLOAD(array_add_AGTB_inA + E)
$ => B :MLOAD(array_add_AGTB_inB + E)
Expand All @@ -98,39 +95,28 @@ array_add_AGTB_loopZero2inB:

D :MSTORE(array_add_AGTB_carry)

E + 1 => E
$ => B :MLOAD(array_add_AGTB_len_inB)
B - E :JMPZ(array_add_AGTB_loop_index_check, array_add_AGTB_loopZero2inB)
E + 1 => E :JMP(array_add_AGTB_inB_index_check)

array_add_AGTB_loop_index_check:
$ => C :MLOAD(array_add_AGTB_len_inA)
C - E :JMPZ(array_add_AGTB_check_carry)
array_add_AGTB_inA_index_check:
$ - E :F_MLOAD(array_add_AGTB_len_inA), JMPZ(array_add_AGTB_check_carry)

array_add_AGTB_last_two_additions:
array_add_AGTB_loop2inA:
; sum = a[i] + carry: This number cannot be GT base, two chunks
; This sum can produce, at most, one additional carry
$ => A :MLOAD(array_add_AGTB_inA + E)
D => B
$ :ADD, MSTORE(array_add_AGTB_out + E), JMPNC(array_add_AGTB_set)

E + 1 => E
C - E :JMPZ(array_add_AGTB_is_carry)

$ => A :MLOAD(array_add_AGTB_inA + E)
1 => B
$ :ADD, MSTORE(array_add_AGTB_out + E)
$ :ADD, MSTORE(array_add_AGTB_out + E), JMPNC(__array_add_AGTB_continue_6)
;-----------------
1 => D :JMP(__array_add_AGTB_continue_7)
__array_add_AGTB_continue_6:
0 => D
__array_add_AGTB_continue_7:
;-----------------

array_add_AGTB_set:
E + 1 => E
$ => A :MLOAD(array_add_AGTB_len_inA)
A - E :JMPZ(array_add_AGTB_trim)
$ => A :MLOAD(array_add_AGTB_inA + E)
A :MSTORE(array_add_AGTB_out + E), JMP(array_add_AGTB_set)
E + 1 => E :JMP(array_add_AGTB_inA_index_check)

array_add_AGTB_check_carry:
D :JMPZ(array_add_AGTB_trim)

array_add_AGTB_is_carry:
; Carry path
E - %ARRAY_MAX_LEN_DOUBLED :JMPZ(failAssert)

Expand All @@ -142,5 +128,4 @@ array_add_AGTB_trim:
E :MSTORE(array_add_AGTB_len_out)

array_add_AGTB_end:
$ => RR :MLOAD(array_add_AGTB_RR)
:RETURN
$ => RR :MLOAD(array_add_AGTB_RR), RETURN
64 changes: 27 additions & 37 deletions main/modexp/array_lib/array_add_short.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@
;; · out = inA + inB, with len(out) <= C + 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; function array_add_short(a: bigint[], b: bigint, base: bigint): bigint[] {
; const alen = a.length;
; let result = new Array<bigint>(alen);
; let sum = 0n;
; let carry = b;
; for (let i = 0; i < alen; i++) {
; sum = a[i] + carry;
; carry = sum >= base ? 1n : 0n;
; out[i] = sum - carry * base;
; }

; if (carry === 1n) {
; result.push(carry);
; }
; return result;
;; WARNING: This function is tailored for checking that a = q·b + r in the array_div_short function.
;; In can be used for other purposes except for the worst case, i.e. when len(inA) == %ARRAY_MAX_LEN.
;; Specifically, there is an implicit assumption that the output len of this function cannot be
;; greater than %ARRAY_MAX_LEN, and do not allow to generate the proof in such case.

; code
; --------------------------
; first_iteration <-- Compute a[0] + b
; while(loop_index_check) { <-- While 0 < i < len(a)
; loop2inA <-- Compute a[i] + carry
; }
; 1] check_carry <-- If there is a carry, append it to the result
; 2] trim <-- Otherwise, trim the result
; end
; --------------------------

VAR GLOBAL array_add_short_inA[%ARRAY_MAX_LEN]
VAR GLOBAL array_add_short_inB
VAR GLOBAL array_add_short_out[%ARRAY_MAX_LEN_PLUS_ONE]
VAR GLOBAL array_add_short_out[%ARRAY_MAX_LEN] ; This cannot be bigger because we use it for division checking
VAR GLOBAL array_add_short_len_inA
VAR GLOBAL array_add_short_len_out

Expand All @@ -44,51 +43,43 @@ array_add_short:

C :MSTORE(array_add_short_len_inA)

; Load b
$ => B :MLOAD(array_add_short_inB)

array_add_short_first_iteration:
; The result will be stored as D·base + C

; a[0] + b, where a[i] ∈ [0,base-1]: the number cannot be GT base + (base - 2), two chunks
; a[0] + b, where a[0] ∈ [0,base-1]: the number cannot be GT base + (base - 2), two chunks
$ => A :MLOAD(array_add_short_inA)
$ => C :ADD, JMPC(__array_add_short_continue_1)
$ => B :MLOAD(array_add_short_inB)
$ :ADD, MSTORE(array_add_short_out), JMPC(__array_add_short_continue_1)
; ---------------------
0 => D :JMP(__array_add_short_continue_2)
__array_add_short_continue_1:
1 => D
__array_add_short_continue_2:
; ---------------------

C :MSTORE(array_add_short_out)

1 => E
$ => A :MLOAD(array_add_short_len_inA)
A - E :JMPZ(array_add_short_check_carry)

array_add_short_loopZero2inA:
; The result will be stored as D·base + C
array_add_short_loop_index_check:
; C = len_inA
C - E :JMPZ(array_add_short_check_carry)

array_add_short_loop2inA:
; a[i] + carry, where a[i] ∈ [0,base-1]: the number cannot be GT base, two chunks
$ => A :MLOAD(array_add_short_inA + E)
D => B
$ => C :ADD, JMPC(__array_add_short_continue_3)
$ :ADD, MSTORE(array_add_short_out + E), JMPC(__array_add_short_continue_3)
; ---------------------
0 => D :JMP(__array_add_short_continue_4)
__array_add_short_continue_3:
1 => D
__array_add_short_continue_4:
; ---------------------

C :MSTORE(array_add_short_out + E)

E + 1 => E
$ => A :MLOAD(array_add_short_len_inA)
A - E :JMPZ(array_add_short_check_carry, array_add_short_loopZero2inA)
E + 1 => E :JMP(array_add_short_loop_index_check)

array_add_short_check_carry:
D :JMPZ(array_add_short_trim)

; Carry path
E - %ARRAY_MAX_LEN :JMPZ(failAssert)

; In this case, the carry = 1 and we should append it to the result
1 :MSTORE(array_add_short_out + E)
Expand All @@ -98,5 +89,4 @@ array_add_short_trim:
E :MSTORE(array_add_short_len_out)

array_add_short_end:
$ => RR :MLOAD(array_add_short_RR)
:RETURN
$ => RR :MLOAD(array_add_short_RR), RETURN
Loading
Loading