Skip to content

Commit

Permalink
Full rewrite: use base32 polynomial hash to represent words
Browse files Browse the repository at this point in the history
Refactor because dictionary comparison in very expensive, using polynomial hashes reduces those by a lot, but makes extracting bits harder, so use base32 and bitify.

# of Wires: 23122 +0ms
# of Constraints: 22172 +0ms
# of Private Inputs: 7 +0ms
# of Public Inputs: 1000 +0ms
# of Labels: 76462 +0ms

Made me revaluate life :)
  • Loading branch information
nalinbhardwaj committed Aug 19, 2021
1 parent 2019af4 commit e59ed70
Show file tree
Hide file tree
Showing 9 changed files with 1,111 additions and 177 deletions.
59 changes: 11 additions & 48 deletions circuits/lines.circom
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
include "../node_modules/circomlib/circuits/gates.circom"
include "../node_modules/circomlib/circuits/comparators.circom"
include "words.circom"

template IsContinual(LINE_SIZE, WORD_SIZE) {
template IsContinual(LINE_SIZE, WORD_SIZE, BIT_SIZE) {
signal input words[LINE_SIZE][WORD_SIZE];
signal input padding[LINE_SIZE];
signal output out;

component is_prefix_continual[LINE_SIZE];
component is_index_continued[LINE_SIZE];
component is_padding_or_continued[LINE_SIZE];
component words_last_char[LINE_SIZE];

for (var i = 0;i < LINE_SIZE;i++) {
is_prefix_continual[i] = AND();
is_index_continued[i] = IsEqual();
is_padding_or_continued[i] = OR();
words_last_char[i] = ExtractLastChar(WORD_SIZE, BIT_SIZE);
}

for (var i = 0;i < LINE_SIZE;i++) {
for (var j = 0;j < WORD_SIZE;j++) {
words_last_char[i].word[j] <== words[i][j];
}
}

for (var i = 0;i < LINE_SIZE;i++) {
is_index_continued[i].in[0] <== words[i][0];
is_index_continued[i].in[1] <== (i == 0) ? words[i][0] : words[i-1][WORD_SIZE-1];
is_index_continued[i].in[1] <== (i == 0) ? words[i][0] : words_last_char[i-1].out;

is_padding_or_continued[i].a <== is_index_continued[i].out;
is_padding_or_continued[i].b <== padding[i];
Expand All @@ -29,49 +38,3 @@ template IsContinual(LINE_SIZE, WORD_SIZE) {

out <== is_prefix_continual[LINE_SIZE-1].out;
}

template IsNotCrossing(WORD_SIZE, FIG_SIZE, SIDE_SIZE) {
signal input word[WORD_SIZE];
signal input figure[FIG_SIZE][SIDE_SIZE];
signal output out;

component are_all_not_crossing = MultiAND((WORD_SIZE-1)*FIG_SIZE*SIDE_SIZE*SIDE_SIZE);
component is_char_same[WORD_SIZE-1][FIG_SIZE][SIDE_SIZE][SIDE_SIZE][2];
component is_prefix_crossing[WORD_SIZE-1][FIG_SIZE][SIDE_SIZE][SIDE_SIZE];
component is_prefix_not_crossing[WORD_SIZE-1][FIG_SIZE][SIDE_SIZE][SIDE_SIZE];

for (var i = 0;i < WORD_SIZE-1;i++) {
for (var j = 0;j < FIG_SIZE;j++) {
for (var k = 0;k < SIDE_SIZE;k++) {
for (var l = 0;l < SIDE_SIZE;l++) {
for (var m = 0;m < 2;m++) is_char_same[i][j][k][l][m] = IsEqual();
is_prefix_crossing[i][j][k][l] = AND();
is_prefix_not_crossing[i][j][k][l] = NOT();
}
}
}
}

var idx = 0;
for (var i = 0;i < WORD_SIZE-1;i++) {
for (var j = 0;j < FIG_SIZE;j++) {
for (var k = 0;k < SIDE_SIZE;k++) {
for (var l = 0;l < SIDE_SIZE;l++) {
is_char_same[i][j][k][l][0].in[0] <== word[i];
is_char_same[i][j][k][l][0].in[1] <== figure[j][k];
is_char_same[i][j][k][l][1].in[0] <== word[i+1];
is_char_same[i][j][k][l][1].in[1] <== figure[j][l];


is_prefix_crossing[i][j][k][l].a <== is_char_same[i][j][k][l][0].out;
is_prefix_crossing[i][j][k][l].b <== is_char_same[i][j][k][l][1].out;
is_prefix_not_crossing[i][j][k][l].in <== is_prefix_crossing[i][j][k][l].out;
are_all_not_crossing.in[idx] <== is_prefix_not_crossing[i][j][k][l].out;
idx++;
}
}
}
}

out <== are_all_not_crossing.out;
}
58 changes: 14 additions & 44 deletions circuits/main.circom
Original file line number Diff line number Diff line change
@@ -1,73 +1,43 @@
include "words.circom"
include "lines.circom"

template Main(LINE_SIZE, DICTIONARY_SIZE, WORD_SIZE, FIG_SIZE, SIDE_SIZE) {
signal private input line[LINE_SIZE][WORD_SIZE];
signal input figure[FIG_SIZE][SIDE_SIZE];
signal input dictionary[DICTIONARY_SIZE][WORD_SIZE];
template Main(LINE_SIZE, WORD_SIZE, BIT_SIZE, DICTIONARY_SIZE, FIG_SIZE, SIDE_SIZE) {
signal private input line[LINE_SIZE];
signal input dictionary[DICTIONARY_SIZE];

component extractor[LINE_SIZE];
for (var i = 0;i < LINE_SIZE;i++) extractor[i] = CharExtractor(WORD_SIZE, 5);
for (var i = 0;i < LINE_SIZE;i++) extractor[i].poly <== line[i];

component is_padding[LINE_SIZE];
for (var i = 0;i < LINE_SIZE;i++) is_padding[i] = IsPadding();
for (var i = 0;i < LINE_SIZE;i++) {
is_padding[i].first_char <== line[i][0];
}

component validator[LINE_SIZE];
component validator_or_padding[LINE_SIZE];
for (var i = 0;i < LINE_SIZE;i++) {
validator[i] = IsWordValid(WORD_SIZE);
validator_or_padding[i] = OR();
}
for (var i = 0;i < LINE_SIZE;i++) {
for (var j = 0;j < WORD_SIZE;j++) validator[i].word[j] <== line[i][j];
validator_or_padding[i].a <== validator[i].out;
validator_or_padding[i].b <== is_padding[i].out;
validator_or_padding[i].out === 1;
is_padding[i].first_char <== extractor[i].out[0];
}

component checker[LINE_SIZE];
component checker_or_padding[LINE_SIZE];
for (var i = 0;i < LINE_SIZE;i++) {
checker[i] = InDictionary(DICTIONARY_SIZE, WORD_SIZE);
checker[i] = InDictionary(DICTIONARY_SIZE);
checker_or_padding[i] = OR();
}
for (var i = 0;i < LINE_SIZE;i++) {
for (var j = 0;j < DICTIONARY_SIZE;j++) {
for (var k = 0;k < WORD_SIZE;k++) {
checker[i].dictionary[j][k] <== dictionary[j][k];
}
checker[i].dictionary[j] <== dictionary[j];
}

for (var j = 0;j < WORD_SIZE;j++) {
checker[i].word[j] <== line[i][j];
}
checker[i].word <== line[i];
checker_or_padding[i].a <== checker[i].out;
checker_or_padding[i].b <== is_padding[i].out;
checker_or_padding[i].out === 1;
}

component continuity = IsContinual(LINE_SIZE, WORD_SIZE);
component continuity = IsContinual(LINE_SIZE, WORD_SIZE, BIT_SIZE);
for (var i = 0;i < LINE_SIZE;i++) {
continuity.padding[i] <== is_padding[i].out;
for (var j = 0;j < WORD_SIZE;j++) continuity.words[i][j] <== line[i][j];
for (var j = 0;j < WORD_SIZE;j++) continuity.words[i][j] <== extractor[i].out[j];
}
continuity.out === 1;

component crossing[LINE_SIZE];
component crossing_or_padding[LINE_SIZE];
for (var i = 0;i < LINE_SIZE;i++) {
crossing[i] = IsNotCrossing(WORD_SIZE, FIG_SIZE, SIDE_SIZE);
crossing_or_padding[i] = OR();
}
for (var i = 0;i < LINE_SIZE;i++) {
for (var j = 0;j < WORD_SIZE;j++) crossing[i].word[j] <== line[i][j];
for (var j = 0;j < FIG_SIZE;j++) {
for (var k = 0;k < SIDE_SIZE;k++) crossing[i].figure[j][k] <== figure[j][k];
}
crossing_or_padding[i].a <== crossing[i].out;
crossing_or_padding[i].b <== is_padding[i].out;
crossing_or_padding[i].out === 1;
}
}

component main = Main(3, 3, 3, 3, 2);
component main = Main(7, 6, 5, 1000, 4, 3);
6 changes: 1 addition & 5 deletions circuits/main.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"line": [[2, 0, 1], [1, 0, 2], [28, 28, 28]],
"figure": [[0, 3], [1, 4], [2, 5]],
"dictionary": [[2, 0, 1], [0, 1, 27], [1, 0, 2]]
}
{"dictionary": [72642427, 72773499, 72970107, 73822075, 74018683, 75689851, 75722619, 75866764, 75970427, 76115835, 76095910, 76096140, 78933883, 78907739, 78907730, 78919547, 78918875, 78918866, 78918914, 78919003, 78918994, 78920571, 79130491, 79104347, 79104338, 79116155, 79115483, 79115522, 79115611, 79115602, 79115730, 79115922, 79121275, 79123323, 79123003, 79122994, 79228795, 80080763, 81863099, 81865595, 81865307, 81869691, 81869403, 81875547, 82057083, 82056795, 82057659, 82063227, 82062939, 82065275, 82064603, 82125222, 82177915, 82158459, 82157659, 82158011, 82157990, 82158226, 82164155, 82164136, 82164283, 82164274, 82164369, 82168699, 82210683, 82194299, 82197138, 82201467, 82203227, 82243451, 82220923, 82220603, 82221947, 82223995, 82223195, 82223547, 82223526, 82223756, 82226043, 82225755, 82226459, 82229819, 82235995, 82374523, 82351995, 82352411, 82352402, 82357115, 82356827, 82360187, 82359554, 82359565, 82359771, 82359963, 82359954, 82360699, 82360973, 82364891, 82364882, 82366542, 82407291, 82387149, 82387387, 82387366, 82391515, 82391506, 82462299, 82462989, 85028731, 85323643, 85421947, 85396347, 85395739, 85395725, 85395803, 85395790, 85395794, 85396114, 85402203, 85406587, 85407067, 85407378, 85553019, 85618555, 85592338, 85592411, 85603195, 85603675, 85604923, 85604914, 85609339, 88157051, 88252507, 88348539, 88348251, 88356059, 88502139, 88488315, 88534907, 88514920, 88553339, 88665979, 88643451, 88643163, 88649595, 88649307, 88651643, 88656763, 88656337, 88656530, 88658478, 88681339, 88681051, 176234939, 176241531, 176247387, 176333222, 176385915, 176376699, 176497062, 176549755, 176530299, 176529830, 176530066, 176534395, 176534107, 176536443, 176540539, 176615291, 176593563, 176595835, 176595018, 176595366, 176595547, 176597883, 176597595, 176601979, 176761723, 179571579, 179571035, 179571026, 179582171, 179582162, 179587419, 179793787, 179768187, 179767643, 179767634, 179779451, 179778779, 179778770, 179783547, 179786299, 179786290, 186085243, 186059099, 186059090, 186067213, 186068226, 186069883, 186075483, 186216315, 186260347, 186260059, 188818299, 188820347, 188820059, 188824210, 188830299, 188968827, 188948955, 188948946, 188959611, 188961659, 189165435, 189151890, 189198203, 189176091, 189176066, 189176077, 189176466, 189177947, 189178459, 189180795, 189180507, 189181211, 189329275, 189306513, 189312891, 189312603, 189315330, 189315341, 189315643, 189315634, 189320059, 189342075, 189342066, 189342118, 189342157, 189344635, 189354587, 209907579, 209940347, 209920891, 209923342, 209931131, 209932724, 210104187, 210090875, 210090641, 210094971, 210169723, 210148219, 210147662, 210152315, 210152027, 210202491, 210333563, 210317787, 210317778, 213125467, 213136731, 213142395, 213141851, 213332859, 213332571, 213332626, 213337979, 213339003, 215429581, 215435538, 215641979, 215632763, 219344763, 219639675, 219619771, 219630459, 219631803, 219770747, 219813755, 219814452, 219816539, 219820923, 219821947, 219827067, 222374779, 222374171, 222374491, 222506254, 222516091, 222719867, 222710651, 222752635, 222731131, 222735227, 222734939, 222736251, 222738854, 222744998, 222883707, 222866299, 222867323, 222876539, 222876251, 222916475, 270808283, 270808274, 271005563, 271004930, 274296699, 275181435, 275161691, 279394171, 280234242, 280234253, 280246715, 280252827, 280439259, 282259323, 282239340, 282239419, 282242939, 282292091, 282343867, 282423163, 282403238, 282409563, 282409544, 282413947, 282554235, 282543323, 282544386, 282544397, 282639227, 282638715, 282644923, 282740155, 283570043, 283550587, 283549787, 283550284, 283550348, 283733883, 283799419, 287585499, 287591291, 287698811, 287678886, 287689595, 287731579, 287722363, 287797115, 287780283, 287983250, 342421371, 344009595, 344009307, 344125307, 344116091, 344269691, 344275227, 344321915, 344307579, 344308603, 344308123, 344308315, 344387451, 344365947, 344365659, 344365723, 344367195, 344367547, 344370043, 344369755, 344374139, 344373851, 344402395, 344410459, 344410450, 349440347, 349636955, 349636946, 349833554, 349849467, 349848955, 349848946, 349849179, 350515067, 350498683, 350498253, 350500301, 350560635, 350560731, 350567291, 350567003, 350613371, 350593915, 350593627, 350600059, 350599771, 350603917, 350678907, 350657403, 350657179, 350658984, 350809979, 350800763, 350802363, 350842747, 350822843, 350829435, 350828966, 350829147, 350908283, 350897608, 350897618, 350898445, 353842641, 353842642, 353842833, 353988475, 354054011, 354709371, 371617659, 377564027, 377563739, 377567789, 377567889, 377679739, 377712507, 377697549, 377697932, 377823675, 377823654, 377828813, 377856652, 377941883, 377920379, 377920091, 377921979, 377921958, 377924475, 377924187, 377928571, 377928123, 377928283, 377928338, 377934235, 377934226, 377934427, 377960315, 378105723, 378088315, 383315835, 383872891, 383853435, 383855483, 383855195, 383859579, 383859291, 383865243, 383865234, 383865435, 384055163, 384060283, 384115579, 384148347, 384148108, 384154491, 384211835, 384211547, 384211602, 384215931, 384364411, 384350075, 384351099, 384350811, 384355195, 384356763, 384377254, 384462715, 384442971, 384448379, 384452475, 384452187, 404942715, 404975483, 408481659, 409530235, 413757307, 416567707, 417427323, 417409915, 417409627, 417414011, 417413194, 417419338, 417623931, 417607547, 417607259, 417610619, 417614235, 417614285, 417670011, 417676155, 417722235, 417702310, 417713019, 417755003, 417741691, 417745787, 417747547, 417787771, 417766267, 417767515, 417768012, 417770363, 417770075, 417774459, 417774171, 417918843, 417897339, 417900813, 417904507, 417905531, 417904731, 417905083, 417905062, 417905211, 417909627, 417911387, 417951611, 417929883, 417934203, 417933915, 417944123, 418017147, 418002811, 418006459, 421752699, 421803355, 421916539, 423698523, 423698875, 423701371, 423701083, 423705147, 423705138, 423705234, 423708123, 423711323, 423849851, 423829883, 423840635, 424046459, 424037243, 424079227, 424057723, 424058971, 424097659, 424096987, 424097026, 424192891, 424192603, 424193293, 424243067, 424221339, 424222811, 424222798, 424223142, 424223181, 424225659, 424225371, 424226061, 424227277, 424229755, 424232395, 444670843, 444672891, 444672603, 444676475, 444676462, 444676466, 444768347, 444821371, 444938683, 444985211, 444971666, 444975995, 445029243, 445036987, 445037147, 445214587, 445195131, 445207131, 450964347, 450964059, 451161979, 451161691, 451276667, 451263355, 451263067, 451309435, 451295515, 451295835, 451300219, 451342203, 451320699, 451321947, 451322317, 451322514, 451328258, 451331725, 451473275, 451455867, 451458939, 451463515, 451506043, 451485915, 451486118, 451486284, 451498875, 451571579, 451557243, 451562363, 457253307, 457600891, 457633659, 457615643, 457615963, 457748347, 457748059, 457797499, 472215419, 472234058, 478227035, 478506875, 478497659, 478539643, 478519718, 478530427, 478587771, 478587483, 478749115, 480538491, 480820091, 480825563, 481573755, 481573467, 481718139, 481773435, 481900987, 482780027, 482779788, 482963323, 483684219, 483670459, 483676571, 483736155, 483807675, 483880827, 483865874, 484068219, 487681915, 487662011, 487780219, 487812987, 487792731, 487805195, 487976827, 487963515, 487963067, 487963227, 488022619, 488059355, 488206203, 488192443, 488927099, 489012091, 489044347, 489044338, 489044626, 490925947, 491024251, 491319163, 491304827, 491309947, 576286587, 585199483, 585182075, 585181787, 585185358, 585185646, 585396091, 585379707, 585379419, 585381755, 585461627, 585471867, 585527163, 585517947, 585559931, 585538427, 585540027, 585540045, 585723771, 585703867, 585703846, 585709928, 585763284, 585774971, 585780091, 589968251, 591473531, 591473243, 591581787, 591622011, 591612795, 591673211, 591672539, 591818619, 591800589, 591804987, 591804978, 591805073, 591809403, 591851387, 591829883, 591829595, 591831131, 591975291, 592015227, 591995323, 591997819, 591997197, 591997531, 609644411, 612462459, 612445051, 612444763, 612593531, 612573563, 612573554, 612576712, 612576722, 612579195, 612578760, 612578765, 612578769, 612578956, 612757371, 612737102, 612744059, 612743771, 612822907, 612801403, 612801115, 612802651, 612802638, 612803148, 612805499, 612805211, 612809147, 612815451, 612839291, 612841339, 612986747, 612967291, 612968717, 613036923, 613036635, 613042459, 614756219, 614739835, 614741883, 614741339, 614741330, 614747003, 614936443, 615778651, 615778642, 615787387, 615789787, 615789778, 615789915, 615789906, 615975259, 615987067, 615986523, 615993915, 616951675, 617023835, 617023826, 617032155, 617032146, 617042523, 617225083, 617236347, 617235794, 618753915, 618734027, 618734226, 618736507, 618736091, 618736219, 618740603, 618928571, 618934139, 618933851, 619016059, 619002747, 619002184, 619048827, 619029371, 619028902, 619028955, 619028941, 619034491, 619035515, 619035067, 619035227, 619039611, 619067026, 619114363, 619092859, 619092571, 619094107, 619094438, 619094484, 619096955, 619231099, 619237979, 619278203, 619264891, 619343739, 619328379, 619333499, 619333211, 619334523, 625025915, 625027963, 625027675, 625143675, 625124219, 625123916, 625156987, 625156699, 625241979, 625226971, 625373051, 625359739, 625359451, 625363835, 625405819, 625384315, 625383790, 625385851, 625388411, 625536891, 625514363, 625514075, 625515387, 625569659, 625550203, 625552650, 625562491, 625562004, 676949883, 677749627, 677749339, 679694907, 684355451, 684892524, 684905019, 684905003, 684945275, 684937490, 684937627, 685010811, 684984972, 684987650, 684997499, 684997051, 685001115, 685001165, 685003355, 685056443, 685056422, 685056426, 685109115, 685089194, 685094986, 685187451, 685196731, 685298107, 685298086, 685388667, 685393339, 689106811, 689119654, 689283494, 689368955, 689349030, 689349196, 689401723, 689385339, 689386754, 689391890, 689532795, 689519035, 689525339, 689598331, 689572507, 689572492, 689578566, 689589115, 690238683, 690238674, 690305371, 690646907, 690632571, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324, 969831324], "line": [186069883, 424222811, 78907739, 344365947, 969831324, 969831324, 969831324]}
Binary file modified circuits/main.r1cs
Binary file not shown.
Binary file modified circuits/main.wasm
Binary file not shown.
Binary file modified circuits/main.zkey
Binary file not shown.
Loading

0 comments on commit e59ed70

Please sign in to comment.